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