W3C

Media Source Extensions

W3C Editor's Draft 30 January 2013

This version:
http://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html
Latest published version:
http://www.w3.org/TR/media-source/
Latest editor's draft:
http://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html
Editors:
Aaron Colwell, Google Inc.
Adrian Bateman, Microsoft Corporation
Mark Watson, Netflix Inc.

Abstract

This specification extends HTMLMediaElement 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.

Status of This Document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.

The working groups maintains a list of all bug reports that the editors have not yet tried to address. This draft highlights some of the pending issues that are still to be discussed in the working group. No decision has been taken on the outcome of these issues including whether they are valid.

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 join the mailing list mentioned below and take part in the discussions.

This document was published by the HTML Working Group as an Editor's Draft. If you wish to make comments regarding this document, please send them to public-html-media@w3.org (subscribe, archives). All feedback is welcome.

Publication as an Editor's Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

Table of Contents

1. Introduction

This specification allows JavaScript to dynamically construct media streams for <audio> and <video>. It defines objects that allow JavaScript to pass media segments to an HTMLMediaElement. A buffering model is also included to describe how the user agent should act when different media segments are appended at different times. Byte stream specifications for WebM, ISO Base Media File Format, and MPEG-2 Transport Streams are given to specify the expected format of byte streams used with these extensions.

Media Source Pipeline Model Diagram

1.1 Goals

This specification was designed with the following goals in mind:

1.2 Definitions

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).

Note

The byte stream format specifications contain format specific examples.

Media Segment

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

Note

The byte stream format specifications contain format specific examples.

Decoder Buffer

A buffer that holds initialization data and coded frames that will be decoded and rendered. This buffer may not exist in actual implementations, but it is intended to represent media data that will be decoded no matter what media segments are appended to update the SourceBuffer. This distinction is important when considering appends that happen close to the current playback position. See Track Buffer to Decoder Buffer transfer for details.

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.

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.

MediaSource object URL

A MediaSource object URL is a unique Blob URI created by createObjectURL(). It is used to attach a MediaSource object to an HTMLMediaElement.

These URLs are the same as what the File API specification calls a Blob URI, except that anything in the definition of that feature that refers to File and Blob objects is hereby extended to also apply to MediaSource objects.

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.

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 must have a unique Track ID.

Coded Frame

A unit of compressed media data that has a presentation timestamp and decode timestamp. The presentation timestamp indicates when the frame should be rendered. The decode timestamp indicates when the frame needs to be decoded. If frames can be decoded out of order, then the decode timestamp must be present in the bytestream. If frames cannot be decoded out of order and a decode timestamp is not present in the bytestream, then the decode timestamp is equal to the presentation timestamp.

Parent Media Source
The parent media source of a SourceBuffer object is the MediaSource object that created it.
Append Sequence
A series of appendArrayBuffer() or appendStream() calls on a SourceBuffer without any intervening abort() calls. The media segments in an append sequence must be adjacent and monotonically increasing in time without any gaps. An abort() call starts a new append sequence which allows media segments to be appended in non-monotonically increasing order.

2. Source Buffer Model

The subsections below outline the buffering model for this specification. It describes the various rules and behaviors associated with appending data to an individual SourceBuffer. At the highest level, the web application creates SourceBuffer objects and appends sequences of initialization segments and media segments to update their state. The media element pulls media data out of the MediaSource object, plays it, and fires events just like it would if a normal URL was passed to the src attribute. The web application is expected to monitor media element events to determine when it needs to append more media segments.

2.1 Appending a Media Segment over a buffered region

There are several ways that media segments can overlap segments in the SourceBuffer. Behavior for the different overlap situations are described below. If more than one overlap applies, then the start overlap must be resolved first, followed by any complete overlaps, and finally the end overlap. If a segment contains multiple tracks then the overlap is resolved independently for each track.

Issue 1

Bug 19673 - Seamless audio signal transitions at splice points

Issue 2

Bug 19784 - timestampOffset with multiplexed Media Segments

2.1.1 Complete Overlap

Complete overlap diagram

The figure above shows how the SourceBuffer is updated when a new media segment completely overlaps a segment in the buffer. In this case, the new segment completely replaces the old segment.

2.1.2 Start Overlap

Start overlap diagram

The figure above shows how the SourceBuffer is updated when the beginning of a new media segment overlaps a segment in the buffer. In this case, the new segment replaces all the old media data in the overlapping region. Since media segments are constrained to starting with random access points, this provides a seamless transition between segments.

When an audio frame in the SourceBuffer overlaps with the start of the new media segment special behavior is required. At a minimum implementations must support dropping the old audio frame that overlaps the start of the new segment and insert silence for the small gap that is created. Higher quality implementations may support crossfading or crosslapping between the overlapping audio frames. No matter which strategy is implemented, no gaps are created in the ranges reported by buffered and playback must never stall at the overlap.

2.1.3 End Overlap

End overlap diagram

The figure above shows how the SourceBuffer is updated when the end of a new media segment overlaps the beginning of a segment in the buffer. In this case, the SourceBuffer tries to keep as much of the old segment as possible. The amount saved depends on where the closest random access point, in the old segment, is to the end of the new segment. In the case of audio, if the gap is smaller than the size of an audio frame, then the SourceBuffer may render silence for this gap. This gap must not be reflect in buffered. The entire new segment must be added to the SourceBuffer, but it is up to the implementation to determine how much of the old segment data is retained.

Note

An implementation may keep old segment data before the end of the new segment to avoid creating a gap if it wishes. Doing this though can significantly increase implementation complexity and could cause delays at the splice point.

Note

The web application can use buffered to determine how much of the old segment was preserved.

2.1.4 Middle Overlap

Middle overlap diagram

The figure above shows how the SourceBuffer is updated when the new media segment is in the middle of the old segment. This condition is handled by first resolving the start overlap and then resolving the end overlap.

2.2 Track Buffer to Decoder Buffer transfer

The track buffer represents the media that the web application would like the media element to play. The decoder buffer contains the data that will actually get decoded and rendered. In most cases the decoder buffer will simply contain a subset of the track buffer near the current playback position. These two buffers start to diverge when media segments that overlap or are very close to the current playback position are appended. Depending on the contents of the new media segment it may not be possible to switch to the new data immediately because there isn't a random access point close enough to the current playback position. The quality of the implementation determines how much data is considered "in the decoder buffer." It should transfer data to the decoder buffer as late as possible whilst maintaining seamless playback. Some implementations may be able to instantiate multiple decoders or decode the new data significantly faster than real-time to achieve a seamless splice immediately. Other implementations may delay until the next random access point before switching to the newly appended data. Notice that this difference in behavior is only observable when appending close to the current playback position. The decoder buffer represents a media subsegment, like a group of pictures or something with similar decode dependencies, that the media element commits to playing. This commitment may be influenced by a variety of things like limited decoding resources, hardware decode buffers, a jitter buffer, or the desire to limit implementation complexity.

