Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to buid a customized Capture Graph filter with DirectShow

Status
Not open for further replies.

jamesbond2000

Programmer
Jan 26, 2002
3
FR
Hi,
I must buid a customized (with given filters) capture graph filter but I experience problems while running the application.
This is what I do :
I create a filter graph.
I create a capture graph filter.
I link those two graphs.
I Create instances of the required filters and add them.
I use the ICaptureGraphBuilder:FindPin to retrieve the pins I need.
I connect those pins with the IGraphBuilder:Connect method.

Here is the code:

#include <windows.h>
#include <dshow.h>

#define WM_GRAPHNOTIFY WM_APP + 1
#define CLASSNAME &quot;EventNotify&quot;

IGraphBuilder *pGraph = NULL;
IMediaControl *pMediaControl = NULL;
IMediaEventEx *pEvent = NULL;
IVideoWindow *pVidWin = NULL;
IBaseFilter *pVDeComp = NULL;
IBaseFilter *pVSplit = NULL;
IBaseFilter *pVRender = NULL;
IBaseFilter *pSrc = NULL;
ICaptureGraphBuilder2 *pBuild = NULL;


HWND g_hwnd;

void PlayFile(void)
{
// Create the filter graph manager
CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void **)&pGraph);
// Create the capture filter graph manager
CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC, IID_ICaptureGraphBuilder2, (void **)&pBuild);
// Attach those two graphs
pBuild->SetFiltergraph(pGraph);

// Add the source filter
pGraph->AddSourceFilter(L&quot;C:\\Sauve\\Maurice_avi.avi&quot;, L&quot;Source Filter&quot;, &pSrc);
// Add the Avi Splitter filter
CoCreateInstance(CLSID_AviSplitter, NULL, CLSCTX_INPROC, IID_IBaseFilter, (void **)&pVSplit);
pGraph->AddFilter(pVSplit, L&quot;Splitter&quot;);
// Add the Avi Decompressor filter
CoCreateInstance(CLSID_AVIDec, NULL, CLSCTX_INPROC, IID_IBaseFilter, (void **)&pVDeComp);
pGraph->AddFilter(pVDeComp, L&quot;Decompressor&quot;);
// Add the Video Renderer filter
CoCreateInstance(CLSID_VideoRenderer, NULL, CLSCTX_INPROC, IID_IBaseFilter, (void **)&pVRender);
pGraph->AddFilter(pVRender, L&quot;Renderer&quot;);

// Retrieve all pins
IPin *outSrc = NULL;
pBuild->FindPin(pSrc,PINDIR_OUTPUT,NULL,NULL,true,0,&outSrc);

IPin *inSplit = NULL;
IPin *outSplit = NULL;
pBuild->FindPin(pVSplit,PINDIR_INPUT,NULL,NULL,true,0,&inSplit);
pBuild->FindPin(pVSplit,PINDIR_OUTPUT,NULL,NULL,true,0,&outSplit);

IPin *inDec = NULL;
IPin *outDec = NULL;
pBuild->FindPin(pVDeComp,PINDIR_INPUT,NULL,NULL,true,0,&inDec);
pBuild->FindPin(pVDeComp,PINDIR_OUTPUT,NULL,NULL,true,0,&outDec);

IPin *inRender = NULL;
pBuild->FindPin(pVRender,PINDIR_INPUT,NULL,NULL,true,0,&inRender);

// Connect all pins
pGraph->Connect(outSrc,inSplit);
pGraph->Connect(outSplit,inDec);
pGraph->Connect(outDec,inRender);


// Free Pins
outSrc->Release();
inSplit->Release();
outSplit->Release();
inDec->Release();
outDec->Release();
inRender->Release();
//pGraph->RenderFile(L&quot;C:\\Sauve\\Maurice_avi.avi&quot;, NULL);

// Specify the owner window.
pGraph->QueryInterface(IID_IVideoWindow, (void **)&pVidWin);
pVidWin->put_Owner((OAHWND)g_hwnd);
pVidWin->put_WindowStyle( WS_CHILD | WS_CLIPSIBLINGS);

// Set the owner window to receive event notices.
pGraph->QueryInterface(IID_IMediaEventEx, (void **)&pEvent);
pEvent->SetNotifyWindow((OAHWND)g_hwnd, WM_GRAPHNOTIFY, 0);

// Run the graph.
pGraph->QueryInterface(IID_IMediaControl, (void **)&pMediaControl);
pMediaControl->Run();
}

void CleanUp(void)
{
pVidWin->put_Visible(OAFALSE);
pVidWin->put_Owner(NULL);

// Stop the graph.
pMediaControl->Stop();

pMediaControl->Release();
pVidWin->Release();
pEvent->Release();
pGraph->Release();
pVDeComp->Release();
pVSplit->Release();
pVRender->Release();
pSrc->Release();

PostQuitMessage(0);
}

void HandleEvent()
{
long evCode, param1, param2;
HRESULT hr;
while (hr = pEvent->GetEvent(&evCode, &param1, &param2, 0), SUCCEEDED(hr))
{
hr = pEvent->FreeEventParams(evCode, param1, param2);
if ((EC_COMPLETE == evCode) || (EC_USERABORT == evCode))
{
CleanUp();
break;
}
}
}

long FAR PASCAL WindowProc( HWND hwnd, UINT msg, UINT wParam, LONG lParam)
{
switch (msg)
{
case WM_DESTROY:
CleanUp();
PostQuitMessage(0);
break;
case WM_GRAPHNOTIFY:
HandleEvent();
break;
default:
return (DefWindowProc(hwnd, msg, wParam, lParam));
}
return(NULL);
}

/* WindowProc goes here. Add the following case statement:
case WM_GRAPHNOTIFY:
HandleEvent();
break;
*/

// WinMain goes here.
int PASCAL WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine,
int nCmdShow )
{
MSG msg;
WNDCLASS wc;

CoInitialize(NULL);

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 = NULL;
wc.lpszClassName = CLASSNAME;
RegisterClass( &wc );

g_hwnd = CreateWindow(
CLASSNAME,
&quot;DirectShow Sample&quot;,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInst,
NULL);

ShowWindow( g_hwnd, nCmdShow );
UpdateWindow( g_hwnd );
PlayFile();

while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}

CoUninitialize();
return msg.wParam;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top