MediaSource Class

A media source (https://w3c.github.io/media-source/#mediasource): a media resource whose data is appended by the scripts to its SourceBuffer objects, attached to a media element by a blob: URL (Url.CreateObjectUrl) or, for a source created in a dedicated worker, by its MediaSource.Handle (HtmlMediaElement.SrcObject).

public class MediaSource : EventTarget

Remarks

The media backend of the browsing context parses the appended bytes (MediaBackend.CreateSegmentParser); the library implements the algorithms of the specification on the coded frames it reports. Without a backend, no type is supported.

See also

Inheritance
EventTarget MediaSource

Constructors

NameDescription
MediaSource()Creates a media source (https://w3c.github.io/media-source/#dom-mediasource-constructor): it is closed until it is attached to a media element.

MediaSource()

MediaSource.cs

Creates a media source (https://w3c.github.io/media-source/#dom-mediasource-constructor): it is closed until it is attached to a media element.

public MediaSource()

Properties

NameDescription
ActiveSourceBuffersGets the source buffers that provide the selected video track and the enabled audio tracks (https://w3c.github.io/media-source/#dom-mediasource-activesourcebuffers), in the order of MediaSource.SourceBuffers.
CanConstructInDedicatedWorkerGets a value indicating whether a media source can be created in a dedicated worker (https://w3c.github.io/media-source/#dom-mediasource-canconstructindedicatedworker): always true.
DurationGets or sets the duration of the presentation in seconds (https://w3c.github.io/media-source/#dom-mediasource-duration): NaN while the source is closed. The setter throws a TypeError for a negative value or NaN, and an InvalidStateError when the source is not open, a source buffer is updating, or the value is less than the highest presentation timestamp of the buffered frames.
HandleGets the handle used to attach the source of a dedicated worker to a media element of its window (https://w3c.github.io/media-source/#dom-mediasource-handle): it is transferred to the window and assigned to srcObject.
OnSourceCloseGets or sets the event handler of the sourceclose event.
OnSourceEndedGets or sets the event handler of the sourceended event.
OnSourceOpenGets or sets the event handler of the sourceopen event.
ReadyStateGets the state of the source (https://w3c.github.io/media-source/#dom-mediasource-readystate).
SourceBuffersGets the source buffers of the source (https://w3c.github.io/media-source/#dom-mediasource-sourcebuffers).

ActiveSourceBuffers

MediaSource.cs

Gets the source buffers that provide the selected video track and the enabled audio tracks (https://w3c.github.io/media-source/#dom-mediasource-activesourcebuffers), in the order of MediaSource.SourceBuffers.

public SourceBufferList ActiveSourceBuffers { get; }

CanConstructInDedicatedWorker

MediaSource.cs

Gets a value indicating whether a media source can be created in a dedicated worker (https://w3c.github.io/media-source/#dom-mediasource-canconstructindedicatedworker): always true.

public static bool CanConstructInDedicatedWorker { get; }

Gets or sets the duration of the presentation in seconds (https://w3c.github.io/media-source/#dom-mediasource-duration): NaN while the source is closed. The setter throws a TypeError for a negative value or NaN, and an InvalidStateError when the source is not open, a source buffer is updating, or the value is less than the highest presentation timestamp of the buffered frames.

public double Duration { get; set; }

Gets the handle used to attach the source of a dedicated worker to a media element of its window (https://w3c.github.io/media-source/#dom-mediasource-handle): it is transferred to the window and assigned to srcObject.

public MediaSourceHandle Handle { get; }

OnSourceClose

MediaSource.cs

Gets or sets the event handler of the sourceclose event.

public Action<Event>? OnSourceClose { get; set; }

OnSourceEnded

MediaSource.cs

Gets or sets the event handler of the sourceended event.

public Action<Event>? OnSourceEnded { get; set; }

OnSourceOpen

MediaSource.cs

Gets or sets the event handler of the sourceopen event.

public Action<Event>? OnSourceOpen { get; set; }

ReadyState

MediaSource.cs

Gets the state of the source (https://w3c.github.io/media-source/#dom-mediasource-readystate).

public MediaSourceReadyState ReadyState { get; }

SourceBuffers

MediaSource.cs

Gets the source buffers of the source (https://w3c.github.io/media-source/#dom-mediasource-sourcebuffers).

public SourceBufferList SourceBuffers { get; }

Methods

NameDescription
AddSourceBuffer(string)Creates a source buffer for a MIME type (https://w3c.github.io/media-source/#dom-mediasource-addsourcebuffer). Throws a TypeError for an empty type, a NotSupportedError when the media backend does not support the type, and an InvalidStateError when the source is not open.
ClearLiveSeekableRange()Clears the live seekable range (https://w3c.github.io/media-source/#dom-mediasource-clearliveseekablerange).
EndOfStream(EndOfStreamError?)Signals the end of the stream (https://w3c.github.io/media-source/#dom-mediasource-endofstream): without error, the duration becomes the end of the buffered data and the media element can play to the end; with an error, the media element fails. Throws an InvalidStateError when the source is not open or a source buffer is updating.
IsTypeSupported(string)Returns whether a source buffer can be created for a MIME type (https://w3c.github.io/media-source/#dom-mediasource-istypesupported): the media backend of the current global object is confident that it can play the type (MediaBackend.CanPlayType is MediaTypeSupport.Probably: as in the browsers, a type that allows the codecs parameter and does not have it is not supported, though addSourceBuffer() accepts it) and creates a byte stream parser for it (MediaBackend.CreateSegmentParser).
RemoveSourceBuffer(SourceBuffer)Removes a source buffer (https://w3c.github.io/media-source/#dom-mediasource-removesourcebuffer): its pending append is aborted, and its tracks are removed from the media element. Throws a NotFoundError when the buffer is not in MediaSource.SourceBuffers.
SetLiveSeekableRange(double, double)Sets the range the media element can seek to when the duration is infinite (https://w3c.github.io/media-source/#dom-mediasource-setliveseekablerange).

AddSourceBuffer(string)

MediaSource.cs

Creates a source buffer for a MIME type (https://w3c.github.io/media-source/#dom-mediasource-addsourcebuffer). Throws a TypeError for an empty type, a NotSupportedError when the media backend does not support the type, and an InvalidStateError when the source is not open.

public SourceBuffer AddSourceBuffer(string type)

ClearLiveSeekableRange()

MediaSource.cs

Clears the live seekable range (https://w3c.github.io/media-source/#dom-mediasource-clearliveseekablerange).

public void ClearLiveSeekableRange()

EndOfStream(EndOfStreamError?)

MediaSource.cs

Signals the end of the stream (https://w3c.github.io/media-source/#dom-mediasource-endofstream): without error, the duration becomes the end of the buffered data and the media element can play to the end; with an error, the media element fails. Throws an InvalidStateError when the source is not open or a source buffer is updating.

public void EndOfStream(EndOfStreamError? error = null)

IsTypeSupported(string)

MediaSource.cs

Returns whether a source buffer can be created for a MIME type (https://w3c.github.io/media-source/#dom-mediasource-istypesupported): the media backend of the current global object is confident that it can play the type (MediaBackend.CanPlayType is MediaTypeSupport.Probably: as in the browsers, a type that allows the codecs parameter and does not have it is not supported, though addSourceBuffer() accepts it) and creates a byte stream parser for it (MediaBackend.CreateSegmentParser).

public static bool IsTypeSupported(string type)

RemoveSourceBuffer(SourceBuffer)

MediaSource.cs

Removes a source buffer (https://w3c.github.io/media-source/#dom-mediasource-removesourcebuffer): its pending append is aborted, and its tracks are removed from the media element. Throws a NotFoundError when the buffer is not in MediaSource.SourceBuffers.

public void RemoveSourceBuffer(SourceBuffer sourceBuffer)

SetLiveSeekableRange(double, double)

MediaSource.cs

Sets the range the media element can seek to when the duration is infinite (https://w3c.github.io/media-source/#dom-mediasource-setliveseekablerange).

public void SetLiveSeekableRange(double start, double end)