www.pudn.com > VideoWindow.rar > VideoWindow.cpp


 
//Include .h file 
#include "stdafx.h" 
#include  
//#include "MXFFilter.h" 
 
//Define macro 
#define MSGERROR(ms)	MessageBox(NULL, ms, "Error", MB_OK) 
#define SAFERELEASE(p)	if(p) p->Release(); p=NULL 
#define CLASSNAME		"MyVideoWindow" 
 
const GUID CLSID_MXFFilter = {  
	0xbcafe7c2, 0x1884, 0x4bd2, 0x9a, 0xb6, 0x7, 0x8e, 0xc2, 0xab, 0xa3, 0x57}; 
 
#define WM_GRAPHNOTIFY	WM_APP + 13 
//Gobal  declare 
	//Interface 
	IGraphBuilder*	g_pIGraphBuilder	= NULL; 
	IMediaControl*	g_pIMediaControl	= NULL; 
	IMediaEventEx*	g_pIMediaEventEx	= NULL; 
	IVideoWindow*	g_pIVideoWindow		= NULL; 
	IFileSourceFilter* g_pIFileSourceFilter = NULL; 
	IBaseFilter*    g_pIAudioRender     = NULL; 
	//Date 
	HINSTANCE	g_hInstance = NULL; 
	HWND		g_hwnd = NULL; 
	static TCHAR MenuName[10] = "MENUDEMO"; 
	 
// Function Declare 
	BOOL	QueryAllInterface(); 
	void	SafeReleaseInterface(); 
	BOOL	CreateWindowMain(HINSTANCE hInst); 
	long __stdcall  WindowProc( HWND hwnd, UINT msg, UINT wParam, LONG lParam); 
	void	PlayFile(); 
	void	Pause(); 
	void	CleanUp(); 
	void	SetVideoPos(); 
	void	HandleEvent(); 
	void	Move(); 
	HRESULT RenderOutputPins(IGraphBuilder *pGB, IBaseFilter *pFilter); 
//////////////// 
//////////////// 
// Main Cpp 
int __stdcall	WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) 
{ 
	//COM library initialize and query all interface we need. 
	if (!QueryAllInterface()) { 
		return(0); 
	} 
	 
	//Create Main Window 
	if (!CreateWindowMain(hInstance)) { 
		MSGERROR("Failed to create main window"); 
		return(0); 
	} 
	 
	if (g_pIGraphBuilder) { 
	//	g_pIGraphBuilder->Connect() 
	} 
 
	if (FAILED(g_pIMediaEventEx->SetNotifyWindow(OAHWND(g_hwnd),WM_GRAPHNOTIFY, 0))) { 
		MSGERROR("Set Notify Failed"); 
		return FALSE; 
	} 
	 
	//Handler message  
	MSG msg; 
	while( GetMessage( &msg, NULL, 0, 0 ) ) 
    { 
        TranslateMessage( &msg ); 
        DispatchMessage( &msg ); 
    }	 
 
	//Release we got interface and COM library 
	SafeReleaseInterface();   
	return(1); 
} 
 
 
BOOL	QueryAllInterface() { 
	BOOL result=TRUE; 
	//Initialize COM Library 
	CoInitialize(NULL); 
     
    // Create the filter graph manager and query for interfaces. 
    CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,  
		IID_IGraphBuilder, (void **)&g_pIGraphBuilder); 
 
	if (FAILED(CoCreateInstance(CLSID_MXFFilter, NULL, CLSCTX_INPROC_SERVER,IID_IFileSourceFilter,(void **)&g_pIFileSourceFilter))) { 
		MSGERROR("Failed to Create Mxf File Source Filter"); 
		result = FALSE; 
	} 
	 
	if (FAILED(CoCreateInstance(CLSID_VideoRenderer, NULL, CLSCTX_INPROC_SERVER,IID_IBaseFilter,(void **)&g_pIAudioRender))) { 
		MSGERROR("Failed to Create Mxf File Source Filter"); 
		result = FALSE; 
	}	 
 
	 
 
	if (g_pIGraphBuilder && g_pIFileSourceFilter) { 
		 
		IBaseFilter* pOutBaseFilter = NULL; 
		if (FAILED(g_pIFileSourceFilter->Load(L"E:\\E0006_Pal.MXF",NULL))) { 
			MSGERROR("Failed"); 
		} 
		g_pIFileSourceFilter->QueryInterface(IID_IBaseFilter,(void **)&pOutBaseFilter); 
		 
		g_pIGraphBuilder->AddFilter(pOutBaseFilter, L"Mxf File Source Filter"); 
		//g_pIGraphBuilder->AddFilter(g_pIAudioRender, L"Audio Rencer"); 
		if (FAILED(RenderOutputPins(g_pIGraphBuilder, pOutBaseFilter))) { 
			 
		} 
		 
	} 
	 
	//Query Interface  
    if(FAILED(g_pIGraphBuilder->QueryInterface(IID_IMediaControl, (void **)&g_pIMediaControl))) { 
		MSGERROR("Failed to Create Filter Graph Manager"); 
		result = FALSE; 
	} 
	 
    if(FAILED(g_pIGraphBuilder->QueryInterface(IID_IMediaEventEx, (void **)&g_pIMediaEventEx))){ 
		MSGERROR("Failed to Query Interface IMediaEvent"); 
		result = FALSE; 
	} 
	 
	if(FAILED(g_pIGraphBuilder->QueryInterface(IID_IVideoWindow, (void **)&g_pIVideoWindow))){ 
		MSGERROR("Failed to Query Interface IMediaEvent"); 
		result = FALSE; 
	} 
 
	return result; 
} 
 
 
 
