Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 927229 of SourceBuffer

  • Revision slug: Web/API/SourceBuffer
  • Revision title: SourceBuffer
  • Revision id: 927229
  • Created:
  • Creator: chrisdavidmills
  • Is current revision? No
  • Comment

Revision Content

{{APIRef("Media Source Extensions")}}{{SeeCompatTable}}{{draft}}

The SourceBuffer interface represents a chunk of media to be passed into an {{domxref("HTMLMediaElement")}} and played, via a MediaSource object. This can be made up of one or several media segments.

Properties

Inherits properties from its parent interface, {{domxref("EventTarget")}}.

{{domxref("SourceBuffer.mode")}}
Controls how the order of media segments in the SourceBuffer is handled, in terms of whether they can be appended in any order, or they have to be kept in a strict sequence.
{{domxref("SourceBuffer.updating")}} {{readonlyInline}}
Indicates whether the SourceBuffer is currently being updated — i.e. whether an {{domxref("SourceBuffer.appendBuffer()")}}, {{domxref("SourceBuffer.appendStream()")}}, or {{domxref("SourceBuffer.remove()")}} operation is currently in progress.
{{domxref("SourceBuffer.buffered")}} {{readonlyInline}}
Returns the time ranges that are currently buffered in the SourceBuffer.
{{domxref("SourceBuffer.timestampOffset")}}
Controls the offset applied to timestamps inside media segments that are subsequently appended to the SourceBuffer.
{{domxref("SourceBuffer.audioTracks")}} {{readonlyInline}}
A list of the audio tracks currently contained inside the SourceBuffer.
{{domxref("SourceBuffer.videoTracks")}} {{readonlyInline}}
A list of the video tracks currently contained inside the SourceBuffer.
{{domxref("SourceBuffer.textTracks")}} {{readonlyInline}}
A list of the text tracks currently contained inside the SourceBuffer.
{{domxref("SourceBuffer.appendWindowStart")}}
Controls the timestamp for the start of the append window. This is a timestamp range that can be used to filter what media data is appended to the SourceBuffer. Coded media frames with timestamps wthin this range will be appended, whereas those outside the range will be filtered out.
{{domxref("SourceBuffer.appendWindowEnd")}}
Controls the timestamp for the end of the append window.
{{domxref("SourceBuffer.trackDefaults")}}
Specifies the default values to use if kind, label, and/or language information is not available in the initialization segment of the media to be appended to the SourceBuffer.

Methods

Inherits properties from its parent interface, {{domxref("EventTarget")}}.

{{domxref("SourceBuffer.appendBuffer()")}}
Appends media segment data from an {{domxref("ArrayBuffer")}} or ArrayBufferView object to the SourceBuffer.
{{domxref("SourceBuffer.appendStream()")}}
Appends media segment data from a ReadableStream object to the SourceBuffer.
{{domxref("SourceBuffer.abort()")}}
Aborts the current segment and resets the segment parser.
{{domxref("SourceBuffer.remove()")}}
Removes media segments within a specific time range from the SourceBuffer.

Examples

The following simple example loads a video chunk by chunk as fast as possible, playing it as soon as it can. This example was written by Nick Desaulniers and can be viewed live here (you can also download the source for further investigation.)

var video = document.querySelector('video');

var assetURL = 'frag_bunny.mp4';
// Need to be specific for Blink regarding codecs
// ./mp4info frag_bunny.mp4 | grep Codec
var mimeCodec = 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"';

if ('MediaSource' in window && MediaSource.isTypeSupported(mimeCodec)) {
  var mediaSource = new MediaSource;
  //console.log(mediaSource.readyState); // closed
  video.src = URL.createObjectURL(mediaSource);
  mediaSource.addEventListener('sourceopen', sourceOpen);
} else {
  console.error('Unsupported MIME type or codec: ', mimeCodec);
}

function sourceOpen (_) {
  //console.log(this.readyState); // open
  var mediaSource = this;
  var sourceBuffer = mediaSource.addSourceBuffer(mimeCodec);
  fetchAB(assetURL, function (buf) {
    sourceBuffer.addEventListener('updateend', function (_) {
      mediaSource.endOfStream();
      video.play();
      //console.log(mediaSource.readyState); // ended
    });
    sourceBuffer.appendBuffer(buf);
  });
};

