|
|
//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright � 2000-2002. All Rights Reserved.
// Version 1.0 24/03/2000
// Last modification: 04/03/2003
// Project Intruder Position Indicator
// SUBSYSTEM: Video camera showing and captiring window
// FILE: VideoDirectShow.cpp
// AUTHOR: Chizhevskiy Sergey
// OVERVIEW
// ~~~~~~~~
// Source file for implementation of class CVideoDirectShow
//----------------------------------------------------------------------------
#include "StdAfx.h"
#include "..\LgLib\Version.h"
#include "VideoDirectShow.h"
//#include "Resource.h"
const GUID IID_ICaptureDIB = { 0xfd5010a3, 0x8ebe, 0x11ce, 0x81, 0x83, 0x00, 0xaa, 0x00, 0x57, 0x7d, 0xa1 };
BOOL CVideoDirectShow::Create( LPCTSTR pFileName ){ HRESULT hr = ::CoCreateInstance( CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void **) &m_pGraph ); if( SUCCEEDED( hr ) ) { hr = m_pGraph->QueryInterface( IID_IMediaControl, (void**)&m_pMediaControl ); if( SUCCEEDED( hr ) ) { WCHAR pwFileName[ MAX_PATH ] = L"";
::MultiByteToWideChar( CP_ACP, 0, pFileName, pFileName ? strlen( pFileName ) : 0, pwFileName, MAX_PATH );
hr = m_pGraph->RenderFile( pwFileName, NULL ); if( SUCCEEDED( hr ) ) { FindVideos();
m_pMediaControl->Run();/*
m_pMediaControl->Stop();
OAFilterState fs = 1;
m_pMediaControl->GetState( INFINITE, &fs );
m_pMediaControl->Run();*/ return TRUE; } } } return FALSE;}
CVideoDirectShow::CVideoDirectShow(){ m_pGraph = NULL; m_pMediaControl = NULL;
m_nCount = 0; ::ZeroMemory( m_ppVideoWindow, sizeof(m_ppVideoWindow) ); ::ZeroMemory( m_ppCaptureDIB, sizeof(m_ppCaptureDIB) ); ::ZeroMemory( m_phWnd, sizeof(m_phWnd) );
::CoInitializeEx( NULL, COINIT_MULTITHREADED );}
CVideoDirectShow::~CVideoDirectShow(){ if( m_pMediaControl ) { m_pMediaControl->Stop(); OAFilterState fs = 1; m_pMediaControl->GetState( INFINITE, &fs ); m_pMediaControl->Release(); m_pMediaControl = NULL; }
for( int i = 0; i < m_nCount; i++ ) { if( m_ppCaptureDIB[i] ) { m_ppCaptureDIB[i]->Release(); m_ppCaptureDIB[i] = NULL; } if( m_ppVideoWindow[i] ) { m_ppVideoWindow[i]->Release(); m_ppVideoWindow[i] = NULL; } }
if( m_pGraph ) { m_pGraph->Release(); m_pGraph = NULL; }
::CoUninitialize();}
BOOL CVideoDirectShow::Capture( int n, LPBITMAPINFO pBmi, LPBYTE pBytes ){ if( n < m_nCount && m_ppCaptureDIB[n] ) { LPBITMAPINFOHEADER pBmih; LPBYTE pBuffer; HRESULT hr = m_ppCaptureDIB[n]->get_DIB( &pBmih, &pBuffer ); if( SUCCEEDED(hr) ) { LPBYTE pDstBytes = NULL; HBITMAP hDstBitmap = ::CreateDIBSection( 0UL, pBmi, DIB_RGB_COLORS, (void**)&pDstBytes, NULL, 0UL ); if( hDstBitmap ) { HWND hWnd = ::GetDesktopWindow(); HDC hDC = ::GetDC( hWnd ); HDC hCopyDC = ::CreateCompatibleDC( hDC ); ::ReleaseDC( hWnd, hDC );
HBITMAP hOldBitmap = (HBITMAP)::SelectObject( hCopyDC, hDstBitmap ); int nOldMode = ::SetStretchBltMode( hCopyDC, COLORONCOLOR ); ::StretchDIBits( hCopyDC, 0, 0, pBmi->bmiHeader.biWidth, pBmi->bmiHeader.biHeight, 0, 0, pBmih->biWidth, pBmih->biHeight, pBuffer, (LPBITMAPINFO)pBmih, DIB_RGB_COLORS, SRCCOPY ); ::SetStretchBltMode( hCopyDC, nOldMode ); ::SelectObject( hCopyDC, hOldBitmap );
::CopyMemory( pBytes, pDstBytes, pBmi->bmiHeader.biSizeImage );
::DeleteDC( hCopyDC );
::DeleteObject( hDstBitmap ); }/*
if( m_phWnd[n]->OpenClipboard() ) { HGLOBAL hMem = ::GlobalAlloc( GMEM_MOVEABLE, pBmih->biSizeImage + sizeof(BITMAPINFOHEADER) ); LPBYTE pMem = (LPBYTE)::GlobalLock( hMem ); ::CopyMemory( pMem, pBmih, sizeof(BITMAPINFOHEADER) ); ::CopyMemory( pMem + sizeof(BITMAPINFOHEADER), pBuffer, pBmih->biSizeImage ); ::GlobalUnlock( hMem ); ::SetClipboardData( CF_DIB, hMem ); ::CloseClipboard(); }*/ ::CoTaskMemFree( pBmih ); ::CoTaskMemFree( pBuffer ); } return TRUE; } return FALSE;}
BOOL CVideoDirectShow::FindVideos(){ IEnumFilters *pEnum = NULL; IBaseFilter *pFilter = NULL; ULONG cFetched; HRESULT hr;
m_pGraph->EnumFilters(&pEnum);
while( pEnum->Next(1, &pFilter, &cFetched) == S_OK ) { IVideoWindow *pVideoWindow = NULL; hr = pFilter->QueryInterface( IID_IVideoWindow, (void**)&pVideoWindow ); if( SUCCEEDED( hr ) ) { CGuidList list; m_ppCaptureDIB[m_nCount] = FindCaptureDIB( pFilter, list ); if( m_ppCaptureDIB[m_nCount] ) { m_ppCaptureDIB[m_nCount]->put_Scale( 1.0, 1.7 ); m_ppCaptureDIB[m_nCount]->put_Shift( 0.0, 0.0 ); m_ppVideoWindow[m_nCount] = pVideoWindow;// m_ppVideoWindow[m_nCount]->put_AutoShow( OAFALSE );
// m_ppVideoWindow[m_nCount]->put_Visible( OAFALSE );
m_nCount++; } pVideoWindow->put_AutoShow( OAFALSE ); pVideoWindow->put_Visible( OAFALSE ); } pFilter->Release(); } pEnum->Release(); return TRUE;}
ICaptureDIB *CVideoDirectShow::FindCaptureDIB( IBaseFilter* pFilter, CGuidList &list ){ IEnumPins *pEnumPins = NULL; IPin *pPinIn = NULL; IPin *pPinOut = NULL; ICaptureDIB *pCaptureDIB = NULL; IPersist *pPersist = NULL;
ULONG cFetched; HRESULT hr;
hr = pFilter->EnumPins( &pEnumPins ); if( SUCCEEDED(hr) ) { while( pEnumPins->Next( 1, &pPinIn, &cFetched ) == S_OK && !pCaptureDIB ) { hr = pPinIn->ConnectedTo( &pPinOut ); if( SUCCEEDED( hr ) ) { PIN_INFO PinInfo; hr = pPinOut->QueryPinInfo( &PinInfo ); if( SUCCEEDED( hr ) ) {/* if( PinInfo.dir == PINDIR_INPUT )
{ if( PinInfo.pFilter ) PinInfo.pFilter->Release(); } else*/ if( PinInfo.pFilter ) { hr = PinInfo.pFilter->QueryInterface( IID_IPersist, (void**)&pPersist ); if( SUCCEEDED( hr ) ) { CLSID clsid; hr = pPersist->GetClassID( &clsid ); if( SUCCEEDED( hr ) ) { if( !list.Find( clsid ) ) { list.AddTail( clsid );// if( !IsCompatible( PinInfo.pFilter, IID_IOverlay ) )
{ hr = PinInfo.pFilter->QueryInterface( IID_ICaptureDIB, (void**)&pCaptureDIB ); if( FAILED( hr ) ) pCaptureDIB = FindCaptureDIB( PinInfo.pFilter, list ); } } PinInfo.pFilter->Release(); } } } } pPinOut->Release(); } pPinIn->Release(); } pEnumPins->Release(); }
return pCaptureDIB;}
bool CVideoDirectShow::IsCompatible( IBaseFilter *pFilter, REFIID IID ){ HRESULT hr;
if( !pFilter ) return false;
IUnknown *pUnknown = NULL;
hr = pFilter->QueryInterface( IID, (void**)&pUnknown );
if( FAILED( hr ) ) { return false; }
pUnknown->Release();
return true;}
BOOL CVideoDirectShow::SetupFilters(){
IEnumFilters *pEnum = NULL; IBaseFilter *pFilter = NULL; ULONG cFetched;
m_pGraph->EnumFilters(&pEnum); while(pEnum->Next(1, &pFilter, &cFetched) == S_OK) { ISpecifyPropertyPages *pSpecify; HRESULT hr = pFilter->QueryInterface(IID_ISpecifyPropertyPages, (void **)&pSpecify); if (SUCCEEDED(hr)) { FILTER_INFO FilterInfo; pFilter->QueryFilterInfo(&FilterInfo);
CAUUID caGUID; pSpecify->GetPages(&caGUID); pSpecify->Release(); OleCreatePropertyFrame( NULL, // Parent window
0, // x (Reserved)
0, // y (Reserved)
FilterInfo.achName, // Caption for the dialog box
1, // Number of filters
(IUnknown **)&pFilter, // Pointer to the filter
caGUID.cElems, // Number of property pages
caGUID.pElems, // Pointer to property page CLSIDs
0, // Locale identifier
0, // Reserved
NULL // Reserved
); CoTaskMemFree(caGUID.pElems); FilterInfo.pGraph->Release(); } pFilter->Release(); } pEnum->Release();
return TRUE;}
BOOL CVideoDirectShow::ConnectWnd(int n, HWND hWnd, int x, int y, int cx, int cy){ if( n < m_nCount && m_ppVideoWindow[n] ) { m_phWnd[n] = hWnd; m_ppVideoWindow[n]->put_Owner( (OAHWND) m_phWnd[n] ); m_ppVideoWindow[n]->put_WindowStyle( WS_CHILD | WS_CLIPCHILDREN ); m_ppVideoWindow[n]->SetWindowPosition( x, y, cx, cy ); m_ppVideoWindow[n]->put_AutoShow( OATRUE ); m_ppVideoWindow[n]->put_Visible( OATRUE ); return TRUE; } return FALSE;}
void CVideoDirectShow::SetWindowPosition( int n, int x, int y, int cx, int cy ){ if( n < m_nCount && m_ppVideoWindow[n] ) { m_ppVideoWindow[n]->SetWindowPosition( x, y, cx, cy+180 ); m_pMediaControl->Run(); }}
void CVideoDirectShow::ShowWindow( int n, int nShow ){ if( n < m_nCount ) { if( nShow == SW_SHOW ) { // m_pMediaControl->Run();
m_ppVideoWindow[n]->put_Visible( OATRUE ); } else { m_ppVideoWindow[n]->put_Visible( OAFALSE ); } }}
/*
BOOL CVideoDirectShow::SetupDialog( CWnd *pWnd, int n ){ if( n < m_nCount ) { HWND hWndOwner = pWnd->GetSafeHwnd(); HINSTANCE hinst = (HINSTANCE)GetWindowLong( hWndOwner, GWL_HINSTANCE );
IBaseFilter *pFilter = NULL; m_ppVideoWindow[n]->QueryInterface( IID_IBaseFilter, (void**)&pFilter ); if( pFilter ) { CGuidList gList; EnumFilters( pFilter, gList, m_SpecifyList ); pFilter->Release(); ::DialogBoxParam( hinst, (LPCTSTR)IDD_VIDEO_SETUP_DLG, hWndOwner, (DLGPROC) DialogProcSetup, (LPARAM)this );
POSITION pos = m_SpecifyList.GetHeadPosition(); while( pos ) { PROPERTY prop = m_SpecifyList.GetNext( pos ); prop.pSpecify->Release(); prop.pUnknown->Release(); } m_SpecifyList.RemoveAll(); } }
return TRUE;}
BOOL CVideoDirectShow::DialogProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ){ switch( uMsg ) { case WM_INITDIALOG: { HINSTANCE hInst = (HINSTANCE)::GetWindowLong( hWnd, GWL_HINSTANCE );
HFONT hFont = (HFONT)::SendMessage( hWnd, WM_GETFONT, 0, 0 );
int i = 0; POSITION pos = m_SpecifyList.GetHeadPosition(); CRect bRect; while( pos ) { PROPERTY prop = m_SpecifyList.GetNext( pos ); CRect rect( 2, 2 + i * 14, 102, 14 + i * 14 ); CRect fRect( &rect ); fRect.bottom += 4; fRect.right += 4; ::MapDialogRect( hWnd, &rect ); ::MapDialogRect( hWnd, &fRect ); if( i ) bRect |= fRect; else bRect = fRect;
CHAR pBuffer[MAX_FILTER_NAME];
::WideCharToMultiByte( CP_ACP, 0, prop.pName, -1, pBuffer, MAX_FILTER_NAME, NULL, NULL );
CRect *pRect = ▭ HWND hChildWnd = ::CreateWindow( "BUTTON", pBuffer, WS_CHILD | WS_VISIBLE, pRect->left, pRect->top, pRect->Width(), pRect->Height(), hWnd, NULL, hInst, 0UL ); ::SendMessage( hChildWnd, WM_SETFONT, (WPARAM)hFont, TRUE ); ::SetWindowLong( hChildWnd, GWL_ID, i + 20 );
i++; } CRect cRect, wRect; GetClientRect( hWnd, &cRect ); GetWindowRect( hWnd, &wRect ); int cx = wRect.Width() - cRect.Width(); int cy = wRect.Height() - cRect.Height();
::MoveWindow( hWnd, wRect.left, wRect.top, cx + bRect.Width(), cy + bRect.Height(), TRUE );
} return TRUE;
case WM_COMMAND: { int i = 20; POSITION pos = m_SpecifyList.GetHeadPosition(); while( pos ) { PROPERTY prop = m_SpecifyList.GetNext( pos ); if( i == LOWORD( wParam ) ) { CAUUID caGUID; prop.pSpecify->GetPages(&caGUID); OleCreatePropertyFrame( hWnd, // Parent window
0, // x (Reserved)
0, // y (Reserved)
prop.pName, // Caption for the dialog box
1, // Number of filters
(IUnknown **)&prop.pUnknown, // Pointer to the filter
caGUID.cElems, // Number of property pages
caGUID.pElems, // Pointer to property page CLSIDs
0, // Locale identifier
0, // Reserved
NULL // Reserved
); CoTaskMemFree(caGUID.pElems); break; } i++; } } return TRUE;
case WM_CLOSE: return ::EndDialog( hWnd, IDCANCEL ); } return FALSE;}*/void CVideoDirectShow::EnumFilters( IBaseFilter* pFilter, CGuidList &gList, CSpecifyList &sList ){ IEnumPins *pEnumPins = NULL; IPin *pPinIn = NULL; IPin *pPinOut = NULL; IPersist *pPersist = NULL;
ULONG cFetched; HRESULT hr;
hr = pFilter->EnumPins( &pEnumPins ); if( SUCCEEDED(hr) ) { while( pEnumPins->Next( 1, &pPinIn, &cFetched ) == S_OK ) { hr = pPinIn->ConnectedTo( &pPinOut ); if( SUCCEEDED( hr ) ) { PIN_INFO PinInfo; hr = pPinOut->QueryPinInfo( &PinInfo ); if( SUCCEEDED( hr ) ) { if( PinInfo.pFilter ) { hr = PinInfo.pFilter->QueryInterface( IID_IPersist, (void**)&pPersist ); if( SUCCEEDED( hr ) ) { CLSID clsid; hr = pPersist->GetClassID( &clsid ); if( SUCCEEDED( hr ) ) { if( !gList.Find( clsid ) ) { gList.AddTail( clsid ); PROPERTY prop; hr = PinInfo.pFilter->QueryInterface( IID_ISpecifyPropertyPages, (void**)&prop.pSpecify ); if( SUCCEEDED( hr ) ) { hr = PinInfo.pFilter->QueryInterface( IID_IUnknown, (void**)&prop.pUnknown ); FILTER_INFO FilterInfo; hr = PinInfo.pFilter->QueryFilterInfo(&FilterInfo); if( SUCCEEDED( hr ) ) { wcscpy( prop.pName, FilterInfo.achName ); if( FilterInfo.pGraph ) FilterInfo.pGraph->Release(); } else wcscpy( prop.pName, L"Unknown" ); sList.AddTail( prop ); } EnumFilters( PinInfo.pFilter, gList, sList ); } PinInfo.pFilter->Release(); } } } } pPinOut->Release(); } pPinIn->Release(); } pEnumPins->Release(); }}
|