void	SafeReleaseInterface() { 
	// Clean up. 
	SAFERELEASE(g_pIVideoWindow); 
	SAFERELEASE(g_pIMediaEventEx); 
	SAFERELEASE(g_pIMediaControl); 
	SAFERELEASE(g_pIGraphBuilder); 
	//UnInitialize the COM library. 
    CoUninitialize(); 
} 
 
BOOL	CreateWindowMain(HINSTANCE hInst) { 
     
	WNDCLASS    wc; 
	 
    ZeroMemory(&wc, sizeof wc); 
 
    wc.lpfnWndProc  = WindowProc; 
    wc.hInstance    = hInst; 
    wc.hIcon        = LoadIcon( NULL, IDI_APPLICATION ); 
    wc.hCursor      = LoadCursor( NULL, IDC_ARROW ); 
    wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH ); 
    wc.lpszMenuName = MenuName; 
    wc.lpszClassName = CLASSNAME; 
    RegisterClass( &wc ); 
	 
    g_hwnd = CreateWindow( 
        CLASSNAME, 
        "Qingzhujunzi Movie verion 1", 
        WS_OVERLAPPEDWINDOW, 
        CW_USEDEFAULT, 
        CW_USEDEFAULT, 
        CW_USEDEFAULT, 
        CW_USEDEFAULT, 
        NULL, 
        NULL, 
        hInst, 
        NULL); 
	if (!g_hwnd) { 
		return FALSE; 
	} 
	//ShowWindow( g_hwnd, SW_NORMAL); 
	long wideofVideo = 400; 
	long hightofVideo = 300; 
	g_pIVideoWindow->get_Width(&wideofVideo); 
	g_pIVideoWindow->get_Height(&wideofVideo); 
	SetWindowPos(g_hwnd, NULL, 400,400, wideofVideo,hightofVideo,SWP_NOMOVE|SWP_SHOWWINDOW); 
	UpdateWindow(g_hwnd); 
	return TRUE; 
} 
 
long __stdcall  WindowProc( HWND hwnd, UINT msg, UINT wParam, LONG lParam) { 
	switch (msg) 
    { 
	case WM_DESTROY: 
		PostQuitMessage(0); 
		CleanUp(); 
		break; 
	case WM_LBUTTONDOWN: 
		PlayFile(); 
		break; 
	case WM_COMMAND: 
		if (LOWORD(wParam) == IDM_FORME ) { 
			Pause(); 
		} 
		break; 
	case WM_SIZE: 
		SetVideoPos(); 
		break; 
	case WM_GRAPHNOTIFY: 
		HandleEvent(); 
		break; 
	case WM_MOVE: 
		Move(); 
    }	 
	return (DefWindowProc(hwnd, msg, wParam, lParam)); 
} 
 
