简体   繁体   中英

DirectShow dilemma - Not able to record

That's the actual main code, preview works fine:

int main()
{
   HRESULT hr = CoInitialize(NULL);

   ICaptureGraphBuilder2   *pBuild;
   IGraphBuilder           *pGraph;
   IMoniker                *pMoniker;
   IMediaControl           *pControl;
   IMediaEvent             *pEvent;

   InitCaptureGraphBuilder(&pGraph, &pBuild);

   hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
   hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);

   IBaseFilter    *pCap;   // Video capture filter
   IEnumMoniker   *pEnum;

   hr = EnumerateDevices(CLSID_VideoInputDeviceCategory, &pEnum);
   DisplayDeviceInformation(pEnum, &pMoniker);
   hr = pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pCap);

   if (SUCCEEDED(hr))
   {
      hr = pGraph->AddFilter(pCap, L"Capture Filter");
   }

   hr = pBuild->RenderStream(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pCap, NULL, NULL);
   hr = pControl->Run();

   _getch();

   pControl->Release();
   pCap->Release();
   pGraph->Release();
   pBuild->Release();
   CoUninitialize();

   return 0;
}

Now, I know that for recording I need this piece of code:

IBaseFilter *pMux;

hr = pBuild->SetOutputFileName(&MEDIASUBTYPE_Avi, L"D:\\test.avi", &pMux, NULL);
hr = pBuild->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, pCap, NULL, pMux);

If I replace this to the preview code, it actually create the AVI file (a very big one), but it's empty, no video. I mean I'm replacing the:

hr = pBuild->RenderStream(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pCap, NULL, NULL);

code, with the one above.

What I'm doing wrong, or better, what I'm missing?

RenderStream is high level method that internally embeds other calls through public documented APIs, typically for the ease of use. While it looks simple, it's not so easy to troubleshoot in case something does not work well and as expected. Even harder to tell inspecting just code visually. It also is not the most efficient because there is something you can do yourself to get closer to the solution, which is:

Your further steps are along either of the two:

  1. You take working sample code and compare to yours looking at differences and locating the source of the problem.

  2. You inspect the resulting filter graph topology putting your graph onto ROT , and checking using GraphEdit or a similar tool to ensure the topology is matching your expectations.

You also certainly need to check HRESULT codes, what you already seem to be doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM