www.pudn.com > Face3DModel.zip > VideoFile.cpp
// VideoFile.cpp: implementation of the CVideoFile class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Face3DModel.h"
#include "VideoFile.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CVideoFile::CVideoFile() : m_pVW(NULL)
, m_fConnect(FALSE)
{
}
CVideoFile::~CVideoFile()
{
if (m_fConnect)
{
Disconnect();
}
}
BOOL CVideoFile::Connect()
{
static OPENFILENAME ofn={0};
TCHAR szFilename[MAX_PATH] = _T("");
// Fill in standard structure fields
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = AfxGetMainWnd()->GetSafeHwnd();
ofn.lpstrFilter = _T("Video Files (*.avi; *.qt; *.mov; *.mpg; *.mpeg; *.m1v)\0*.avi; *.qt; *.mov; *.mpg; *.mpeg; *.m1v\0All Files (*.*)\0*.*\0\0");
ofn.lpstrCustomFilter = NULL;
ofn.nFilterIndex = 1;
ofn.lpstrFile = szFilename;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrTitle = TEXT("Open Media File...\0");
ofn.lpstrFileTitle = NULL;
ofn.lpstrDefExt = TEXT("*\0");
ofn.Flags = OFN_FILEMUSTEXIST | OFN_READONLY | OFN_PATHMUSTEXIST;
ofn.lpstrInitialDir = NULL;
// Create the standard file open dialog and return its result
if (GetOpenFileName((LPOPENFILENAME)&ofn))
{
HRESULT hr = E_FAIL;
// Create the filter graph.
hr = m_pGraph.CoCreateInstance(CLSID_FilterGraph);
if (FAILED(hr))
{
m_pGraph.Release();
return FALSE;
}
if (IsWindowsMediaFile(szFilename))
{
hr = CoCreateInstance(CLSID_WMAsfReader, NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (void **) &m_pSrc);
hr = m_pGraph->AddFilter(m_pSrc, _T("Source"));
IFileSourceFilter* pFileSource = NULL;
hr = m_pSrc->QueryInterface(IID_IFileSourceFilter, (void **) &pFileSource);
hr = pFileSource->Load(szFilename, NULL);
if (pFileSource != NULL)
{
pFileSource->Release();
}
}
else
{
hr = m_pGraph->AddSourceFilter(szFilename, L"Source", &m_pSrc);
}
hr = m_pSample.CoCreateInstance(CLSID_SampleGrabber);
hr = m_pSample->QueryInterface(IID_ISampleGrabber,
reinterpret_cast(&m_pSampleGrabber));
hr = m_pGraph->AddFilter(m_pSample, L"SampleGrabber");
AM_MEDIA_TYPE mt;
ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
mt.majortype = MEDIATYPE_Video;
mt.subtype = MEDIASUBTYPE_RGB24;
hr = m_pSampleGrabber->SetMediaType(&mt);
m_pSampleGrabber->SetOneShot(FALSE);
m_pSampleGrabber->SetBufferSamples(TRUE);
hr = RenderOutputPins(m_pGraph, m_pSrc);
if (FAILED(hr))
{
Disconnect();
return FALSE;
}
CComPtr pPin, pPinConnect;
hr = GetPin(m_pSample, PINDIR_INPUT, &pPin);
hr = pPin->ConnectedTo(&pPinConnect);
// Pin is not connected
if (hr == VFW_E_NOT_CONNECTED)
{
//CComPtr pColorConvert;
//hr = CoCreateInstance(CLSID_Colour, NULL, CLSCTX_INPROC_SERVER,
// IID_IBaseFilter, (void **) &pColorConvert);
//hr = m_pGraph->AddFilter(pColorConvert, _T("Color Space"));
hr = InsertSampleFilter(m_pGraph, m_pSrc, m_pSample);
if (FAILED(hr))
{
Disconnect();
return FALSE;
}
}
hr = m_pGraph->QueryInterface(IID_IVideoWindow, (void **)&m_pVW);
hr = m_pGraph->QueryInterface(IID_IBasicVideo, (void **)&m_pBasicVideo);
m_fConnect = TRUE;
return TRUE;
}
return FALSE;
}
BOOL CVideoFile::Disconnect()
{
if (m_pVW != NULL)
{
m_pVW->get_Owner(NULL);
m_pVW->put_Visible(OAFALSE);
m_pVW->Release();
m_pVW = NULL;
}
m_pSampleGrabber.Release();
m_pSrc.Release();
m_pBasicVideo.Release();
m_pSample.Release();
m_pGraph.Release();
m_fConnect = FALSE;
return TRUE;
}
BOOL CVideoFile::IsConnect()
{
return m_fConnect;
}
BOOL CVideoFile::GetSize(long *pWidth, long *pHeight)
{
return SUCCEEDED(m_pBasicVideo->GetVideoSize(pWidth, pHeight));
}
BOOL CVideoFile::SetVideoWindow(HWND hWnd, long x, long y, long width, long height)
{
HRESULT hr = S_OK;
if (SUCCEEDED(hr))
{
hr = m_pVW->put_Visible(FALSE);
}
if (SUCCEEDED(hr))
{
hr = m_pVW->put_Owner((OAHWND)hWnd);
}
if (SUCCEEDED(hr))
{
hr = m_pVW->put_WindowStyle(WS_CHILD);
}
if (SUCCEEDED(hr))
{
hr = m_pVW->SetWindowPosition(x, y, width, height); // be this big
}
if (SUCCEEDED(hr))
{
hr = m_pVW->put_Visible(OATRUE);
}
return SUCCEEDED(hr);
}
BOOL CVideoFile::Run()
{
CComQIPtr pControl(m_pGraph);
return SUCCEEDED(pControl->Run());
}
BOOL CVideoFile::Pause()
{
CComQIPtr pControl(m_pGraph);
return SUCCEEDED(pControl->Pause());
}
BOOL CVideoFile::Stop()
{
CComQIPtr pControl(m_pGraph);
return SUCCEEDED(pControl->Stop());
}
LONG CVideoFile::GetState()
{
CComQIPtr pControl(m_pGraph);
OAFilterState filterState;
if (SUCCEEDED(pControl->GetState(50, &filterState)))
{
return filterState;
}
return State_Stopped;
}
BOOL CVideoFile::GetCurrentBuffer(long * pBufferSize, long *pBuffer)
{
//return SUCCEEDED(m_pSampleGrabber->GetCurrentBuffer(pBufferSize, pBuffer));
HRESULT hr = m_pSampleGrabber->GetCurrentBuffer(pBufferSize, pBuffer);
if (FAILED(hr))
{
CString strInfo;
strInfo.Format(_T("%x"), hr);
AfxMessageBox(strInfo);
}
return SUCCEEDED(hr);
}