apShow.cpp
资源名称:Direct90.rar [点击查看]
上传用户:maxzhb99
上传日期:2013-03-13
资源大小:48k
文件大小:12k
源码类别:
DirextX编程
开发平台:
Visual C++
- // apShow.cpp: CapShow 僋儔僗偺僀儞僾儕儊儞僥乕僔儑儞
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "apShow.h"
- //////////////////////////////////////////////////////////////////////
- // 峔抸/徚柵
- //////////////////////////////////////////////////////////////////////
- CapShow::CapShow()
- {
- this->g_psCurrent = Stopped;
- g_pGB = NULL;
- g_pVW = NULL;
- g_pMC = NULL;
- g_pME = NULL;
- g_pGraph = NULL;
- g_pCapture = NULL;
- }
- CapShow::~CapShow()
- {
- }
- void CapShow::SetFont(HWND hwnd)
- {
- g_hFont = m_blender.SetTextFont(hwnd,TRUE); // Don't display the Font Select dialog
- }
- void CapShow::UpdataStr(HWND ghApp,CString str)
- {
- TCHAR szStr[256];
- strcpy(szStr,str);
- m_blender.BlendText(g_pWC,ghApp,szStr,g_hFont);
- }
- void CapShow::OnPaint(HWND hwnd)
- {
- HRESULT hr;
- PAINTSTRUCT ps;
- HDC hdc;
- RECT rcClient;
- GetClientRect(hwnd, &rcClient);
- hdc = BeginPaint(hwnd, &ps);
- if(g_pWC)
- {
- // When using VMR Windowless mode, you must explicitly tell the
- // renderer when to repaint the video in response to WM_PAINT
- // messages. This is most important when the video is stopped
- // or paused, since the VMR won't be automatically updating the
- // window as the video plays.
- hr = g_pWC->RepaintVideo(hwnd, hdc);
- }
- else // No video image. Just paint the whole client area.
- {
- FillRect(hdc, &rcClient, (HBRUSH)(COLOR_BTNFACE + 1));
- }
- EndPaint(hwnd, &ps);
- }
- HRESULT CapShow::PlayWindow(HWND ghApp)
- {
- USES_CONVERSION;
- HRESULT hr;
- // Get the interface for DirectShow's GraphBuilder
- JIF(CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
- IID_IGraphBuilder, (void **)&g_pGB));
- if(SUCCEEDED(hr))
- {
- // Create the Video Mixing Renderer and add it to the graph
- JIF(InitializeWindowlessVMR(ghApp));
- // QueryInterface for DirectShow interfaces
- JIF(g_pGB->QueryInterface(IID_IMediaControl, (void **)&g_pMC));
- JIF(g_pGB->QueryInterface(IID_IMediaEventEx, (void **)&g_pME));
- JIF(g_pGB->QueryInterface(IID_IMediaSeeking, (void **)&g_pMS));
- /*
- // Is this an audio-only file (no video component)?
- if (CheckVideoVisibility())
- {
- JIF(InitVideoWindow(ghApp,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;
- }
- */
- // Select a text font if not already set
- //if (!g_hFont)
- g_hFont = m_blender.SetTextFont(ghApp, FALSE); // Don't display the Font Select dialog
- // Add the dynamic text bitmap to the VMR's input
- // If the initial blend fails, post a close message to exit the app
- hr = m_blender.BlendText(g_pWC, ghApp, "test", g_hFont);
- // 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(g_pMC->Run());
- // Start the text update timer
- //StartTimer();
- }
- return hr;
- }
- HRESULT CapShow::InitializeWindowlessVMR(HWND ghApp)
- {
- IBaseFilter* pVmr = NULL;
- // Create the VMR and add it to the filter graph.
- HRESULT hr = CoCreateInstance(CLSID_VideoMixingRenderer, NULL,
- CLSCTX_INPROC, IID_IBaseFilter, (void**)&pVmr);
- if (SUCCEEDED(hr))
- {
- hr = g_pGB->AddFilter(pVmr, L"Video Mixing Renderer");
- if (SUCCEEDED(hr))
- {
- // Set the rendering mode and number of streams.
- IVMRFilterConfig* pConfig;
- hr = pVmr->QueryInterface(IID_IVMRFilterConfig, (void**)&pConfig);
- if( SUCCEEDED(hr))
- {
- hr = pConfig->SetRenderingMode(VMRMode_Windowless);
- // Set two streams - video and alpha-blended bitmap
- hr = pConfig->SetNumberOfStreams(2);
- pConfig->Release();
- }
- hr = pVmr->QueryInterface(IID_IVMRWindowlessControl, (void**)&g_pWC);
- if( SUCCEEDED(hr))
- {
- hr = g_pWC->SetVideoClippingWindow(ghApp);
- hr = g_pWC->SetBorderColor(RGB(0,0,0));
- }
- // Request point filtering (instead of bilinear filtering)
- // to improve the text quality. In general, if you are
- // not scaling the app Image, you should use point filtering.
- // This is very important if you are doing source color keying.
- IVMRMixerControl *pMix;
- hr = pVmr->QueryInterface(IID_IVMRMixerControl, (void**)&pMix);
- if( SUCCEEDED(hr))
- {
- DWORD dwPrefs=0;
- hr = pMix->GetMixingPrefs(&dwPrefs);
- if (SUCCEEDED(hr))
- {
- dwPrefs |= MixerPref_PointFiltering;
- dwPrefs &= ~(MixerPref_BiLinearFiltering);
- hr = pMix->SetMixingPrefs(dwPrefs);
- }
- pMix->Release();
- }
- // Get alpha-blended bitmap interface
- hr = pVmr->QueryInterface(IID_IVMRMixerBitmap, (void**)&m_blender.pBMP);
- }
- else
- Msg(TEXT("Failed to add VMR to graph! hr=0x%xrn"), hr);
- pVmr->Release();
- }
- else
- Msg(TEXT("Failed to create VMR! hr=0x%xrn"), hr);
- return hr;
- }
- HRESULT CapShow::CaptureVideo(HWND ghApp)
- {
- HRESULT hr;
- IBaseFilter *pSrcFilter=NULL;
- // Get DirectShow interfaces
- hr = GetInterfaces(ghApp);
- if (FAILED(hr))
- {
- return hr;
- }
- // Attach the filter graph to the capture graph
- hr = g_pCapture->SetFiltergraph(g_pGraph);
- if (FAILED(hr))
- {
- return hr;
- }
- // Use the system device enumerator and class enumerator to find
- // a video capture/preview device, such as a desktop USB video camera.
- hr = FindCaptureDevice(&pSrcFilter);
- if (FAILED(hr))
- {
- return hr;
- }
- // Add Capture filter to our graph.
- hr = g_pGraph->AddFilter(pSrcFilter, L"Video Capture");
- if (FAILED(hr))
- {
- pSrcFilter->Release();
- return hr;
- }
- // Render the preview pin on the video capture filter
- // Use this instead of g_pGraph->RenderFile
- //hr = g_pCapture->RenderStream (&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,
- // pSrcFilter, NULL, NULL);
- hr = g_pCapture->RenderStream(NULL, NULL, pSrcFilter, NULL,NULL);
- if (FAILED(hr))
- {
- pSrcFilter->Release();
- return hr;
- }
- // Now that the filter has been added to the graph and we have
- // rendered its stream, we can release this reference to the filter.
- pSrcFilter->Release();
- // Set video window style and position
- hr = SetupVideoWindow(ghApp);
- if (FAILED(hr))
- {
- return hr;
- }
- // Start previewing video data
- hr = g_pMC->Run();
- if (FAILED(hr))
- {
- return hr;
- }
- //****************************************************************************
- //***************************僥僗僩*******************************************
- //****************************************************************************
- // Start the media file
- hr = PlayWindow(ghApp);
- InitializeWindowlessVMR(ghApp);
- SetForegroundWindow(ghApp);
- // Remember current state
- g_psCurrent = Running;
- return S_OK;
- }
- HRESULT CapShow::GetInterfaces(HWND ghApp)
- {
- HRESULT hr;
- // Create the filter graph
- hr = CoCreateInstance (CLSID_FilterGraph, NULL, CLSCTX_INPROC,
- IID_IGraphBuilder, (void **) &g_pGraph);
- if (FAILED(hr))
- return hr;
- // Create the capture graph builder
- hr = CoCreateInstance (CLSID_CaptureGraphBuilder2 , NULL, CLSCTX_INPROC,
- IID_ICaptureGraphBuilder2, (void **) &g_pCapture);
- if (FAILED(hr))
- return hr;
- // Obtain interfaces for media control and Video Window
- hr = g_pGraph->QueryInterface(IID_IMediaControl,(LPVOID *) &g_pMC);
- if (FAILED(hr))
- return hr;
- hr = g_pGraph->QueryInterface(IID_IVideoWindow, (LPVOID *) &g_pVW);
- if (FAILED(hr))
- return hr;
- hr = g_pGraph->QueryInterface(IID_IMediaEvent, (LPVOID *) &g_pME);
- if (FAILED(hr))
- return hr;
- return hr;
- }
- HRESULT CapShow::FindCaptureDevice(IBaseFilter **ppSrcFilter)
- {
- HRESULT hr;
- IBaseFilter * pSrc = NULL;
- CComPtr <IMoniker> pMoniker =NULL;
- ULONG cFetched;
- if (!ppSrcFilter)
- return E_POINTER;
- // Create the system device enumerator
- CComPtr <ICreateDevEnum> pDevEnum =NULL;
- hr = CoCreateInstance (CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
- IID_ICreateDevEnum, (void **) &pDevEnum);
- if (FAILED(hr))
- {
- return hr;
- }
- // Create an enumerator for the video capture devices
- CComPtr <IEnumMoniker> pClassEnum = NULL;
- hr = pDevEnum->CreateClassEnumerator (CLSID_VideoInputDeviceCategory, &pClassEnum, 0);
- if (FAILED(hr))
- {
- return hr;
- }
- // If there are no enumerators for the requested type, then
- // CreateClassEnumerator will succeed, but pClassEnum will be NULL.
- if (pClassEnum == NULL)
- {
- return E_FAIL;
- }
- // Use the first video capture device on the device list.
- // Note that if the Next() call succeeds but there are no monikers,
- // it will return S_FALSE (which is not a failure). Therefore, we
- // check that the return code is S_OK instead of using SUCCEEDED() macro.
- if (S_OK == (pClassEnum->Next (1, &pMoniker, &cFetched)))
- {
- // Bind Moniker to a filter object
- hr = pMoniker->BindToObject(0,0,IID_IBaseFilter, (void**)&pSrc);
- if (FAILED(hr))
- {
- return hr;
- }
- }
- else
- {
- return E_FAIL;
- }
- // Copy the found filter pointer to the output parameter.
- // Do NOT Release() the reference, since it will still be used
- // by the calling function.
- *ppSrcFilter = pSrc;
- return hr;
- }
- HRESULT CapShow::SetupVideoWindow(HWND ghApp)
- {
- HRESULT hr;
- // Set the video window to be a child of the main window
- hr = g_pVW->put_Owner((OAHWND)ghApp);
- if (FAILED(hr))
- return hr;
- // Set video window style
- hr = g_pVW->put_WindowStyle(WS_CHILD | WS_CLIPCHILDREN);
- if (FAILED(hr))
- return hr;
- // Use helper function to position video window in client rect
- // of main application window
- ResizeVideoWindow(ghApp);
- // Make the video window visible, now that it is properly positioned
- hr = g_pVW->put_Visible(OATRUE);
- if (FAILED(hr))
- return hr;
- return hr;
- }
- void CapShow::ResizeVideoWindow(HWND ghApp)
- {
- // Resize the video preview window to match owner window size
- if (g_pVW)
- {
- RECT rc;
- // Make the preview video fill our window
- GetClientRect(ghApp, &rc);
- g_pVW->SetWindowPosition(0, 0, rc.right, rc.bottom);
- }
- }
- void CapShow::CloseInterfaces()
- {
- // Stop previewing data
- if (g_pMC)
- g_pMC->StopWhenReady();
- g_psCurrent = Stopped;
- // Relinquish ownership (IMPORTANT!) of the video window.
- // Failing to call put_Owner can lead to assert failures within
- // the video renderer, as it still assumes that it has a valid
- // parent window.
- if(g_pVW)
- {
- g_pVW->put_Visible(OAFALSE);
- g_pVW->put_Owner(NULL);
- }
- // Release DirectShow interfaces
- // SAFE_RELEASE(g_pMC);
- // SAFE_RELEASE(g_pME);
- // SAFE_RELEASE(g_pVW);
- // SAFE_RELEASE(g_pGraph);
- // SAFE_RELEASE(g_pCapture);
- }