Example #1
0
////////////////////////////////////////////////////////////////////////////////
// Set up the device (called by server thread).
int CameraV4L2::Setup()
{
    int err=0;
    int i;

    source = back_source;
    norm = back_norm;
    depth = back_depth;
    width = back_width;
    height = back_height;
    fieldType = back_fieldType;
    v4l2_type_id = back_v4l2_type_id;

    PLAYER_MSG1(0,"Camerav4l2: Setting up device %s", this->device);
    this->fg = fg2_createFrameGrabber();
    // set initial settings
    if (fg2_open(this->fg, this->device)!=0)
    {
        PLAYER_ERROR1("unable to open %s", this->device);
        fg2_delete(&fg);
        return -1;
    }

    PLAYER_MSG0(1,"Camerav4l2: device opened, aplying settings");

    err |= fg2_set_source(this->fg, this->source, this->sourceCh);
    err |= fg2_set_source_norm(this->fg, this->norm);
    if (err!=0){
        PLAYER_ERROR("Camerav4l2: failed source or norm");
        fg2_delete(&fg);
        return -1;
    }
    for(i=0; i<numOfCtls; i++){
        fg2_setControlValue(fg, ctlNames[i], ctlVals[i]);
    }

    if (fg2_setPixelSettings(fg, this->width, this->height, this->v4l2_type_id, this->fieldType, this->depth)==0) {
    } else {
        PLAYER_ERROR("Camerav4l2: nie udalo sie ustawic podanych parametrow");
        //fg2_delete(&fg);
        //return -1;
    }

    // finally start streaming with new settings
    if (fg2_startCapture(fg)!=0){
        fg2_delete(&fg);
        return -1;
    }

  // Start the driver thread.
    this->StartThread();

    return 0;
}
void V4L2CaptureStream::dispose()// throws CaptureException;
{	
	printf("V4L2CaptureStream::dispose()\n");
	//fg_close(fg); // TODO: we have to wait until the thread is done!
	//fg = 0;
	if (started)
	{
		disposing = true;
		printf("V4L2CaptureStream::dispose: waiting for thread to stop\n");
		fflush(stdout);
		while (!disposed)
		{	sleep(1); // TODO: sleep shorter period
		}
		printf("V4L2CaptureStream::dispose: thread stopped\n");
		fflush(stdout);
	}
	else
	{
		// thread not started, dispose ourselves.
		disposing = true;
		fg2_delete(&fg);
		if (rgbbuf != 0)
		{	delete[] rgbbuf;
			rgbbuf = 0;
		}
		disposed = true;
	}
	started = false;
}
Example #3
0
////////////////////////////////////////////////////////////////////////////////
// Shutdown the device (called by server thread).
int CameraV4L2::Shutdown()
{
    //printf("CameraV4L2::Shutdown()");
  // Stop the driver thread.
    StopThread();

    fg2_delete(&fg);
    return 0;
}
//static bool first = true;	// TODO: this is a hack for testing.
void V4L2CaptureStream::threadMain()// throws CaptureException;
{
	int res;

	//if (first)
	{	printf("V4L2CaptureStream::threadMain()\n");
		fflush(stdout);
		streamThrottle.waitUntilStart();
		//first = false;
		printf("V4L2CaptureStream streamThrottle.waitUntilStart completed\n");
		fflush(stdout);
	}




	res = fg2_startCapture(fg);
	if (res != 0)
		FailWithException("fg2_startCapture failed", res);

	while (!disposing)
	{
	struct my_buffer* frame = NULL;

	// TODO: support double-buffering.
//	printf("V4L2CaptureStream::fg2_grab...\n");
	//fflush(stdout);	
    frame = getFrameBuffer( fg );
//	printf("V4L2CaptureStream::fg2_grab: %lx\n", (unsigned long) frame);
	//fflush(stdout);	
    if (frame == 0)
    	FailWithException("getFrameBuffer failed", -1);	// TODO: notify observer instead.


	void *data = frame->start;
	int width = format.getWidth();
	int height = format.getHeight();

	switch (formatTypeV4L2)
	{
		// TODO: other formats
       case V4L2_PIX_FMT_RGB24:
			if (observer != 0)
			{	
				Image image = Image(format, (unsigned char *) data, width * height * 3);
				observer->onNewImage(this, &image);	
			}			
			break;
       case V4L2_PIX_FMT_YUYV:
			if (observer != 0)
			{	
				// 4 bytes = 2 pixels
				Image image = Image(format, yuyvToRGB((unsigned char *) data, width, height), width * height * 3, true);
				observer->onNewImage(this, &image);	
			}			
			break;
       case V4L2_PIX_FMT_RGB32:
			if (observer != 0)
			{	
				Image image = Image(format, (unsigned char *) data, width * height * 4);
				observer->onNewImage(this, &image);	
			}			
			break;
 		case V4L2_PIX_FMT_YUV420:
	 		{	if (rgbbufsize == 0)
	 			{	rgbbufsize = width * height * 3;
	 				rgbbuf = new unsigned char[rgbbufsize];
	 			}
				yuv2rgb_buf((unsigned char *) data, width, height, rgbbuf);
				if (observer != 0)
				{	
					Image image = Image(format, rgbbuf, rgbbufsize);
					observer->onNewImage(this, &image);	
				}			
				
			}
			break;
		default:
			printf("unknown or unsupported: %i\n", formatTypeV4L2);
			//FailWithException("unknown or unsupported format", formatTypeV4L2);
	}
	giveBackFrameBuffer(fg, frame);
	}
	res = fg2_stopCapture(fg);
	if (res != 0)
		FailWithException("fg2_stopCapture failed", res);

	fg2_delete(&fg);
	if (rgbbuf != 0)
	{	delete[] rgbbuf;
		rgbbuf = 0;
	}
	disposed = true;
	

}