DirectShow question

Dmitry_
Dmitry_ used Ask the Experts™
on
I'm trying to create simple application for capturing single frame:

IGraphBuilder *pGraphBuilder = NULL;
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraphBuilder);

IMediaControl *pMediaControl = NULL;
hr = pGraphBuilder->QueryInterface(IID_IMediaControl, (void **)&pMediaControl);

ICaptureGraphBuilder2 *pCapture = NULL;
hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2, (void **)&pCapture);

hr = pCapture->SetFiltergraph(pGraphBuilder);

IBaseFilter *pSrcFilter = NULL;
hr = FindVideoCaptureDevice(&pSrcFilter);

hr = pGraphBuilder->AddFilter(pSrcFilter, L"Video Capture");

hr = pCapture->RenderStream(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pSrcFilter, NULL, NULL);
   
hr = pMediaControl->Run();
::MessageBox(NULL, "waiting", "debug", MB_OK);

hr = pMediaControl->Pause();
//here hr = S_FALSE

IBasicVideo* bv = NULL;
hr = pMediaControl->QueryInterface(IID_IBasicVideo, (void **)&bv);
 
long lWidth, lHeight;
hr = bv->GetVideoSize(&lWidth, &lHeight);

long size;
hr = bv->GetCurrentImage(&size,NULL);
char* buffer = new char[size];
hr = bv->GetCurrentImage(&size,(long*)buffer);
//here hr = E_UNEXPECTED

What's wrong in this code? How can I get image ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
You should use the 'Sample Grabber' Filter. Add the filter to your graph and set:

SampleGrabber->SetBufferSamples(TRUE);
SampleGrabber->SetOneShot(TRUE);

and Run() it

Use IMediaEventEx Interface and call WaitForCompletion(INFINITE, &evCode) to wait till it's done

Commented:
Let me give a really nice feature which will remove the need to set NULL to your Interface pointers and to release them afterwards. In ATL there exists smartpointer that is really really useful and comfortable:

you declare smartpointers to specific Interface with typedef called _COM_SMART_PTR_TYPEDEF:

_COM_SMARTPTR_TYPEDEF(IGraphBuilder  , IID_IGraphBuilder);
_COM_SMARTPTR_TYPEDEF(IMediaControl  , IID_IMediaControl);
_COM_SMARTPTR_TYPEDEF(IFilterGraph   , IID_IFilterGraph);

now you have declared smartpointers to IGraphBuilder, IMediaControl and IFilterGraph. You create a smartpointer by simply adding 'Ptr' after the Interface name. To create a smartpointer to IMediaControl, you write IMediaControlPtr. Now see how easy it is to create a instance of FilterGraph and get the default interface, IGraphBuilder and get IMediaControl and IFilterGraph:

// create an instance of FilterGraph and get IGraphBuilder Interface

IGraphBuilderPtr spGraph(CLSID_FilterGraph);

// get IMediaControl interface from the instance

IMediaControlPtr spMediaControl = spGraph;

// get IFilterGraph interface from the instance

IFilterGraphPtr  spFilter       = spGraph;

this is all you have to do. Once you leave the scope, the smartpointers will call Release and take care of deletion. And this smartpointer is reference counting as well. If one interface couldn't be retrieved or an instance couldn't be created, an '_com_error' exception will be thrown. So you could use try-catch:

try {

  IGraphBuilderPtr spGraph(CLSID_FilterGraph);
  IMediaControlPtr spMediaControl = spGraph;  // say this failed -> execption
}
catch (_com_error &comerr)
{
   cout << "comerror : " << comerr.ErrorInfo() << endl;
}
   

Author

Commented:
Thank you, Kimpan.
I try to obtain instance of SampleGrabber:

hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)&pF);
I got an error: class not registered.
I'm working on Windows 2000 Service Pack 2.
Maybe I need install some update?
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Commented:
SampleGrabber is a part of DirextX 8.x SDK

Commented:
download example from www.wordware.com/directshow

Author

Commented:
I have Microsoft Platform SDK July 2002 installed.
It's contain DirectX 8.1 SDK.
I'm not understanding what the problem due creating istnace of SampleGrabber.
I download the file from www.wordware.com/directshow
It's containing 16 examples, but I can't find example of SampleGrabber.

Author

Commented:
I have Microsoft Platform SDK July 2002 installed.
It's contain DirectX 8.1 SDK.
I'm not understanding what the problem due creating istnace of SampleGrabber.
I download the file from www.wordware.com/directshow
It's containing 16 examples, but I can't find example of SampleGrabber.

Commented:
oh, too bad. I thought the example I saw in the book was there too. Microsoft DirectX 8.1 SDK comes with source code to examples. Look for project SampGrabCB in the Samples folder

Author

Commented:
I try SampGrabCB example, but I get error during creating instance of IMediaDet: file:SampGrabCB.cpp, line 95.

hr = CoCreateInstance( CLSID_MediaDet, NULL, CLSCTX_INPROC_SERVER, IID_IMediaDet, (void**) &pDet );
hr returns "class not registered"!
Maybe something missing on my DirectX SDK installation?

Commented:
try to register qedit.dll in Windows\System32\

regsvr32 windows\system32\qedit.dll

Commented:
Hi,  Dmitry_

So, how was your problem solved?   I am having the same issue, and could not find qedit.dll on my machine (Win2k, with DirectX 9 downloaded).....is that the correct DLL name for "qedit.h"?

Author

Commented:
Just reinstallation of the DirectX SDK solve the problem.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial