How to add DMO in DirectShow filter graph

Introduction.


In this article, we'll see how easily we can add a DMO (Echo) to a DirectShow filter graph. As a result, we'll get audio with Echo effect. Installed Windows SDK for 7 on Windows 7 and run GraphEdt.exe. Now, select the menu Graph-->Insert Filters. Filter window will pop up, See the Example below:

In this picture, there is a section named "DMO audio effects". There are some DMOs that we can use to add effect to our audio. In this case, I've picked up the DMO 'Echo'. Now the question is how do I know, my system has the required DMO installed. This problem can be solved if we enemerate system for audio DMOs and then select the desired one if available. The following code section demonstrate how we can enumerate system for DMO.

void EnumAudioDMO()
{
	IEnumDMO* pEnum = NULL;
	HRESULT hr = DMOEnum(
		DMOCATEGORY_AUDIO_EFFECT, // Category
		DMO_ENUMF_INCLUDE_KEYED,  // Included keyed DMOs
		0, NULL,                  // Input types (don't care)
		0, NULL,                  // Output types (don't care)
		&pEnum);

	if (SUCCEEDED(hr)) 
	{
		CLSID clsidDMO;
		WCHAR* wszName;
		do
		{
			hr = pEnum->Next(1, &clsidDMO, &wszName, NULL);
			if (hr == S_OK) 
			{  
				// Now wszName holds the friendly name of the DMO, 
				// and clsidDMO holds the CLSID. 

				wprintf(L"DMO Name: %s\n", wszName);
				if(wcscmp(wszName, L"Echo") == 0)
				{
					g_clsidDMO = clsidDMO;
					g_bFound = TRUE;
				}
				
				// Remember to release wszName!
				CoTaskMemFree(wszName);
			}
		} while (hr == S_OK);
		pEnum->Release();
	}
}

There is DMOEnum API to scan registry for installed DMOs. Then, checked for Echo DMO. Once It's located then it's class id can be used to insert the DMO into the DirectShow filter graph. DMO can be used in DirectShow based application but we need to provide a DMO wrapper. If we know the class identifier (CLSID) of a specific DMO that we want to use, we can initialize the DMO Wrapper filter with that DMO. In the above code snippet we already got to know the CLSID of the target DMO while enumerating the system for DMO. Follwoing part shows how we can create DMO wrapper and insert the DMO in DirectShow filter graph:

1. Call CoCreateInstance to create the DMO Wrapper filter.
2. Query the DMO Wrapper filter for the IDMOWrapperFilter interface.
3. Call the IDMOWrapperFilter::Init method. Specify the CLSID of the DMO and the GUID of the DMO's category. 4. Finally add the DMO to DirectShow Filter graph.

The following code shows, how we can build a graph with Echo DMO to add echo in audio output:

void BuildFilterGraph()
{
	CoInitialize(NULL);

	IGraphBuilder	*pGraphBuilder = NULL;
	IBaseFilter	*pFilter = NULL;
	IMediaControl	*pMediaControl = NULL;
	IMediaEventEx	*pEvt = NULL;
	
	CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraphBuilder);

	pGraphBuilder->QueryInterface(IID_IMediaControl, reinterpret_cast(&pMediaControl));
	pGraphBuilder->QueryInterface(IID_IMediaEventEx, reinterpret_cast(&pEvt));

	if(g_bFound)	// If found then add the Echo DMO to the filter graph
	{
		HRESULT hr = CoCreateInstance(CLSID_DMOWrapperFilter, NULL, 
			CLSCTX_INPROC_SERVER, IID_IBaseFilter, reinterpret_cast(&pFilter));

		if (SUCCEEDED(hr)) 
		{
			// Query for IDMOWrapperFilter.
			IDMOWrapperFilter *pDmoWrapper;
			hr = pFilter->QueryInterface(IID_IDMOWrapperFilter, reinterpret_cast(&pDmoWrapper));

			if (SUCCEEDED(hr)) 
			{     
				// Initialize the filter.
				hr = pDmoWrapper->Init(g_clsidDMO, DMOCATEGORY_VIDEO_EFFECT); 
				pDmoWrapper->Release();

				if (SUCCEEDED(hr)) 
				{
					// Add the filter to the graph.
					hr = pGraphBuilder->AddFilter(pFilter, L"Echo");
				}
			}
		}
	}

	pGraphBuilder->RenderFile(L"C:\\Test.wmv", NULL);
	
	pMediaControl->Run();

	long evCode; 
	pEvt->WaitForCompletion(INFINITE, &evCode);

	pFilter->Release();
	pGraphBuilder->Release();
}

