Microsoft DirectX 9.0

Using the DMO Class Template

The DirectX SDK includes a class template, IMediaObjectImpl, for implementing DMOs. The template handles many of the "bookkeeping" tasks, such as validating input parameters. By using the template, you can focus on the functionality that is specific to your DMO. In addition, the template helps to ensure that you create a robust implementation. The template is defined in the header file Dmoimpl.h, located in the Include directory of the SDK.

The IMediaObjectImpl template inherits the IMediaObject interface. To create a DMO using the template, define a new class that derives from IMediaObjectImpl. The template implements all of the IMediaObject methods. In most cases, the template calls a corresponding private method on the derived class. The template provides the following features:

The derived class must implement the IUnknown interface; the template does not provide this interface. You can use the Active Template Library (ATL) to implement IUnknown, or you can provide some other implementation. The template also does not implement the locking mechanism. The derived class must implement the Lock and Unlock methods. If you create your class using ATL, you can use the default ATL implementations.

Declaring the Derived Class

The IMediaObjectImpl class template is declared as follows:

class IMediaObjectImpl : public ImediaObject

The three template parameters are _DERIVED_, NUMBEROFINPUTS, and NUMBEROFOUTPUTS. Set _DERIVED_ equal to the name of your class. The other two parameters define the number of input streams and output streams on the DMO. For example, to create a DMO class named CMyDmo that supports one input stream and two output streams, use the following declaration:

class CMyDmo : public IMediaObjectImpl<CMyDmo, 1, 2>

The remainder of this section describes how the template implements the various methods in IMediaObject.

Methods for Setting Media Types

The following methods set or retrieve media types on the DMO:

Informational Methods

The following methods provide information about the DMO.

Methods for Resource Allocation

The client of the DMO is not required to call these methods, but the template automatically calls AllocateStreamingResources before streaming starts. Therefore, the DMO can assume that resources are have been allocated correctly by the time ProcessInput is called. The DMO should call FreeStreamingResources in its destructor.

Also, when the template calls InternalAllocateStreamingResources, it sets an internal flag, so that it does not call that method again until calling InternalFreeStreamingResources. This ensures that resources are not re-allocated accidentally, which could cause memory leaks.

Methods for Streaming

The following methods are used to stream data:

The template does not provide any direct support for the IMediaObjectInPlace interface.

Methods for Locking

Locking is used to protect the DMO's state in a multithreaded environment. In an ATL project, the IMediaObject::Lock method causes a name conflict with the ATL Lock method. To resolve the conflict, the template renames the IMediaObject method to DMOLock. When you compile the derived class, define FIX_LOCK_NAME before including the header file Dmo.h:

#include <dmo.h>

This directive causes the preprocessor to substitute DMOLock for Lock in the declaration of the IMediaObject interface. Applications can still invoke the method using the name Lock, because the vtable order does not change. The DMOLock method calls Lock or Unlock on the derived class. If you are using ATL to implement the derived class, these methods are already defined by ATL, so no additional code is necessary. If you are not using ATL, you must provide Lock and Unlock methods in your derived class.

The template automatically locks the DMO in each of the IMediaObject methods. The derived class might need to lock the DMO inside other public methods that it implements (for example, if it supports IMediaObjectInPlace). The class template also provides an internal helper class, IMediaObjectImpl::LockIt, which is useful for locking and unlocking the DMO.


For the following IMediaObject methods, the template calls a corresponding method with the same signature on the derived class. The derived class must implement each of the methods shown in the second column.

IMediaObject Method Derived Class Method
AllocateStreamingResources InternalAllocateStreamingResources
Discontinuity InternalDiscontinuity
Flush InternalFlush
FreeStreamingResources InternalFreeStreamingResources
GetInputMaxLatency InternalGetInputMaxLatency
GetInputSizeInfo InternalGetInputSizeInfo
GetInputStreamInfo InternalGetInputStreamInfo
GetInputType InternalGetInputType
GetOutputSizeInfo InternalGetOutputSizeInfo
GetOutputStreamInfo InternalGetOutputStreamInfo
GetOutputType InternalGetOutputType
ProcessInput InternalProcessInput
ProcessOutput InternalProcessOutput
SetInputMaxLatency InternalSetInputMaxLatency

For the remaining IMediaObject methods, there is not a one-to-one correspondence between template methods and derived-class methods. The following table summarizes which methods are fully implemented by the template, and which methods call other methods on the derived class.

IMediaObject Method Derived Class Method
GetInputCurrentType Fully implemented
GetOutputCurrentType Fully implemented
GetStreamCount Fully implemented
GetInputStatus InternalAcceptingInput
Lock (implemented as DMOLock) Lock, Unlock
SetInputType InternalCheckInputType
SetOutputType InternalCheckOutputType

See Also