Reword the bubble title for unsupported (non-cws) extensions.
[chromium-blink-merge.git] / remoting / webapp / media_source_renderer.js
bloba79173b01e79302d5e5c88a3de44d360e03154a3
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.
5 'use strict';
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
10 /**
11  * @param {HTMLMediaElement} videoTag <video> tag to render to.
12  * @constructor
13  */
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
25    * processed . */
26   this.buffers_ = [];
29 /**
30  * @param {string} format Format of the stream.
31  */
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.");
40   });
41   this.mediaSource_.addEventListener('sourceended', function(e) {
42     console.error("MediaSource ended unexpectedly.");
43   });
45   // Start playback from new MediaSource.
46   this.video_.src =
47       /** @type {string} */(
48           window.URL.createObjectURL(/** @type {!Blob} */(this.mediaSource_)));
49   this.video_.play();
52 /**
53  * @param {string} format
54  * @private
55  */
56 remoting.MediaSourceRenderer.prototype.onSourceOpen_ = function(format) {
57   this.sourceBuffer_ =
58       this.mediaSource_.addSourceBuffer(format);
60   this.sourceBuffer_.addEventListener(
61       'updateend', this.processPendingData_.bind(this));
62   this.processPendingData_();
65 /**
66  * @private
67  */
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()));
75     }
76   }
79 /**
80  * @param {ArrayBuffer} data
81  */
82 remoting.MediaSourceRenderer.prototype.onIncomingData = function(data) {
83   this.buffers_.push(data);
84   this.processPendingData_();