Link to home
Start Free TrialLog in
Avatar of Dmitry_
Dmitry_

asked on

DirectShow question

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 ?
ASKER CERTIFIED SOLUTION
Avatar of Kimpan
Kimpan
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;
}
   

Avatar of Dmitry_
Dmitry_

ASKER

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?
SampleGrabber is a part of DirextX 8.x SDK
download example from www.wordware.com/directshow
Avatar of Dmitry_

ASKER

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.
Avatar of Dmitry_

ASKER

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.
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
Avatar of Dmitry_

ASKER

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?
try to register qedit.dll in Windows\System32\

regsvr32 windows\system32\qedit.dll
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"?
Avatar of Dmitry_

ASKER

Just reinstallation of the DirectX SDK solve the problem.