Selecting and playing from a video input device

This page shows you how to list, select, and play video input device. Please note that error handling codes are omitted to keep the sample code simple.

Sample code

The previous sample only showed information about the video input device. This sample lists available video device, and asks if you want to play it. The video input from the selected video input device will be displayed.

Please use this sample code form your console. When you execute this sample code, the FriendlyName of the video input device will be shown with the message, "select this device ? [y] or [n]". If you press the 'y' key, this sample will start playing. If you press any other key, this sample will look for another device.

The following is the sample code.


#include <stdio.h>

#include <dshow.h>

int
main()
{
 // for playing
 IGraphBuilder *pGraphBuilder;
 ICaptureGraphBuilder2 *pCaptureGraphBuilder2;
 IMediaControl *pMediaControl;
 IBaseFilter *pDeviceFilter = NULL;

 // to select a video input device
 ICreateDevEnum *pCreateDevEnum = NULL;
 IEnumMoniker *pEnumMoniker = NULL;
 IMoniker *pMoniker = NULL;
 ULONG nFetched = 0;

 // initialize COM
 CoInitialize(NULL);

 //
 // selecting a device
 //

 // Create CreateDevEnum to list device
 CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, 
   IID_ICreateDevEnum, (PVOID *)&pCreateDevEnum);
    
 // Create EnumMoniker to list VideoInputDevice 
 pCreateDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,
   &pEnumMoniker, 0);
 if (pEnumMoniker == NULL) {
   // this will be shown if there is no capture device
   printf("no device\n");
   return 0;
 }

 // reset EnumMoniker
 pEnumMoniker->Reset();

 // get each Moniker
 while (pEnumMoniker->Next(1, &pMoniker, &nFetched) == S_OK) {
   IPropertyBag *pPropertyBag;
   TCHAR devname[256];

   // bind to IPropertyBag
   pMoniker->BindToStorage(0, 0, IID_IPropertyBag,
     (void **)&pPropertyBag);

   VARIANT var;

   // get FriendlyName
   var.vt = VT_BSTR;
   pPropertyBag->Read(L"FriendlyName", &var, 0);
   WideCharToMultiByte(CP_ACP, 0,
     var.bstrVal, -1, devname, sizeof(devname), 0, 0);
   VariantClear(&var);

   printf("%s\r\n", devname);
   printf("  select this device ? [y] or [n]\r\n");
   int ch = getchar();

   // you can start playing by 'y' + return key
   // if you press the other key, it will not be played.
   if (ch == 'y') {
     // Bind Monkier to Filter
     pMoniker->BindToObject(0, 0, IID_IBaseFilter,
         (void**)&pDeviceFilter );
   }

   // release
   pMoniker->Release();
   pPropertyBag->Release();

   if (pDeviceFilter != NULL) {
     // go out of loop if getchar() returns 'y'
     break;
   }
 }

 if (pDeviceFilter != NULL) {
   //
   // PLAY
   //

   // create FilterGraph
   CoCreateInstance(CLSID_FilterGraph,
     NULL,
     CLSCTX_INPROC,
     IID_IGraphBuilder,
     (LPVOID *)&pGraphBuilder);

   // create CaptureGraphBuilder2
   CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC, 
     IID_ICaptureGraphBuilder2, 
     (LPVOID *)&pCaptureGraphBuilder2);

   // set FilterGraph
   pCaptureGraphBuilder2->SetFiltergraph(pGraphBuilder);

   // get MediaControl interface
   pGraphBuilder->QueryInterface(IID_IMediaControl,
     (LPVOID *)&pMediaControl);

   // add device filter to FilterGraph
   pGraphBuilder->AddFilter(pDeviceFilter, L"Device Filter");

   // create Graph
   pCaptureGraphBuilder2->RenderStream(&PIN_CATEGORY_PREVIEW,
     NULL, pDeviceFilter, NULL, NULL);

   // start playing
   pMediaControl->Run();

   // to block execution
   // without this messagebox, the graph will be stopped immediately
   MessageBox(NULL,
     "Block Execution",
     "Block",
     MB_OK);

   // release
   pMediaControl->Release();
   pCaptureGraphBuilder2->Release();
   pGraphBuilder->Release();
 }

 // release
 pEnumMoniker->Release();
 pCreateDevEnum->Release();

 // finalize COM
 CoUninitialize();

 return 0;
}

  

Copyright (C) GeekPage.JP. All rights reserved.