1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
11 * @param {HTMLMediaElement} videoTag <video> tag to render to.
14 remoting.MediaSourceRenderer = function(videoTag) {
15 /** @type {HTMLMediaElement} */
16 this.video_ = videoTag;
18 /** @type {MediaSource} */
19 this.mediaSource_ = null;
21 /** @type {SourceBuffer} */
22 this.sourceBuffer_ = null;
24 /** @type {!Array.<ArrayBuffer>} Queue of pending buffers that haven't been
30 * @param {string} format Format of the stream.
32 remoting.MediaSourceRenderer.prototype.reset = function(format) {
33 // Create a new MediaSource instance.
34 this.sourceBuffer_ = null;
35 this.mediaSource_ = new MediaSource();
36 this.mediaSource_.addEventListener('sourceopen',
37 this.onSourceOpen_.bind(this, format));
38 this.mediaSource_.addEventListener('sourceclose', function(e) {
39 console.error("MediaSource closed unexpectedly.");
41 this.mediaSource_.addEventListener('sourceended', function(e) {
42 console.error("MediaSource ended unexpectedly.");
45 // Start playback from new MediaSource.
47 /** @type {string} */(
48 window.URL.createObjectURL(/** @type {!Blob} */(this.mediaSource_)));
53 * @param {string} format
56 remoting.MediaSourceRenderer.prototype.onSourceOpen_ = function(format) {
58 this.mediaSource_.addSourceBuffer(format);
60 this.sourceBuffer_.addEventListener(
61 'updateend', this.processPendingData_.bind(this));
62 this.processPendingData_();
68 remoting.MediaSourceRenderer.prototype.processPendingData_ = function() {
69 if (this.sourceBuffer_) {
70 while (this.buffers_.length > 0 && !this.sourceBuffer_.updating) {
71 // TODO(sergeyu): Figure out the way to determine when a frame is rendered
72 // and use it to report performance statistics.
73 this.sourceBuffer_.appendBuffer(
74 /** @type {ArrayBuffer} */(this.buffers_.shift()));
80 * @param {ArrayBuffer} data
82 remoting.MediaSourceRenderer.prototype.onIncomingData = function(data) {
83 this.buffers_.push(data);
84 this.processPendingData_();