Here is an example to help clarify the role of the decoder buffer. Say the current playback position has a timestamp of 8 and the media element pulled frames with timestamp 9 & 10 into the decoder buffer. The web application then appends a higher quality media segment that starts with a random access point at timestamp 9. The track buffer will get updated with the higher quality data, but the media element won't be able to switch to this higher quality data until the next random access point at timestamp 20. This is because a frame for timestamp 9 is already in the decoder buffer. The decoder buffer represents the "point of no return." for decoding. If a seek occurs the media element may choose to use the higher quality data since a seek might imply flushing the decoder buffer and the user expects a break in playback.

3. MediaSource Object

The MediaSource object represents a source of media data for an HTMLMediaElement. It keeps track of the 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 sourceBuffers to add media data to this source. The HTMLMediaElement fetches this media data from the MediaSource object when it is needed during playback.

enum ReadyState {
    "closed",
    "open",
    "ended"
};
Enumeration description
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 sourceBuffers.
ended The source is still attached to a media element, but endOfStream() has been called.
enum EndOfStreamError {
    "network",
    "decode"
};
Enumeration description
network

Terminates playback and signals that a network error has occured.

Note

If the JavaScript fetching media data encounters a network error it should use this status code to terminate playback.

decode

Terminates playback and signals that a decoding error has occured.

Note

If the JavaScript code fetching media data has problems parsing the data it should use this status code to terminate playback.

[Constructor]
interface MediaSource : EventTarget {
    readonly attribute SourceBufferList    sourceBuffers;
    readonly attribute SourceBufferList    activeSourceBuffers;
    readonly attribute ReadyState          readyState;
             attribute unrestricted double duration;
    SourceBuffer addSourceBuffer (DOMString type);
    void         removeSourceBuffer (SourceBuffer sourceBuffer);
    void         endOfStream (optional EndOfStreamError error);
    static bool  isTypeSupported (DOMString type);
};

3.1 Attributes

activeSourceBuffers of type SourceBufferList, readonly

Contains the subset of sourceBuffers that are providing the selected video track, the enabled audio tracks, and the "showing" or "hidden" text tracks.

Note

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

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 readyState attribute is "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 an INVALID_ACCESS_ERR exception and abort these steps.
  2. If the readyState attribute is not "open" then throw an INVALID_STATE_ERR exception and abort these steps.
  3. Run the duration change algorithm with new duration set to the value being assigned to this attribute.
    Note

    appendArrayBuffer(), appendStream() and endOfStream() can update the duration under certain circumstances.

readyState of type ReadyState, readonly

Indicates the current state of the MediaSource object. When the MediaSource is created readyState must be set to "closed".

sourceBuffers of type SourceBufferList, readonly
Contains the list of SourceBuffer objects associated with this MediaSource. When readyState equals "closed" this list will be empty. Once readyState transitions to "open" SourceBuffer objects can be added to this list by using addSourceBuffer().

3.2 Methods

addSourceBuffer

Adds a new SourceBuffer to sourceBuffers.

ParameterTypeNullableOptionalDescription
typeDOMString
Return type: SourceBuffer

When this method is invoked, the user agent must run the following steps:

  1. If type is null or an empty string then throw an INVALID_ACCESS_ERR 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 sourceBuffers, then throw a NOT_SUPPORTED_ERR exception and abort these steps.
  3. If the user agent can't handle any more SourceBuffer objects then throw a QUOTA_EXCEEDED_ERR exception and abort these steps.
  4. If the readyState attribute is not in the "open" state then throw an INVALID_STATE_ERR exception and abort these steps.
  5. Create a new SourceBuffer object and associated resources.
  6. Add the new object to sourceBuffers and queue a task to fire a simple event named addsourcebuffer at sourceBuffers.
  7. Return the new object.
endOfStream

Signals the end of the stream.

ParameterTypeNullableOptionalDescription
errorEndOfStreamError
Return type: void

When this method is invoked, the user agent must run the following steps:

  1. If the readyState attribute is not in the "open" state then throw an INVALID_STATE_ERR exception and abort these steps.
  2. Change the readyState attribute value to "ended".
  3. Queue a task to fire a simple event named sourceended at the MediaSource.
  4. If error is not set, null, or an empty string
    1. Run the duration change algorithm with new duration set to the highest end timestamp across all SourceBuffer objects in sourceBuffers.
      Note

      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. Playback should continue until all the media passed in via appendArrayBuffer() and appendStream() has been played.
    If error is set to "network"
    If the HTMLMediaElement.readyState attribute equals HAVE_NOTHING
    Run the steps of the resource fetch algorithm.
    If the HTMLMediaElement.readyState attribute is greater than 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.
    If error is set to "decode"
    If the HTMLMediaElement.readyState attribute equals 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.
    If the HTMLMediaElement.readyState attribute is greater than HAVE_NOTHING
    Run the media data is corrupted steps of the resource fetch algorithm.
    Otherwise
    Throw an INVALID_ACCESS_ERR exception.
isTypeSupported, static

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

Note

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 addSourceBuffer() call may still fail if sufficient resources are not available to support the addition of a new SourceBuffer.

Note

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
typeDOMString
Return type: bool

When this method is invoked, the user agent must run the following steps:

  1. If type 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 at 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.
removeSourceBuffer

Removes a SourceBuffer from sourceBuffers.

ParameterTypeNullableOptionalDescription
sourceBufferSourceBuffer
Return type: void

When this method is invoked, the user agent must run the following steps:

  1. If sourceBuffer is null then throw an INVALID_ACCESS_ERR exception and abort these steps.
  2. If sourceBuffer specifies an object that is not in sourceBuffers then throw a NOT_FOUND_ERR exception and abort these steps.
  3. Set the the sourceBuffer attribute in all tracks in sourceBuffer.audioTracks, sourceBuffer.videoTracks, and sourceBuffer.textTracks to null.
  4. Remove all the tracks in sourceBuffer.audioTracks, sourceBuffer.videoTracks, and sourceBuffer.textTracks from the respective audioTracks, videoTracks, and textTracks attributes on the HTMLMediaElement.
  5. Remove all the tracks in sourceBuffer.audioTracks, sourceBuffer.videoTracks, and sourceBuffer.textTracks and queue a task to fire a simple event named change at the modified lists.
  6. Queue a task to fire a simple event named change at the HTMLMediaElement track lists that were modified.
  7. If sourceBuffer is in activeSourceBuffers, then remove it from activeSourceBuffers and queue a task to fire a simple event named removesourcebuffer at activeSourceBuffers.
  8. Remove sourceBuffer from sourceBuffers and queue a task to fire a simple event named removesourcebuffer at sourceBuffers.
  9. Destroy all resources for sourceBuffer.

3.3 Event Summary