function fetchAB (url, cb) {
  console.log(url);
  var xhr = new XMLHttpRequest;
  xhr.open('get', url);
  xhr.responseType = 'arraybuffer';
  xhr.onload = function () {
    cb(xhr.response);
  };
  xhr.send();
};

Specifications

Specification Status Comment
{{SpecName('Media Source Extensions', '#sourcebuffer', 'SourceBuffer')}} {{Spec2('Media Source Extensions')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 23 {{CompatGeckoDesktop("25.0")}}[1]
{{CompatGeckoDesktop("42.0")}}
11[2] 15 8
Feature Android Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support 4.4.4

{{CompatNo}}

{{CompatNo}} 11 30 {{CompatNo}}

[1] Available after switching the about:config preference media.mediasource.enabled to true. In addition, support was limited to a whitelist of sites, for example YouTube, Netflix, and other popular streaming sites. This was removed in 42+.

[2] Only works on Windows 8+.

See also

  • {{domxref("MediaSource")}}
  • {{domxref("SourceBufferList")}}

Revision Source

<p>{{APIRef("Media Source Extensions")}}{{SeeCompatTable}}{{draft}}</p>

<p>The <strong><code>SourceBuffer</code></strong> interface represents a chunk of media to be passed into an {{domxref("HTMLMediaElement")}} and played, via a <code>MediaSource</code> object. This can be made up of one or several media segments.</p>

<h2 id="Properties">Properties</h2>

<p><em>Inherits properties from its parent interface, {{domxref("EventTarget")}}.</em></p>

<dl>
 <dt>{{domxref("SourceBuffer.mode")}}</dt>
 <dd>Controls how the order of media segments in the <code>SourceBuffer</code> is handled, in terms of whether they can be appended in any order, or they have to be kept in a strict sequence.</dd>
 <dt>{{domxref("SourceBuffer.updating")}} {{readonlyInline}}</dt>
 <dd>Indicates whether the <code>SourceBuffer</code> is currently being updated — i.e. whether an {{domxref("SourceBuffer.appendBuffer()")}}, {{domxref("SourceBuffer.appendStream()")}}, or {{domxref("SourceBuffer.remove()")}} operation is currently in progress.</dd>
 <dt>{{domxref("SourceBuffer.buffered")}} {{readonlyInline}}</dt>
 <dd>Returns the time ranges that are currently buffered in the <code>SourceBuffer</code>.</dd>
 <dt>{{domxref("SourceBuffer.timestampOffset")}}</dt>
 <dd>Controls the offset applied to timestamps inside media segments that are subsequently appended to the <code>SourceBuffer</code>.</dd>
 <dt>{{domxref("SourceBuffer.audioTracks")}} {{readonlyInline}}</dt>
 <dd>A list of the audio tracks currently contained inside the <code>SourceBuffer</code>.</dd>
 <dt>{{domxref("SourceBuffer.videoTracks")}} {{readonlyInline}}</dt>
 <dd>A list of the video tracks currently contained inside the <code>SourceBuffer</code>.</dd>
 <dt>{{domxref("SourceBuffer.textTracks")}} {{readonlyInline}}</dt>
 <dd>A list of the text tracks currently contained inside the <code>SourceBuffer</code>.</dd>
 <dt>{{domxref("SourceBuffer.appendWindowStart")}}</dt>
 <dd>Controls the timestamp for the start of the <a href="https://w3c.github.io/media-source/#append-window">append window</a>. This is a timestamp range that can be used to filter what media data is appended to the <code>SourceBuffer</code>. Coded media frames with timestamps wthin this range will be appended, whereas those outside the range will be filtered out.</dd>
 <dt>{{domxref("SourceBuffer.appendWindowEnd")}}</dt>
 <dd>Controls the timestamp for the end of the append window.</dd>
 <dt>{{domxref("SourceBuffer.trackDefaults")}}</dt>
 <dd>Specifies the default values to use if kind, label, and/or language information is not available in the <a href="https://w3c.github.io/media-source/#init-segment">initialization segment</a> of the media to be appended to the <code>SourceBuffer</code>.</dd>
</dl>

<dl>
</dl>

<dl>
</dl>

<h2 id="Methods">Methods</h2>

<p><em>Inherits properties from its parent interface, {{domxref("EventTarget")}}.</em></p>

<dl>
 <dt>{{domxref("SourceBuffer.appendBuffer()")}}</dt>
 <dd>Appends media segment data from an {{domxref("ArrayBuffer")}} or <code>ArrayBufferView</code> object to the <code>SourceBuffer</code>.</dd>
 <dt>{{domxref("SourceBuffer.appendStream()")}}</dt>
 <dd>Appends media segment data from a <code>ReadableStream</code> object to the <code>SourceBuffer</code>.</dd>
 <dt>{{domxref("SourceBuffer.abort()")}}</dt>
 <dd>Aborts the current segment and resets the segment parser.</dd>
 <dt>{{domxref("SourceBuffer.remove()")}}</dt>
 <dd>Removes media segments within a specific time range from the <code>SourceBuffer</code>.</dd>
</dl>

<h2 id="Examples">Examples</h2>

<p>The following simple example loads a video chunk by chunk as fast as possible, playing it as soon as it can. This example was written by Nick Desaulniers and can be <a href="https://nickdesaulniers.github.io/netfix/demo/bufferAll.html">viewed live here</a> (you can also <a href="https://github.com/nickdesaulniers/netfix/blob/gh-pages/demo/bufferAll.html">download the source</a> for further investigation.)</p>

<pre class="brush: js">
var video = document.querySelector('video');

var assetURL = 'frag_bunny.mp4';
// Need to be specific for Blink regarding codecs
// ./mp4info frag_bunny.mp4 | grep Codec
var mimeCodec = 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"';

if ('MediaSource' in window &amp;&amp; MediaSource.isTypeSupported(mimeCodec)) {
  var mediaSource = new MediaSource;
  //console.log(mediaSource.readyState); // closed
  video.src = URL.createObjectURL(mediaSource);
  mediaSource.addEventListener('sourceopen', sourceOpen);
} else {
  console.error('Unsupported MIME type or codec: ', mimeCodec);
}

function sourceOpen (_) {
  //console.log(this.readyState); // open
  var mediaSource = this;
  var sourceBuffer = mediaSource.addSourceBuffer(mimeCodec);
  fetchAB(assetURL, function (buf) {
    sourceBuffer.addEventListener('updateend', function (_) {
      mediaSource.endOfStream();
      video.play();
      //console.log(mediaSource.readyState); // ended
    });
    sourceBuffer.appendBuffer(buf);
  });
};

function fetchAB (url, cb) {
  console.log(url);
  var xhr = new XMLHttpRequest;
  xhr.open('get', url);
  xhr.responseType = 'arraybuffer';
  xhr.onload = function () {
    cb(xhr.response);
  };
  xhr.send();
};</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('Media Source Extensions', '#sourcebuffer', 'SourceBuffer')}}</td>
   <td>{{Spec2('Media Source Extensions')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>23</td>
   <td>{{CompatGeckoDesktop("25.0")}}<sup>[1]</sup><br />
    {{CompatGeckoDesktop("42.0")}}</td>
   <td>11<sup>[2]</sup></td>
   <td>15</td>
   <td>8</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>Firefox OS (Gecko)</th>
   <th>IE Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>4.4.4</td>
   <td>
    <p>{{CompatNo}}</p>
   </td>
   <td>{{CompatNo}}</td>
   <td>11</td>
   <td>30</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] Available after switching the <code>about:config</code> preference <code>media.mediasource.enabled</code> to <code>true</code>. In addition, support was limited to a whitelist of sites, for example YouTube, Netflix, and other popular streaming sites. This was removed in 42+.</p>

<p>[2] Only works on Windows 8+.</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{domxref("MediaSource")}}</li>
 <li>{{domxref("SourceBufferList")}}</li>
</ul>
Revert to this revision