void playNormalVideo() { IGraphBuilder *pGraphBuilder; IMediaControl *pMediaControl; IMediaEvent *pMediaEvent; long eventCode; CoInitialize(NULL); CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (LPVOID *)&pGraphBuilder); pGraphBuilder->QueryInterface(IID_IMediaControl, (LPVOID *)&pMediaControl); pGraphBuilder->QueryInterface(IID_IMediaEvent, (LPVOID *)&pMediaEvent); pMediaControl->RenderFile(L"Rscreen_shortFinger.avi"); pMediaControl->Run(); FILTER_STATE fs; HRESULT hr = pMediaControl->GetState(100, (OAFilterState*)&fs); // The first argument is timeout value. // If you change the "-1" part into "2", // WaitForCompletion will timeout after 2 seconds. pMediaEvent->WaitForCompletion(-1, &eventCode); switch (eventCode) { case 0: printf("timeout\n"); break; case EC_COMPLETE: printf("complete\n"); break; case EC_ERRORABORT: printf("errorabort\n"); break; case EC_USERABORT: printf("userabort\n"); break; } hr = pMediaControl->GetState(100, (OAFilterState*)&fs); pMediaControl->Run(); hr = pMediaControl->GetState(100, (OAFilterState*)&fs); pMediaEvent->WaitForCompletion(-1, &eventCode); pMediaControl->Release(); pGraphBuilder->Release(); CoUninitialize(); }
//------------------------------------------------------------------------------ // Name: WaitForCompletion() // Desc: Desc: Waits for a media event that signifies completion or cancellation // of a task. // // pGraph: Pointer to the Filter Graph Manager's IGraphBuilder interface. //------------------------------------------------------------------------------ LONG WaitForCompletion( IGraphBuilder *pGraph ) { HRESULT hr; LONG lEvCode = 0; IMediaEvent *pEvent; pGraph->QueryInterface(IID_IMediaEvent, (void **) &pEvent); do { MSG Message; // Pump queued Windows messages while(PeekMessage(&Message, NULL, 0, 0, TRUE)) { TranslateMessage(&Message); DispatchMessage(&Message); } // Wait 10ms for an event hr = pEvent->WaitForCompletion(10, &lEvCode); } while(lEvCode == 0); pEvent->Release(); return lEvCode; }
void video_start(int id, bool loop) { get_video(videoStruct, id); IMediaControl *pControl = NULL; IMediaEvent *pEvent = NULL; HRESULT hr = videoStruct->pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl); hr = videoStruct->pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent); // Run the graph. hr = pControl->Run(); while (SUCCEEDED(hr)) { if (SUCCEEDED(hr)) { // Wait for completion. long evCode; pEvent->WaitForCompletion(INFINITE, &evCode); } else { MessageBox(NULL, "Failed to run the COM control.", "ERROR", MB_ICONERROR | MB_OK); // Don't return so pointers can be released. } if (loop) { video_set_seek(id, 0); } else { break; } } if (FAILED(hr)) { MessageBox(NULL, "Failed to render the Filter Graph Manager.", "ERROR", MB_ICONERROR | MB_OK); // Don't return so pointers can be released. } pControl->Release(); pEvent->Release(); }
void main() { int option; IGraphBuilder *pGraph = NULL; //Creem l'apuntador al graf de filtres IMediaControl *pControl = NULL; //creem l'apuntador a un controlador per ayurar i iniciar el graf IMediaEvent *pEvent = NULL; // apunta a l'objecte necessari per obtenir events del filter graph manager //IBaseFilter *pGrabberF = NULL; //ISampleGrabber *pGrabber = NULL; HRESULT hr = CoInitialize(NULL); // Inicialitzem la llibreria COM if ( FAILED(hr) ){ printf("ERROR - Could not initialize COM library"); return; } // Create the filter graph manager and query for interfaces. hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph); if (FAILED(hr)) { printf("ERROR - Could not create the Filter Graph Manager."); return; } hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl); hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent); // Build the graph. IMPORTANT: Change this string to a file on your system. cout<<"introduce 1:chicken 2:futbol 3: video futbol audio chicken: \n"; cin>>option; switch(option) { case 1: hr = pGraph->RenderFile(L"C:\\Users\\Victor\\Downloads\\chicken.wmv", NULL); break; case 2: hr = pGraph->RenderFile(L"C:\\Users\\Victor\\Downloads\\futbol.mpg", NULL); break; case 3: // Create the Sample Grabber filter. break; } if (SUCCEEDED(hr)) { // Run the graph. hr = pControl->Run(); if (SUCCEEDED(hr)) { // Wait for completion. long evCode; pEvent->WaitForCompletion(INFINITE, &evCode); // Note: Do not use INFINITE in a real application, because it // can block indefinitely. } } pControl->Release(); pEvent->Release(); pGraph->Release(); CoUninitialize(); }
void play_movie( HWND hwnd ) { IGraphBuilder *pGraph; IMediaControl *pMediaControl; IMediaEvent *pEvent; IBasicVideo *pBasic; IVideoWindow *pVidWin = NULL; RECT grc; long width, height; CoInitialize(NULL); // Create the filter graph manager and query for interfaces. CoCreateInstance( CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph); pGraph->QueryInterface(IID_IVideoWindow, (void **)&pVidWin); pGraph->QueryInterface(IID_IMediaControl, (void **)&pMediaControl); pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent); pGraph->QueryInterface(IID_IBasicVideo, (void**)&pBasic ); // Build the graph. IMPORTANT: Change string to a file on your system. pGraph->RenderFile(L"e:\\alpha\\running.avi", NULL); pBasic->GetVideoSize( &width, &height ); printf( "video frames are %d x %d\n", width, height ); pVidWin->put_Owner((OAHWND)hwnd); pVidWin->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS); GetClientRect( hwnd, &grc ); pVidWin->SetWindowPosition(10, 10, width, height); printf( "window is %d x %d\n", grc.right, grc.bottom ); // Run the graph. pMediaControl->Run(); // Wait for completion. long evCode; pEvent->WaitForCompletion(INFINITE, &evCode); pVidWin->put_Visible(OAFALSE); pVidWin->put_Owner(NULL); // Clean up. pBasic->Release(); pVidWin->Release(); pMediaControl->Release(); pEvent->Release(); pGraph->Release(); CoUninitialize(); }
CHogVideo::~CHogVideo() { // Wait for completion. this->UnHog(); if (m_pEvent) { long evCode; IMediaEvent *pEvent = (IMediaEvent*)m_pEvent; pEvent->WaitForCompletion(INFINITE, &evCode); } // Clean up. this->Cleanup(); }
void Video::PlayMovie(string path) { IGraphBuilder *pGraph = NULL; IMediaControl *pControl = NULL; IMediaEvent *pEvent = NULL; IVideoWindow *pVideo = NULL; // Initialize the COM library. CoInitialize(NULL); // Create the filter graph manager and query for interfaces. CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph); // Build the graph int len; int slength = (int)path.length() + 1; len = MultiByteToWideChar(CP_ACP, 0, path.c_str(), slength, 0, 0); wchar_t* buf = new wchar_t[len]; MultiByteToWideChar(CP_ACP, 0, path.c_str(), slength, buf, len); std::wstring r(buf); delete[] buf; pGraph->RenderFile(LPCWSTR(r.c_str()), NULL); // set the owner window pGraph->QueryInterface(IID_IVideoWindow, (void **) &pVideo); pVideo->put_Owner((OAHWND)window); pVideo->put_WindowStyle( WS_CHILD ); pVideo->put_Left(0); pVideo->put_Top(0); pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl); pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent); pControl->Run(); long evCode; pEvent->WaitForCompletion(-1, &evCode); // release controls pControl->Release(); pEvent->Release(); pGraph->Release(); pVideo->Release(); CoUninitialize(); }
int _tmain(int argc, _TCHAR* argv[]) { IGraphBuilder *pGraph = NULL; IMediaControl *pControl = NULL; IMediaEvent *pEvent = NULL; //Get some param-------------- HRESULT hr1; IBasicVideo *pVideo=NULL; IBasicAudio *pAudio=NULL; IVideoWindow *pWindow=NULL; IMediaSeeking *pSeeking=NULL; // Init COM HRESULT hr = CoInitialize(NULL); if (FAILED(hr)){ printf("Error - Can't init COM."); return -1; } // Create FilterGraph hr=CoCreateInstance(CLSID_FilterGraph, NULL,CLSCTX_INPROC_SERVER,IID_IGraphBuilder, (void **)&pGraph); if (FAILED(hr)){ printf("Error - Can't create Filter Graph."); return -1; } // Query Interface hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl); hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent); // RenderFile hr = pGraph->RenderFile(L"cuc_ieschool.mov", NULL); if (FAILED(hr)){ printf("Error - Can't Render File."); return -1; } #if OUTPUT_INFO //Get some information---------- long video_w=0,video_h=0,video_bitrate=0,audio_volume=0; long long duration_1=0,position_1=0; REFTIME avgtimeperframe=0; float framerate=0,duration_sec=0,progress=0,position_sec=0; //Video hr1=pGraph->QueryInterface(IID_IBasicVideo, (void **)&pVideo); pVideo->get_VideoWidth(&video_w); pVideo->get_VideoHeight(&video_h); pVideo->get_AvgTimePerFrame(&avgtimeperframe); framerate=1/avgtimeperframe; //pVideo->get_BitRate(&video_bitrate); //Audio hr1=pGraph->QueryInterface(IID_IBasicAudio, (void **)&pAudio); //Mute //pAudio->put_Volume(-10000); printf("Some Information:\n"); printf("Video Resolution:\t%dx%d\n",video_w,video_h); printf("Video Framerate:\t%.3f\n",framerate); //Window hr1=pGraph->QueryInterface(IID_IVideoWindow, (void **)&pWindow); pWindow->put_Caption(L"Simplest DirectShow Player"); //pWindow->put_Width(480); //pWindow->put_Height(272); //Seek hr1=pGraph->QueryInterface(IID_IMediaSeeking, (void **)&pSeeking); pSeeking->GetDuration(&duration_1); //time unit:100ns=0.0000001s duration_sec=(float)duration_1/10000000.0; printf("Duration:\t%.2f s\n",duration_sec); //pSeeking->SetPositions(); //PlayBack Rate //pSeeking->SetRate(2.0); //Show Filter in FilterGpagh show_filters_in_filtergraph(pGraph); //---------------------- #endif printf("Progress Info\n"); printf("Position\tProgress\n"); if (SUCCEEDED(hr)){ // Run hr = pControl->Run(); if (SUCCEEDED(hr)){ long evCode=0; //pEvent->WaitForCompletion(INFINITE, &evCode); while(evCode!=EC_COMPLETE){ //Info #if OUTPUT_INFO pSeeking->GetCurrentPosition(&position_1); position_sec=(float)position_1/10000000.0; progress=position_sec*100/duration_sec; printf("%7.2fs\t%5.2f%%\n",position_sec,progress); #endif //1000ms pEvent->WaitForCompletion(1000, &evCode); } } } // Release resource pControl->Release(); pEvent->Release(); pGraph->Release(); CoUninitialize(); return 0; }
int JukeboxDS::player() { if( FAILED(CoInitialize(NULL)) ) return -1; IGraphBuilder * pGB = NULL; IMediaControl * pMC = NULL; IBasicAudio * pBA = NULL; IMediaEvent * pME = NULL; while( m_Active ) { // check the state of the music, if stopped move onto the next file if ( pME != NULL ) { long eventCode; if ( pME->WaitForCompletion( 0, &eventCode ) == S_OK ) nextTrack(); // current song has ended, next track } // check for a volume change if ( m_UpdateVolume.signaled() ) { m_UpdateVolume.clear(); if ( pBA != NULL ) { // set the volume long pv = (100 - m_Volume) * -100; pBA->put_Volume( pv ); } } if (! m_TrackEvent.wait( 250 ) ) { AutoLock lock( &m_Lock ); if ( m_PlayLists.isValid( m_ActiveList ) && m_PlayLists[ m_ActiveList ].files.isValid( m_CurrentTrack ) ) { // get the filename of the current track CharString file = m_PlayLists[ m_ActiveList ].files[ m_CurrentTrack ]; // release the previous interfaces RELEASEQI( pGB ); RELEASEQI( pMC ); RELEASEQI( pBA ); RELEASEQI( pME ); // Create DirectShow Graph if ( FAILED( CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void **)&pGB) ) ) return -1; try { // load the first file if ( FAILED( pGB->RenderFile( String( file ), NULL ) ) ) return -1; } catch( ... ) { // driver failure, don't crash the game due to a bad codec.. return -1; } // Get the IMediaControl Interface if ( FAILED( pGB->QueryInterface(IID_IMediaControl, (void **)&pMC ) ) ) return -1; // Get the IBasicAudio interface if ( FAILED( pGB->QueryInterface(IID_IBasicAudio, (void **)&pBA) ) ) return -1; // Get the IBasicAudio interface if ( FAILED( pGB->QueryInterface(IID_IMediaEvent, (void **)&pME) ) ) return -1; // set the volume long pv = (100 - m_Volume) * -100; pBA->put_Volume( pv ); // play the music pMC->Run(); } } } RELEASEQI( pGB ); RELEASEQI( pMC ); RELEASEQI( pBA ); RELEASEQI( pME ); CoUninitialize(); return 0; }
int main(int argc, char* argv[]) { ICaptureGraphBuilder2 *pCaptureGraphBuilder = NULL; IGraphBuilder *pGraphBuilder = NULL; IBaseFilter *pSource = NULL; IBaseFilter *pMux = NULL; IBaseFilter *pVideoCompressor = NULL; IBaseFilter *pAudioCompressor = NULL; IAMStreamConfig *pAMStreamConfig = NULL; IAMVideoCompression *pAMVideoCompression = NULL; IMediaControl *pControl = NULL; IMediaSeeking *pSeek = NULL; IMediaEvent *pEvent = NULL; HRESULT hr; DWORD pdwRegister=0; CoInitialize(NULL); // Create the capture graph builder. CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC, IID_ICaptureGraphBuilder2, (void **)&pCaptureGraphBuilder); // Make the rendering section of the graph. pCaptureGraphBuilder->SetOutputFileName( &MEDIASUBTYPE_Avi, // File type. L"C:\\STDIUE1.avi", // File name. &pMux, // pointer to the multiplexer. NULL); // pointer to the file writer. // Load the source file. pCaptureGraphBuilder->GetFiltergraph(&pGraphBuilder); pGraphBuilder->AddSourceFilter(L"C:\\Program Files\\Microsoft Money\\Media\\STDIUE1.avi", L"Source Filter", &pSource); // Add the compressor filter. CoCreateInstance(CLSID_AVICo, NULL, CLSCTX_INPROC, IID_IBaseFilter, (void **)&pVideoCompressor); pGraphBuilder->AddFilter(pVideoCompressor, L"Video Compressor"); // Render the video stream, through the compressor. pCaptureGraphBuilder->RenderStream( NULL, // Output pin category NULL, // Media type pSource, // Source filter pVideoCompressor, // Compressor filter pMux); // Sink filter (the AVI Mux) /* CoCreateInstance(CLSID_GSM, NULL, CLSCTX_INPROC, IID_IBaseFilter, (void **)&pAudioCompressor); pGraphBuilder->AddFilter(pAudioCompressor, L"Audio Compressor");*/ // Render the audio stream. pCaptureGraphBuilder->RenderStream( NULL, NULL, pSource, pAudioCompressor, pMux); // Compress at 100k/second data rate. AM_MEDIA_TYPE *pmt; pCaptureGraphBuilder->FindInterface(NULL, NULL, pVideoCompressor, IID_IAMStreamConfig, (void **)&pAMStreamConfig); pAMStreamConfig->GetFormat(&pmt); if (pmt->formattype == FORMAT_VideoInfo) { ((VIDEOINFOHEADER *)(pmt->pbFormat))->dwBitRate = 100000; pAMStreamConfig->SetFormat(pmt); } // Request key frames every four frames. pAMStreamConfig->QueryInterface(IID_IAMVideoCompression, (void **)&pAMVideoCompression); pAMVideoCompression->put_KeyFrameRate(4); pAMVideoCompression->Release(); pAMStreamConfig->Release(); // Run the graph. pGraphBuilder->QueryInterface(IID_IMediaControl, (void **)&pControl); pGraphBuilder->QueryInterface(IID_IMediaEvent, (void **)&pEvent); hr = pMux->QueryInterface(IID_IMediaSeeking, (void**)&pSeek); pControl->Run(); printf("Recompressing... \n"); long evCode; if (SUCCEEDED(hr)) { REFERENCE_TIME rtTotal, rtNow = 0; pSeek->GetDuration(&rtTotal); while ((pEvent->WaitForCompletion(1000, &evCode)) == E_ABORT) { pSeek->GetCurrentPosition(&rtNow); printf("%d%%\n", (rtNow * 100)/rtTotal); } pSeek->Release(); } else // Cannot update the progress. { pEvent->WaitForCompletion(INFINITE, &evCode); } pControl->Stop(); printf("All done\n"); pSource->Release(); pMux->Release(); pVideoCompressor->Release(); pAudioCompressor->Release (); pControl->Release(); pEvent->Release(); pCaptureGraphBuilder->Release(); pGraphBuilder->Release(); CoUninitialize(); return 0; }