This specification extends {{HTMLMediaElement}} [[HTML]] to allow JavaScript to generate media streams for playback. Allowing JavaScript to generate streams facilitates a variety of use cases like adaptive streaming and time shifting live streams.

On top of editorial updates, substantives changes since publication as a W3C Recommendation in November 2016 are the addition of a {{SourceBuffer/changeType()}} method to switch codecs, the possibility to create and use {{MediaSource}} objects off the main thread in dedicated workers, and the removal of the createObjectURL() extension to the {{URL}} object following its integration in the File API [[FILEAPI]]. For a full list of changes done since the previous version, see the commits.

The working group maintains a list of all bug reports that the editors have not yet tried to address.

Implementors should be aware that this specification is not stable. Implementors who are not taking part in the discussions are likely to find the specification changing out from under them in incompatible ways. Vendors interested in implementing this specification before it eventually reaches the Candidate Recommendation stage should track the GitHub repository and take part in the discussions.

Introduction

This specification allows JavaScript to dynamically construct media streams for <audio> and <video>. It defines a MediaSource object that can serve as a source of media data for an HTMLMediaElement. MediaSource objects have one or more SourceBuffer objects. Applications append data segments to the SourceBuffer objects, and can adapt the quality of appended data based on system performance and other factors. Data from the SourceBuffer objects is managed as track buffers for audio, video and text data that is decoded and played. Byte stream specifications used with these extensions are available in the byte stream format registry [[MSE-REGISTRY]].

Media Source Pipeline Model Diagram

Goals

This specification was designed with the following goals in mind:

This specification defines:

Definitions

Active Track Buffers

The [=track buffers=] that provide [=coded frames=] for the {{AudioTrack/enabled}} {{HTMLMediaElement/audioTracks}}, the {{VideoTrack/selected}} {{HTMLMediaElement/videoTracks}}, and the or {{HTMLMediaElement/textTracks}}. All these tracks are associated with SourceBuffer objects in the {{MediaSource/activeSourceBuffers}} list.

Append Window

A [=presentation timestamp=] range used to filter out [=coded frames=] while appending. The append window represents a single continuous time range with a single start time and end time. Coded frames with [=presentation timestamp=] within this range are allowed to be appended to the SourceBuffer while coded frames outside this range are filtered out. The append window start and end times are controlled by the {{SourceBuffer/appendWindowStart}} and {{SourceBuffer/appendWindowEnd}} attributes respectively.

Coded Frame

A unit of media data that has a [=presentation timestamp=], a [=decode timestamp=], and a [=coded frame duration=].

Coded Frame Duration

The duration of a [=coded frame=]. For video and text, the duration indicates how long the video frame or text SHOULD be displayed. For audio, the duration represents the sum of all the samples contained within the coded frame. For example, if an audio frame contained 441 samples @44100Hz the frame duration would be 10 milliseconds.

Coded Frame End Timestamp

The sum of a [=coded frame=] [=presentation timestamp=] and its [=coded frame duration=]. It represents the [=presentation timestamp=] that immediately follows the coded frame.

Coded Frame Group

A group of [=coded frames=] that are adjacent and have monotonically increasing [=decode timestamps=] without any gaps. Discontinuities detected by the [=coded frame processing=] algorithm and {{SourceBuffer/abort()}} calls trigger the start of a new coded frame group.

Decode Timestamp

The decode timestamp indicates the latest time at which the frame needs to be decoded assuming instantaneous decoding and rendering of this and any dependant frames (this is equal to the [=presentation timestamp=] of the earliest frame, in [=presentation order=], that is dependant on this frame). If frames can be decoded out of [=presentation order=], then the decode timestamp MUST be present in or derivable from the byte stream. The user agent MUST run the [=append error=] algorithm if this is not the case. If frames cannot be decoded out of [=presentation order=] and a decode timestamp is not present in the byte stream, then the decode timestamp is equal to the [=presentation timestamp=].

Initialization Segment

A sequence of bytes that contain all of the initialization information required to decode a sequence of [=media segments=]. This includes codec initialization data, [=Track ID=] mappings for multiplexed segments, and timestamp offsets (e.g., edit lists).

The [=byte stream format specifications=] in the byte stream format registry [[MSE-REGISTRY]] contain format specific examples.

Media Segment

A sequence of bytes that contain packetized & timestamped media data for a portion of the . Media segments are always associated with the most recently appended [=initialization segment=].

The [=byte stream format specifications=] in the byte stream format registry [[MSE-REGISTRY]] contain format specific examples.

MediaSource object URL

A MediaSource object URL is a unique [[FILEAPI]] created by {{URL/createObjectURL()}}. It is used to attach a MediaSource object to an HTMLMediaElement.

These URLs are the same as a , except that anything in the definition of that feature that refers to and objects is hereby extended to also apply to MediaSource objects.

The [=origin=] of the MediaSource object URL is the [=relevant settings object=] of this during the call to {{URL/createObjectURL()}}.

For example, the [=origin=] of the MediaSource object URL affects the way that the media element is consumed by canvas.

Parent Media Source

The parent media source of a SourceBuffer object is the MediaSource object that created it.

Presentation Start Time

The presentation start time is the earliest time point in the presentation and specifies the initial playback position and earliest possible position. All presentations created using this specification have a presentation start time of 0.

For the purposes of determining if {{HTMLMediaElement}}.{{HTMLMediaElement/buffered}} contains a {{TimeRanges}} that includes the current playback position, implementations MAY choose to allow a current playback position at or after [=presentation start time=] and before the first {{TimeRanges}} to play the first {{TimeRanges}} if that {{TimeRanges}} starts within a reasonably short time, like 1 second, after [=presentation start time=]. This allowance accommodates the reality that muxed streams commonly do not begin all tracks precisely at [=presentation start time=]. Implementations MUST report the actual buffered range, regardless of this allowance.

Presentation Interval