void	PlayFile() {	 
	//Set the video window. 
    g_pIVideoWindow->put_Owner((OAHWND)g_hwnd); 
    g_pIVideoWindow->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS); 
	 
	RECT grc; 
    GetClientRect(g_hwnd, &grc); 
    g_pIVideoWindow->SetWindowPosition(0, 0, grc.right, grc.bottom);	 
	if (FAILED(g_pIMediaControl->Run())) { 
		MSGERROR("Error"); 
	} 
} 
 
void	Pause() { 
	static BOOL isPause = FALSE; 
	 
	if (isPause) { 
		g_pIMediaControl->Run(); 
		isPause = FALSE; 
	} else { 
		g_pIMediaControl->Pause(); 
		isPause = TRUE; 
	} 
} 
 
void	CleanUp(){ 
	// Stop the graph. 
    g_pIMediaControl->Stop(); 
	 
	g_pIVideoWindow->put_Visible(OAFALSE); 
    g_pIVideoWindow->put_Owner(NULL);  
	 
	g_pIMediaEventEx->SetNotifyWindow(NULL, 0, 0);	 
     
	 
} 
 
void	SetVideoPos(){ 
	RECT grc; 
    GetClientRect(g_hwnd, &grc); 
	g_pIVideoWindow->put_Top(grc.top); 
	g_pIVideoWindow->put_Left(grc.left); 
	g_pIVideoWindow->put_Width(grc.right-grc.left); 
	g_pIVideoWindow->put_Height(grc.bottom-grc.top); 
 
} 
 
void HandleEvent() { 
	long evCode, param1, param2; 
    HRESULT hr; 
	 
    
    while (hr = g_pIMediaEventEx->GetEvent(&evCode, ¶m1, ¶m2, 0), SUCCEEDED(hr)) 
    {  
        hr = g_pIMediaEventEx->FreeEventParams(evCode, param1, param2); 
        if (EC_PAUSED == evCode) 
        {  
			MSGERROR("Pause"); 
            break; 
        }  
    }  
} 
 
void Move() { 
	RECT rect; 
	GetClientRect(g_hwnd, &rect); 
 
	g_pIVideoWindow->SetWindowPosition(rect.left,rect.top,rect.right - rect.left,rect.bottom - rect.top); 
} 
 
HRESULT RenderOutputPins(IGraphBuilder *pGB, IBaseFilter *pFilter) 
{ 
	HRESULT			hr = S_OK; 
	IEnumPins *		pEnumPin = NULL; 
	IPin *			pConnectedPin = NULL, * pPin = NULL; 
	PIN_DIRECTION	PinDirection; 
	ULONG			ulFetched; 
	 
    // Enumerate all pins on the filter 
	hr = pFilter->EnumPins(&pEnumPin); 
	int i =0; 
	if(SUCCEEDED(hr)) 
	{ 
        // Step through every pin, looking for the output pins 
		while (S_OK == (hr = pEnumPin->Next(1L, &pPin, &ulFetched))) 
		{ 
            // Is this pin connected?  We're not interested in connected pins. 
			hr = pPin->ConnectedTo(&pConnectedPin); 
			if (pConnectedPin) 
			{ 
				pConnectedPin->Release(); 
				pConnectedPin = NULL; 
			} 
			 
            // If this pin is not connected, render it. 
			if (VFW_E_NOT_CONNECTED == hr) 
			{ 
				hr = pPin->QueryDirection(&PinDirection); 
				if ((S_OK == hr) && (PinDirection == PINDIR_OUTPUT)) 
				{ 
                    if (i++ < 3) { 
						hr = pGB->Render(pPin); 
					} 
 
				} 
			} 
			pPin->Release(); 
			 
            // If there was an error, stop enumerating 
            if (FAILED(hr))                       
                break; 
		} 
	} 
	 
    // Release pin enumerator 
	pEnumPin->Release(); 
	return hr; 
}