-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
PlaybackController.js
723 lines (624 loc) · 26.3 KB
/
PlaybackController.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
/**
* 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 Constants from '../constants/Constants';
import BufferController from './BufferController';
import URIFragmentModel from '../models/URIFragmentModel';
import EventBus from '../../core/EventBus';
import Events from '../../core/events/Events';
import FactoryMaker from '../../core/FactoryMaker';
import Debug from '../../core/Debug';
const LIVE_UPDATE_PLAYBACK_TIME_INTERVAL_MS = 500;
const DEFAULT_CATCHUP_PLAYBACK_RATE = 0.05;
// Start catching up mechanism for low latency live streaming
// when latency goes beyong targetDelay * (1 + LIVE_CATCHUP_START_THRESHOLD)
const LIVE_CATCHUP_START_THRESHOLD = 0.35;
function PlaybackController() {
const context = this.context;
const eventBus = EventBus(context).getInstance();
let instance,
logger,
streamController,
metricsModel,
dashMetrics,
manifestModel,
dashManifestModel,
adapter,
videoModel,
currentTime,
liveStartTime,
wallclockTimeIntervalId,
commonEarliestTime,
liveDelay,
bufferedRange,
streamInfo,
isDynamic,
mediaPlayerModel,
playOnceInitialized,
lastLivePlaybackTime,
originalPlaybackRate,
catchingUp,
availabilityStartTime;
let catchUpPlaybackRate = DEFAULT_CATCHUP_PLAYBACK_RATE;
function setup() {
logger = Debug(context).getInstance().getLogger(instance);
reset();
}
function initialize(StreamInfo) {
streamInfo = StreamInfo;
addAllListeners();
isDynamic = streamInfo.manifestInfo.isDynamic;
liveStartTime = streamInfo.start;
eventBus.on(Events.DATA_UPDATE_COMPLETED, onDataUpdateCompleted, this);
eventBus.on(Events.BYTES_APPENDED_END_FRAGMENT, onBytesAppended, this);
eventBus.on(Events.BUFFER_LEVEL_STATE_CHANGED, onBufferLevelStateChanged, this);
eventBus.on(Events.PERIOD_SWITCH_STARTED, onPeriodSwitchStarted, this);
eventBus.on(Events.PLAYBACK_PROGRESS, onPlaybackProgression, this);
eventBus.on(Events.PLAYBACK_TIME_UPDATED, onPlaybackProgression, this);
if (playOnceInitialized) {
playOnceInitialized = false;
play();
}
}
function onPeriodSwitchStarted(e) {
if (!isDynamic && e.fromStreamInfo && commonEarliestTime[e.fromStreamInfo.id] !== undefined) {
delete bufferedRange[e.fromStreamInfo.id];
delete commonEarliestTime[e.fromStreamInfo.id];
}
}
function getTimeToStreamEnd() {
const startTime = getStreamStartTime(true);
const offset = isDynamic ? startTime - streamInfo.start : 0;
return parseFloat((startTime + (streamInfo.duration - offset) - getTime()).toFixed(5));
}
function play() {
if (streamInfo && videoModel && videoModel.getElement()) {
videoModel.play();
} else {
playOnceInitialized = true;
}
}
function isPaused() {
return streamInfo && videoModel ? videoModel.isPaused() : null;
}
function pause() {
if (streamInfo && videoModel) {
videoModel.pause();
}
}
function isSeeking() {
return streamInfo && videoModel ? videoModel.isSeeking() : null;
}
function seek(time) {
if (streamInfo && videoModel) {
eventBus.trigger(Events.PLAYBACK_SEEK_ASKED);
logger.info('Requesting seek to time: ' + time);
videoModel.setCurrentTime(time);
}
}
function getTime() {
return streamInfo && videoModel ? videoModel.getTime() : null;
}
function getPlaybackRate() {
return streamInfo && videoModel ? videoModel.getPlaybackRate() : null;
}
function getPlayedRanges() {
return streamInfo && videoModel ? videoModel.getPlayedRanges() : null;
}
function getEnded() {
return streamInfo && videoModel ? videoModel.getEnded() : null;
}
function getIsDynamic() {
return isDynamic;
}
function getStreamController() {
return streamController;
}
function setLiveStartTime(value) {
liveStartTime = value;
}
function getLiveStartTime() {
return liveStartTime;
}
function setCatchUpPlaybackRate(value) {
catchUpPlaybackRate = value;
// If value == 0.0, deactivate catchup mechanism
if (value === 0.0 && getPlaybackRate() > 1.0) {
stopPlaybackCatchUp();
}
}
function getCatchUpPlaybackRate() {
return catchUpPlaybackRate;
}
/**
* Computes the desirable delay for the live edge to avoid a risk of getting 404 when playing at the bleeding edge
* @param {number} fragmentDuration - seconds?
* @param {number} dvrWindowSize - seconds?
* @returns {number} object
* @memberof PlaybackController#
*/
function computeLiveDelay(fragmentDuration, dvrWindowSize) {
const mpd = dashManifestModel.getMpd(manifestModel.getValue());
let delay;
let ret;
const END_OF_PLAYLIST_PADDING = 10;
if (mediaPlayerModel.getUseSuggestedPresentationDelay() && mpd.hasOwnProperty(Constants.SUGGESTED_PRESENTATION_DELAY)) {
delay = mpd.suggestedPresentationDelay;
} else if (mediaPlayerModel.getLowLatencyEnabled()) {
delay = 0;
} else if (mediaPlayerModel.getLiveDelay()) {
delay = mediaPlayerModel.getLiveDelay(); // If set by user, this value takes precedence
} else if (!isNaN(fragmentDuration)) {
delay = fragmentDuration * mediaPlayerModel.getLiveDelayFragmentCount();
} else {
delay = streamInfo.manifestInfo.minBufferTime * 2;
}
if (mpd.availabilityStartTime) {
availabilityStartTime = mpd.availabilityStartTime.getTime();
}
if (dvrWindowSize > 0) {
// cap target latency to:
// - dvrWindowSize / 2 for short playlists
// - dvrWindowSize - END_OF_PLAYLIST_PADDING for longer playlists
const targetDelayCapping = Math.max(dvrWindowSize - END_OF_PLAYLIST_PADDING, dvrWindowSize / 2);
ret = Math.min(delay, targetDelayCapping);
} else {
ret = delay;
}
liveDelay = ret;
return ret;
}
function getLiveDelay() {
return liveDelay;
}
function getCurrentLiveLatency() {
if (!isDynamic || isNaN(availabilityStartTime)) {
return NaN;
}
const currentTime = getTime();
if (isNaN(currentTime) || currentTime === 0) {
return 0;
}
return ((Math.round(new Date().getTime() - (currentTime * 1000 + availabilityStartTime))) / 1000).toFixed(3);
}
function startPlaybackCatchUp() {
if (videoModel) {
const playbackRate = 1 + getCatchUpPlaybackRate();
const currentRate = getPlaybackRate();
if (playbackRate !== currentRate) {
catchingUp = true;
logger.info('Starting live catchup mechanism. Setting playback rate to', playbackRate);
originalPlaybackRate = currentRate;
videoModel.getElement().playbackRate = playbackRate;
eventBus.trigger(Events.PLAYBACK_CATCHUP_START, { sender: instance });
}
}
}
function stopPlaybackCatchUp() {
if (videoModel) {
const playbackRate = originalPlaybackRate || 1;
if (playbackRate !== getPlaybackRate()) {
catchingUp = false;
logger.info('Stopping live catchup mechanism. Setting playback rate to', playbackRate);
videoModel.getElement().playbackRate = playbackRate;
eventBus.trigger(Events.PLAYBACK_CATCHUP_END, { sender: instance });
}
}
}
function reset() {
currentTime = 0;
liveStartTime = NaN;
playOnceInitialized = false;
commonEarliestTime = {};
liveDelay = 0;
availabilityStartTime = 0;
catchUpPlaybackRate = DEFAULT_CATCHUP_PLAYBACK_RATE;
bufferedRange = {};
if (videoModel) {
eventBus.off(Events.DATA_UPDATE_COMPLETED, onDataUpdateCompleted, this);
eventBus.off(Events.BUFFER_LEVEL_STATE_CHANGED, onBufferLevelStateChanged, this);
eventBus.off(Events.BYTES_APPENDED_END_FRAGMENT, onBytesAppended, this);
eventBus.off(Events.PERIOD_SWITCH_STARTED, onPeriodSwitchStarted, this);
eventBus.off(Events.PLAYBACK_PROGRESS, onPlaybackProgression, this);
eventBus.off(Events.PLAYBACK_TIME_UPDATED, onPlaybackProgression, this);
stopUpdatingWallclockTime();
removeAllListeners();
}
wallclockTimeIntervalId = null;
videoModel = null;
streamInfo = null;
isDynamic = null;
}
function setConfig(config) {
if (!config) return;
if (config.streamController) {
streamController = config.streamController;
}
if (config.metricsModel) {
metricsModel = config.metricsModel;
}
if (config.dashMetrics) {
dashMetrics = config.dashMetrics;
}
if (config.manifestModel) {
manifestModel = config.manifestModel;
}
if (config.dashManifestModel) {
dashManifestModel = config.dashManifestModel;
}
if (config.mediaPlayerModel) {
mediaPlayerModel = config.mediaPlayerModel;
}
if (config.adapter) {
adapter = config.adapter;
}
if (config.videoModel) {
videoModel = config.videoModel;
}
}
function getStartTimeFromUriParameters() {
const fragData = URIFragmentModel(context).getInstance().getURIFragmentData();
let uriParameters;
if (fragData) {
uriParameters = {};
const r = parseInt(fragData.r, 10);
if (r >= 0 && streamInfo && r < streamInfo.manifestInfo.DVRWindowSize && fragData.t === null) {
fragData.t = Math.floor(Date.now() / 1000) - streamInfo.manifestInfo.DVRWindowSize + r;
}
uriParameters.fragS = parseInt(fragData.s, 10);
uriParameters.fragT = parseInt(fragData.t, 10);
}
return uriParameters;
}
/**
* @param {boolean} ignoreStartOffset - ignore URL fragment start offset if true
* @param {number} liveEdge - liveEdge value
* @returns {number} object
* @memberof PlaybackController#
*/
function getStreamStartTime(ignoreStartOffset, liveEdge) {
let presentationStartTime;
let startTimeOffset = NaN;
const uriParameters = getStartTimeFromUriParameters();
if (uriParameters) {
if (!ignoreStartOffset) {
startTimeOffset = !isNaN(uriParameters.fragS) ? uriParameters.fragS : uriParameters.fragT;
} else {
startTimeOffset = streamInfo.start;
}
} else {
// handle case where no media fragments are parsed from the manifest URL
startTimeOffset = 0;
}
if (isDynamic) {
if (!isNaN(startTimeOffset)) {
presentationStartTime = startTimeOffset - (streamInfo.manifestInfo.availableFrom.getTime() / 1000);
if (presentationStartTime > liveStartTime ||
presentationStartTime < (!isNaN(liveEdge) ? (liveEdge - streamInfo.manifestInfo.DVRWindowSize) : NaN)) {
presentationStartTime = null;
}
}
presentationStartTime = presentationStartTime || liveStartTime;
} else {
if (!isNaN(startTimeOffset) && startTimeOffset < Math.max(streamInfo.manifestInfo.duration, streamInfo.duration) && startTimeOffset >= 0) {
presentationStartTime = startTimeOffset;
} else {
let earliestTime = commonEarliestTime[streamInfo.id]; //set by ready bufferStart after first onBytesAppended
presentationStartTime = earliestTime !== undefined ? Math.max(earliestTime.audio !== undefined ? earliestTime.audio : 0, earliestTime.video !== undefined ? earliestTime.video : 0, streamInfo.start) : streamInfo.start;
}
}
return presentationStartTime;
}
function getActualPresentationTime(currentTime) {
const metrics = metricsModel.getReadOnlyMetricsFor(Constants.VIDEO) || metricsModel.getReadOnlyMetricsFor(Constants.AUDIO);
const DVRMetrics = dashMetrics.getCurrentDVRInfo(metrics);
const DVRWindow = DVRMetrics ? DVRMetrics.range : null;
let actualTime;
if (!DVRWindow) return NaN;
if (currentTime > DVRWindow.end) {
actualTime = Math.max(DVRWindow.end - streamInfo.manifestInfo.minBufferTime * 2, DVRWindow.start);
} else if (currentTime + 0.250 < DVRWindow.start) {
// Checking currentTime plus 250ms as the 'timeupdate' is fired with a frequency between 4Hz and 66Hz
// https://developer.mozilla.org/en-US/docs/Web/Events/timeupdate
// http://w3c.github.io/html/single-page.html#offsets-into-the-media-resource
actualTime = DVRWindow.start;
} else {
return currentTime;
}
return actualTime;
}
function startUpdatingWallclockTime() {
if (wallclockTimeIntervalId !== null) return;
const tick = function () {
onWallclockTime();
};
wallclockTimeIntervalId = setInterval(tick, mediaPlayerModel.getWallclockTimeUpdateInterval());
}
function stopUpdatingWallclockTime() {
clearInterval(wallclockTimeIntervalId);
wallclockTimeIntervalId = null;
}
function updateCurrentTime() {
if (isPaused() || !isDynamic || videoModel.getReadyState() === 0) return;
const currentTime = getTime();
const actualTime = getActualPresentationTime(currentTime);
const timeChanged = (!isNaN(actualTime) && actualTime !== currentTime);
if (timeChanged) {
seek(actualTime);
}
}
function onDataUpdateCompleted(e) {
if (e.error) return;
const representationInfo = adapter.convertDataToRepresentationInfo(e.currentRepresentation);
const info = representationInfo.mediaInfo.streamInfo;
if (streamInfo.id !== info.id) return;
streamInfo = info;
updateCurrentTime();
}
function onCanPlay() {
eventBus.trigger(Events.CAN_PLAY);
}
function onPlaybackStart() {
logger.info('Native video element event: play');
updateCurrentTime();
startUpdatingWallclockTime();
eventBus.trigger(Events.PLAYBACK_STARTED, {
startTime: getTime()
});
}
function onPlaybackWaiting() {
logger.info('Native video element event: waiting');
eventBus.trigger(Events.PLAYBACK_WAITING, {
playingTime: getTime()
});
}
function onPlaybackPlaying() {
logger.info('Native video element event: playing');
eventBus.trigger(Events.PLAYBACK_PLAYING, {
playingTime: getTime()
});
}
function onPlaybackPaused() {
logger.info('Native video element event: pause');
eventBus.trigger(Events.PLAYBACK_PAUSED, {
ended: getEnded()
});
}
function onPlaybackSeeking() {
const seekTime = getTime();
logger.info('Seeking to: ' + seekTime);
startUpdatingWallclockTime();
eventBus.trigger(Events.PLAYBACK_SEEKING, {
seekTime: seekTime
});
}
function onPlaybackSeeked() {
logger.info('Native video element event: seeked');
eventBus.trigger(Events.PLAYBACK_SEEKED);
}
function onPlaybackTimeUpdated() {
const time = getTime();
currentTime = time;
eventBus.trigger(Events.PLAYBACK_TIME_UPDATED, {
timeToEnd: getTimeToStreamEnd(),
time: time
});
}
function updateLivePlaybackTime() {
const now = Date.now();
if (!lastLivePlaybackTime || now > lastLivePlaybackTime + LIVE_UPDATE_PLAYBACK_TIME_INTERVAL_MS) {
lastLivePlaybackTime = now;
onPlaybackTimeUpdated();
}
}
function onPlaybackProgress() {
eventBus.trigger(Events.PLAYBACK_PROGRESS);
}
function onPlaybackRateChanged() {
const rate = getPlaybackRate();
logger.info('Native video element event: ratechange: ', rate);
eventBus.trigger(Events.PLAYBACK_RATE_CHANGED, {
playbackRate: rate
});
}
function onPlaybackMetaDataLoaded() {
logger.info('Native video element event: loadedmetadata');
eventBus.trigger(Events.PLAYBACK_METADATA_LOADED);
startUpdatingWallclockTime();
}
function onPlaybackEnded() {
logger.info('Native video element event: ended');
pause();
stopUpdatingWallclockTime();
eventBus.trigger(Events.PLAYBACK_ENDED);
}
function onPlaybackError(event) {
const target = event.target || event.srcElement;
eventBus.trigger(Events.PLAYBACK_ERROR, {
error: target.error
});
}
function onWallclockTime() {
eventBus.trigger(Events.WALLCLOCK_TIME_UPDATED, {
isDynamic: isDynamic,
time: new Date()
});
// Updates playback time for paused dynamic streams
// (video element doesn't call timeupdate when the playback is paused)
if (getIsDynamic() && isPaused()) {
updateLivePlaybackTime();
}
}
function checkTimeInRanges(time, ranges) {
if (ranges && ranges.length > 0) {
for (let i = 0, len = ranges.length; i < len; i++) {
if (time >= ranges.start(i) && time < ranges.end(i)) {
return true;
}
}
}
return false;
}
function onPlaybackProgression() {
if (isDynamic && mediaPlayerModel.getLowLatencyEnabled() && getCatchUpPlaybackRate() > 0.0) {
if (!catchingUp && needToCatchUp()) {
startPlaybackCatchUp();
} else if (stopCatchingUp()) {
stopPlaybackCatchUp();
}
}
}
function needToCatchUp() {
return getCurrentLiveLatency() > (mediaPlayerModel.getLiveDelay() * (1 + LIVE_CATCHUP_START_THRESHOLD));
}
function stopCatchingUp() {
return getCurrentLiveLatency() <= (mediaPlayerModel.getLiveDelay() );
}
function onBytesAppended(e) {
let earliestTime,
initialStartTime;
let ranges = e.bufferedRanges;
if (!ranges || !ranges.length) return;
if (commonEarliestTime[streamInfo.id] && commonEarliestTime[streamInfo.id].started === true) {
//stream has already been started.
return;
}
const type = e.sender.getType();
if (bufferedRange[streamInfo.id] === undefined) {
bufferedRange[streamInfo.id] = [];
}
bufferedRange[streamInfo.id][type] = ranges;
if (commonEarliestTime[streamInfo.id] === undefined) {
commonEarliestTime[streamInfo.id] = [];
commonEarliestTime[streamInfo.id].started = false;
}
if (commonEarliestTime[streamInfo.id][type] === undefined) {
commonEarliestTime[streamInfo.id][type] = Math.max(ranges.start(0), streamInfo.start);
}
const hasVideoTrack = streamController.isVideoTrackPresent();
const hasAudioTrack = streamController.isAudioTrackPresent();
initialStartTime = getStreamStartTime(false);
if (hasAudioTrack && hasVideoTrack) {
//current stream has audio and video contents
if (!isNaN(commonEarliestTime[streamInfo.id].audio) && !isNaN(commonEarliestTime[streamInfo.id].video)) {
if (commonEarliestTime[streamInfo.id].audio < commonEarliestTime[streamInfo.id].video) {
// common earliest is video time
// check buffered audio range has video time, if ok, we seek, otherwise, we wait some other data
earliestTime = commonEarliestTime[streamInfo.id].video > initialStartTime ? commonEarliestTime[streamInfo.id].video : initialStartTime;
ranges = bufferedRange[streamInfo.id].audio;
} else {
// common earliest is audio time
// check buffered video range has audio time, if ok, we seek, otherwise, we wait some other data
earliestTime = commonEarliestTime[streamInfo.id].audio > initialStartTime ? commonEarliestTime[streamInfo.id].audio : initialStartTime;
ranges = bufferedRange[streamInfo.id].video;
}
if (checkTimeInRanges(earliestTime, ranges)) {
if (!isSeeking()) {
seek(earliestTime);
}
commonEarliestTime[streamInfo.id].started = true;
}
}
} else {
//current stream has only audio or only video content
if (commonEarliestTime[streamInfo.id][type]) {
earliestTime = commonEarliestTime[streamInfo.id][type] > initialStartTime ? commonEarliestTime[streamInfo.id][type] : initialStartTime;
if (!isSeeking()) {
seek(earliestTime);
}
commonEarliestTime[streamInfo.id].started = true;
}
}
}
function onBufferLevelStateChanged(e) {
// do not stall playback when get an event from Stream that is not active
if (e.streamInfo.id !== streamInfo.id) return;
videoModel.setStallState(e.mediaType, e.state === BufferController.BUFFER_EMPTY);
}
function addAllListeners() {
videoModel.addEventListener('canplay', onCanPlay);
videoModel.addEventListener('play', onPlaybackStart);
videoModel.addEventListener('waiting', onPlaybackWaiting);
videoModel.addEventListener('playing', onPlaybackPlaying);
videoModel.addEventListener('pause', onPlaybackPaused);
videoModel.addEventListener('error', onPlaybackError);
videoModel.addEventListener('seeking', onPlaybackSeeking);
videoModel.addEventListener('seeked', onPlaybackSeeked);
videoModel.addEventListener('timeupdate', onPlaybackTimeUpdated);
videoModel.addEventListener('progress', onPlaybackProgress);
videoModel.addEventListener('ratechange', onPlaybackRateChanged);
videoModel.addEventListener('loadedmetadata', onPlaybackMetaDataLoaded);
videoModel.addEventListener('ended', onPlaybackEnded);
}
function removeAllListeners() {
videoModel.removeEventListener('canplay', onCanPlay);
videoModel.removeEventListener('play', onPlaybackStart);
videoModel.removeEventListener('waiting', onPlaybackWaiting);
videoModel.removeEventListener('playing', onPlaybackPlaying);
videoModel.removeEventListener('pause', onPlaybackPaused);
videoModel.removeEventListener('error', onPlaybackError);
videoModel.removeEventListener('seeking', onPlaybackSeeking);
videoModel.removeEventListener('seeked', onPlaybackSeeked);
videoModel.removeEventListener('timeupdate', onPlaybackTimeUpdated);
videoModel.removeEventListener('progress', onPlaybackProgress);
videoModel.removeEventListener('ratechange', onPlaybackRateChanged);
videoModel.removeEventListener('loadedmetadata', onPlaybackMetaDataLoaded);
videoModel.removeEventListener('ended', onPlaybackEnded);
}
instance = {
initialize: initialize,
setConfig: setConfig,
getStartTimeFromUriParameters: getStartTimeFromUriParameters,
getStreamStartTime: getStreamStartTime,
getTimeToStreamEnd: getTimeToStreamEnd,
getTime: getTime,
getPlaybackRate: getPlaybackRate,
getPlayedRanges: getPlayedRanges,
getEnded: getEnded,
getIsDynamic: getIsDynamic,
getStreamController: getStreamController,
setCatchUpPlaybackRate: setCatchUpPlaybackRate,
setLiveStartTime: setLiveStartTime,
getLiveStartTime: getLiveStartTime,
computeLiveDelay: computeLiveDelay,
getLiveDelay: getLiveDelay,
getCurrentLiveLatency: getCurrentLiveLatency,
play: play,
isPaused: isPaused,
pause: pause,
isSeeking: isSeeking,
seek: seek,
reset: reset
};
setup();
return instance;
}
PlaybackController.__dashjs_factory_name = 'PlaybackController';
export default FactoryMaker.getSingletonFactory(PlaybackController);