Class MFSample
The IMFSample interface represents a multimedia sample for a multimedia stream type. An typical example is a video frame, and a notable exception is audio where an IMFSample does not usually represent a single sample of audio, but rather a chunk of audio samples. Note that this allows us to reduce the overhead for representing audio in the pipeline.
A sample may consist of a multiple buffers as is the case of compressed audio or video samples received from the network or being sent to an ASF media sink or ASF multiplexer.
The IMFAttributes interface can be used to tag the sample with extra information that is not represented by the IMFSample methods. The MFSampleExtension_xxx GUIDs in mfapi.h define some standard attributes, but custom attributes are allowed and are preserved through the Media Foundation pipeline as well as possible.
Implements
Inherited Members
Namespace: JeremyAnsel.DirectX.MediaFoundation
Assembly: JeremyAnsel.DirectX.MediaFoundation.dll
Syntax
[SecurityCritical]
public class MFSample : MFAttributes, IDisposable
Remarks
MediaFoundation does not provide a synchronized (thread-safe) way to call the IMFSample methods. To guarantee thread-safety, the caller should obtain a lock while working with sample object to prevent access by other threads.
Constructors
| Edit this page View SourceMFSample(nint)
Initializes a new instance of the MFSample class.
Declaration
public MFSample(nint comPtr)
Parameters
| Type | Name | Description |
|---|---|---|
| nint | comPtr |
Fields
| Edit this page View SourceMFSampleGuid
The interface GUID.
Declaration
public static readonly Guid MFSampleGuid
Field Value
| Type | Description |
|---|---|
| Guid |
Methods
| Edit this page View SourceAddBuffer(MFMediaBuffer)
The AddBuffer method allows the caller to add a media buffer to the end of current list of buffers in the sample.
Declaration
public void AddBuffer(MFMediaBuffer pBuffer)
Parameters
| Type | Name | Description |
|---|---|---|
| MFMediaBuffer | pBuffer | Pointer to a buffer object. |
Remarks
The newly added buffer represents sample data at an offset equal to the previous total length of the sample.
ConvertToContiguousBuffer()
The GetContiguousBuffer method converts sample with multiple buffers into sample with single buffer and returns a pointer to this buffer.
Declaration
public MFMediaBuffer ConvertToContiguousBuffer()
Returns
| Type | Description |
|---|---|
| MFMediaBuffer |
Remarks
This method copies the content of the sample into a contiguous media buffer, and should be used with care.
CopyToBuffer(MFMediaBuffer)
The CopyToBuffer method allows the caller to copy the sample data to the buffer object provided.
Declaration
public void CopyToBuffer(MFMediaBuffer pBuffer)
Parameters
| Type | Name | Description |
|---|---|---|
| MFMediaBuffer | pBuffer | Pointer to a buffer object. |
GetBufferByIndex(uint)
The GetBufferByIndex method returns a pointer to the IMFMediaBuffer object at a specified index in the sample.
Declaration
public MFMediaBuffer GetBufferByIndex(uint dwIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| uint | dwIndex | 32 bit value specifying the index of the buffer object requested. |
Returns
| Type | Description |
|---|---|
| MFMediaBuffer |
GetBufferCount()
The GetBufferCount method returns the count of the buffers in that are currently associated with this sample.
Declaration
public uint GetBufferCount()
Returns
| Type | Description |
|---|---|
| uint |
Remarks
It is valid to have a sample with a buffer count of zero. This is considered to be an empty sample.
GetSampleDuration()
The GetSampleDuration method returns the duration of the sample.
Declaration
public ulong GetSampleDuration()
Returns
| Type | Description |
|---|---|
| ulong | If the method succeeds, it returns S_OK. If no the sample does not have a duration MF_E_NO_SAMPLE_DURATION is returned. If this method fails otherwise it returns an error code. |
Remarks
The duration is specified in 100ns interval units. If the duration is zero, then the sample duration is unknown. In such cases, it is possible that it can be derived from the media type e.g. video frame rate. Application should avoid calculating duration of presentation as sum of sample durations because of possible cumulative error. For example cumulative error for 60fps video on 24 hours interval is 0.1728 seconds. For 60fps, there are 5184000 frames * 166667 100ns/frame = 24 hours + 0.1728 seconds
GetSampleTime()
The GetSampleTime method returns the presentation time associated with the sample.
Declaration
public ulong GetSampleTime()
Returns
| Type | Description |
|---|---|
| ulong | If the method succeeds, it returns S_OK. If no the sample does not have a sample time MF_E_NO_SAMPLE_TIMESTAMP is returned. If this method fails otherwise it returns an error code. |
Remarks
The presentation time is stored in 100ns interval units.
GetTotalLength()
The GetTotalLength method returns the total length of all buffers objects in the sample.
Declaration
public uint GetTotalLength()
Returns
| Type | Description |
|---|---|
| uint |
Remarks
The total length of the sample is tracked when buffers are added (via AddBuffer), removed (via RemoveBufferByIndex and RemoveAllBuffers).
RemoveAllBuffers()
The RemoveAllBuffers method allows the caller to remove all buffer objects associated with the sample.
Declaration
public void RemoveAllBuffers()
Remarks
After this call, the buffer count of the sample is zero i.e. it is an empty sample.
RemoveBufferByIndex(uint)
The RemoveBufferByIndex method allows the caller to remove a buffer object at a specified index within the list of available buffer objects in the sample.
Declaration
public void RemoveBufferByIndex(uint dwIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| uint | dwIndex | 32 bit value specifying the index. |
Remarks
The total length of the sample is reduced by the length of the removed buffer, and the offsets of all buffer objects that were at indices greater than dwIndex are shifted up by the length of the removed buffer.
SetSampleDuration(ulong)
The SetSampleDuration method allows the caller to set the duration of the sample.
Declaration
public void SetSampleDuration(ulong hnsSampleDuration)
Parameters
| Type | Name | Description |
|---|---|---|
| ulong | hnsSampleDuration | 64 bit value specifying the sample duration. |
Remarks
The duration is specified in 100ns interval units. This value should be set wherever possible to aid in the processing of samples in a multimedia pipeline.
SetSampleTime(ulong)
The SetSampleTime method allows the caller to set the presentation time associated with the sample.
Declaration
public void SetSampleTime(ulong hnsSampleTime)
Parameters
| Type | Name | Description |
|---|---|---|
| ulong | hnsSampleTime | 64 bit value specifying the presentation time. |
Remarks
The presentation time is stored in 100ns interval units.