Show / Hide Table of Contents

Class MediaSource

Represents a media source that handles reading and decoding of media data from various input sources.

Inheritance
object
MediaSource
Implements
IDisposable
Inherited Members
object.GetType()
object.MemberwiseClone()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: FFmpeg
Assembly: FFmpeg.dll
Syntax
public class MediaSource : IDisposable

Constructors

View Source

MediaSource(DemuxerContext)

Initializes a new instance of the MediaSource class with a specified format context.

Declaration
public MediaSource(DemuxerContext context)
Parameters
Type Name Description
DemuxerContext context

The format context to associate with this media source.

View Source

MediaSource(DemuxerContext, DeviceType)

Initializes a new instance of the MediaSource class with a specified format context and device type.

Declaration
public MediaSource(DemuxerContext context, DeviceType deviceType)
Parameters
Type Name Description
DemuxerContext context

The format context to associate with this media source.

DeviceType deviceType

The device type for hardware-accelerated decoding.

Fields

View Source

GetCodec

A function to retrieve a codec by its ID. This can be set to provide custom codec retrieval logic.

Declaration
public Func<AVStream, Codec?>? GetCodec
Field Value
Type Description
Func<AVStream, Codec?>

Properties

View Source

CodecContexts

Gets the list of codec contexts for the streams in this media source.

Declaration
public IReadOnlyList<CodecContext> CodecContexts { get; }
Property Value
Type Description
IReadOnlyList<CodecContext>
Remarks

This property initializes codec contexts lazily based on the number of streams. It also sets up codec contexts for each stream, guessing the frame rate for video streams and adjusting codec parameters as needed.

View Source

FormatContext

Gets the format context associated with this media source.

Declaration
public DemuxerContext FormatContext { get; }
Property Value
Type Description
DemuxerContext
View Source

Metadata

Gets the metadata associated with the format context.

Declaration
public AVDictionary_ref Metadata { get; }
Property Value
Type Description
AVDictionary_ref
View Source

Seekable

Gets the types of seeking supported by the underlying I/O context.

Declaration
public Seekable Seekable { get; }
Property Value
Type Description
Seekable
Remarks

The value indicates which seeking operations are supported by the underlying AVIOContext. Derived classes can modify this value when creating or configuring a custom I/O context.

View Source

Streams

Gets the list of streams available in this media source.

Declaration
public IReadOnlyList<AVStream> Streams { get; }
Property Value
Type Description
IReadOnlyList<AVStream>

Methods

View Source

Decode(AVPacket, AVFrame)

Decodes a packet and writes the next available decoded frame into the specified AVFrame.

Declaration
public AVResult32 Decode(AVPacket packet, AVFrame frame)
Parameters
Type Name Description
AVPacket packet

The encoded packet to submit to the decoder.

AVFrame frame

Receives the decoded frame if one is available.

Returns
Type Description
AVResult32

An AVResult32 indicating the result of the decoding operation.

Remarks

This method forwards the packet to the decoder associated with StreamIndex.

A successful return value indicates that a decoded frame was written to frame.

TryAgain indicates that the decoder requires additional input packets before it can produce another frame.

EndOfFile indicates that the decoder has been fully drained and no more frames are available.

View Source

DecodeSubtitle(AVPacket, Subtitle)

Decodes a subtitle from the given AVPacket and outputs the result as a Subtitle.

Declaration
public AVResult32 DecodeSubtitle(AVPacket srcPacket, Subtitle subtitle)
Parameters
Type Name Description
AVPacket srcPacket
Subtitle subtitle

The decoded subtitle output as a Subtitle.

Returns
Type Description
AVResult32

The result of the decoding operation as AVResult32. This result indicates whether the operation was successful or if an error occurred.

Remarks

This method is specific to subtitle codecs. If the codec type is not set to Subtitle, the operation will throw a NotSupportedException.

Exceptions
Type Condition
NotSupportedException

Thrown if the codec is not of type subtitle.

View Source

Dispose()

Releases all resources used by the MediaSource object.

Declaration
public void Dispose()
View Source

