When filters are connected, they typically negotiate a type between them. This type describes the format of the data to be exchanged; if the filters do not agree on a media type, they cannot connect. Microsoft® DirectShow™ describes types through the media type structure, which contains two conceptual parts. The first is a list of members that describes generic attributes of a data stream. An example of this is a member that declares whether the data will be passed in fixed-size buffers. The second part of the structure is a variable-length block of data. How large the block of data should be and what it will contain depend on the type of data stream. For example, if the data stream is digital video, the format block is a VIDEOINFOHEADER structure. If, on the other hand, it is digital audio, the format block is a Microsoft Win32® WAVEFORMATEX structure.
A data stream type (for example, digital video) is set with a combination of two globally unique identifiers (GUIDs), called a major type and a subtype. The major type describes the overall class of data, examples of which might be digital video, digital audio, MIDI, or text captions. The subtype should supply a more specific description of the data type. In the case of digital video, for example, the subtype could be RGB8, RGB16, or RGB32 (among others). By having these two types in a generic structure (AM_MEDIA_TYPE), a component, such as a filter graph, can connect filters without any knowledge that is type specific.
The distinction between what goes in the major type and the subtype is somewhat arbitrary. However, as a general rule, transformations between major types (for example, video to audio or video to MIDI) should be relatively rare. Such a rare exception might be a transformation between audio and MIDI. As for the subtype, the more information promoted from the type-specific format block into the subtype, the better the design.
As an example of promoting type-specific information to the subtype, video in DirectShow uses a VIDEOINFOHEADER structure for the type-specific format block. This contains a Win32 BITMAPINFOHEADER structure that defines the video stream. BITMAPINFOHEADER contains the bit depth of the video, such as 8-bit, 16-bit, or 24-bit. This information is duplicated in the subtype field, because a subtype of RGB8 directly infers a bit count of 8.
DirectShow defines a number of major types. The most important of these are a video type that uses VIDEOINFOHEADER for the variable-length format block, and an audio that uses WAVEFORMATEX. However, it is insufficient to have a major type (such as digital video) inferring the contents of the format block (in this case, VIDEOINFOHEADER). The principal reason for this is extensibility: the format block type must be able to be updated without changing the less-specific major type. Therefore, what the format block actually contains is inferred by another GUID called the format type. If the format block contains VIDEOINFOHEADER, the format type GUID will be FORMAT_VideoInfo.
The principal use of the CMediaType class is to manage a media type structure in a simple way. At the same time, the class provides some extra helper functions (such as format-block copying and allocation). The class can be cast to an AM_MEDIA_TYPE structure when an interface method requires one to be passed to it.
The CMediaType class contains a pointer to a block of memory. When copying a CMediaType object, it is insufficient to simply copy the pointer. In C++, a data copy is required, which actually allocates a new block of memory and copies the data into it. This is the purpose of the copy operator.
Similarly, when comparing two CMediaType objects, you must compare the blocks of variable-length data (actually using memcmp) when producing the final result. To make this possible, CMediaType overrides the equivalence operator.
Member Functions
Name | Description |
AllocFormatBuffer | Allocates an uninitialized format block in the object. |
CMediaType | Constructs a CMediaType object. |
Format | Returns the format block for this media type. |
FormatLength | Returns the length of the format block of this object. |
FormatType | Returns a pointer to the format type. |
GetSampleSize | Returns the size of the samples. |
InitMediaType | Initializes the media type. |
IsFixedSize | Queries whether the samples are fixed in length. |
IsPartiallySpecified | Checks if the media type is not completely specified. |
IsTemporalCompressed | Queries whether the data stream is compressed temporally. |
IsValid | Queries whether the media type is currently valid. |
MatchesPartial | Checks whether this media type matches another media type that is only partially specified. |
ReallocFormatBuffer | Reallocates the format block, maintaining its current content where possible. |
ResetFormatBuffer | Deletes any format block that is currently present. |
SetFormat | Sets the format block. |
SetFormatType | Sets the type of the format block in the object. |
SetSampleSize | Sets the size of the samples. |
SetSubtype | Sets the subtype. |
SetTemporalCompression | Marks the media type to indicate that samples will be temporally compressed. |
SetType | Sets the major type. |
SetVariableSize | Marks the media type to indicate that samples will vary in length. |
Subtype | Returns a pointer to the subtype. |
Type | Returns a pointer to the major type. |
Operators
Name | Description |
operator = | Performs a copy operation. |
operator == | Tests for equality between CMediaType objects. |
operator != | Tests for inequality between CMediaType objects. |
Allocates a block of memory for the format block.
BYTE* AllocFormatBuffer(
ULONG length
);
Returns a pointer to the new block if successful; otherwise, returns NULL.
Any previous format block is deleted and a new block is allocated and installed. The size required must be nonzero.
Constructs a CMediaType object.
CMediaType( );
CMediaType(
const GUID * majortype
);
CMediaType(
const AM_MEDIA_TYPE& mtype
);
CMediaType(
const CMediaType& cmtype
);
No return value.
A CMediaType object can be constructed in a number of different ways. The class provides a default constructor that takes no parameters. It can also be constructed based on an AM_MEDIA_TYPE structure or another CMediaType object. In both cases, it takes a data copy of the format block before returning.
Returns a pointer to the variable-length format block of the object.
BYTE* Format( ) const;
Returns the format block of the object whose content is type-specific.
If no format block has been allocated, it might return NULL.
Returns the size, in bytes, of the format block that the object contains.
ULONG FormatLength( ) const;
Returns the length of the format block, or NULL if no format block is present.
Retrieves the format type.
const GUID *FormatType( ) const;
Returns a pointer to the format type.
The format GUID describes the content of the variable-length format block. Examples of format types are FORMAT_VideoInfo and FORMAT_WaveFormatEx.
Returns the maximum sample size for the data stream.
ULONG GetSampleSize( ) const;
Returns the maximum size of any sample to be sent, or zero to indicate that the sample size is variable.
Initializes the sample.
void InitMediaType( );
No return value.
This member function clears memory, sets the fixed sample size property, and sets the sample size to 1.
Determines if the samples for the stream will be fixed or variable size.
BOOL IsFixedSize( ) const;
Returns one of the following values.
Value | Meaning |
TRUE | Samples will be fixed size. |
FALSE | Samples will be variable length. |
Determines if the media type is only partially defined. This is the case if the major type or format type is GUID_NULL.
BOOL IsPartiallySpecified( ) const;
Returns one of the following values.
Value | Meaning |
TRUE | Media type is partially specified. |
FALSE | Media type is completely specified. |
This function does not check the sub type.
Asks if the stream will be compressed temporally.
BOOL IsTemporalCompressed( ) const;
Returns one of the following values.
Value | Meaning |
TRUE | Stream will have temporal compression. |
FALSE | Stream will have no temporal compression. |
Some data streams, such as compressed video, have temporal dependencies between successive samples. Other data streams do not have temporal dependencies between their samples; that is, each sample can be treated as an independent unit; for example, MIDI.
Queries whether the object has a valid major type.
BOOL IsValid( ) const;
Returns one of the following values.
Value | Meaning |
TRUE | CMediaType object has a valid major type. |
FALSE | CMediaType object does not have a valid major type. |
When CMediaType objects are constructed, their GUIDs are initialized with GUID_NULL (unless they are constructed based on another AM_MEDIA_TYPE structure or CMediaType object). This member function is useful for discovering if the object has been correctly initialized.
Determines if this media type matches the media type pointed to by the ppartial parameter.
BOOL MatchesPartial(
const CMediaType *ppartial
) const;
Returns one of the following values.
Value | Meaning |
TRUE | Media types match for the parts that are defined. |
FALSE | Media types do not match. |
The matching applies only for the parts of ppartial that are defined. That is, this only matches the major type, subtype, or format type of the media type if these are not defined as GUID_NULL.
Reallocates the format block to a new size.
BYTE* ReallocFormatBuffer(
ULONG length
);
Returns a pointer to the new block if successful; otherwise, returns NULL.
Any current format block will be copied into the newly allocated block up to its maximum size. Any excess will be lost when the new block is smaller than the old one. When the new block is larger, the excess is not filled with zeros.
The size required must be nonzero.
Deletes any format block currently held, sets it to NULL, and sets the size of the format block to zero.
void ResetFormatBuffer( );
No return value.
Sets the variable-length format block.
BOOL SetFormat(
BYTE *pFormat,
ULONG length
);
Returns one of the following values.
Value | Meaning |
TRUE | Format block was set. |
FALSE | An error occurred; most likely there was no memory available. |
The function takes a copy of the format block and stores that internally.
Sets the GUID that describes the content of the format block.
void SetFormatType(
const GUID * pformattype
);
No return value.
The format GUID describes what can be expected to be found in the variable-length format block. For example, if the format type is FORMAT_VideoInfo, the format block should contain a VIDEOINFOHEADER structure. The creator of this object is responsible for making them consistent.
Sets the maximum sample size for the data stream.
void SetSampleSize(
ULONG sz
);
No return value.
If the sample size passed is zero, the object is set so that the data stream will send variable-length samples (the CMediaType::GetSampleSize member function will return zero). Otherwise, it will set the maximum size of the sample to the size specified in the sz parameter.
Sets the subtype for the object.
void SetSubtype(
const GUID * psubtype
);
No return value.
Marks the media type so that the data stream it describes might or might not contain temporal compression (according to the input Boolean flag).
void SetTemporalCompression(
BOOL bCompressed
);
No return value.
Sets the major type for the object.
void SetType(
const GUID * ptype
);
No return value.
Sets the media type to indicate that the data stream will send variable-length samples.
void SetVariableSize( );
No return value.
Subsequent calls to CMediaType::GetSampleSize will return zero.
Retrieves the subtype.
const GUID *Type( ) const;
Returns a pointer to the subtype.
The subtype GUID gives finer detail within the major type of data represented by this media type.
Retrieves the major type.
const GUID *Type( ) const;
Returns a pointer to the major type.
The major type GUID describes the class of data represented by this media type.
The CMediaType variation of this operator is the copy constructor for a CMediaType object.
The AM_MEDIA_TYPE variation of this operator is the copy constructor for an AM_MEDIA_TYPE object.
CMediaType& operator=(
const CMediaType& rt
);
CMediaType& operator=(
const AM_MEDIA_TYPE& mrt
);
Returns a reference to this object after the operation.
Because the CMediaType class inherits publicly from AM_MEDIA_TYPE, the compiler could generate the copy constructor for the AM_MEDIA_TYPE object itself. However, this could introduce some memory conflicts and leaks in the process because the structure contains a dynamically allocated block (which the AM_MEDIA_TYPE pbFormat member points to), which the compiler's copy constructor will not copy correctly.
Tests for equality between CMediaType objects.
inline BOOL operator==(const CMediaType& rt) const;
Returns TRUE if the CMediaType object tested is equal to this object; otherwise, returns FALSE.
This object is on the left side of the operator.
Tests for inequality between CMediaType objects.
BOOL operator!=(
const CMediaType& rt
) const;
Returns TRUE if the CMediaType object tested is not equal to this object; otherwise, returns FALSE.
This object is on the left side of the operator.
© 1997 Microsoft Corporation. All rights reserved. Terms of Use.