@gcorevideo/player 2.29.0 → 2.30.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/README.md +108 -0
- package/dist/core.js +81 -22
- package/dist/index.css +370 -370
- package/dist/index.embed.js +80 -23
- package/dist/index.js +459 -87
- package/docs/api/player.md +37 -0
- package/docs/api/player.player.getplugin.md +59 -0
- package/docs/api/player.player.md +14 -0
- package/docs/api/player.tokenrefreshoptions.gettoken.md +13 -0
- package/docs/api/player.tokenrefreshoptions.ipbound.md +13 -0
- package/docs/api/player.tokenrefreshoptions.md +115 -0
- package/docs/api/player.tokenrefreshoptions.ontokenrefreshed.md +13 -0
- package/docs/api/player.tokenrefreshoptions.refreshleadseconds.md +13 -0
- package/docs/api/player.tokenrefreshplugin.md +50 -0
- package/docs/api/player.tokenresponse.client_ip.md +13 -0
- package/docs/api/player.tokenresponse.expires.md +13 -0
- package/docs/api/player.tokenresponse.md +153 -0
- package/docs/api/player.tokenresponse.token.md +13 -0
- package/docs/api/player.tokenresponse.token_ip.md +13 -0
- package/docs/api/player.tokenresponse.url.md +13 -0
- package/docs/api/player.tokenresponse.url_ip.md +13 -0
- package/lib/Player.d.ts +9 -0
- package/lib/Player.d.ts.map +1 -1
- package/lib/Player.js +11 -0
- package/lib/index.plugins.d.ts +1 -0
- package/lib/index.plugins.d.ts.map +1 -1
- package/lib/index.plugins.js +1 -0
- package/lib/playback/dash-playback/DashPlayback.d.ts.map +1 -1
- package/lib/playback/dash-playback/DashPlayback.js +5 -1
- package/lib/playback/hls-playback/HlsPlayback.d.ts +1 -1
- package/lib/playback/hls-playback/HlsPlayback.d.ts.map +1 -1
- package/lib/playback/hls-playback/HlsPlayback.js +23 -20
- package/lib/playback/hls-playback/RangesList.d.ts +7 -0
- package/lib/playback/hls-playback/RangesList.d.ts.map +1 -0
- package/lib/playback/hls-playback/RangesList.js +41 -0
- package/lib/plugins/subtitles/ClosedCaptions.d.ts.map +1 -1
- package/lib/plugins/subtitles/ClosedCaptions.js +0 -2
- package/lib/plugins/token-refresh/TokenRefreshPlugin.d.ts +119 -0
- package/lib/plugins/token-refresh/TokenRefreshPlugin.d.ts.map +1 -0
- package/lib/plugins/token-refresh/TokenRefreshPlugin.js +318 -0
- package/lib/plugins/token-refresh/index.d.ts +2 -0
- package/lib/plugins/token-refresh/index.d.ts.map +1 -0
- package/lib/plugins/token-refresh/index.js +1 -0
- package/package.json +1 -1
- package/src/Player.ts +12 -0
- package/src/index.plugins.ts +1 -0
- package/src/playback/dash-playback/DashPlayback.ts +6 -1
- package/src/playback/hls-playback/HlsPlayback.ts +40 -37
- package/src/playback/hls-playback/RangesList.ts +45 -0
- package/src/playback/hls-playback/__tests__/RangesList.test.ts +60 -0
- package/src/plugins/subtitles/ClosedCaptions.ts +0 -5
- package/src/plugins/token-refresh/TokenRefreshPlugin.ts +425 -0
- package/src/plugins/token-refresh/index.ts +5 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { $, CorePlugin, Events } from '@clappr/core';
|
|
2
|
+
import HLSJS from 'hls.js';
|
|
3
|
+
import { trace } from '@gcorevideo/utils';
|
|
4
|
+
import { CLAPPR_VERSION } from '../../build.js';
|
|
5
|
+
const T = 'plugins.token_refresh';
|
|
6
|
+
/**
|
|
7
|
+
* Matches the `/{token}/{expires}/` segment in a Gcore protected-content URL.
|
|
8
|
+
* Token is base64url (letters, digits, `-`, `_`); expires is a ≥10-digit Unix timestamp.
|
|
9
|
+
*/
|
|
10
|
+
const TOKEN_SEGMENT_RE = /\/([A-Za-z0-9_-]{6,})\/(1\d{9,})\//;
|
|
11
|
+
function extractTokenState(url) {
|
|
12
|
+
const m = url.match(TOKEN_SEGMENT_RE);
|
|
13
|
+
if (!m)
|
|
14
|
+
return null;
|
|
15
|
+
return { token: m[1], expires: parseInt(m[2], 10) };
|
|
16
|
+
}
|
|
17
|
+
/** Replaces the exact `/{oldToken}/{oldExpires}/` segment in a URL. */
|
|
18
|
+
function rewriteUrl(url, from, to) {
|
|
19
|
+
const oldPart = `/${from.token}/${from.expires}/`;
|
|
20
|
+
const newPart = `/${to.token}/${to.expires}/`;
|
|
21
|
+
return url.includes(oldPart) ? url.replace(oldPart, newPart) : url;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Normalises a URL by removing the `/{token}/{expires}/` segment so two URLs
|
|
25
|
+
* for the same stream with different token pairs compare equal.
|
|
26
|
+
* Returns `null` for unparseable input.
|
|
27
|
+
*/
|
|
28
|
+
function streamKey(url) {
|
|
29
|
+
try {
|
|
30
|
+
const u = new URL(url);
|
|
31
|
+
return u.origin + u.pathname.replace(TOKEN_SEGMENT_RE, '/');
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Returns a custom hls.js loader class that transparently rewrites the
|
|
39
|
+
* token/expires path segments in every request URL.
|
|
40
|
+
*
|
|
41
|
+
* The returned class extends the default hls.js XhrLoader so all native
|
|
42
|
+
* hls.js behaviour (retry, timeout, range requests …) is preserved.
|
|
43
|
+
*/
|
|
44
|
+
function createTokenRewritingLoader(getOriginal, getCurrent) {
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
46
|
+
const DefaultLoader = HLSJS.DefaultConfig.loader;
|
|
47
|
+
return class TokenRewritingLoader extends DefaultLoader {
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
49
|
+
load(context, config, callbacks) {
|
|
50
|
+
const original = getOriginal();
|
|
51
|
+
const current = getCurrent();
|
|
52
|
+
if (original && current && context.url) {
|
|
53
|
+
context.url = rewriteUrl(context.url, original, current);
|
|
54
|
+
}
|
|
55
|
+
super.load(context, config, callbacks);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* `PLUGIN` — automatic token refresh for Gcore protected-content streams.
|
|
61
|
+
*
|
|
62
|
+
* Supports all three playback engines:
|
|
63
|
+
*
|
|
64
|
+
* | Engine | Mechanism | Interruption |
|
|
65
|
+
* |--------|-----------|--------------|
|
|
66
|
+
* | **hls.js** | Custom loader rewrites every request URL | None |
|
|
67
|
+
* | **dash.js** | `addRequestInterceptor` rewrites every request URL | None |
|
|
68
|
+
* | **Native `<video>`** (Safari ≤ iOS 14.4) | Source reload + seek restore | Brief |
|
|
69
|
+
*
|
|
70
|
+
* @public
|
|
71
|
+
* @remarks
|
|
72
|
+
* Register the plugin once before creating any player instance:
|
|
73
|
+
* ```ts
|
|
74
|
+
* import { Player, TokenRefreshPlugin } from '@gcorevideo/player'
|
|
75
|
+
* Player.registerPlugin(TokenRefreshPlugin)
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* Then pass `tokenRefresh` in `PlayerConfig`:
|
|
79
|
+
* ```ts
|
|
80
|
+
* const player = new Player({
|
|
81
|
+
* sources: [{ source: initialUrl, mimeType: 'application/x-mpegURL' }],
|
|
82
|
+
* tokenRefresh: {
|
|
83
|
+
* getToken: () => fetch('https://…/token').then(r => r.json()),
|
|
84
|
+
* ipBound: false,
|
|
85
|
+
* refreshLeadSeconds: 5,
|
|
86
|
+
* onTokenRefreshed: (data) => console.log('new token expires', data.expires),
|
|
87
|
+
* },
|
|
88
|
+
* })
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* Safari native — opt-in Service Worker for fully seamless refresh:
|
|
93
|
+
* ```js
|
|
94
|
+
* // Register token-refresh-sw.js (see example/ directory)
|
|
95
|
+
* // and omit tokenRefresh config — the SW handles rewriting.
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export class TokenRefreshPlugin extends CorePlugin {
|
|
99
|
+
/** @internal */
|
|
100
|
+
static get type() {
|
|
101
|
+
return 'core';
|
|
102
|
+
}
|
|
103
|
+
/** @internal */
|
|
104
|
+
get name() {
|
|
105
|
+
return 'token_refresh';
|
|
106
|
+
}
|
|
107
|
+
/** @internal */
|
|
108
|
+
get supportedVersion() {
|
|
109
|
+
return { min: CLAPPR_VERSION };
|
|
110
|
+
}
|
|
111
|
+
/** Token state extracted from the currently-managed source URL */
|
|
112
|
+
originalState = null;
|
|
113
|
+
/** Latest token state (updated after each refresh) */
|
|
114
|
+
currentState = null;
|
|
115
|
+
/** Scheduled refresh timer handle */
|
|
116
|
+
refreshTimer = null;
|
|
117
|
+
/** Playback time (seconds) to restore after a native-video source reload */
|
|
118
|
+
savedPosition = null;
|
|
119
|
+
/** True when using native HTML5 Video playback (no request interception) */
|
|
120
|
+
isNativePlayback = false;
|
|
121
|
+
/** Set in destroy(); short-circuits late timer callbacks and getToken() resolutions */
|
|
122
|
+
destroyed = false;
|
|
123
|
+
/** @internal */
|
|
124
|
+
bindEvents() {
|
|
125
|
+
this.listenTo(this.core, Events.CORE_CONTAINERS_CREATED, this.onContainersCreated);
|
|
126
|
+
}
|
|
127
|
+
/** @internal */
|
|
128
|
+
destroy() {
|
|
129
|
+
this.destroyed = true;
|
|
130
|
+
this.clearTimer();
|
|
131
|
+
super.destroy();
|
|
132
|
+
}
|
|
133
|
+
onContainersCreated() {
|
|
134
|
+
const container = this.core.containers[0];
|
|
135
|
+
if (!container)
|
|
136
|
+
return;
|
|
137
|
+
const playbackName = container.playback.name;
|
|
138
|
+
const src = container.playback.options?.src ?? '';
|
|
139
|
+
trace(`${T} onContainersCreated`, { playbackName, src: src.slice(0, 80) });
|
|
140
|
+
this.isNativePlayback = playbackName !== 'hls' && playbackName !== 'dash';
|
|
141
|
+
const state = extractTokenState(src);
|
|
142
|
+
if (!state) {
|
|
143
|
+
// Active source has no token pattern — drop any refresh state we were
|
|
144
|
+
// holding for a previous stream (e.g. SourceController rotated away).
|
|
145
|
+
if (this.originalState) {
|
|
146
|
+
trace(`${T} active source has no token pattern — clearing refresh state`);
|
|
147
|
+
this.clearTimer();
|
|
148
|
+
this.originalState = null;
|
|
149
|
+
this.currentState = null;
|
|
150
|
+
}
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
// Adopt the new token state if this is the first source we see, or if
|
|
154
|
+
// the stream has changed (SourceController rotated, consumer called
|
|
155
|
+
// player.load() with a different stream, or the plugin itself reloaded
|
|
156
|
+
// with a refreshed URL in the native path).
|
|
157
|
+
const isNewStream = !this.originalState ||
|
|
158
|
+
state.token !== this.originalState.token ||
|
|
159
|
+
state.expires !== this.originalState.expires;
|
|
160
|
+
if (isNewStream) {
|
|
161
|
+
trace(`${T} adopting source token state`, {
|
|
162
|
+
token: state.token.slice(0, 8) + '…',
|
|
163
|
+
expires: new Date(state.expires * 1000).toISOString(),
|
|
164
|
+
});
|
|
165
|
+
this.originalState = { ...state };
|
|
166
|
+
this.currentState = { ...state };
|
|
167
|
+
this.scheduleRefresh();
|
|
168
|
+
}
|
|
169
|
+
// Inject the appropriate interception mechanism for this playback engine.
|
|
170
|
+
switch (playbackName) {
|
|
171
|
+
case 'hls':
|
|
172
|
+
this.injectHlsLoader(container);
|
|
173
|
+
break;
|
|
174
|
+
case 'dash':
|
|
175
|
+
this.injectDashInterceptor(container);
|
|
176
|
+
break;
|
|
177
|
+
default:
|
|
178
|
+
// Native HTML5 Video — no request hooks available.
|
|
179
|
+
// Seek restore after a token-triggered reload.
|
|
180
|
+
this.listenToOnce(this.core, Events.CORE_ACTIVE_CONTAINER_CHANGED, this.onActiveContainerChangedForNative);
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
injectHlsLoader(container) {
|
|
185
|
+
const getOriginal = () => this.originalState;
|
|
186
|
+
const getCurrent = () => this.currentState;
|
|
187
|
+
const TokenLoader = createTokenRewritingLoader(getOriginal, getCurrent);
|
|
188
|
+
$.extend(true, container.playback.options, {
|
|
189
|
+
playback: {
|
|
190
|
+
hlsjsConfig: {
|
|
191
|
+
loader: TokenLoader,
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
trace(`${T} HLS custom loader injected`);
|
|
196
|
+
}
|
|
197
|
+
injectDashInterceptor(container) {
|
|
198
|
+
$.extend(true, container.playback.options, {
|
|
199
|
+
dash: {
|
|
200
|
+
requestInterceptor: (request) => {
|
|
201
|
+
if (this.originalState && this.currentState) {
|
|
202
|
+
request.url = rewriteUrl(request.url, this.originalState, this.currentState);
|
|
203
|
+
}
|
|
204
|
+
return Promise.resolve(request);
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
trace(`${T} DASH request interceptor injected`);
|
|
209
|
+
}
|
|
210
|
+
async reloadNativeSource(data) {
|
|
211
|
+
const container = this.core.activeContainer;
|
|
212
|
+
const playback = container?.playback;
|
|
213
|
+
if (!playback)
|
|
214
|
+
return;
|
|
215
|
+
// SourceController (or any other actor) may have switched the active
|
|
216
|
+
// playback to hls/dash while getToken() was in flight. Those engines
|
|
217
|
+
// have their own request-interception path; a native reload would
|
|
218
|
+
// undo the switch.
|
|
219
|
+
if (playback.name === 'hls' || playback.name === 'dash') {
|
|
220
|
+
trace(`${T} skipping native reload — active playback is ${playback.name}`);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
if (!this.isNativePlayback) {
|
|
224
|
+
trace(`${T} skipping native reload — no longer in native playback mode`);
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
// Verify the URL we're about to load belongs to the same stream that's
|
|
228
|
+
// currently active. If SourceController rotated to a different stream
|
|
229
|
+
// during the getToken() await, the refreshed URL would silently swap
|
|
230
|
+
// us back, undoing SourceController's decision.
|
|
231
|
+
const currentSrc = playback.options?.src ?? '';
|
|
232
|
+
const newUrl = this.opts.ipBound ? data.url_ip : data.url;
|
|
233
|
+
const activeKey = streamKey(currentSrc);
|
|
234
|
+
const nextKey = streamKey(newUrl);
|
|
235
|
+
if (!activeKey || !nextKey || activeKey !== nextKey) {
|
|
236
|
+
trace(`${T} skipping native reload — active source differs from refresh URL`, {
|
|
237
|
+
activeKey,
|
|
238
|
+
nextKey,
|
|
239
|
+
});
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
// Capture current playback position before tearing down the container.
|
|
243
|
+
const mediaEl = playback.el;
|
|
244
|
+
const currentTime = mediaEl?.currentTime ?? 0;
|
|
245
|
+
this.savedPosition = currentTime > 0 ? currentTime : null;
|
|
246
|
+
trace(`${T} native reload`, { newUrl: newUrl.slice(0, 80), savedPosition: this.savedPosition });
|
|
247
|
+
// core.load() destroys and recreates all containers.
|
|
248
|
+
this.core.load([{ source: newUrl, mimeType: this.core.options.mimeType ?? 'application/x-mpegURL' }]);
|
|
249
|
+
}
|
|
250
|
+
onActiveContainerChangedForNative() {
|
|
251
|
+
if (this.savedPosition === null)
|
|
252
|
+
return;
|
|
253
|
+
const pos = this.savedPosition;
|
|
254
|
+
this.savedPosition = null;
|
|
255
|
+
// Wait for the new container to be fully ready before seeking.
|
|
256
|
+
const container = this.core.activeContainer;
|
|
257
|
+
if (!container)
|
|
258
|
+
return;
|
|
259
|
+
this.listenToOnce(container, Events.CONTAINER_READY, () => {
|
|
260
|
+
trace(`${T} native: restoring position`, { pos });
|
|
261
|
+
container.seek(pos);
|
|
262
|
+
container.play();
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
scheduleRefresh() {
|
|
266
|
+
this.clearTimer();
|
|
267
|
+
if (this.destroyed || !this.currentState)
|
|
268
|
+
return;
|
|
269
|
+
const leadMs = (this.opts.refreshLeadSeconds ?? 5) * 1000;
|
|
270
|
+
const msUntilRefresh = this.currentState.expires * 1000 - Date.now() - leadMs;
|
|
271
|
+
trace(`${T} next refresh in`, {
|
|
272
|
+
seconds: Math.round(msUntilRefresh / 1000),
|
|
273
|
+
expires: new Date(this.currentState.expires * 1000).toISOString(),
|
|
274
|
+
});
|
|
275
|
+
this.refreshTimer = setTimeout(() => this.performRefresh(), Math.max(msUntilRefresh, 1000));
|
|
276
|
+
}
|
|
277
|
+
async performRefresh() {
|
|
278
|
+
trace(`${T} fetching new token`);
|
|
279
|
+
try {
|
|
280
|
+
const data = await this.opts.getToken();
|
|
281
|
+
// Plugin may have been destroyed while getToken() was in flight; drop the result.
|
|
282
|
+
if (this.destroyed)
|
|
283
|
+
return;
|
|
284
|
+
const newToken = this.opts.ipBound ? data.token_ip : data.token;
|
|
285
|
+
const newState = { token: newToken, expires: data.expires };
|
|
286
|
+
if (this.isNativePlayback) {
|
|
287
|
+
// Must reload source because the <video> element has no request hook.
|
|
288
|
+
await this.reloadNativeSource(data);
|
|
289
|
+
}
|
|
290
|
+
// originalState is never changed after init — it holds the token that was
|
|
291
|
+
// baked into every URL in the initial manifest. hls.js/dash.js always
|
|
292
|
+
// produces request URLs based on that manifest, so every segment URL
|
|
293
|
+
// still contains the original token regardless of how many refreshes
|
|
294
|
+
// have already happened. The loader replaces original→current on each
|
|
295
|
+
// request, so updating only currentState is sufficient.
|
|
296
|
+
this.currentState = newState;
|
|
297
|
+
this.opts.onTokenRefreshed?.(data);
|
|
298
|
+
trace(`${T} token refreshed`, {
|
|
299
|
+
token: newToken.slice(0, 8) + '…',
|
|
300
|
+
expires: new Date(data.expires * 1000).toISOString(),
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
catch (err) {
|
|
304
|
+
trace(`${T} token refresh failed`, { err });
|
|
305
|
+
}
|
|
306
|
+
// Always reschedule, even after an error (will retry near next expiry).
|
|
307
|
+
this.scheduleRefresh();
|
|
308
|
+
}
|
|
309
|
+
get opts() {
|
|
310
|
+
return this.options.tokenRefresh;
|
|
311
|
+
}
|
|
312
|
+
clearTimer() {
|
|
313
|
+
if (this.refreshTimer !== null) {
|
|
314
|
+
clearTimeout(this.refreshTimer);
|
|
315
|
+
this.refreshTimer = null;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/plugins/token-refresh/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,KAAK,mBAAmB,EACxB,KAAK,aAAa,GACnB,MAAM,yBAAyB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { TokenRefreshPlugin, } from './TokenRefreshPlugin.js';
|
package/package.json
CHANGED
package/src/Player.ts
CHANGED
|
@@ -278,6 +278,18 @@ export class Player {
|
|
|
278
278
|
this.player?.load(ms, ms[0].mimeType ?? '')
|
|
279
279
|
}
|
|
280
280
|
|
|
281
|
+
/**
|
|
282
|
+
* Returns a registered core plugin instance by name, or `null` if not found.
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* ```ts
|
|
286
|
+
* const tokenRefresh = player.getPlugin('token_refresh') as TokenRefreshPlugin | null
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
289
|
+
getPlugin(name: string) {
|
|
290
|
+
return this.player?.core.getPlugin(name) ?? null
|
|
291
|
+
}
|
|
292
|
+
|
|
281
293
|
/**
|
|
282
294
|
* Mutes the sound of the video.
|
|
283
295
|
*/
|
package/src/index.plugins.ts
CHANGED
|
@@ -243,6 +243,7 @@ export default class DashPlayback extends BasePlayback {
|
|
|
243
243
|
this._dash.initialize()
|
|
244
244
|
|
|
245
245
|
if (this.options.dash) {
|
|
246
|
+
const { requestInterceptor, ...dashSettings } = this.options.dash as Record<string, unknown>
|
|
246
247
|
const settings = $.extend(
|
|
247
248
|
true,
|
|
248
249
|
{
|
|
@@ -257,9 +258,13 @@ export default class DashPlayback extends BasePlayback {
|
|
|
257
258
|
},
|
|
258
259
|
},
|
|
259
260
|
},
|
|
260
|
-
|
|
261
|
+
dashSettings,
|
|
261
262
|
)
|
|
262
263
|
this._dash.updateSettings(settings)
|
|
264
|
+
|
|
265
|
+
if (typeof requestInterceptor === 'function') {
|
|
266
|
+
this._dash.addRequestInterceptor(requestInterceptor as Parameters<typeof this._dash.addRequestInterceptor>[0])
|
|
267
|
+
}
|
|
263
268
|
}
|
|
264
269
|
|
|
265
270
|
this._dash.attachView(this.el as HTMLMediaElement)
|
|
@@ -35,6 +35,7 @@ import { BasePlayback } from '../BasePlayback.js'
|
|
|
35
35
|
import { CLAPPR_VERSION } from '../../build.js'
|
|
36
36
|
import { AudioTrack } from '@clappr/core/types/base/playback/playback.js'
|
|
37
37
|
import { VTTCueInfo } from '../types.js'
|
|
38
|
+
import { RangesList } from './RangesList.js'
|
|
38
39
|
|
|
39
40
|
const { now } = Utils
|
|
40
41
|
|
|
@@ -70,8 +71,6 @@ type CustomListener = {
|
|
|
70
71
|
}
|
|
71
72
|
|
|
72
73
|
export default class HlsPlayback extends BasePlayback {
|
|
73
|
-
private _ccTracksUpdated = false
|
|
74
|
-
|
|
75
74
|
private _currentFragment: Fragment | null = null
|
|
76
75
|
|
|
77
76
|
private _currentLevel: number | null = null
|
|
@@ -120,7 +119,9 @@ export default class HlsPlayback extends BasePlayback {
|
|
|
120
119
|
|
|
121
120
|
oncueexit: ((e: { id: string }) => void) | null = null
|
|
122
121
|
|
|
123
|
-
private cues: VTTCueInfo
|
|
122
|
+
private cues: RangesList<VTTCueInfo> | null = null
|
|
123
|
+
|
|
124
|
+
private cuesByTrack: Record<number, RangesList<VTTCueInfo>> = {}
|
|
124
125
|
|
|
125
126
|
private currentCueId: string | null = null
|
|
126
127
|
|
|
@@ -310,7 +311,7 @@ export default class HlsPlayback extends BasePlayback {
|
|
|
310
311
|
// added/removed every 5.
|
|
311
312
|
this._extrapolatedWindowNumSegments =
|
|
312
313
|
!this.options.playback ||
|
|
313
|
-
|
|
314
|
+
typeof this.options.playback.extrapolatedWindowNumSegments === 'undefined'
|
|
314
315
|
? 2
|
|
315
316
|
: this.options.playback.extrapolatedWindowNumSegments
|
|
316
317
|
|
|
@@ -362,7 +363,6 @@ export default class HlsPlayback extends BasePlayback {
|
|
|
362
363
|
}
|
|
363
364
|
this._manifestParsed = false
|
|
364
365
|
// this._ccIsSetup = false
|
|
365
|
-
this._ccTracksUpdated = false
|
|
366
366
|
this._setInitialState()
|
|
367
367
|
this._hls.destroy()
|
|
368
368
|
this._hls = null
|
|
@@ -436,9 +436,7 @@ export default class HlsPlayback extends BasePlayback {
|
|
|
436
436
|
(evt: HlsEvents.LEVEL_SWITCHED, data: { level: number }) =>
|
|
437
437
|
this._onLevelSwitched(evt, data),
|
|
438
438
|
)
|
|
439
|
-
this._hls.on(HlsEvents.ERROR, (evt, data) =>
|
|
440
|
-
this._onHLSJSError(evt, data),
|
|
441
|
-
)
|
|
439
|
+
this._hls.on(HlsEvents.ERROR, (evt, data) => this._onHLSJSError(evt, data))
|
|
442
440
|
this._hls.on(HlsEvents.AUDIO_TRACKS_UPDATED, (evt, data) =>
|
|
443
441
|
this._onAudioTracksUpdated(evt, data),
|
|
444
442
|
)
|
|
@@ -447,12 +445,20 @@ export default class HlsPlayback extends BasePlayback {
|
|
|
447
445
|
)
|
|
448
446
|
this._hls.on(HlsEvents.CUES_PARSED, (evt, data) => {
|
|
449
447
|
data.cues?.forEach((cue: any) => {
|
|
450
|
-
this.cues
|
|
448
|
+
if (!this.cues) {
|
|
449
|
+
const trackId = this._hls!.subtitleTrack
|
|
450
|
+
if (!this.cuesByTrack[trackId]) {
|
|
451
|
+
this.cuesByTrack[trackId] = new RangesList<VTTCueInfo>()
|
|
452
|
+
}
|
|
453
|
+
this.cues = this.cuesByTrack[trackId]
|
|
454
|
+
}
|
|
455
|
+
const cueInfo = {
|
|
451
456
|
id: cue.id,
|
|
452
457
|
start: cue.startTime,
|
|
453
458
|
end: cue.endTime,
|
|
454
459
|
text: cue.text,
|
|
455
|
-
} as VTTCueInfo
|
|
460
|
+
} as VTTCueInfo
|
|
461
|
+
this.cues.insert(cue.startTime, cue.endTime, cueInfo)
|
|
456
462
|
})
|
|
457
463
|
})
|
|
458
464
|
this.bindCustomListeners()
|
|
@@ -514,7 +520,7 @@ export default class HlsPlayback extends BasePlayback {
|
|
|
514
520
|
}
|
|
515
521
|
|
|
516
522
|
// this playback manages the src on the video element itself
|
|
517
|
-
protected override _setupSrc(srcUrl: string) {
|
|
523
|
+
protected override _setupSrc(srcUrl: string) {} // eslint-disable-line no-unused-vars
|
|
518
524
|
|
|
519
525
|
private _startTimeUpdateTimer() {
|
|
520
526
|
if (this._timeUpdateTimer) {
|
|
@@ -579,7 +585,7 @@ export default class HlsPlayback extends BasePlayback {
|
|
|
579
585
|
// assume live if time within 3 seconds of end of stream
|
|
580
586
|
this.dvrEnabled && this._updateDvr(time < this.getDuration() - 3)
|
|
581
587
|
time += this._startTime
|
|
582
|
-
|
|
588
|
+
;(this.el as HTMLMediaElement).currentTime = time
|
|
583
589
|
|
|
584
590
|
this.triggerCues()
|
|
585
591
|
}
|
|
@@ -719,7 +725,7 @@ export default class HlsPlayback extends BasePlayback {
|
|
|
719
725
|
}
|
|
720
726
|
|
|
721
727
|
private reload() {
|
|
722
|
-
this.cues =
|
|
728
|
+
this.cues = null
|
|
723
729
|
this.currentCueId = null
|
|
724
730
|
this._hls?.startLoad(-1)
|
|
725
731
|
}
|
|
@@ -777,12 +783,12 @@ export default class HlsPlayback extends BasePlayback {
|
|
|
777
783
|
start: Math.max(
|
|
778
784
|
0,
|
|
779
785
|
(this.el as HTMLMediaElement).buffered.start(i) -
|
|
780
|
-
|
|
786
|
+
this._playableRegionStartTime,
|
|
781
787
|
),
|
|
782
788
|
end: Math.max(
|
|
783
789
|
0,
|
|
784
790
|
(this.el as HTMLMediaElement).buffered.end(i) -
|
|
785
|
-
|
|
791
|
+
this._playableRegionStartTime,
|
|
786
792
|
),
|
|
787
793
|
},
|
|
788
794
|
]
|
|
@@ -804,9 +810,7 @@ export default class HlsPlayback extends BasePlayback {
|
|
|
804
810
|
|
|
805
811
|
private triggerCues() {
|
|
806
812
|
const currentTime = this.getCurrentTime()
|
|
807
|
-
|
|
808
|
-
// TODO build a search tree
|
|
809
|
-
const cue = this.cues.find((cue: VTTCueInfo) => currentTime >= cue.start && currentTime <= cue.end)
|
|
813
|
+
const cue = this.cues?.find(currentTime)
|
|
810
814
|
if (cue) {
|
|
811
815
|
this.currentCueId = cue.id
|
|
812
816
|
this.oncueenter?.(cue)
|
|
@@ -836,7 +840,7 @@ export default class HlsPlayback extends BasePlayback {
|
|
|
836
840
|
if (!this._hls) {
|
|
837
841
|
return
|
|
838
842
|
}
|
|
839
|
-
;
|
|
843
|
+
;(this.el as HTMLMediaElement).pause()
|
|
840
844
|
if (this.dvrEnabled) {
|
|
841
845
|
this._updateDvr(true)
|
|
842
846
|
}
|
|
@@ -853,6 +857,8 @@ export default class HlsPlayback extends BasePlayback {
|
|
|
853
857
|
destroy() {
|
|
854
858
|
this._stopTimeUpdateTimer()
|
|
855
859
|
this._destroyHLSInstance()
|
|
860
|
+
this.cues = null
|
|
861
|
+
this.cuesByTrack = {}
|
|
856
862
|
return super.destroy()
|
|
857
863
|
}
|
|
858
864
|
|
|
@@ -865,14 +871,6 @@ export default class HlsPlayback extends BasePlayback {
|
|
|
865
871
|
data.details.live ? Playback.LIVE : Playback.VOD
|
|
866
872
|
) as PlaybackType
|
|
867
873
|
this._onLevelUpdated(evt, data)
|
|
868
|
-
// Live stream subtitle tracks detection hack (may not immediately available)
|
|
869
|
-
// if (
|
|
870
|
-
// this._ccTracksUpdated &&
|
|
871
|
-
// this._playbackType === Playback.LIVE &&
|
|
872
|
-
// this.hasClosedCaptionsTracks
|
|
873
|
-
// ) {
|
|
874
|
-
// this._onSubtitleLoaded()
|
|
875
|
-
// }
|
|
876
874
|
if (prevPlaybackType !== this._playbackType) {
|
|
877
875
|
this._updateSettings()
|
|
878
876
|
}
|
|
@@ -952,7 +950,7 @@ export default class HlsPlayback extends BasePlayback {
|
|
|
952
950
|
Math.max(
|
|
953
951
|
fragments[0].start,
|
|
954
952
|
previousPlayableRegionStartTime +
|
|
955
|
-
|
|
953
|
+
this._extrapolatedWindowDuration,
|
|
956
954
|
) * 1000,
|
|
957
955
|
}
|
|
958
956
|
}
|
|
@@ -1139,7 +1137,10 @@ export default class HlsPlayback extends BasePlayback {
|
|
|
1139
1137
|
return
|
|
1140
1138
|
}
|
|
1141
1139
|
this._hls!.subtitleTrack = id
|
|
1142
|
-
this.
|
|
1140
|
+
if (!this.cuesByTrack[id]) {
|
|
1141
|
+
this.cuesByTrack[id] = new RangesList<VTTCueInfo>()
|
|
1142
|
+
}
|
|
1143
|
+
this.cues = this.cuesByTrack[id]
|
|
1143
1144
|
}
|
|
1144
1145
|
|
|
1145
1146
|
/**
|
|
@@ -1150,15 +1151,17 @@ export default class HlsPlayback extends BasePlayback {
|
|
|
1150
1151
|
}
|
|
1151
1152
|
|
|
1152
1153
|
getTextTracks() {
|
|
1153
|
-
return
|
|
1154
|
-
|
|
1155
|
-
name: t.name,
|
|
1156
|
-
track: {
|
|
1154
|
+
return (
|
|
1155
|
+
this._hls?.subtitleTracks.map((t: MediaPlaylist) => ({
|
|
1157
1156
|
id: t.id,
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1157
|
+
name: t.name,
|
|
1158
|
+
track: {
|
|
1159
|
+
id: t.id,
|
|
1160
|
+
label: t.name,
|
|
1161
|
+
language: t.lang,
|
|
1162
|
+
},
|
|
1163
|
+
})) || []
|
|
1164
|
+
)
|
|
1162
1165
|
}
|
|
1163
1166
|
}
|
|
1164
1167
|
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
|
|
2
|
+
export class RangesList<T> {
|
|
3
|
+
// TODO write an efficient implementation
|
|
4
|
+
private items: Array<[number, number, T]> = [];
|
|
5
|
+
|
|
6
|
+
insert(start: number, end: number, value: T) {
|
|
7
|
+
const index = this.findIndex((start + end) / 2);
|
|
8
|
+
this.items.splice(index, 0, [start, end, value]);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
find(position: number): T | null {
|
|
12
|
+
const index = this.findIndex(position);
|
|
13
|
+
const item = this.items[index];
|
|
14
|
+
if (!item || item[0] > position || item[1] < position) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
return item[2];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
private findIndex(position: number): number {
|
|
21
|
+
let low = 0;
|
|
22
|
+
let high = this.items.length;
|
|
23
|
+
let index = 0;
|
|
24
|
+
while (low < high) {
|
|
25
|
+
index = low + Math.floor((high - low) / 2);
|
|
26
|
+
const item = this.items[index];
|
|
27
|
+
if (item[0] > position) {
|
|
28
|
+
if (index === low) {
|
|
29
|
+
return index
|
|
30
|
+
}
|
|
31
|
+
high = index;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (item[1] <= position) {
|
|
35
|
+
if (index === high - 1) {
|
|
36
|
+
return index + 1
|
|
37
|
+
}
|
|
38
|
+
low = index + 1;
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
return index;
|
|
44
|
+
}
|
|
45
|
+
}
|