UNPKG

6.34 kBJavaScriptView Raw
1/**
2 * @file source-updater.js
3 */
4'use strict';
5
6Object.defineProperty(exports, '__esModule', {
7 value: true
8});
9
10var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
11
12function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
13
14function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
15
16var _videoJs = require('video.js');
17
18var _videoJs2 = _interopRequireDefault(_videoJs);
19
20var noop = function noop() {};
21
22/**
23 * A queue of callbacks to be serialized and applied when a
24 * MediaSource and its associated SourceBuffers are not in the
25 * updating state. It is used by the segment loader to update the
26 * underlying SourceBuffers when new data is loaded, for instance.
27 *
28 * @class SourceUpdater
29 * @param {MediaSource} mediaSource the MediaSource to create the
30 * SourceBuffer from
31 * @param {String} mimeType the desired MIME type of the underlying
32 * SourceBuffer
33 */
34
35var SourceUpdater = (function () {
36 function SourceUpdater(mediaSource, mimeType) {
37 var _this = this;
38
39 _classCallCheck(this, SourceUpdater);
40
41 var createSourceBuffer = function createSourceBuffer() {
42 _this.sourceBuffer_ = mediaSource.addSourceBuffer(mimeType);
43
44 // run completion handlers and process callbacks as updateend
45 // events fire
46 _this.onUpdateendCallback_ = function () {
47 var pendingCallback = _this.pendingCallback_;
48
49 _this.pendingCallback_ = null;
50
51 if (pendingCallback) {
52 pendingCallback();
53 }
54
55 _this.runCallback_();
56 };
57
58 _this.sourceBuffer_.addEventListener('updateend', _this.onUpdateendCallback_);
59
60 _this.runCallback_();
61 };
62
63 this.callbacks_ = [];
64 this.pendingCallback_ = null;
65 this.timestampOffset_ = 0;
66 this.mediaSource = mediaSource;
67 this.processedAppend_ = false;
68
69 if (mediaSource.readyState === 'closed') {
70 mediaSource.addEventListener('sourceopen', createSourceBuffer);
71 } else {
72 createSourceBuffer();
73 }
74 }
75
76 /**
77 * Aborts the current segment and resets the segment parser.
78 *
79 * @param {Function} done function to call when done
80 * @see http://w3c.github.io/media-source/#widl-SourceBuffer-abort-void
81 */
82
83 _createClass(SourceUpdater, [{
84 key: 'abort',
85 value: function abort(done) {
86 var _this2 = this;
87
88 if (this.processedAppend_) {
89 this.queueCallback_(function () {
90 _this2.sourceBuffer_.abort();
91 }, done);
92 }
93 }
94
95 /**
96 * Queue an update to append an ArrayBuffer.
97 *
98 * @param {ArrayBuffer} bytes
99 * @param {Function} done the function to call when done
100 * @see http://www.w3.org/TR/media-source/#widl-SourceBuffer-appendBuffer-void-ArrayBuffer-data
101 */
102 }, {
103 key: 'appendBuffer',
104 value: function appendBuffer(bytes, done) {
105 var _this3 = this;
106
107 this.processedAppend_ = true;
108
109 this.queueCallback_(function () {
110 _this3.sourceBuffer_.appendBuffer(bytes);
111 }, done);
112 }
113
114 /**
115 * Indicates what TimeRanges are buffered in the managed SourceBuffer.
116 *
117 * @see http://www.w3.org/TR/media-source/#widl-SourceBuffer-buffered
118 */
119 }, {
120 key: 'buffered',
121 value: function buffered() {
122 if (!this.sourceBuffer_) {
123 return _videoJs2['default'].createTimeRanges();
124 }
125 return this.sourceBuffer_.buffered;
126 }
127
128 /**
129 * Queue an update to remove a time range from the buffer.
130 *
131 * @param {Number} start where to start the removal
132 * @param {Number} end where to end the removal
133 * @see http://www.w3.org/TR/media-source/#widl-SourceBuffer-remove-void-double-start-unrestricted-double-end
134 */
135 }, {
136 key: 'remove',
137 value: function remove(start, end) {
138 var _this4 = this;
139
140 if (this.processedAppend_) {
141 this.queueCallback_(function () {
142 _this4.sourceBuffer_.remove(start, end);
143 }, noop);
144 }
145 }
146
147 /**
148 * Whether the underlying sourceBuffer is updating or not
149 *
150 * @return {Boolean} the updating status of the SourceBuffer
151 */
152 }, {
153 key: 'updating',
154 value: function updating() {
155 return !this.sourceBuffer_ || this.sourceBuffer_.updating || this.pendingCallback_;
156 }
157
158 /**
159 * Set/get the timestampoffset on the SourceBuffer
160 *
161 * @return {Number} the timestamp offset
162 */
163 }, {
164 key: 'timestampOffset',
165 value: function timestampOffset(offset) {
166 var _this5 = this;
167
168 if (typeof offset !== 'undefined') {
169 this.queueCallback_(function () {
170 _this5.sourceBuffer_.timestampOffset = offset;
171 });
172 this.timestampOffset_ = offset;
173 }
174 return this.timestampOffset_;
175 }
176
177 /**
178 * Queue a callback to run
179 */
180 }, {
181 key: 'queueCallback_',
182 value: function queueCallback_(callback, done) {
183 this.callbacks_.push([callback.bind(this), done]);
184 this.runCallback_();
185 }
186
187 /**
188 * Run a queued callback
189 */
190 }, {
191 key: 'runCallback_',
192 value: function runCallback_() {
193 var callbacks = undefined;
194
195 if (!this.updating() && this.callbacks_.length) {
196 callbacks = this.callbacks_.shift();
197 this.pendingCallback_ = callbacks[1];
198 callbacks[0]();
199 }
200 }
201
202 /**
203 * dispose of the source updater and the underlying sourceBuffer
204 */
205 }, {
206 key: 'dispose',
207 value: function dispose() {
208 this.sourceBuffer_.removeEventListener('updateend', this.onUpdateendCallback_);
209 if (this.sourceBuffer_ && this.mediaSource.readyState === 'open') {
210 this.sourceBuffer_.abort();
211 }
212 }
213 }]);
214
215 return SourceUpdater;
216})();
217
218exports['default'] = SourceUpdater;
219module.exports = exports['default'];
\No newline at end of file