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
package/lib/engine.d.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { MediaPlayerClass } from "dashjs";
|
|
2
|
+
import { CoreConfig, CoreEventMap, DefinedCoreConfig, DynamicCoreConfig } from "p2p-media-loader-core";
|
|
3
|
+
/** A type for specifying dynamic configuration options that can be changed at runtime for the P2P engine's core. */
|
|
4
|
+
export type DynamicDashJsP2PEngineConfig = {
|
|
5
|
+
/** Dynamic core config */
|
|
6
|
+
core?: DynamicCoreConfig;
|
|
7
|
+
};
|
|
8
|
+
/** Represents the complete configuration for the `DashJsP2PEngine`. */
|
|
9
|
+
export type DashJsP2PEngineConfig = {
|
|
10
|
+
/** Complete core configuration settings. */
|
|
11
|
+
core: DefinedCoreConfig;
|
|
12
|
+
};
|
|
13
|
+
/** Allows for partial configuration settings for the `DashJsP2PEngine`. */
|
|
14
|
+
export type PartialDashJsP2PEngineConfig = {
|
|
15
|
+
/** Partial core config */
|
|
16
|
+
core?: Partial<CoreConfig>;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Represents a Peer-to-Peer (P2P) engine designed to enhance media streaming efficiency.
|
|
20
|
+
* This class integrates P2P technologies into dash.js, enabling the distribution of media segments via a peer network
|
|
21
|
+
* alongside traditional HTTP fetching. This reduces server bandwidth costs and improves scalability by sharing the load
|
|
22
|
+
* across multiple clients.
|
|
23
|
+
*
|
|
24
|
+
* The engine has three responsibilities (see specs/player-adapters.md): it
|
|
25
|
+
* hands every MPD dash.js fetches to the core, routes segment requests
|
|
26
|
+
* through the core, and reports playback state from the media element. dash.js's
|
|
27
|
+
* own manifest parsing runs untouched; the core parses the same bytes itself.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* const player = dashjs.MediaPlayer().create();
|
|
31
|
+
* const engine = new DashJsP2PEngine({
|
|
32
|
+
* core: {
|
|
33
|
+
* swarmId: "example-swarm-id",
|
|
34
|
+
* announceTrackers: ["wss://personal.tracker.com"],
|
|
35
|
+
* },
|
|
36
|
+
* });
|
|
37
|
+
* engine.bindPlayer(player); // before initialize()
|
|
38
|
+
* player.initialize(videoElement, manifestUrl, true);
|
|
39
|
+
*/
|
|
40
|
+
export declare class DashJsP2PEngine {
|
|
41
|
+
private player?;
|
|
42
|
+
private media?;
|
|
43
|
+
private readonly core;
|
|
44
|
+
/** False when the integrator configured a live delay themselves. */
|
|
45
|
+
private managesLiveDelay;
|
|
46
|
+
private readonly debug;
|
|
47
|
+
/**
|
|
48
|
+
* Constructs an instance of `DashJsP2PEngine`.
|
|
49
|
+
*
|
|
50
|
+
* @param config An optional configuration for customizing the P2P engine's behavior.
|
|
51
|
+
*/
|
|
52
|
+
constructor(config?: PartialDashJsP2PEngineConfig);
|
|
53
|
+
/**
|
|
54
|
+
* Hooks the engine into a dash.js player. Call before `player.initialize()`:
|
|
55
|
+
* the engine replaces the player's `XHRLoader` through `player.extend`,
|
|
56
|
+
* which dash.js resolves when it first loads.
|
|
57
|
+
*
|
|
58
|
+
* @param player The dash.js `MediaPlayer` instance.
|
|
59
|
+
*/
|
|
60
|
+
bindPlayer(player: MediaPlayerClass): void;
|
|
61
|
+
/**
|
|
62
|
+
* Applies dynamic configuration updates to the P2P engine.
|
|
63
|
+
*
|
|
64
|
+
* @param dynamicConfig The configuration changes to apply.
|
|
65
|
+
*/
|
|
66
|
+
applyDynamicConfig(dynamicConfig: DynamicDashJsP2PEngineConfig): void;
|
|
67
|
+
/**
|
|
68
|
+
* Retrieves the current configuration of the `DashJsP2PEngine`.
|
|
69
|
+
*
|
|
70
|
+
* @returns The configuration as a readonly object.
|
|
71
|
+
*/
|
|
72
|
+
getConfig(): DashJsP2PEngineConfig;
|
|
73
|
+
/**
|
|
74
|
+
* Adds an event listener for the specified event.
|
|
75
|
+
* @param eventName The name of the event to listen for.
|
|
76
|
+
* @param listener The callback function to be invoked when the event is triggered.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* engine.addEventListener('onSegmentLoaded', (details) => {
|
|
80
|
+
* console.log('Segment Loaded:', details);
|
|
81
|
+
* });
|
|
82
|
+
*/
|
|
83
|
+
addEventListener<K extends keyof CoreEventMap>(eventName: K, listener: CoreEventMap[K]): void;
|
|
84
|
+
/**
|
|
85
|
+
* Removes an event listener for the specified event.
|
|
86
|
+
* @param eventName The name of the event.
|
|
87
|
+
* @param listener The callback function that was previously added.
|
|
88
|
+
*/
|
|
89
|
+
removeEventListener<K extends keyof CoreEventMap>(eventName: K, listener: CoreEventMap[K]): void;
|
|
90
|
+
/**
|
|
91
|
+
* Places the player in the live window from the window the core just
|
|
92
|
+
* parsed: how far behind the edge it plays, and how far ahead of the
|
|
93
|
+
* playhead it may fetch. The MPD reaches the core before dash.js parses the
|
|
94
|
+
* same bytes, so both are in place when dash.js computes its live position.
|
|
95
|
+
* Re-applied only when the window changes by at least half a segment.
|
|
96
|
+
*/
|
|
97
|
+
private applyLivePlacement;
|
|
98
|
+
/**
|
|
99
|
+
* How far ahead of the playhead dash.js may fetch: the high-demand window,
|
|
100
|
+
* never closer to the live edge than a segment, never less than a couple of
|
|
101
|
+
* segments. The segments beyond it are the core's to prefetch, and they are
|
|
102
|
+
* the ones peers exchange.
|
|
103
|
+
*
|
|
104
|
+
* Left alone, dash.js buffers `bufferTimeAtTopQualityLongForm` — a minute,
|
|
105
|
+
* since a dynamic stream is long-form by its duration — which on a live
|
|
106
|
+
* stream is the whole window. Its playhead then sits a live delay behind the
|
|
107
|
+
* edge while its *fetch* position rides the edge itself, where the registry
|
|
108
|
+
* cannot yet know the segment: core learns of one only when dash.js refreshes
|
|
109
|
+
* the MPD, and dash.js derives availability from its own synced clock. Every
|
|
110
|
+
* such request misses the registry and falls through to dash.js's own loader,
|
|
111
|
+
* so the stream plays perfectly with no P2P at all.
|
|
112
|
+
*
|
|
113
|
+
* Settings the integrator already holds below this are left alone; the value
|
|
114
|
+
* is a ceiling, not a target.
|
|
115
|
+
*/
|
|
116
|
+
private forwardBufferSettings;
|
|
117
|
+
private handleStreamInitialized;
|
|
118
|
+
private handleStreamTeardown;
|
|
119
|
+
private registerMediaElement;
|
|
120
|
+
private unregisterMediaElement;
|
|
121
|
+
private handlePlaybackUpdate;
|
|
122
|
+
/** Cleans up and releases all resources, and unregisters all event handlers. */
|
|
123
|
+
destroy(): void;
|
|
124
|
+
}
|
package/lib/engine.js
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { dashManifestParser } from "p2p-media-loader-core/dash";
|
|
2
|
+
import { Core, debug, getPlaybackStateFromMediaElement, liveDelayFor, } from "p2p-media-loader-core";
|
|
3
|
+
import { createXhrLoaderExtension } from "./loader.js";
|
|
4
|
+
/**
|
|
5
|
+
* Live delay until the first live manifest says how wide the window is; from
|
|
6
|
+
* then on the delay follows the window (see the core's liveDelayFor). Only
|
|
7
|
+
* applied when the integrator left dash.js's own default in place.
|
|
8
|
+
*/
|
|
9
|
+
const INITIAL_LIVE_EDGE_DELAY = 25;
|
|
10
|
+
/** Least forward buffer to leave the player, whatever the windows work out to. */
|
|
11
|
+
const MIN_BUFFER_SEGMENTS = 2;
|
|
12
|
+
/** Segments left between the player's forward buffer and the live edge. */
|
|
13
|
+
const LIVE_EDGE_MARGIN_SEGMENTS = 1;
|
|
14
|
+
const STREAM_INITIALIZED = "streamInitialized";
|
|
15
|
+
const STREAM_TEARDOWN_COMPLETE = "streamTeardownComplete";
|
|
16
|
+
// Every event after which the buffer ahead of the playhead or the rate may
|
|
17
|
+
// have changed. `progress` covers buffer growth without playhead movement.
|
|
18
|
+
const PLAYBACK_EVENTS = [
|
|
19
|
+
"timeupdate",
|
|
20
|
+
"progress",
|
|
21
|
+
"seeking",
|
|
22
|
+
"seeked",
|
|
23
|
+
"ratechange",
|
|
24
|
+
"play",
|
|
25
|
+
"pause",
|
|
26
|
+
"waiting",
|
|
27
|
+
];
|
|
28
|
+
/**
|
|
29
|
+
* Represents a Peer-to-Peer (P2P) engine designed to enhance media streaming efficiency.
|
|
30
|
+
* This class integrates P2P technologies into dash.js, enabling the distribution of media segments via a peer network
|
|
31
|
+
* alongside traditional HTTP fetching. This reduces server bandwidth costs and improves scalability by sharing the load
|
|
32
|
+
* across multiple clients.
|
|
33
|
+
*
|
|
34
|
+
* The engine has three responsibilities (see specs/player-adapters.md): it
|
|
35
|
+
* hands every MPD dash.js fetches to the core, routes segment requests
|
|
36
|
+
* through the core, and reports playback state from the media element. dash.js's
|
|
37
|
+
* own manifest parsing runs untouched; the core parses the same bytes itself.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* const player = dashjs.MediaPlayer().create();
|
|
41
|
+
* const engine = new DashJsP2PEngine({
|
|
42
|
+
* core: {
|
|
43
|
+
* swarmId: "example-swarm-id",
|
|
44
|
+
* announceTrackers: ["wss://personal.tracker.com"],
|
|
45
|
+
* },
|
|
46
|
+
* });
|
|
47
|
+
* engine.bindPlayer(player); // before initialize()
|
|
48
|
+
* player.initialize(videoElement, manifestUrl, true);
|
|
49
|
+
*/
|
|
50
|
+
export class DashJsP2PEngine {
|
|
51
|
+
/**
|
|
52
|
+
* Constructs an instance of `DashJsP2PEngine`.
|
|
53
|
+
*
|
|
54
|
+
* @param config An optional configuration for customizing the P2P engine's behavior.
|
|
55
|
+
*/
|
|
56
|
+
constructor(config) {
|
|
57
|
+
var _a, _b;
|
|
58
|
+
Object.defineProperty(this, "player", {
|
|
59
|
+
enumerable: true,
|
|
60
|
+
configurable: true,
|
|
61
|
+
writable: true,
|
|
62
|
+
value: void 0
|
|
63
|
+
});
|
|
64
|
+
Object.defineProperty(this, "media", {
|
|
65
|
+
enumerable: true,
|
|
66
|
+
configurable: true,
|
|
67
|
+
writable: true,
|
|
68
|
+
value: void 0
|
|
69
|
+
});
|
|
70
|
+
Object.defineProperty(this, "core", {
|
|
71
|
+
enumerable: true,
|
|
72
|
+
configurable: true,
|
|
73
|
+
writable: true,
|
|
74
|
+
value: void 0
|
|
75
|
+
});
|
|
76
|
+
/** False when the integrator configured a live delay themselves. */
|
|
77
|
+
Object.defineProperty(this, "managesLiveDelay", {
|
|
78
|
+
enumerable: true,
|
|
79
|
+
configurable: true,
|
|
80
|
+
writable: true,
|
|
81
|
+
value: false
|
|
82
|
+
});
|
|
83
|
+
Object.defineProperty(this, "debug", {
|
|
84
|
+
enumerable: true,
|
|
85
|
+
configurable: true,
|
|
86
|
+
writable: true,
|
|
87
|
+
value: debug("p2pml-dashjs:engine")
|
|
88
|
+
});
|
|
89
|
+
/**
|
|
90
|
+
* Places the player in the live window from the window the core just
|
|
91
|
+
* parsed: how far behind the edge it plays, and how far ahead of the
|
|
92
|
+
* playhead it may fetch. The MPD reaches the core before dash.js parses the
|
|
93
|
+
* same bytes, so both are in place when dash.js computes its live position.
|
|
94
|
+
* Re-applied only when the window changes by at least half a segment.
|
|
95
|
+
*/
|
|
96
|
+
Object.defineProperty(this, "applyLivePlacement", {
|
|
97
|
+
enumerable: true,
|
|
98
|
+
configurable: true,
|
|
99
|
+
writable: true,
|
|
100
|
+
value: (manifest) => {
|
|
101
|
+
var _a, _b;
|
|
102
|
+
if (!this.player || !this.managesLiveDelay)
|
|
103
|
+
return;
|
|
104
|
+
const target = liveDelayFor(manifest);
|
|
105
|
+
if (!target)
|
|
106
|
+
return;
|
|
107
|
+
const current = (_b = (_a = this.player.getSettings().streaming) === null || _a === void 0 ? void 0 : _a.delay) === null || _b === void 0 ? void 0 : _b.liveDelay;
|
|
108
|
+
if (current !== undefined &&
|
|
109
|
+
!Number.isNaN(current) &&
|
|
110
|
+
Math.abs(current - target.delay) < target.segment / 2) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const buffer = this.forwardBufferSettings(target);
|
|
114
|
+
this.debug(`Setting liveDelay to ${target.delay}, forward buffer to ${buffer.bufferTimeDefault}`);
|
|
115
|
+
this.player.updateSettings({
|
|
116
|
+
streaming: { delay: { liveDelay: target.delay }, buffer },
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
Object.defineProperty(this, "handleStreamInitialized", {
|
|
121
|
+
enumerable: true,
|
|
122
|
+
configurable: true,
|
|
123
|
+
writable: true,
|
|
124
|
+
value: () => {
|
|
125
|
+
this.registerMediaElement();
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
Object.defineProperty(this, "handleStreamTeardown", {
|
|
129
|
+
enumerable: true,
|
|
130
|
+
configurable: true,
|
|
131
|
+
writable: true,
|
|
132
|
+
value: () => {
|
|
133
|
+
this.core.destroy();
|
|
134
|
+
this.unregisterMediaElement();
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
Object.defineProperty(this, "handlePlaybackUpdate", {
|
|
138
|
+
enumerable: true,
|
|
139
|
+
configurable: true,
|
|
140
|
+
writable: true,
|
|
141
|
+
value: (event) => {
|
|
142
|
+
const media = event.target;
|
|
143
|
+
this.core.updatePlayback(getPlaybackStateFromMediaElement(media));
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
this.core = new Core(Object.assign(Object.assign({}, config === null || config === void 0 ? void 0 : config.core), {
|
|
147
|
+
// dash.js plays DASH only, so its bundle carries the DASH parser only.
|
|
148
|
+
manifestParsers: (_b = (_a = config === null || config === void 0 ? void 0 : config.core) === null || _a === void 0 ? void 0 : _a.manifestParsers) !== null && _b !== void 0 ? _b : [dashManifestParser] }));
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Hooks the engine into a dash.js player. Call before `player.initialize()`:
|
|
152
|
+
* the engine replaces the player's `XHRLoader` through `player.extend`,
|
|
153
|
+
* which dash.js resolves when it first loads.
|
|
154
|
+
*
|
|
155
|
+
* @param player The dash.js `MediaPlayer` instance.
|
|
156
|
+
*/
|
|
157
|
+
bindPlayer(player) {
|
|
158
|
+
var _a, _b;
|
|
159
|
+
if (this.player === player)
|
|
160
|
+
return;
|
|
161
|
+
if (this.player)
|
|
162
|
+
this.destroy();
|
|
163
|
+
this.player = player;
|
|
164
|
+
const liveDelay = (_b = (_a = player.getSettings().streaming) === null || _a === void 0 ? void 0 : _a.delay) === null || _b === void 0 ? void 0 : _b.liveDelay;
|
|
165
|
+
this.managesLiveDelay = liveDelay === undefined || Number.isNaN(liveDelay);
|
|
166
|
+
if (this.managesLiveDelay) {
|
|
167
|
+
player.updateSettings({
|
|
168
|
+
streaming: {
|
|
169
|
+
delay: {
|
|
170
|
+
liveDelay: INITIAL_LIVE_EDGE_DELAY,
|
|
171
|
+
// A server's suggestion places the player near the edge, where
|
|
172
|
+
// there is nothing to share.
|
|
173
|
+
useSuggestedPresentationDelay: false,
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
player.extend("XHRLoader", createXhrLoaderExtension(this.core, {
|
|
179
|
+
onManifestProcessed: this.applyLivePlacement,
|
|
180
|
+
}), true);
|
|
181
|
+
player.on(STREAM_INITIALIZED, this.handleStreamInitialized);
|
|
182
|
+
player.on(STREAM_TEARDOWN_COMPLETE, this.handleStreamTeardown);
|
|
183
|
+
this.registerMediaElement();
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Applies dynamic configuration updates to the P2P engine.
|
|
187
|
+
*
|
|
188
|
+
* @param dynamicConfig The configuration changes to apply.
|
|
189
|
+
*/
|
|
190
|
+
applyDynamicConfig(dynamicConfig) {
|
|
191
|
+
if (dynamicConfig.core)
|
|
192
|
+
this.core.applyDynamicConfig(dynamicConfig.core);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Retrieves the current configuration of the `DashJsP2PEngine`.
|
|
196
|
+
*
|
|
197
|
+
* @returns The configuration as a readonly object.
|
|
198
|
+
*/
|
|
199
|
+
getConfig() {
|
|
200
|
+
return { core: this.core.getConfig() };
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Adds an event listener for the specified event.
|
|
204
|
+
* @param eventName The name of the event to listen for.
|
|
205
|
+
* @param listener The callback function to be invoked when the event is triggered.
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* engine.addEventListener('onSegmentLoaded', (details) => {
|
|
209
|
+
* console.log('Segment Loaded:', details);
|
|
210
|
+
* });
|
|
211
|
+
*/
|
|
212
|
+
addEventListener(eventName, listener) {
|
|
213
|
+
this.core.addEventListener(eventName, listener);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Removes an event listener for the specified event.
|
|
217
|
+
* @param eventName The name of the event.
|
|
218
|
+
* @param listener The callback function that was previously added.
|
|
219
|
+
*/
|
|
220
|
+
removeEventListener(eventName, listener) {
|
|
221
|
+
this.core.removeEventListener(eventName, listener);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* How far ahead of the playhead dash.js may fetch: the high-demand window,
|
|
225
|
+
* never closer to the live edge than a segment, never less than a couple of
|
|
226
|
+
* segments. The segments beyond it are the core's to prefetch, and they are
|
|
227
|
+
* the ones peers exchange.
|
|
228
|
+
*
|
|
229
|
+
* Left alone, dash.js buffers `bufferTimeAtTopQualityLongForm` — a minute,
|
|
230
|
+
* since a dynamic stream is long-form by its duration — which on a live
|
|
231
|
+
* stream is the whole window. Its playhead then sits a live delay behind the
|
|
232
|
+
* edge while its *fetch* position rides the edge itself, where the registry
|
|
233
|
+
* cannot yet know the segment: core learns of one only when dash.js refreshes
|
|
234
|
+
* the MPD, and dash.js derives availability from its own synced clock. Every
|
|
235
|
+
* such request misses the registry and falls through to dash.js's own loader,
|
|
236
|
+
* so the stream plays perfectly with no P2P at all.
|
|
237
|
+
*
|
|
238
|
+
* Settings the integrator already holds below this are left alone; the value
|
|
239
|
+
* is a ceiling, not a target.
|
|
240
|
+
*/
|
|
241
|
+
forwardBufferSettings(target) {
|
|
242
|
+
var _a, _b;
|
|
243
|
+
const { mainStream, secondaryStream } = this.core.getConfig();
|
|
244
|
+
const highDemandTimeWindow = Math.max(mainStream.highDemandTimeWindow, secondaryStream.highDemandTimeWindow);
|
|
245
|
+
const bufferTime = Math.max(target.segment * MIN_BUFFER_SEGMENTS, Math.min(highDemandTimeWindow, target.delay - target.segment * LIVE_EDGE_MARGIN_SEGMENTS));
|
|
246
|
+
const current = (_b = (_a = this.player) === null || _a === void 0 ? void 0 : _a.getSettings().streaming) === null || _b === void 0 ? void 0 : _b.buffer;
|
|
247
|
+
const lower = (setting) => setting === undefined || Number.isNaN(setting) || setting > bufferTime
|
|
248
|
+
? bufferTime
|
|
249
|
+
: setting;
|
|
250
|
+
return {
|
|
251
|
+
bufferTimeDefault: lower(current === null || current === void 0 ? void 0 : current.bufferTimeDefault),
|
|
252
|
+
bufferTimeAtTopQuality: lower(current === null || current === void 0 ? void 0 : current.bufferTimeAtTopQuality),
|
|
253
|
+
bufferTimeAtTopQualityLongForm: lower(current === null || current === void 0 ? void 0 : current.bufferTimeAtTopQualityLongForm),
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
registerMediaElement() {
|
|
257
|
+
var _a;
|
|
258
|
+
let media;
|
|
259
|
+
try {
|
|
260
|
+
// Throws until a view is attached.
|
|
261
|
+
media = (_a = this.player) === null || _a === void 0 ? void 0 : _a.getVideoElement();
|
|
262
|
+
}
|
|
263
|
+
catch (_b) {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
if (!media || media === this.media)
|
|
267
|
+
return;
|
|
268
|
+
this.unregisterMediaElement();
|
|
269
|
+
this.media = media;
|
|
270
|
+
for (const event of PLAYBACK_EVENTS) {
|
|
271
|
+
media.addEventListener(event, this.handlePlaybackUpdate);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
unregisterMediaElement() {
|
|
275
|
+
const { media } = this;
|
|
276
|
+
if (!media)
|
|
277
|
+
return;
|
|
278
|
+
for (const event of PLAYBACK_EVENTS) {
|
|
279
|
+
media.removeEventListener(event, this.handlePlaybackUpdate);
|
|
280
|
+
}
|
|
281
|
+
this.media = undefined;
|
|
282
|
+
}
|
|
283
|
+
/** Cleans up and releases all resources, and unregisters all event handlers. */
|
|
284
|
+
destroy() {
|
|
285
|
+
this.core.destroy();
|
|
286
|
+
this.unregisterMediaElement();
|
|
287
|
+
if (this.player) {
|
|
288
|
+
this.player.off(STREAM_INITIALIZED, this.handleStreamInitialized);
|
|
289
|
+
this.player.off(STREAM_TEARDOWN_COMPLETE, this.handleStreamTeardown);
|
|
290
|
+
}
|
|
291
|
+
this.player = undefined;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
//# sourceMappingURL=engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.js","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EACL,IAAI,EAMJ,KAAK,EACL,gCAAgC,EAChC,YAAY,GAEb,MA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/lib/index.js
ADDED
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC"}
|
package/lib/loader.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Core, ProcessedManifest } from "p2p-media-loader-core";
|
|
2
|
+
import { FactoryMakerThis, XhrLoaderLike } from "./types.js";
|
|
3
|
+
export type LoaderHooks = {
|
|
4
|
+
/** Called with what the core read from each MPD, before dash.js parses it. */
|
|
5
|
+
onManifestProcessed?: (manifest: ProcessedManifest) => void;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* The `XHRLoader` replacement, in the shape dash.js's `FactoryMaker` applies
|
|
9
|
+
* it: a plain function it calls with `this.parent` set to the real loader,
|
|
10
|
+
* whose `load` and `abort` the returned object overrides. Registered per
|
|
11
|
+
* player through `player.extend("XHRLoader", extension, true)`.
|
|
12
|
+
*
|
|
13
|
+
* Every request dash.js makes over HTTP passes through here — MPD, init,
|
|
14
|
+
* media and index segments, licences — and is told apart by the type of the
|
|
15
|
+
* `FragmentRequest` behind it. See specs/player-adapters.md, "dash.js".
|
|
16
|
+
*/
|
|
17
|
+
export declare function createXhrLoaderExtension(core: Core, hooks?: LoaderHooks): (this: FactoryMakerThis) => XhrLoaderLike;
|
package/lib/loader.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { CoreRequestError, byteRangeFromRangeHeader, debug, } from "p2p-media-loader-core";
|
|
2
|
+
import { REQUEST_TYPE, } from "./types.js";
|
|
3
|
+
const SEGMENT_TYPES = new Set([
|
|
4
|
+
REQUEST_TYPE.INIT_SEGMENT,
|
|
5
|
+
REQUEST_TYPE.MEDIA_SEGMENT,
|
|
6
|
+
REQUEST_TYPE.INDEX_SEGMENT,
|
|
7
|
+
]);
|
|
8
|
+
/**
|
|
9
|
+
* The `XHRLoader` replacement, in the shape dash.js's `FactoryMaker` applies
|
|
10
|
+
* it: a plain function it calls with `this.parent` set to the real loader,
|
|
11
|
+
* whose `load` and `abort` the returned object overrides. Registered per
|
|
12
|
+
* player through `player.extend("XHRLoader", extension, true)`.
|
|
13
|
+
*
|
|
14
|
+
* Every request dash.js makes over HTTP passes through here — MPD, init,
|
|
15
|
+
* media and index segments, licences — and is told apart by the type of the
|
|
16
|
+
* `FragmentRequest` behind it. See specs/player-adapters.md, "dash.js".
|
|
17
|
+
*/
|
|
18
|
+
export function createXhrLoaderExtension(core, hooks = {}) {
|
|
19
|
+
return function xhrLoaderExtension() {
|
|
20
|
+
// FactoryMaker copies the returned `load` and `abort` onto the parent
|
|
21
|
+
// instance itself, so `this.parent.load` would point back here once the
|
|
22
|
+
// override is applied. Keep dash.js's own methods before that happens.
|
|
23
|
+
const { parent } = this;
|
|
24
|
+
const original = {
|
|
25
|
+
load: parent.load.bind(parent),
|
|
26
|
+
abort: parent.abort.bind(parent),
|
|
27
|
+
};
|
|
28
|
+
const router = new RequestRouter(core, original, hooks);
|
|
29
|
+
return {
|
|
30
|
+
load: (request, response) => router.load(request, response),
|
|
31
|
+
abort: () => router.abort(),
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
class RequestRouter {
|
|
36
|
+
constructor(core, parent, hooks) {
|
|
37
|
+
Object.defineProperty(this, "core", {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
configurable: true,
|
|
40
|
+
writable: true,
|
|
41
|
+
value: core
|
|
42
|
+
});
|
|
43
|
+
Object.defineProperty(this, "parent", {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
configurable: true,
|
|
46
|
+
writable: true,
|
|
47
|
+
value: parent
|
|
48
|
+
});
|
|
49
|
+
Object.defineProperty(this, "hooks", {
|
|
50
|
+
enumerable: true,
|
|
51
|
+
configurable: true,
|
|
52
|
+
writable: true,
|
|
53
|
+
value: hooks
|
|
54
|
+
});
|
|
55
|
+
Object.defineProperty(this, "logger", {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
configurable: true,
|
|
58
|
+
writable: true,
|
|
59
|
+
value: debug("p2pml-dashjs:loader")
|
|
60
|
+
});
|
|
61
|
+
/** Aborts the request served by the core most recently, if still in flight. */
|
|
62
|
+
Object.defineProperty(this, "abortCurrent", {
|
|
63
|
+
enumerable: true,
|
|
64
|
+
configurable: true,
|
|
65
|
+
writable: true,
|
|
66
|
+
value: void 0
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
load(request, response) {
|
|
70
|
+
var _a, _b, _c, _d, _e, _f;
|
|
71
|
+
const type = (_c = (_b = (_a = request.customData) === null || _a === void 0 ? void 0 : _a.request) === null || _b === void 0 ? void 0 : _b.type) !== null && _c !== void 0 ? _c : undefined;
|
|
72
|
+
const { url } = request;
|
|
73
|
+
const byteRange = byteRangeFromRangeHeader((_e = (_d = request.headers) === null || _d === void 0 ? void 0 : _d.Range) !== null && _e !== void 0 ? _e : (_f = request.headers) === null || _f === void 0 ? void 0 : _f.range);
|
|
74
|
+
// Every MPD dash.js fetches — first load and each refresh — reaches the
|
|
75
|
+
// core before dash.js parses the same bytes.
|
|
76
|
+
if (type === REQUEST_TYPE.MPD) {
|
|
77
|
+
return this.passThroughAndObserve(request, response, (data) => {
|
|
78
|
+
var _a, _b;
|
|
79
|
+
if (typeof data !== "string" && !isBinary(data))
|
|
80
|
+
return;
|
|
81
|
+
// The response URL, when the loader filled it in, follows redirects.
|
|
82
|
+
const responseUrl = response.url;
|
|
83
|
+
const processed = this.core.processManifest({
|
|
84
|
+
url: responseUrl !== undefined && responseUrl !== "" ? responseUrl : url,
|
|
85
|
+
data,
|
|
86
|
+
});
|
|
87
|
+
if (processed)
|
|
88
|
+
(_b = (_a = this.hooks).onManifestProcessed) === null || _b === void 0 ? void 0 : _b.call(_a, processed);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
if (type !== undefined && SEGMENT_TYPES.has(type)) {
|
|
92
|
+
// A SegmentBase stream's index: dash.js loads it, the core reads the
|
|
93
|
+
// segment list from the same bytes. Recognition, not lookup.
|
|
94
|
+
if (this.core.isSegmentIndex(url, byteRange)) {
|
|
95
|
+
return this.passThroughAndObserve(request, response, (data) => {
|
|
96
|
+
if (!isBinary(data))
|
|
97
|
+
return;
|
|
98
|
+
this.core.processSegmentIndex({ url, byteRange, data });
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
// Whitelist by lookup: a segment the registry knows on a stream with
|
|
102
|
+
// P2P enabled is served by the core; initialization segments and
|
|
103
|
+
// anything the registry does not know load through dash.js itself.
|
|
104
|
+
if (type === REQUEST_TYPE.MEDIA_SEGMENT &&
|
|
105
|
+
this.core.isSegmentLoadable(url, byteRange)) {
|
|
106
|
+
this.serve(request, response, byteRange);
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
// Licences, certificates, steering, XLink, init segments: untouched.
|
|
111
|
+
return this.parent.load(request, response);
|
|
112
|
+
}
|
|
113
|
+
abort() {
|
|
114
|
+
var _a;
|
|
115
|
+
(_a = this.abortCurrent) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
116
|
+
this.parent.abort();
|
|
117
|
+
}
|
|
118
|
+
passThroughAndObserve(request, response, observe) {
|
|
119
|
+
var _a;
|
|
120
|
+
(_a = request.customData) !== null && _a !== void 0 ? _a : (request.customData = {});
|
|
121
|
+
const { customData } = request;
|
|
122
|
+
const original = customData.onloadend;
|
|
123
|
+
// dash.js's XHRLoader copies `onloadend` onto the XHR at `load`, so the
|
|
124
|
+
// wrap has to be in place before the parent runs.
|
|
125
|
+
customData.onloadend = () => {
|
|
126
|
+
try {
|
|
127
|
+
if (response.status >= 200 && response.status <= 299) {
|
|
128
|
+
observe(response.data);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
this.logger("failed to process %s: %O", request.url, error);
|
|
133
|
+
}
|
|
134
|
+
original === null || original === void 0 ? void 0 : original();
|
|
135
|
+
};
|
|
136
|
+
return this.parent.load(request, response);
|
|
137
|
+
}
|
|
138
|
+
serve(request, response, byteRange) {
|
|
139
|
+
var _a;
|
|
140
|
+
const { url } = request;
|
|
141
|
+
(_a = request.customData) !== null && _a !== void 0 ? _a : (request.customData = {});
|
|
142
|
+
const { customData } = request;
|
|
143
|
+
let settled = false;
|
|
144
|
+
const abort = () => {
|
|
145
|
+
if (settled)
|
|
146
|
+
return;
|
|
147
|
+
this.core.abortSegmentLoading(url, byteRange);
|
|
148
|
+
};
|
|
149
|
+
customData.abort = abort;
|
|
150
|
+
this.abortCurrent = abort;
|
|
151
|
+
this.core
|
|
152
|
+
.loadSegment(url, { byteRange })
|
|
153
|
+
.then(({ data, bandwidth }) => {
|
|
154
|
+
var _a, _b, _c;
|
|
155
|
+
settled = true;
|
|
156
|
+
response.url = url;
|
|
157
|
+
response.status = 200;
|
|
158
|
+
response.statusText = "OK";
|
|
159
|
+
response.headers = {};
|
|
160
|
+
response.data = data;
|
|
161
|
+
// dash.js measures throughput from progress traces, dropping the
|
|
162
|
+
// first (it carries the latency) and summing the rest. Two events
|
|
163
|
+
// give it one download trace whose duration is the core's bandwidth
|
|
164
|
+
// hint — wall-clock time says nothing about the network for a segment
|
|
165
|
+
// that came from a peer or from storage. Never 0 ms.
|
|
166
|
+
const total = data.byteLength;
|
|
167
|
+
(_a = customData.onprogress) === null || _a === void 0 ? void 0 : _a.call(customData, {
|
|
168
|
+
lengthComputable: true,
|
|
169
|
+
loaded: 0,
|
|
170
|
+
total,
|
|
171
|
+
time: 1,
|
|
172
|
+
});
|
|
173
|
+
(_b = customData.onprogress) === null || _b === void 0 ? void 0 : _b.call(customData, {
|
|
174
|
+
lengthComputable: true,
|
|
175
|
+
loaded: total,
|
|
176
|
+
total,
|
|
177
|
+
time: loadingTimeMs(bandwidth, total),
|
|
178
|
+
});
|
|
179
|
+
(_c = customData.onloadend) === null || _c === void 0 ? void 0 : _c.call(customData);
|
|
180
|
+
})
|
|
181
|
+
.catch((error) => {
|
|
182
|
+
var _a, _b;
|
|
183
|
+
settled = true;
|
|
184
|
+
if (error instanceof CoreRequestError && error.type === "aborted") {
|
|
185
|
+
(_a = customData.onabort) === null || _a === void 0 ? void 0 : _a.call(customData);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
// A non-2xx status is what dash.js's HTTPLoader retries on; the
|
|
189
|
+
// failure is reported as one rather than thrown into dash.js.
|
|
190
|
+
response.url = url;
|
|
191
|
+
response.status = 0;
|
|
192
|
+
response.statusText =
|
|
193
|
+
error instanceof Error ? error.message : String(error);
|
|
194
|
+
response.data = undefined;
|
|
195
|
+
this.logger("core failed to load %s: %O", url, error);
|
|
196
|
+
(_b = customData.onloadend) === null || _b === void 0 ? void 0 : _b.call(customData);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
function isBinary(data) {
|
|
201
|
+
return data instanceof ArrayBuffer || ArrayBuffer.isView(data);
|
|
202
|
+
}
|
|
203
|
+
/** Download time in ms for the core's bandwidth hint; see the Shaka adapter. */
|
|
204
|
+
function loadingTimeMs(bandwidth, bytes) {
|
|
205
|
+
if (bandwidth <= 0)
|
|
206
|
+
return 1;
|
|
207
|
+
return Math.max(1, Math.round((bytes * 8 * 1000) / bandwidth));
|
|
208
|
+
}
|
|
209
|
+
//# sourceMappingURL=loader.js.map
|