forked from video-dev/hls.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer-controller.js
457 lines (419 loc) · 16 KB
/
buffer-controller.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
/*
* Buffer Controller
*/
import Event from '../events';
import EventHandler from '../event-handler';
import {logger} from '../utils/logger';
import {ErrorTypes, ErrorDetails} from '../errors';
class BufferController extends EventHandler {
constructor(hls) {
super(hls,
Event.MEDIA_ATTACHING,
Event.MEDIA_DETACHING,
Event.MANIFEST_PARSED,
Event.BUFFER_RESET,
Event.BUFFER_APPENDING,
Event.BUFFER_CODECS,
Event.BUFFER_EOS,
Event.BUFFER_FLUSHING,
Event.LEVEL_UPDATED);
// the value that we have set mediasource.duration to
// (the actual duration may be tweaked slighly by the browser)
this._msDuration = null;
// the value that we want to set mediaSource.duration to
this._levelDuration = null;
// Source Buffer listeners
this.onsbue = this.onSBUpdateEnd.bind(this);
this.onsbe = this.onSBUpdateError.bind(this);
this.pendingTracks = {};
}
destroy() {
EventHandler.prototype.destroy.call(this);
}
onManifestParsed(data) {
let audioExpected = data.audio,
videoExpected = data.video,
sourceBufferNb = 0;
if (audioExpected || videoExpected) {
sourceBufferNb = (audioExpected ? 1 : 0) + (videoExpected ? 1 : 0);
logger.log(`${sourceBufferNb} sourceBuffer(s) expected`);
}
this.sourceBufferNb = sourceBufferNb;
}
onMediaAttaching(data) {
let media = this.media = data.media;
if (media) {
// setup the media source
var ms = this.mediaSource = new MediaSource();
//Media Source listeners
this.onmso = this.onMediaSourceOpen.bind(this);
this.onmse = this.onMediaSourceEnded.bind(this);
this.onmsc = this.onMediaSourceClose.bind(this);
ms.addEventListener('sourceopen', this.onmso);
ms.addEventListener('sourceended', this.onmse);
ms.addEventListener('sourceclose', this.onmsc);
// link video and media Source
media.src = URL.createObjectURL(ms);
}
}
onMediaDetaching() {
logger.log('media source detaching');
var ms = this.mediaSource;
if (ms) {
if (ms.readyState === 'open') {
try {
// endOfStream could trigger exception if any sourcebuffer is in updating state
// we don't really care about checking sourcebuffer state here,
// as we are anyway detaching the MediaSource
// let's just avoid this exception to propagate
ms.endOfStream();
} catch(err) {
logger.warn(`onMediaDetaching:${err.message} while calling endOfStream`);
}
}
ms.removeEventListener('sourceopen', this.onmso);
ms.removeEventListener('sourceended', this.onmse);
ms.removeEventListener('sourceclose', this.onmsc);
try {
// unlink MediaSource from video tag
this.media.src = '';
this.media.removeAttribute('src');
} catch(err) {
logger.warn(`onMediaDetaching:${err.message} while unlinking video.src`);
}
this.mediaSource = null;
this.media = null;
this.pendingTracks = {};
this.sourceBuffer = {};
}
this.onmso = this.onmse = this.onmsc = null;
this.hls.trigger(Event.MEDIA_DETACHED);
}
onMediaSourceOpen() {
logger.log('media source opened');
this.hls.trigger(Event.MEDIA_ATTACHED, { media : this.media });
// once received, don't listen anymore to sourceopen event
this.mediaSource.removeEventListener('sourceopen', this.onmso);
this.checkPendingTracks();
}
checkPendingTracks() {
// if any buffer codecs pending, check if we have enough to create sourceBuffers
let pendingTracks = this.pendingTracks,
pendingTracksNb = Object.keys(pendingTracks).length;
// if any pending tracks and (if nb of pending tracks matching expected nb or if unknoown expected nb)
if (pendingTracksNb && (
this.sourceBufferNb === pendingTracksNb ||
this.sourceBufferNb === 0)) {
// ok, let's create them now !
this.createSourceBuffers(pendingTracks);
this.pendingTracks = {};
// append any pending segments now !
this.doAppending();
}
}
onMediaSourceClose() {
logger.log('media source closed');
}
onMediaSourceEnded() {
logger.log('media source ended');
}
onSBUpdateEnd() {
if (this._needsFlush) {
this.doFlush();
}
if (this._needsEos) {
this.onBufferEos();
}
this.appending = false;
this.hls.trigger(Event.BUFFER_APPENDED, { parent : this.parent});
// don't append in flushing mode
if (!this._needsFlush) {
this.doAppending();
}
this.updateMediaElementDuration();
}
onSBUpdateError(event) {
logger.error(`sourceBuffer error:${event}`);
// according to http://www.w3.org/TR/media-source/#sourcebuffer-append-error
// this error might not always be fatal (it is fatal if decode error is set, in that case
// it will be followed by a mediaElement error ...)
this.hls.trigger(Event.ERROR, {type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.BUFFER_APPENDING_ERROR, fatal: false});
// we don't need to do more than that, as accordin to the spec, updateend will be fired just after
}
onBufferReset() {
var sourceBuffer = this.sourceBuffer;
for(var type in sourceBuffer) {
var sb = sourceBuffer[type];
try {
this.mediaSource.removeSourceBuffer(sb);
sb.removeEventListener('updateend', this.onsbue);
sb.removeEventListener('error', this.onsbe);
} catch(err) {
}
}
this.sourceBuffer = {};
this.flushRange = [];
this.appended = 0;
}
onBufferCodecs(tracks) {
// if source buffer(s) not created yet, appended buffer tracks in this.pendingTracks
// if sourcebuffers already created, do nothing ...
if (Object.keys(this.sourceBuffer).length === 0) {
for (var trackName in tracks) { this.pendingTracks[trackName] = tracks[trackName]; }
let mediaSource = this.mediaSource;
if (mediaSource && mediaSource.readyState === 'open') {
// try to create sourcebuffers if mediasource opened
this.checkPendingTracks();
}
}
}
createSourceBuffers(tracks) {
var sourceBuffer = this.sourceBuffer,mediaSource = this.mediaSource;
for (let trackName in tracks) {
if(!sourceBuffer[trackName]) {
let track = tracks[trackName];
// use levelCodec as first priority
let codec = track.levelCodec || track.codec;
let mimeType = `${track.container};codecs=${codec}`;
logger.log(`creating sourceBuffer with mimeType:${mimeType}`);
try {
let sb = sourceBuffer[trackName] = mediaSource.addSourceBuffer(mimeType);
sb.addEventListener('updateend', this.onsbue);
sb.addEventListener('error', this.onsbe);
track.buffer = sb;
} catch(err) {
logger.error(`error while trying to add sourceBuffer:${err.message}`);
this.hls.trigger(Event.ERROR, {type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.BUFFER_ADD_CODEC_ERROR, fatal: false, err: err, mimeType : mimeType});
}
}
}
this.hls.trigger(Event.BUFFER_CREATED, { tracks : tracks } );
}
onBufferAppending(data) {
if (!this._needsFlush) {
if (!this.segments) {
this.segments = [ data ];
} else {
this.segments.push(data);
}
this.doAppending();
}
}
onBufferAppendFail(data) {
logger.error(`sourceBuffer error:${data.event}`);
// according to http://www.w3.org/TR/media-source/#sourcebuffer-append-error
// this error might not always be fatal (it is fatal if decode error is set, in that case
// it will be followed by a mediaElement error ...)
this.hls.trigger(Event.ERROR, {type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.BUFFER_APPENDING_ERROR, fatal: false, frag: this.fragCurrent});
}
onBufferEos() {
var sb = this.sourceBuffer, mediaSource = this.mediaSource;
if (!mediaSource || mediaSource.readyState !== 'open') {
return;
}
if (!((sb.audio && sb.audio.updating) || (sb.video && sb.video.updating))) {
logger.log('all media data available, signal endOfStream() to MediaSource and stop loading fragment');
//Notify the media element that it now has all of the media data
mediaSource.endOfStream();
this._needsEos = false;
} else {
this._needsEos = true;
}
}
onBufferFlushing(data) {
this.flushRange.push({start: data.startOffset, end: data.endOffset, type : data.type});
// attempt flush immediatly
this.flushBufferCounter = 0;
this.doFlush();
}
onLevelUpdated(event) {
let details = event.details;
if (details.fragments.length === 0) {
return;
}
this._levelDuration = details.totalduration + details.fragments[0].start;
this.updateMediaElementDuration();
}
// https://github.com/dailymotion/hls.js/issues/355
updateMediaElementDuration() {
if (this._levelDuration === null) {
return;
}
let media = this.media,
mediaSource = this.mediaSource,
sourceBuffer = this.sourceBuffer;
if (!media || !mediaSource || !sourceBuffer || media.readyState === 0 || mediaSource.readyState !== 'open') {
return;
}
for (let type in sourceBuffer) {
if (sourceBuffer[type].updating) {
// can't set duration whilst a buffer is updating
return;
}
}
if (this._msDuration === null) {
// initialise to the value that the media source is reporting
this._msDuration = mediaSource.duration;
}
// this._levelDuration was the last value we set.
// not using mediaSource.duration as the browser may tweak this value
// only update mediasource duration if its value increase, this is to avoid
// flushing already buffered portion when switching between quality level, as they
if (this._levelDuration > this._msDuration) {
logger.log(`Updating mediasource duration to ${this._levelDuration}`);
mediaSource.duration = this._levelDuration;
this._msDuration = this._levelDuration;
}
}
doFlush() {
// loop through all buffer ranges to flush
while(this.flushRange.length) {
var range = this.flushRange[0];
// flushBuffer will abort any buffer append in progress and flush Audio/Video Buffer
if (this.flushBuffer(range.start, range.end, range.type)) {
// range flushed, remove from flush array
this.flushRange.shift();
this.flushBufferCounter = 0;
} else {
this._needsFlush = true;
// avoid looping, wait for SB update end to retrigger a flush
return;
}
}
if (this.flushRange.length === 0) {
// everything flushed
this._needsFlush = false;
// let's recompute this.appended, which is used to avoid flush looping
var appended = 0;
var sourceBuffer = this.sourceBuffer;
for (var type in sourceBuffer) {
appended += sourceBuffer[type].buffered.length;
}
this.appended = appended;
this.hls.trigger(Event.BUFFER_FLUSHED);
}
}
doAppending() {
var hls = this.hls, sourceBuffer = this.sourceBuffer, segments = this.segments;
if (Object.keys(sourceBuffer).length) {
if (this.media.error) {
this.segments = [];
logger.error('trying to append although a media error occured, flush segment and abort');
return;
}
if (this.appending) {
//logger.log(`sb appending in progress`);
return;
}
if (segments && segments.length) {
var segment = segments.shift();
try {
if(sourceBuffer[segment.type]) {
//logger.log(`appending ${segment.type} SB, size:${segment.data.length}`);
this.parent = segment.parent;
sourceBuffer[segment.type].appendBuffer(segment.data);
this.appendError = 0;
this.appended++;
this.appending = true;
} else {
// in case we don't have any source buffer matching with this segment type,
// it means that Mediasource fails to create sourcebuffer
// discard this segment, and trigger update end
this.onSBUpdateEnd();
}
} catch(err) {
// in case any error occured while appending, put back segment in segments table
logger.error(`error while trying to append buffer:${err.message}`);
segments.unshift(segment);
var event = {type: ErrorTypes.MEDIA_ERROR};
if(err.code !== 22) {
if (this.appendError) {
this.appendError++;
} else {
this.appendError = 1;
}
event.details = ErrorDetails.BUFFER_APPEND_ERROR;
event.frag = this.fragCurrent;
/* with UHD content, we could get loop of quota exceeded error until
browser is able to evict some data from sourcebuffer. retrying help recovering this
*/
if (this.appendError > hls.config.appendErrorMaxRetry) {
logger.log(`fail ${hls.config.appendErrorMaxRetry} times to append segment in sourceBuffer`);
segments = [];
event.fatal = true;
hls.trigger(Event.ERROR, event);
return;
} else {
event.fatal = false;
hls.trigger(Event.ERROR, event);
}
} else {
// QuotaExceededError: http://www.w3.org/TR/html5/infrastructure.html#quotaexceedederror
// let's stop appending any segments, and report BUFFER_FULL_ERROR error
this.segments = [];
event.details = ErrorDetails.BUFFER_FULL_ERROR;
hls.trigger(Event.ERROR,event);
return;
}
}
}
}
}
/*
flush specified buffered range,
return true once range has been flushed.
as sourceBuffer.remove() is asynchronous, flushBuffer will be retriggered on sourceBuffer update end
*/
flushBuffer(startOffset, endOffset, typeIn) {
var sb, i, bufStart, bufEnd, flushStart, flushEnd;
//logger.log('flushBuffer,pos/start/end: ' + this.media.currentTime + '/' + startOffset + '/' + endOffset);
// safeguard to avoid infinite looping : don't try to flush more than the nb of appended segments
if (this.flushBufferCounter < this.appended && this.sourceBuffer) {
for (var type in this.sourceBuffer) {
// check if sourcebuffer type is defined (typeIn): if yes, let's only flush this one
// if no, let's flush all sourcebuffers
if (typeIn && type !== typeIn) {
continue;
}
sb = this.sourceBuffer[type];
if (!sb.updating) {
for (i = 0; i < sb.buffered.length; i++) {
bufStart = sb.buffered.start(i);
bufEnd = sb.buffered.end(i);
// workaround firefox not able to properly flush multiple buffered range.
if (navigator.userAgent.toLowerCase().indexOf('firefox') !== -1 && endOffset === Number.POSITIVE_INFINITY) {
flushStart = startOffset;
flushEnd = endOffset;
} else {
flushStart = Math.max(bufStart, startOffset);
flushEnd = Math.min(bufEnd, endOffset);
}
/* sometimes sourcebuffer.remove() does not flush
the exact expected time range.
to avoid rounding issues/infinite loop,
only flush buffer range of length greater than 500ms.
*/
if (Math.min(flushEnd,bufEnd) - flushStart > 0.5 ) {
this.flushBufferCounter++;
logger.log(`flush ${type} [${flushStart},${flushEnd}], of [${bufStart},${bufEnd}], pos:${this.media.currentTime}`);
sb.remove(flushStart, flushEnd);
return false;
}
}
} else {
//logger.log('abort ' + type + ' append in progress');
// this will abort any appending in progress
//sb.abort();
logger.warn('cannot flush, sb updating in progress');
return false;
}
}
} else {
logger.warn('abort flushing too many retries');
}
logger.log('buffer flushed');
// everything flushed !
return true;
}
}
export default BufferController;