DirectShow - 选择视频设备

时间:2013-06-06 15:02:25

标签: c++ windows device directshow enumerate

这是我能够在MSND参考之后编写的代码: 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;
}

现在我仍然不明白如何选择我想要的视频设备。 使用EnumerateDevices()函数,我返回枚举器(我认为是一种包含所有设备的向量),但是如何在PrepareOutputFile()之前使用它(我必须写)?!

1 个答案:

答案 0 :(得分:3)

请参阅MSDN上的Selecting a Capture Device。你基本上正在做那里建议的拳头代码片段。进一步了解 - 它会指导您接下来要做什么,特别是IMoniker::BindToObject是一个调用,可以让您过滤捕获图的实例。