TV Tuner Control

This page shows you how to retreive a TV tuner device interface, IAMTVTuner. Please note that error handling codes are omitted to keep the sample code simple.

Sample code

This sample code shows you how to get a TV Tuner device interface using directshow. This sample code simply tries to get a IAMTVTuner interface from the first found capture device. If IAMTVTuner inteface can not be obtained, this sample will simply exit. Please note that this sample does not display video using the TV tuner. It will retrieve the IAMTVTuner interface and ends.


#include <stdio.h>

#include <dshow.h>

int
main()
{
 IGraphBuilder *pGraphBuilder;
 ICaptureGraphBuilder2 *pCaptureGraphBuilder2;

 ICreateDevEnum *pCreateDevEnum = NULL;
 IEnumMoniker *pEnumMoniker = NULL;
 IMoniker *pMoniker = NULL;

 ULONG nFetched = 0;
 IBaseFilter *pDeviceFilter;

 // initialize COM
 CoInitialize(NULL);

 // 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);

 // 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 the first Moniker
 pEnumMoniker->Next(1, &pMoniker, &nFetched);

 // bind Monkier to Filter
 pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pDeviceFilter );

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

 // Monkier and Enumerator can be freed now
 pMoniker->Release();
 pEnumMoniker->Release();
 pCreateDevEnum->Release();

 /*
  * IAMTVTuner
  */
 HRESULT hr;
 IAMTVTuner *pTvTuner;

 // Get IAMTVTuner from Graph.
 // If there are more than 1 TV Tuner devices,
 // you will need to use FindInterface again.
 hr = pCaptureGraphBuilder2->FindInterface(&LOOK_UPSTREAM_ONLY,
	    NULL, pDeviceFilter,
        IID_IAMTVTuner, (LPVOID *)&pTvTuner);
 if (hr != S_OK) {
   printf("could not get TV Tuner\n");
   return 1;
 }

 // release
 pTvTuner->Release();

 pCaptureGraphBuilder2->Release();
 pGraphBuilder->Release();

 // finalize COM
 CoUninitialize();

 return 0;
}

This sample only tries to retrive the IAMTVTuner interface from the first capture device found. If you want to know how to handle multiple capture devices, please see "Selecting and playing from a video input device".

  

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