Dispose(bool)

Releases the unmanaged resources used by the MediaSource and optionally releases the managed resources.

Declaration
protected virtual void Dispose(bool disposing)
Parameters
Type Name Description
bool disposing

A boolean indicating whether to release both managed and unmanaged resources (true), or only unmanaged resources (false).

View Source

FindBestStream(MediaType)

Finds the best stream of the specified media type within this media source.

Declaration
public int FindBestStream(MediaType type)
Parameters
Type Name Description
MediaType type

The media type to search for (e.g., audio or video).

Returns
Type Description
int

The index of the best stream of the specified type, or -1 if no suitable stream is found.

View Source

FlushBuffers()

Flushes the buffers of all codec contexts, clearing any buffered data.

Declaration
public void FlushBuffers()
View Source

GuessFramerate(AVStream)

Guesses the frame rate of the given stream.

Declaration
public Rational GuessFramerate(AVStream stream)
Parameters
Type Name Description
AVStream stream
Returns
Type Description
Rational

A Rational representing the guessed frame rate.

View Source

GuessFramerate(int)

Estimates the frame rate of the specified stream.

Declaration
public Rational GuessFramerate(int streamIndex)
Parameters
Type Name Description
int streamIndex
Returns
Type Description
Rational

The estimated frame rate.

View Source

Open(DemuxerContext, DeviceType)

Creates a new MediaSource instance from an existing format context.

Declaration
public static MediaSource Open(DemuxerContext file, DeviceType deviceType = DeviceType.None)
Parameters
Type Name Description
DemuxerContext file

The format context representing the media source.

DeviceType deviceType

The device type for hardware-accelerated decoding (default is None).

Returns
Type Description
MediaSource

A new MediaSource instance.

View Source

Open(IOContext, InputFormat?, IDictionary<string, string>?, DeviceType)

Opens a media source from an I/O context.

Declaration
public static MediaSource Open(IOContext io, InputFormat? format = null, IDictionary<string, string>? options = null, DeviceType deviceType = DeviceType.None)
Parameters
Type Name Description
IOContext io

The I/O context representing the media source.

InputFormat? format

Optional input format.

IDictionary<string, string> options

Optional options for opening the media source.

DeviceType deviceType

Optional device type for hardware-accelerated decoding.

Returns
Type Description
MediaSource

A new MediaSource instance if successful; otherwise, null.

View Source

Open(Stream, InputFormat?, IDictionary<string, string>?, DeviceType)

Opens a media source from a stream.

Declaration
public static MediaSource Open(Stream stream, InputFormat? format = null, IDictionary<string, string>? options = null, DeviceType deviceType = DeviceType.None)
Parameters
Type Name Description
Stream stream

The stream representing the media source.

InputFormat? format

Optional input format.

IDictionary<string, string> options

Optional options for opening the media source.

DeviceType deviceType

Optional device type for hardware-accelerated decoding.

Returns
Type Description
MediaSource

A new MediaSource instance if successful; otherwise, null.

View Source

Open(string, InputFormat?, IDictionary<string, string>?, DeviceType)

Opens a media source from a URL.

Declaration
public static MediaSource Open(string url, InputFormat? format = null, IDictionary<string, string>? options = null, DeviceType deviceType = DeviceType.None)
Parameters
Type Name Description
string url

The URL of the media source.

InputFormat? format

Optional input format.

IDictionary<string, string> options

Optional options for opening the media source.

DeviceType deviceType

Optional device type for hardware-accelerated decoding.

Returns
Type Description
MediaSource

A new MediaSource instance if successful; otherwise, null.

View Source

ReadAndDecodeAVFrame(AVFrame)

Reads a packet and decodes it into a frame. Continues until a frame is successfully decoded or no more frames can be read.

Declaration
public AVResult32 ReadAndDecodeAVFrame(AVFrame frame)
Parameters
Type Name Description
AVFrame frame

The AVFrame to populate with the decoded frame.

Returns
Type Description
AVResult32

An AVResult32 indicating the result of the operation. Returns the stream index if successful; otherwise, the error result.

