/* 构建滤波器链表,添加各个滤波器,链接并运行链表*/ HRESULT CVMR_Capture::Init(int iDeviceID,HWND hWnd, int iWidth, int iHeight) { HRESULT hr; //再次调用函数,释放已经建立的链表 CloseInterfaces(); // 创建IGraphBuilder hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&m_pGB); if (SUCCEEDED(hr)) { // 创建VMR并添加到Graph中 InitializeWindowlessVMR(hWnd); // 把指定的设备捆绑到一个滤波器 if(!BindFilter(iDeviceID, &m_pDF)) return S_FALSE; // 添加采集设备滤波器到Graph中 hr = m_pGB->AddFilter(m_pDF, L"Video Capture"); if (FAILED(hr)) return hr; // 获取捕获滤波器的引脚 IEnumPins *pEnum; m_pDF->EnumPins(&pEnum); hr |= pEnum->Reset(); hr |= pEnum->Next(1, &m_pCamOutPin, NULL); // 获取媒体控制和事件接口 hr |= m_pGB->QueryInterface(IID_IMediaControl, (void **)&m_pMC); hr |= m_pGB->QueryInterface(IID_IMediaEventEx, (void **)&m_pME); // 设置窗口通知消息处理 //hr = pME->SetNotifyWindow((OAHWND)hWnd, WM_GRAPHNOTIFY, 0); // 匹配视频分辨率,对视频显示窗口设置 hr |= InitVideoWindow(hWnd,iWidth, iHeight); // 为捕获图像帧申请内存 m_nFramelen=iWidth*iHeight*3; m_pFrame=(BYTE*) new BYTE[m_nFramelen]; // 运行Graph,捕获视频 m_psCurrent = STOPPED; hr |= m_pGB->Render(m_pCamOutPin); hr |= m_pMC->Run(); if (FAILED(hr)) return hr; m_psCurrent = RUNNING; } return hr; }
HRESULT CVMR_Capture::Init(int iDeviceID,HWND hWnd, int iWidth, int iHeight) { HRESULT hr; // Get the interface for DirectShow's GraphBuilder hr=CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&m_pGB); if(SUCCEEDED(hr)) { // Create the Video Mixing Renderer and add it to the graph InitializeWindowlessVMR(hWnd); // Bind Device Filter. We know the device because the id was passed in if(!BindFilter(iDeviceID, &m_pDF)) return S_FALSE; hr=m_pGB->AddFilter(m_pDF, L"Video Capture"); if (FAILED(hr)) return hr; CComPtr<IEnumPins> pEnum; m_pDF->EnumPins(&pEnum); hr = pEnum->Reset(); hr = pEnum->Next(1, &m_pCamOutPin, NULL); // QueryInterface for DirectShow interfaces hr = m_pGB->QueryInterface(IID_IMediaControl, (void **)&m_pMC); hr = m_pGB->QueryInterface(IID_IMediaEventEx, (void **)&m_pME); // Have the graph signal event via window callbacks for performance //hr = pME->SetNotifyWindow((OAHWND)hWnd, WM_GRAPHNOTIFY, 0); hr = InitVideoWindow(hWnd,iWidth, iHeight); m_nFramelen=iWidth*iHeight*3; m_pFrame=(BYTE*) new BYTE[m_nFramelen]; // Run the graph to play the media file m_psCurrent=Stopped; hr = m_pGB->Render(m_pCamOutPin); hr = m_pMC->Run(); m_psCurrent=Running; } return hr; }
HRESULT ConfigureMultiFileVMR9(WCHAR *wFile1, WCHAR *wFile2) { HRESULT hr=S_OK; CComPtr <IBaseFilter> pVmr; // Create the Video Mixing Renderer and add it to the graph JIF(InitializeWindowlessVMR(&pVmr)); // Render the files programmatically to use the VMR9 as renderer if (SUCCEEDED(hr = RenderFileToVMR9(pGB, wFile1, pVmr))) hr = RenderFileToVMR9(pGB, wFile2, pVmr); return hr; }
HRESULT PlayMovieInWindow(LPTSTR szFile) { USES_CONVERSION; WCHAR wFile[MAX_PATH]; HRESULT hr; // Check input string if (szFile == NULL) return E_POINTER; // Clear open dialog remnants before calling RenderFile() UpdateWindow(ghApp); // Convert filename to wide character string wcsncpy(wFile, T2W(szFile), NUMELMS(wFile)-1); wFile[MAX_PATH-1] = 0; // Get the interface for DirectShow's GraphBuilder JIF(CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGB)); if(SUCCEEDED(hr)) { // Create the Video Mixing Renderer and add it to the graph JIF(InitializeWindowlessVMR()); // Have the graph builder construct its the appropriate graph automatically JIF(pGB->RenderFile(wFile, NULL)); // QueryInterface for DirectShow interfaces JIF(pGB->QueryInterface(IID_IMediaControl, (void **)&pMC)); JIF(pGB->QueryInterface(IID_IMediaEventEx, (void **)&pME)); JIF(pGB->QueryInterface(IID_IMediaSeeking, (void **)&pMS)); // Is this an audio-only file (no video component)? if (CheckVideoVisibility()) { JIF(InitVideoWindow(1, 1)); } else { // This sample requires a video clip to be loaded Msg(TEXT("This sample requires media with a video component. ") TEXT("Please select another file.")); return E_FAIL; } // Have the graph signal event via window callbacks for performance JIF(pME->SetNotifyWindow((OAHWND)ghApp, WM_GRAPHNOTIFY, 0)); // Add the bitmap (static image or dynamic text) to the VMR's input if (g_dwTickerFlags & MARK_STATIC_IMAGE) { hr = BlendApplicationImage(ghApp); if (FAILED(hr)) PostMessage(ghApp, WM_CLOSE, 0, 0); CheckMenuItem(ghMenu, ID_TICKER_STATIC_IMAGE, MF_CHECKED); CheckMenuItem(ghMenu, ID_TICKER_DYNAMIC_TEXT, MF_UNCHECKED); } else // MARK_DYNAMIC_TEXT { if (!g_hFont) g_hFont = SetTextFont(FALSE); // Don't display the Windows Font Select dialog // If the initial blend fails, post a close message to exit the app hr = BlendApplicationText(ghApp, g_szAppText); if (FAILED(hr)) PostMessage(ghApp, WM_CLOSE, 0, 0); CheckMenuItem(ghMenu, ID_TICKER_STATIC_IMAGE, MF_UNCHECKED); CheckMenuItem(ghMenu, ID_TICKER_DYNAMIC_TEXT, MF_CHECKED); } // Complete the window setup ShowWindow(ghApp, SW_SHOWNORMAL); UpdateWindow(ghApp); SetForegroundWindow(ghApp); SetFocus(ghApp); #ifdef REGISTER_FILTERGRAPH if (FAILED(AddGraphToRot(pGB, &g_dwGraphRegister))) { Msg(TEXT("Failed to register filter graph with ROT!")); g_dwGraphRegister = 0; } #endif // Run the graph to play the media file JIF(pMC->Run()); // Start animation by default PostMessage(ghApp, WM_COMMAND, ID_SLIDE, 0); } return hr; }
HRESULT PlayMovieInWindow(LPTSTR szFile) { HRESULT hr; // Check input string if (szFile == NULL) return E_POINTER; // Clear open dialog remnants before calling RenderFile() UpdateWindow(ghApp); // Get the interface for DirectShow's GraphBuilder JIF(CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGB)); if(SUCCEEDED(hr)) { SmartPtr <IBaseFilter> pVmr; // Create the Video Mixing Renderer and add it to the graph JIF(InitializeWindowlessVMR(&pVmr)); // Render the file programmatically to use the VMR9 as renderer. // Pass TRUE to create an audio renderer also. if (FAILED(hr = RenderFileToVideoRenderer(pGB, szFile, TRUE))) return hr; // QueryInterface for DirectShow interfaces JIF(pGB->QueryInterface(IID_IMediaControl, (void **)&pMC)); JIF(pGB->QueryInterface(IID_IMediaEventEx, (void **)&pME)); JIF(pGB->QueryInterface(IID_IMediaSeeking, (void **)&pMS)); JIF(pGB->QueryInterface(IID_IBasicAudio, (void **)&pBA)); // Is this an audio-only file (no video component)? if (CheckVideoVisibility()) { JIF(InitVideoWindow(1, 1)); } else { // This sample requires a video clip to be loaded Msg(TEXT("This sample requires media with a video component. ") TEXT("Please select another file.")); return E_FAIL; } // Add the bitmap to the VMR's input BlendApplicationImage(ghApp); // Have the graph signal event via window callbacks for performance JIF(pME->SetNotifyWindow((OAHWND)ghApp, WM_GRAPHNOTIFY, 0)); // Complete the window setup ShowWindow(ghApp, SW_SHOWNORMAL); UpdateWindow(ghApp); SetForegroundWindow(ghApp); SetFocus(ghApp); UpdateMainTitle(); #ifdef REGISTER_FILTERGRAPH hr = AddGraphToRot(pGB, &g_dwGraphRegister); if (FAILED(hr)) { Msg(TEXT("Failed to register filter graph with ROT! hr=0x%x"), hr); g_dwGraphRegister = 0; } #endif // Run the graph to play the media file JIF(pMC->Run()); g_psCurrent=Running; } return hr; }