p2p-media-loader-dashjs 5.0.0-alpha.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/LICENSE +202 -0
- package/README.md +119 -0
- package/dist/p2p-media-loader-dashjs.es.js +377 -0
- package/dist/p2p-media-loader-dashjs.es.js.map +1 -0
- package/dist/p2p-media-loader-dashjs.es.min.js +207 -0
- package/dist/p2p-media-loader-dashjs.es.min.js.map +1 -0
- package/dist/p2p-media-loader-dashjs.iife.js +8561 -0
- package/dist/p2p-media-loader-dashjs.iife.js.map +1 -0
- package/dist/p2p-media-loader-dashjs.iife.min.js +2 -0
- package/dist/p2p-media-loader-dashjs.iife.min.js.map +1 -0
- package/lib/engine.d.ts +124 -0
- package/lib/engine.js +294 -0
- package/lib/engine.js.map +1 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +3 -0
- package/lib/index.js.map +1 -0
- package/lib/loader.d.ts +17 -0
- package/lib/loader.js +209 -0
- package/lib/loader.js.map +1 -0
- package/lib/types.d.ts +68 -0
- package/lib/types.js +14 -0
- package/lib/types.js.map +1 -0
- package/package.json +59 -0
- package/src/engine.ts +321 -0
- package/src/index.ts +8 -0
- package/src/loader.ts +220 -0
- package/src/types.ts +78 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,gBAAgB,EAEhB,wBAAwB,EACxB,KAAK,GACN,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAIL,YAAY,GAEb,MAAM,YAAY,CAAC;AAOpB,MAAM,aAAa,GAAwB,IAAI,GAAG,CAAC;IACjD,YAAY,CAAC,YAAY;IACzB,YAAY,CAAC,aAAa;IAC1B,YAAY,CAAC,aAAa;CAC3B,CAAC,CAAC;AAEH;;;;;;;;;GASG;AACH,MAAM,UAAU,wBAAwB,CAAC,IAAU,EAAE,QAAqB,EAAE;IAC1E,OA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slice of dash.js's request plumbing the adapter touches, typed
|
|
3
|
+
* structurally. dash.js's own declarations for these live in
|
|
4
|
+
* `@svta/cml-request`, a transitive dependency this package cannot import
|
|
5
|
+
* directly, and the adapter needs only a handful of fields.
|
|
6
|
+
*/
|
|
7
|
+
/** `HTTPRequest.*_TYPE` values a request carries in `customData.request.type`. */
|
|
8
|
+
export declare const REQUEST_TYPE: {
|
|
9
|
+
readonly MPD: "MPD";
|
|
10
|
+
readonly INIT_SEGMENT: "InitializationSegment";
|
|
11
|
+
readonly MEDIA_SEGMENT: "MediaSegment";
|
|
12
|
+
readonly INDEX_SEGMENT: "IndexSegment";
|
|
13
|
+
};
|
|
14
|
+
/** What `_onprogress` in dash.js's HTTPLoader reads off a progress event. */
|
|
15
|
+
export type ProgressEventLike = {
|
|
16
|
+
lengthComputable: boolean;
|
|
17
|
+
loaded: number;
|
|
18
|
+
total: number;
|
|
19
|
+
/** Trace duration in ms; dash.js uses it instead of the wall clock when set. */
|
|
20
|
+
time?: number;
|
|
21
|
+
};
|
|
22
|
+
/** The dash.js `FragmentRequest` (or `TextRequest`) behind a network request. */
|
|
23
|
+
type FragmentRequestLike = {
|
|
24
|
+
type?: string | null;
|
|
25
|
+
url?: string | null;
|
|
26
|
+
range?: string | null;
|
|
27
|
+
mediaType?: string | null;
|
|
28
|
+
};
|
|
29
|
+
/** Callbacks and state dash.js's HTTPLoader hangs on a request. */
|
|
30
|
+
type RequestCustomData = {
|
|
31
|
+
request?: FragmentRequestLike;
|
|
32
|
+
onloadend?: () => void;
|
|
33
|
+
onprogress?: (event: ProgressEventLike) => void;
|
|
34
|
+
onabort?: () => void;
|
|
35
|
+
ontimeout?: (event: unknown) => void;
|
|
36
|
+
abort?: () => void;
|
|
37
|
+
};
|
|
38
|
+
/** `CommonMediaRequest` as HTTPLoader builds it. */
|
|
39
|
+
export type CommonMediaRequestLike = {
|
|
40
|
+
url: string;
|
|
41
|
+
method?: string;
|
|
42
|
+
responseType?: string;
|
|
43
|
+
headers?: Record<string, string | undefined>;
|
|
44
|
+
customData?: RequestCustomData;
|
|
45
|
+
};
|
|
46
|
+
/** `CommonMediaResponse` as HTTPLoader expects the loader to fill it. */
|
|
47
|
+
export type CommonMediaResponseLike = {
|
|
48
|
+
url?: string;
|
|
49
|
+
status: number;
|
|
50
|
+
statusText?: string;
|
|
51
|
+
headers?: Record<string, string> | null;
|
|
52
|
+
data?: unknown;
|
|
53
|
+
};
|
|
54
|
+
/** The instance dash.js's own `XHRLoader` factory produces. */
|
|
55
|
+
export type XhrLoaderLike = {
|
|
56
|
+
load(request: CommonMediaRequestLike, response: CommonMediaResponseLike): boolean;
|
|
57
|
+
abort(): void;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* What `FactoryMaker` binds as `this` when it applies an `override`
|
|
61
|
+
* extension: the real instance is `parent`.
|
|
62
|
+
*/
|
|
63
|
+
export type FactoryMakerThis = {
|
|
64
|
+
context: object;
|
|
65
|
+
factory: unknown;
|
|
66
|
+
parent: XhrLoaderLike;
|
|
67
|
+
};
|
|
68
|
+
export {};
|
package/lib/types.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The slice of dash.js's request plumbing the adapter touches, typed
|
|
3
|
+
* structurally. dash.js's own declarations for these live in
|
|
4
|
+
* `@svta/cml-request`, a transitive dependency this package cannot import
|
|
5
|
+
* directly, and the adapter needs only a handful of fields.
|
|
6
|
+
*/
|
|
7
|
+
/** `HTTPRequest.*_TYPE` values a request carries in `customData.request.type`. */
|
|
8
|
+
export const REQUEST_TYPE = {
|
|
9
|
+
MPD: "MPD",
|
|
10
|
+
INIT_SEGMENT: "InitializationSegment",
|
|
11
|
+
MEDIA_SEGMENT: "MediaSegment",
|
|
12
|
+
INDEX_SEGMENT: "IndexSegment",
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=types.js.map
|
package/lib/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,kFAAkF;AAClF,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B,GAAG,EAAE,KAAK;IACV,YAAY,EAAE,uBAAuB;IACrC,aAAa,EAAE,cAAc;IAC7B,aAAa,EAAE,cAAc;CACrB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "p2p-media-loader-dashjs",
|
|
3
|
+
"version": "5.0.0-alpha.1",
|
|
4
|
+
"description": "P2P Media Loader dash.js integration",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "Novage",
|
|
7
|
+
"homepage": "https://github.com/Novage/p2p-media-loader",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/Novage/p2p-media-loader",
|
|
11
|
+
"directory": "packages/p2p-media-loader-dashjs"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"p2p",
|
|
15
|
+
"peer-to-peer",
|
|
16
|
+
"dash",
|
|
17
|
+
"webrtc",
|
|
18
|
+
"video",
|
|
19
|
+
"mse",
|
|
20
|
+
"player",
|
|
21
|
+
"torrent",
|
|
22
|
+
"bittorrent",
|
|
23
|
+
"webtorrent",
|
|
24
|
+
"dashjs",
|
|
25
|
+
"ecdn",
|
|
26
|
+
"cdn"
|
|
27
|
+
],
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"lib",
|
|
31
|
+
"src"
|
|
32
|
+
],
|
|
33
|
+
"exports": "./lib/index.js",
|
|
34
|
+
"types": "./lib/index.d.ts",
|
|
35
|
+
"sideEffects": false,
|
|
36
|
+
"type": "module",
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"p2p-media-loader-core": "5.0.0-alpha.1"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"dashjs": "^5.2.1",
|
|
42
|
+
"vitest": "^5.0.0"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"dev": "vite",
|
|
46
|
+
"build": "rimraf lib build && pnpm build:es && pnpm build:esm && pnpm build:esm-min && pnpm build:iife && pnpm build:iife-min",
|
|
47
|
+
"build:esm": "vite build --mode esm",
|
|
48
|
+
"build:esm-min": "vite build --mode esm-min",
|
|
49
|
+
"build:iife": "vite build --mode iife",
|
|
50
|
+
"build:iife-min": "vite build --mode iife-min",
|
|
51
|
+
"build:es": "tsc",
|
|
52
|
+
"prettier": "prettier --write .",
|
|
53
|
+
"lint": "eslint src --report-unused-disable-directives --max-warnings 0 --flag unstable_native_nodejs_ts_config",
|
|
54
|
+
"clean:build": "rimraf lib dist build p2p-media-loader-dashjs-*.tgz",
|
|
55
|
+
"type-check": "npx tsc --noEmit",
|
|
56
|
+
"test": "vitest",
|
|
57
|
+
"test:ci": "vitest run"
|
|
58
|
+
}
|
|
59
|
+
}
|
package/src/engine.ts
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import type { MediaPlayerClass, MediaPlayerEvents } from "dashjs";
|
|
2
|
+
import { dashManifestParser } from "p2p-media-loader-core/dash";
|
|
3
|
+
import {
|
|
4
|
+
Core,
|
|
5
|
+
CoreConfig,
|
|
6
|
+
CoreEventMap,
|
|
7
|
+
DefinedCoreConfig,
|
|
8
|
+
DynamicCoreConfig,
|
|
9
|
+
ProcessedManifest,
|
|
10
|
+
debug,
|
|
11
|
+
getPlaybackStateFromMediaElement,
|
|
12
|
+
liveDelayFor,
|
|
13
|
+
type LiveDelay,
|
|
14
|
+
} from "p2p-media-loader-core";
|
|
15
|
+
import { createXhrLoaderExtension } from "./loader.js";
|
|
16
|
+
|
|
17
|
+
/** A type for specifying dynamic configuration options that can be changed at runtime for the P2P engine's core. */
|
|
18
|
+
export type DynamicDashJsP2PEngineConfig = {
|
|
19
|
+
/** Dynamic core config */
|
|
20
|
+
core?: DynamicCoreConfig;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** Represents the complete configuration for the `DashJsP2PEngine`. */
|
|
24
|
+
export type DashJsP2PEngineConfig = {
|
|
25
|
+
/** Complete core configuration settings. */
|
|
26
|
+
core: DefinedCoreConfig;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** Allows for partial configuration settings for the `DashJsP2PEngine`. */
|
|
30
|
+
export type PartialDashJsP2PEngineConfig = {
|
|
31
|
+
/** Partial core config */
|
|
32
|
+
core?: Partial<CoreConfig>;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Live delay until the first live manifest says how wide the window is; from
|
|
37
|
+
* then on the delay follows the window (see the core's liveDelayFor). Only
|
|
38
|
+
* applied when the integrator left dash.js's own default in place.
|
|
39
|
+
*/
|
|
40
|
+
const INITIAL_LIVE_EDGE_DELAY = 25;
|
|
41
|
+
|
|
42
|
+
/** Least forward buffer to leave the player, whatever the windows work out to. */
|
|
43
|
+
const MIN_BUFFER_SEGMENTS = 2;
|
|
44
|
+
/** Segments left between the player's forward buffer and the live edge. */
|
|
45
|
+
const LIVE_EDGE_MARGIN_SEGMENTS = 1;
|
|
46
|
+
|
|
47
|
+
const STREAM_INITIALIZED: MediaPlayerEvents["STREAM_INITIALIZED"] =
|
|
48
|
+
"streamInitialized";
|
|
49
|
+
const STREAM_TEARDOWN_COMPLETE: MediaPlayerEvents["STREAM_TEARDOWN_COMPLETE"] =
|
|
50
|
+
"streamTeardownComplete";
|
|
51
|
+
|
|
52
|
+
// Every event after which the buffer ahead of the playhead or the rate may
|
|
53
|
+
// have changed. `progress` covers buffer growth without playhead movement.
|
|
54
|
+
const PLAYBACK_EVENTS = [
|
|
55
|
+
"timeupdate",
|
|
56
|
+
"progress",
|
|
57
|
+
"seeking",
|
|
58
|
+
"seeked",
|
|
59
|
+
"ratechange",
|
|
60
|
+
"play",
|
|
61
|
+
"pause",
|
|
62
|
+
"waiting",
|
|
63
|
+
] as const;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Represents a Peer-to-Peer (P2P) engine designed to enhance media streaming efficiency.
|
|
67
|
+
* This class integrates P2P technologies into dash.js, enabling the distribution of media segments via a peer network
|
|
68
|
+
* alongside traditional HTTP fetching. This reduces server bandwidth costs and improves scalability by sharing the load
|
|
69
|
+
* across multiple clients.
|
|
70
|
+
*
|
|
71
|
+
* The engine has three responsibilities (see specs/player-adapters.md): it
|
|
72
|
+
* hands every MPD dash.js fetches to the core, routes segment requests
|
|
73
|
+
* through the core, and reports playback state from the media element. dash.js's
|
|
74
|
+
* own manifest parsing runs untouched; the core parses the same bytes itself.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* const player = dashjs.MediaPlayer().create();
|
|
78
|
+
* const engine = new DashJsP2PEngine({
|
|
79
|
+
* core: {
|
|
80
|
+
* swarmId: "example-swarm-id",
|
|
81
|
+
* announceTrackers: ["wss://personal.tracker.com"],
|
|
82
|
+
* },
|
|
83
|
+
* });
|
|
84
|
+
* engine.bindPlayer(player); // before initialize()
|
|
85
|
+
* player.initialize(videoElement, manifestUrl, true);
|
|
86
|
+
*/
|
|
87
|
+
export class DashJsP2PEngine {
|
|
88
|
+
private player?: MediaPlayerClass;
|
|
89
|
+
private media?: HTMLMediaElement;
|
|
90
|
+
private readonly core: Core;
|
|
91
|
+
/** False when the integrator configured a live delay themselves. */
|
|
92
|
+
private managesLiveDelay = false;
|
|
93
|
+
private readonly debug = debug("p2pml-dashjs:engine");
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Constructs an instance of `DashJsP2PEngine`.
|
|
97
|
+
*
|
|
98
|
+
* @param config An optional configuration for customizing the P2P engine's behavior.
|
|
99
|
+
*/
|
|
100
|
+
constructor(config?: PartialDashJsP2PEngineConfig) {
|
|
101
|
+
this.core = new Core({
|
|
102
|
+
...config?.core,
|
|
103
|
+
// dash.js plays DASH only, so its bundle carries the DASH parser only.
|
|
104
|
+
manifestParsers: config?.core?.manifestParsers ?? [dashManifestParser],
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Hooks the engine into a dash.js player. Call before `player.initialize()`:
|
|
110
|
+
* the engine replaces the player's `XHRLoader` through `player.extend`,
|
|
111
|
+
* which dash.js resolves when it first loads.
|
|
112
|
+
*
|
|
113
|
+
* @param player The dash.js `MediaPlayer` instance.
|
|
114
|
+
*/
|
|
115
|
+
bindPlayer(player: MediaPlayerClass) {
|
|
116
|
+
if (this.player === player) return;
|
|
117
|
+
if (this.player) this.destroy();
|
|
118
|
+
this.player = player;
|
|
119
|
+
|
|
120
|
+
const liveDelay = player.getSettings().streaming?.delay?.liveDelay;
|
|
121
|
+
this.managesLiveDelay = liveDelay === undefined || Number.isNaN(liveDelay);
|
|
122
|
+
if (this.managesLiveDelay) {
|
|
123
|
+
player.updateSettings({
|
|
124
|
+
streaming: {
|
|
125
|
+
delay: {
|
|
126
|
+
liveDelay: INITIAL_LIVE_EDGE_DELAY,
|
|
127
|
+
// A server's suggestion places the player near the edge, where
|
|
128
|
+
// there is nothing to share.
|
|
129
|
+
useSuggestedPresentationDelay: false,
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
player.extend(
|
|
136
|
+
"XHRLoader",
|
|
137
|
+
createXhrLoaderExtension(this.core, {
|
|
138
|
+
onManifestProcessed: this.applyLivePlacement,
|
|
139
|
+
}),
|
|
140
|
+
true,
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
player.on(STREAM_INITIALIZED, this.handleStreamInitialized);
|
|
144
|
+
player.on(STREAM_TEARDOWN_COMPLETE, this.handleStreamTeardown);
|
|
145
|
+
this.registerMediaElement();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Applies dynamic configuration updates to the P2P engine.
|
|
150
|
+
*
|
|
151
|
+
* @param dynamicConfig The configuration changes to apply.
|
|
152
|
+
*/
|
|
153
|
+
applyDynamicConfig(dynamicConfig: DynamicDashJsP2PEngineConfig) {
|
|
154
|
+
if (dynamicConfig.core) this.core.applyDynamicConfig(dynamicConfig.core);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Retrieves the current configuration of the `DashJsP2PEngine`.
|
|
159
|
+
*
|
|
160
|
+
* @returns The configuration as a readonly object.
|
|
161
|
+
*/
|
|
162
|
+
getConfig(): DashJsP2PEngineConfig {
|
|
163
|
+
return { core: this.core.getConfig() };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Adds an event listener for the specified event.
|
|
168
|
+
* @param eventName The name of the event to listen for.
|
|
169
|
+
* @param listener The callback function to be invoked when the event is triggered.
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* engine.addEventListener('onSegmentLoaded', (details) => {
|
|
173
|
+
* console.log('Segment Loaded:', details);
|
|
174
|
+
* });
|
|
175
|
+
*/
|
|
176
|
+
addEventListener<K extends keyof CoreEventMap>(
|
|
177
|
+
eventName: K,
|
|
178
|
+
listener: CoreEventMap[K],
|
|
179
|
+
) {
|
|
180
|
+
this.core.addEventListener(eventName, listener);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Removes an event listener for the specified event.
|
|
185
|
+
* @param eventName The name of the event.
|
|
186
|
+
* @param listener The callback function that was previously added.
|
|
187
|
+
*/
|
|
188
|
+
removeEventListener<K extends keyof CoreEventMap>(
|
|
189
|
+
eventName: K,
|
|
190
|
+
listener: CoreEventMap[K],
|
|
191
|
+
) {
|
|
192
|
+
this.core.removeEventListener(eventName, listener);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Places the player in the live window from the window the core just
|
|
197
|
+
* parsed: how far behind the edge it plays, and how far ahead of the
|
|
198
|
+
* playhead it may fetch. The MPD reaches the core before dash.js parses the
|
|
199
|
+
* same bytes, so both are in place when dash.js computes its live position.
|
|
200
|
+
* Re-applied only when the window changes by at least half a segment.
|
|
201
|
+
*/
|
|
202
|
+
private applyLivePlacement = (manifest: ProcessedManifest) => {
|
|
203
|
+
if (!this.player || !this.managesLiveDelay) return;
|
|
204
|
+
const target = liveDelayFor(manifest);
|
|
205
|
+
if (!target) return;
|
|
206
|
+
|
|
207
|
+
const current = this.player.getSettings().streaming?.delay?.liveDelay;
|
|
208
|
+
if (
|
|
209
|
+
current !== undefined &&
|
|
210
|
+
!Number.isNaN(current) &&
|
|
211
|
+
Math.abs(current - target.delay) < target.segment / 2
|
|
212
|
+
) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const buffer = this.forwardBufferSettings(target);
|
|
217
|
+
this.debug(
|
|
218
|
+
`Setting liveDelay to ${target.delay}, forward buffer to ${buffer.bufferTimeDefault}`,
|
|
219
|
+
);
|
|
220
|
+
this.player.updateSettings({
|
|
221
|
+
streaming: { delay: { liveDelay: target.delay }, buffer },
|
|
222
|
+
});
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* How far ahead of the playhead dash.js may fetch: the high-demand window,
|
|
227
|
+
* never closer to the live edge than a segment, never less than a couple of
|
|
228
|
+
* segments. The segments beyond it are the core's to prefetch, and they are
|
|
229
|
+
* the ones peers exchange.
|
|
230
|
+
*
|
|
231
|
+
* Left alone, dash.js buffers `bufferTimeAtTopQualityLongForm` — a minute,
|
|
232
|
+
* since a dynamic stream is long-form by its duration — which on a live
|
|
233
|
+
* stream is the whole window. Its playhead then sits a live delay behind the
|
|
234
|
+
* edge while its *fetch* position rides the edge itself, where the registry
|
|
235
|
+
* cannot yet know the segment: core learns of one only when dash.js refreshes
|
|
236
|
+
* the MPD, and dash.js derives availability from its own synced clock. Every
|
|
237
|
+
* such request misses the registry and falls through to dash.js's own loader,
|
|
238
|
+
* so the stream plays perfectly with no P2P at all.
|
|
239
|
+
*
|
|
240
|
+
* Settings the integrator already holds below this are left alone; the value
|
|
241
|
+
* is a ceiling, not a target.
|
|
242
|
+
*/
|
|
243
|
+
private forwardBufferSettings(target: LiveDelay) {
|
|
244
|
+
const { mainStream, secondaryStream } = this.core.getConfig();
|
|
245
|
+
const highDemandTimeWindow = Math.max(
|
|
246
|
+
mainStream.highDemandTimeWindow,
|
|
247
|
+
secondaryStream.highDemandTimeWindow,
|
|
248
|
+
);
|
|
249
|
+
const bufferTime = Math.max(
|
|
250
|
+
target.segment * MIN_BUFFER_SEGMENTS,
|
|
251
|
+
Math.min(
|
|
252
|
+
highDemandTimeWindow,
|
|
253
|
+
target.delay - target.segment * LIVE_EDGE_MARGIN_SEGMENTS,
|
|
254
|
+
),
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
const current = this.player?.getSettings().streaming?.buffer;
|
|
258
|
+
const lower = (setting: number | undefined) =>
|
|
259
|
+
setting === undefined || Number.isNaN(setting) || setting > bufferTime
|
|
260
|
+
? bufferTime
|
|
261
|
+
: setting;
|
|
262
|
+
|
|
263
|
+
return {
|
|
264
|
+
bufferTimeDefault: lower(current?.bufferTimeDefault),
|
|
265
|
+
bufferTimeAtTopQuality: lower(current?.bufferTimeAtTopQuality),
|
|
266
|
+
bufferTimeAtTopQualityLongForm: lower(
|
|
267
|
+
current?.bufferTimeAtTopQualityLongForm,
|
|
268
|
+
),
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
private handleStreamInitialized = () => {
|
|
273
|
+
this.registerMediaElement();
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
private handleStreamTeardown = () => {
|
|
277
|
+
this.core.destroy();
|
|
278
|
+
this.unregisterMediaElement();
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
private registerMediaElement() {
|
|
282
|
+
let media: HTMLMediaElement | undefined;
|
|
283
|
+
try {
|
|
284
|
+
// Throws until a view is attached.
|
|
285
|
+
media = this.player?.getVideoElement();
|
|
286
|
+
} catch {
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
if (!media || media === this.media) return;
|
|
290
|
+
this.unregisterMediaElement();
|
|
291
|
+
this.media = media;
|
|
292
|
+
for (const event of PLAYBACK_EVENTS) {
|
|
293
|
+
media.addEventListener(event, this.handlePlaybackUpdate);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
private unregisterMediaElement() {
|
|
298
|
+
const { media } = this;
|
|
299
|
+
if (!media) return;
|
|
300
|
+
for (const event of PLAYBACK_EVENTS) {
|
|
301
|
+
media.removeEventListener(event, this.handlePlaybackUpdate);
|
|
302
|
+
}
|
|
303
|
+
this.media = undefined;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
private handlePlaybackUpdate = (event: Event) => {
|
|
307
|
+
const media = event.target as HTMLMediaElement;
|
|
308
|
+
this.core.updatePlayback(getPlaybackStateFromMediaElement(media));
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
/** Cleans up and releases all resources, and unregisters all event handlers. */
|
|
312
|
+
destroy() {
|
|
313
|
+
this.core.destroy();
|
|
314
|
+
this.unregisterMediaElement();
|
|
315
|
+
if (this.player) {
|
|
316
|
+
this.player.off(STREAM_INITIALIZED, this.handleStreamInitialized);
|
|
317
|
+
this.player.off(STREAM_TEARDOWN_COMPLETE, this.handleStreamTeardown);
|
|
318
|
+
}
|
|
319
|
+
this.player = undefined;
|
|
320
|
+
}
|
|
321
|
+
}
|