📄 quicktimeliveimagestream.cpp
字号:
result = SGVideoDigitizerChanged( m_gVideoChannel); result = SGGetSrcVideoBounds ( m_gVideoChannel, &gActiveVideoRect); result = SGSetChannelBounds ( m_gVideoChannel, &gActiveVideoRect); result = SGChangedSource (m_gSeqGrabber, m_gVideoChannel); Fixed frame_rate; result = SGGetFrameRate (m_gVideoChannel, &frame_rate); int zx = 0; result = SGSetFrameRate (m_gVideoChannel, 100); // // Sound /* long sound_id; Str255 sound_driver_name; char* sound_driver_name_cstr; vid_err = VDGetSoundInputSource(vdig, (long)m_videoDeviceInputID, &sound_id); vid_err = VDGetSoundInputDriver(vdig, sound_driver_name); sound_driver_name_cstr = pstr_printable(sound_driver_name); osg::notify(osg::DEBUG_INFO) << "vdig sound driver name :" << sound_driver_name_cstr << std::endl; osg::notify(osg::DEBUG_INFO) << "vdig sound driver id :" << sound_id << std::endl; */ } else { osg::notify(osg::FATAL) << "Could not create SGNewChannel for Video Channel" << std::endl; } // Set GWorld back SetGWorld(origPort, origDevice);}static OSErr MySGDataProc (SGChannel c,Ptr p,long len,long *offset,long chRefCon,TimeValue time,short writeType,long refCon ){ QuicktimeLiveImageStream* p_is = (QuicktimeLiveImageStream*)refCon; return p_is->dataProcCallback(c,p,len,offset,chRefCon,time,writeType,refCon);}OSErr QuicktimeLiveImageStream::dataProcCallback( SGChannel c,Ptr p,long len,long *offset,long chRefCon,TimeValue time,short writeType,long refCon ){ OSErr err = noErr; // osg::notify(osg::INFO) << " Video " << refCon << std::endl; dirty(); // return err;}// Create the Sequence Grabber DataProc setup for Recordvoid QuicktimeLiveImageStream::createSequenceGrabberDataProc(){ OSErr err; err = SGSetDataRef(m_gSeqGrabber, 0, 0, seqGrabToMemory | seqGrabDontMakeMovie); if (err != noErr) osg::notify(osg::FATAL) << "SGSetDataRef : error" << std::endl; // specify a sequence grabber data function err = SGSetDataProc(m_gSeqGrabber, NewSGDataUPP(MySGDataProc), (long)this); if (err != noErr) osg::notify(osg::FATAL) << "SGSetDataProc : error" << std::endl;}// Create the Sequence Grabber Audio Channelvoid QuicktimeLiveImageStream::createSequenceGrabberAudioChannel(){ // Check capability and setting of Sequence Grabber GDHandle origDevice; CGrafPtr origPort; // Create GWorld GetGWorld (&origPort, &origDevice); SetGWorld (m_gw, NULL); // set current graphics port to offscreen // Setup // Get a video channel ComponentResult result = SGNewChannel (m_gSeqGrabber, SoundMediaType, &m_gSoundChannel); if ((m_gSoundChannel != nil) && (result == noErr)) { result = SGInitChannel(m_gSoundChannel, m_gSeqGrabber); // result = SGSetChannelUsage (m_gSoundChannel, seqGrabPreview ); // Usage if (g_s_use_sg_record) result = SGSetChannelUsage (m_gSoundChannel, seqGrabRecord | seqGrabLowLatencyCapture); else { result = SGSetChannelUsage (m_gSoundChannel, seqGrabPreview | seqGrabRecord | seqGrabLowLatencyCapture); } // Get Str255 deviceName; Str255 inputName; short inputNumber; result = SGGetChannelDeviceAndInputNames( m_gSoundChannel, deviceName, inputName, &inputNumber); // Set // osg::notify(osg::DEBUG_INFO) << "Setting up audio component from input prefs" << std::endl; result = SGSetChannelDevice ( m_gSoundChannel, m_soundDeviceIDStr); result = SGSetChannelDeviceInput( m_gSoundChannel, m_soundDeviceInputID); // Set the volume low to prevent feedback when we start the preview, // in case the mic is anywhere near the speaker. short volume = 0; result = SGGetChannelVolume (m_gSoundChannel, &volume ); // result = SGSetChannelVolume (m_gSoundChannel, 255); // Inform result = SGChangedSource ( m_gSeqGrabber, m_gSoundChannel); } else { osg::notify(osg::FATAL) << "Could not create SGNewChannel for Sound Channel" << std::endl; } // Set GWorld back SetGWorld(origPort, origDevice);}// GrabFrameCompleteProc (QT callback)static ComponentResult GrabFrameCompleteProc(SGChannel sgChan, short nBufferNum, Boolean *pbDone, long lRefCon){ QuicktimeLiveImageStream* p_is = (QuicktimeLiveImageStream*)lRefCon; return p_is->grabFrameCompleteProc(sgChan, nBufferNum, pbDone, lRefCon);}// GrabFrameCompleteProc (QuicktimeLiveImageStream)ComponentResult QuicktimeLiveImageStream::grabFrameCompleteProc(SGChannel sgChan, short nBufferNum, Boolean *pbDone, long lRefCon){ ComponentResult err = noErr; // call the default grab-complete function err = SGGrabFrameComplete(sgChan, // channel reference nBufferNum, // buffer identifier, provided for you pbDone); // pointer to a boolean, has the frame been completely captured? provided for you static unsigned int fps_counter = 0; static osg::Timer_t start, finish; if (fps_counter == 0) start = osg::Timer::instance()->tick(); // if the frame is done, make sure the Image is replaced if (*pbDone && (sgChan == m_gVideoChannel)) { dirty(); ++fps_counter; if (fps_counter == 100) { finish = osg::Timer::instance()->tick(); double dur = osg::Timer::instance()->delta_s(start, finish); double fps = 100.0 / dur; osg::notify(osg::NOTICE) << "Executed 100 frames in " << dur << " seconds : ~" << fps << " fps" << std::endl; fps_counter = 0; } } return err;}// Create callbacksvoid QuicktimeLiveImageStream::createSequenceGrabberVideoBottlenecks(){ OSErr err = noErr; // set the value of a reference constant that is passed to the callback functions err = SGSetChannelRefCon(m_gVideoChannel, (long)this); if (err == noErr) { VideoBottles vb; // get the current bottlenecks vb.procCount = 9; err = SGGetVideoBottlenecks(m_gVideoChannel, &vb); if (err == noErr) { // add our GrabFrameComplete function vb.grabCompleteProc = NewSGGrabCompleteBottleUPP(GrabFrameCompleteProc); err = SGSetVideoBottlenecks(m_gVideoChannel, &vb); } }}// 2.// CreateAndRunWithVideoDigitizervoid QuicktimeLiveImageStream::createAndRunWithVideoDigitizer(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); // Create the Image createImage(); // Create the offscreen GWorld (using Image as target memory) createGWorld(); // Create the Sequence Grabber (using GWorld as target memory) createVideoDigitizer(); // Go _status = ImageStream::PLAYING; VideoDigitizerError error = VDSetPlayThruOnOff(m_vdig, vdPlayThruOn); if (error != noErr) osg::notify(osg::FATAL) << "VDSetPlayThruOnOff : error" << std::endl; // Ticker start();}// 2.// Create the Video Digitizer (using GWorld Pixmap as target mamory)void QuicktimeLiveImageStream::createVideoDigitizer(){ // #define videoDigitizerComponentType = 'vdig' ComponentDescription video_component_description; video_component_description.componentType = 'vdig'; /* A unique 4-byte code indentifying the command set */ video_component_description.componentSubType = 0; /* Particular flavor of this instance */ video_component_description.componentManufacturer = 0; /* Vendor indentification */ video_component_description.componentFlags = 0; /* 8 each for Component,Type,SubType,Manuf/revision */ video_component_description.componentFlagsMask = 0; /* Mask for specifying which flags to consider in search, zero during registration */ long num_video_components = CountComponents (&video_component_description); osg::notify(osg::DEBUG_INFO) << " available Video DigitizerComponents : " << num_video_components << std::endl; if (num_video_components) { Component aComponent = 0; short aDeviceID = 0; do { ComponentDescription full_video_component_description = video_component_description; aComponent = FindNextComponent(aComponent, &full_video_component_description); if (aComponent && (aDeviceID == m_videoDeviceID)) { osg::notify(osg::DEBUG_INFO) << "Component" << std::endl; OSErr err; Handle compName = NewHandle(256); Handle compInfo = NewHandle(256); err = GetComponentInfo( aComponent, &full_video_component_description, compName,compInfo,0); osg::notify(osg::DEBUG_INFO) << " Name: " << pstr_printable((StringPtr)*compName) << std::endl; osg::notify(osg::DEBUG_INFO) << " Desc: " << pstr_printable((StringPtr)*compInfo) << std::endl; //Capabilities VideoDigitizerComponent component_instance = OpenComponent(aComponent); m_vdig = component_instance; //Setup // Onscreen // Check capability and setting of Sequence Grabber GDHandle origDevice; CGrafPtr origPort; GetGWorld (&origPort, &origDevice); VideoDigitizerError error; Rect destinationBounds; destinationBounds.left = 0; destinationBounds.top = 0; destinationBounds.right = m_videoRectWidth; destinationBounds.bottom = m_videoRectHeight; error = VDSetPlayThruDestination(m_vdig, m_pixmap, &destinationBounds, 0, 0); //error = VDSetPlayThruGlobalRect(m_vdig, (GrafPtr)origPort, &destinationBounds); if (error != noErr) osg::notify(osg::FATAL) << "VDSetPlayThruDestination : error" << std::endl; print_video_component_capability(component_instance); break; } ++aDeviceID; } while (0 != aComponent); }}// Thread run methodvoid QuicktimeLiveImageStream::run(){ ComponentResult result = noErr; bool done = false; //memset( data(), 255, 720*250*4); while (!done) { // Do some funky rotational memset // void * memset ( void * ptr, int value, size_t num ); //memset // dirty(); if (g_s_use_sg) { result = SGIdle(m_gSeqGrabber); if (result != noErr) osg::notify(osg::FATAL) << "SGIdle : error" << std::endl; } //OpenThreads::Thread::microSleep(250000); // 25fps (1,000,000 = 1 fps) //OpenThreads::Thread::microSleep(50000); // 200fps (1,000,000 = 1 fps) //OpenThreads::Thread::microSleep(25000); // 400fps (1,000,000 = 1 fps) // Ridiculous OpenThreads::Thread::microSleep(10000); // 1000fps (1,000,000 = 1 fps) }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -