Microsoft DirectX 9.0 |
DMO Sample is a sample Microsoft® DirectX® Media Object (DMO). It takes MPEG-1 video packets as input and produces two output streams. The first output stream contains video frames in RGB 565 format. Each frame displays the time code taken from the MPEG-1 video, and is otherwise blank. The second output stream is an optional text stream with the time code.
DMO Sample uses the IMediaObjectImpl class template to implement the IMediaObject interface. The Active Template Library (ATL) handles various COM details, including registration, aggregation, IUnknown, and the DLL entry points.
Path
Source: (SDK root)\Samples\C++\DirectShow\DMO\DMOSample
User's Guide
The project workspace builds a DLL named Dmosample.dll. Use the Regsvr32 utility to register the DLL. To see the DMO in action, you can use the Microsoft® DirectShow® GraphEdit utility. You will need an MPEG-1 video file. Perform the following steps:
When the graph runs, a video window appears that displays the time codes.
Programming Notes
The DMO Sample contains the following source files:
File | Description |
Dmosample.cpp | Code for implementing basic COM functionality in ATL. |
Resource.h | Defines resource constants. |
Sample.h, Sample.cpp | Defined the CSample class, which implements the DMO. |
State.h, State.cpp | Defines the CStreamState class, a helper class that generates time stamps from the MPEG data. |
Stdafx.h, Stdafx.cpp | Generates the precompiled header. |
Util.h, Util.cpp | Contains utility functions for drawing the output image. |
Dmosample.rc | Defines a resource with version information, and links to the Sample.rgs file. |
Sample.rgs | Defines a text resource with registry information. ATL uses this resource to create registry entries when it registers the DLL. |
The following remarks describe the implementation in greater detail.
Dmosample.cpp
The file contains the object map that ATL uses to create and register the DMO object. It also contains the exported DLL functions. These include the entry-point function (DllMain), and the functions that COM requires (DllCanUnloadNow, DllGetClassObject, DllRegisterServer, and DllUnregisterServer).
The ATL COM AppWizard automatically generates most of the code contained in this file. Two things are added:
Resource.h
The one constant declared in this header file, IDR_SAMPLE, is used by ATL's self-registration mechanism. The constant IDR_SAMPLE identifies a text resource, which is defined in the resource file Sample.rgs and imported by the resource file Dmosample.rc. The header file Sample.h contains the following line:
DECLARE_REGISTRY_RESOURCEID(IDR_SAMPLE)
This causes ATL to use the registration information declared in the IDR_SAMPLE resource.
Sample.h
The DEFINE_GUID macro declares the class identifier (CLSID) of the DMO.
The CSample class derives from the following classes:
CSample aggregates the Free Threaded Marshaler, which is an object that performs custom marshaling. If the COM library marshals a CSample pointer across threads within the sample process, the exact same pointer is returned. If the COM library tries to marshal the object across processes or across machines, it fails. ATL adds all of the code need to aggregrate the Free Threaded Marshaler. This code appears in the FinalConstruct and FinalRelease methods. The COM_INTERFACE_ENTRY_AGGREGATE entry in the COM map delegates the IMarshal interface to the Free Threaded Marshaler.
The CSample class declares the following member variables:
Variable | Description |
m_pUnkMarshaler | Pointer to the Free Threaded Marshaler object. |
m_pBuffer | Pointer to the input buffer. The DMO holds a reference count on the buffer until it processes all of the input. |
m_pbData | Pointer to the next byte in the input buffer. |
m_cbData | Number of bytes remaining to process. |
m_rtFrame | Time stamp from the most recent output frame. |
m_StreamState | Instance of the CStreamState helper class, which parses MPEG data. |
m_bPicture | Boolean flag that indicates whether the DMO has picture information to output. |
Sample.cpp
This file implements the methods in the CSample class.
After the types are set, the method fails unless the new type matches the current type. If a DMO cannot handle format changes while streaming, it should include similar code. In fact, the sample DMO could easily accept format changes, because it generates output one frame at a time.
1. Calls the IMediaBuffer::GetBufferAndLength method to retrieve a pointer to the input data and the size of the input.
2. Holds a reference count on the input buffer. The m_pBuffer variable is an ATL smart pointer, so assigning to the variable has the effect of calling the IUnknown::AddRef method.
3. Passes the time stamp to the CStreamState object. If the sample does not have a time stamp, the method uses the value INVALID_TIME.
4. Calls Process, a private helper method, to process the data.
1. Checks whether the output buffer is large enough to hold the output data. If not, the method returns an error.
2. Calls the OurFillRect method to draw the background, and the DrawOurText method to draw the text.
3. Sets the time stamp. If there is a valid time stamp for this frame, it is used. Otherwise, the previous time stamp is incremented by the average frame rate.
4. Calls the Process method again to process any data that might remain in the input buffer. If this results in more output data, the DMO_OUTPUT_DATA_BUFFERF_INCOMPLETE flag is set.
Stream 1 is optional, so the caller might not allocate a buffer for that stream. Assuming that stream 1 does have an output buffer, however, the DMO writes the time code as a text string. The time stamps and processing flags are taken from stream 0.