@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,750 @@
1
+ import { HTML5Video, Playback, Events, Log, PlayerError, Utils } from '@clappr/core';
2
+ import { a as assert } from './index-DTOY9tbl.js';
3
+ import HLSJS from 'hls.js';
4
+ import 'event-lite';
5
+
6
+ // Copyright 2014 Globo.com Player authors. All rights reserved.
7
+ // Use of this source code is governed by a BSD-style
8
+ // license that can be found in the LICENSE file.
9
+ const { now, listContainsIgnoreCase } = Utils;
10
+ assert(process.env.CLAPPR_VERSION, 'CLAPPR_VERSION is required');
11
+ const CLAPPR_VERSION = process.env.CLAPPR_VERSION;
12
+ const AUTO = -1;
13
+ const DEFAULT_RECOVER_ATTEMPTS = 16;
14
+ Events.register('PLAYBACK_FRAGMENT_CHANGED');
15
+ Events.register('PLAYBACK_FRAGMENT_PARSING_METADATA');
16
+ class HlsPlayback extends HTML5Video {
17
+ _ccIsSetup = false;
18
+ _ccTracksUpdated = false;
19
+ _currentFragment = null;
20
+ _currentLevel = null;
21
+ _durationExcludesAfterLiveSyncPoint = false;
22
+ _extrapolatedWindowNumSegments = 0; // TODO
23
+ highDefinition = false;
24
+ _hls = null;
25
+ _isReadyState = false;
26
+ _lastDuration = null;
27
+ _lastTimeUpdate = null;
28
+ _levels = null;
29
+ _localStartTimeCorrelation = null;
30
+ _localEndTimeCorrelation = null;
31
+ _manifestParsed = false;
32
+ _playableRegionDuration = 0;
33
+ _playbackType = Playback.VOD;
34
+ _playlistType = null;
35
+ _playableRegionStartTime = 0;
36
+ _programDateTime = null;
37
+ _recoverAttemptsRemaining = 0;
38
+ _recoveredAudioCodecError = false;
39
+ _recoveredDecodingError = false;
40
+ _segmentTargetDuration = null;
41
+ _timeUpdateTimer = null;
42
+ get name() {
43
+ return 'hls';
44
+ }
45
+ get supportedVersion() {
46
+ return { min: CLAPPR_VERSION };
47
+ }
48
+ get levels() {
49
+ return this._levels || [];
50
+ }
51
+ get currentLevel() {
52
+ return this._currentLevel ?? AUTO;
53
+ }
54
+ get isReady() {
55
+ return this._isReadyState;
56
+ }
57
+ set currentLevel(id) {
58
+ this._currentLevel = id;
59
+ this.trigger(Events.PLAYBACK_LEVEL_SWITCH_START);
60
+ assert.ok(this._hls, 'Hls.js instance is not available');
61
+ if (this.options.playback.hlsUseNextLevel) {
62
+ this._hls.nextLevel = this._currentLevel;
63
+ }
64
+ else {
65
+ this._hls.currentLevel = this._currentLevel;
66
+ }
67
+ }
68
+ get latency() {
69
+ assert.ok(this._hls, 'Hls.js instance is not available');
70
+ return this._hls.latency;
71
+ }
72
+ get currentProgramDateTime() {
73
+ assert.ok(this._hls, 'Hls.js instance is not available');
74
+ assert.ok(this._hls.playingDate, 'Hls.js playingDate is not defined');
75
+ return this._hls.playingDate;
76
+ }
77
+ get _startTime() {
78
+ if (this._playbackType === Playback.LIVE && this._playlistType !== 'EVENT') {
79
+ return this._extrapolatedStartTime;
80
+ }
81
+ return this._playableRegionStartTime;
82
+ }
83
+ get _now() {
84
+ return now();
85
+ }
86
+ // the time in the video element which should represent the start of the sliding window
87
+ // extrapolated to increase in real time (instead of jumping as the early segments are removed)
88
+ get _extrapolatedStartTime() {
89
+ if (!this._localStartTimeCorrelation) {
90
+ return this._playableRegionStartTime;
91
+ }
92
+ const corr = this._localStartTimeCorrelation;
93
+ const timePassed = this._now - corr.local;
94
+ const extrapolatedWindowStartTime = (corr.remote + timePassed) / 1000;
95
+ // cap at the end of the extrapolated window duration
96
+ return Math.min(extrapolatedWindowStartTime, this._playableRegionStartTime + this._extrapolatedWindowDuration);
97
+ }
98
+ // the time in the video element which should represent the end of the content
99
+ // extrapolated to increase in real time (instead of jumping as segments are added)
100
+ get _extrapolatedEndTime() {
101
+ const actualEndTime = this._playableRegionStartTime + this._playableRegionDuration;
102
+ if (!this._localEndTimeCorrelation) {
103
+ return actualEndTime;
104
+ }
105
+ const correlation = this._localEndTimeCorrelation;
106
+ const timePassed = this._now - correlation.local;
107
+ const extrapolatedEndTime = (correlation.remote + timePassed) / 1000;
108
+ return Math.max(actualEndTime - this._extrapolatedWindowDuration, Math.min(extrapolatedEndTime, actualEndTime));
109
+ }
110
+ get _duration() {
111
+ return this._extrapolatedEndTime - this._startTime;
112
+ }
113
+ // Returns the duration (seconds) of the window that the extrapolated start time is allowed
114
+ // to move in before being capped.
115
+ // The extrapolated start time should never reach the cap at the end of the window as the
116
+ // window should slide as chunks are removed from the start.
117
+ // This also applies to the extrapolated end time in the same way.
118
+ //
119
+ // If chunks aren't being removed for some reason that the start time will reach and remain fixed at
120
+ // playableRegionStartTime + extrapolatedWindowDuration
121
+ //
122
+ // <-- window duration -->
123
+ // I.e playableRegionStartTime |-----------------------|
124
+ // | --> . . .
125
+ // . --> | --> . .
126
+ // . . --> | --> .
127
+ // . . . --> |
128
+ // . . . .
129
+ // extrapolatedStartTime
130
+ get _extrapolatedWindowDuration() {
131
+ if (this._segmentTargetDuration === null) {
132
+ return 0;
133
+ }
134
+ return this._extrapolatedWindowNumSegments * this._segmentTargetDuration;
135
+ }
136
+ get bandwidthEstimate() {
137
+ return this._hls && this._hls.bandwidthEstimate;
138
+ }
139
+ get defaultOptions() {
140
+ return { preload: true };
141
+ }
142
+ get customListeners() {
143
+ return this.options.hlsPlayback && this.options.hlsPlayback.customListeners || [];
144
+ }
145
+ get sourceMedia() {
146
+ return this.options.src;
147
+ }
148
+ get currentTimestamp() {
149
+ if (!this._currentFragment) {
150
+ return null;
151
+ }
152
+ assert(this._currentFragment.programDateTime !== null, 'Hls.js programDateTime is not defined');
153
+ const startTime = this._currentFragment.programDateTime;
154
+ const playbackTime = this.el.currentTime;
155
+ const playTimeOffSet = playbackTime - this._currentFragment.start;
156
+ const currentTimestampInMs = startTime + playTimeOffSet * 1000;
157
+ return currentTimestampInMs / 1000;
158
+ }
159
+ static get HLSJS() {
160
+ return HLSJS;
161
+ }
162
+ constructor(...args) {
163
+ // @ts-ignore
164
+ super(...args);
165
+ this.options.hlsPlayback = { ...this.defaultOptions, ...this.options.hlsPlayback };
166
+ this._setInitialState();
167
+ }
168
+ _setInitialState() {
169
+ // @ts-ignore
170
+ this._minDvrSize = typeof (this.options.hlsMinimumDvrSize) === 'undefined' ? 60 : this.options.hlsMinimumDvrSize;
171
+ // The size of the start time extrapolation window measured as a multiple of segments.
172
+ // Should be 2 or higher, or 0 to disable. Should only need to be increased above 2 if more than one segment is
173
+ // removed from the start of the playlist at a time. E.g if the playlist is cached for 10 seconds and new chunks are
174
+ // added/removed every 5.
175
+ this._extrapolatedWindowNumSegments = !this.options.playback || typeof (this.options.playback.extrapolatedWindowNumSegments) === 'undefined' ? 2 : this.options.playback.extrapolatedWindowNumSegments;
176
+ this._playbackType = Playback.VOD;
177
+ this._lastTimeUpdate = { current: 0, total: 0 };
178
+ this._lastDuration = null;
179
+ // for hls streams which have dvr with a sliding window,
180
+ // the content at the start of the playlist is removed as new
181
+ // content is appended at the end.
182
+ // this means the actual playable start time will increase as the
183
+ // start content is deleted
184
+ // For streams with dvr where the entire recording is kept from the
185
+ // beginning this should stay as 0
186
+ this._playableRegionStartTime = 0;
187
+ // {local, remote} remote is the time in the video element that should represent 0
188
+ // local is the system time when the 'remote' measurment took place
189
+ this._localStartTimeCorrelation = null;
190
+ // {local, remote} remote is the time in the video element that should represents the end
191
+ // local is the system time when the 'remote' measurment took place
192
+ this._localEndTimeCorrelation = null;
193
+ // if content is removed from the beginning then this empty area should
194
+ // be ignored. "playableRegionDuration" excludes the empty area
195
+ this._playableRegionDuration = 0;
196
+ // #EXT-X-PROGRAM-DATE-TIME
197
+ this._programDateTime = null;
198
+ // true when the actual duration is longer than hlsjs's live sync point
199
+ // when this is false playableRegionDuration will be the actual duration
200
+ // when this is true playableRegionDuration will exclude the time after the sync point
201
+ this._durationExcludesAfterLiveSyncPoint = false;
202
+ // #EXT-X-TARGETDURATION
203
+ this._segmentTargetDuration = null;
204
+ // #EXT-X-PLAYLIST-TYPE
205
+ this._playlistType = null;
206
+ // TODO options.hlsRecoverAttempts
207
+ this._recoverAttemptsRemaining = this.options.hlsRecoverAttempts || DEFAULT_RECOVER_ATTEMPTS;
208
+ }
209
+ _setup() {
210
+ this._destroyHLSInstance();
211
+ this._createHLSInstance();
212
+ this._listenHLSEvents();
213
+ this._attachHLSMedia();
214
+ }
215
+ _destroyHLSInstance() {
216
+ if (!this._hls) {
217
+ return;
218
+ }
219
+ this._manifestParsed = false;
220
+ this._ccIsSetup = false;
221
+ this._ccTracksUpdated = false;
222
+ this._setInitialState();
223
+ this._hls.destroy();
224
+ this._hls = null;
225
+ }
226
+ _createHLSInstance() {
227
+ const config = {
228
+ ...this.options.playback.hlsjsConfig,
229
+ maxBufferLength: 2,
230
+ maxMaxBufferLength: 4,
231
+ };
232
+ this._hls = new HLSJS(config);
233
+ }
234
+ _attachHLSMedia() {
235
+ if (!this._hls) {
236
+ return;
237
+ }
238
+ this._hls.attachMedia(this.el);
239
+ }
240
+ _listenHLSEvents() {
241
+ if (!this._hls) {
242
+ return;
243
+ }
244
+ this._hls.once(HLSJS.Events.MEDIA_ATTACHED, () => {
245
+ assert.ok(this._hls, 'Hls.js instance is not available');
246
+ this.options.hlsPlayback.preload && this._hls.loadSource(this.options.src);
247
+ });
248
+ const onPlaying = () => {
249
+ if (this._hls) {
250
+ this._hls.config.maxBufferLength = this.options.hlsPlayback.maxBufferLength || 30;
251
+ this._hls.config.maxMaxBufferLength = this.options.hlsPlayback.maxMaxBufferLength || 60;
252
+ }
253
+ this.el.removeEventListener('playing', onPlaying);
254
+ };
255
+ this.el.addEventListener('playing', onPlaying);
256
+ this._hls.on(HLSJS.Events.MANIFEST_PARSED, () => this._manifestParsed = true);
257
+ this._hls.on(HLSJS.Events.LEVEL_LOADED, (evt, data) => this._updatePlaybackType(evt, data));
258
+ this._hls.on(HLSJS.Events.LEVEL_UPDATED, (evt, data) => this._onLevelUpdated(evt, data));
259
+ this._hls.on(HLSJS.Events.LEVEL_SWITCHING, (evt, data) => this._onLevelSwitch(evt, data));
260
+ this._hls.on(HLSJS.Events.FRAG_CHANGED, (evt, data) => this._onFragmentChanged(evt, data));
261
+ this._hls.on(HLSJS.Events.FRAG_LOADED, (evt, data) => this._onFragmentLoaded(evt, data));
262
+ this._hls.on(HLSJS.Events.FRAG_PARSING_METADATA, (evt, data) => this._onFragmentParsingMetadata(evt, data));
263
+ this._hls.on(HLSJS.Events.ERROR, (evt, data) => this._onHLSJSError(evt, data));
264
+ // this._hls.on(HLSJS.Events.SUBTITLE_TRACK_LOADED, (evt, data) => this._onSubtitleLoaded(evt, data));
265
+ this._hls.on(HLSJS.Events.SUBTITLE_TRACK_LOADED, () => this._onSubtitleLoaded());
266
+ this._hls.on(HLSJS.Events.SUBTITLE_TRACKS_UPDATED, () => this._ccTracksUpdated = true);
267
+ this.bindCustomListeners();
268
+ }
269
+ bindCustomListeners() {
270
+ this.customListeners.forEach((item) => {
271
+ const requestedEventName = item.eventName;
272
+ const typeOfListener = item.once ? 'once' : 'on';
273
+ assert.ok(this._hls, 'Hls.js instance is not available');
274
+ requestedEventName && this._hls[`${typeOfListener}`](requestedEventName, item.callback);
275
+ });
276
+ }
277
+ unbindCustomListeners() {
278
+ this.customListeners.forEach((item) => {
279
+ const requestedEventName = item.eventName;
280
+ assert.ok(this._hls, 'Hls.js instance is not available');
281
+ requestedEventName && this._hls.off(requestedEventName, item.callback);
282
+ });
283
+ }
284
+ _onFragmentParsingMetadata(evt, data) {
285
+ // @ts-ignore
286
+ this.trigger(Events.Custom.PLAYBACK_FRAGMENT_PARSING_METADATA, { evt, data });
287
+ }
288
+ render() {
289
+ this._ready();
290
+ return super.render();
291
+ }
292
+ _ready() {
293
+ if (this._isReadyState) {
294
+ return;
295
+ }
296
+ !this._hls && this._setup();
297
+ this._isReadyState = true;
298
+ this.trigger(Events.PLAYBACK_READY, this.name);
299
+ }
300
+ _recover(evt, data, error) {
301
+ if (!this._recoveredDecodingError) {
302
+ this._recoveredDecodingError = true;
303
+ assert(this._hls, 'Hls.js instance is not available');
304
+ this._hls.recoverMediaError();
305
+ }
306
+ else if (!this._recoveredAudioCodecError) {
307
+ this._recoveredAudioCodecError = true;
308
+ assert(this._hls, 'Hls.js instance is not available');
309
+ this._hls.swapAudioCodec();
310
+ this._hls.recoverMediaError();
311
+ }
312
+ else {
313
+ Log.error('hlsjs: failed to recover', { evt, data });
314
+ error.level = PlayerError.Levels.FATAL;
315
+ const formattedError = this.createError(error);
316
+ this.trigger(Events.PLAYBACK_ERROR, formattedError);
317
+ this.stop();
318
+ }
319
+ }
320
+ // override
321
+ // this playback manages the src on the video element itself
322
+ _setupSrc(srcUrl) { } // eslint-disable-line no-unused-vars
323
+ _startTimeUpdateTimer() {
324
+ if (this._timeUpdateTimer) {
325
+ return;
326
+ }
327
+ this._timeUpdateTimer = setInterval(() => {
328
+ this._onDurationChange();
329
+ this._onTimeUpdate();
330
+ }, 100);
331
+ }
332
+ _stopTimeUpdateTimer() {
333
+ if (!this._timeUpdateTimer) {
334
+ return;
335
+ }
336
+ clearInterval(this._timeUpdateTimer);
337
+ this._timeUpdateTimer = null;
338
+ }
339
+ getProgramDateTime() {
340
+ return this._programDateTime;
341
+ }
342
+ // the duration on the video element itself should not be used
343
+ // as this does not necesarily represent the duration of the stream
344
+ // https://github.com/clappr/clappr/issues/668#issuecomment-157036678
345
+ getDuration() {
346
+ return this._duration;
347
+ }
348
+ getCurrentTime() {
349
+ // e.g. can be < 0 if user pauses near the start
350
+ // eventually they will then be kicked to the end by hlsjs if they run out of buffer
351
+ // before the official start time
352
+ return Math.max(0, this.el.currentTime - this._startTime);
353
+ }
354
+ // the time that "0" now represents relative to when playback started
355
+ // for a stream with a sliding window this will increase as content is
356
+ // removed from the beginning
357
+ getStartTimeOffset() {
358
+ return this._startTime;
359
+ }
360
+ seekPercentage(percentage) {
361
+ const seekTo = (percentage > 0)
362
+ ? this._duration * (percentage / 100)
363
+ : this._duration;
364
+ this.seek(seekTo);
365
+ }
366
+ seek(time) {
367
+ if (time < 0) {
368
+ Log.warn('Attempt to seek to a negative time. Resetting to live point. Use seekToLivePoint() to seek to the live point.');
369
+ time = this.getDuration();
370
+ }
371
+ // assume live if time within 3 seconds of end of stream
372
+ this.dvrEnabled && this._updateDvr(time < this.getDuration() - 3);
373
+ time += this._startTime;
374
+ this.el.currentTime = time;
375
+ }
376
+ seekToLivePoint() {
377
+ this.seek(this.getDuration());
378
+ }
379
+ _updateDvr(status) {
380
+ this.trigger(Events.PLAYBACK_DVR, status);
381
+ this.trigger(Events.PLAYBACK_STATS_ADD, { 'dvr': status });
382
+ }
383
+ _updateSettings() {
384
+ if (this._playbackType === Playback.VOD) {
385
+ this.settings.left = ['playpause', 'position', 'duration'];
386
+ }
387
+ else if (this.dvrEnabled) {
388
+ this.settings.left = ['playpause'];
389
+ }
390
+ else {
391
+ this.settings.left = ['playstop'];
392
+ }
393
+ this.settings.seekEnabled = this.isSeekEnabled();
394
+ this.trigger(Events.PLAYBACK_SETTINGSUPDATE);
395
+ }
396
+ _onHLSJSError(evt, data) {
397
+ const error = {
398
+ code: `${data.type}_${data.details}`,
399
+ description: `${this.name} error: type: ${data.type}, details: ${data.details}`,
400
+ raw: data,
401
+ };
402
+ let formattedError;
403
+ if (data.response) {
404
+ error.description += `, response: ${JSON.stringify(data.response)}`;
405
+ }
406
+ // only report/handle errors if they are fatal
407
+ // hlsjs should automatically handle non fatal errors
408
+ if (data.fatal) {
409
+ if (this._recoverAttemptsRemaining > 0) {
410
+ this._recoverAttemptsRemaining -= 1;
411
+ switch (data.type) {
412
+ case HLSJS.ErrorTypes.NETWORK_ERROR:
413
+ switch (data.details) {
414
+ // The following network errors cannot be recovered with HLS.startLoad()
415
+ // For more details, see https://github.com/video-dev/hls.js/blob/master/doc/design.md#error-detection-and-handling
416
+ // For "level load" fatal errors, see https://github.com/video-dev/hls.js/issues/1138
417
+ case HLSJS.ErrorDetails.MANIFEST_LOAD_ERROR:
418
+ case HLSJS.ErrorDetails.MANIFEST_LOAD_TIMEOUT:
419
+ case HLSJS.ErrorDetails.MANIFEST_PARSING_ERROR:
420
+ case HLSJS.ErrorDetails.LEVEL_LOAD_ERROR:
421
+ case HLSJS.ErrorDetails.LEVEL_LOAD_TIMEOUT:
422
+ Log.error('hlsjs: unrecoverable network fatal error.', { evt, data });
423
+ formattedError = this.createError(error);
424
+ this.trigger(Events.PLAYBACK_ERROR, formattedError);
425
+ this.stop();
426
+ break;
427
+ default:
428
+ Log.warn('hlsjs: trying to recover from network error.', { evt, data });
429
+ error.level = PlayerError.Levels.WARN;
430
+ assert(this._hls, 'Hls.js instance is not available');
431
+ this._hls.startLoad();
432
+ break;
433
+ }
434
+ break;
435
+ case HLSJS.ErrorTypes.MEDIA_ERROR:
436
+ Log.warn('hlsjs: trying to recover from media error.', { evt, data });
437
+ error.level = PlayerError.Levels.WARN;
438
+ this._recover(evt, data, error);
439
+ break;
440
+ default:
441
+ Log.error('hlsjs: could not recover from error.', { evt, data });
442
+ formattedError = this.createError(error);
443
+ this.trigger(Events.PLAYBACK_ERROR, formattedError);
444
+ this.stop();
445
+ break;
446
+ }
447
+ }
448
+ else {
449
+ Log.error('hlsjs: could not recover from error after maximum number of attempts.', { evt, data });
450
+ formattedError = this.createError(error);
451
+ this.trigger(Events.PLAYBACK_ERROR, formattedError);
452
+ this.stop();
453
+ }
454
+ }
455
+ else {
456
+ // Transforms HLSJS.ErrorDetails.KEY_LOAD_ERROR non-fatal error to
457
+ // playback fatal error if triggerFatalErrorOnResourceDenied playback
458
+ // option is set. HLSJS.ErrorTypes.KEY_SYSTEM_ERROR are fatal errors
459
+ // and therefore already handled.
460
+ if (this.options.playback.triggerFatalErrorOnResourceDenied && this._keyIsDenied(data)) {
461
+ Log.error('hlsjs: could not load decrypt key.', { evt, data });
462
+ formattedError = this.createError(error);
463
+ this.trigger(Events.PLAYBACK_ERROR, formattedError);
464
+ this.stop();
465
+ return;
466
+ }
467
+ error.level = PlayerError.Levels.WARN;
468
+ Log.warn('hlsjs: non-fatal error occurred', { evt, data });
469
+ }
470
+ }
471
+ _keyIsDenied(data) {
472
+ return data.type === HLSJS.ErrorTypes.NETWORK_ERROR
473
+ && data.details === HLSJS.ErrorDetails.KEY_LOAD_ERROR
474
+ && data.response
475
+ && data.response.code
476
+ && data.response.code >= 400;
477
+ }
478
+ _onTimeUpdate() {
479
+ const update = { current: this.getCurrentTime(), total: this.getDuration(), firstFragDateTime: this.getProgramDateTime() };
480
+ const isSame = this._lastTimeUpdate && (update.current === this._lastTimeUpdate.current &&
481
+ update.total === this._lastTimeUpdate.total);
482
+ if (isSame) {
483
+ return;
484
+ }
485
+ this._lastTimeUpdate = update;
486
+ this.trigger(Events.PLAYBACK_TIMEUPDATE, update, this.name);
487
+ }
488
+ _onDurationChange() {
489
+ const duration = this.getDuration();
490
+ if (this._lastDuration === duration) {
491
+ return;
492
+ }
493
+ this._lastDuration = duration;
494
+ super._onDurationChange();
495
+ }
496
+ _onProgress() {
497
+ if (!this.el.buffered.length) {
498
+ return;
499
+ }
500
+ let buffered = [];
501
+ let bufferedPos = 0;
502
+ for (let i = 0; i < this.el.buffered.length; i++) {
503
+ buffered = [...buffered, {
504
+ // for a stream with sliding window dvr something that is buffered my slide off the start of the timeline
505
+ start: Math.max(0, this.el.buffered.start(i) - this._playableRegionStartTime),
506
+ end: Math.max(0, this.el.buffered.end(i) - this._playableRegionStartTime)
507
+ }];
508
+ if (this.el.currentTime >= buffered[i].start && this.el.currentTime <= buffered[i].end) {
509
+ bufferedPos = i;
510
+ }
511
+ }
512
+ const progress = {
513
+ start: buffered[bufferedPos].start,
514
+ current: buffered[bufferedPos].end,
515
+ total: this.getDuration()
516
+ };
517
+ this.trigger(Events.PLAYBACK_PROGRESS, progress, buffered);
518
+ }
519
+ load(url) {
520
+ this._stopTimeUpdateTimer();
521
+ this.options.src = url;
522
+ this._setup();
523
+ }
524
+ play() {
525
+ !this._hls && this._setup();
526
+ assert.ok(this._hls, 'Hls.js instance is not available');
527
+ !this._manifestParsed && !this.options.hlsPlayback.preload && this._hls.loadSource(this.options.src);
528
+ super.play();
529
+ this._startTimeUpdateTimer();
530
+ }
531
+ pause() {
532
+ if (!this._hls) {
533
+ return;
534
+ }
535
+ this.el.pause();
536
+ if (this.dvrEnabled) {
537
+ this._updateDvr(true);
538
+ }
539
+ }
540
+ stop() {
541
+ this._stopTimeUpdateTimer();
542
+ if (this._hls) {
543
+ super.stop();
544
+ }
545
+ this._destroyHLSInstance();
546
+ }
547
+ destroy() {
548
+ this._stopTimeUpdateTimer();
549
+ this._destroyHLSInstance();
550
+ return super.destroy();
551
+ }
552
+ _updatePlaybackType(evt, data) {
553
+ this._playbackType = (data.details.live ? Playback.LIVE : Playback.VOD);
554
+ this._onLevelUpdated(evt, data);
555
+ // Live stream subtitle tracks detection hack (may not immediately available)
556
+ if (this._ccTracksUpdated && this._playbackType === Playback.LIVE && this.hasClosedCaptionsTracks) {
557
+ this._onSubtitleLoaded();
558
+ }
559
+ }
560
+ _fillLevels() {
561
+ assert.ok(this._hls, 'Hls.js instance is not available');
562
+ this._levels = this._hls.levels.map((level, index) => {
563
+ return { id: index, level: level, label: `${level.bitrate / 1000}Kbps` };
564
+ });
565
+ this.trigger(Events.PLAYBACK_LEVELS_AVAILABLE, this._levels);
566
+ }
567
+ _onLevelUpdated(evt, data) {
568
+ this._segmentTargetDuration = data.details.targetduration;
569
+ this._playlistType = data.details.type || null;
570
+ let startTimeChanged = false;
571
+ let durationChanged = false;
572
+ const fragments = data.details.fragments;
573
+ const previousPlayableRegionStartTime = this._playableRegionStartTime;
574
+ const previousPlayableRegionDuration = this._playableRegionDuration;
575
+ if (fragments.length === 0) {
576
+ return;
577
+ }
578
+ // #EXT-X-PROGRAM-DATE-TIME
579
+ if (fragments[0].rawProgramDateTime) {
580
+ this._programDateTime = fragments[0].rawProgramDateTime;
581
+ }
582
+ if (this._playableRegionStartTime !== fragments[0].start) {
583
+ startTimeChanged = true;
584
+ this._playableRegionStartTime = fragments[0].start;
585
+ }
586
+ if (startTimeChanged) {
587
+ if (!this._localStartTimeCorrelation) {
588
+ // set the correlation to map to middle of the extrapolation window
589
+ this._localStartTimeCorrelation = {
590
+ local: this._now,
591
+ remote: (fragments[0].start + (this._extrapolatedWindowDuration / 2)) * 1000
592
+ };
593
+ }
594
+ else {
595
+ // check if the correlation still works
596
+ const corr = this._localStartTimeCorrelation;
597
+ const timePassed = this._now - corr.local;
598
+ // this should point to a time within the extrapolation window
599
+ const startTime = (corr.remote + timePassed) / 1000;
600
+ if (startTime < fragments[0].start) {
601
+ // our start time is now earlier than the first chunk
602
+ // (maybe the chunk was removed early)
603
+ // reset correlation so that it sits at the beginning of the first available chunk
604
+ this._localStartTimeCorrelation = {
605
+ local: this._now,
606
+ remote: fragments[0].start * 1000
607
+ };
608
+ }
609
+ else if (startTime > previousPlayableRegionStartTime + this._extrapolatedWindowDuration) {
610
+ // start time was past the end of the old extrapolation window (so would have been capped)
611
+ // see if now that time would be inside the window, and if it would be set the correlation
612
+ // so that it resumes from the time it was at at the end of the old window
613
+ // update the correlation so that the time starts counting again from the value it's on now
614
+ this._localStartTimeCorrelation = {
615
+ local: this._now,
616
+ remote: Math.max(fragments[0].start, previousPlayableRegionStartTime + this._extrapolatedWindowDuration) * 1000
617
+ };
618
+ }
619
+ }
620
+ }
621
+ let newDuration = data.details.totalduration;
622
+ // if it's a live stream then shorten the duration to remove access
623
+ // to the area after hlsjs's live sync point
624
+ // seeks to areas after this point sometimes have issues
625
+ if (this._playbackType === Playback.LIVE) {
626
+ const fragmentTargetDuration = data.details.targetduration;
627
+ const hlsjsConfig = this.options.playback.hlsjsConfig || {};
628
+ const liveSyncDurationCount = hlsjsConfig.liveSyncDurationCount || HLSJS.DefaultConfig.liveSyncDurationCount;
629
+ const hiddenAreaDuration = fragmentTargetDuration * liveSyncDurationCount;
630
+ if (hiddenAreaDuration <= newDuration) {
631
+ newDuration -= hiddenAreaDuration;
632
+ this._durationExcludesAfterLiveSyncPoint = true;
633
+ }
634
+ else {
635
+ this._durationExcludesAfterLiveSyncPoint = false;
636
+ }
637
+ }
638
+ if (newDuration !== this._playableRegionDuration) {
639
+ durationChanged = true;
640
+ this._playableRegionDuration = newDuration;
641
+ }
642
+ // Note the end time is not the playableRegionDuration
643
+ // The end time will always increase even if content is removed from the beginning
644
+ const endTime = fragments[0].start + newDuration;
645
+ const previousEndTime = previousPlayableRegionStartTime + previousPlayableRegionDuration;
646
+ const endTimeChanged = endTime !== previousEndTime;
647
+ if (endTimeChanged) {
648
+ if (!this._localEndTimeCorrelation) {
649
+ // set the correlation to map to the end
650
+ this._localEndTimeCorrelation = {
651
+ local: this._now,
652
+ remote: endTime * 1000
653
+ };
654
+ }
655
+ else {
656
+ // check if the correlation still works
657
+ const corr = this._localEndTimeCorrelation;
658
+ const timePassed = this._now - corr.local;
659
+ // this should point to a time within the extrapolation window from the end
660
+ const extrapolatedEndTime = (corr.remote + timePassed) / 1000;
661
+ if (extrapolatedEndTime > endTime) {
662
+ this._localEndTimeCorrelation = {
663
+ local: this._now,
664
+ remote: endTime * 1000
665
+ };
666
+ }
667
+ else if (extrapolatedEndTime < endTime - this._extrapolatedWindowDuration) {
668
+ // our extrapolated end time is now earlier than the extrapolation window from the actual end time
669
+ // (maybe a chunk became available early)
670
+ // reset correlation so that it sits at the beginning of the extrapolation window from the end time
671
+ this._localEndTimeCorrelation = {
672
+ local: this._now,
673
+ remote: (endTime - this._extrapolatedWindowDuration) * 1000
674
+ };
675
+ }
676
+ else if (extrapolatedEndTime > previousEndTime) {
677
+ // end time was past the old end time (so would have been capped)
678
+ // set the correlation so that it resumes from the time it was at at the end of the old window
679
+ this._localEndTimeCorrelation = {
680
+ local: this._now,
681
+ remote: previousEndTime * 1000
682
+ };
683
+ }
684
+ }
685
+ }
686
+ // now that the values have been updated call any methods that use on them so they get the updated values
687
+ // immediately
688
+ durationChanged && this._onDurationChange();
689
+ startTimeChanged && this._onProgress();
690
+ }
691
+ _onFragmentChanged(evt, data) {
692
+ this._currentFragment = data.frag;
693
+ // @ts-ignore
694
+ this.trigger(Events.Custom.PLAYBACK_FRAGMENT_CHANGED, data);
695
+ }
696
+ _onFragmentLoaded(evt, data) {
697
+ this.trigger(Events.PLAYBACK_FRAGMENT_LOADED, data);
698
+ }
699
+ _onSubtitleLoaded() {
700
+ // This event may be triggered multiple times
701
+ // Setup CC only once (disable CC by default)
702
+ if (!this._ccIsSetup) {
703
+ this.trigger(Events.PLAYBACK_SUBTITLE_AVAILABLE);
704
+ const trackId = this._playbackType === Playback.LIVE ? -1 : this.closedCaptionsTrackId;
705
+ this.closedCaptionsTrackId = trackId;
706
+ this._ccIsSetup = true;
707
+ }
708
+ }
709
+ _onLevelSwitch(evt, data) {
710
+ if (!this.levels.length) {
711
+ this._fillLevels();
712
+ }
713
+ this.trigger(Events.PLAYBACK_LEVEL_SWITCH_END);
714
+ this.trigger(Events.PLAYBACK_LEVEL_SWITCH, data);
715
+ assert(this._hls, 'Hls.js instance is not available');
716
+ const currentLevel = this._hls.levels[data.level];
717
+ if (currentLevel) {
718
+ // TODO should highDefinition be private and maybe have a read only accessor if it's used somewhere
719
+ this.highDefinition = (currentLevel.height >= 720 || (currentLevel.bitrate / 1000) >= 2000);
720
+ this.trigger(Events.PLAYBACK_HIGHDEFINITIONUPDATE, this.highDefinition);
721
+ this.trigger(Events.PLAYBACK_BITRATE, {
722
+ height: currentLevel.height,
723
+ width: currentLevel.width,
724
+ bandwidth: currentLevel.bitrate,
725
+ bitrate: currentLevel.bitrate,
726
+ level: data.level
727
+ });
728
+ }
729
+ }
730
+ get dvrEnabled() {
731
+ // enabled when:
732
+ // - the duration does not include content after hlsjs's live sync point
733
+ // - the playable region duration is longer than the configured duration to enable dvr after
734
+ // - the playback type is LIVE.
735
+ return (this._durationExcludesAfterLiveSyncPoint && this._duration >= this._minDvrSize && this.getPlaybackType() === Playback.LIVE);
736
+ }
737
+ getPlaybackType() {
738
+ return this._playbackType;
739
+ }
740
+ isSeekEnabled() {
741
+ return (this._playbackType === Playback.VOD || this.dvrEnabled);
742
+ }
743
+ }
744
+ HlsPlayback.canPlay = function (resource, mimeType) {
745
+ const resourceParts = resource.split('?')[0].match(/.*\.(.*)$/) || [];
746
+ const isHls = ((resourceParts.length > 1 && resourceParts[1].toLowerCase() === 'm3u8') || listContainsIgnoreCase(mimeType, ['application/vnd.apple.mpegurl', 'application/x-mpegURL']));
747
+ return !!(HLSJS.isSupported() && isHls);
748
+ };
749
+
750
+ export { HlsPlayback as default };