Event name Interface Dispatched when...
sourceopen Event readyState transitions from "closed" to "open" or from "ended" to "open".
sourceended Event readyState transitions from "open" to "ended".
sourceclose Event readyState transitions from "open" to "closed" or "ended" to "closed".

3.4 Algorithms

3.4.1 Attaching to a media element

A MediaSource object can be attached to a media element by assigning a MediaSource object URL to the media element 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 createObjectURL().

If the resource fetch algorithm absolute URL matches the MediaSource object URL, run the following steps right before the "Perform a potentially CORS-enabled fetch" step in the resource fetch algorithm.

If readyState is NOT set to "closed"
Run the steps of the resource fetch algorithm.
Otherwise
  1. Set the readyState attribute to "open".
  2. Queue a task to fire a simple event named sourceopen at the MediaSource.
  3. Allow the resource fetch algorithm to progress based on data passed in via appendArrayBuffer() and appendStream().

3.4.2 Detaching from a media element

The following steps are run in any case where the media element is going to transition to NETWORK_EMPTY and queue a task to fire a simple event named emptied at the media element. These steps should be run right before the transition.

  1. Set the readyState attribute to "closed".
  2. Set the duration attribute to NaN.
  3. Remove all the SourceBuffer objects from activeSourceBuffers.
  4. Queue a task to fire a simple event named removesourcebuffer at activeSourceBuffers.
  5. Remove all the SourceBuffer objects from sourceBuffers.
  6. Queue a task to fire a simple event named removesourcebuffer at sourceBuffers.
  7. Queue a task to fire a simple event named sourceclose at the MediaSource.

3.4.3 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 seek algorithm:

  1. The media element looks for media segments containing the new playback position in each SourceBuffer object in activeSourceBuffers.
    If one or more of the objects in activeSourceBuffers is missing media segments for the new playback position
    1. Set the HTMLMediaElement.readyState attribute to HAVE_METADATA.
    2. The media element waits for the necessary media segments to be passed to appendArrayBuffer() or appendStream().
      Note

      The web application can use buffered to determine what the media element needs to resume playback.

    Otherwise
    Continue
  2. The media element resets all decoders and initializes each one with data from the appropriate initialization segment.
  3. The media element feeds data from the media segments into the decoders until the new playback position is reached.
  4. Resume the seek algorithm at the "Await a stable state" step.

3.4.4 SourceBuffer Monitoring

The following steps are periodically run during playback to make sure that all of the SourceBuffer objects in activeSourceBuffers have enough data to ensure uninterrupted playback. Appending new segments and changes to 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 HAVE_ENOUGH_DATA ready state. These transitions indicate when the user agent believes it has enough data buffered or it needs more data respectively.

Note

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.readyState to determine whether more data is needed or not.

Note

When the media element needs more data, it should transition from HAVE_ENOUGH_DATA to 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 buffered for all objects in activeSourceBuffers do not contain TimeRanges for the current playback position:
  1. Set the HTMLMediaElement.readyState attribute to HAVE_METADATA.
  2. If this is the first transition to HAVE_METADATA, then queue a task to fire a simple event named loadedmetadata at the media element.
  3. Abort these steps.
If buffered for all objects in activeSourceBuffers contain TimeRanges that include the current playback position and enough data to ensure uninterrupted playback:
  1. Set the HTMLMediaElement.readyState attribute to HAVE_ENOUGH_DATA.
  2. Queue a task to fire a simple event named canplaythrough at the media element.
  3. Playback may resume at this point if it was previously suspended by a transition to HAVE_CURRENT_DATA.
  4. Abort these steps.
If buffered for at least one object in activeSourceBuffers contains a TimeRange that includes the current playback position but not enough data to ensure uninterrupted playback:
  1. Set the HTMLMediaElement.readyState attribute to HAVE_FUTURE_DATA.
  2. If the previous value of HTMLMediaElement.readyState was less than HAVE_FUTURE_DATA, then queue a task to fire a simple event named canplay at the media element.
  3. Playback may resume at this point if it was previously suspended by a transition to HAVE_CURRENT_DATA.
  4. Abort these steps.
If buffered for at least one object in activeSourceBuffers contains a TimeRange 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.readyState attribute to HAVE_CURRENT_DATA.
  2. If this is the first transition to HAVE_CURRENT_DATA, then queue a task to fire a simple event named loadeddata at the media element.
  3. Playback is suspended at this point since the media element doesn't have enough data to advance the timeline.
  4. Abort these steps.

3.4.5 Changes to selected/enabled track state

During playback activeSourceBuffers needs to be updated if the selected video track, the enabled audio tracks, or a text track mode changes. When one or more of these changes occur the following steps need to be followed.

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 activeSourceBuffers.
    2. Queue a task to fire a simple event named removesourcebuffer at activeSourceBuffers
  2. If the SourceBuffer associated with the newly selected video track is not already in activeSourceBuffers, run the following steps:
    1. Add the SourceBuffer to activeSourceBuffers.
    2. Queue a task to fire a simple event named addsourcebuffer at 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 activeSourceBuffers
  2. Queue a task to fire a simple event named removesourcebuffer at activeSourceBuffers
If an audio track becomes enabled and the SourceBuffer associated with this track is not already in activeSourceBuffers, then run the following steps:
  1. Add the SourceBuffer associated with the audio track to activeSourceBuffers
  2. Queue a task to fire a simple event named addsourcebuffer at activeSourceBuffers
If a text track mode 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 text track from activeSourceBuffers
  2. Queue a task to fire a simple event named removesourcebuffer at activeSourceBuffers
If a text track mode becomes "showing" or "hidden" and the SourceBuffer associated with this track is not already in activeSourceBuffers, then run the following steps:
  1. Add the SourceBuffer associated with the text track to activeSourceBuffers
  2. Queue a task to fire a simple event named addsourcebuffer at activeSourceBuffers

3.4.6 Duration change

Follow these steps when duration needs to change to a new duration.

  1. If the current value of duration is equal to new duration, then abort these steps.
  2. Set old duration to the current value of duration.
  3. Update duration to new duration.
  4. If the new duration is less than old duration, then call remove(new duration, old duration) on all objects in sourceBuffers.
    Note

    This preserves audio frames that start before and end after the duration. The user agent must end playback at duration even if the audio frame extends beyond this time.

  5. Update the media controller duration to new duration and run the HTMLMediaElement duration change algorithm.

4. SourceBuffer Object

Issue 3

Bug 20327 - Continuous splice flag

interface SourceBuffer : EventTarget {
    readonly attribute boolean        appending;
    readonly attribute TimeRanges     buffered;
             attribute double         timestampOffset;
    readonly attribute AudioTrackList audioTracks;
    readonly attribute VideoTrackList videoTracks;
    readonly attribute TextTrackList  textTracks;
    void appendArrayBuffer (ArrayBuffer data);
    void appendStream (Stream stream, optional unsigned long long maxSize);
    void abort ();
    void remove (double start, double end);
};

