Exemple #1
0
static gboolean
prepare_capture (GstOSXVideoSrc * self)
{
  ComponentResult err;

  err = SGSetChannelUsage (self->video_chan, seqGrabRecord);
  if (err != noErr) {
    GST_ERROR_OBJECT (self, "SGSetChannelUsage returned %d", (int) err);
    return FALSE;
  }

  err = SGSetDataProc (self->seq_grab, NewSGDataUPP (data_proc), (long) self);
  if (err != noErr) {
    GST_ERROR_OBJECT (self, "SGSetDataProc returned %d", (int) err);
    return FALSE;
  }

  err = SGPrepare (self->seq_grab, false, true);
  if (err != noErr) {
    GST_ERROR_OBJECT (self, "SGPrepare returnd %d", (int) err);
    return FALSE;
  }

  return TRUE;
}
// 1.
// CreateAndRunWithSequenceGrabber
void QuicktimeLiveImageStream::createAndRunWithSequenceGrabber(std::string fileName)
{
   std::string::size_type idx = fileName.find(':');
   if (idx == std::string::npos)
   {
       osg::notify(osg::FATAL) << "Error while parsing deviceID:deviceInputID.live path : " << fileName << std::endl;        
   }
   // Better c++ code is to use istrstream
   std::string deviceIDStr      = fileName.substr(0,idx);
   std::string deviceInputIDStr = fileName.substr(idx+1);
   m_videoDeviceID      = static_cast<short>(atoi(deviceIDStr.c_str()));
   m_videoDeviceInputID = static_cast<short>(atoi(deviceInputIDStr.c_str()));
   // Get Video Digitizer Rectangle bounds from a Sequence Grabber proxy (using IDs)
   get_video_device_bounds_idstr(m_videoDeviceID, m_videoDeviceInputID, m_videoRectWidth, m_videoRectHeight, m_videoDeviceIDStr);
   // Sound
   m_soundDeviceID = 2; m_soundDeviceInputID = 0;
   //get_sound_device_idstr(m_soundDeviceID, m_soundDeviceInputID, m_soundDeviceIDStr);
   // Create the Image
   createImage();
   // Create the offscreen GWorld (using Image  as target memory)
   createGWorld();
   // Create the Sequence Grabber (using GWorld as target memory)
   createSequenceGrabber();
   // Create the Sequence Grabber Video Channel
   createSequenceGrabberVideoChannel();
   if (g_s_use_sg_record)
   {
       // Create the Sequence Grabber DataProc setup for Record
       createSequenceGrabberDataProc();
   }
   // Create the Sequence Grabber Audio Channel
   createSequenceGrabberAudioChannel();
   // Start the engine Jack!
   // Callbacks
   createSequenceGrabberVideoBottlenecks();
   
   ComponentResult result = noErr;
   result = SGPrepare( m_gSeqGrabber, TRUE, FALSE);
   if (result != noErr)
       osg::notify(osg::FATAL) << "SGPrepare : error" << std::endl;
   if (g_s_use_sg_record)
   {
       result = SGStartRecord(m_gSeqGrabber);
       if (result != noErr)
           osg::notify(osg::FATAL) << "SGStartRecord : error" << std::endl;
   }
   else
   {
       result = SGStartPreview(m_gSeqGrabber);
       if (result != noErr)
           osg::notify(osg::FATAL) << "SGStartPreview : error" << std::endl;
   }
 
   _status = ImageStream::PLAYING;
   // Ticker
   start();
}
// ######################################################################
void QuickTimeGrabber::Impl::startStream()
{
  // lights...camera...
  if (noErr != SGPrepare(itsSeqGrab.it, false, true))
    LFATAL("SGPrepare() failed");

  // ...action
  if (noErr != SGStartRecord(itsSeqGrab.it))
    LFATAL("SGStartRecord() failed");

  itsStreamStarted = true;
}
Exemple #4
0
bool macsgCamera::initCamera(int width, int height, bool colour) {

	if (cameraID < 0) return false;

	this->width = width;
	this->height = height;
	this->colour = colour;
	this->fps = 30;
	
	bytes = (colour?3:1);
	int rowlength= width*bytes;
	
	switch (colour) {
		case false: {
			pixelFormat = k8IndexedGrayPixelFormat;
			//pixelFormat = kYVYU422PixelFormat;
			break;
		} case true: {
			pixelFormat = k24RGBPixelFormat;
			break;
		} 
	}
	
	OSErr result;
    Rect srcRect = {0,0, height, width};
   
    sg = OpenDefaultComponent(SeqGrabComponentType, 0);
    if(sg==NULL){
		fprintf(stderr, "could not open default component\n");
    }
	
	result = SGInitialize(sg);
    if(result!=noErr){
         fprintf(stdout, "could not initialize SG\n");
    }
    
	
    result = SGSetDataRef(sg, 0, 0, seqGrabDontMakeMovie);
        if (result != noErr){
             fprintf(stdout, "dataref failed\n");
        }
        
    result = SGNewChannel(sg, VideoMediaType, &vc);		
    if(result!=noErr){
         //fprintf(stdout, "could not make new SG channnel\n");
		 return false;
    }

//	result = SGSettingsDialog ( sg, vc ,0 ,NULL ,seqGrabSettingsPreviewOnly,NULL,0);
//    if(result!=noErr){
//         fprintf(stdout, "could not get settings from dialog\n");
//    }
    
    result = SGSetChannelBounds(vc, &srcRect);
    if(result!=noErr){
         fprintf(stdout, "could not set SG ChannelBounds\n");
    }
	
	/*result = SGSetFrameRate (vc, fps);
    if(result!=noErr){
         fprintf(stdout, "could not set SG FrameRate\n");
    }*/
      
    result = SGSetChannelUsage(vc, seqGrabPreview);
    if(result!=noErr){
         fprintf(stdout, "could not set SG ChannelUsage\n");
    }
    
	result = SGSetChannelPlayFlags(vc, channelPlayAllData);
	if(result!=noErr){
         fprintf(stdout, "could not set SG AllData\n");
	};

	buffer = new unsigned char[width*height*bytes];

	result = QTNewGWorldFromPtr (&srcGWorld,
									pixelFormat,
                                    &srcRect, 
                                    NULL, 
                                    NULL, 
                                    0, 
                                    buffer, 
                                    rowlength);
        
	if (result!= noErr)
  	{
		fprintf(stdout, "%d error at QTNewGWorldFromPtr\n", result);
		delete []buffer;
		buffer = NULL;
		return false;
	}  
	
    if (srcGWorld == NULL)
	{
		fprintf(stdout, "Could not allocate off screen\n");
		delete []buffer;
		buffer = NULL;
		return false;
	}
	
    result = SGSetGWorld(sg,(CGrafPtr)srcGWorld, NULL);
	if (result != noErr) {
		fprintf(stdout, "Could not set SGSetGWorld\n");
		delete []buffer;
		buffer = NULL;
		return false;
	}

    result = SGPrepare(sg, TRUE, FALSE);
    if (result != noErr) {
            fprintf(stderr, "SGPrepare Preview failed\n");
	}

	pbuffer = new unsigned char[width*height*bytes];
	return true;
}
Exemple #5
0
//--------------------------------------------------------------------
bool ofVideoGrabber::initGrabber(int w, int h, bool setUseTexture){

	bUseTexture = setUseTexture;

	//---------------------------------
	#ifdef OF_VIDEO_CAPTURE_QUICKTIME
	//---------------------------------

		//---------------------------------- 1 - open the sequence grabber
		if( !qtInitSeqGrabber() ){
			ofLog(OF_LOG_ERROR, "error: unable to initialize the seq grabber");
			return false;
		}

		//---------------------------------- 2 - set the dimensions
		width 		= w;
		height 		= h;

		MacSetRect(&videoRect, 0, 0, width, height);

		//---------------------------------- 3 - buffer allocation
		// Create a buffer big enough to hold the video data,
		// make sure the pointer is 32-byte aligned.
		// also the rgb image that people will grab

		offscreenGWorldPixels 	= (unsigned char*)malloc(4 * width * height + 32);
		pixels					= new unsigned char[width*height*3];
		QTNewGWorldFromPtr (&videogworld, k32ARGBPixelFormat, &videoRect, NULL, NULL, 0, offscreenGWorldPixels, 4 * width);
		LockPixels(GetGWorldPixMap(videogworld));
		SetGWorld (videogworld, NULL);
		SGSetGWorld(gSeqGrabber, videogworld, nil);


		//---------------------------------- 4 - device selection
		bool didWeChooseADevice = bChooseDevice;
		bool deviceIsSelected	=  false;

		//if we have a device selected then try first to setup
		//that device
		if(didWeChooseADevice){
			deviceIsSelected = qtSelectDevice(deviceID, true);
			if(!deviceIsSelected && bVerbose) ofLog(OF_LOG_WARNING, "unable to open device[%i] - will attempt other devices", deviceID);
		}

		//if we couldn't select our required device
		//or we aren't specifiying a device to setup
		//then lets try to setup ANY device!
		if(deviceIsSelected == false){
			//lets list available devices
			listDevices();

			setDeviceID(0);
			deviceIsSelected = qtSelectDevice(deviceID, false);
		}

		//if we still haven't been able to setup a device
		//we should error and stop!
		if( deviceIsSelected == false){
			goto bail;
		}

		//---------------------------------- 5 - final initialization steps
		OSStatus err;

	 	err = SGSetChannelUsage(gVideoChannel,seqGrabPreview);
		if ( err != noErr ) goto bail;

		err = SGSetChannelBounds(gVideoChannel, &videoRect);
		if ( err != noErr ) goto bail;

		err = SGPrepare(gSeqGrabber,  true, false); //theo swapped so preview is true and capture is false
		if ( err != noErr ) goto bail;

		err = SGStartPreview(gSeqGrabber);
		if ( err != noErr ) goto bail;

		bGrabberInited = true;
		loadSettings();

		ofLog(OF_LOG_NOTICE,"end setup ofVideoGrabber");
		ofLog(OF_LOG_NOTICE,"-------------------------------------\n");


		//---------------------------------- 6 - setup texture if needed

		if (bUseTexture){
			// create the texture, set the pixels to black and
			// upload them to the texture (so at least we see nothing black the callback)
			tex.allocate(width,height,GL_RGB);
			memset(pixels, 0, width*height*3);
			tex.loadData(pixels, width, height, GL_RGB);
		}

		// we are done
		return true;


		//--------------------- (bail) something's wrong -----
		bail:

			ofLog(OF_LOG_ERROR, "***** ofVideoGrabber error *****");
			ofLog(OF_LOG_ERROR, "-------------------------------------\n");

			//if we don't close this - it messes up the next device!
			if(bSgInited) qtCloseSeqGrabber();

			bGrabberInited = false;
			return false;

	//---------------------------------
	#endif
	//---------------------------------


	//---------------------------------
	#ifdef OF_VIDEO_CAPTURE_DIRECTSHOW
	//---------------------------------

		if (bChooseDevice){
			device = deviceID;
			ofLog(OF_LOG_NOTICE, "choosing %i", deviceID);
		} else {
			device = 0;
		}

		width = w;
		height = h;
		bGrabberInited = false;

		bool bOk = VI.setupDevice(device, width, height);

		int ourRequestedWidth = width;
		int ourRequestedHeight = height;

		if (bOk == true){
			bGrabberInited = true;
			width 	= VI.getWidth(device);
			height 	= VI.getHeight(device);

			if (width == ourRequestedWidth && height == ourRequestedHeight){
				bDoWeNeedToResize = false;
			} else {
				bDoWeNeedToResize = true;
				width = ourRequestedWidth;
				height = ourRequestedHeight;
			}


			pixels	= new unsigned char[width * height * 3];

			if (bUseTexture){
				// create the texture, set the pixels to black and
				// upload them to the texture (so at least we see nothing black the callback)
				tex.allocate(width,height,GL_RGB);
				memset(pixels, 0, width*height*3);
				tex.loadData(pixels, width, height, GL_RGB);
			}

			return true;
		} else {
			ofLog(OF_LOG_ERROR, "error allocating a video device");
			ofLog(OF_LOG_ERROR, "please check your camera with AMCAP or other software");
			bGrabberInited = false;
			return false;
		}

	//---------------------------------
	#endif
	//---------------------------------



	//---------------------------------
	#ifdef OF_VIDEO_CAPTURE_UNICAP
	//--------------------------------
		if( !bGrabberInited ){
			if ( !bChooseDevice ){
				deviceID = 0;
			}

			width 	= w;
			height 	= h;
			pixels	= new unsigned char[width * height * 3];

			if (bUseTexture){
				// create the texture, set the pixels to black and
				// upload them to the texture (so at least we see nothing black the callback)
				tex.allocate(width,height,GL_RGB);
				memset(pixels, 0, width*height*3);
				tex.loadData(pixels, width, height, GL_RGB);
			}

			bGrabberInited = ucGrabber.open_device (deviceID);
			if( bGrabberInited ){
			ofLog(OF_LOG_NOTICE, "choosing device %i: %s", deviceID,ucGrabber.device_identifier());
				ucGrabber.set_format(w,h);
				ucGrabber.start_capture();
			}

		}
			return bGrabberInited;
	//---------------------------------
	#endif
	//---------------------------------

	//---------------------------------
	#ifdef OF_VIDEO_CAPTURE_GSTREAMER
	//--------------------------------
		if(gstUtils.initGrabber(w,h)){
			if ( !bChooseDevice ){
				deviceID = 0;
			}

			width 	= w;
			height 	= h;
			if (bUseTexture){
				// create the texture, set the pixels to black and
				// upload them to the texture (so at least we see nothing black the callback)
				tex.allocate(width,height,GL_RGB);
				tex.loadData(gstUtils.getPixels(), width, height, GL_RGB);
			}
			bGrabberInited = true;
			ofLog(OF_LOG_VERBOSE, "ofVideoGrabber: initied");
		}else{
			bGrabberInited = false;
			ofLog(OF_LOG_ERROR, "ofVideoGrabber: couldn't init");
		}
		return bGrabberInited;


	//---------------------------------
	#endif
	//---------------------------------

	//---------------------------------
	#ifdef OF_VIDEO_CAPTURE_V4L
	//--------------------------------
		if (bChooseDevice){
			device = deviceID;
		} else {
			device = 0;
		}
		sprintf(dev_name, "/dev/video%i", device);
		ofLog(OF_LOG_NOTICE, "choosing device %s",dev_name);

		bool bOk = initV4L(w, h, dev_name);

		if (bOk == true){
			bV4LGrabberInited = true;
			width 	= getV4L_Width();
			height 	= getV4L_Height();
			pixels	= new unsigned char[width * height * 3];

			if (bUseTexture){
				// create the texture, set the pixels to black and
				// upload them to the texture (so at least we see nothing black the callback)
				tex.allocate(width,height,GL_RGB);
				//memset(pixels, 0, width*height*3);
				//tex.loadData(pixels, width, height, GL_RGB);
			}

			ofLog(OF_LOG_NOTICE, "success allocating a video device ");
			return true;
		} else {
			ofLog(OF_LOG_ERROR, "error allocating a video device");
			ofLog(OF_LOG_ERROR, "please check your camera and verify that your driver is correctly installed.");
			return false;
		}	//---------------------------------


	//---------------------------------
	#endif
	//---------------------------------



}
//--------------------------------------------------------------------
bool ofQuickTimeGrabber::initGrabber(int w, int h){

	//---------------------------------
	#ifdef OF_VIDEO_CAPTURE_QUICKTIME
	//---------------------------------

		//---------------------------------- 1 - open the sequence grabber
		if( !qtInitSeqGrabber() ){
			ofLogError("ofQuickTimeGrabber") << "initGrabber(): unable to initialize the seq grabber";
			return false;
		}

		//---------------------------------- 2 - set the dimensions
		//width 		= w;
		//height 		= h;

		MacSetRect(&videoRect, 0, 0, w, h);

		//---------------------------------- 3 - buffer allocation
		// Create a buffer big enough to hold the video data,
		// make sure the pointer is 32-byte aligned.
		// also the rgb image that people will grab

		offscreenGWorldPixels 	= (unsigned char*)malloc(4 * w * h + 32);
		pixels.allocate(w, h, OF_IMAGE_COLOR);
		
		#if defined(TARGET_OSX) && defined(__BIG_ENDIAN__)
			QTNewGWorldFromPtr (&(videogworld), k32ARGBPixelFormat, &(videoRect), NULL, NULL, 0, (offscreenGWorldPixels), 4 * w);		
		#else
			QTNewGWorldFromPtr (&(videogworld), k24RGBPixelFormat, &(videoRect), NULL, NULL, 0, (pixels.getPixels()), 3 * w);
		#endif		
		
		LockPixels(GetGWorldPixMap(videogworld));
		SetGWorld (videogworld, NULL);
		SGSetGWorld(gSeqGrabber, videogworld, nil);

		
		//---------------------------------- 4 - device selection
		bool didWeChooseADevice = bChooseDevice;
		bool deviceIsSelected	=  false;

		//if we have a device selected then try first to setup
		//that device
		if(didWeChooseADevice){
			deviceIsSelected = qtSelectDevice(deviceID, true);
			if(!deviceIsSelected && bVerbose)
				ofLogError("ofQuickTimeGrabber") << "initGrabber(): unable to open device[" << deviceID << "], will attempt other devices";
		}

		//if we couldn't select our required device
		//or we aren't specifiying a device to setup
		//then lets try to setup ANY device!
		if(deviceIsSelected == false){
			//lets list available devices
			listDevices();

			setDeviceID(0);
			deviceIsSelected = qtSelectDevice(deviceID, false);
		}

		//if we still haven't been able to setup a device
		//we should error and stop!
		if( deviceIsSelected == false){
			goto bail;
		}

		//---------------------------------- 5 - final initialization steps
		OSStatus err;

	 	err = SGSetChannelUsage(gVideoChannel,seqGrabPreview);
		if ( err != noErr ) goto bail;

	
		//----------------- callback method for notifying new frame
		err = SGSetChannelRefCon(gVideoChannel, (long)&bHavePixelsChanged );
		if(!err) {

			VideoBottles vb; 
			/* get the current bottlenecks */ 
			vb.procCount = 9; 
			err = SGGetVideoBottlenecks(gVideoChannel, &vb); 
			if (!err) { 			
				myGrabCompleteProc = NewSGGrabCompleteBottleUPP(frameIsGrabbedProc);
				vb.grabCompleteProc = myGrabCompleteProc;
			
				/* add our GrabFrameComplete function */ 
				err = SGSetVideoBottlenecks(gVideoChannel, &vb); 	
			}
		
		}
				
		err = SGSetChannelBounds(gVideoChannel, &videoRect);
		if ( err != noErr ) goto bail;

		err = SGPrepare(gSeqGrabber,  true, false); //theo swapped so preview is true and capture is false
		if ( err != noErr ) goto bail;

		err = SGStartPreview(gSeqGrabber);
		if ( err != noErr ) goto bail;

		bGrabberInited = true;
		loadSettings();
		
		if( attemptFramerate >= 0 ){
			err = SGSetFrameRate(gVideoChannel, IntToFixed(attemptFramerate) );
			if ( err != noErr ){
				ofLogError("ofQuickTimeGrabber") << "initGrabber: couldn't setting framerate to " << attemptFramerate << ": OSStatus " << err;
			}		
		}
		
		ofLogNotice("ofQuickTimeGrabber") << "           inited grabbed            ";
		ofLogNotice("ofQuickTimeGrabber") << "-------------------------------------";

		// we are done
		return true;


		//--------------------- (bail) something's wrong -----
		bail:

			ofLogError("ofQuickTimeGrabber") << "***** ofQuickTimeGrabber error *****";
			ofLogError("ofQuickTimeGrabber") << "------------------------------------";

			//if we don't close this - it messes up the next device!
			if(bSgInited) qtCloseSeqGrabber();

			bGrabberInited = false;
			return false;
			
	//---------------------------------
	#else
	//---------------------------------
		return false;
	//---------------------------------
	#endif
	//---------------------------------

}