The following sample code shows, how we can write DirectShow application and add echo to an existing video without modifying the content.

// DMOEnumeration.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include 
#include 
#include 

CLSID	g_clsidDMO;
BOOL	g_bFound;

// Enumerate audio DMO and pick Echo, DMO
void EnumAudioDMO()
{
	IEnumDMO* pEnum = NULL;
	HRESULT hr = DMOEnum(
		DMOCATEGORY_AUDIO_EFFECT, // Category
		DMO_ENUMF_INCLUDE_KEYED,  // Included keyed DMOs
		0, NULL,                  // Input types (don't care)
		0, NULL,                  // Output types (don't care)
		&pEnum);

	if (SUCCEEDED(hr)) 
	{
		CLSID clsidDMO;
		WCHAR* wszName;
		do
		{
			hr = pEnum->Next(1, &clsidDMO, &wszName, NULL);
			if (hr == S_OK) 
			{  
				// Now wszName holds the friendly name of the DMO, 
				// and clsidDMO holds the CLSID. 

				wprintf(L"DMO Name: %s\n", wszName);
				if(wcscmp(wszName, L"Echo") == 0)
				{
					g_clsidDMO = clsidDMO;
					g_bFound = TRUE;
				}
				
				// Remember to release wszName!
				CoTaskMemFree(wszName);
			}
		} while (hr == S_OK);
		pEnum->Release();
	}
}

void BuildFilterGraph()
{
	CoInitialize(NULL);

	IGraphBuilder	*pGraphBuilder = NULL;
	IBaseFilter		*pFilter = NULL;
	IMediaControl	*pMediaControl = NULL;
	IMediaEventEx	*pEvt = NULL;
	
	CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, 
		IID_IGraphBuilder, (void **)&pGraphBuilder);

	pGraphBuilder->QueryInterface(IID_IMediaControl, reinterpret_cast(&pMediaControl));
	pGraphBuilder->QueryInterface(IID_IMediaEventEx, reinterpret_cast(&pEvt));

	if(g_bFound)	// If found then add the Echo DMO to the filter graph
	{
		HRESULT hr = CoCreateInstance(CLSID_DMOWrapperFilter, NULL, 
			CLSCTX_INPROC_SERVER, IID_IBaseFilter, reinterpret_cast(&pFilter));

		if (SUCCEEDED(hr)) 
		{
			// Query for IDMOWrapperFilter.
			IDMOWrapperFilter *pDmoWrapper;
			hr = pFilter->QueryInterface(IID_IDMOWrapperFilter, reinterpret_cast(&pDmoWrapper));

			if (SUCCEEDED(hr)) 
			{     
				// Initialize the filter.
				hr = pDmoWrapper->Init(g_clsidDMO, DMOCATEGORY_VIDEO_EFFECT); 
				pDmoWrapper->Release();

				if (SUCCEEDED(hr)) 
				{
					// Add the filter to the graph.
					hr = pGraphBuilder->AddFilter(pFilter, L"Echo");
				}
			}
		}
	}

	pGraphBuilder->RenderFile(L"C:\\Test.wmv", NULL);
	
	pMediaControl->Run();

	long evCode; 
	pEvt->WaitForCompletion(INFINITE, &evCode);

	pFilter->Release();
	pGraphBuilder->Release();
}

int _tmain(int argc, _TCHAR* argv[])
{
	g_bFound = FALSE;

	EnumAudioDMO();
	BuildFilterGraph();

	return 0;
}


About the Author

Mufti Mohammed

Small-Talk and Small-Programming working together. My Blog

Comments

  • n00b

    Posted by Walt on 09/09/2012 09:00pm

    Thanks for this post! I've just gotten started with DirectShow. There seems to be very little useful information about DMO filters. This has helped me immensely!

    • Thank you

      Posted by Mufti on 12/23/2012 08:24am

      I'm glad to hear that it helped you. Thanks, Mufti

      Reply
    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 23, 2014 @ 12:00 p.m. ET / 9:00 a.m. PT Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this eSeminar to learn why physical appliances are preferable to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds