-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathSourceBufferSink.js
493 lines (431 loc) · 17.4 KB
/
SourceBufferSink.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/**
* The copyright in this software is being made available under the BSD License,
* included below. This software may be subject to other third party and contributor
* rights, including patent rights, and no such rights are granted under this license.
*
* Copyright (c) 2013, Dash Industry Forum.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* * Neither the name of Dash Industry Forum nor the names of its
* contributors may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
import Debug from '../core/Debug.js';
import DashJSError from './vo/DashJSError.js';
import FactoryMaker from '../core/FactoryMaker.js';
import Errors from '../core/errors/Errors.js';
import Settings from '../core/Settings.js';
import constants from './constants/Constants.js';
import {HTTPRequest} from './vo/metrics/HTTPRequest.js';
import Events from '../core/events/Events.js';
const APPEND_WINDOW_START_OFFSET = 0.1;
const APPEND_WINDOW_END_OFFSET = 0.01;
/**
* @class SourceBufferSink
* @ignore
* @implements FragmentSink
*/
const CHECK_INTERVAL = 50;
function SourceBufferSink(config) {
const context = this.context;
const settings = Settings(context).getInstance();
const textController = config.textController;
const eventBus = config.eventBus;
let instance,
type,
logger,
buffer,
mediaInfo,
intervalId;
let callbacks = [];
let appendQueue = [];
let isAppendingInProgress = false;
let mediaSource = config.mediaSource;
let lastRequestAppended = null;
function setup() {
logger = Debug(context).getInstance().getLogger(instance);
}
function _getCodecStringForRepresentation(representation) {
return representation.mimeType + ';codecs="' + representation.codecs + '"';
}
function initializeForStreamSwitch(mInfo, selectedRepresentation, oldSourceBufferSink) {
mediaInfo = mInfo;
type = mediaInfo.type;
_copyPreviousSinkData(oldSourceBufferSink);
_addEventListeners();
}
function changeType(representation) {
const codec = _getCodecStringForRepresentation(representation);
return new Promise((resolve) => {
_waitForUpdateEnd(() => {
if (buffer.changeType) {
logger.debug(`Changing SourceBuffer codec to ${codec}`);
buffer.changeType(codec);
}
resolve();
});
});
}
function _copyPreviousSinkData(oldSourceBufferSink) {
buffer = oldSourceBufferSink.getBuffer();
}
function initializeForFirstUse(mInfo, selectedRepresentation) {
mediaInfo = mInfo;
const streamInfo = mInfo.streamInfo;
type = mediaInfo.type;
const codec = selectedRepresentation ? _getCodecStringForRepresentation(selectedRepresentation) : mInfo.codec;
try {
// Safari claims to support anything starting 'application/mp4'.
// it definitely doesn't understand 'application/mp4;codecs="stpp"'
// - currently no browser does, so check for it and use our own
// implementation. The same is true for codecs="wvtt".
if (codec.match(/application\/mp4;\s*codecs="(stpp|wvtt).*"/i)) {
return _initializeForText(streamInfo);
}
buffer = mediaSource.addSourceBuffer(codec);
_addEventListeners();
const promises = [];
promises.push(updateAppendWindow(mediaInfo.streamInfo));
if (selectedRepresentation && selectedRepresentation.mseTimeOffset !== undefined) {
promises.push(updateTimestampOffset(selectedRepresentation.mseTimeOffset));
}
return Promise.all(promises);
} catch (e) {
// Note that in the following, the quotes are open to allow for extra text after stpp and wvtt
if ((mediaInfo.type == constants.TEXT && !mediaInfo.isFragmented) || (codec.indexOf('codecs="stpp') !== -1) || (codec.indexOf('codecs="vtt') !== -1) || (codec.indexOf('text/vtt') !== -1)) {
return _initializeForText(streamInfo);
}
return Promise.reject(e);
}
}
function _initializeForText(streamInfo) {
buffer = textController.getTextSourceBuffer(streamInfo);
return Promise.resolve();
}
function _addEventListeners() {
// use updateend event if possible
if (typeof buffer.addEventListener === 'function') {
try {
buffer.addEventListener('updateend', _updateEndHandler, false);
buffer.addEventListener('error', _errHandler, false);
buffer.addEventListener('abort', _errHandler, false);
} catch (err) {
// use setInterval to periodically check if updating has been completed
intervalId = setInterval(_updateEndHandler, CHECK_INTERVAL);
}
} else {
// use setInterval to periodically check if updating has been completed
intervalId = setInterval(_updateEndHandler, CHECK_INTERVAL);
}
}
function getType() {
return type;
}
function removeEventListeners() {
try {
if (typeof buffer.removeEventListener === 'function') {
buffer.removeEventListener('updateend', _updateEndHandler, false);
buffer.removeEventListener('error', _errHandler, false);
buffer.removeEventListener('abort', _errHandler, false);
}
clearInterval(intervalId);
} catch (e) {
logger.error(e);
}
}
function updateAppendWindow(sInfo) {
return new Promise((resolve) => {
if (!buffer || !settings.get().streaming.buffer.useAppendWindow) {
resolve();
return;
}
_waitForUpdateEnd(() => {
try {
if (!buffer) {
resolve();
return;
}
let appendWindowEnd = mediaSource.duration;
let appendWindowStart = 0;
if (sInfo && !isNaN(sInfo.start) && !isNaN(sInfo.duration) && isFinite(sInfo.duration)) {
appendWindowEnd = sInfo.start + sInfo.duration;
}
if (sInfo && !isNaN(sInfo.start)) {
appendWindowStart = sInfo.start;
}
if (buffer.appendWindowEnd !== appendWindowEnd || buffer.appendWindowStart !== appendWindowStart) {
buffer.appendWindowStart = 0;
buffer.appendWindowEnd = appendWindowEnd + APPEND_WINDOW_END_OFFSET;
buffer.appendWindowStart = Math.max(appendWindowStart - APPEND_WINDOW_START_OFFSET, 0);
logger.debug(`Updated append window for ${mediaInfo.type}. Set start to ${buffer.appendWindowStart} and end to ${buffer.appendWindowEnd}`);
}
resolve();
} catch (e) {
logger.warn(`Failed to set append window`);
resolve();
}
});
});
}
function updateTimestampOffset(mseTimeOffset) {
return new Promise((resolve) => {
if (!buffer) {
resolve();
return;
}
_waitForUpdateEnd(() => {
try {
if (buffer.timestampOffset !== mseTimeOffset && !isNaN(mseTimeOffset)) {
buffer.timestampOffset = mseTimeOffset;
logger.debug(`Set MSE timestamp offset to ${mseTimeOffset}`);
}
resolve();
} catch (e) {
resolve();
}
});
});
}
function reset() {
if (buffer) {
try {
callbacks = [];
removeEventListeners();
isAppendingInProgress = false;
appendQueue = [];
if (!buffer.getClassName || buffer.getClassName() !== 'TextSourceBuffer') {
logger.debug(`Removing sourcebuffer from media source`);
mediaSource.removeSourceBuffer(buffer);
}
} catch (e) {
}
buffer = null;
}
lastRequestAppended = null;
}
function getBuffer() {
return buffer;
}
function getAllBufferRanges() {
try {
return buffer.buffered;
} catch (e) {
logger.error('getAllBufferRanges exception: ' + e.message);
return null;
}
}
function append(chunk, request = null) {
return new Promise((resolve, reject) => {
if (!chunk) {
reject({
chunk: chunk,
error: new DashJSError(Errors.APPEND_ERROR_CODE, Errors.APPEND_ERROR_MESSAGE)
});
return;
}
appendQueue.push({ data: chunk, promise: { resolve, reject }, request });
_waitForUpdateEnd(_appendNextInQueue.bind(this));
});
}
function abortBeforeAppend() {
return new Promise((resolve) => {
_waitForUpdateEnd(() => {
// Save the append window, which is reset on abort().
const appendWindowStart = buffer.appendWindowStart;
const appendWindowEnd = buffer.appendWindowEnd;
if (buffer) {
buffer.abort();
buffer.appendWindowStart = appendWindowStart;
buffer.appendWindowEnd = appendWindowEnd;
}
resolve();
});
});
}
function remove(range) {
return new Promise((resolve, reject) => {
const start = range.start;
const end = range.end;
// make sure that the given time range is correct. Otherwise we will get InvalidAccessError
if (!((start >= 0) && (end > start))) {
resolve();
return;
}
_waitForUpdateEnd(function () {
try {
buffer.remove(start, end);
// updating is in progress, we should wait for it to complete before signaling that this operation is done
_waitForUpdateEnd(function () {
resolve({
from: start,
to: end,
unintended: false
});
if (range.resolve) {
range.resolve();
}
});
} catch (err) {
reject({
from: start,
to: end,
unintended: false,
error: new DashJSError(Errors.REMOVE_ERROR_CODE, Errors.REMOVE_ERROR_MESSAGE)
});
if (range.reject) {
range.reject(err);
}
}
});
});
}
function _appendNextInQueue() {
if (isAppendingInProgress) {
return;
}
if (appendQueue.length > 0) {
isAppendingInProgress = true;
const nextChunk = appendQueue[0];
appendQueue.splice(0, 1);
const afterSuccess = function () {
isAppendingInProgress = false;
if (appendQueue.length > 0) {
_appendNextInQueue.call(this);
}
// Init segments are cached. In any other case we dont need the chunk bytes anymore and can free the memory
if (nextChunk && nextChunk.data && nextChunk.data.segmentType && nextChunk.data.segmentType !== HTTPRequest.INIT_SEGMENT_TYPE) {
delete nextChunk.data.bytes;
}
nextChunk.promise.resolve({ chunk: nextChunk.data });
};
try {
lastRequestAppended = nextChunk.request;
if (nextChunk.data.bytes.byteLength === 0) {
afterSuccess.call(this);
} else {
try {
logger.debug(`Appending ${nextChunk.data.segmentType} from period ${nextChunk.data.streamId} to buffer. Request URL: ${nextChunk.request.url}, Representation: ID: ${nextChunk.data.representation.id}, bitrate: ${nextChunk.data.representation.bitrateInKbit}`)
} catch (e) {
}
if (buffer.appendBuffer) {
buffer.appendBuffer(nextChunk.data.bytes);
} else {
buffer.append(nextChunk.data.bytes, nextChunk.data);
}
// updating is in progress, we should wait for it to complete before signaling that this operation is done
_waitForUpdateEnd(afterSuccess.bind(this));
}
} catch (err) {
logger.fatal('SourceBuffer append failed "' + err + '"');
if (appendQueue.length > 0) {
_appendNextInQueue();
} else {
isAppendingInProgress = false;
}
delete nextChunk.data.bytes;
nextChunk.promise.reject({ chunk: nextChunk.data, error: new DashJSError(err.code, err.message) });
}
}
}
function abort() {
return new Promise((resolve) => {
try {
appendQueue = [];
if (mediaSource.readyState === 'open') {
_waitForUpdateEnd(() => {
try {
if (buffer) {
buffer.abort();
}
resolve();
} catch (e) {
resolve();
}
});
} else if (buffer && buffer.setTextTrack && mediaSource.readyState === 'ended') {
buffer.abort(); //The cues need to be removed from the TextSourceBuffer via a call to abort()
resolve();
} else {
resolve();
}
} catch (e) {
resolve();
}
});
}
function _executeCallback() {
if (callbacks.length > 0) {
if (!buffer.updating) {
const cb = callbacks.shift();
cb();
// Try to execute next callback if still not updating
_executeCallback();
}
}
}
function _updateEndHandler() {
// if updating is still in progress do nothing and wait for the next check again.
if (buffer.updating) {
return;
}
// updating is completed, now we can stop checking and resolve the promise
_executeCallback();
}
function _errHandler(e) {
const error = e.target || {};
_triggerEvent(Events.SOURCE_BUFFER_ERROR, { error, lastRequestAppended })
}
function _triggerEvent(eventType, data) {
let payload = data || {};
eventBus.trigger(eventType, payload, { streamId: mediaInfo.streamInfo.id, mediaType: type });
}
function _waitForUpdateEnd(callback) {
try {
callbacks.push(callback);
if (!buffer.updating) {
_executeCallback();
}
} catch (e) {
logger.error(e);
}
}
instance = {
abort,
abortBeforeAppend,
append,
changeType,
getAllBufferRanges,
getBuffer,
getType,
initializeForFirstUse,
initializeForStreamSwitch,
remove,
removeEventListeners,
reset,
updateAppendWindow,
updateTimestampOffset,
};
setup();
return instance;
}
SourceBufferSink.__dashjs_factory_name = 'SourceBufferSink';
const factory = FactoryMaker.getClassFactory(SourceBufferSink);
export default factory;