View Source

ReadAndDecodeHWAVFrame(AVFrame)

Reads a packet and decodes it into a frame. Continues until a frame is successfully decoded or no more frames can be read.
This methods does not transfer a HwFrame to Ram.

Declaration
public AVResult32 ReadAndDecodeHWAVFrame(AVFrame frame)
Parameters
Type Name Description
AVFrame frame

The AVFrame to populate with the decoded frame.

Returns
Type Description
AVResult32

An AVResult32 indicating the result of the operation. Returns the stream index if successful; otherwise, the error result.

View Source

ReadFrame(AVPacket, AVFrame)

Sends a packet to the codec context and attempts to receive a frame.

Declaration
[Obsolete("Use Decode")]
public AVResult32 ReadFrame(AVPacket srcPacket, AVFrame dstFrame)
Parameters
Type Name Description
AVPacket srcPacket

The AVPacket to send to the codec context.

AVFrame dstFrame

The AVFrame to receive the decoded frame.

Returns
Type Description
AVResult32

An AVResult32 indicating the result of the operation.

View Source

ReadPacket(AVPacket)

Reads a packet of data from the media source into the provided AVPacket instance.

Declaration
public AVResult32 ReadPacket(AVPacket packet)
Parameters
Type Name Description
AVPacket packet

The AVPacket instance to populate with data.

Returns
Type Description
AVResult32

An AVResult32 indicating the result of the operation.

View Source

Seek(long)

Seeks to the specified frame number within the media stream.

Declaration
public AVResult32 Seek(long frame)
Parameters
Type Name Description
long frame

The frame number to seek to.

Returns
Type Description
AVResult32

An AVResult32 indicating the result of the seek operation.

View Source

Seek(long, int)

Seeks to the specified frame number within the media stream for a specific stream index.

Declaration
public AVResult32 Seek(long frame, int streamIndex)
Parameters
Type Name Description
long frame

The frame number to seek to.

int streamIndex

The index of the stream to seek within.

Returns
Type Description
AVResult32

An AVResult32 indicating the result of the seek operation.

View Source

Seek(TimeSpan)

Seeks to the specified time position within the media stream.

Declaration
public AVResult32 Seek(TimeSpan time)
Parameters
Type Name Description
TimeSpan time

The time position to seek to.

Returns
Type Description
AVResult32

An AVResult32 indicating the result of the seek operation.

View Source

Seek(TimeSpan, int)

Seeks to the specified time position within the media stream for a specific stream index.

Declaration
public AVResult32 Seek(TimeSpan time, int streamIndex)
Parameters
Type Name Description
TimeSpan time

The time position to seek to.

int streamIndex

The index of the stream to seek within.

Returns
Type Description
AVResult32

An AVResult32 indicating the result of the seek operation.

View Source

SeekExactly(TimeSpan, int)

Seeks exactly to the specified time position within the media stream for a specific stream index.

Declaration
public AVResult32 SeekExactly(TimeSpan timeSpan, int streamIndex)
Parameters
Type Name Description
TimeSpan timeSpan

The position to seek to.

int streamIndex

The stream index.

Returns
Type Description
AVResult32

Negative if error.

View Source

SetCodec(Codec, int)

Replaces the codec context for the specified stream with a new context using the specified codec.

Declaration
public CodecContext SetCodec(Codec codec, int streamIndex)
Parameters
Type Name Description
Codec codec

The codec to use for the new codec context.

int streamIndex

The zero-based index of the stream whose codec context should be replaced.

Returns
Type Description
CodecContext

The newly created CodecContext.

Remarks

The existing codec context for the stream, if any, is disposed before the new context is created.

The new codec context is initialized from the stream's CodecParameters, configured with the current hardware device type, and assigned the stream's packet time base.

For video streams, the frame rate is initialized using GuessFrameRate(AVStream, AVFrame?).

Exceptions
Type Condition
ArgumentOutOfRangeException

streamIndex is outside the range of available streams.

Implements

IDisposable
  • View Source
In this article
Back to top Generated by DocFX