4.1 Attributes

appending of type boolean, readonly

Indicates whether an appendArrayBuffer() or appendStream() operation is still being processed.

audioTracks of type AudioTrackList, readonly
The list of AudioTrack objects created by this object.
buffered of type TimeRanges, readonly

Indicates what TimeRanges are buffered in the SourceBuffer.

When the attribute is read the following steps must occur:

  1. If this object has been removed from the sourceBuffers attribute of the parent media source then throw an INVALID_STATE_ERR exception and abort these steps.
  2. Return a new static normalized TimeRanges object for the media segments buffered.
textTracks of type TextTrackList, readonly
The list of TextTrack objects created by this object.
timestampOffset of type double

Controls the offset applied to timestamps inside subsequent media segments that are appended to this SourceBuffer. The 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. If this object has been removed from the sourceBuffers attribute of the parent media source, then throw an INVALID_STATE_ERR exception and abort these steps.
  2. If the readyState attribute of the parent media source is not in the "open" state, then throw an INVALID_STATE_ERR exception and abort these steps.
  3. If this object is waiting for the end of a media segment to be appended, then throw an INVALID_STATE_ERR and abort these steps.
  4. Update the attribute to the new value.
Issue 4

Bug 19676 - timestampOffset accuracy

videoTracks of type VideoTrackList, readonly
The list of VideoTrack objects created by this object.

4.2 Methods

abort

Aborts the current segment and resets the segment parser.

No parameters.
Return type: void

When this method is invoked, the user agent must run the following steps:

  1. If this object has been removed from the sourceBuffers attribute of the parent media source then throw an INVALID_STATE_ERR exception and abort these steps.
  2. If the readyState attribute of the parent media source is not in the "open" state then throw an INVALID_STATE_ERR exception and abort these steps.
  3. Run the reset parser state algorithm.
  4. If the appending attribute equals true, then run the following steps:
    1. Set the appending attribute to false.
    2. Queue a task to fire a simple event named abort at this SourceBuffer object.
    3. Queue a task to fire a simple event named appendend at this SourceBuffer object.
appendArrayBuffer

Appends the segment data in an ArrayBuffer to the source buffer.

ParameterTypeNullableOptionalDescription
dataArrayBuffer
Return type: void

When this method is invoked, the user agent must run the following steps:

  1. If data is null then throw an INVALID_ACCESS_ERR exception and abort these steps.
  2. If this object has been removed from the sourceBuffers attribute of the parent media source then throw an INVALID_STATE_ERR exception and abort these steps.
  3. If the appending attribute equals true, then throw an INVALID_STATE_ERR exception and abort these steps.
  4. If the readyState attribute of the parent media source is in the "closed" state then throw an INVALID_STATE_ERR exception and abort these steps.
  5. If the readyState attribute of the parent media source is in the "ended" state then run the following steps:

    1. Set the readyState attribute of the parent media source to "open"
    2. Queue a task to fire a simple event named sourceopen at the parent media source .
  6. If the buffer full flag equals true, then throw a QUOTA_EXCEEDED_ERR exception and abort these step.

    Note

    The web application must use remove() to free up space in the SourceBuffer.

  7. Add data to the end of the input buffer.
  8. Set the appending attribute to true.
  9. Queue a task to fire a simple event named appendstart at this SourceBuffer object.
  10. Asynchronously run the segment parser loop algorithm.
appendStream

Appends segment data to the source buffer from a Stream.

ParameterTypeNullableOptionalDescription
streamStream
maxSizeunsigned long long
Return type: void

When this method is invoked, the user agent must run the following steps:

  1. If stream is null then throw an INVALID_ACCESS_ERR exception and abort these steps.
  2. If this object has been removed from the sourceBuffers attribute of the parent media source then throw an INVALID_STATE_ERR exception and abort these steps.
  3. If the readyState attribute of the parent media source is in the "closed" state then throw an INVALID_STATE_ERR exception and abort these steps.
  4. If the appending attribute equals true, then throw an INVALID_STATE_ERR exception and abort these steps.
  5. If the readyState attribute of the parent media source is in the "ended" state then run the following steps:

    1. Set the readyState attribute of the parent media source to "open"
    2. Queue a task to fire a simple event named sourceopen at the parent media source .
  6. If the buffer full flag equals true, then throw a QUOTA_EXCEEDED_ERR exception and abort these step.

    Note

    The web application must use remove() to free up space in the SourceBuffer.

  7. Set the appending attribute to true.
  8. Queue a task to fire a simple event named appendstart at this SourceBuffer object.
  9. Asynchronously run the stream append loop algorithm with stream and maxSize.
remove

Removes media for a specific time range.

ParameterTypeNullableOptionalDescription
startdouble
enddouble
Return type: void

When this method is invoked, the user agent must run the following steps:

  1. If start is negative or greater than duration, then throw an INVALID_ACCESS_ERR exception and abort these steps.
  2. If end is less than or equal to start, then throw an INVALID_ACCESS_ERR exception and abort these steps.
  3. If this object has been removed from the sourceBuffers attribute of the parent media source then throw an INVALID_STATE_ERR exception and abort these steps.
  4. If the readyState attribute of the parent media source is not in the "open" state then throw an INVALID_STATE_ERR exception and abort these steps.
  5. For each track buffer in this source buffer, run the following steps:

    1. Let remove end timestamp be the current value of 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 timestamp.

      Note

      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.
    4. If this object is in activeSourceBuffers, the current playback position is greater than or equal to start and less than the remove end timestamp, and HTMLMediaElement.readyState is greater than HAVE_METADATA, then set the HTMLMediaElement.readyState attribute to HAVE_METADATA and stall playback.

      Note

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

  6. If buffer full flag equals true and this object is ready to accept more bytes, then set the buffer full flag to false.

4.3 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 append sequence. The variable is initially unset to indicate that no coded frames have been appended yet.

4.4 Event Summary

Event name Interface Dispatched when...
appendstart Event appending transitions from false to true.
appendend Event appending transitions from true to false.
error Event An error occurred during the append.
abort Event The append was aborted by an abort() call.

4.5 Algorithms

4.5.1 Segment Parser Loop

All SourceBuffer objects have an internal append state variable 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.

The input buffer is a byte buffer that is used to hold unparsed bytes across appendArrayBuffer() and appendStream() calls. The buffer is empty when the SourceBuffer object is created.

The buffer full flag keeps track of whether appendArrayBuffer() or appendStream() 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.

