@gcorevideo/player 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,662 @@
1
+ import { HTML5Video, Playback, Events, Log, Utils } from '@clappr/core';
2
+ import { a as assert, t as trace } from './index-DTOY9tbl.js';
3
+ import DASHJS from 'dashjs';
4
+ import 'hls.js';
5
+ import 'event-lite';
6
+
7
+ // Copyright 2014 Globo.com Player authors. All rights reserved.
8
+ // Use of this source code is governed by a BSD-style
9
+ // license that can be found in the LICENSE file.
10
+ const AUTO = -1;
11
+ const { now } = Utils;
12
+ const T = "DashPlayback";
13
+ class DashPlayback extends HTML5Video {
14
+ _levels = null;
15
+ _currentLevel = null;
16
+ _durationExcludesAfterLiveSyncPoint = false;
17
+ _isReadyState = false;
18
+ _playableRegionDuration = 0;
19
+ _playableRegionStartTime = 0;
20
+ _playbackType = Playback.VOD;
21
+ _playlistType = null;
22
+ // #EXT-X-PROGRAM-DATE-TIME
23
+ _programDateTime = 0;
24
+ _dash = null;
25
+ _extrapolatedWindowDuration = 0;
26
+ _extrapolatedWindowNumSegments = 0;
27
+ _lastDuration = null;
28
+ _lastTimeUpdate = { current: 0, total: 0 };
29
+ _localStartTimeCorrelation = null;
30
+ _localEndTimeCorrelation = null;
31
+ _recoverAttemptsRemaining = 0;
32
+ _recoveredAudioCodecError = false;
33
+ _recoveredDecodingError = false;
34
+ startChangeQuality = false;
35
+ manifestInfo = null;
36
+ // #EXT-X-TARGETDURATION
37
+ _segmentTargetDuration = null;
38
+ _timeUpdateTimer = null;
39
+ get name() {
40
+ return 'dash';
41
+ }
42
+ get levels() {
43
+ return this._levels || [];
44
+ }
45
+ get currentLevel() {
46
+ if (this._currentLevel === null) {
47
+ return AUTO;
48
+ }
49
+ // 0 is a valid level ID
50
+ return this._currentLevel;
51
+ }
52
+ get isReady() {
53
+ return this._isReadyState;
54
+ }
55
+ set currentLevel(id) {
56
+ this._currentLevel = id;
57
+ this.trigger(Events.PLAYBACK_LEVEL_SWITCH_START);
58
+ const cfg = {
59
+ streaming: {
60
+ abr: {
61
+ autoSwitchBitrate: {
62
+ video: id === -1,
63
+ },
64
+ ABRStrategy: 'abrL2A'
65
+ }
66
+ },
67
+ };
68
+ assert.ok(this._dash, 'An instance of dashjs MediaPlayer is required to switch levels');
69
+ const dash = this._dash;
70
+ this.options.dash && dash.updateSettings({ ...this.options.dash, ...cfg });
71
+ if (id !== -1) {
72
+ this._dash.setQualityFor('video', id);
73
+ }
74
+ if (this._playbackType === Playback.VOD) {
75
+ const curr_time = this._dash.time();
76
+ this.startChangeQuality = true;
77
+ dash.seek(0);
78
+ setTimeout(() => {
79
+ dash.seek(curr_time);
80
+ dash.play();
81
+ this.startChangeQuality = false;
82
+ }, 100);
83
+ }
84
+ }
85
+ get _startTime() {
86
+ if (this._playbackType === Playback.LIVE && this._playlistType !== 'EVENT') {
87
+ return this._extrapolatedStartTime;
88
+ }
89
+ return this._playableRegionStartTime;
90
+ }
91
+ get _now() {
92
+ return now();
93
+ }
94
+ // the time in the video element which should represent the start of the sliding window
95
+ // extrapolated to increase in real time (instead of jumping as the early segments are removed)
96
+ get _extrapolatedStartTime() {
97
+ if (!this._localStartTimeCorrelation) {
98
+ return this._playableRegionStartTime;
99
+ }
100
+ const corr = this._localStartTimeCorrelation;
101
+ const timePassed = this._now - corr.local;
102
+ const extrapolatedWindowStartTime = (corr.remote + timePassed) / 1000;
103
+ // cap at the end of the extrapolated window duration
104
+ return Math.min(extrapolatedWindowStartTime, this._playableRegionStartTime + this._extrapolatedWindowDuration);
105
+ }
106
+ // the time in the video element which should represent the end of the content
107
+ // extrapolated to increase in real time (instead of jumping as segments are added)
108
+ get _extrapolatedEndTime() {
109
+ const actualEndTime = this._playableRegionStartTime + this._playableRegionDuration;
110
+ if (!this._localEndTimeCorrelation) {
111
+ return actualEndTime;
112
+ }
113
+ const corr = this._localEndTimeCorrelation;
114
+ const timePassed = this._now - corr.local;
115
+ const extrapolatedEndTime = (corr.remote + timePassed) / 1000;
116
+ return Math.max(actualEndTime - this._extrapolatedWindowDuration, Math.min(extrapolatedEndTime, actualEndTime));
117
+ }
118
+ get _duration() {
119
+ if (!this._dash) {
120
+ return null;
121
+ }
122
+ return this._dash.duration();
123
+ }
124
+ constructor(options, i18n, playerError) {
125
+ super(options, i18n, playerError);
126
+ // backwards compatibility (TODO: remove on 0.3.0)
127
+ // this.options.playback || (this.options.playback = this.options);
128
+ // The size of the start time extrapolation window measured as a multiple of segments.
129
+ // Should be 2 or higher, or 0 to disable. Should only need to be increased above 2 if more than one segment is
130
+ // removed from the start of the playlist at a time. E.g if the playlist is cached for 10 seconds and new chunks are
131
+ // added/removed every 5.
132
+ this._extrapolatedWindowNumSegments = this.options.playback?.extrapolatedWindowNumSegments ?? 2;
133
+ if (this.options.playbackType) {
134
+ this._playbackType = this.options.playbackType;
135
+ }
136
+ // this._lastTimeUpdate = { current: 0, total: 0 };
137
+ // this._lastDuration = null;
138
+ // for hls streams which have dvr with a sliding window,
139
+ // the content at the start of the playlist is removed as new
140
+ // content is appended at the end.
141
+ // this means the actual playable start time will increase as the
142
+ // start content is deleted
143
+ // For streams with dvr where the entire recording is kept from the
144
+ // beginning this should stay as 0
145
+ // this._playableRegionStartTime = 0;
146
+ // {local, remote} remote is the time in the video element that should represent 0
147
+ // local is the system time when the 'remote' measurment took place
148
+ // this._localStartTimeCorrelation = null;
149
+ // {local, remote} remote is the time in the video element that should represents the end
150
+ // local is the system time when the 'remote' measurment took place
151
+ // this._localEndTimeCorrelation = null;
152
+ // if content is removed from the beginning then this empty area should
153
+ // be ignored. "playableRegionDuration" excludes the empty area
154
+ // this._playableRegionDuration = 0;
155
+ // #EXT-X-PROGRAM-DATE-TIME
156
+ // this._programDateTime = 0;
157
+ // this.manifestInfo = null;
158
+ // true when the actual duration is longer than hlsjs's live sync point
159
+ // when this is false playableRegionDuration will be the actual duration
160
+ // when this is true playableRegionDuration will exclude the time after the sync point
161
+ // this._durationExcludesAfterLiveSyncPoint = false;
162
+ // // #EXT-X-TARGETDURATION
163
+ // this._segmentTargetDuration = null;
164
+ // #EXT-X-PLAYLIST-TYPE
165
+ // this._playlistType = null;
166
+ if (this.options.hlsRecoverAttempts) {
167
+ this._recoverAttemptsRemaining = this.options.hlsRecoverAttempts;
168
+ }
169
+ }
170
+ _setup() {
171
+ trace(`${T} _setup`, { el: this.el });
172
+ const dash = DASHJS.MediaPlayer().create();
173
+ this._dash = dash;
174
+ this._dash.initialize();
175
+ const cfg = this.options.dash ?? {};
176
+ cfg.streaming = cfg.streaming || {};
177
+ cfg.streaming.text = cfg.streaming.text || { defaultEnabled: false };
178
+ this.options.dash && this._dash.updateSettings(cfg);
179
+ this._dash.attachView(this.el);
180
+ this._dash.setAutoPlay(false);
181
+ this._dash.attachSource(this.options.src);
182
+ this._dash.on(DASHJS.MediaPlayer.events.ERROR, this._onDASHJSSError);
183
+ this._dash.on(DASHJS.MediaPlayer.events.PLAYBACK_ERROR, this._onPlaybackError);
184
+ this._dash.on(DASHJS.MediaPlayer.events.STREAM_INITIALIZED, () => {
185
+ const bitrates = dash.getBitrateInfoListFor('video');
186
+ trace(`${T} STREAM_INITIALIZED`, { bitrates });
187
+ this._updatePlaybackType();
188
+ this._fillLevels(bitrates);
189
+ dash.on(DASHJS.MediaPlayer.events.QUALITY_CHANGE_REQUESTED, (evt) => {
190
+ // TODO
191
+ assert.ok(this._levels, 'An array of levels is required to change quality');
192
+ const newLevel = this._levels.find((level) => level.id === evt.newQuality); // TODO or simply this._levels[evt.newQuality]?
193
+ assert.ok(newLevel, 'A valid level is required to change quality');
194
+ this.onLevelSwitch(newLevel.level);
195
+ });
196
+ });
197
+ this._dash.on(DASHJS.MediaPlayer.events.METRIC_ADDED, (e) => {
198
+ // console.log(`${T} onMetricAdded`, e);
199
+ // TODO
200
+ // Listen for the first manifest request in order to update player UI
201
+ if (e.metric === 'DVRInfo') { // TODO fix typings
202
+ assert.ok(this._dash, 'An instance of dashjs MediaPlayer is required to get metrics');
203
+ const dvrInfo = this._dash.getDashMetrics().getCurrentDVRInfo('video');
204
+ // trace(`${T} onMetricAdded DVRInfo`, {metric: e.metric, dvrInfo});
205
+ if (dvrInfo) {
206
+ // Extract time info
207
+ this.manifestInfo = dvrInfo.manifestInfo;
208
+ }
209
+ }
210
+ });
211
+ this._dash.on(DASHJS.MediaPlayer.events.PLAYBACK_RATE_CHANGED, () => {
212
+ this.trigger('dash:playback-rate-changed');
213
+ });
214
+ }
215
+ render() {
216
+ this._ready();
217
+ return super.render();
218
+ }
219
+ _ready() {
220
+ this._isReadyState = true;
221
+ this.trigger(Events.PLAYBACK_READY, this.name);
222
+ }
223
+ // TODO
224
+ // _recover(evt, data, error) {
225
+ // console.warn('recover', evt, data, error);
226
+ // assert.ok(this._dash, 'An instance of dashjs MediaPlayer is required to recover');
227
+ // // TODO figure out what's going on here
228
+ // const dash = this._dash;
229
+ // if (!this._recoveredDecodingError) {
230
+ // this._recoveredDecodingError = true;
231
+ // // dash.recoverMediaError();
232
+ // } else if (!this._recoveredAudioCodecError) {
233
+ // this._recoveredAudioCodecError = true;
234
+ // // dash.swapAudioCodec();
235
+ // // dash.recoverMediaError();
236
+ // } else {
237
+ // // TODO what does it have to do with hlsjs?
238
+ // Log.error('hlsjs: failed to recover', { evt, data });
239
+ // error.level = PlayerError.Levels.FATAL;
240
+ // const formattedError = this.createError(error);
241
+ // this.trigger(Events.PLAYBACK_ERROR, formattedError);
242
+ // this.stop();
243
+ // }
244
+ // }
245
+ // override
246
+ _setupSrc() {
247
+ console.log(`${T} _setupSrc`);
248
+ // this playback manages the src on the video element itself
249
+ }
250
+ _startTimeUpdateTimer() {
251
+ this._stopTimeUpdateTimer();
252
+ this._timeUpdateTimer = setInterval(() => {
253
+ this._onDurationChange();
254
+ this._onTimeUpdate();
255
+ }, 100);
256
+ }
257
+ _stopTimeUpdateTimer() {
258
+ if (this._timeUpdateTimer) {
259
+ clearInterval(this._timeUpdateTimer);
260
+ }
261
+ }
262
+ getProgramDateTime() {
263
+ return this._programDateTime;
264
+ }
265
+ // the duration on the video element itself should not be used
266
+ // as this does not necesarily represent the duration of the stream
267
+ // https://github.com/clappr/clappr/issues/668#issuecomment-157036678
268
+ getDuration() {
269
+ assert.ok(this._duration !== null, 'A valid duration is required to get the duration');
270
+ return this._duration;
271
+ }
272
+ getCurrentTime() {
273
+ // e.g. can be < 0 if user pauses near the start
274
+ // eventually they will then be kicked to the end by hlsjs if they run out of buffer
275
+ // before the official start time
276
+ return this._dash ? this._dash.time() : 0;
277
+ }
278
+ // the time that "0" now represents relative to when playback started
279
+ // for a stream with a sliding window this will increase as content is
280
+ // removed from the beginning
281
+ getStartTimeOffset() {
282
+ return this._startTime;
283
+ }
284
+ seekPercentage(percentage) {
285
+ let seekTo = this._duration;
286
+ if (percentage > 0) {
287
+ assert.ok(this._duration !== null, 'A valid duration is required to seek by percentage');
288
+ seekTo = this._duration * (percentage / 100);
289
+ }
290
+ assert.ok(seekTo !== null, 'A valid seek time is required');
291
+ this.seek(seekTo);
292
+ }
293
+ seek(time) {
294
+ if (time < 0) {
295
+ // eslint-disable-next-line max-len
296
+ Log.warn('Attempt to seek to a negative time. Resetting to live point. Use seekToLivePoint() to seek to the live point.');
297
+ time = this.getDuration();
298
+ }
299
+ this.dvrEnabled && this._updateDvr(time < this.getDuration() - 10);
300
+ assert.ok(this._dash, 'An instance of dashjs MediaPlayer is required to seek');
301
+ this._dash.seek(time);
302
+ }
303
+ seekToLivePoint() {
304
+ this.seek(this.getDuration());
305
+ }
306
+ _updateDvr(status) {
307
+ this.trigger(Events.PLAYBACK_DVR, status);
308
+ this.trigger(Events.PLAYBACK_STATS_ADD, { 'dvr': status });
309
+ }
310
+ _updateSettings() {
311
+ if (this._playbackType === Playback.VOD) {
312
+ this.settings.left = ['playpause', 'position', 'duration'];
313
+ // this.settings.left.push('playstop');
314
+ }
315
+ else if (this.dvrEnabled) {
316
+ this.settings.left = ['playpause'];
317
+ }
318
+ else {
319
+ this.settings.left = ['playstop'];
320
+ }
321
+ this.settings.seekEnabled = this.isSeekEnabled();
322
+ this.trigger(Events.PLAYBACK_SETTINGSUPDATE);
323
+ }
324
+ _onPlaybackError = (event) => {
325
+ // TODO
326
+ };
327
+ _onDASHJSSError = (event) => {
328
+ // TODO
329
+ // only report/handle errors if they are fatal
330
+ // hlsjs should automatically handle non fatal errors
331
+ this._stopTimeUpdateTimer();
332
+ if (event.error === 'capability' && event.event === 'mediasource') {
333
+ // No support for MSE
334
+ const formattedError = this.createError(event.error);
335
+ this.trigger(Events.PLAYBACK_ERROR, formattedError);
336
+ Log.error('The media cannot be played because it requires a feature ' +
337
+ 'that your browser does not support.');
338
+ }
339
+ else if (event.error === 'manifestError' && (
340
+ // Manifest type not supported
341
+ (event.event.id === 'createParser') ||
342
+ // Codec(s) not supported
343
+ (event.event.id === 'codec') ||
344
+ // No streams available to stream
345
+ (event.event.id === 'nostreams') ||
346
+ // Error creating Stream object
347
+ (event.event.id === 'nostreamscomposed') ||
348
+ // syntax error parsing the manifest
349
+ (event.event.id === 'parse') ||
350
+ // a stream has multiplexed audio+video
351
+ (event.event.id === 'multiplexedrep'))) {
352
+ // These errors have useful error messages, so we forward it on
353
+ const formattedError = this.createError(event.error);
354
+ this.trigger(Events.PLAYBACK_ERROR, formattedError);
355
+ if (event.error) {
356
+ Log.error(event.event.message);
357
+ }
358
+ }
359
+ else if (event.error === 'mediasource') {
360
+ // This error happens when dash.js fails to allocate a SourceBuffer
361
+ // OR the underlying video element throws a `MediaError`.
362
+ // If it's a buffer allocation fail, the message states which buffer
363
+ // (audio/video/text) failed allocation.
364
+ // If it's a `MediaError`, dash.js inspects the error object for
365
+ // additional information to append to the error type.
366
+ const formattedError = this.createError(event.error);
367
+ this.trigger(Events.PLAYBACK_ERROR, formattedError);
368
+ Log.error(event.event);
369
+ }
370
+ else if (event.error === 'capability' && event.event === 'encryptedmedia') {
371
+ // Browser doesn't support EME
372
+ const formattedError = this.createError(event.error);
373
+ this.trigger(Events.PLAYBACK_ERROR, formattedError);
374
+ Log.error('The media cannot be played because it requires encryption ' +
375
+ 'that your browser does not support.');
376
+ }
377
+ else if (event.error === 'key_session') {
378
+ // This block handles pretty much all errors thrown by the
379
+ // encryption subsystem
380
+ const formattedError = this.createError(event.error);
381
+ this.trigger(Events.PLAYBACK_ERROR, formattedError);
382
+ Log.error(event.event);
383
+ }
384
+ else if (event.error === 'download') {
385
+ const formattedError = this.createError(event.error);
386
+ this.trigger(Events.PLAYBACK_ERROR, formattedError);
387
+ Log.error('The media playback was aborted because too many consecutive ' +
388
+ 'download errors occurred.');
389
+ // } else if (event.error === 'mssError') {
390
+ // const formattedError = this.createError(event.error);
391
+ // this.trigger(Events.PLAYBACK_ERROR, formattedError);
392
+ // if (event.error) {
393
+ // Log.error(event.error.message);
394
+ // }
395
+ }
396
+ else {
397
+ // ignore the error
398
+ if (typeof event.error === "object") {
399
+ const formattedError = this.createError(event.error);
400
+ this.trigger(Events.PLAYBACK_ERROR, formattedError);
401
+ Log.error(event.error.message);
402
+ }
403
+ else {
404
+ Log.error(event.error);
405
+ }
406
+ return;
407
+ }
408
+ // only reset the dash player in 10ms async, so that the rest of the
409
+ // calling function finishes
410
+ setTimeout(() => {
411
+ assert.ok(this._dash, 'An instance of dashjs MediaPlayer is required to reset');
412
+ this._dash.reset();
413
+ }, 10);
414
+ };
415
+ _onTimeUpdate() {
416
+ if (this.startChangeQuality) {
417
+ return;
418
+ }
419
+ const update = {
420
+ current: this.getCurrentTime(),
421
+ total: this.getDuration(),
422
+ firstFragDateTime: this.getProgramDateTime()
423
+ };
424
+ const isSame = this._lastTimeUpdate && (update.current === this._lastTimeUpdate.current &&
425
+ update.total === this._lastTimeUpdate.total);
426
+ if (isSame) {
427
+ return;
428
+ }
429
+ this._lastTimeUpdate = update;
430
+ this.trigger(Events.PLAYBACK_TIMEUPDATE, update, this.name);
431
+ }
432
+ _onDurationChange() {
433
+ const duration = this.getDuration();
434
+ if (this._lastDuration === duration) {
435
+ return;
436
+ }
437
+ this._lastDuration = duration;
438
+ super._onDurationChange();
439
+ }
440
+ get dvrEnabled() {
441
+ assert.ok(this._dash, 'An instance of dashjs MediaPlayer is required to get the DVR status');
442
+ return this._dash?.getDVRWindowSize() >= this._minDvrSize && this.getPlaybackType() === Playback.LIVE;
443
+ }
444
+ _onProgress() {
445
+ if (!this._dash) {
446
+ return;
447
+ }
448
+ let buffer = this._dash.getDashMetrics().getCurrentBufferLevel('video');
449
+ if (!buffer) {
450
+ buffer = this._dash.getDashMetrics().getCurrentBufferLevel('audio');
451
+ }
452
+ const progress = {
453
+ start: this.getCurrentTime(),
454
+ current: this.getCurrentTime() + buffer,
455
+ total: this.getDuration()
456
+ };
457
+ this.trigger(Events.PLAYBACK_PROGRESS, progress, {});
458
+ }
459
+ play() {
460
+ trace(`${T} play`, { dash: !!this._dash });
461
+ if (!this._dash) {
462
+ this._setup();
463
+ }
464
+ super.play();
465
+ this._startTimeUpdateTimer();
466
+ }
467
+ pause() {
468
+ if (!this._dash) {
469
+ return;
470
+ }
471
+ super.pause();
472
+ if (this.dvrEnabled) {
473
+ this._updateDvr(true);
474
+ }
475
+ }
476
+ stop() {
477
+ if (this._dash) {
478
+ this._stopTimeUpdateTimer();
479
+ this._dash.reset();
480
+ super.stop();
481
+ this._dash = null;
482
+ }
483
+ }
484
+ destroy() {
485
+ this._stopTimeUpdateTimer();
486
+ if (this._dash) {
487
+ this._dash.off(DASHJS.MediaPlayer.events.ERROR, this._onDASHJSSError);
488
+ this._dash.off(DASHJS.MediaPlayer.events.PLAYBACK_ERROR, this._onPlaybackError);
489
+ this._dash.off(DASHJS.MediaPlayer.events.MANIFEST_LOADED, this.getDuration);
490
+ this._dash.reset();
491
+ }
492
+ this._dash = null;
493
+ return super.destroy();
494
+ }
495
+ _updatePlaybackType() {
496
+ assert.ok(this._dash, 'An instance of dashjs MediaPlayer is required to update the playback type');
497
+ this._playbackType = this._dash.isDynamic() ? Playback.LIVE : Playback.VOD;
498
+ trace(`${T} _updatePlaybackType`, {
499
+ playbackType: this._playbackType,
500
+ });
501
+ }
502
+ _fillLevels(levels) {
503
+ // trace(`${T} _fillLevels`, {levels});
504
+ // TOOD check that levels[i].qualityIndex === i
505
+ this._levels = levels.map((level) => {
506
+ return { id: level.qualityIndex, level: level };
507
+ });
508
+ this.trigger(Events.PLAYBACK_LEVELS_AVAILABLE, this._levels);
509
+ }
510
+ // _onLevelUpdated(_: any, data) {
511
+ // this._segmentTargetDuration = data.details.targetduration;
512
+ // this._playlistType = data.details.type || null;
513
+ // let startTimeChanged = false;
514
+ // let durationChanged = false;
515
+ // const fragments = data.details.fragments;
516
+ // const previousPlayableRegionStartTime = this._playableRegionStartTime;
517
+ // const previousPlayableRegionDuration = this._playableRegionDuration;
518
+ // if (fragments.length === 0) {
519
+ // return;
520
+ // }
521
+ // // #EXT-X-PROGRAM-DATE-TIME
522
+ // if (fragments[0].rawProgramDateTime) {
523
+ // this._programDateTime = fragments[0].rawProgramDateTime;
524
+ // }
525
+ // if (this._playableRegionStartTime !== fragments[0].start) {
526
+ // startTimeChanged = true;
527
+ // this._playableRegionStartTime = fragments[0].start;
528
+ // }
529
+ // if (startTimeChanged) {
530
+ // if (!this._localStartTimeCorrelation) {
531
+ // // set the correlation to map to middle of the extrapolation window
532
+ // this._localStartTimeCorrelation = {
533
+ // local: this._now,
534
+ // remote: (fragments[0].start + (this._extrapolatedWindowDuration / 2)) * 1000
535
+ // };
536
+ // } else {
537
+ // // check if the correlation still works
538
+ // const corr = this._localStartTimeCorrelation;
539
+ // const timePassed = this._now - corr.local;
540
+ // // this should point to a time within the extrapolation window
541
+ // const startTime = (corr.remote + timePassed) / 1000;
542
+ // if (startTime < fragments[0].start) {
543
+ // // our start time is now earlier than the first chunk
544
+ // // (maybe the chunk was removed early)
545
+ // // reset correlation so that it sits at the beginning of the first available chunk
546
+ // this._localStartTimeCorrelation = {
547
+ // local: this._now,
548
+ // remote: fragments[0].start * 1000
549
+ // };
550
+ // } else if (startTime > previousPlayableRegionStartTime + this._extrapolatedWindowDuration) {
551
+ // // start time was past the end of the old extrapolation window (so would have been capped)
552
+ // // see if now that time would be inside the window, and if it would be set the correlation
553
+ // // so that it resumes from the time it was at at the end of the old window
554
+ // // update the correlation so that the time starts counting again from the value it's on now
555
+ // this._localStartTimeCorrelation = {
556
+ // local: this._now,
557
+ // remote: Math.max(
558
+ // fragments[0].start,
559
+ // previousPlayableRegionStartTime + this._extrapolatedWindowDuration
560
+ // ) * 1000
561
+ // };
562
+ // }
563
+ // }
564
+ // }
565
+ // let newDuration = data.details.totalduration;
566
+ // // if it's a live stream then shorten the duration to remove access
567
+ // // to the area after hlsjs's live sync point
568
+ // // seeks to areas after this point sometimes have issues
569
+ // if (this._playbackType === Playback.LIVE) {
570
+ // const fragmentTargetDuration = data.details.targetduration;
571
+ // const hlsjsConfig = this.options.playback.hlsjsConfig || {};
572
+ // // eslint-disable-next-line no-undef
573
+ // const liveSyncDurationCount = hlsjsConfig.liveSyncDurationCount || HLSJS.DefaultConfig.liveSyncDurationCount;
574
+ // const hiddenAreaDuration = fragmentTargetDuration * liveSyncDurationCount;
575
+ // if (hiddenAreaDuration <= newDuration) {
576
+ // newDuration -= hiddenAreaDuration;
577
+ // this._durationExcludesAfterLiveSyncPoint = true;
578
+ // } else {
579
+ // this._durationExcludesAfterLiveSyncPoint = false;
580
+ // }
581
+ // }
582
+ // if (newDuration !== this._playableRegionDuration) {
583
+ // durationChanged = true;
584
+ // this._playableRegionDuration = newDuration;
585
+ // }
586
+ // // Note the end time is not the playableRegionDuration
587
+ // // The end time will always increase even if content is removed from the beginning
588
+ // const endTime = fragments[0].start + newDuration;
589
+ // const previousEndTime = previousPlayableRegionStartTime + previousPlayableRegionDuration;
590
+ // const endTimeChanged = endTime !== previousEndTime;
591
+ // if (endTimeChanged) {
592
+ // if (!this._localEndTimeCorrelation) {
593
+ // // set the correlation to map to the end
594
+ // this._localEndTimeCorrelation = {
595
+ // local: this._now,
596
+ // remote: endTime * 1000
597
+ // };
598
+ // } else {
599
+ // // check if the correlation still works
600
+ // const corr = this._localEndTimeCorrelation;
601
+ // const timePassed = this._now - corr.local;
602
+ // // this should point to a time within the extrapolation window from the end
603
+ // const extrapolatedEndTime = (corr.remote + timePassed) / 1000;
604
+ // if (extrapolatedEndTime > endTime) {
605
+ // this._localEndTimeCorrelation = {
606
+ // local: this._now,
607
+ // remote: endTime * 1000
608
+ // };
609
+ // } else if (extrapolatedEndTime < endTime - this._extrapolatedWindowDuration) {
610
+ // // our extrapolated end time is now earlier than the extrapolation window from the actual end time
611
+ // // (maybe a chunk became available early)
612
+ // // reset correlation so that it sits at the beginning of the extrapolation window from the end time
613
+ // this._localEndTimeCorrelation = {
614
+ // local: this._now,
615
+ // remote: (endTime - this._extrapolatedWindowDuration) * 1000
616
+ // };
617
+ // } else if (extrapolatedEndTime > previousEndTime) {
618
+ // // end time was past the old end time (so would have been capped)
619
+ // // set the correlation so that it resumes from the time it was at at the end of the old window
620
+ // this._localEndTimeCorrelation = {
621
+ // local: this._now,
622
+ // remote: previousEndTime * 1000
623
+ // };
624
+ // }
625
+ // }
626
+ // }
627
+ // // now that the values have been updated call any methods that use on them so they get the updated values
628
+ // // immediately
629
+ // durationChanged && this._onDurationChange();
630
+ // startTimeChanged && this._onProgress();
631
+ // }
632
+ // _onFragmentLoaded(evt, data) {
633
+ // this.trigger(Events.PLAYBACK_FRAGMENT_LOADED, data);
634
+ // }
635
+ onLevelSwitch(currentLevel) {
636
+ trace(`${T} onLevelSwitch`, { currentLevel });
637
+ this.trigger(Events.PLAYBACK_BITRATE, {
638
+ height: currentLevel.height,
639
+ width: currentLevel.width,
640
+ bitrate: currentLevel.bitrate,
641
+ level: currentLevel.qualityIndex
642
+ });
643
+ }
644
+ getPlaybackType() {
645
+ return this._playbackType;
646
+ }
647
+ isSeekEnabled() {
648
+ return (this._playbackType === Playback.VOD || this.dvrEnabled);
649
+ }
650
+ }
651
+ DashPlayback.canPlay = function (resource, mimeType) {
652
+ console.log(`${T} canPlay resource:%s mimeType:%s`, resource, mimeType);
653
+ const resourceParts = resource.split('?')[0].match(/.*\.(.*)$/) || [];
654
+ const isDash = ((resourceParts.length > 1 && resourceParts[1].toLowerCase() === 'mpd') ||
655
+ mimeType === 'application/dash+xml' || mimeType === 'video/mp4');
656
+ const ctor = window.MediaSource || ('WebKitMediaSource' in window ? window.WebKitMediaSource : undefined);
657
+ const isSupportByBrowser = typeof ctor === 'function';
658
+ console.log(`${T} canPlay isSupportByBrowser:%s isDash:%s`, isSupportByBrowser, isDash);
659
+ return !!(isSupportByBrowser && isDash);
660
+ };
661
+
662
+ export { DashPlayback as default };