// DXGraph.cpp: implementation of the CMyDWGraph class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "MovePlayer.h" #include "MyDWGraph.h" #include #include #include #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CMyDWGraph::CMyDWGraph() { CoInitialize(NULL); m_pGraph = NULL; m_pMediaControl = NULL; m_pMediaEvent = NULL; m_pVideoWindow = NULL; m_pBasicVideo = NULL; m_pBasicAudio = NULL; m_pMediaSeeking = NULL; //m_pMediaContent = NULL; } CMyDWGraph::~CMyDWGraph() { CoUninitialize(); } BOOL CMyDWGraph::Create() { if (!m_pGraph) { if (SUCCEEDED(CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&m_pGraph))) { return QueryInterfaces(); } m_pGraph = NULL; } return false; } BOOL CMyDWGraph::QueryInterfaces() { if (m_pGraph) { HRESULT hr = NOERROR; hr |= m_pGraph->QueryInterface(IID_IMediaControl, (void **)&m_pMediaControl); hr |= m_pGraph->QueryInterface(IID_IMediaEventEx, (void **)&m_pMediaEvent); hr |= m_pGraph->QueryInterface(IID_IBasicVideo, (void **)&m_pBasicVideo); hr |= m_pGraph->QueryInterface(IID_IBasicAudio, (void **)&m_pBasicAudio); hr |= m_pGraph->QueryInterface(IID_IVideoWindow, (void **)&m_pVideoWindow); hr |= m_pGraph->QueryInterface(IID_IMediaSeeking, (void **)&m_pMediaSeeking); //hr |= m_pGraph->QueryInterface(IID_IAMMediaContent, (void **)&m_pMediaContent); return SUCCEEDED(hr); } return false; } void CMyDWGraph::Release() { if (m_pMediaSeeking) { m_pMediaSeeking->Release(); m_pMediaSeeking = NULL; } if (m_pMediaControl) { m_pMediaControl->Release(); m_pMediaControl = NULL; } if (m_pMediaEvent) { m_pMediaEvent->Release(); m_pMediaEvent = NULL; } if (m_pBasicVideo) { m_pBasicVideo->Release(); m_pBasicVideo = NULL; } if (m_pBasicAudio) { m_pBasicAudio->Release(); m_pBasicAudio = NULL; } //if (m_pMediaContent) //{ // m_pMediaContent->Release(); // m_pMediaContent = NULL; //} if (m_pVideoWindow) { m_pVideoWindow->put_Visible(OAFALSE); m_pVideoWindow->put_MessageDrain((OAHWND)NULL); m_pVideoWindow->put_Owner(OAHWND(0)); m_pVideoWindow->Release(); m_pVideoWindow = NULL; } if (m_pGraph) { m_pGraph->Release(); m_pGraph = NULL; } } void CMyDWGraph::SetDisplayWindow(HWND inWindow) { m_hWindow = inWindow; long lngHeight = 0; long lngWidth = 0; m_pBasicVideo->get_SourceHeight(&lngHeight);//原始大小 m_pBasicVideo->get_SourceWidth(&lngWidth); m_pVideoWindow->put_Visible(OAFALSE); m_pVideoWindow->put_Owner((OAHWND)inWindow); m_pVideoWindow->put_Left(0); m_pVideoWindow->put_Top(0); m_pVideoWindow->put_Width(lngWidth); m_pVideoWindow->put_Height(lngHeight); m_pVideoWindow->put_WindowStyle(WS_CHILD|WS_CLIPCHILDREN|WS_CLIPSIBLINGS); m_pVideoWindow->put_MessageDrain((OAHWND)inWindow); } BOOL CMyDWGraph::SetFileName(char* pszFileName) { strcpy(m_szFileName, pszFileName); WCHAR szFileName[MAX_PATH]; MultiByteToWideChar(CP_ACP, 0, m_szFileName, -1, szFileName, MAX_PATH); HRESULT hr = m_pGraph->RenderFile(szFileName, NULL); return SUCCEEDED(hr); } BOOL CMyDWGraph::Play() { if(m_pGraph) { if(!IsRunning()) return SUCCEEDED(m_pMediaControl->Run()); } return false; } BOOL CMyDWGraph::Pause() { if(m_pGraph) { if(!IsPaused()) return SUCCEEDED(m_pMediaControl->Pause()); } return false; } BOOL CMyDWGraph::Stop() { if(m_pGraph) { if(!IsStopped()) return SUCCEEDED(m_pMediaControl->Stop()); } return false; } BOOL CMyDWGraph::IsRunning() { if (m_pGraph) { OAFilterState state = State_Stopped; if (SUCCEEDED(m_pMediaControl->GetState(10, &state))) return state == State_Running; } return false; } BOOL CMyDWGraph::IsStopped() { if (m_pGraph) { OAFilterState state = State_Stopped; if (SUCCEEDED(m_pMediaControl->GetState(10, &state))) return state == State_Stopped; } return false; } BOOL CMyDWGraph::IsPaused() { if (m_pGraph) { OAFilterState state = State_Stopped; if (SUCCEEDED(m_pMediaControl->GetState(10, &state))) return state == State_Paused; } return false; } long CMyDWGraph::GetSourceWidth() { if(m_pGraph) { long lngValue = 0; m_pBasicVideo->get_SourceWidth(&lngValue); //检测视频窗口的宽度 return lngValue; } return 0; } long CMyDWGraph::GetSourceHeight() { if(m_pGraph) { long lngValue = 0; m_pBasicVideo->get_SourceHeight(&lngValue); return lngValue; } return 0; } BOOL CMyDWGraph::SetWindowPos(LPRECT rc) { if(m_pVideoWindow) { return m_pVideoWindow->SetWindowPosition(0, 0, rc->right, rc->bottom); } return false; } BOOL CMyDWGraph::SnapshotBitmap(const char *pszFileName) { if (m_pBasicVideo) { long bitmapSize = 0; if (SUCCEEDED(m_pBasicVideo->GetCurrentImage(&bitmapSize, 0))) { unsigned char * buffer = new unsigned char[bitmapSize]; if (SUCCEEDED(m_pBasicVideo->GetCurrentImage(&bitmapSize, (long *)buffer))) { BITMAPFILEHEADER hdr; LPBITMAPINFOHEADER lpbi; lpbi = (LPBITMAPINFOHEADER)buffer; int nColors = 0; if (lpbi->biBitCount <= 8) nColors = 1 << lpbi->biBitCount; hdr.bfType = ((WORD) ('M' << 8) | 'B'); //always is "BM" hdr.bfSize = bitmapSize + sizeof( hdr ); hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; hdr.bfOffBits = (DWORD) (sizeof(BITMAPFILEHEADER) + lpbi->biSize + nColors * sizeof(RGBQUAD)); CFile bitmapFile(pszFileName, CFile::modeReadWrite | CFile::modeCreate | CFile::typeBinary); bitmapFile.Write(&hdr, sizeof(BITMAPFILEHEADER)); bitmapFile.Write(buffer, bitmapSize); bitmapFile.Close(); return true; } delete [] buffer; } } return false; } __int64 CMyDWGraph::GetLength() { if(m_pGraph) { __int64 len; m_pMediaSeeking->GetDuration(&len); return len; } return 0; } __int64 CMyDWGraph::GetPosition() { if(m_pGraph) { __int64 pos; m_pMediaSeeking->GetCurrentPosition(&pos); return pos; } return 0; } BOOL CMyDWGraph::SetPosition(__int64 pos) { if(m_pGraph) { if(SUCCEEDED(m_pMediaSeeking->SetPositions(&pos, AM_SEEKING_AbsolutePositioning | AM_SEEKING_SeekToKeyFrame, 0, AM_SEEKING_NoPositioning))) return true; } return false; } BOOL CMyDWGraph::SetFullScreen(BOOL bEnabled) { if (m_pGraph) { HRESULT hr = m_pVideoWindow->put_FullScreenMode(bEnabled ? OATRUE : OAFALSE); m_pVideoWindow->HideCursor(bEnabled ? OATRUE : OAFALSE); return SUCCEEDED(hr); } return false; } BOOL CMyDWGraph::GetFullScreen() { if (m_pVideoWindow) { long fullScreenMode = OAFALSE; m_pVideoWindow->get_FullScreenMode(&fullScreenMode); return (fullScreenMode == OATRUE); } return false; } BOOL CMyDWGraph::IsHaveVideo() { if (m_pVideoWindow) { return (GetSourceWidth() && GetSourceHeight()); } return false; } BOOL CMyDWGraph::SetAudioVolume(long nValue) { if (m_pBasicAudio) { HRESULT hr = m_pBasicAudio->put_Volume(nValue); return SUCCEEDED(hr); } return false; } long CMyDWGraph::GetAudioVolume() { long volume = 0; if (m_pBasicAudio) { m_pBasicAudio->get_Volume(&volume); } return volume; } BOOL CMyDWGraph::SetAudioBalance(long nValue) { if (m_pBasicAudio) { HRESULT hr = m_pBasicAudio->put_Balance(nValue); return SUCCEEDED(hr); } return false; } long CMyDWGraph::GetAudioBalance() { long balance = 0; if (m_pBasicAudio) { m_pBasicAudio->get_Balance(&balance); } return balance; } BOOL CMyDWGraph::SetNotifyWindow(HWND hwnd, long msg) { if (m_pMediaEvent) { m_pMediaEvent->SetNotifyWindow((OAHWND)hwnd, msg, 0); return true; } return false; } IMediaEventEx* CMyDWGraph::GetMediaEventPointer() { return m_pMediaEvent; }