When this algorithm is invoked, run the following steps:

  1. Loop Top: If the input buffer is empty, then jump to the need more data step below.
  2. If the input buffer starts with bytes that violate the byte stream format specifications, then run the append error algorithm and abort this algorithm.
  3. Remove any bytes that the byte stream format specifications say should be ignored from the start of the input buffer.
  4. If the append state equals WAITING_FOR_SEGMENT, then run the following steps:

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

    1. If the 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 input buffer.
    4. Set append state to WAITING_FOR_SEGMENT.
    5. Jump to the loop top step above.
  6. If the append state equals PARSING_MEDIA_SEGMENT, then run the following steps:

    1. If the first initialization segment flag is false, then run the append error algorithm and abort this algorithm.
    2. If the input buffer does not contain a complete media segment header yet, then jump to the need more data step below.

      Note

      Implementations may choose to implement this state as an incremental parser so that it is not necessary to have the entire media segment before running the coded frame processing algorithm.

    3. Run the coded frame processing algorithm.
    4. Remove the media segment bytes from the beginning of the input buffer.
    5. If this SourceBuffer is full and cannot accept more media data, then set the buffer full flag to true.
    6. Set append state to WAITING_FOR_SEGMENT.

      Note

      Incremental parsers should only do this transition after the entire media segment has been received.

    7. Jump to the loop top step above.
  7. Need more data: If the stream append loop algorithm is running and still has data to read, then abort these steps.
  8. Set the appending attribute to false.
  9. Queue a task to fire a simple event named appendend at this SourceBuffer object.

4.5.2 Reset Parser State

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

  1. If the append state equals PARSING_MEDIA_SEGMENT and the input buffer contains some complete coded frames, then run the coded frame processing algorithm as if the media segment only contained these frames.
  2. Unset the last decode timestamp on all track buffers.
  3. Remove all bytes from the input buffer.
  4. Set append state to WAITING_FOR_SEGMENT.

4.5.3 Append Error

When an error occurs during an append, run the following steps:

  1. Run the reset parser state algorithm.
  2. Abort the stream append loop algorithm if it is running.
  3. Set the appending attribute to false.
  4. Queue a task to fire a simple event named error at this SourceBuffer object.
    Issue 5

    Need a way to convey error information.

  5. Queue a task to fire a simple event named appendend at this SourceBuffer object.

4.5.4 Stream Append Loop

When a Stream is passed to appendStream(), the following steps are run to transfer data from the Stream to the SourceBuffer. This algorithm is initialized with the stream and maxSize parameters from the appendStream() call.

  1. If maxSize is set, then let bytesLeft equal maxSize.
  2. Loop Top: If maxSize is set and bytesLeft equals 0, then jump to the loop done step below.
  3. If stream has been aborted, then run the append error algorithm and abort this algorithm.
  4. If stream has been closed, then jump to the loop done step below.
  5. If the buffer full flag equals true, then run the append error algorithm and abort this algorithm.

    Note

    The web application must use remove() to free up space in the SourceBuffer.

  6. Read data from stream into data:
    If maxSize is set:
    1. Read up to bytesLeft bytes from stream into data.
    2. Subtract the number of bytes in data from bytesLeft.
    Otherwise:
    Read all available bytes in stream into data.
  7. If an error occured while reading from stream, then run the append error algorithm and abort this algorithm.
  8. Add data to the end of the input buffer.
  9. Run the segment parser loop algorithm.
  10. Jump to the loop top step above.
  11. Loop Done: Set the appending attribute to false.
  12. Queue a task to fire a simple event named appendend at this SourceBuffer object.

4.5.5 Initialization Segment Received

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

Each SourceBuffer object has an internal first initialization segment flag that tracks whether the first initialization segment has been appended. This flag is set to false when the SourceBuffer is created and updated by the algorithm below.

  1. Update the duration attribute if it currently equals NaN:
    If the initialization segment contains a duration:
    Run the duration change algorithm with new duration 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 call endOfStream("decode") and abort these steps.
  3. If the first initialization segment flag is true, then run the following steps:
    1. Verify the following properties. If any of the checks fail then call endOfStream("decode") and abort these steps.
    2. Add the appropriate track descriptions from this initialization segment to each of the track buffers.
  4. Let active track flag equal false.
  5. If the first initialization segment flag is false, then run the following steps:

    1. For each audio track in the initialization segment, run following steps:

      1. Let new audio track be a new AudioTrack object.
      2. Generate a unique ID and assign it to the id property on new audio track.
      3. If audioTracks.length equals 0, then run the following steps:

        1. Set the enabled property on new audio track to true.
        2. Set active track flag to true.
      4. Add new audio track to the audioTracks attribute on this SourceBuffer object.
      5. Add new audio track to the audioTracks attribute on the HTMLMediaElement.
      6. Create a new track buffer to store coded frames for this track.
      7. Add the track description for this track to the track buffer.
    2. For each video track in the initialization segment, run following steps:

      1. Let new video track be a new VideoTrack object.
      2. Generate a unique ID and assign it to the id property on new video track.
      3. If videoTracks.length equals 0, then run the following steps:

        1. Set the selected property on new video track to true.
        2. Set active track flag to true.
      4. Add new video track to the videoTracks attribute on this SourceBuffer object.
      5. Add new video track to the videoTracks attribute on the HTMLMediaElement.
      6. Create a new track buffer to store coded frames for this track.
      7. Add the track description for this track to the track buffer.
    3. For each text track in the initialization segment, run following steps:

      1. Let new text track be a new TextTrack object with its properties populated with the appropriate information from the initialization segment.
      2. If the mode property on new text track equals "showing" or "hidden", then set active track flag to true.
      3. Add new text track to the textTracks attribute on this SourceBuffer object.
      4. Add new text track to the textTracks attribute on the HTMLMediaElement.
      5. Create a new track buffer to store coded frames for this track.
      6. Add the track description for this track to the track buffer.
    4. If active track flag equals true, then run the following steps:
      1. Add this SourceBuffer to activeSourceBuffers.
      2. Queue a task to fire a simple event named addsourcebuffer at activeSourceBuffers
    5. Set first initialization segment flag to true.
  6. If the HTMLMediaElement.readyState attribute is HAVE_NOTHING, then run the following steps:

    1. If one or more objects in sourceBuffers have first initialization segment flag set to false, then abort these steps.
    2. Set the HTMLMediaElement.readyState attribute to HAVE_METADATA.
    3. Queue a task to fire a simple event named loadedmetadata at the media element.
  7. If the active track flag equals true and the HTMLMediaElement.readyState attribute is greater than HAVE_CURRENT_DATA, then set the HTMLMediaElement.readyState attribute to HAVE_METADATA.

