void CloseClip() { HRESULT hr; // Stop media playback if(pMC) hr = pMC->Stop(); // Clear global flags g_psCurrent = Stopped; g_bAudioOnly = TRUE; g_bFullscreen = FALSE; // Free DirectShow interfaces CloseInterfaces(); // Clear file name to allow selection of new file with open dialog g_szFileName[0] = L'\0'; // No current media state g_psCurrent = Init; // Reset the player window RECT rect; GetClientRect(ghApp, &rect); InvalidateRect(ghApp, &rect, TRUE); UpdateMainTitle(); InitPlayerWindow(); }
LRESULT CALLBACK WndMainProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_GRAPHNOTIFY: HandleGraphEvent(); break; case WM_SIZE: ResizeVideoWindow(); break; case WM_WINDOWPOSCHANGED: ChangePreviewState(! (IsIconic(hwnd))); break; case WM_CLOSE: // Hide the main window while the graph is destroyed ShowWindow(ghApp, SW_HIDE); CloseInterfaces(); // Stop capturing and release interfaces break; case WM_DESTROY: PostQuitMessage(0); return 0; } // Pass this message to the video window for notification of system changes if (g_pVW) g_pVW->NotifyOwnerMessage((LONG_PTR) hwnd, message, wParam, lParam); return DefWindowProc (hwnd , message, wParam, lParam); }
/* 构建滤波器链表,添加各个滤波器,链接并运行链表*/ 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; }
// 카메라 영상 이미지 버퍼를 리턴한다. BYTE* cDxCapture::GetCurrentBuffer(long &size) { AutoCS cs(&m_criticalSection); if (!m_pGrabber) return NULL; if (m_isUnpluggedInit) { // 카메라가 언플러그 된 후, 다시 플러그 되었을 때, 재초기화 를 한다. CloseInterfaces(); Init(m_cameraIndex, m_width, m_height, m_isThreadMode); m_isUnpluggedInit = false; } // Find the required buffer size. HRESULT hr; long cbBuffer; hr = m_pGrabber->GetCurrentBuffer(&cbBuffer, NULL); if (FAILED(hr)) return NULL; // 영상 크기가 다르다면, 새 버퍼를 생성한다. if (m_frameBufferSize != cbBuffer) { CoTaskMemFree(m_pFrameBuffer); m_pFrameBuffer = (BYTE*)CoTaskMemAlloc(cbBuffer); if (!m_pFrameBuffer) return NULL; m_frameBufferSize = cbBuffer; } hr = m_pGrabber->GetCurrentBuffer(&cbBuffer, (long*)m_pFrameBuffer); if (FAILED(hr)) { CoTaskMemFree(m_pFrameBuffer); return NULL; } m_isUpdateBuffer = true; size = cbBuffer; return m_pFrameBuffer; }
cDxCapture::~cDxCapture() { m_threadLoop = false; ::WaitForSingleObject(m_handle, 1000); DeleteCriticalSection(&m_criticalSection); CoTaskMemFree(m_pFrameBuffer); m_pFrameBuffer = NULL; CoTaskMemFree(m_pCloneFrameBuffer); m_pCloneFrameBuffer = NULL; FreeMediaType(m_mt); ZeroMemory(&m_mt, sizeof(m_mt)); CloseInterfaces(); // Release COM CoUninitialize(); }
void CloseFiles() { HRESULT hr; // Stop any running timer StopTimer(); // Stop media playback if(pMC) hr = pMC->Stop(); // Free DirectShow interfaces CloseInterfaces(); EnableMenus(FALSE); // Reset the player window RECT rect; GetClientRect(ghApp, &rect); InvalidateRect(ghApp, &rect, TRUE); InitPlayerWindow(); }
void CloseClip() { HRESULT hr; // Stop media playback if(pMC) hr = pMC->Stop(); // Free DirectShow interfaces CloseInterfaces(); // Clear file name to allow selection of new file with open dialog g_szFileName[0] = L'\0'; // Reset the player window RECT rect; GetClientRect(ghApp, &rect); InvalidateRect(ghApp, &rect, TRUE); InitPlayerWindow(); EnableTickerMenu(FALSE); }
void CPlayWMV::CloseClip() { HRESULT hr; // Stop media playback if (m_pMC) hr =m_pMC->Stop(); // Clear global flags m_nPlayState = Stopped; m_bAudioOnly = TRUE; m_bFullscreen = FALSE; // Free DirectShow interfaces CloseInterfaces(); // No current media state m_nPlayState = Init; // Reset the player window InvalidateRect(m_hWnd, &m_Rect, TRUE); }
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hInstP, LPSTR lpCmdLine, int nCmdShow) { MSG msg={0}; WNDCLASS wc; // Initialize COM if(FAILED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED))) { Msg(TEXT("CoInitialize Failed!\r\n")); exit(1); } // Register the window class ZeroMemory(&wc, sizeof wc); wc.lpfnWndProc = WndMainProc; wc.hInstance = hInstance; wc.lpszClassName = CLASSNAME; wc.lpszMenuName = NULL; wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hIcon = NULL; if(!RegisterClass(&wc)) { Msg(TEXT("RegisterClass Failed! Error=0x%x\r\n"), GetLastError()); CoUninitialize(); exit(1); } // Create the main window. The WS_CLIPCHILDREN style is required. ghApp = CreateWindow(CLASSNAME, APPLICATIONNAME, WS_OVERLAPPEDWINDOW | WS_CAPTION | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, DEFAULT_VIDEO_WIDTH, DEFAULT_VIDEO_HEIGHT, 0, 0, hInstance, 0); if(ghApp) { HRESULT hr; // Create DirectShow graph and start capturing video hr = CaptureVideo(); if (FAILED (hr)) { CloseInterfaces(); DestroyWindow(ghApp); } else { // Don't display the main window until the DirectShow // preview graph has been created. Once video data is // being received and processed, the window will appear // and immediately have useful video data to display. // Otherwise, it will be black until video data arrives. ShowWindow(ghApp, nCmdShow); } // Main message loop while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } } // Release COM CoUninitialize(); return (int) msg.wParam; }
CAccessSys::~CAccessSys(void) { if (e_psCurrent == Running) StopPreview(); CloseInterfaces(); }
CVMR_Capture::~CVMR_Capture() { CloseInterfaces(); CoUninitialize( ); }