The presentation interval of a [=coded frame=] is the time interval from its [=presentation timestamp=] to the [=presentation timestamp=] plus the [=coded frame duration|coded frame's duration=]. For example, if a coded frame has a presentation timestamp of 10 seconds and a [=coded frame duration=] of 100 milliseconds, then the presentation interval would be [10-10.1). Note that the start of the range is inclusive, but the end of the range is exclusive.

Presentation Order

The order that [=coded frames=] are rendered in the presentation. The presentation order is achieved by ordering [=coded frames=] in monotonically increasing order by their [=presentation timestamps=].

Presentation Timestamp

A reference to a specific time in the presentation. The presentation timestamp in a [=coded frame=] indicates when the frame SHOULD be rendered.

Random Access Point

A position in a [=media segment=] where decoding and continuous playback can begin without relying on any previous data in the segment. For video this tends to be the location of I-frames. In the case of audio, most audio frames can be treated as a random access point. Since video tracks tend to have a more sparse distribution of random access points, the location of these points are usually considered the random access points for multiplexed streams.

SourceBuffer byte stream format specification

The specific [=byte stream format specification=] that describes the format of the byte stream accepted by a SourceBuffer instance. The [=byte stream format specification=], for a SourceBuffer object, is initially selected based on the |type:DOMString| passed to the {{MediaSource/addSourceBuffer()}} call that created the object, and can be updated by {{SourceBuffer/changeType()}} calls on the object.

SourceBuffer configuration

A specific set of tracks distributed across one or more SourceBuffer objects owned by a single MediaSource instance.

Implementations MUST support at least 1 MediaSource object with the following configurations:

  • A single SourceBuffer with 1 audio track and/or 1 video track.
  • Two SourceBuffers with one handling a single audio track and the other handling a single video track.

MediaSource objects MUST support each of the configurations above, but they are only required to support one configuration at a time. Supporting multiple configurations at once or additional configurations is a quality of implementation issue.

Track Description

A byte stream format specific structure that provides the [=Track ID=], codec configuration, and other metadata for a single track. Each track description inside a single [=initialization segment=] has a unique [=Track ID=]. The user agent MUST run the [=append error=] algorithm if the [=Track ID=] is not unique within the [=initialization segment=].

Track ID

A Track ID is a byte stream format specific identifier that marks sections of the byte stream as being part of a specific track. The Track ID in a [=track description=] identifies which sections of a [=media segment=] belong to that track.

MediaSource Object

The MediaSource object represents a source of media data for an HTMLMediaElement. It keeps track of the {{MediaSource/readyState}} for this source as well as a list of SourceBuffer objects that can be used to add media data to the presentation. MediaSource objects are created by the web application and then attached to an HTMLMediaElement. The application uses the SourceBuffer objects in {{MediaSource/sourceBuffers}} to add media data to this source. The HTMLMediaElement fetches this media data from the MediaSource object when it is needed during playback.

Each {{MediaSource}} object has a [[\live seekable range]] internal slot that stores a . It is initialized to an empty {{TimeRanges}} object when the {{MediaSource}} object is created, is maintained by {{MediaSource/setLiveSeekableRange()}} and {{MediaSource/clearLiveSeekableRange()}}, and is used in HTMLMediaElement Extensions to modify {{HTMLMediaElement}}.{{HTMLMediaElement/seekable}} behavior.

Each {{MediaSource}} object has a [[\has ever been attached]] internal slot that stores a {{boolean}}. It is initialized to false when the {{MediaSource}} object is created, and is set true in the extended {{HTMLMediaElement}}'s [=resource fetch algorithm=] as described in the [=attaching to a media element=] algorithm. The extended [=resource fetch algorithm=] uses this internal slot to conditionally fail attachment of a {{MediaSource}} using a {{MediaSourceHandle}} set on a {{HTMLMediaElement}}'s {{HTMLMediaElement/srcObject}} attribute.

        enum ReadyState {
          "closed",
          "open",
          "ended",
        };
      
closed
Indicates the source is not currently attached to a media element.
open
The source has been opened by a media element and is ready for data to be appended to the {{SourceBuffer}} objects in {{MediaSource}}'s {{MediaSource/sourceBuffers}}.
ended
The source is still attached to a media element, but {{MediaSource}}'s {{MediaSource/endOfStream()}} has been called.
        enum EndOfStreamError {
          "network",
          "decode",
        };
      
network

Terminates playback and signals that a network error has occurred.

JavaScript applications SHOULD use this status code to terminate playback with a network error. For example, if a network error occurs while fetching media data.

decode

Terminates playback and signals that a decoding error has occurred.

JavaScript applications SHOULD use this status code to terminate playback with a decode error. For example, if a parsing error occurs while processing out-of-band media data.

[Exposed=(Window,DedicatedWorker)]
interface MediaSource : EventTarget {
    constructor();

    [ SameObject, Exposed=DedicatedWorker ]
    readonly              attribute MediaSourceHandle   handle;

    readonly              attribute SourceBufferList    sourceBuffers;
    readonly              attribute SourceBufferList    activeSourceBuffers;
    readonly              attribute ReadyState          readyState;
                          attribute unrestricted double duration;
                          attribute EventHandler        onsourceopen;
                          attribute EventHandler        onsourceended;
                          attribute EventHandler        onsourceclose;
    static readonly       attribute boolean canConstructInDedicatedWorker;
    SourceBuffer          addSourceBuffer (DOMString type);
    undefined             removeSourceBuffer (SourceBuffer sourceBuffer);
    undefined             endOfStream (optional EndOfStreamError error);
    undefined             setLiveSeekableRange (double start, double end);
    undefined             clearLiveSeekableRange ();
    static boolean        isTypeSupported (DOMString type);
};

Attributes

handle of type MediaSourceHandle, [SameObject] readonly

Contains a handle useful for attachment of a dedicated worker {{MediaSource}} object to an {{HTMLMediaElement}} via {{HTMLMediaElement/srcObject}}. The handle remains the same object for this {{MediaSource}} object across accesses of this attribute, but it is distinct for each {{MediaSource}} object.

This specification may eventually enable visibility of this attribute on {{MediaSource}} objects on the main Window context. If so, specification care will be necessary to prevent potential backwards incompatible changes, such as could happen if exceptions were thrown on accesses to this attribute.

On getting, run the following steps:

  1. If the handle for this {{MediaSource}} object has not yet been created, then run the following steps:
    1. Let |created handle:MediaSourceHandle| be the result of creating a new {{MediaSourceHandle}} object and associated resources, linked internally to this {{MediaSource}}.
    2. Update the attribute to be |created handle|.
  2. Return the {{MediaSourceHandle}} object that is this attribute's value.
sourceBuffers of type SourceBufferList, readonly
Contains the list of SourceBuffer objects associated with this MediaSource. When {{MediaSource/readyState}} equals {{ReadyState/""closed""}} this list will be empty. Once {{MediaSource/readyState}} transitions to {{ReadyState/""open""}} SourceBuffer objects can be added to this list by using {{MediaSource/addSourceBuffer()}}.
activeSourceBuffers of type SourceBufferList, readonly

Contains the subset of {{MediaSource/sourceBuffers}} that are providing the {{VideoTrack/selected}} video track, the {{AudioTrack/enabled}} audio track(s), and the or text track(s).

SourceBuffer objects in this list MUST appear in the same order as they appear in the {{MediaSource/sourceBuffers}} attribute; e.g., if only sourceBuffers[0] and sourceBuffers[3] are in {{MediaSource/activeSourceBuffers}}, then activeSourceBuffers[0] MUST equal sourceBuffers[0] and activeSourceBuffers[1] MUST equal sourceBuffers[3].

The Changes to selected/enabled track state section describes how this attribute gets updated.

readyState of type {{ReadyState}}, readonly

Indicates the current state of the MediaSource object. When the MediaSource is created {{MediaSource/readyState}} MUST be set to {{ReadyState/""closed""}}.

duration of type {{unrestricted double}}

Allows the web application to set the presentation duration. The duration is initially set to NaN when the MediaSource object is created.

On getting, run the following steps:

  1. If the {{MediaSource/readyState}} attribute is {{ReadyState/""closed""}} then return NaN and abort these steps.
  2. Return the current value of the attribute.

On setting, run the following steps:

  1. If the value being set is negative or NaN then throw a {{TypeError}} exception and abort these steps.
  2. If the {{MediaSource/readyState}} attribute is not {{ReadyState/""open""}} then throw an {{InvalidStateError}} exception and abort these steps.
  3. If the {{SourceBuffer/updating}} attribute equals true on any SourceBuffer in {{MediaSource/sourceBuffers}}, then throw an {{InvalidStateError}} exception and abort these steps.
  4. Run the [=duration change=] algorithm with |new duration:unrestricted double| set to the value being assigned to this attribute.

    The [=duration change=] algorithm will adjust |new duration| higher if there is any currently buffered coded frame with a higher end time.

    {{SourceBuffer/appendBuffer()}} and {{MediaSource/endOfStream()}} can update the duration under certain circumstances.

onsourceopen of type {{EventHandler}}

The event handler for the {{sourceopen}} event.

onsourceended of type {{EventHandler}}

The event handler for the {{sourceended}} event.

onsourceclose of type {{EventHandler}}

The event handler for the {{sourceclose}} event.

canConstructInDedicatedWorker of type {{boolean}}

Returns true.

This attribute enables main thread and dedicated worker feature detection of support for creating and using a {{MediaSource}} object in a dedicated worker, and mitigates the need for higher latency detection polyfills like attempting creation of a {{MediaSource}} object from a dedicated worker, especially if the feature is not supported.

Methods

addSourceBuffer

Adds a new SourceBuffer to {{MediaSource/sourceBuffers}}.

  1. If |type:DOMString| is an empty string then throw a {{TypeError}} exception and abort these steps.
  2. If |type| contains a MIME type that is not supported or contains a MIME type that is not supported with the types specified for the other SourceBuffer objects in {{MediaSource/sourceBuffers}}, then throw a {{NotSupportedError}} exception and abort these steps.
  3. If the user agent can't handle any more SourceBuffer objects or if creating a SourceBuffer based on |type| would result in an unsupported [=SourceBuffer configuration=], then throw a {{QuotaExceededError}} exception and abort these steps.

    For example, a user agent MAY throw a {{QuotaExceededError}} exception if the media element has reached the {{HTMLMediaElement/HAVE_METADATA}} readyState. This can occur if the user agent's media engine does not support adding more tracks during playback.

  4. If the {{MediaSource/readyState}} attribute is not in the {{ReadyState/""open""}} state then throw an {{InvalidStateError}} exception and abort these steps.
  5. Let |buffer| be a new instance of a {{ManagedSourceBuffer}} if |this| is a {{ManagedMediaSource}}, or a {{SourceBuffer}} otherwise, with their respective associated resources.
  6. Set |buffer|'s {{SourceBuffer/[[generate timestamps flag]]}} to the value in the "Generate Timestamps Flag" column of the [[[MSE-REGISTRY]]] entry that is associated with |type|.
  7. If |buffer|'s {{SourceBuffer/[[generate timestamps flag]]}} is true, set |buffer|'s {{SourceBuffer/mode}} to {{AppendMode/"sequence"}}. Otherwise, set |buffer|'s {{SourceBuffer/mode}} to {{AppendMode/"segments"}}.
  8. [=List/Append=] |buffer| to |this|'s {{MediaSource/sourceBuffers}}.
  9. [=Queue a task=] to [=fire an event=] named {{addsourcebuffer}} at |this|'s {{MediaSource/sourceBuffers}}.
  10. Return |buffer|.
removeSourceBuffer

Removes a {{SourceBuffer}} from {{MediaSource/sourceBuffers}}.

  1. If |sourceBuffer:SourceBuffer| specifies an object that is not in {{MediaSource/sourceBuffers}} then throw a {{NotFoundError}} exception and abort these steps.
  2. If the |sourceBuffer|.{{SourceBuffer/updating}} attribute equals true, then run the following steps:
    1. Abort the [=buffer append=] algorithm if it is running.
    2. Set the |sourceBuffer|.{{SourceBuffer/updating}} attribute to false.
    3. [=Queue a task=] to [=fire an event=] named {{abort}} at |sourceBuffer|.
    4. [=Queue a task=] to [=fire an event=] named {{updateend}} at |sourceBuffer|.
  3. Let |SourceBuffer audioTracks list:AudioTrackList| equal the {{AudioTrackList}} object returned by |sourceBuffer|.{{SourceBuffer/audioTracks}}.
  4. If the |SourceBuffer audioTracks list| is not empty, then run the following steps:
    1. For each {{AudioTrack}} object in the |SourceBuffer audioTracks list|, run the following steps:
      1. Set the {{AudioTrack/sourceBuffer}} attribute on the {{AudioTrack}} object to null.
      2. Remove the {{AudioTrack}} object from the |SourceBuffer audioTracks list|.

        This should trigger {{AudioTrackList}} [[HTML]] logic to [=queue a task=] to [=fire an event=] named [=AudioTrackList/removetrack=] using {{TrackEvent}} with the {{TrackEvent/track}} attribute initialized to the {{AudioTrack}} object, at the |SourceBuffer audioTracks list|. If the {{AudioTrack/enabled}} attribute on the {{AudioTrack}} object was true at the beginning of this removal step, then this should also trigger {{AudioTrackList}} [[HTML]] logic to [=queue a task=] to [=fire an event=] named [=AudioTrackList/change=] at the |SourceBuffer audioTracks list|.

      3. Use the [=mirror if necessary=] algorithm to run the following steps in {{Window}}, to remove the {{AudioTrack}} object (or instead, the {{Window}} mirror of it if the {{MediaSource}} object was constructed in a {{DedicatedWorkerGlobalScope}}) from the media element:
        1. Let |HTMLMediaElement audioTracks list:AudioTrackList| equal the {{AudioTrackList}} object returned by the {{HTMLMediaElement/audioTracks}} attribute on the HTMLMediaElement.
        2. Remove the {{AudioTrack}} object from the |HTMLMediaElement audioTracks list|.

          This should trigger {{AudioTrackList}} [[HTML]] logic to [=queue a task=] to [=fire an event=] named [=AudioTrackList/removetrack=] using {{TrackEvent}} with the {{TrackEvent/track}} attribute initialized to the {{AudioTrack}} object, at the |HTMLMediaElement audioTracks list|. If the {{AudioTrack/enabled}} attribute on the {{AudioTrack}} object was true at the beginning of this removal step, then this should also trigger {{AudioTrackList}} [[HTML]] logic to [=queue a task=] to [=fire an event=] named [=AudioTrackList/change=] at the |HTMLMediaElement audioTracks list|.

  5. Let |SourceBuffer videoTracks list:VideoTrackList| equal the {{VideoTrackList}} object returned by |sourceBuffer|.{{SourceBuffer/videoTracks}}.
  6. If the |SourceBuffer videoTracks list| is not empty, then run the following steps:
    1. For each {{VideoTrack}} object in the |SourceBuffer videoTracks list|, run the following steps:
      1. Set the {{VideoTrack/sourceBuffer}} attribute on the {{VideoTrack}} object to null.
      2. Remove the {{VideoTrack}} object from the |SourceBuffer videoTracks list|.

        This should trigger {{VideoTrackList}} [[HTML]] logic to [=queue a task=] to [=fire an event=] named [=VideoTrackList/removetrack=] using {{TrackEvent}} with the {{TrackEvent/track}} attribute initialized to the {{VideoTrack}} object, at the |SourceBuffer videoTracks list|. If the {{VideoTrack/selected}} attribute on the {{VideoTrack}} object was true at the beginning of this removal step, then this should also trigger {{VideoTrackList}} [[HTML]] logic to [=queue a task=] to [=fire an event=] named [=VideoTrackList/change=] at the |SourceBuffer videoTracks list|.

      3. Use the [=mirror if necessary=] algorithm to run the following steps in {{Window}}, to remove the {{VideoTrack}} object (or instead, the {{Window}} mirror of it if the {{MediaSource}} object was constructed in a {{DedicatedWorkerGlobalScope}}) from the media element:
        1. Let |HTMLMediaElement videoTracks list:VideoTrackList| equal the {{VideoTrackList}} object returned by the {{HTMLMediaElement/videoTracks}} attribute on the HTMLMediaElement.
        2. Remove the {{VideoTrack}} object from the |HTMLMediaElement videoTracks list|.

          This should trigger {{VideoTrackList}} [[HTML]] logic to [=queue a task=] to [=fire an event=] named [=VideoTrackList/removetrack=] using {{TrackEvent}} with the {{TrackEvent/track}} attribute initialized to the {{VideoTrack}} object, at the |HTMLMediaElement videoTracks list|. If the {{VideoTrack/selected}} attribute on the {{VideoTrack}} object was true at the beginning of this removal step, then this should also trigger {{VideoTrackList}} [[HTML]] logic to [=queue a task=] to [=fire an event=] named [=VideoTrackList/change=] at the |HTMLMediaElement videoTracks list|.

  7. Let |SourceBuffer textTracks list:TextTrackList| equal the {{TextTrackList}} object returned by |sourceBuffer|.{{SourceBuffer/textTracks}}.
  8. If the |SourceBuffer textTracks list| is not empty, then run the following steps:
    1. For each {{TextTrack}} object in the |SourceBuffer textTracks list|, run the following steps:
      1. Set the {{TextTrack/sourceBuffer}} attribute on the {{TextTrack}} object to null.
      2. Remove the {{TextTrack}} object from the |SourceBuffer textTracks list|.

        This should trigger {{TextTrackList}} [[HTML]] logic to [=queue a task=] to [=fire an event=] named [=TextTrackList/removetrack=] using {{TrackEvent}} with the {{TrackEvent/track}} attribute initialized to the {{TextTrack}} object, at the |SourceBuffer textTracks list|. If the {{TextTrack/mode}} attribute on the {{TextTrack}} object was or at the beginning of this removal step, then this should also trigger {{TextTrackList}} [[HTML]] logic to [=queue a task=] to [=fire an event=] named [=TextTrackList/change=] at the |SourceBuffer textTracks list|.

      3. Use the [=mirror if necessary=] algorithm to run the following steps in {{Window}}, to remove the {{TextTrack}} object (or instead, the {{Window}} mirror of it if the {{MediaSource}} object was constructed in a {{DedicatedWorkerGlobalScope}}) from the media element:
        1. Let |HTMLMediaElement textTracks list:TextTrackList| equal the {{TextTrackList}} object returned by the {{HTMLMediaElement/textTracks}} attribute on the HTMLMediaElement.
        2. Remove the {{TextTrack}} object from the |HTMLMediaElement textTracks list|.

          This should trigger {{TextTrackList}} [[HTML]] logic to [=queue a task=] to [=fire an event=] named [=TextTrackList/removetrack=] using {{TrackEvent}} with the {{TrackEvent/track}} attribute initialized to the {{TextTrack}} object, at the |HTMLMediaElement textTracks list|. If the {{TextTrack/mode}} attribute on the {{TextTrack}} object was or at the beginning of this removal step, then this should also trigger {{TextTrackList}} [[HTML]] logic to [=queue a task=] to [=fire an event=] named [=TextTrackList/change=] at the |HTMLMediaElement textTracks list|.

  9. If |sourceBuffer| is in {{MediaSource/activeSourceBuffers}}, then remove |sourceBuffer| from {{MediaSource/activeSourceBuffers}} and [=queue a task=] to [=fire an event=] named {{removesourcebuffer}} at the SourceBufferList returned by {{MediaSource/activeSourceBuffers}}.
  10. Remove |sourceBuffer| from {{MediaSource/sourceBuffers}} and [=queue a task=] to [=fire an event=] named {{removesourcebuffer}} at the SourceBufferList returned by {{MediaSource/sourceBuffers}}.
  11. Destroy all resources for |sourceBuffer|.
ParameterTypeNullableOptionalDescription
|sourceBuffer|{{SourceBuffer}}
Return type: {{undefined}}
endOfStream

Signals the end of the stream.

  1. If the {{MediaSource/readyState}} attribute is not in the {{ReadyState/""open""}} state then throw an {{InvalidStateError}} exception and abort these steps.
  2. If the {{SourceBuffer/updating}} attribute equals true on any SourceBuffer in {{MediaSource/sourceBuffers}}, then throw an {{InvalidStateError}} exception and abort these steps.
  3. Run the [=end of stream=] algorithm with the error parameter set to |error:EndOfStreamError|.
ParameterTypeNullableOptionalDescription
|error|{{EndOfStreamError}}
Return type: {{undefined}}
setLiveSeekableRange

Updates {{MediaSource/[[live seekable range]]}} that is used in HTMLMediaElement Extensions to modify {{HTMLMediaElement}}.{{HTMLMediaElement/seekable}} behavior.

  1. If the {{MediaSource/readyState}} attribute is not {{ReadyState/""open""}} then throw an {{InvalidStateError}} exception and abort these steps.
  2. If |start:double| is negative or greater than |end:double|, then throw a {{TypeError}} exception and abort these steps.
  3. Set {{MediaSource/[[live seekable range]]}} to be a new containing a single range whose start position is |start| and end position is |end|.
Parameter Type Nullable Optional Description
|start| {{double}} The start of the range, in seconds measured from [=presentation start time=]. While set, and if {{MediaSource/duration}} equals positive Infinity, {{HTMLMediaElement}}.{{HTMLMediaElement/seekable}} will return a non-empty TimeRanges object with a lowest range start timestamp no greater than |start|.
|end| {{double}} The end of range, in seconds measured from [=presentation start time=]. While set, and if {{MediaSource/duration}} equals positive Infinity, {{HTMLMediaElement}}.{{HTMLMediaElement/seekable}} will return a non-empty TimeRanges object with a highest range end timestamp no less than |end|.
Return type: {{undefined}}
clearLiveSeekableRange

Updates {{MediaSource/[[live seekable range]]}} that is used in HTMLMediaElement Extensions to modify {{HTMLMediaElement}}.{{HTMLMediaElement/seekable}} behavior.

  1. If the {{MediaSource/readyState}} attribute is not {{ReadyState/""open""}} then throw an {{InvalidStateError}} exception and abort these steps.
  2. If {{MediaSource/[[live seekable range]]}} contains a range, then set {{MediaSource/[[live seekable range]]}} to be a new empty {{TimeRanges}} object.
No parameters.
Return type: {{undefined}}
isTypeSupported, static

Check to see whether the MediaSource is capable of creating SourceBuffer objects for the specified MIME type.

  1. If |type:DOMString| is an empty string, then return false.
  2. If |type| does not contain a valid MIME type string, then return false.
  3. If |type| contains a media type or media subtype that the MediaSource does not support, then return false.
  4. If |type| contains a codec that the MediaSource does not support, then return false.
  5. If the MediaSource does not support the specified combination of media type, media subtype, and codecs then return false.
  6. Return true.

If true is returned from this method, it only indicates that the MediaSource implementation is capable of creating SourceBuffer objects for the specified MIME type. An {{MediaSource/addSourceBuffer()}} call SHOULD still fail if sufficient resources are not available to support the addition of a new SourceBuffer.

This method returning true implies that HTMLMediaElement.canPlayType() will return "maybe" or "probably" since it does not make sense for a MediaSource to support a type the HTMLMediaElement knows it cannot play.

ParameterTypeNullableOptionalDescription
|type|{{DOMString}}
Return type: {{boolean}}

Event Summary

Event name Interface Dispatched when...
sourceopen {{Event}} {{MediaSource}}'s {{MediaSource/readyState}} transitions from {{ReadyState/"closed"}} to {{ReadyState/"open"}} or from {{ReadyState/"ended"}} to {{ReadyState/"open"}}.
sourceended {{Event}} {{MediaSource}}'s {{MediaSource/readyState}} transitions from {{ReadyState/"open"}} to {{ReadyState/"ended"}}.
sourceclose {{Event}} {{MediaSource}}'s {{MediaSource/readyState}} transitions from {{ReadyState/"open"}} to {{ReadyState/"closed"}} or {{ReadyState/"ended"}} to {{ReadyState/"closed"}}.

Cross-context communication model

When a {{Window}} {{HTMLMediaElement}} is attached to a {{DedicatedWorkerGlobalScope}} {{MediaSource}}, each context has algorithms that depend on information from the other.

{{HTMLMediaElement}} is exposed only to {{Window}} contexts, but {{MediaSource}} and related objects defined in this specification are exposed in {{Window}} and {{DedicatedWorkerGlobalScope}} contexts. This lets applications construct a {{MediaSource}} object in either of those types of context and attach it to an {{HTMLMediaElement}} object in a {{Window}} context using a [=MediaSource object URL=] or a {{MediaSourceHandle}} as described in the [=attaching to a media element=] algorithm. A {{MediaSource}} object is not {{Transferable}}; it is only visible in the context where it was created.

The rest of this section describes a model for bounding information latency for attachments of a {{Window}} media element to a {{DedicatedWorkerGlobalScope}} {{MediaSource}}. While the model describes communication using message passing, implementations MAY choose to communicate in potentially faster ways, such as using shared memory and locks. Attachments to a {{Window}} {{MediaSource}} synchronously have the information already without communicating it across contexts.

A {{MediaSource}} that is constructed in a {{DedicatedWorkerGlobalScope}} has a [[\port to main]] internal slot that stores a {{MessagePort}} setup during attachment and nulled during detachment. A {{Window}} {{MediaSource/[[port to main]]}} is always null.

An {{HTMLMediaElement}} extended by this specification and attached to a {{DedicatedWorkerGlobalScope}} {{MediaSource}} similarly has a [[\port to worker]] internal slot that stores a {{MessagePort}} and a [[\channel with worker]] internal slot that stores a {{MessageChannel}}, both setup during attachment and nulled during detachment. Both {{HTMLMediaElement/[[port to worker]]}} and {{HTMLMediaElement/[[channel with worker]]}} are null unless attached to a {{DedicatedWorkerGlobalScope}} {{MediaSource}}.

Algorithms in this specification that need to communicate information from a {{Window}} {{HTMLMediaElement}} to an attached {{DedicatedWorkerGlobalScope}} {{MediaSource}}, or vice versa, will use these internal ports implicitly to post a message to their counterpart, where the implicit handler of the message runs steps as described in the algorithms.

Algorithms

Attaching to a media element

There are distinct mechanisms for attaching a {{MediaSource}} to a media element depending on where the {{MediaSource}} object was constructed, in a {{Window}} versus in a {{DedicatedWorkerGlobalScope}}:

  • Attaching a {{MediaSource}} that was constructed in a {{Window}} can be done by assigning a [=MediaSource object URL=] for that {{MediaSource}} to the media element {{HTMLMediaElement/src}} attribute or the src attribute of a <source> inside a media element. A [=MediaSource object URL=] is created by passing a MediaSource object to {{URL/createObjectURL()}}.

    Though implementations MAY allow [=MediaSource object URL=] creation in a {{DedicatedWorkerGlobalScope}} for a {{MediaSource}} constructed in that worker, attempting to use that [=MediaSource object URL=] to attach to a media element using either the {{HTMLMediaElement/src}} attribute or the src attribute of a <source> inside a media element MUST fail in the media element's [=resource fetch algorithm=], as extended below.

    Extending the object URL attachment mechanism to worker MediaSource object URLs would further propagate this idiom that is less preferred versus using srcObject, and would unnecessarily increase user agent interoperability risk and implementation complexity.

  • Attaching a {{MediaSource}} that was constructed in a {{DedicatedWorkerGlobalScope}} can only be done by obtaining a handle from it using {{MediaSource/handle}}, transferring that {{MediaSourceHandle}} to the {{Window}} context and assigning it to the media element {{HTMLMediaElement/srcObject}} attribute. For the purposes of aligning this specification with {{HTMLMediaElement}} resource loading and fetching algorithms, the underlying {{DedicatedWorkerGlobalScope}} {{MediaSource}} is the MediaSource object mentioned there, and the {{MediaSourceHandle}} object is the media provider object.

If the [=resource fetch algorithm=] was invoked with a media provider object that is a {{MediaSource}} object, a {{MediaSourceHandle}} object or a URL record whose object is a {{MediaSource}} object, then let mode be local, skip the first step in the [=resource fetch algorithm=] (which may otherwise set mode to remote) and continue the execution of the [=resource fetch algorithm=].

The first step of the [=resource fetch algorithm=] is expected to eventually align with selecting local mode for URL records whose objects are media provider objects. The intent is that if the HTMLMediaElement's src attribute or selected child <source>'s src attribute is a blob: URL matching a [=MediaSource object URL=] when the respective src attribute was last changed, then that MediaSource object is used as the media provider object and current media resource in the local mode logic in the [=resource fetch algorithm=]. This also means that the remote mode logic that includes observance of any preload attribute is skipped when a MediaSource object is attached. Even with that eventual change to [[HTML]], the execution of the following steps at the beginning of the local mode logic is still required when the current media resource is a MediaSource object.

At the beginning of the "Otherwise (mode is local)" section of the [=resource fetch algorithm=], execute the additional steps, below.

Relative to the action which triggered the media element's resource selection algorithm, these steps are asynchronous. The [=resource fetch algorithm=] is run after the task that invoked the resource selection algorithm is allowed to continue and a stable state is reached. Implementations may delay the steps in the "Otherwise" clause, below, until the MediaSource object is ready for use.

  1. If the [=resource fetch algorithm=] was invoked with a media provider object that is a {{MediaSource}} object, a {{MediaSourceHandle}} object or a URL record whose object is a {{MediaSource}} object, then:
    If the media provider object is a URL record whose object is a {{MediaSource}} that was constructed in a {{DedicatedWorkerGlobalScope}}, such as would occur if attempting to use a [=MediaSource object URL=] from a {{DedicatedWorkerGlobalScope}} {{MediaSource}}
    Run the steps of the [=resource fetch algorithm=]'s .
    This prevents using [=MediaSource object URLs=] for DedicatedWorker MediaSource attachments. Transferring {{MediaSource}}.{{MediaSource/handle}} from the DedicatedWorker to the Window context and assigning it to the media element's {{HTMLMediaElement/srcObject}} attribute is the only way to attach such a MediaSource.
    If the media provider object is a {{MediaSourceHandle}} whose {{MediaSourceHandle/[[Detached]]}} internal slot is true
    Run the steps of the [=resource fetch algorithm=]'s .
    If the media provider object is a {{MediaSourceHandle}} whose underlying {{MediaSource}}'s {{MediaSource/[[has ever been attached]]}} internal slot is true
    Run the steps of the [=resource fetch algorithm=]'s .
    This prevents loading an underlying {{MediaSource}} more than once using a {{MediaSourceHandle}}, even if the {{MediaSource}} was constructed on {{Window}} and had been loaded previously using a [=MediaSource object URL=]. This doesn't preclude subsequent use of a [=MediaSource object URL=] for a {{Window}} {{MediaSource}} from succeeding though.
    If {{MediaSource/readyState}} is NOT set to {{ReadyState/""closed""}}
    Run the steps of the [=resource fetch algorithm=]'s .
    Otherwise
    1. Set the {{MediaSource}}'s {{MediaSource/[[has ever been attached]]}} internal slot to true.
    2. Set the media element's to false.
    3. If the {{MediaSource}} was constructed in a {{DedicatedWorkerGlobalScope}}, then setup worker attachment communication and open the {{MediaSource}}:
      1. Set {{HTMLMediaElement/[[channel with worker]]}} to be a new {{MessageChannel}}.
      2. Set {{HTMLMediaElement/[[port to worker]]}} to the {{MessageChannel/port1}} value of {{HTMLMediaElement/[[channel with worker]]}}.
      3. Execute [=StructuredSerializeWithTransfer=] with the {{MessageChannel/port2}} of {{HTMLMediaElement/[[channel with worker]]}} as both the value and the sole member of the |transferList|, and let the result be |serialized port2:MessagePort|.
      4. [=Queue a task=] on the {{MediaSource}}'s {{DedicatedWorkerGlobalScope}} that will
        1. Execute [=StructuredDeserializeWithTransfer=] with |serialized port2| and {{DedicatedWorkerGlobalScope}}'s [=environment settings object/realm=], and set {{MediaSource/[[port to main]]}} to be the resulting deserialized clone of the transferred {{MessageChannel/port2}} value of {{HTMLMediaElement/[[channel with worker]]}}.
        2. Set the {{MediaSource/readyState}} attribute to {{ReadyState/""open""}}.
        3. [=Queue a task=] to [=fire an event=] named {{sourceopen}} at the MediaSource.
      Otherwise, the {{MediaSource}} was constructed in a {{Window}}:
      1. Set {{HTMLMediaElement/[[channel with worker]]}} null.
      2. Set {{HTMLMediaElement/[[port to worker]]}} null.
      3. Set {{MediaSource/[[port to main]]}} null.
      4. Set the {{MediaSource/readyState}} attribute to {{ReadyState/""open""}}.
      5. [=Queue a task=] to [=fire an event=] named {{sourceopen}} at the MediaSource.
    4. Continue the [=resource fetch algorithm=] by running the remaining steps, with these requirements:
      1. Text in the [=resource fetch algorithm=] or the that refers to "the download", "bytes received", or "whenever new data for the current media resource becomes available" refers to data passed in via {{SourceBuffer/appendBuffer()}}.
      2. References to HTTP in the [=resource fetch algorithm=] and the shall not apply because the HTMLMediaElement does not fetch media data via HTTP when a {{MediaSource}} is attached.

An attached MediaSource does not use the remote mode steps in the [=resource fetch algorithm=], so the media element will not fire "suspend" events. Though future versions of this specification will likely remove "progress" and "stalled" events from a media element with an attached MediaSource, user agents conforming to this version of the specification may still fire these two events as these [[HTML]] references changed after implementations of this specification stabilized.

Detaching from a media element

The following steps are run in any case where the media element is going to transition to {{HTMLMediaElement/NETWORK_EMPTY}} and [=queue a task=] to [=fire an event=] named [=HTMLMediaElement/emptied=] at the media element. These steps SHOULD be run right before the transition.

  1. If the {{MediaSource}} was constructed in a {{DedicatedWorkerGlobalScope}}:
    1. Notify the {{MediaSource}} using an internal detach message posted to {{HTMLMediaElement/[[port to worker]]}}.
    2. Set {{HTMLMediaElement/[[port to worker]]}} null.
    3. Set {{HTMLMediaElement/[[channel with worker]]}} null.
    4. The implicit message handler for this detach notification runs the remainder of these steps in the {{DedicatedWorkerGlobalScope}} {{MediaSource}}.
    Otherwise, the {{MediaSource}} was constructed in a {{Window}}:
    Continue the remainder of these steps on the {{Window}} {{MediaSource}}.
  2. Set {{MediaSource/[[port to main]]}} null.
  3. Set the {{MediaSource/readyState}} attribute to {{ReadyState/""closed""}}.
  4. If [=this=] is a {{ManagedMediaSource}}, then set {{ManagedMediaSource/streaming}} attribute to `false`.
  5. Update {{MediaSource/duration}} to NaN.
  6. Remove all the SourceBuffer objects from {{MediaSource/activeSourceBuffers}}.
  7. [=Queue a task=] to [=fire an event=] named {{removesourcebuffer}} at {{MediaSource/activeSourceBuffers}}.
  8. Remove all the SourceBuffer objects from {{MediaSource/sourceBuffers}}.
  9. [=Queue a task=] to [=fire an event=] named {{removesourcebuffer}} at {{MediaSource/sourceBuffers}}.
  10. [=Queue a task=] to [=fire an event=] named {{sourceclose}} at the MediaSource.

Going forward, this algorithm is intended to be externally called and run in any case where the attached MediaSource, if any, must be detached from the media element. It MAY be called on HTMLMediaElement [[HTML]] operations like load() and [=resource fetch algorithm=] failures in addition to, or in place of, when the media element transitions to {{HTMLMediaElement/NETWORK_EMPTY}}. Resource fetch algorithm failures are those which abort either the resource fetch algorithm or the resource selection algorithm, with the exception that the "Final step" [[HTML]] is not considered a failure that triggers detachment.

Seeking

Run the following steps as part of the "Wait until the user agent has established whether or not the media data for the new playback position is available, and, if it is, until it has decoded enough data to play back that position" step of the :

  1. The media element looks for [=media segments=] containing the |new playback position:double| in each SourceBuffer object in {{MediaSource/activeSourceBuffers}}. Any position within a {{TimeRanges}} in the current value of the {{HTMLMediaElement}}.{{HTMLMediaElement/buffered}} attribute has all necessary media segments buffered for that position.

    If |new playback position| is not in any {{TimeRanges}} of {{HTMLMediaElement}}.{{HTMLMediaElement/buffered}}
    1. If the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute is greater than {{HTMLMediaElement/HAVE_METADATA}}, then set the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute to {{HTMLMediaElement/HAVE_METADATA}}.

      Per [[HTML]] logic, {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} changes may trigger events on the HTMLMediaElement.

    2. The media element waits until an {{SourceBuffer/appendBuffer()}} call causes the [=coded frame processing=] algorithm to set the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute to a value greater than {{HTMLMediaElement/HAVE_METADATA}}.

      The web application can use {{SourceBuffer/buffered}} and {{HTMLMediaElement}}.{{HTMLMediaElement/buffered}} to determine what the media element needs to resume playback.

    Otherwise
    Continue

    If the {{MediaSource/readyState}} attribute is {{ReadyState/""ended""}} and the |new playback position| is within a {{TimeRanges}} currently in {{HTMLMediaElement}}.{{HTMLMediaElement/buffered}}, then the seek operation must continue to completion here even if one or more currently selected or enabled track buffers' largest range end timestamp is less than |new playback position|. This condition should only occur due to logic in {{SourceBuffer/buffered}} when {{MediaSource/readyState}} is {{ReadyState/""ended""}}.

  2. The media element resets all decoders and initializes each one with data from the appropriate [=initialization segment=].
  3. The media element feeds [=coded frames=] from the [=active track buffers=] into the decoders starting with the closest [=random access point=] before the |new playback position|.
  4. Resume the at the "Await a stable state" step.

SourceBuffer Monitoring

The following steps are periodically run during playback to make sure that all of the SourceBuffer objects in {{MediaSource/activeSourceBuffers}} have [=enough data to ensure uninterrupted playback=]. Changes to {{MediaSource/activeSourceBuffers}} also cause these steps to run because they affect the conditions that trigger state transitions.

Having enough data to ensure uninterrupted playback is an implementation specific condition where the user agent determines that it currently has enough data to play the presentation without stalling for a meaningful period of time. This condition is constantly evaluated to determine when to transition the media element into and out of the {{HTMLMediaElement/HAVE_ENOUGH_DATA}} ready state. These transitions indicate when the user agent believes it has enough data buffered or it needs more data respectively.

An implementation MAY choose to use bytes buffered, time buffered, the append rate, or any other metric it sees fit to determine when it has enough data. The metrics used MAY change during playback so web applications SHOULD only rely on the value of {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} to determine whether more data is needed or not.

When the media element needs more data, the user agent SHOULD transition it from {{HTMLMediaElement/HAVE_ENOUGH_DATA}} to {{HTMLMediaElement/HAVE_FUTURE_DATA}} early enough for a web application to be able to respond without causing an interruption in playback. For example, transitioning when the current playback position is 500ms before the end of the buffered data gives the application roughly 500ms to append more data before playback stalls.

If the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute equals {{HTMLMediaElement/HAVE_NOTHING}}:
  1. Abort these steps.
If {{HTMLMediaElement}}.{{HTMLMediaElement/buffered}} does not contain a {{TimeRanges}} for the current playback position:
  1. Set the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute to {{HTMLMediaElement/HAVE_METADATA}}.

    Per [[HTML]] logic, {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} changes may trigger events on the HTMLMediaElement.

  2. Abort these steps.
If {{HTMLMediaElement}}.{{HTMLMediaElement/buffered}} contains a {{TimeRanges}} that includes the current playback position and [=enough data to ensure uninterrupted playback=]:
  1. Set the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute to {{HTMLMediaElement/HAVE_ENOUGH_DATA}}.

    Per [[HTML]] logic, {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} changes may trigger events on the HTMLMediaElement.

  2. Playback may resume at this point if it was previously suspended by a transition to {{HTMLMediaElement/HAVE_CURRENT_DATA}}.
  3. Abort these steps.
If {{HTMLMediaElement}}.{{HTMLMediaElement/buffered}} contains a {{TimeRanges}} that includes the current playback position and some time beyond the current playback position, then run the following steps:
  1. Set the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute to {{HTMLMediaElement/HAVE_FUTURE_DATA}}.

    Per [[HTML]] logic, {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} changes may trigger events on the HTMLMediaElement.

  2. Playback may resume at this point if it was previously suspended by a transition to {{HTMLMediaElement/HAVE_CURRENT_DATA}}.
  3. Abort these steps.
If {{HTMLMediaElement}}.{{HTMLMediaElement/buffered}} contains a {{TimeRanges}} that ends at the current playback position and does not have a range covering the time immediately after the current position:
  1. Set the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute to {{HTMLMediaElement/HAVE_CURRENT_DATA}}.

    Per [[HTML]] logic, {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} changes may trigger events on the HTMLMediaElement.

  2. Playback is suspended at this point since the media element doesn't have enough data to advance the .
  3. Abort these steps.

Changes to selected/enabled track state

During playback {{MediaSource/activeSourceBuffers}} needs to be updated if the {{VideoTrack/selected}} video track, the {{AudioTrack/enabled}} audio track(s), or a text track {{TextTrack/mode}} changes. When one or more of these changes occur the following steps need to be followed. Also, when {{MediaSource}} was constructed in a {{DedicatedWorkerGlobalScope}}, then each change that occurs to a {{Window}} mirror of a track created previously by the implicit handler for the internal create track mirror message MUST also be made to the corresponding {{DedicatedWorkerGlobalScope}} track using an internal update track state message posted to {{HTMLMediaElement/[[port to worker]]}} whose implicit handler makes the change and runs the following steps. Likewise, each change that occurs to a {{DedicatedWorkerGlobalScope}} track MUST also be made to the corresponding {{Window}} mirror of the track using an internal update track state message posted to {{MediaSource/[[port to main]]}} whose implicit handler makes the change to the mirror.

If the selected video track changes, then run the following steps:
  1. If the SourceBuffer associated with the previously selected video track is not associated with any other enabled tracks, run the following steps:
    1. Remove the SourceBuffer from {{MediaSource/activeSourceBuffers}}.
    2. [=Queue a task=] to [=fire an event=] named {{removesourcebuffer}} at {{MediaSource/activeSourceBuffers}}
  2. If the SourceBuffer associated with the newly selected video track is not already in {{MediaSource/activeSourceBuffers}}, run the following steps:
    1. Add the SourceBuffer to {{MediaSource/activeSourceBuffers}}.
    2. [=Queue a task=] to [=fire an event=] named {{addsourcebuffer}} at {{MediaSource/activeSourceBuffers}}
If an audio track becomes disabled and the SourceBuffer associated with this track is not associated with any other enabled or selected track, then run the following steps:
  1. Remove the SourceBuffer associated with the audio track from {{MediaSource/activeSourceBuffers}}
  2. [=Queue a task=] to [=fire an event=] named {{removesourcebuffer}} at {{MediaSource/activeSourceBuffers}}
If an audio track becomes enabled and the SourceBuffer associated with this track is not already in {{MediaSource/activeSourceBuffers}}, then run the following steps:
  1. Add the SourceBuffer associated with the audio track to {{MediaSource/activeSourceBuffers}}
  2. [=Queue a task=] to [=fire an event=] named {{addsourcebuffer}} at {{MediaSource/activeSourceBuffers}}
If a text track {{TextTrack/mode}} becomes and the SourceBuffer associated with this track is not associated with any other enabled or selected track, then run the following steps:
  1. Remove the SourceBuffer associated with the text track from {{MediaSource/activeSourceBuffers}}
  2. [=Queue a task=] to [=fire an event=] named {{removesourcebuffer}} at {{MediaSource/activeSourceBuffers}}
If a text track {{TextTrack/mode}} becomes or and the SourceBuffer associated with this track is not already in {{MediaSource/activeSourceBuffers}}, then run the following steps:
  1. Add the SourceBuffer associated with the text track to {{MediaSource/activeSourceBuffers}}
  2. [=Queue a task=] to [=fire an event=] named {{addsourcebuffer}} at {{MediaSource/activeSourceBuffers}}

Duration change

Follow these steps when {{MediaSource/duration}} needs to change to a |new duration:unrestricted double|.

  1. If the current value of {{MediaSource/duration}} is equal to |new duration|, then return.
  2. If |new duration| is less than the highest [=presentation timestamp=] of any buffered [=coded frames=] for all SourceBuffer objects in {{MediaSource/sourceBuffers}}, then throw an {{InvalidStateError}} exception and abort these steps.

    Duration reductions that would truncate currently buffered media are disallowed. When truncation is necessary, use {{SourceBuffer/remove()}} to reduce the buffered range before updating {{MediaSource/duration}}.

  3. Let |highest end time:unrestricted double| be the largest [=track buffer ranges=] end time across all the [=track buffers=] across all SourceBuffer objects in {{MediaSource/sourceBuffers}}.
  4. If |new duration| is less than |highest end time|, then

    This condition can occur because the [=coded frame removal=] algorithm preserves coded frames that start before the start of the removal range.

    1. Update |new duration| to equal |highest end time|.
  5. Update {{MediaSource/duration}} to |new duration|.
  6. Use the [=mirror if necessary=] algorithm to run the following steps in {{Window}} to update the media element's duration:
    1. Update the media element's {{HTMLMediaElement/duration}} to |new duration|.
    2. Run the .

End of stream

This algorithm gets called when the application signals the end of stream via an {{MediaSource/endOfStream()}} call or an algorithm needs to signal a decode error. This algorithm takes an |error:EndOfStreamError| parameter that indicates whether an error will be signalled.

  1. Change the {{MediaSource/readyState}} attribute value to {{ReadyState/""ended""}}.
  2. [=Queue a task=] to [=fire an event=] named {{sourceended}} at the MediaSource.
  3. If |error| is not set
    1. Run the [=duration change=] algorithm with |new duration:unrestricted double| set to the largest [=track buffer ranges=] end time across all the [=track buffers=] across all SourceBuffer objects in {{MediaSource/sourceBuffers}}.

      This allows the duration to properly reflect the end of the appended media segments. For example, if the duration was explicitly set to 10 seconds and only media segments for 0 to 5 seconds were appended before endOfStream() was called, then the duration will get updated to 5 seconds.

    2. Notify the media element that it now has all of the media data.
    If |error| is set to {{EndOfStreamError/""network""}}
    Use the [=mirror if necessary=] algorithm to run the following steps in {{Window}}:
    If the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute equals {{HTMLMediaElement/HAVE_NOTHING}}
    Run the steps of the [=resource fetch algorithm=]'s .
    If the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute is greater than {{HTMLMediaElement/HAVE_NOTHING}}
    Run the "If the connection is interrupted after some media data has been received, causing the user agent to give up trying to fetch the resource" steps of the [=resource fetch algorithm=]'s .
    If |error| is set to {{EndOfStreamError/""decode""}}
    Use the [=mirror if necessary=] algorithm to run the following steps in {{Window}}:
    If the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute equals {{HTMLMediaElement/HAVE_NOTHING}}
    Run the "If the media data can be fetched but is found by inspection to be in an unsupported format, or can otherwise not be rendered at all" steps of the [=resource fetch algorithm=]'s .
    If the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute is greater than {{HTMLMediaElement/HAVE_NOTHING}}
    Run the steps of the [=resource fetch algorithm=]'s .

Mirror if necessary

This algorithm is used to run steps on {{Window}} from a {{MediaSource}} attached from either the same {{Window}} or from a {{DedicatedWorkerGlobalScope}}, usually to update the state of the attached {{HTMLMediaElement}}. This algorithm takes a |steps| parameter that lists the steps to run on {{Window}}.

If the {{MediaSource}} was constructed in a {{DedicatedWorkerGlobalScope}}:
Post an internal mirror on window message to {{MediaSource/[[port to main]]}} whose implicit handler in {{Window}} will run |steps|. Return control to the caller without awaiting that handler's receipt of the message.
The purpose of the mirror message mechanism is to ensure that:
  1. |steps| run asynchronously as their own task on {{Window}} rather than these |steps| somehow happening in the middle of some other {{Window}} task's execution, and
  2. |steps| are run without blocking the synchronous execution and return of this algorithm on {{DedicatedWorkerGlobalScope}}.
Otherwise:
Run |steps|.

MediaSourceHandle Object

The MediaSourceHandle object represents a proxy for a {{MediaSource}} object that is useful for attaching a {{DedicatedWorkerGlobalScope}} {{MediaSource}} to a {{Window}} {{HTMLMediaElement}} using {{HTMLMediaElement/srcObject}} as described in the [=attaching to a media element=] algorithm.

This distinct object is necessary to attach a cross-context {{MediaSource}} to a media element because {{MediaSource}} objects themselves are not transferable since they are event targets.

Each {{MediaSourceHandle}} object has a [[\has ever been assigned as srcobject]] internal slot that stores a {{boolean}}. It is initialized to false when the {{MediaSourceHandle}} object is created, is set true in the extended {{HTMLMediaElement}}'s {{HTMLMediaElement/srcObject}} setter as described in HTMLMediaElement Extensions, and if true, prevents successful transfer of the {{MediaSourceHandle}} as described in the [=Transfer=] section.

{{MediaSourceHandle}} objects are {{Transferable}}, each having a [[\Detached]] internal slot that is used to ensure that once the handle object instance has been transferred, that instance cannot be transferred again.

[Transferable, Exposed=(Window,DedicatedWorker)]
interface MediaSourceHandle {};

Transfer

The {{MediaSourceHandle}} [=transfer steps=] and [=transfer-receiving steps=] require the implementation to maintain an implicit internal slot referencing the underlying {{MediaSource}} to enable [=attaching to a media element=] using {{HTMLMediaElement/srcObject}} and consequent setup of an attachment's [=cross-context communication model=].

Implementors should be aware that assumption of "move" semantics implied by {{Transferable}} is not always reality. For example, extensions or internal implementations of postMessage using broadcast may cause unintended multiple recipients of a transferred {{MediaSourceHandle}}. For this reason, implementations are guided to not resolve which potential clone of a transferred {{MediaSourceHandle}} is still valid for attachment until and unless any handle for the underlying {{MediaSource}} object is used in the asynchronous portion of the media element's resource selection algorithm. This is similar to the existing behavior for attachment via [=MediaSource object URLs=], which can be cloned easily, where such a URL is valid for at most one attachment start (across all of its potentially many clones).

Implementations MUST support at most one attachment (load) via {{HTMLMediaElement/srcObject}} ever for the {{MediaSource}} object underlying a {{MediaSourceHandle}}, regardless of potential cloning of the {{MediaSourceHandle}} due to varying implementations of {{Transferable}}.

See [=attaching to a media element=] for how this is enforced during the asynchronous portion of the media element's resource selection algorithm.

{{MediaSourceHandle}} is only exposed on {{Window}} and {{DedicatedWorkerGlobalScope}} contexts, and cannot successfully transfer between different [=ECMAScript/agent clusters=] [[!ECMASCRIPT]]. Transfer of a {{MediaSourceHandle}} object can only succeed within the same [=ECMAScript/agent cluster=].

For example, transfer of a {{MediaSourceHandle}} object from either a {{Window}} or {{DedicatedWorkerGlobalScope}} to either a SharedWorker or a ServiceWorker will not succeed. Developers should be aware of this difference versus [=MediaSource object URLs=] which are {{DOMString}}s that can be communicated many ways. Even so, [=attaching to a media element=] using a [=MediaSource object URL=] can only succeed for a {{MediaSource}} that was constructed in a {{Window}} context. See also the integration of the [=ECMAScript/agent=] and [=ECMAScript/agent cluster=] formalisms for Web Application APIs [[HTML]] where related concepts such as [=dedicated worker agents=] are defined.

[=Transfer steps=] for a {{MediaSourceHandle}} object MUST include the following step:

  1. If the {{MediaSourceHandle}}'s {{MediaSourceHandle/[[has ever been assigned as srcobject]]}} internal slot is true, then the [=transfer steps=] must fail by throwing a {{DataCloneError}} exception.

SourceBuffer Object

        enum AppendMode {
          "segments",
          "sequence",
        };
      
segments
The timestamps in the media segment determine where the [=coded frames=] are placed in the presentation. Media segments can be appended in any order.
sequence
Media segments will be treated as adjacent in time independent of the timestamps in the media segment. Coded frames in a new media segment will be placed immediately after the coded frames in the previous media segment. The {{SourceBuffer/timestampOffset}} attribute will be updated if a new offset is needed to make the new media segments adjacent to the previous media segment. Setting the {{SourceBuffer/timestampOffset}} attribute in {{AppendMode/""sequence""}} mode allows a media segment to be placed at a specific position in the timeline without any knowledge of the timestamps in the media segment.
[Exposed=(Window,DedicatedWorker)]
interface SourceBuffer : EventTarget {
                    attribute AppendMode          mode;
    readonly        attribute boolean             updating;
    readonly        attribute TimeRanges          buffered;
                    attribute double              timestampOffset;
    readonly        attribute AudioTrackList      audioTracks;
    readonly        attribute VideoTrackList      videoTracks;
    readonly        attribute TextTrackList       textTracks;
                    attribute double              appendWindowStart;
                    attribute unrestricted double appendWindowEnd;
                    attribute EventHandler        onupdatestart;
                    attribute EventHandler        onupdate;
                    attribute EventHandler        onupdateend;
                    attribute EventHandler        onerror;
                    attribute EventHandler        onabort;
    undefined appendBuffer (BufferSource data);
    undefined abort ();
    undefined changeType (DOMString type);
    undefined remove (double start, unrestricted double end);
};
[[HTML]] {{AudioTrackList}}, {{VideoTrackList}} and {{TextTrackList}} need Window+DedicatedWorker exposure.

Attributes

mode of type AppendMode

Controls how a sequence of [=media segments=] are handled. This attribute is initially set by {{MediaSource/addSourceBuffer()}} after the object is created, and can be updated by {{SourceBuffer/changeType()}} or setting this attribute.

On getting, Return the initial value or the last value that was successfully set.

On setting, run the following steps:

  1. If this object has been removed from the {{MediaSource/sourceBuffers}} attribute of the [=parent media source=], then throw an {{InvalidStateError}} exception and abort these steps.
  2. If the {{SourceBuffer/updating}} attribute equals true, then throw an {{InvalidStateError}} exception and abort these steps.
  3. Let |new mode:AppendMode| equal the new value being assigned to this attribute.
  4. If {{SourceBuffer/[[generate timestamps flag]]}} equals true and |new mode| equals {{AppendMode/""segments""}}, then throw a {{TypeError}} exception and abort these steps.
  5. If the {{MediaSource/readyState}} attribute of the [=parent media source=] is in the {{ReadyState/""ended""}} state then run the following steps:

    1. Set the {{MediaSource/readyState}} attribute of the [=parent media source=] to {{ReadyState/""open""}}
    2. [=Queue a task=] to [=fire an event=] named {{sourceopen}} at the [=parent media source=].
  6. If the {{SourceBuffer/[[append state]]}} equals [=PARSING_MEDIA_SEGMENT=], then throw an {{InvalidStateError}} and abort these steps.
  7. If the |new mode| equals {{AppendMode/""sequence""}}, then set the {{SourceBuffer/[[group start timestamp]]}} to the {{SourceBuffer/[[group end timestamp]]}}.
  8. Update the attribute to |new mode|.
updating of type {{boolean}}, readonly

Indicates whether the asynchronous continuation of an {{SourceBuffer/appendBuffer()}} or {{SourceBuffer/remove()}} operation is still being processed. This attribute is initially set to false when the object is created.

buffered of type {{TimeRanges}}, readonly

Indicates what {{TimeRanges}} are buffered in the SourceBuffer. This attribute is initially set to an empty {{TimeRanges}} object when the object is created.

When the attribute is read the following steps MUST occur:

  1. If this object has been removed from the {{MediaSource/sourceBuffers}} attribute of the [=parent media source=] then throw an {{InvalidStateError}} exception and abort these steps.
  2. Let |highest end time:double| be the largest [=track buffer ranges=] end time across all the [=track buffers=] managed by this SourceBuffer object.
  3. Let |intersection ranges:normalized TimeRanges| equal a {{TimeRanges}} object containing a single range from 0 to |highest end time|.
  4. For each audio and video [=track buffer=] managed by this SourceBuffer, run the following steps:

    Text [=track buffers=] are included in the calculation of |highest end time|, above, but excluded from the buffered range calculation here. They are not necessarily continuous, nor should any discontinuity within them trigger playback stall when the other media tracks are continuous over the same time range.

    1. Let |track ranges:normalized TimeRanges| equal the [=track buffer ranges=] for the current [=track buffer=].
    2. If {{MediaSource/readyState}} is {{ReadyState/""ended""}}, then set the end time on the last range in |track ranges| to |highest end time|.
    3. Let |new intersection ranges:normalized TimeRanges| equal the intersection between the |intersection ranges| and the |track ranges|.
    4. Replace the ranges in |intersection ranges| with the |new intersection ranges|.
  5. If |intersection ranges| does not contain the exact same range information as the current value of this attribute, then update the current value of this attribute to |intersection ranges|.
  6. Return the current value of this attribute.
timestampOffset of type {{double}}

Controls the offset applied to timestamps inside subsequent [=media segments=] that are appended to this SourceBuffer. The {{SourceBuffer/timestampOffset}} is initially set to 0 which indicates that no offset is being applied.

On getting, Return the initial value or the last value that was successfully set.

On setting, run the following steps:

  1. Let |new timestamp offset:double| equal the new value being assigned to this attribute.
  2. If this object has been removed from the {{MediaSource/sourceBuffers}} attribute of the [=parent media source=], then throw an {{InvalidStateError}} exception and abort these steps.
  3. If the {{SourceBuffer/updating}} attribute equals true, then throw an {{InvalidStateError}} exception and abort these steps.
  4. If the {{MediaSource/readyState}} attribute of the [=parent media source=] is in the {{ReadyState/""ended""}} state then run the following steps:

    1. Set the {{MediaSource/readyState}} attribute of the [=parent media source=] to {{ReadyState/""open""}}
    2. [=Queue a task=] to [=fire an event=] named {{sourceopen}} at the [=parent media source=].
  5. If the {{SourceBuffer/[[append state]]}} equals [=PARSING_MEDIA_SEGMENT=], then throw an {{InvalidStateError}} and abort these steps.
  6. If the {{SourceBuffer/mode}} attribute equals {{AppendMode/""sequence""}}, then set the {{SourceBuffer/[[group start timestamp]]}} to |new timestamp offset|.
  7. Update the attribute to |new timestamp offset|.
audioTracks of type {{AudioTrackList}}, readonly
The list of {{AudioTrack}} objects created by this object.
videoTracks of type {{VideoTrackList}}, readonly
The list of {{VideoTrack}} objects created by this object.
textTracks of type {{TextTrackList}}, readonly
The list of {{TextTrack}} objects created by this object.
appendWindowStart of type {{double}}

The [=presentation timestamp=] for the start of the [=append window=]. This attribute is initially set to the [=presentation start time=].

On getting, Return the initial value or the last value that was successfully set.

On setting, run the following steps:

  1. If this object has been removed from the {{MediaSource/sourceBuffers}} attribute of the [=parent media source=], then throw an {{InvalidStateError}} exception and abort these steps.
  2. If the {{SourceBuffer/updating}} attribute equals true, then throw an {{InvalidStateError}} exception and abort these steps.
  3. If the new value is less than 0 or greater than or equal to {{SourceBuffer/appendWindowEnd}} then throw a {{TypeError}} exception and abort these steps.
  4. Update the attribute to the new value.
appendWindowEnd of type {{unrestricted double}}

The [=presentation timestamp=] for the end of the [=append window=]. This attribute is initially set to positive Infinity.

On getting, Return the initial value or the last value that was successfully set.

On setting, run the following steps:

  1. If this object has been removed from the {{MediaSource/sourceBuffers}} attribute of the [=parent media source=], then throw an {{InvalidStateError}} exception and abort these steps.
  2. If the {{SourceBuffer/updating}} attribute equals true, then throw an {{InvalidStateError}} exception and abort these steps.
  3. If the new value equals NaN, then throw a {{TypeError}} and abort these steps.
  4. If the new value is less than or equal to {{SourceBuffer/appendWindowStart}} then throw a {{TypeError}} exception and abort these steps.
  5. Update the attribute to the new value.
onupdatestart of type {{EventHandler}}

The event handler for the {{updatestart}} event.

onupdate of type {{EventHandler}}

The event handler for the {{update}} event.

onupdateend of type {{EventHandler}}

The event handler for the {{updateend}} event.

onerror of type {{EventHandler}}

The event handler for the {{error}} event.

onabort of type {{EventHandler}}

The event handler for the {{abort}} event.

Methods

appendBuffer

Appends the segment data in an BufferSource[[!WEBIDL]] to the {{SourceBuffer}}.

  1. Run the [=prepare append=] algorithm.
  2. Add |data:BufferSource| to the end of the {{SourceBuffer/[[input buffer]]}}.
  3. Set the {{SourceBuffer/updating}} attribute to true.
  4. [=Queue a task=] to [=fire an event=] named {{updatestart}} at this SourceBuffer object.
  5. Asynchronously run the [=buffer append=] algorithm.
ParameterTypeNullableOptionalDescription
|data|{{BufferSource}}
Return type: {{undefined}}
abort

Aborts the current segment and resets the segment parser.

  1. If this object has been removed from the {{MediaSource/sourceBuffers}} attribute of the [=parent media source=] then throw an {{InvalidStateError}} exception and abort these steps.
  2. If the {{MediaSource/readyState}} attribute of the [=parent media source=] is not in the {{ReadyState/""open""}} state then throw an {{InvalidStateError}} exception and abort these steps.
  3. If the [=range removal=] algorithm is running, then throw an {{InvalidStateError}} exception and abort these steps.
  4. If the {{SourceBuffer/updating}} attribute equals true, then run the following steps:
    1. Abort the [=buffer append=] algorithm if it is running.
    2. Set the {{SourceBuffer/updating}} attribute to false.
    3. [=Queue a task=] to [=fire an event=] named {{abort}} at this SourceBuffer object.
    4. [=Queue a task=] to [=fire an event=] named {{updateend}} at this SourceBuffer object.
  5. Run the [=reset parser state=] algorithm.
  6. Set {{SourceBuffer/appendWindowStart}} to the [=presentation start time=].
  7. Set {{SourceBuffer/appendWindowEnd}} to positive Infinity.
No parameters.
Return type: {{undefined}}
changeType

Changes the MIME type associated with this object. Subsequent {{SourceBuffer/appendBuffer()}} calls will expect the newly appended bytes to conform to the new type.

  1. If |type:DOMString| is an empty string then throw a {{TypeError}} exception and abort these steps.
  2. If this object has been removed from the {{MediaSource/sourceBuffers}} attribute of the [=parent media source=], then throw an {{InvalidStateError}} exception and abort these steps.
  3. If the {{SourceBuffer/updating}} attribute equals true, then throw an {{InvalidStateError}} exception and abort these steps.
  4. If |type| contains a MIME type that is not supported or contains a MIME type that is not supported with the types specified (currently or previously) of {{SourceBuffer}} objects in the {{MediaSource/sourceBuffers}} attribute of the [=parent media source=], then throw a {{NotSupportedError}} exception and abort these steps.
  5. If the {{MediaSource/readyState}} attribute of the [=parent media source=] is in the {{ReadyState/""ended""}} state then run the following steps:

    1. Set the {{MediaSource/readyState}} attribute of the [=parent media source=] to {{ReadyState/""open""}}.
    2. [=Queue a task=] to [=fire an event=] named {{sourceopen}} at the [=parent media source=].
  6. Run the [=reset parser state=] algorithm.
  7. Update the {{SourceBuffer/[[generate timestamps flag]]}} on this {{SourceBuffer}} object to the value in the "Generate Timestamps Flag" column of the byte stream format registry [[MSE-REGISTRY]] entry that is associated with |type|.
  8. If the {{SourceBuffer/[[generate timestamps flag]]}} equals true:
    Set the {{SourceBuffer/mode}} attribute on this {{SourceBuffer}} object to {{AppendMode/""sequence""}}, including running the associated steps for that attribute being set.
    Otherwise:
    Keep the previous value of the {{SourceBuffer/mode}} attribute on this {{SourceBuffer}} object, without running any associated steps for that attribute being set.
  9. Set the {{SourceBuffer/[[pending initialization segment for changeType flag]]}} on this {{SourceBuffer}} object to true.
Parameter Type Nullable Optional Description
|type| {{DOMString}}
Return type: {{undefined}}
remove

Removes media for a specific time range.

  1. If this object has been removed from the {{MediaSource/sourceBuffers}} attribute of the [=parent media source=] then throw an {{InvalidStateError}} exception and abort these steps.
  2. If the {{SourceBuffer/updating}} attribute equals true, then throw an {{InvalidStateError}} exception and abort these steps.
  3. If {{MediaSource/duration}} equals NaN, then throw a {{TypeError}} exception and abort these steps.
  4. If |start:double| is negative or greater than {{MediaSource/duration}}, then throw a {{TypeError}} exception and abort these steps.
  5. If |end:unrestricted double| is less than or equal to |start| or |end| equals NaN, then throw a {{TypeError}} exception and abort these steps.
  6. If the {{MediaSource/readyState}} attribute of the [=parent media source=] is in the {{ReadyState/""ended""}} state then run the following steps:

    1. Set the {{MediaSource/readyState}} attribute of the [=parent media source=] to {{ReadyState/""open""}}
    2. [=Queue a task=] to [=fire an event=] named {{sourceopen}} at the [=parent media source=].
  7. Run the [=range removal=] algorithm with |start| and |end| as the start and end of the removal range.
Parameter Type Nullable Optional Description
|start| {{double}} The start of the removal range, in seconds measured from [=presentation start time=].
|end| {{unrestricted double}} The end of the removal range, in seconds measured from [=presentation start time=].
Return type: {{undefined}}

Track Buffers

A track buffer stores the [=track descriptions=] and [=coded frames=] for an individual track. The track buffer is updated as [=initialization segments=] and [=media segments=] are appended to the SourceBuffer.

Each [=track buffer=] has a last decode timestamp variable that stores the decode timestamp of the last [=coded frame=] appended in the current [=coded frame group=]. The variable is initially unset to indicate that no [=coded frames=] have been appended yet.

Each [=track buffer=] has a last frame duration variable that stores the [=coded frame duration=] of the last [=coded frame=] appended in the current [=coded frame group=]. The variable is initially unset to indicate that no [=coded frames=] have been appended yet.

Each [=track buffer=] has a highest end timestamp variable that stores the highest [=coded frame end timestamp=] across all [=coded frames=] in the current [=coded frame group=] that were appended to this track buffer. The variable is initially unset to indicate that no [=coded frames=] have been appended yet.

Each [=track buffer=] has a need random access point flag variable that keeps track of whether the track buffer is waiting for a [=random access point=] [=coded frame=]. The variable is initially set to true to indicate that [=random access point=] [=coded frame=] is needed before anything can be added to the [=track buffer=].

Each [=track buffer=] has a track buffer ranges variable that represents the presentation time ranges occupied by the [=coded frames=] currently stored in the track buffer.

For track buffer ranges, these presentation time ranges are based on [=presentation timestamps=], frame durations, and potentially coded frame group start times for coded frame groups across track buffers in a muxed SourceBuffer.

For specification purposes, this information is treated as if it were stored in a . Intersected [=track buffer ranges=] are used to report {{HTMLMediaElement}}.{{HTMLMediaElement/buffered}}, and MUST therefore support uninterrupted playback within each range of {{HTMLMediaElement}}.{{HTMLMediaElement/buffered}}.

These coded frame group start times differ slightly from those mentioned in the [=coded frame processing=] algorithm in that they are the earliest [=presentation timestamp=] across all track buffers following a discontinuity. Discontinuities can occur within the [=coded frame processing=] algorithm or result from the [=coded frame removal=] algorithm, regardless of {{SourceBuffer/mode}}. The threshold for determining disjointness of [=track buffer ranges=] is implementation-specific. For example, to reduce unexpected playback stalls, implementations MAY approximate the [=coded frame processing=] algorithm's discontinuity detection logic by coalescing adjacent ranges separated by a gap smaller than 2 times the maximum frame duration buffered so far in this [=track buffer=]. Implementations MAY also use coded frame group start times as range start times across [=track buffers=] in a muxed SourceBuffer to further reduce unexpected playback stalls.

Event Summary

Event name Interface Dispatched when...
updatestart {{Event}} {{SourceBuffer}}'s {{SourceBuffer/updating}} transitions from false to true.
update {{Event}} A {{SourceBuffer}}'s append or remove successfully completed. {{SourceBuffer}}'s {{SourceBuffer/updating}} transitions from true to false.
updateend {{Event}} The append or remove of a {{SourceBuffer}} ended.
error {{Event}} An error occurred during the append to a {{SourceBuffer}}. {{SourceBuffer/updating}} transitions from true to false.
abort {{Event}} The {{SourceBuffer}}'s append was aborted by an {{SourceBuffer/abort()}} call. {{SourceBuffer/updating}} transitions from true to false.

Algorithms

Segment Parser Loop

Each {{SourceBuffer}} object has an [[\append state]] internal slot that keeps track of the high-level segment parsing state. It is initially set to [=WAITING_FOR_SEGMENT=] and can transition to the following states as data is appended.

Append state name Description
WAITING_FOR_SEGMENT Waiting for the start of an [=initialization segment=] or [=media segment=] to be appended.
PARSING_INIT_SEGMENT Currently parsing an [=initialization segment=].
PARSING_MEDIA_SEGMENT Currently parsing a [=media segment=].

Each {{SourceBuffer}} object has an [[\input buffer]] internal slot that is a byte buffer that holds unparsed bytes across {{SourceBuffer/appendBuffer()}} calls. The buffer is empty when the {{SourceBuffer}} object is created.

Each {{SourceBuffer}} object has a [[\buffer full flag]] internal slot that keeps track of whether {{SourceBuffer/appendBuffer()}} is allowed to accept more bytes. It is set to false when the {{SourceBuffer}} object is created and gets updated as data is appended and removed.

Each {{SourceBuffer}} object has a [[\group start timestamp]] internal slot that keeps track of the starting timestamp for a new [=coded frame group=] in the {{AppendMode/""sequence""}} mode. It is unset when the SourceBuffer object is created and gets updated when the {{SourceBuffer/mode}} attribute equals {{AppendMode/""sequence""}} and the {{SourceBuffer/timestampOffset}} attribute is set, or the [=coded frame processing=] algorithm runs.

Each {{SourceBuffer}} object has a [[\group end timestamp]] internal slot that stores the highest [=coded frame end timestamp=] across all [=coded frames=] in the current [=coded frame group=]. It is set to 0 when the SourceBuffer object is created and gets updated by the [=coded frame processing=] algorithm.

The {{SourceBuffer/[[group end timestamp]]}} stores the highest [=coded frame end timestamp=] across all [=track buffers=] in a SourceBuffer. Therefore, care should be taken in setting the {{SourceBuffer/mode}} attribute when appending multiplexed segments in which the timestamps are not aligned across tracks.

Each {{SourceBuffer}} object has a [[\generate timestamps flag]] internal slot that is a boolean that keeps track of whether timestamps need to be generated for the [=coded frames=] passed to the [=coded frame processing=] algorithm. This flag is set by {{MediaSource/addSourceBuffer()}} when the {{SourceBuffer}} object is created and is updated by {{SourceBuffer/changeType()}}.

When the segment parser loop algorithm is invoked, run the following steps:

  1. Loop Top: If the {{SourceBuffer/[[input buffer]]}} is empty, then jump to the need more data step below.
  2. If the {{SourceBuffer/[[input buffer]]}} contains bytes that violate the [=SourceBuffer byte stream format specification=], then run the [=append error=] algorithm and abort this algorithm.
  3. Remove any bytes that the [=byte stream format specifications=] say MUST be ignored from the start of the {{SourceBuffer/[[input buffer]]}}.
  4. If the {{SourceBuffer/[[append state]]}} equals [=WAITING_FOR_SEGMENT=], then run the following steps:

    1. If the beginning of the {{SourceBuffer/[[input buffer]]}} indicates the start of an [=initialization segment=], set the {{SourceBuffer/[[append state]]}} to [=PARSING_INIT_SEGMENT=].
    2. If the beginning of the {{SourceBuffer/[[input buffer]]}} indicates the start of a [=media segment=], set {{SourceBuffer/[[append state]]}} to [=PARSING_MEDIA_SEGMENT=].
    3. Jump to the loop top step above.
  5. If the {{SourceBuffer/[[append state]]}} equals [=PARSING_INIT_SEGMENT=], then run the following steps:

    1. If the {{SourceBuffer/[[input buffer]]}} does not contain a complete [=initialization segment=] yet, then jump to the need more data step below.
    2. Run the [=initialization segment received=] algorithm.
    3. Remove the [=initialization segment=] bytes from the beginning of the {{SourceBuffer/[[input buffer]]}}.
    4. Set {{SourceBuffer/[[append state]]}} to [=WAITING_FOR_SEGMENT=].
    5. Jump to the loop top step above.
  6. If the {{SourceBuffer/[[append state]]}} equals [=PARSING_MEDIA_SEGMENT=], then run the following steps:

    1. If the {{SourceBuffer/[[first initialization segment received flag]]}} is false or the {{SourceBuffer/[[pending initialization segment for changeType flag]]}} is true, then run the [=append error=] algorithm and abort this algorithm.
    2. If the {{SourceBuffer/[[input buffer]]}} contains one or more complete [=coded frames=], then run the [=coded frame processing=] algorithm.

      The frequency at which the coded frame processing algorithm is run is implementation-specific. The coded frame processing algorithm MAY be called when the input buffer contains the complete media segment or it MAY be called multiple times as complete coded frames are added to the input buffer.

    3. If this {{SourceBuffer}} is full and cannot accept more media data, then set the {{SourceBuffer/[[buffer full flag]]}} to true.
    4. If the {{SourceBuffer/[[input buffer]]}} does not contain a complete [=media segment=], then jump to the need more data step below.
    5. Remove the [=media segment=] bytes from the beginning of the {{SourceBuffer/[[input buffer]]}}.
    6. Set {{SourceBuffer/[[append state]]}} to [=WAITING_FOR_SEGMENT=].
    7. Jump to the loop top step above.
  7. Need more data: Return control to the calling algorithm.

Reset Parser State

When the parser state needs to be reset, run the following steps:

  1. If the {{SourceBuffer/[[append state]]}} equals [=PARSING_MEDIA_SEGMENT=] and the {{SourceBuffer/[[input buffer]]}} contains some complete [=coded frames=], then run the [=coded frame processing=] algorithm until all of these complete [=coded frames=] have been processed.
  2. Unset the [=last decode timestamp=] on all [=track buffers=].
  3. Unset the [=last frame duration=] on all [=track buffers=].
  4. Unset the [=highest end timestamp=] on all [=track buffers=].
  5. Set the [=need random access point flag=] on all [=track buffers=] to true.
  6. If the {{SourceBuffer/mode}} attribute equals {{AppendMode/""sequence""}}, then set the {{SourceBuffer/[[group start timestamp]]}} to the {{SourceBuffer/[[group end timestamp]]}}
  7. Remove all bytes from the {{SourceBuffer/[[input buffer]]}}.
  8. Set {{SourceBuffer/[[append state]]}} to [=WAITING_FOR_SEGMENT=].

Append Error

This algorithm is called when an error occurs during an append.

  1. Run the [=reset parser state=] algorithm.
  2. Set the {{SourceBuffer/updating}} attribute to false.
  3. [=Queue a task=] to [=fire an event=] named {{error}} at this SourceBuffer object.
  4. [=Queue a task=] to [=fire an event=] named {{updateend}} at this SourceBuffer object.
  5. Run the [=end of stream=] algorithm with the |error:EndOfStreamError| parameter set to {{EndOfStreamError/""decode""}}.

Prepare Append

When an append operation begins, the following steps are run to validate and prepare the SourceBuffer.

  1. If the SourceBuffer has been removed from the {{MediaSource/sourceBuffers}} attribute of the [=parent media source=] then throw an {{InvalidStateError}} exception and abort these steps.
  2. If the {{SourceBuffer/updating}} attribute equals true, then throw an {{InvalidStateError}} exception and abort these steps.
  3. Let |recent element error:boolean| be determined as follows:
    If the {{MediaSource}} was constructed in a {{Window}}
    Let |recent element error| be true if the {{HTMLMediaElement}}.{{HTMLMediaElement/error}} attribute is not null. If that attribute is null, then let |recent element error| be false.
    Otherwise
    Let |recent element error| be the value resulting from the steps for the {{Window}} case, but run on the {{Window}} {{HTMLMediaElement}} on any change to its {{HTMLMediaElement/error}} attribute and communicated by using {{HTMLMediaElement/[[port to worker]]}} implicit messages. If such a message has not yet been received, then let |recent element error| be false.
  4. If |recent element error| is true, then throw an {{InvalidStateError}} exception and abort these steps.
  5. If the {{MediaSource/readyState}} attribute of the [=parent media source=] is in the {{ReadyState/""ended""}} state then run the following steps:

    1. Set the {{MediaSource/readyState}} attribute of the [=parent media source=] to {{ReadyState/""open""}}
    2. [=Queue a task=] to [=fire an event=] named {{sourceopen}} at the [=parent media source=].
  6. Run the [=coded frame eviction=] algorithm.
  7. If the {{SourceBuffer/[[buffer full flag]]}} equals true, then throw a {{QuotaExceededError}} exception and abort these steps.

    This is the signal that the implementation was unable to evict enough data to accommodate the append or the append is too big. The web application SHOULD use {{SourceBuffer/remove()}} to explicitly free up space and/or reduce the size of the append.

Buffer Append

When {{SourceBuffer/appendBuffer()}} is called, the following steps are run to process the appended data.

  1. Run the [=segment parser loop=] algorithm.
  2. If the [=segment parser loop=] algorithm in the previous step was aborted, then abort this algorithm.
  3. Set the {{SourceBuffer/updating}} attribute to false.
  4. [=Queue a task=] to [=fire an event=] named {{update}} at this SourceBuffer object.
  5. [=Queue a task=] to [=fire an event=] named {{updateend}} at this SourceBuffer object.

Range Removal

Follow these steps when a caller needs to initiate a JavaScript visible range removal operation that blocks other SourceBuffer updates:

  1. Let |start:double| equal the starting [=presentation timestamp=] for the removal range, in seconds measured from [=presentation start time=].
  2. Let |end:unrestricted double| equal the end [=presentation timestamp=] for the removal range, in seconds measured from [=presentation start time=].
  3. Set the {{SourceBuffer/updating}} attribute to true.
  4. [=Queue a task=] to [=fire an event=] named {{updatestart}} at this SourceBuffer object.
  5. Return control to the caller and run the rest of the steps asynchronously.
  6. Run the [=coded frame removal=] algorithm with |start| and |end| as the start and end of the removal range.
  7. Set the {{SourceBuffer/updating}} attribute to false.
  8. [=Queue a task=] to [=fire an event=] named {{update}} at this SourceBuffer object.
  9. [=Queue a task=] to [=fire an event=] named {{updateend}} at this SourceBuffer object.

Initialization Segment Received

The following steps are run when the [=segment parser loop=] successfully parses a complete [=initialization segment=]:

Each SourceBuffer object has a [[\first initialization segment received flag]] internal slot that tracks whether the first [=initialization segment=] has been appended and received by this algorithm. This flag is set to false when the SourceBuffer is created and updated by the algorithm below.

Each SourceBuffer object has a [[\pending initialization segment for changeType flag]] internal slot that tracks whether an [=initialization segment=] is needed since the most recent {{SourceBuffer/changeType()}}. This flag is set to false when the SourceBuffer is created, set to true by {{SourceBuffer/changeType()}} and reset to false by the algorithm below.

  1. Update the {{MediaSource/duration}} attribute if it currently equals NaN:
    If the initialization segment contains a duration:
    Run the [=duration change=] algorithm with |new duration:unrestricted double| set to the duration in the initialization segment.
    Otherwise:
    Run the [=duration change=] algorithm with |new duration| set to positive Infinity.
  2. If the [=initialization segment=] has no audio, video, or text tracks, then run the [=append error=] algorithm and abort these steps.
  3. If the {{SourceBuffer/[[first initialization segment received flag]]}} is true, then run the following steps:
    1. Verify the following properties. If any of the checks fail then run the [=append error=] algorithm and abort these steps.
      • The number of audio, video, and text tracks match what was in the first [=initialization segment=].
      • If more than one track for a single type are present (e.g., 2 audio tracks), then the [=Track IDs=] match the ones in the first [=initialization segment=].
      • The codecs for each track are supported by the user agent.

        User agents MAY consider codecs, that would otherwise be supported, as "not supported" here if the codecs were not specified in |type:DOMString| parameter passed to (a) the most recently successful {{SourceBuffer/changeType()}} on this {{SourceBuffer}} object, or (b) if no successful {{SourceBuffer/changeType()}} has yet occurred on this object, the {{MediaSource/addSourceBuffer()}} that created this {{SourceBuffer}} object. For example, if the most recently successful {{SourceBuffer/changeType()}} was called with 'video/webm' or 'video/webm; codecs="vp8"', and a video track containing vp9 appears in the initialization segment, then the user agent MAY use this step to trigger a decode error even if the other two properties' checks, above, pass. Implementations are encouraged to trigger error in such cases only when the codec is indeed not supported or the other two properties' checks fail. Web authors are encouraged to use {{SourceBuffer/changeType()}}, {{MediaSource/addSourceBuffer()}} and {{MediaSource/isTypeSupported()}} with precise codec parameters to more proactively detect user agent support. {{SourceBuffer/changeType()}} is required if the {{SourceBuffer}} object's bytestream format is changing.

    2. Add the appropriate [=track descriptions=] from this [=initialization segment=] to each of the [=track buffers=].
    3. Set the [=need random access point flag=] on all track buffers to true.
  4. Let |active track flag:boolean| equal false.
  5. If the {{SourceBuffer/[[first initialization segment received flag]]}} is false, then run the following steps:

    1. If the [=initialization segment=] contains tracks with codecs the user agent does not support, then run the [=append error=] algorithm and abort these steps.

      User agents MAY consider codecs, that would otherwise be supported, as "not supported" here if the codecs were not specified in |type:DOMString| parameter passed to (a) the most recently successful {{SourceBuffer/changeType()}} on this {{SourceBuffer}} object, or (b) if no successful {{SourceBuffer/changeType()}} has yet occurred on this object, the {{MediaSource/addSourceBuffer()}} that created this {{SourceBuffer}} object. For example, MediaSource.isTypeSupported('video/webm;codecs="vp8,vorbis"') may return true, but if {{MediaSource/addSourceBuffer()}} was called with 'video/webm;codecs="vp8"' and a Vorbis track appears in the [=initialization segment=], then the user agent MAY use this step to trigger a decode error. Implementations are encouraged to trigger error in such cases only when the codec is indeed not supported. Web authors are encouraged to use {{SourceBuffer/changeType()}}, {{MediaSource/addSourceBuffer()}} and {{MediaSource/isTypeSupported()}} with precise codec parameters to more proactively detect user agent support. {{SourceBuffer/changeType()}} is required if the {{SourceBuffer}} object's bytestream format is changing.

    2. For each audio track in the [=initialization segment=], run following steps:

      1. Let |audio byte stream track ID| be the [=Track ID=] for the current track being processed.
      2. Let |audio language:DOMString| be a BCP 47 language tag for the language specified in the [=initialization segment=] for this track or an empty string if no language info is present.
      3. If |audio language| equals the 'und' BCP 47 value, then assign an empty string to |audio language|.
      4. Let |audio label:DOMString| be a label specified in the [=initialization segment=] for this track or an empty string if no label info is present.
      5. Let |audio kinds:DOMString sequence| be a sequence of kind strings specified in the [=initialization segment=] for this track or a sequence with a single empty string element in it if no kind information is provided.
      6. For each value in |audio kinds|, run the following steps:
        1. Let |current audio kind:DOMString| equal the value from |audio kinds| for this iteration of the loop.
        2. Let |new audio track:AudioTrack| be a new {{AudioTrack}} object.
        3. Generate a unique ID and assign it to the {{AudioTrack/id}} property on |new audio track|.
        4. Assign |audio language| to the {{AudioTrack/language}} property on |new audio track|.
        5. Assign |audio label| to the {{AudioTrack/label}} property on |new audio track|.
        6. Assign |current audio kind| to the {{AudioTrack/kind}} property on |new audio track|.
        7. If this {{SourceBuffer}} object's {{SourceBuffer/audioTracks}}.{{AudioTrackList/length}} equals 0, then run the following steps:

          1. Set the {{AudioTrack/enabled}} property on |new audio track| to true.
          2. Set |active track flag| to true.
        8. Add |new audio track| to the {{SourceBuffer/audioTracks}} attribute on this SourceBuffer object.

          This should trigger {{AudioTrackList}} [[HTML]] logic to [=queue a task=] to [=fire an event=] named [=AudioTrackList/addtrack=] using {{TrackEvent}} with the {{TrackEvent/track}} attribute initialized to |new audio track|, at the {{AudioTrackList}} object referenced by the {{SourceBuffer/audioTracks}} attribute on this SourceBuffer object.

        9. If the [=parent media source=] was constructed in a {{DedicatedWorkerGlobalScope}}:
          Post an internal create track mirror message to {{MediaSource/[[port to main]]}} whose implicit handler in {{Window}} runs the following steps:
          1. Let |mirrored audio track:AudioTrack| be a new {{AudioTrack}} object.
          2. Assign the same property values to |mirrored audio track| as were determined for |new audio track|.
          3. Add |mirrored audio track| to the {{HTMLMediaElement/audioTracks}} attribute on the HTMLMediaElement.
          Otherwise:
          Add |new audio track| to the {{HTMLMediaElement/audioTracks}} attribute on the HTMLMediaElement.

          This should trigger {{AudioTrackList}} [[HTML]] logic to [=queue a task=] to [=fire an event=] named [=AudioTrackList/addtrack=] using {{TrackEvent}} with the {{TrackEvent/track}} attribute initialized to |mirrored audio track| or |new audio track|, at the {{AudioTrackList}} object referenced by the {{HTMLMediaElement/audioTracks}} attribute on the HTMLMediaElement.

      7. Create a new [=track buffer=] to store [=coded frames=] for this track.
      8. Add the [=track description=] for this track to the [=track buffer=].
    3. For each video track in the [=initialization segment=], run following steps:

      1. Let |video byte stream track ID| be the [=Track ID=] for the current track being processed.
      2. Let |video language:DOMString| be a BCP 47 language tag for the language specified in the [=initialization segment=] for this track or an empty string if no language info is present.
      3. If |video language| equals the 'und' BCP 47 value, then assign an empty string to |video language|.
      4. Let |video label:DOMString| be a label specified in the [=initialization segment=] for this track or an empty string if no label info is present.
      5. Let |video kinds:DOMString sequence| be a sequence of kind strings specified in the [=initialization segment=] for this track or a sequence with a single empty string element in it if no kind information is provided.
      6. For each value in |video kinds|, run the following steps:
        1. Let |current video kind:DOMString| equal the value from |video kinds| for this iteration of the loop.
        2. Let |new video track:VideoTrack| be a new {{VideoTrack}} object.
        3. Generate a unique ID and assign it to the {{VideoTrack/id}} property on |new video track|.
        4. Assign |video language| to the {{VideoTrack/language}} property on |new video track|.
        5. Assign |video label| to the {{VideoTrack/label}} property on |new video track|.
        6. Assign |current video kind| to the {{VideoTrack/kind}} property on |new video track|.
        7. If this {{SourceBuffer}} object's {{SourceBuffer/videoTracks}}.{{VideoTrackList/length}} equals 0, then run the following steps:

          1. Set the {{VideoTrack/selected}} property on |new video track| to true.
          2. Set |active track flag| to true.
        8. Add |new video track| to the {{SourceBuffer/videoTracks}} attribute on this SourceBuffer object.

          This should trigger {{VideoTrackList}} [[HTML]] logic to [=queue a task=] to [=fire an event=] named [=VideoTrackList/addtrack=] using {{TrackEvent}} with the {{TrackEvent/track}} attribute initialized to |new video track|, at the {{VideoTrackList}} object referenced by the {{SourceBuffer/videoTracks}} attribute on this SourceBuffer object.

        9. If the [=parent media source=] was constructed in a {{DedicatedWorkerGlobalScope}}:
          Post an internal create track mirror message to {{MediaSource/[[port to main]]}} whose implicit handler in {{Window}} runs the following steps:
          1. Let |mirrored video track:VideoTrack| be a new {{VideoTrack}} object.
          2. Assign the same property values to |mirrored video track| as were determined for |new video track|.
          3. Add |mirrored video track| to the {{HTMLMediaElement/videoTracks}} attribute on the HTMLMediaElement.
          Otherwise:
          Add |new video track| to the {{HTMLMediaElement/videoTracks}} attribute on the HTMLMediaElement.

          This should trigger {{VideoTrackList}} [[HTML]] logic to [=queue a task=] to [=fire an event=] named [=VideoTrackList/addtrack=] using {{TrackEvent}} with the {{TrackEvent/track}} attribute initialized to |mirrored video track| or |new video track|, at the {{VideoTrackList}} object referenced by the {{HTMLMediaElement/videoTracks}} attribute on the HTMLMediaElement.

      7. Create a new [=track buffer=] to store [=coded frames=] for this track.
      8. Add the [=track description=] for this track to the [=track buffer=].
    4. For each text track in the [=initialization segment=], run following steps:

      1. Let |text byte stream track ID| be the [=Track ID=] for the current track being processed.
      2. Let |text language:DOMString| be a BCP 47 language tag for the language specified in the [=initialization segment=] for this track or an empty string if no language info is present.
      3. If |text language| equals the 'und' BCP 47 value, then assign an empty string to |text language|.
      4. Let |text label:DOMString| be a label specified in the [=initialization segment=] for this track or an empty string if no label info is present.
      5. Let |text kinds:DOMString sequence| be a sequence of kind strings specified in the [=initialization segment=] for this track or a sequence with a single empty string element in it if no kind information is provided.
      6. For each value in |text kinds|, run the following steps:
        1. Let |current text kind:DOMString| equal the value from |text kinds| for this iteration of the loop.
        2. Let |new text track:TextTrack| be a new {{TextTrack}} object.
        3. Generate a unique ID and assign it to the {{TextTrack/id}} property on |new text track|.
        4. Assign |text language| to the {{TextTrack/language}} property on |new text track|.
        5. Assign |text label| to the {{TextTrack/label}} property on |new text track|.
        6. Assign |current text kind| to the {{TextTrack/kind}} property on |new text track|.
        7. Populate the remaining properties on |new text track| with the appropriate information from the [=initialization segment=].
        8. If the {{TextTrack/mode}} property on |new text track| equals or , then set |active track flag| to true.
        9. Add |new text track| to the {{SourceBuffer/textTracks}} attribute on this SourceBuffer object.

          This should trigger {{TextTrackList}} [[HTML]] logic to [=queue a task=] to [=fire an event=] named [=TextTrackList/addtrack=] using {{TrackEvent}} with the {{TrackEvent/track}} attribute initialized to |new text track|, at the {{TextTrackList}} object referenced by the {{SourceBuffer/textTracks}} attribute on this SourceBuffer object.

        10. If the [=parent media source=] was constructed in a {{DedicatedWorkerGlobalScope}}:
          Post an internal create track mirror message to {{MediaSource/[[port to main]]}} whose implicit handler in {{Window}} runs the following steps:
          1. Let |mirrored text track:TextTrack| be a new {{TextTrack}} object.
          2. Assign the same property values to |mirrored text track| as were determined for |new text track|.
          3. Add |mirrored text track| to the {{HTMLMediaElement/textTracks}} attribute on the HTMLMediaElement.
          Otherwise:
          Add |new text track| to the {{HTMLMediaElement/textTracks}} attribute on the HTMLMediaElement.

          This should trigger {{TextTrackList}} [[HTML]] logic to [=queue a task=] to [=fire an event=] named [=TextTrackList/addtrack=] using {{TrackEvent}} with the {{TrackEvent/track}} attribute initialized to |mirrored text track| or |new text track|, at the {{TextTrackList}} object referenced by the {{HTMLMediaElement/textTracks}} attribute on the HTMLMediaElement.

      7. Create a new [=track buffer=] to store [=coded frames=] for this track.
      8. Add the [=track description=] for this track to the [=track buffer=].
    5. If |active track flag| equals true, then run the following steps:
      1. Add this SourceBuffer to {{MediaSource/activeSourceBuffers}}.
      2. [=Queue a task=] to [=fire an event=] named {{addsourcebuffer}} at {{MediaSource/activeSourceBuffers}}
    6. Set {{SourceBuffer/[[first initialization segment received flag]]}} to true.
  6. Set {{SourceBuffer/[[pending initialization segment for changeType flag]]}} to false.
  7. If the |active track flag| equals true, then run the following steps:
  8. Use the [=parent media source=]'s [=mirror if necessary=] algorithm to run the following step in {{Window}}:
    1. If the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute is greater than {{HTMLMediaElement/HAVE_CURRENT_DATA}}, then set the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute to {{HTMLMediaElement/HAVE_METADATA}}.

      Per [[HTML]] logic, {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} changes may trigger events on the HTMLMediaElement.

  9. If each object in {{MediaSource/sourceBuffers}} of the [=parent media source=] has {{SourceBuffer/[[first initialization segment received flag]]}} equal to true, then use the [=parent media source=]'s [=mirror if necessary=] algorithm to run the following step in {{Window}}:
    1. If the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute is {{HTMLMediaElement/HAVE_NOTHING}}, then set the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute to {{HTMLMediaElement/HAVE_METADATA}}.

      Per [[HTML]] logic, {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} changes may trigger events on the HTMLMediaElement. If transition from {{HTMLMediaElement/HAVE_NOTHING}} to {{HTMLMediaElement/HAVE_METADATA}} occurs, it should trigger HTMLMediaElement logic to [=queue a task=] to [=fire an event=] named [=HTMLMediaElement/loadedmetadata=] at the media element.

Coded Frame Processing

When complete [=coded frames=] have been parsed by the [=segment parser loop=] then the following steps are run:

  1. For each [=coded frame=] in the [=media segment=] run the following steps:

    1. Loop Top:
      If {{SourceBuffer/[[generate timestamps flag]]}} equals true:
      1. Let |presentation timestamp:double| equal 0.
      2. Let |decode timestamp:double| equal 0.
      Otherwise:
      1. Let |presentation timestamp| be a double precision floating point representation of the coded frame's [=presentation timestamp=] in seconds.

        Special processing may be needed to determine the presentation and decode timestamps for timed text frames since this information may not be explicitly present in the underlying format or may be dependent on the order of the frames. Some metadata text tracks, like MPEG2-TS PSI data, may only have implied timestamps. Format specific rules for these situations SHOULD be in the [=byte stream format specifications=] or in separate extension specifications.

      2. Let |decode timestamp| be a double precision floating point representation of the coded frame's decode timestamp in seconds.

        Implementations don't have to internally store timestamps in a double precision floating point representation. This representation is used here because it is the representation for timestamps in the HTML spec. The intention here is to make the behavior clear without adding unnecessary complexity to the algorithm to deal with the fact that adding a timestampOffset may cause a timestamp rollover in the underlying timestamp representation used by the byte stream format. Implementations can use any internal timestamp representation they wish, but the addition of timestampOffset SHOULD behave in a similar manner to what would happen if a double precision floating point representation was used.

    2. Let |frame duration:double| be a double precision floating point representation of the [=coded frame duration|coded frame's duration=] in seconds.
    3. If {{SourceBuffer/mode}} equals {{AppendMode/""sequence""}} and {{SourceBuffer/[[group start timestamp]]}} is set, then run the following steps:
      1. Set {{SourceBuffer/timestampOffset}} equal to {{SourceBuffer/[[group start timestamp]]}} minus |presentation timestamp|.
      2. Set {{SourceBuffer/[[group end timestamp]]}} equal to {{SourceBuffer/[[group start timestamp]]}}.
      3. Set the [=need random access point flag=] on all [=track buffers=] to true.
      4. Unset {{SourceBuffer/[[group start timestamp]]}}.
    4. If {{SourceBuffer/timestampOffset}} is not 0, then run the following steps:

      1. Add {{SourceBuffer/timestampOffset}} to the |presentation timestamp|.
      2. Add {{SourceBuffer/timestampOffset}} to the |decode timestamp|.
    5. Let |track buffer| equal the [=track buffer=] that the coded frame will be added to.
    6. If [=last decode timestamp=] for |track buffer| is set and |decode timestamp| is less than [=last decode timestamp=]:
      OR
      If [=last decode timestamp=] for |track buffer| is set and the difference between |decode timestamp| and [=last decode timestamp=] is greater than 2 times [=last frame duration=]:
      1. If {{SourceBuffer/mode}} equals {{AppendMode/""segments""}}:
        Set {{SourceBuffer/[[group end timestamp]]}} to |presentation timestamp|.
        If {{SourceBuffer/mode}} equals {{AppendMode/""sequence""}}:
        Set {{SourceBuffer/[[group start timestamp]]}} equal to the {{SourceBuffer/[[group end timestamp]]}}.
      2. Unset the [=last decode timestamp=] on all [=track buffers=].
      3. Unset the [=last frame duration=] on all [=track buffers=].
      4. Unset the [=highest end timestamp=] on all [=track buffers=].
      5. Set the [=need random access point flag=] on all [=track buffers=] to true.
      6. Jump to the Loop Top step above to restart processing of the current [=coded frame=].
      Otherwise:
      Continue.
    7. Let |frame end timestamp:double| equal the sum of |presentation timestamp| and |frame duration|.
    8. If |presentation timestamp| is less than {{SourceBuffer/appendWindowStart}}, then set the [=need random access point flag=] to true, drop the coded frame, and jump to the top of the loop to start processing the next coded frame.

      Some implementations MAY choose to collect some of these coded frames with |presentation timestamp| less than {{SourceBuffer/appendWindowStart}} and use them to generate a splice at the first coded frame that has a [=presentation timestamp=] greater than or equal to {{SourceBuffer/appendWindowStart}} even if that frame is not a [=random access point=]. Supporting this requires multiple decoders or faster than real-time decoding so for now this behavior will not be a normative requirement.

    9. If |frame end timestamp| is greater than {{SourceBuffer/appendWindowEnd}}, then set the [=need random access point flag=] to true, drop the coded frame, and jump to the top of the loop to start processing the next coded frame.

      Some implementations MAY choose to collect coded frames with |presentation timestamp| less than {{SourceBuffer/appendWindowEnd}} and |frame end timestamp| greater than {{SourceBuffer/appendWindowEnd}} and use them to generate a splice across the portion of the collected coded frames within the append window at time of collection, and the beginning portion of later processed frames which only partially overlap the end of the collected coded frames. Supporting this requires multiple decoders or faster than real-time decoding so for now this behavior will not be a normative requirement. In conjunction with collecting coded frames that span {{SourceBuffer/appendWindowStart}}, implementations MAY thus support gapless audio splicing.

    10. If the [=need random access point flag=] on |track buffer| equals true, then run the following steps:
      1. If the coded frame is not a [=random access point=], then drop the coded frame and jump to the top of the loop to start processing the next coded frame.
      2. Set the [=need random access point flag=] on |track buffer| to false.
    11. Let |spliced audio frame| be an unset variable for holding audio splice information
    12. Let |spliced timed text frame| be an unset variable for holding timed text splice information
    13. If [=last decode timestamp=] for |track buffer| is unset and |presentation timestamp| falls within the [=presentation interval=] of a [=coded frame=] in |track buffer|, then run the following steps:
      1. Let |overlapped frame| be the [=coded frame=] in |track buffer| that matches the condition above.
      2. If |track buffer| contains audio [=coded frames=]:
        Run the [=audio splice frame=] algorithm and if a splice frame is returned, assign it to |spliced audio frame|.
        If |track buffer| contains video [=coded frames=]:
        1. Let |remove window timestamp:double| equal the |overlapped frame| [=presentation timestamp=] plus 1 microsecond.
        2. If the |presentation timestamp| is less than the |remove window timestamp|, then remove |overlapped frame| from |track buffer|.

          This is to compensate for minor errors in frame timestamp computations that can appear when converting back and forth between double precision floating point numbers and rationals. This tolerance allows a frame to replace an existing one as long as it is within 1 microsecond of the existing frame's start time. Frames that come slightly before an existing frame are handled by the removal step below.

        If |track buffer| contains timed text [=coded frames=]:
        Run the [=text splice frame=] algorithm and if a splice frame is returned, assign it to |spliced timed text frame|.
    14. Remove existing coded frames in |track buffer|:
      If [=highest end timestamp=] for |track buffer| is not set:
      Remove all [=coded frames=] from |track buffer| that have a [=presentation timestamp=] greater than or equal to |presentation timestamp| and less than |frame end timestamp|.
      If [=highest end timestamp=] for |track buffer| is set and less than or equal to |presentation timestamp|:
      Remove all [=coded frames=] from |track buffer| that have a [=presentation timestamp=] greater than or equal to [=highest end timestamp=] and less than |frame end timestamp|.
    15. Remove all possible decoding dependencies on the [=coded frames=] removed in the previous two steps by removing all [=coded frames=] from |track buffer| between those frames removed in the previous two steps and the next [=random access point=] after those removed frames.

      Removing all [=coded frames=] until the next [=random access point=] is a conservative estimate of the decoding dependencies since it assumes all frames between the removed frames and the next random access point depended on the frames that were removed.

    16. If |spliced audio frame| is set:
      Add |spliced audio frame| to the |track buffer|.
      If |spliced timed text frame| is set:
      Add |spliced timed text frame| to the |track buffer|.
      Otherwise:
      Add the [=coded frame=] with the |presentation timestamp|, |decode timestamp|, and |frame duration| to the |track buffer|.
    17. Set [=last decode timestamp=] for |track buffer| to |decode timestamp|.
    18. Set [=last frame duration=] for |track buffer| to |frame duration|.
    19. If [=highest end timestamp=] for |track buffer| is unset or |frame end timestamp| is greater than [=highest end timestamp=], then set [=highest end timestamp=] for |track buffer| to |frame end timestamp|.

      The greater than check is needed because bidirectional prediction between coded frames can cause |presentation timestamp| to not be monotonically increasing even though the decode timestamps are monotonically increasing.

    20. If |frame end timestamp| is greater than {{SourceBuffer/[[group end timestamp]]}}, then set {{SourceBuffer/[[group end timestamp]]}} equal to |frame end timestamp|.
    21. If {{SourceBuffer/[[generate timestamps flag]]}} equals true, then set {{SourceBuffer/timestampOffset}} equal to |frame end timestamp|.
  2. If the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute is {{HTMLMediaElement/HAVE_METADATA}} and the new [=coded frames=] cause {{HTMLMediaElement}}.{{HTMLMediaElement/buffered}} to have a {{TimeRanges}} for the current playback position, then set the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute to {{HTMLMediaElement/HAVE_CURRENT_DATA}}.

    Per [[HTML]] logic, {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} changes may trigger events on the HTMLMediaElement.

  3. If the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute is {{HTMLMediaElement/HAVE_CURRENT_DATA}} and the new [=coded frames=] cause {{HTMLMediaElement}}.{{HTMLMediaElement/buffered}} to have a {{TimeRanges}} that includes the current playback position and some time beyond the current playback position, then set the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute to {{HTMLMediaElement/HAVE_FUTURE_DATA}}.

    Per [[HTML]] logic, {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} changes may trigger events on the HTMLMediaElement.

  4. If the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute is {{HTMLMediaElement/HAVE_FUTURE_DATA}} and the new [=coded frames=] cause {{HTMLMediaElement}}.{{HTMLMediaElement/buffered}} to have a {{TimeRanges}} that includes the current playback position and [=enough data to ensure uninterrupted playback=], then set the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute to {{HTMLMediaElement/HAVE_ENOUGH_DATA}}.

    Per [[HTML]] logic, {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} changes may trigger events on the HTMLMediaElement.

  5. If the [=media segment=] contains data beyond the current {{MediaSource/duration}}, then run the [=duration change=] algorithm with |new duration:unrestricted double| set to the maximum of the current duration and the {{SourceBuffer/[[group end timestamp]]}}.

Coded Frame Removal

Follow these steps when [=coded frames=] for a specific time range need to be removed from the SourceBuffer:

  1. Let |start:double| be the starting [=presentation timestamp=] for the removal range.
  2. Let |end:unrestricted double| be the end [=presentation timestamp=] for the removal range.
  3. For each [=track buffer=] in this {{SourceBuffer}}, run the following steps:

    1. Let |remove end timestamp:unrestricted double| be the current value of {{MediaSource/duration}}
    2. If this [=track buffer=] has a [=random access point=] timestamp that is greater than or equal to |end|, then update |remove end timestamp| to that random access point timestamp.

      Random access point timestamps can be different across tracks because the dependencies between [=coded frames=] within a track are usually different than the dependencies in another track.

    3. Remove all media data, from this [=track buffer=], that contain starting timestamps greater than or equal to |start| and less than the |remove end timestamp|.
      1. For each removed frame, if the frame has a [=decode timestamp=] equal to the [=last decode timestamp=] for the frame's track, run the following steps:

        If {{SourceBuffer/mode}} equals {{AppendMode/""segments""}}:
        Set {{SourceBuffer/[[group end timestamp]]}} to [=presentation timestamp=].
        If {{SourceBuffer/mode}} equals {{AppendMode/""sequence""}}:
        Set {{SourceBuffer/[[group start timestamp]]}} equal to the {{SourceBuffer/[[group end timestamp]]}}.
      2. Unset the [=last decode timestamp=] on all [=track buffers=].
      3. Unset the [=last frame duration=] on all [=track buffers=].
      4. Unset the [=highest end timestamp=] on all [=track buffers=].
      5. Set the [=need random access point flag=] on all [=track buffers=] to true.
    4. Remove all possible decoding dependencies on the [=coded frames=] removed in the previous step by removing all [=coded frames=] from this [=track buffer=] between those frames removed in the previous step and the next [=random access point=] after those removed frames.

      Removing all [=coded frames=] until the next [=random access point=] is a conservative estimate of the decoding dependencies since it assumes all frames between the removed frames and the next random access point depended on the frames that were removed.

    5. If this object is in {{MediaSource/activeSourceBuffers}}, the is greater than or equal to |start| and less than the |remove end timestamp|, and {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} is greater than {{HTMLMediaElement/HAVE_METADATA}}, then set the {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} attribute to {{HTMLMediaElement/HAVE_METADATA}} and stall playback.

      Per [[HTML]] logic, {{HTMLMediaElement}}.{{HTMLMediaElement/readyState}} changes may trigger events on the HTMLMediaElement.

      This transition occurs because media data for the current position has been removed. Playback cannot progress until media for the is appended or the selected/enabled tracks change.

  4. If the {{SourceBuffer/[[buffer full flag]]}} equals true and this object is ready to accept more bytes, then set the {{SourceBuffer/[[buffer full flag]]}} to false.

Coded Frame Eviction

This algorithm is run to free up space in this {{SourceBuffer}} when new data is appended.

  1. Let |new data:BufferSource| equal the data that is about to be appended to this SourceBuffer.

    Need to recognize step here that implementations MAY decide to set {{SourceBuffer/[[buffer full flag]]}} true here if it predicts that processing |new data| in addition to any existing bytes in {{SourceBuffer/[[input buffer]]}} would exceed the capacity of the {{SourceBuffer}}. Such a step enables more proactive push-back from implementations before accepting |new data| which would overflow resources, for example. In practice, at least one implementation already does this.

  2. If the {{SourceBuffer/[[buffer full flag]]}} equals false, then abort these steps.
  3. Let |removal ranges:normalized TimeRanges| equal a list of presentation time ranges that can be evicted from the presentation to make room for the |new data|.

    Implementations MAY use different methods for selecting |removal ranges| so web applications SHOULD NOT depend on a specific behavior. The web application can use the {{SourceBuffer/buffered}} attribute to observe whether portions of the buffered data have been evicted.

  4. For each range in |removal ranges|, run the [=coded frame removal=] algorithm with |start:double| and |end:unrestricted double| equal to the removal range start and end timestamp respectively.

Audio Splice Frame

Follow these steps when the [=coded frame processing=] algorithm needs to generate a splice frame for two overlapping audio [=coded frames=]:

  1. Let |track buffer| be the [=track buffer=] that will contain the splice.
  2. Let |new coded frame| be the new [=coded frame=], that is being added to |track buffer|, which triggered the need for a splice.
  3. Let |presentation timestamp:double| be the [=presentation timestamp=] for |new coded frame|.
  4. Let |decode timestamp:double| be the decode timestamp for |new coded frame|.
  5. Let |frame duration:double| be the [=coded frame duration=] of |new coded frame|.
  6. Let |overlapped frame| be the [=coded frame=] in |track buffer| with a [=presentation interval=] that contains |presentation timestamp|.
  7. Update |presentation timestamp| and |decode timestamp| to the nearest audio sample timestamp based on sample rate of the audio in |overlapped frame|. If a timestamp is equidistant from both audio sample timestamps, then use the higher timestamp (e.g., floor(x * sample_rate + 0.5) / sample_rate).

    For example, given the following values:

    • The [=presentation timestamp=] of |overlapped frame| equals 10.
    • The sample rate of |overlapped frame| equals 8000 Hz
    • |presentation timestamp| equals 10.01255
    • |decode timestamp| equals 10.01255

    |presentation timestamp| and |decode timestamp| are updated to 10.0125 since 10.01255 is closer to 10 + 100/8000 (10.0125) than 10 + 101/8000 (10.012625)

  8. If the user agent does not support crossfading then run the following steps:
    1. Remove |overlapped frame| from |track buffer|.
    2. Add a silence frame to |track buffer| with the following properties:
      • The [=presentation timestamp=] set to the |overlapped frame| [=presentation timestamp=].
      • The [=decode timestamp=] set to the |overlapped frame| [=decode timestamp=].
      • The [=coded frame duration=] set to difference between |presentation timestamp| and the |overlapped frame| [=presentation timestamp=].

      Some implementations MAY apply fades to/from silence to coded frames on either side of the inserted silence to make the transition less jarring.

    3. Return to caller without providing a splice frame.

      This is intended to allow |new coded frame| to be added to the |track buffer| as if |overlapped frame| had not been in the |track buffer| to begin with.

  9. Let |frame end timestamp:double| equal the sum of |presentation timestamp| and |frame duration|.
  10. Let |splice end timestamp:double| equal the sum of |presentation timestamp| and the splice duration of 5 milliseconds.
  11. Let |fade out coded frames| equal |overlapped frame| as well as any additional frames in |track buffer| that have a [=presentation timestamp=] greater than |presentation timestamp| and less than |splice end timestamp|.
  12. Remove all the frames included in |fade out coded frames| from |track buffer|.
  13. Return a splice frame with the following properties:
    • The [=presentation timestamp=] set to the |overlapped frame| [=presentation timestamp=].
    • The [=decode timestamp=] set to the |overlapped frame| [=decode timestamp=].
    • The [=coded frame duration=] set to difference between |frame end timestamp| and the |overlapped frame| [=presentation timestamp=].
    • The fade out coded frames equals |fade out coded frames|.
    • The fade in coded frame equals |new coded frame|.

      If the |new coded frame| is less than 5 milliseconds in duration, then coded frames that are appended after the |new coded frame| will be needed to properly render the splice.

    • The splice timestamp equals |presentation timestamp|.

    See the [=audio splice rendering=] algorithm for details on how this splice frame is rendered.

Audio Splice Rendering

The following steps are run when a spliced frame, generated by the [=audio splice frame=] algorithm, needs to be rendered by the media element:

  1. Let |fade out coded frames| be the [=coded frames=] that are faded out during the splice.
  2. Let |fade in coded frames| be the [=coded frames=] that are faded in during the splice.
  3. Let |presentation timestamp:double| be the [=presentation timestamp=] of the first coded frame in |fade out coded frames|.
  4. Let |end timestamp:double| be the sum of the [=presentation timestamp=] and the [=coded frame duration=] of the last frame in |fade in coded frames|.
  5. Let |splice timestamp:double| be the [=presentation timestamp=] where the splice starts. This corresponds with the [=presentation timestamp=] of the first frame in |fade in coded frames|.
  6. Let |splice end timestamp:double| equal |splice timestamp| plus five milliseconds.
  7. Let |fade out samples| be the samples generated by decoding |fade out coded frames|.
  8. Trim |fade out samples| so that it only contains samples between |presentation timestamp| and |splice end timestamp|.
  9. Let |fade in samples| be the samples generated by decoding |fade in coded frames|.
  10. If |fade out samples| and |fade in samples| do not have a common sample rate and channel layout, then convert |fade out samples| and |fade in samples| to a common sample rate and channel layout.
  11. Let |output samples| be a buffer to hold the output samples.
  12. Apply a linear gain fade out with a starting gain of 1 and an ending gain of 0 to the samples between |splice timestamp| and |splice end timestamp| in |fade out samples|.
  13. Apply a linear gain fade in with a starting gain of 0 and an ending gain of 1 to the samples between |splice timestamp| and |splice end timestamp| in |fade in samples|.
  14. Copy samples between |presentation timestamp| to |splice timestamp| from |fade out samples| into |output samples|.
  15. For each sample between |splice timestamp| and |splice end timestamp|, compute the sum of a sample from |fade out samples| and the corresponding sample in |fade in samples| and store the result in |output samples|.
  16. Copy samples between |splice end timestamp| to |end timestamp| from |fade in samples| into |output samples|.
  17. Render |output samples|.

Here is a graphical representation of this algorithm.

Audio splice diagram

Text Splice Frame

Follow these steps when the [=coded frame processing=] algorithm needs to generate a splice frame for two overlapping timed text [=coded frames=]:

  1. Let |track buffer| be the [=track buffer=] that will contain the splice.
  2. Let |new coded frame| be the new [=coded frame=], that is being added to |track buffer|, which triggered the need for a splice.
  3. Let |presentation timestamp:double| be the [=presentation timestamp=] for |new coded frame|
  4. Let |decode timestamp:double| be the decode timestamp for |new coded frame|.
  5. Let |frame duration:double| be the [=coded frame duration=] of |new coded frame|.
  6. Let |frame end timestamp:double| equal the sum of |presentation timestamp| and |frame duration|.
  7. Let |first overlapped frame| be the [=coded frame=] in |track buffer| with a [=presentation interval=] that contains |presentation timestamp|.
  8. Let |overlapped presentation timestamp:double| be the [=presentation timestamp=] of the |first overlapped frame|.
  9. Let |overlapped frames| equal |first overlapped frame| as well as any additional frames in |track buffer| that have a [=presentation timestamp=] greater than |presentation timestamp| and less than |frame end timestamp|.
  10. Remove all the frames included in |overlapped frames| from |track buffer|.
  11. Update the [=coded frame duration=] of the |first overlapped frame| to |presentation timestamp| minus |overlapped presentation timestamp|.
  12. Add |first overlapped frame| to the |track buffer|.
  13. Return to caller without providing a splice frame.

    This is intended to allow |new coded frame| to be added to the |track buffer| as if it hadn't overlapped any frames in |track buffer| to begin with.

SourceBufferList Object

SourceBufferList is a simple container object for SourceBuffer objects. It provides read-only array access and fires events when the list is modified.

[Exposed=(Window,DedicatedWorker)]
interface SourceBufferList : EventTarget {
    readonly        attribute unsigned long length;
                    attribute EventHandler  onaddsourcebuffer;
                    attribute EventHandler  onremovesourcebuffer;
    getter SourceBuffer (unsigned long index);
};

Attributes

length of type {{unsigned long}}, readonly

Indicates the number of SourceBuffer objects in the list.

onaddsourcebuffer of type {{EventHandler}}

The event handler for the {{addsourcebuffer}} event.

onremovesourcebuffer of type {{EventHandler}}

The event handler for the {{removesourcebuffer}} event.

Methods

getter

Allows the SourceBuffer objects in the list to be accessed with an array operator (i.e., []).

  1. If |index:unsigned long| is greater than or equal to the {{SourceBufferList/length}} attribute then return undefined and abort these steps.
  2. Return the |index|'th SourceBuffer object in the list.
ParameterTypeNullableOptionalDescription
|index| {{unsigned long}}
Return type: {{SourceBuffer}}

Event Summary

Event name Interface Dispatched when...
addsourcebuffer {{Event}} When a {{SourceBuffer}} is added to the list.
removesourcebuffer {{Event}} When a {{SourceBuffer}} is removed from the list.

ManagedMediaSource interface

A {{ManagedMediaSource}} is a {{MediaSource}} that actively manages its memory content. Unlike a {{MediaSource}}, the [=user agent=] can evict evict content through the [=ManagedMediaSource/memory cleanup=] algorithm from its {{MediaSource/sourceBuffers}} (populated with {{ManagedSourceBuffer}}) for any reason.

      [Exposed=(Window,DedicatedWorker)]
      interface ManagedMediaSource : MediaSource {
        constructor();
        readonly attribute boolean streaming;
        attribute EventHandler onstartstreaming;
        attribute EventHandler onendstreaming;
      };
      

Attributes

streaming

On getting:

  1. Return the current value of the attribute.

Event Summary

Event name Interface Dispatched when...
startstreaming {{Event}} {{ManagedMediaSource/streaming}} attribute changed from `false` to `true`.
endstreaming {{Event}} {{ManagedMediaSource/streaming}} attribute changed from `true` to `false`.

Algorithms

`ManagedSourceBuffer` Monitoring

The following steps are run periodically, whenever the [=MediaSource/SourceBuffer Monitoring=] algorithm is scheduled to run.

Having enough managed data to ensure uninterrupted playback is an implementation defined condition where the user agent determines that it currently has enough data to play the presentation without stalling for a meaningful period of time. This condition is constantly evaluated to determine when to transition the value of {{ManagedMediaSource/streaming}}. These transitions indicate when the user agent believes it has enough data buffered or it needs more data respectively.

Being able to retrieve and buffer data in an efficient way is an implementation defined condition where the user agent determines that it can fetch new data in an energy efficient manner while able to achieve the desired memory usage.

  1. Run the {{MediaSource}} [=MediaSource/SourceBuffer Monitoring=] algorithm.
  2. Let |can play uninterupted and efficiently| be a flag that is true if the {{HTMLMediaElement/buffered}} attribute contains a {{TimeRanges}} that includes the current playback position and [=enough managed data to ensure uninterrupted playback=] and [=able to retrieve and buffer data in an efficient way=]
    If |can play uninterupted and efficiently| is not equal to {{ManagedMediaSource/streaming}}, [=queue an element task=] on the [=media element=] that runs the following steps:
    1. Set |this| {{ManagedMediaSource/streaming}} attribute to |can play uninterupted and efficiently|.
    2. If |can play uninterupted and efficiently| is false [=fire an event=] called {{startstreaming}} at the {{ManagedMediaSource}}.
    3. Else [=fire an event=] called {{endstreaming}} at the {{ManagedMediaSource}}.

Memory Cleanup

  1. For each |buffer:ManagedSourceBuffer| in [=this=]'s {{MediaSource/sourceBuffers}}:
    1. Run the |buffer|'s [=ManagedSourceBuffer/memory cleanup=] algorithm.

BufferedChangeEvent interface

        [Exposed=(Window,DedicatedWorker)]
        interface BufferedChangeEvent : Event {
          constructor(DOMString type, BufferedChangeEventInit eventInitDict);

          [SameObject] readonly attribute TimeRanges addedRanges;
          [SameObject] readonly attribute TimeRanges removedRanges;
        };

        dictionary BufferedChangeEventInit : EventInit {
          TimeRanges addedRanges;
          TimeRanges removedRanges;
        };
      

Attributes

addedRanges
The time ranges added during the last run of the [=coded frame processing=] algorithm.
removedRanges
The time ranges removed during the last run of the [=coded frame removal=] or [=coded frame eviction=] algorithm or if the user agent evicted content in response to a [=ManagedSourceBuffer/memory cleanup=].

ManagedSourceBuffer interface

        [Exposed=(Window,DedicatedWorker)]
        interface ManagedSourceBuffer : SourceBuffer {
          attribute EventHandler onbufferedchange;
        };
      

Attributes

onbufferedchange

An [=event handler IDL attribute=] whose [=event handler event type=] is {{bufferedchange}}.

Event Summary

Event name Interface Dispatched when...
bufferedchange {{Event}} The {{ManagedSourceBuffer}}'s buffered range changed following a call to {{SourceBuffer/appendBuffer()}}, {{SourceBuffer/remove()}}, or as a consequence of the user agent running the [=ManagedSourceBuffer/memory cleanup=] algorithm.

Algorithms

Buffered Change

The following steps are run at the completion of all operations to the {{ManagedSourceBuffer}} |buffer:ManagedSourceBuffer| that would cause a |buffer|'s {{SourceBuffer/buffered}} to change. That is once {{SourceBuffer/appendBuffer()}}, {{SourceBuffer/remove()}} or [=ManagedSourceBuffer/memory cleanup=] algorithm have completed.

  1. Let |previous buffered ranges:normalized TimeRanges| equal the {{SourceBuffer/buffered}} attribute before the changes occurred.
  2. Let |new buffered ranges:normalized TimeRanges| equal the new {{SourceBuffer/buffered}} {{TimeRanges}}.
  3. Let |added:normalized TimeRanges| equal the |previous buffered ranges:normalized TimeRanges| subtracted from |new buffered ranges:normalized TimeRanges|.
  4. Let |removed:normalized TimeRanges| equal the |new buffered ranges:normalized TimeRanges| subtracted from |previous buffered ranges:normalized TimeRanges|.
  5. Let |eventInitDict| be a new {{BufferedChangeEventInit}} dictionary initialized with |added| as its {{BufferedChangeEventInit/addedRanges}} and |removed| as its {{BufferedChangeEventInit/removedRanges}}
  6. [=Queue a task=] to [=fire an event=] named {{bufferedchange}} at |buffer| using the {{BufferedChangeEvent}} interface, initialized with |eventInitDict|.

Memory cleanup

  1. If [=this=] is not in [=this=]'s {{ManagedMediaSource}} parent {{MediaSource/activeSourceBuffers}}:
    1. Run the [=coded frame removal=] algorithm with 0 and positive infinity equal to the removal range start and end timestamp respectively, and abort these steps.
  2. Let |removal ranges:normalized TimeRanges| equal a list of presentation time ranges that can be evicted from the presentation to ensure uninterrupted playback from {{HTMLMediaElement.currentTime}} until such presentation could be fetched again.

    Implementations can use different strategies for selecting |removal ranges| so web applications shouldn't depend on a specific behavior. The web application would listen to the {{bufferedchange}} event to observe whether portions of the buffered data have been evicted.

  3. For each range in |removal ranges|, run the [=coded frame removal=] algorithm with |start:double| and |end:unrestricted double| equal to the removal range start and end timestamp respectively.

HTMLMediaElement Extensions

This section specifies what existing {{HTMLMediaElement}}.{{HTMLMediaElement/seekable}} and {{HTMLMediaElement}}.{{HTMLMediaElement/buffered}} attributes on the {{HTMLMediaElement}} MUST return when a MediaSource is attached to the element, and what the existing {{HTMLMediaElement}}.{{HTMLMediaElement/srcObject}} attribute MUST also do when it is set to be a {{MediaSourceHandle}} object.

{{HTMLMediaElement}}.{{HTMLMediaElement/seekable}}

The {{HTMLMediaElement}}.{{HTMLMediaElement/seekable}} attribute returns a new static created based on the following steps:

  1. If the {{MediaSource}} was constructed in a {{DedicatedWorkerGlobalScope}} that is terminated or is closing then return an empty {{TimeRanges}} object and abort these steps.

    This case is intended to handle implementations that may no longer maintain any previous information about buffered or seekable media in a MediaSource that was constructed in a DedicatedWorkerGlobalScope that has been terminated by {{Worker/terminate()}} or user agent execution of [=terminate a worker=] for the MediaSource's DedicatedWorkerGlobalScope, for instance as the eventual result of {{DedicatedWorkerGlobalScope/close()}} execution.

    Should there be some (eventual) media element error transition in the case of an attached worker MediaSource having its context destroyed? The experimental Chromium implementation of worker MSE just keeps the element readyState, networkState and error the same as prior to that context destruction, though the seekable and buffered attributes each report an empty TimeRange.

  2. Let |recent duration:unrestricted double| and |recent live seekable range:normalized TimeRanges| respectively be the recent values of {{MediaSource/duration}} and {{MediaSource/[[live seekable range]]}}, determined as follows:
    If the {{MediaSource}} was constructed in a {{Window}}
    Set |recent duration| to be {{MediaSource/duration}} and set |recent live seekable range| to be {{MediaSource/[[live seekable range]]}}.
    Otherwise:
    Set |recent duration| and |recent live seekable range| respectively to be what the {{MediaSource/duration}} and {{MediaSource/[[live seekable range]]}} were recently, updated by handling implicit messages posted by the {{MediaSource}} to its {{MediaSource/[[port to main]]}} on every change to {{MediaSource/duration}} or {{MediaSource/[[live seekable range]]}}.
  3. If |recent duration| equals NaN:
    Return an empty {{TimeRanges}} object.
    If |recent duration| equals positive Infinity:
    1. If |recent live seekable range| is not empty:
      1. Let |union ranges:normalized TimeRanges| be the union of |recent live seekable range| and the {{HTMLMediaElement}}.{{HTMLMediaElement/buffered}} attribute.
      2. Return a single range with a start time equal to the earliest start time in |union ranges| and an end time equal to the highest end time in |union ranges| and abort these steps.
    2. If the {{HTMLMediaElement}}.{{HTMLMediaElement/buffered}} attribute returns an empty {{TimeRanges}} object, then return an empty {{TimeRanges}} object and abort these steps.
    3. Return a single range with a start time of 0 and an end time equal to the highest end time reported by the {{HTMLMediaElement}}.{{HTMLMediaElement/buffered}} attribute.
    Otherwise:
    Return a single range with a start time of 0 and an end time equal to |recent duration|.

{{HTMLMediaElement}}.{{HTMLMediaElement/buffered}}

The {{HTMLMediaElement}}.{{HTMLMediaElement/buffered}} attribute returns a static based on the following steps.

  1. If the {{MediaSource}} was constructed in a {{DedicatedWorkerGlobalScope}} that is terminated or is closing then return an empty {{TimeRanges}} object and abort these steps.

    This case is intended to handle implementations that may no longer maintain any previous information about buffered or seekable media in a MediaSource that was constructed in a DedicatedWorkerGlobalScope that has been terminated by {{Worker/terminate()}} or user agent execution of [=terminate a worker=] for the MediaSource's DedicatedWorkerGlobalScope, for instance as the eventual result of {{DedicatedWorkerGlobalScope/close()}} execution.

    Should there be some (eventual) media element error transition in the case of an attached worker MediaSource having its context destroyed? The experimental Chromium implementation of worker MSE just keeps the element readyState, networkState and error the same as prior to that context destruction, though the seekable and buffered attributes each report an empty TimeRange.

  2. Let |recent intersection ranges:normalized TimeRanges| be determined as follows:
    If the {{MediaSource}} was constructed in a {{Window}}
    1. Let |recent intersection ranges| equal an empty {{TimeRanges}} object.
    2. If {{MediaSource/activeSourceBuffers}}.length does not equal 0 then run the following steps:
      1. Let |active ranges:sequence of normalized TimeRanges| be the ranges returned by {{SourceBuffer/buffered}} for each SourceBuffer object in {{MediaSource/activeSourceBuffers}}.
      2. Let |highest end time:unrestricted double| be the largest range end time in the |active ranges|.
      3. Let |recent intersection ranges:normalized TimeRanges| equal a {{TimeRanges}} object containing a single range from 0 to |highest end time|.
      4. For each SourceBuffer object in {{MediaSource/activeSourceBuffers}} run the following steps:
        1. Let |source ranges:normalized TimeRanges| equal the ranges returned by the {{SourceBuffer/buffered}} attribute on the current SourceBuffer.
        2. If {{MediaSource/readyState}} is {{ReadyState/""ended""}}, then set the end time on the last range in |source ranges| to |highest end time|.
        3. Let |new intersection ranges:normalized TimeRanges| equal the intersection between the |recent intersection ranges| and the |source ranges|.
        4. Replace the ranges in |recent intersection ranges| with the |new intersection ranges|.
    Otherwise:
    Let |recent intersection ranges| be the {{TimeRanges}} resulting from the steps for the {{Window}} case, but run with the {{MediaSource}} and its {{SourceBuffer}} objects in their {{DedicatedWorkerGlobalScope}} and communicated by using {{MediaSource/[[port to main]]}} implicit messages on every update to the {{MediaSource/activeSourceBuffers}}, {{MediaSource/readyState}}, or any of the buffering state that would change any of the values of each of those {{SourceBuffer/buffered}} attributes of the {{MediaSource/activeSourceBuffers}}.

    The overhead of recalculating and communicating |recent intersection ranges| so frequently is one reason for allowing implementation flexibility to query this information on-demand using other mechanisms such as shared memory and locks as mentioned in [=cross-context communication model=].

  3. If the current value of this attribute has not been set by this algorithm or |recent intersection ranges| does not contain the exact same range information as the current value of this attribute, then update the current value of this attribute to |recent intersection ranges|.
  4. Return the current value of this attribute.

{{HTMLMediaElement}}.{{HTMLMediaElement/srcObject}}

If a {{HTMLMediaElement}}.{{HTMLMediaElement/srcObject}} attribute is assigned a {{MediaSourceHandle}}, then set {{MediaSourceHandle/[[has ever been assigned as srcobject]]}} for that {{MediaSourceHandle}} to true as part of the synchronous steps of the extended {{HTMLMediaElement}}'s {{HTMLMediaElement/srcObject}} setter that occur before invoking the element's load algorithm.

This prevents transferring that {{MediaSourceHandle}} object ever again, enabling clear synchronous exception if that is attempted.

MediaSourceHandle needs to be added to HTMLMediaElement's MediaProvider IDL typedef and related text involving media provider objects.

AudioTrack Extensions

This section specifies extensions to the [[HTML]] {{AudioTrack}} definition.

[Exposed=(Window,DedicatedWorker)]
partial interface AudioTrack {
    readonly        attribute SourceBuffer? sourceBuffer;
};
[[HTML]] {{AudioTrack}} needs Window+DedicatedWorker exposure.

Attributes

sourceBuffer of type SourceBuffer, readonly , nullable

On getting, run the following step:

If this track was created by a {{SourceBuffer}} that was created on the same [=global object/realm=] as this track, and if that {{SourceBuffer}} has not been removed from the {{MediaSource/sourceBuffers}} attribute of its [=parent media source=]:
Return the {{SourceBuffer}} that created this track.
Otherwise:
Return null.
For example, if a {{DedicatedWorkerGlobalScope}} {{SourceBuffer}} notified its internal create track mirror handler in {{Window}} to create this track, then the {{Window}} copy of the track would return null for this attribute.

VideoTrack Extensions

This section specifies extensions to the [[HTML]] {{VideoTrack}} definition.

[Exposed=(Window,DedicatedWorker)]
partial interface VideoTrack {
    readonly        attribute SourceBuffer? sourceBuffer;
};
[[HTML]] {{VideoTrack}} needs Window+DedicatedWorker exposure.

Attributes

sourceBuffer of type SourceBuffer, readonly , nullable

On getting, run the following step:

If this track was created by a {{SourceBuffer}} that was created on the same [=global object/realm=] as this track, and if that {{SourceBuffer}} has not been removed from the {{MediaSource/sourceBuffers}} attribute of its [=parent media source=]:
Return the {{SourceBuffer}} that created this track.
Otherwise:
Return null.
For example, if a {{DedicatedWorkerGlobalScope}} {{SourceBuffer}} notified its internal create track mirror handler in {{Window}} to create this track, then the {{Window}} copy of the track would return null for this attribute.

TextTrack Extensions

This section specifies extensions to the [[HTML]] {{TextTrack}} definition.

[Exposed=(Window,DedicatedWorker)]
partial interface TextTrack {
    readonly        attribute SourceBuffer? sourceBuffer;
};
[[HTML]] {{TextTrack}} needs Window+DedicatedWorker exposure.

Attributes

sourceBuffer of type SourceBuffer, readonly , nullable

On getting, run the following step:

If this track was created by a {{SourceBuffer}} that was created on the same [=global object/realm=] as this track, and if that {{SourceBuffer}} has not been removed from the {{MediaSource/sourceBuffers}} attribute of its [=parent media source=]:
Return the {{SourceBuffer}} that created this track.
Otherwise:
Return null.
For example, if a {{DedicatedWorkerGlobalScope}} {{SourceBuffer}} notified its internal create track mirror handler in {{Window}} to create this track, then the {{Window}} copy of the track would return null for this attribute.

Byte Stream Formats

The bytes provided through {{SourceBuffer/appendBuffer()}} for a SourceBuffer form a logical byte stream. The format and semantics of these byte streams are defined in byte stream format specifications. The byte stream format registry [[MSE-REGISTRY]] provides mappings between a MIME type that may be passed to {{MediaSource/addSourceBuffer()}}, {{MediaSource/isTypeSupported()}} or {{SourceBuffer/changeType()}} and the byte stream format expected by a {{SourceBuffer}} using that MIME type for parsing newly appended data. Implementations are encouraged to register mappings for byte stream formats they support to facilitate interoperability. The byte stream format registry [[MSE-REGISTRY]] is the authoritative source for these mappings. If an implementation claims to support a MIME type listed in the registry, its SourceBuffer implementation MUST conform to the [=byte stream format specification=] listed in the registry entry.

The byte stream format specifications in the registry are not intended to define new storage formats. They simply outline the subset of existing storage format structures that implementations of this specification will accept.

Byte stream format parsing and validation is implemented in the [=segment parser loop=] algorithm.

This section provides general requirements for all byte stream format specifications:

Byte stream specifications MUST at a minimum define constraints which ensure that the above requirements hold. Additional constraints MAY be defined, for example to simplify implementation.

Examples

Use of the Media Source Extensions

<script>
  function onSourceOpen(videoTag, e) {
    var mediaSource = e.target;

    if (mediaSource.sourceBuffers.length > 0)
        return;

    var sourceBuffer = mediaSource.addSourceBuffer('video/webm; codecs="vorbis,vp8"');

    videoTag.addEventListener('seeking', onSeeking.bind(videoTag, mediaSource));
    videoTag.addEventListener('progress', onProgress.bind(videoTag, mediaSource));

    var initSegment = GetInitializationSegment();

    if (initSegment == null) {
      // Error fetching the initialization segment. Signal end of stream with an error.
      mediaSource.endOfStream("network");
      return;
    }

    // Append the initialization segment.
    var firstAppendHandler = function(e) {
      var sourceBuffer = e.target;
      sourceBuffer.removeEventListener('updateend', firstAppendHandler);

      // Append some initial media data.
      appendNextMediaSegment(mediaSource);
    };
    sourceBuffer.addEventListener('updateend', firstAppendHandler);
    sourceBuffer.appendBuffer(initSegment);
  }

  function appendNextMediaSegment(mediaSource) {
    if (mediaSource.readyState == "closed")
      return;

    // If we have run out of stream data, then signal end of stream.
    if (!HaveMoreMediaSegments()) {
      mediaSource.endOfStream();
      return;
    }

    // Make sure the previous append is not still pending.
    if (mediaSource.sourceBuffers[0].updating)
        return;

    var mediaSegment = GetNextMediaSegment();

    if (!mediaSegment) {
      // Error fetching the next media segment.
      mediaSource.endOfStream("network");
      return;
    }

    // NOTE: If mediaSource.readyState == “ended”, this appendBuffer() call will
    // cause mediaSource.readyState to transition to "open". The web application
    // should be prepared to handle multiple “sourceopen” events.
    mediaSource.sourceBuffers[0].appendBuffer(mediaSegment);
  }

  function onSeeking(mediaSource, e) {
    var video = e.target;

    if (mediaSource.readyState == "open") {
      // Abort current segment append.
      mediaSource.sourceBuffers[0].abort();
    }

    // Notify the media segment loading code to start fetching data at the
    // new playback position.
    SeekToMediaSegmentAt(video.currentTime);

    // Append a media segment from the new playback position.
    appendNextMediaSegment(mediaSource);
  }

  function onProgress(mediaSource, e) {
    appendNextMediaSegment(mediaSource);
  }
</script>

<video id="v" autoplay> </video>

<script>
  var video = document.getElementById('v');
  var mediaSource = new MediaSource();
  mediaSource.addEventListener('sourceopen', onSourceOpen.bind(this, video));
  video.src = window.URL.createObjectURL(mediaSource);
</script>
          

Use of the Managed Media Source Extensions

      <script>
      async function setUpVideoStream() {
        // Specific video format and codec
        const mediaType = 'video/mp4; codecs="mp4a.40.2,avc1.4d4015"';

        // Check if the type of video format / codec is supported.
        if (ManagedMediaSource?.isTypeSupported(mediaType)) {
          return; // Not supported, do something else.
        }

        // Set up video and its managed source.
        const video = document.createElement("video");
        const source = new ManagedMediaSource();

        video.controls = true;

        await new Promise((resolve) => {
          video.src = URL.createObjectURL(source);
          source.addEventListener("sourceopen", resolve, { once: true });
          document.body.appendChild(video);
        });

        const sourceBuffer = source.addSourceBuffer(mediaType);

        // Set up the event handlers
        sourceBuffer.onbufferedchange = (e) => {
          console.log("onbufferedchange event fired.");
          console.log(`Added Ranges: ${timeRangesToString(e.addedRanges)}`);
          console.log(`Removed Ranges: ${timeRangesToString(e.removedRanges)}`);
        };

        source.onstartstreaming = async () => {
          const response = await fetch("./videos/bipbop.mp4");
          const buffer = await response.arrayBuffer();
          await new Promise((resolve) => {
            sourceBuffer.addEventListener("updateend", resolve, { once: true });
            sourceBuffer.appendBuffer(buffer);
          });
        };

        source.onendstreaming = async () => {
          // Stop fetching new segments here
        };
      }

      // Helper function...
      function timeRangesToString(timeRanges) {
        const ranges = [];
        for (let i = 0; i < timeRanges.length; i++) {
          ranges.push([timeRanges.start(i), timeRanges.end(i)]);
        }
        return "[" + ranges.map(([start, end]) => `[${start}, ${end})` ) + "]";     
      }
      </script>
      <body onload="setUpVideoStream()"></body>
    

Acknowledgments

The editors would like to thank for their contributions to this specification.

VideoPlaybackQuality

The video playback quality metrics described in previous revisions of this specification (e.g., sections 5 and 10 of the Candidate Recommendation) are now being developed as part of [[MEDIA-PLAYBACK-QUALITY]]. Some implementations may have implemented the earlier draft VideoPlaybackQuality object and the {{HTMLVideoElement}} extension method getVideoPlaybackQuality() described in those previous revisions.