4.5.6 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. Let presentation timestamp be a double precision floating point representation of the coded frame's presentation timestamp.
    2. Let decode timestamp be a double precision floating point representation of the coded frame's decode timestamp.
    3. If timestampOffset is not 0, then run the following steps:

      1. Add timestampOffset to the presentation timestamp.
      2. Add timestampOffset to the decode timestamp.
      3. If the presentation timestamp or decode timestamp is less than the presentation start time, then call endOfStream("decode"), and abort these steps.
    4. Let track buffer equal the track buffer that the coded frame should be added to.
    5. If last decode timestamp for track buffer is set and decode timestamp is less than last decode timestamp, then call endOfStream("decode") and abort these steps.
    6. Add the coded frame with the presentation timestamp and decode timestamp, to the track buffer.
    7. Set last decode timestamp for track buffer to decode timestamp.
  2. If the HTMLMediaElement.readyState attribute is HAVE_METADATA and the new coded frames cause all objects in activeSourceBuffers to have media data for the current playback position, then run the following steps:

    1. Set the HTMLMediaElement.readyState attribute to HAVE_CURRENT_DATA.
    2. If this is the first transition to HAVE_CURRENT_DATA, then queue a task to fire a simple event named loadeddata at the media element.
  3. If the HTMLMediaElement.readyState attribute is HAVE_CURRENT_DATA and the new coded frames cause all objects in activeSourceBuffers to have media data beyond the current playback position, then run the following steps:

    1. Set the HTMLMediaElement.readyState attribute to HAVE_FUTURE_DATA.
    2. Queue a task to fire a simple event named canplay at the media element.
  4. If the HTMLMediaElement.readyState attribute is HAVE_FUTURE_DATA and the new coded frames cause all objects in activeSourceBuffers to have enough data to ensure uninterrupted playback, then run the following steps:

    1. Set the HTMLMediaElement.readyState attribute to HAVE_ENOUGH_DATA.
    2. Queue a task to fire a simple event named canplaythrough at the media element.
  5. If the media segment contains data beyond the current duration, then run the duration change algorithm with new duration set to the maximum of the current duration and the highest end timestamp reported by HTMLMediaElement.buffered.

5. 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.

interface SourceBufferList : EventTarget {
    readonly attribute unsigned long length;
    getter SourceBuffer (unsigned long index);
};

5.1 Attributes

length of type unsigned long, readonly

Indicates the number of SourceBuffer objects in the list.

5.2 Methods

SourceBuffer

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

ParameterTypeNullableOptionalDescription
indexunsigned long
Return type: getter

When this method is invoked, the user agent must run the following steps:

  1. If index is greater than or equal to the length attribute then return undefined and abort these steps.
  2. Return the index'th SourceBuffer object in the list.

5.3 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.

6. URL Object

partial interface URL {
    static DOMString createObjectURL (MediaSource mediaSource);
};

6.1 Methods

createObjectURL, static

Creates URLs for MediaSource objects.

Note

This algorithm is intended to mirror the behavior of the File API createObjectURL() method with autoRevoke set to true.

ParameterTypeNullableOptionalDescription
mediaSourceMediaSource
Return type: DOMString

When this method is invoked, the user agent must run the following steps:

  1. If mediaSource is NULL the return null.
  2. Return a unique MediaSource object URL that can be used to dereference the mediaSource argument, and run the rest of the algorithm asynchronously.
  3. provide a stable state
  4. Revoke the MediaSource object URL by calling revokeObjectURL() on it.

7. HTMLMediaElement attributes

This section specifies what existing attributes on the HTMLMediaElement should return when a MediaSource is attached to the element.

The HTMLMediaElement.seekable attribute returns a new static normalized TimeRanges object created based on the following steps:

If duration equals NaN
Return an empty TimeRanges object.
If duration equals positive Infinity
Return a single range with a start time of 0 and an end time equal to the highest end time reported by the HTMLMediaElement.buffered attribute.
Otherwise
Return a single range with a start time of 0 and an end time equal to duration.

The HTMLMediaElement.buffered attribute returns a new static normalized TimeRanges object created based on the following steps:

  1. If activeSourceBuffers.length equals 0 then return an empty TimeRanges object and abort these steps.
  2. Let active ranges be the ranges returned by buffered for each SourceBuffer object in activeSourceBuffers.
  3. Let highest end time be the largest range end time in the active ranges.
  4. Let intersection ranges equal a TimeRange object containing a single range from 0 to highest end time.
  5. For each SourceBuffer object in activeSourceBuffers run the following steps:
    1. Let source ranges equal the ranges returned by the buffered attribute on the current SourceBuffer.
    2. If readyState is "ended", then set the end time on the last range in source ranges to highest end time.
    3. Let new intersection ranges equal the the intersection between the intersection ranges and the source ranges.
    4. Replace the ranges in intersection ranges with the new intersection ranges.
  6. Return the intersection ranges.
Issue 6

Bug 18615 - Define how SourceBuffer.buffered maps to HTMLMediaElement.buffered

Issue 7

Bug 18400 - Define and document timestamp heuristics

8. AudioTrack Extensions

This section specifies extensions to the HTML AudioTrack definition.

partial interface AudioTrack {
             attribute DOMString     kind;
             attribute DOMString     language;
    readonly attribute SourceBuffer? sourceBuffer;
};

8.1 Attributes

kind of type DOMString

Allows the web application to get and update the track kind.

On getting, return the current value of the attribute. This is either the value provided when this object was created or the value provided on the last successful set operation.

On setting, run the following steps:

  1. If the value being assigned to this attribute does not match one of the kind categories, then abort these steps.
  2. Update this attribute to the new value.
  3. If the sourceBuffer attribute on this track is not null, then queue a task to fire a simple event named change at sourceBuffer.audioTracks.
  4. Queue a task to fire a simple event named change at the audioTracks attribute on the HTMLMediaElement.
language of type DOMString

Allows the web application to get and update the track language.

On getting, return the current value of the attribute. This is either the value provided when this object was created or the value provided on the last successful set operation.

On setting, run the following steps:

  1. If the value being assigned to this attribute is not an empty string or a BCP 47 language tag[BCP47], then abort these steps.
  2. Update this attribute to the new value.
  3. If the sourceBuffer attribute on this track is not null, then queue a task to fire a simple event named change at sourceBuffer.audioTracks.
  4. Queue a task to fire a simple event named change at the audioTracks attribute on the HTMLMediaElement.
sourceBuffer of type SourceBuffer, readonly, nullable

Returns the SourceBuffer that created this track. Returns null if this track was not created by a SourceBuffer or the SourceBuffer has been removed from the sourceBuffers attribute of its parent media source.

9. VideoTrack Extensions

This section specifies extensions to the HTML VideoTrack definition.

partial interface VideoTrack {
             attribute DOMString     kind;
             attribute DOMString     language;
    readonly attribute SourceBuffer? sourceBuffer;
};

9.1 Attributes

kind of type DOMString

Allows the web application to get and update the track kind.

On getting, return the current value of the attribute. This is either the value provided when this object was created or the value provided on the last successful set operation.

On setting, run the following steps:

  1. If the value being assigned to this attribute does not match one of the kind categories, then abort these steps.
  2. Update this attribute to the new value.
  3. If the sourceBuffer attribute on this track is not null, then queue a task to fire a simple event named change at sourceBuffer.videoTracks.
  4. Queue a task to fire a simple event named change at the videoTracks attribute on the HTMLMediaElement.
language of type DOMString

