Question

This is the code I was able to write following the MSND reference: http://msdn.microsoft.com/en-us/library/dd407269(v=vs.85).aspx

#include <iostream>
#include <conio.h>
#include <dshow.h>

// Helper function for initializing ICaptureGraphBuilder2 and IGraphBuilder
// MSDN function: http://msdn.microsoft.com/en-us/library/dd373396(v=vs.85).aspx
// Arguments are passed  by pointers
HRESULT InitCaptureGraphBuilder(ICaptureGraphBuilder2 **, IGraphBuilder **);

// Helper function for retriving an enumerator for a specified device category
// MSDN function:          http://msdn.microsoft.com/en-us/library/dd377566(v=vs.85).aspx
// MSDN about moniker:     http://msdn.microsoft.com/en-us/library/ms679705(v=vs.85).aspx
// MSDN about enumerator:  http://msdn.microsoft.com/en-us/library/dd407292(v=vs.85).aspx
// You need to pass by reference the type of device a nd by pointer the IEnumMoniker
HRESULT EnumerateDevices(REFGUID, IEnumMoniker **);

// Helper function for preparing the output video file
// You need to pass by ponter ICaptureGraphBuilder2 and IBaseFilter
HRESULT PrepareOutputFile(ICaptureGraphBuilder2 **, IBaseFilter **);

int main()
{
   ICaptureGraphBuilder2   *capture_graph       = NULL;
   IGraphBuilder           *graph               = NULL;
   IEnumMoniker            *device_enumerator;
   IBaseFilter             *capture_filter;
   HRESULT                 hr;

   hr = CoInitialize(NULL);
   hr = InitCaptureGraphBuilder(&capture_graph, &graph);
   hr = EnumerateDevices(CLSID_VideoInputDeviceCategory, &device_enumerator);

   // hr = PrepareOutputFile(&capture_graph, &capture_filter);

   // At the end:
   // capture_graph->Release();
   // graph->Release();

}

HRESULT InitCaptureGraphBuilder(ICaptureGraphBuilder2 **ppCapture_graph, IGraphBuilder **ppGraph)
{
   if (!ppCapture_graph || !ppGraph) {
      return E_POINTER;
   }

   ICaptureGraphBuilder2   *capture_graph  = NULL;
   IGraphBuilder           *graph  = NULL;

   // Create the Capture Graph Builder
   HRESULT hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2, (void**)&capture_graph);

   if (SUCCEEDED(hr)) {
      // Create the Filter Graph Manager
      hr = CoCreateInstance(CLSID_FilterGraph, 0, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)&graph);

      if (SUCCEEDED(hr)) {
         // Initialize the Capture Graph Builder
         capture_graph->SetFiltergraph(graph);

         // Return both interface pointers to the caller
         // !! The caller must release both interfaces !!
         *ppCapture_graph  = capture_graph;
         *ppGraph          = graph;

         return S_OK;
      } else {
         capture_graph->Release();
      }
   }

   // Failed
   return hr;
}

HRESULT EnumerateDevices(REFGUID category, IEnumMoniker **ppDevice_enumerator)
{
   ICreateDevEnum *create_dev_enum;

   // Create the System Device Enumerator
   HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&create_dev_enum));

   if (SUCCEEDED(hr)) {
      // Create an enumerator for the selected category
      hr = create_dev_enum->CreateClassEnumerator(category, ppDevice_enumerator, 0);

      if (hr == S_FALSE) {
         // The category is empty. Treat as an error
         hr = VFW_E_NOT_FOUND;
      }
      create_dev_enum->Release();
   }

   return hr;
}

Now I still don't understand how to select the video device I want. With the EnumerateDevices() function I return the enumerator (I think a kind of vector with all the devices), but how do I use it before the PrepareOutputFile() (that I have to write) ?!

Was it helpful?

Solution

See Selecting a Capture Device on MSDN. You are basically doing what fist code snippet there suggests. Have a look further - it guides you what to do next, in particular IMoniker::BindToObject is the call which gets you filter instance for your capture graph.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top