Allows the web application to get and update the track language.

On getting, return the current value of the attribute. This is either the value provided when this object was created or the value provided on the last successful set operation.

On setting, run the following steps:

  1. If the value being assigned to this attribute is not an empty string or a BCP 47 language tag[BCP47], then abort these steps.
  2. Update this attribute to the new value.
  3. If the sourceBuffer attribute on this track is not null, then queue a task to fire a simple event named change at sourceBuffer.videoTracks.
  4. Queue a task to fire a simple event named change at the videoTracks attribute on the HTMLMediaElement.
sourceBuffer of type SourceBuffer, readonly, nullable

Returns the SourceBuffer that created this track. Returns null if this track was not created by a SourceBuffer or the SourceBuffer has been removed from the sourceBuffers attribute of its parent media source.

10. TextTrack Extensions

This section specifies extensions to the HTML TextTrack definition.

partial interface TextTrack {
             attribute DOMString     kind;
             attribute DOMString     language;
    readonly attribute SourceBuffer? sourceBuffer;
};

10.1 Attributes

kind of type DOMString

Allows the web application to get and update the track kind.

On getting, return the current value of the attribute. This is either the value provided when this object was created or the value provided on the last successful set operation.

On setting, run the following steps:

  1. If the value being assigned to this attribute does not match one of the text track kinds, then abort these steps.
  2. Update this attribute to the new value.
  3. If the sourceBuffer attribute on this track is not null, then queue a task to fire a simple event named change at sourceBuffer.textTracks.
  4. Queue a task to fire a simple event named change at the textTracks attribute on the HTMLMediaElement.
language of type DOMString

Allows the web application to get and update the track language.

On getting, return the current value of the attribute. This is either the value provided when this object was created or the value provided on the last successful set operation.

On setting, run the following steps:

  1. If the value being assigned to this attribute is not an valid text track language, then abort these steps.
  2. Update this attribute to the new value.
  3. If the sourceBuffer attribute on this track is not null, then queue a task to fire a simple event named change at sourceBuffer.textTracks.
  4. Queue a task to fire a simple event named change at the textTracks attribute on the HTMLMediaElement.
sourceBuffer of type SourceBuffer, readonly, nullable

Returns the SourceBuffer that created this track. Returns null if this track was not created by a SourceBuffer or the SourceBuffer has been removed from the sourceBuffers attribute of its parent media source.

11. Byte Stream Formats

The bytes provided through appendArrayBuffer() and appendStream() for a SourceBuffer form a logical byte stream. The format of this byte stream depends on the media container format in use and is defined in a byte stream format specification. Byte stream format specifications based on WebM , the ISO Base Media File Format, and MPEG-2 Transport Streams are provided below. These format specifications are intended to be the authoritative source for how data from these containers is formatted and passed to a SourceBuffer. If a MediaSource implementation claims to support any of these container formats, then it must implement the corresponding byte stream format specification described below.

This section provides general requirements for all byte stream formats:

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.

11.1 WebM Byte Streams

This section defines segment formats for implementations that choose to support WebM.

11.1.1 Initialization Segments

A WebM initialization segment must contain a subset of the elements at the start of a typical WebM file.

The following rules apply to WebM initialization segments:

  1. The initialization segment must start with an EBML Header element, followed by a Segment header.
  2. The size value in the Segment header must signal an "unknown size" or contain a value large enough to include the Segment Information and Tracks elements that follow.
  3. A Segment Information element and a Tracks element must appear, in that order, after the Segment header and before any further EBML Header or Cluster elements.
  4. Any elements other than an EBML Header or a Cluster that occur before, in between, or after the Segment Information and Tracks elements are ignored.

11.1.2 Media Segments

A WebM media segment is a single Cluster element.

The following rules apply to WebM media segments:

  1. The Timecode element in the Cluster contains a presentation timestamp in TimecodeScale units.
  2. The TimecodeScale in the WebM initialization segment most recently appended applies to all timestamps in the Cluster
  3. The Cluster header may contain an "unknown" size value. If it does then the end of the cluster is reached when another Cluster header or an element header that indicates the start of an WebM initialization segment is encountered.
  4. Block & SimpleBlock elements must be in time increasing order consistent with the WebM spec.
  5. If the most recent WebM initialization segment describes multiple tracks, then blocks from all the tracks must be interleaved in time increasing order. At least one block from all audio and video tracks must be present.
  6. Cues or Chapters elements may follow a Cluster element. These elements must be accepted and ignored by the user agent.

11.1.3 Random Access Points

A SimpleBlock element with its Keyframe flag set signals the location of a random access point for that track. Media segments containing multiple tracks are only considered a random access point if the first SimpleBlock for each track has its Keyframe flag set. The order of the multiplexed blocks must conform to the WebM Muxer Guidelines.

11.2 ISO Base Media File Format Byte Streams

This section defines segment formats for implementations that choose to support the ISO Base Media File Format ISO/IEC 14496-12 (ISO BMFF).

Issue 8

Bug 18933 - Segment byte boundaries are not defined

11.2.1 Initialization Segments

An ISO BMFF initialization segment must contain a single Movie Header Box (moov). The tracks in the Movie Header Box must not contain any samples (i.e. the entry_count in the stts, stsc and stco boxes must be set to zero). A Movie Extends (mvex) box must be contained in the Movie Header Box to indicate that Movie Fragments are to be expected.

The initialization segment may contain Edit Boxes (edts) which provide a mapping of composition times for each track to the global presentation time.

11.2.2 Media Segments

An ISO BMFF media segment must contain a single Movie Fragment Box (moof) followed by one or more Media Data Boxes (mdat).

The following rules apply to ISO BMFF media segments:

  1. The Movie Fragment Box must contain at least one Track Fragment Box (traf).
  2. The Movie Fragment Box must use movie-fragment relative addressing and the flag default-base-is-moof must be set; absolute byte-offsets must not be used.
  3. External data references must not be used.
  4. If the Movie Fragment contains multiple tracks, the duration by which each track extends should be as close to equal as practical.
  5. Each Track Fragment Box must contain a Track Fragment Decode Time Box (tfdt)
  6. The first sample in each Track Fragment Run Box (trun) must indicate that the sample is a random access point.
  7. The Media Data Boxes must contain all the samples referenced by the Track Fragment Run Boxes (trun) of the Movie Fragment Box.

11.2.3 Random Access Points

A random access point as defined in this specification corresponds to a Stream Access Point of type 1 or 2 as defined in Annex I of ISO/IEC 14496-12.

11.3 MPEG-2 Transport Stream Byte Streams

This section defines segment formats for implementations that choose to support MPEG-2 Transport Streams (MPEG-2 TS) specified in ISO/IEC 13818-1.

11.3.1 General

MPEG-2 TS media and initialization segments must conform to the MPEG-2 TS Adaptive Profile (ISO/IEC 13818-1:2012 Amd. 2).

The following rules must apply to all MPEG-2 TS segments:

  1. Segments must contain complete MPEG-2 TS packets.
  2. Segments must contain only complete PES packets and sections.
  3. Segments must contain exactly one program.
  4. All MPEG-2 TS packets must have the transport_error_indicator set to 0

11.3.2 Initialization Segments

An MPEG-2 TS initialization segment must contain a single PAT and a single PMT. Other SI, such as CAT, that are invariant for all subsequent media segments, may be present.

11.3.3 Media Segments

The following rules apply to all MPEG-2 TS media segments:

  1. PSI that is identical to the information in the initialization segment may appear repeatedly throughout the segment.
  2. The media segment will not rely on initialization information in another media segment.
  3. Media Segments must contain only complete PES packets and sections.
  4. Each PES packet must be comprised of one or more complete access units.
  5. Each PES packet must have a PTS timestamp.
  6. PCR must be present in the Segment prior to the first byte of a TS packet payload containing media data.
  7. The presentation duration of each media component within the Media Segment should be as close to equal as practical.

11.3.4 Random Access Points

A random access point as defined in this specification corresponds to Elementary Stream Random Access Point as defined in ISO/IEC 13818-1.

11.3.5 Timestamp Rollover & Discontinuities

Timestamp rollovers and discontinuities must be handled by the UA. The UA's MPEG-2 TS implementation must maintain an internal offset variable, MPEG2TS_timestampOffset, to keep track of the offset that needs to be applied to timestamps that have rolled over or are part of a discontinuity. MPEG2TS_timestampOffset is initially set to 0 when the SourceBuffer is created. This offset must be applied to the timestamps as part of the conversion process from MPEG-2 TS packets into coded frames for the coded frame processing algorithm. This results in the coded frame timestamps for a packet being computed by the following equations:

Coded Frame Presentation Timestamp = (MPEG-2 TS presentation timestamp) + MPEG2TS_timestampOffset
Coded Frame Decode Timestamp = (MPEG-2 TS decode timestamp) + MPEG2TS_timestampOffset

MPEG2TS_timestampOffset is updated in the following ways:

  • Each time a timestamp rollover is detected, 2^33 must be added to MPEG2TS_timestampOffset.
  • When a discontinuity is detected, MPEG2TS_timestampOffset must be adjusted to make the timestamps after the discontinuity appear to come immediately after the timestamps before the discontinuity.
  • When abort() is called, MPEG2TS_timestampOffset must be set to 0.
  • When timestampOffset is successfully set, MPEG2TS_timestampOffset must be set to 0.

12. Examples

Example use of the Media Source Extensions

<script>
  function onSourceOpen(videoTag, e) {
    var mediaSource = e.target;
    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('appendend', firstAppendHandler);

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

  function appendNextMediaSegment(mediaSource) {
    if (mediaSource.readyState == "ended")
      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].appending)
        return;

    var mediaSegment = GetNextMediaSegment();

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

    mediaSource.sourceBuffers[0].appendArrayBuffer(mediaSegment);
  }

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

    // 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>
          

13. Revision History

Version Comment
30 January 2013
  • Remove early abort step on 0-byte appends so the same events fire as a normal append with bytes.
  • Added definition for 'enough data to ensure uninterrupted playback'.
  • Updated buffered ranges algorithm to properly compute the ranges for Philip's example.
15 January 2013 Replace setTrackInfo() and getSourceBuffer() with AudioTrack, VideoTrack, and TextTrack extensions.
04 January 2013
  • Renamed append() to appendArrayBuffer() and made appending asynchronous.
  • Added SourceBuffer.appendStream().
  • Added SourceBuffer.setTrackInfo() methods.
  • Added issue boxes to relevant sections for outstanding bugs.
14 December 2012 Pubrules, Link Checker, and Markup Validation fixes.
13 December 2012
  • Added MPEG-2 Transport Stream section.
  • Added text to require abort() for out-of-order appends.
  • Renamed "track buffer" to "decoder buffer".
  • Redefined "track buffer" to mean the per-track buffers that hold the SourceBuffer media data.
  • Editorial fixes.
08 December 2012
  • Added MediaSource.getSourceBuffer() methods.
  • Section 2 cleanup.
06 December 2012
  • append() now throws a QUOTA_EXCEEDED_ERR when the SourceBuffer is full.
  • Added unique ID generation text to Initialization Segment Received algorithm.
  • Remove 2.x subsections that are already covered by algorithm text.
  • Rework byte stream format text so it doesn't imply that the MediaSource implementation must support all formats supported by the HTMLMediaElement.
28 November 2012
  • Added transition to HAVE_METADATA when current playback position is removed.
  • Added remove() calls to duration change algorithm.
  • Added MediaSource.isTypeSupported() method.
  • Remove initialization segments are optional text.
09 November 2012 Converted document to ReSpec.
18 October 2012 Refactored SourceBuffer.append() & added SourceBuffer.remove().
8 October 2012
  • Defined what HTMLMediaElement.seekable and HTMLMediaElement.buffered should return.
  • Updated seeking algorithm to run inside Step 10 of the HTMLMediaElement seeking algorithm.
  • Removed transition from "ended" to "open" in the seeking algorithm.
  • Clarified all the event targets.
1 October 2012 Fixed various addsourcebuffer & removesourcebuffer bugs and allow append() in ended state.
13 September 2012 Updated endOfStream() behavior to change based on the value of HTMLMediaElement.readyState.
24 August 2012
  • Added early abort on to duration change algorithm.
  • Added createObjectURL() IDL & algorithm.
  • Added Track ID & Track description definitions.
  • Rewrote start overlap for audio frames text.
  • Removed rendering silence requirement from section 2.5.
22 August 2012
  • Clarified WebM byte stream requirements.
  • Clarified SourceBuffer.buffered return value.
  • Clarified addsourcebuffer & removesourcebuffer event targets.
  • Clarified when media source attaches to the HTMLMediaElement.
  • Introduced duration change algorithm and update relevant algorithms to use it.
17 August 2012 Minor editorial fixes.
09 August 2012 Change presentation start time to always be 0 instead of using format specific rules about the first media segment appended.
30 July 2012 Added SourceBuffer.timestampOffset and MediaSource.duration.
17 July 2012 Replaced SourceBufferList.remove() with MediaSource.removeSourceBuffer().
02 July 2012 Converted to the object-oriented API
26 June 2012 Converted to Editor's draft.
0.5 Minor updates before proposing to W3C HTML-WG.
0.4 Major revision. Adding source IDs, defining buffer model, and clarifying byte stream formats.
0.3 Minor text updates.
0.2 Updates to reflect initial WebKit implementation.
0.1 Initial Proposal

A. References

A.1 Informative references

[BCP47]
A. Phillips; M. Davis. Tags for Identifying Languages September 2009. IETF Best Current Practice. URL: http://tools.ietf.org/html/bcp47