moqtail 0.16.0 → 0.17.0
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 +3 -3
- package/dist/{client-DUzLw_Y8.d.cts → client-CjdF7_cL.d.cts} +0 -4
- package/dist/{client-DQ5aGbLR.cjs → client-FnSNJocV.cjs} +20 -125
- package/dist/{client-epHqMdHm.d.mts → client-I8mDg-Ls.d.mts} +1 -5
- package/dist/{client-DVi4Nh1J.mjs → client-MXn-lkAA.mjs} +21 -108
- package/dist/client.cjs +82 -4
- package/dist/client.d.cts +1 -1
- package/dist/client.d.mts +2 -2
- package/dist/client.mjs +79 -1
- package/dist/{index-CNn9BgO6.d.mts → index-BaZgHZ8D.d.mts} +5 -0
- package/dist/{index-BtemTtwe.d.cts → index-Bn8VlVoN.d.cts} +182 -11
- package/dist/{index-B-BMK-b6.d.mts → index-Ch5QUygw.d.mts} +183 -12
- package/dist/index.cjs +30 -30
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +3 -3
- package/dist/model.cjs +277 -251
- package/dist/model.d.cts +5 -0
- package/dist/model.d.mts +1 -1
- package/dist/model.mjs +277 -251
- package/dist/player/component.cjs +53 -6
- package/dist/player/component.d.cts +12 -3
- package/dist/player/component.d.mts +12 -3
- package/dist/player/component.mjs +53 -6
- package/dist/{player-CJayEzwt.cjs → player-C6y-qpHz.cjs} +1808 -70
- package/dist/{player-DcctWhyM.mjs → player-DfJB6vBH.mjs} +1758 -64
- package/dist/player.cjs +6 -2
- package/dist/player.d.cts +2 -2
- package/dist/player.d.mts +2 -2
- package/dist/player.mjs +1 -1
- package/package.json +2 -1
- package/dist/rolldown-runtime-CmQyd0Gu.cjs +0 -27
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { AudioConfig, FillParameters, FullTrackName, KeyValuePair, Location, LocationFilter, MoqtObject, MoqtUrl, RequestError, SetupOptions, SstsAlgorithms, SwitchingSetAssignment, Tuple, TupleField, VideoConfig, findProperty } from "./model.mjs";
|
|
1
|
+
import { AudioConfig, FillParameters, FrozenByteBuffer, FullTrackName, KeyValuePair, Location, LocationFilter, MoqtObject, MoqtUrl, RequestError, SetupOptions, SstsAlgorithms, SwitchingSetAssignment, Tuple, TupleField, VideoConfig, findProperty } from "./model.mjs";
|
|
2
2
|
import { i as logger, t as Emitter } from "./emitter-UbeRcbuf.mjs";
|
|
3
|
-
import {
|
|
3
|
+
import { E as errorMessage, t as MOQtailClient } from "./client-MXn-lkAA.mjs";
|
|
4
4
|
import * as v from "valibot";
|
|
5
5
|
// src/player/error.ts
|
|
6
6
|
const CATEGORY = {
|
|
@@ -86,8 +86,8 @@ function parseMsfUrl(uri) {
|
|
|
86
86
|
case "wallclock-range":
|
|
87
87
|
source.wallclockRanges.push(parseTimeRange(value));
|
|
88
88
|
break;
|
|
89
|
-
case "
|
|
90
|
-
source.
|
|
89
|
+
case "cat":
|
|
90
|
+
source.cat = value;
|
|
91
91
|
break;
|
|
92
92
|
case "connection":
|
|
93
93
|
if (value !== "q" && value !== "wt") unsupported(`connection must be q or wt, got ${value}`);
|
|
@@ -1652,7 +1652,7 @@ const hasBom = (bytes) => bytes[0] === 239 && bytes[1] === 187 && bytes[2] === 1
|
|
|
1652
1652
|
* (MSF 5.2.39 to 5.2.42, CMSF 5.1) stay untyped extra fields.
|
|
1653
1653
|
*/
|
|
1654
1654
|
/** The `version` values this player parses, with the editor's draft rules. */
|
|
1655
|
-
const SUPPORTED_MSF_VERSIONS = ["draft-01"];
|
|
1655
|
+
const SUPPORTED_MSF_VERSIONS = ["draft-01", "draft-02"];
|
|
1656
1656
|
const int = v.pipe(v.number(), v.integer());
|
|
1657
1657
|
const location = v.tuple([int, int]);
|
|
1658
1658
|
/** MSF 5.2.5: `key=value;key=value`, the first occurrence of a key wins. */
|
|
@@ -1973,7 +1973,7 @@ function checkNames(tracks) {
|
|
|
1973
1973
|
* Applies a delta update object to `base`, which is left as it is.
|
|
1974
1974
|
* @throws PlayerError `INVALID_CATALOG_UPDATE` when the delta, or the catalog it leads to, is invalid.
|
|
1975
1975
|
*/
|
|
1976
|
-
function applyDelta(base, bytes, ctx) {
|
|
1976
|
+
function applyDelta$1(base, bytes, ctx) {
|
|
1977
1977
|
try {
|
|
1978
1978
|
const warnings = [];
|
|
1979
1979
|
const delta = v.safeParse(DeltaSchema, readJson(bytes, ctx, warnings));
|
|
@@ -2062,7 +2062,7 @@ const IGNORED = /* @__PURE__ */ new Set([
|
|
|
2062
2062
|
"namespace",
|
|
2063
2063
|
"template"
|
|
2064
2064
|
]);
|
|
2065
|
-
const KNOWN = /* @__PURE__ */ new Set([...TRACK_FIELD_NAMES, "init"]);
|
|
2065
|
+
const KNOWN$1 = /* @__PURE__ */ new Set([...TRACK_FIELD_NAMES, "init"]);
|
|
2066
2066
|
function diffTracks(before, after) {
|
|
2067
2067
|
const previous = new Map(before.map((t) => [trackKey(t), t]));
|
|
2068
2068
|
const diff = {
|
|
@@ -2092,7 +2092,7 @@ function diffTracks(before, after) {
|
|
|
2092
2092
|
}
|
|
2093
2093
|
const changed = [.../* @__PURE__ */ new Set([...Object.keys(old), ...Object.keys(track)])].filter((field) => !IGNORED.has(field) && !deepEqual(old[field], track[field]));
|
|
2094
2094
|
const inPlace = (field) => {
|
|
2095
|
-
if (!KNOWN.has(field)) return true;
|
|
2095
|
+
if (!KNOWN$1.has(field)) return true;
|
|
2096
2096
|
return IN_PLACE.has(field) && (field !== "isLive" || old.isLive);
|
|
2097
2097
|
};
|
|
2098
2098
|
if (changed.every(inPlace)) {
|
|
@@ -2106,6 +2106,25 @@ function diffTracks(before, after) {
|
|
|
2106
2106
|
return diff;
|
|
2107
2107
|
}
|
|
2108
2108
|
// src/player/elements/catalog/selection.ts
|
|
2109
|
+
/**
|
|
2110
|
+
* Copyright 2026 The MOQtail Authors
|
|
2111
|
+
*
|
|
2112
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2113
|
+
* you may not use this file except in compliance with the License.
|
|
2114
|
+
* You may obtain a copy of the License at
|
|
2115
|
+
*
|
|
2116
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2117
|
+
*
|
|
2118
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
2119
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2120
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2121
|
+
* See the License for the specific language governing permissions and
|
|
2122
|
+
* limitations under the License.
|
|
2123
|
+
*/
|
|
2124
|
+
/**
|
|
2125
|
+
* Default source selection: one video and one audio switching
|
|
2126
|
+
* set from the first render group, plus the event timelines the player opted into.
|
|
2127
|
+
*/
|
|
2109
2128
|
/** A track with its `role` inferred when the catalog leaves it out: MIME type, then video or audio fields. */
|
|
2110
2129
|
function withRole(track) {
|
|
2111
2130
|
if (track.role !== void 0) return track;
|
|
@@ -2168,9 +2187,20 @@ function startFirst(tracks) {
|
|
|
2168
2187
|
const start = spaced.reduce((a, b) => b.keyframeSpacing < a.keyframeSpacing ? b : a);
|
|
2169
2188
|
return [start, ...tracks.filter((t) => t !== start)];
|
|
2170
2189
|
}
|
|
2171
|
-
/**
|
|
2190
|
+
/**
|
|
2191
|
+
* The default video and audio sets, in that order, with their start tracks first. Of the sets
|
|
2192
|
+
* sharing an `altGroup` and `lang`, only those whose definition has the lowest priority are kept.
|
|
2193
|
+
*/
|
|
2172
2194
|
function selectMedia(tracks, options) {
|
|
2173
|
-
const
|
|
2195
|
+
const all = mediaSets(tracks, options.probe);
|
|
2196
|
+
const key = (s) => s.tracks[0].altGroup === void 0 ? void 0 : setKey(s.tracks[0], 0);
|
|
2197
|
+
const rank = (s) => s.definition.priority ?? 100;
|
|
2198
|
+
const best = /* @__PURE__ */ new Map();
|
|
2199
|
+
for (const s of all) {
|
|
2200
|
+
const k = key(s);
|
|
2201
|
+
if (k !== void 0) best.set(k, Math.min(best.get(k) ?? Infinity, rank(s)));
|
|
2202
|
+
}
|
|
2203
|
+
const sets = all.filter((s) => key(s) === void 0 || rank(s) === best.get(key(s)));
|
|
2174
2204
|
return [sets.find((s) => s.tracks[0].role === "video"), pickAudio(sets.filter((s) => s.tracks[0].role === "audio"), options.language)].filter((s) => s !== void 0).map((s) => ({
|
|
2175
2205
|
...s,
|
|
2176
2206
|
tracks: startFirst(s.tracks)
|
|
@@ -2256,7 +2286,7 @@ var CatalogElement = class extends SourceElement {
|
|
|
2256
2286
|
onIndependent: (payload) => this.#received(parseCatalog(payload, this.#parse)),
|
|
2257
2287
|
onIncremental: (payload) => {
|
|
2258
2288
|
if (!this.#catalog) throw new Error("A delta update arrived before any catalog");
|
|
2259
|
-
this.#received(applyDelta(this.#catalog, payload, this.#parse));
|
|
2289
|
+
this.#received(applyDelta$1(this.#catalog, payload, this.#parse));
|
|
2260
2290
|
},
|
|
2261
2291
|
onStale: (reason) => this.#onStale(reason)
|
|
2262
2292
|
});
|
|
@@ -2697,7 +2727,7 @@ function accessibleStart(track, timeline) {
|
|
|
2697
2727
|
return timeline.timeToLocation(start);
|
|
2698
2728
|
}
|
|
2699
2729
|
/** The span `location` falls in, or the nearest one, and the location clamped into it. */
|
|
2700
|
-
function clamp(location, spans) {
|
|
2730
|
+
function clamp$1(location, spans) {
|
|
2701
2731
|
let best = {
|
|
2702
2732
|
index: 0,
|
|
2703
2733
|
location: spans[0].start,
|
|
@@ -3008,12 +3038,14 @@ var Delivery = class {
|
|
|
3008
3038
|
#trackProperties(track, initData) {
|
|
3009
3039
|
const duration = this.duration ?? (!track.isLive && track.trackDuration !== void 0 ? track.trackDuration / 1e3 : void 0);
|
|
3010
3040
|
const latency = track.targetLatency ?? track.buffers?.target;
|
|
3041
|
+
const window = track.isLive ? track.accessibleWindowTime : void 0;
|
|
3011
3042
|
return {
|
|
3012
3043
|
track: renditionId(track, this.#context.catalog),
|
|
3013
3044
|
mimeType: this.#packaging.mimeType(track),
|
|
3014
3045
|
live: track.isLive,
|
|
3015
3046
|
...duration !== void 0 && { duration },
|
|
3016
3047
|
...latency !== void 0 && { targetLatency: latency / 1e3 },
|
|
3048
|
+
...window !== void 0 && { accessibleWindow: window / 1e3 },
|
|
3017
3049
|
...this.#groupProperties(track),
|
|
3018
3050
|
...initData && { initData }
|
|
3019
3051
|
};
|
|
@@ -3136,6 +3168,16 @@ var Delivery = class {
|
|
|
3136
3168
|
const key = trackKey(track);
|
|
3137
3169
|
const initData = this.#packaging.objectInitData(track, object);
|
|
3138
3170
|
if (!initData && this.#packaging.initOnObjects(track) && this.#initFor !== key) return;
|
|
3171
|
+
const packet = this.#packaging.packet(track, object, initData ?? this.#properties?.initData);
|
|
3172
|
+
if (!packet) {
|
|
3173
|
+
const resume = new Location(object.location.group + 1n, 0n);
|
|
3174
|
+
this.#gaps.get(this.#current.accepted.requestId)?.push({
|
|
3175
|
+
from: object.location,
|
|
3176
|
+
resume
|
|
3177
|
+
});
|
|
3178
|
+
this.gap = true;
|
|
3179
|
+
return;
|
|
3180
|
+
}
|
|
3139
3181
|
if (this.seeks > 0) {
|
|
3140
3182
|
this.gap = false;
|
|
3141
3183
|
await this.#answerSeeks("ok");
|
|
@@ -3155,7 +3197,7 @@ var Delivery = class {
|
|
|
3155
3197
|
}
|
|
3156
3198
|
await this.pad.write({
|
|
3157
3199
|
type: "packet",
|
|
3158
|
-
object
|
|
3200
|
+
object: packet
|
|
3159
3201
|
});
|
|
3160
3202
|
const previous = this.#last;
|
|
3161
3203
|
this.#last = {
|
|
@@ -3164,7 +3206,7 @@ var Delivery = class {
|
|
|
3164
3206
|
};
|
|
3165
3207
|
this.delivered = true;
|
|
3166
3208
|
if (previous?.location.group !== object.location.group || previous.track !== key) {
|
|
3167
|
-
this.#recordTime(track,
|
|
3209
|
+
this.#recordTime(track, packet);
|
|
3168
3210
|
await this.#setProperties(this.#groupProperties(track));
|
|
3169
3211
|
}
|
|
3170
3212
|
const changed = previous !== void 0 && previous.track !== key;
|
|
@@ -3583,7 +3625,7 @@ var Recovery = class {
|
|
|
3583
3625
|
if (!location) return this.#delivery.failSeeks();
|
|
3584
3626
|
let fetch = !track.isLive;
|
|
3585
3627
|
if (this.#spans.length > 0) {
|
|
3586
|
-
const clamped = clamp(mapLocation(location, track, this.#first, set), this.#spans);
|
|
3628
|
+
const clamped = clamp$1(mapLocation(location, track, this.#first, set), this.#spans);
|
|
3587
3629
|
this.#span = clamped.index;
|
|
3588
3630
|
location = mapLocation(clamped.location, this.#first, track, set);
|
|
3589
3631
|
fetch = this.#spans[clamped.index].fetch;
|
|
@@ -3958,9 +4000,9 @@ var SwitchingSetSource = class extends SourceElement {
|
|
|
3958
4000
|
constructor(context, options, plan = { spans: [] }) {
|
|
3959
4001
|
super(context);
|
|
3960
4002
|
this.options = options;
|
|
3961
|
-
const start = context.tracks[0];
|
|
3962
4003
|
this.#tracks = context.tracks;
|
|
3963
4004
|
this.#selected = new Set(context.tracks.map((t) => this.#id(t)));
|
|
4005
|
+
const start = this.#start();
|
|
3964
4006
|
this.pad = this.createPad("src", this.padCapabilities(start.role ?? "video"));
|
|
3965
4007
|
this.#delivery = new Delivery(this.pad, context, options.requests, this);
|
|
3966
4008
|
this.#delivery.duration = plan.duration;
|
|
@@ -4050,6 +4092,18 @@ var SwitchingSetSource = class extends SourceElement {
|
|
|
4050
4092
|
this.#shown = shown;
|
|
4051
4093
|
this.#switcher.upgrade();
|
|
4052
4094
|
}
|
|
4095
|
+
/**
|
|
4096
|
+
* A live set starts at its middle bitrate, so ABR can move either way before it knows the link;
|
|
4097
|
+
* VOD, which gets no ABR controller, at its first track.
|
|
4098
|
+
*/
|
|
4099
|
+
#start() {
|
|
4100
|
+
const first = this.#tracks[0];
|
|
4101
|
+
if (!first.isLive) return first;
|
|
4102
|
+
const heads = this.#tracks.filter((t) => this.#head(t) === t).sort((a, b) => bitrateOf(a) - bitrateOf(b));
|
|
4103
|
+
if (!heads.every((t) => bitrateOf(t) > 0)) return first;
|
|
4104
|
+
const middle = heads[heads.length - 1 >> 1];
|
|
4105
|
+
return ladderOf(middle, this.#tracks).reduce((a, b) => (b.keyframeSpacing ?? Infinity) < (a.keyframeSpacing ?? Infinity) ? b : a, middle);
|
|
4106
|
+
}
|
|
4053
4107
|
/** The track `track`'s rendition stands for: the longest GoP of its ladder, which switches climb to. */
|
|
4054
4108
|
#head(track) {
|
|
4055
4109
|
return ladderOf(track, this.#tracks).reduce((a, b) => (b.keyframeSpacing ?? 0) > (a.keyframeSpacing ?? 0) ? b : a, track);
|
|
@@ -4059,10 +4113,13 @@ var SwitchingSetSource = class extends SourceElement {
|
|
|
4059
4113
|
}
|
|
4060
4114
|
/** `track` is the head of its ladder. */
|
|
4061
4115
|
#rendition(track) {
|
|
4062
|
-
return
|
|
4063
|
-
|
|
4064
|
-
|
|
4065
|
-
|
|
4116
|
+
return {
|
|
4117
|
+
...rendition(track, this.context.catalog, {
|
|
4118
|
+
active: trackKey(track) === trackKey(this.#head(this.#recovery.active)),
|
|
4119
|
+
selected: this.#selected.has(this.#id(track))
|
|
4120
|
+
}),
|
|
4121
|
+
ladder: ladderOf(track, this.#tracks).map((t) => t.name)
|
|
4122
|
+
};
|
|
4066
4123
|
}
|
|
4067
4124
|
#id(track) {
|
|
4068
4125
|
return renditionId(track, this.context.catalog);
|
|
@@ -4106,29 +4163,31 @@ async function planSource(context, options, element) {
|
|
|
4106
4163
|
*/
|
|
4107
4164
|
/** The payload of the first box at `path`, each name nested in the previous one. */
|
|
4108
4165
|
function findBox(data, path) {
|
|
4109
|
-
|
|
4110
|
-
let [start, end] = [0, data.byteLength];
|
|
4166
|
+
let found = data;
|
|
4111
4167
|
for (const name of path) {
|
|
4112
|
-
|
|
4113
|
-
while (start + 8 <= end) {
|
|
4114
|
-
let size = view.getUint32(start);
|
|
4115
|
-
let header = 8;
|
|
4116
|
-
if (size === 1) {
|
|
4117
|
-
if (start + 16 > end) return void 0;
|
|
4118
|
-
size = Number(view.getBigUint64(start + 8));
|
|
4119
|
-
header = 16;
|
|
4120
|
-
} else if (size === 0) size = end - start;
|
|
4121
|
-
if (size < header || start + size > end) return void 0;
|
|
4122
|
-
if (String.fromCharCode(...data.subarray(start + 4, start + 8)) === name) {
|
|
4123
|
-
[start, end] = [start + header, start + size];
|
|
4124
|
-
found = true;
|
|
4125
|
-
break;
|
|
4126
|
-
}
|
|
4127
|
-
start += size;
|
|
4128
|
-
}
|
|
4168
|
+
found = [...children(found)].find((b) => b.type === name)?.body;
|
|
4129
4169
|
if (!found) return void 0;
|
|
4130
4170
|
}
|
|
4131
|
-
return
|
|
4171
|
+
return found;
|
|
4172
|
+
}
|
|
4173
|
+
/** The boxes at the top level of `data`, up to the first malformed one. */
|
|
4174
|
+
function* children(data) {
|
|
4175
|
+
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
4176
|
+
for (let start = 0; start + 8 <= data.byteLength;) {
|
|
4177
|
+
let size = view.getUint32(start);
|
|
4178
|
+
let header = 8;
|
|
4179
|
+
if (size === 1) {
|
|
4180
|
+
if (start + 16 > data.byteLength) return;
|
|
4181
|
+
size = Number(view.getBigUint64(start + 8));
|
|
4182
|
+
header = 16;
|
|
4183
|
+
} else if (size === 0) size = data.byteLength - start;
|
|
4184
|
+
if (size < header || start + size > data.byteLength) return;
|
|
4185
|
+
yield {
|
|
4186
|
+
type: String.fromCharCode(...data.subarray(start + 4, start + 8)),
|
|
4187
|
+
body: data.subarray(start + header, start + size)
|
|
4188
|
+
};
|
|
4189
|
+
start += size;
|
|
4190
|
+
}
|
|
4132
4191
|
}
|
|
4133
4192
|
/** `moof/traf/tfdt` base media decode time, in timescale units. */
|
|
4134
4193
|
function baseMediaDecodeTime(fragment) {
|
|
@@ -4227,6 +4286,9 @@ var CmafSource = class extends SwitchingSetSource {
|
|
|
4227
4286
|
if (!object.payload) return void 0;
|
|
4228
4287
|
return fragmentTime(object.payload, track.timescale ?? (initData && mediaTimescale(initData)));
|
|
4229
4288
|
}
|
|
4289
|
+
packet(_track, object, _initData) {
|
|
4290
|
+
return object;
|
|
4291
|
+
}
|
|
4230
4292
|
};
|
|
4231
4293
|
// src/player/elements/cmaf/index.ts
|
|
4232
4294
|
/**
|
|
@@ -4266,6 +4328,609 @@ function probeCmaf(identifiers) {
|
|
|
4266
4328
|
if (packaging !== "cmaf" || role !== "video" && role !== "audio") return false;
|
|
4267
4329
|
return identifiers["encryptionScheme"] === void 0 && identifiers["contentProtectionRefIDs"] === void 0;
|
|
4268
4330
|
}
|
|
4331
|
+
// src/player/elements/locmaf/object.ts
|
|
4332
|
+
/**
|
|
4333
|
+
* Copyright 2026 The MOQtail Authors
|
|
4334
|
+
*
|
|
4335
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4336
|
+
* you may not use this file except in compliance with the License.
|
|
4337
|
+
* You may obtain a copy of the License at
|
|
4338
|
+
*
|
|
4339
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
4340
|
+
*
|
|
4341
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
4342
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
4343
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
4344
|
+
* See the License for the specific language governing permissions and
|
|
4345
|
+
* limitations under the License.
|
|
4346
|
+
*/
|
|
4347
|
+
/** The framing of a LOCMAF Object (draft-einarsson-moq-locmaf-01 §7–9): its elements and header fields. */
|
|
4348
|
+
/** A malformed object, or one that can't be decoded where it sits in its group. */
|
|
4349
|
+
var LocmafError = class extends Error {};
|
|
4350
|
+
/** Header field IDs (§10, Table 4). */
|
|
4351
|
+
const Field = {
|
|
4352
|
+
sampleSizes: 1,
|
|
4353
|
+
sampleDescriptionIndex: 2,
|
|
4354
|
+
sampleDurations: 3,
|
|
4355
|
+
defaultSampleDuration: 4,
|
|
4356
|
+
compositionTimeOffsets: 5,
|
|
4357
|
+
defaultSampleSize: 6,
|
|
4358
|
+
sampleFlags: 7,
|
|
4359
|
+
defaultSampleFlags: 8,
|
|
4360
|
+
initializationVector: 9,
|
|
4361
|
+
baseMediaDecodeTime: 10,
|
|
4362
|
+
subsampleCount: 11,
|
|
4363
|
+
firstSampleFlags: 12,
|
|
4364
|
+
bytesOfClearData: 13,
|
|
4365
|
+
sampleCount: 14,
|
|
4366
|
+
bytesOfProtectedData: 15,
|
|
4367
|
+
perSampleIVSize: 16,
|
|
4368
|
+
deleted: 27
|
|
4369
|
+
};
|
|
4370
|
+
const KNOWN = new Set(Object.values(Field).map(BigInt));
|
|
4371
|
+
const GEN_BOX = 1n;
|
|
4372
|
+
const FULL_HEADER = 2n;
|
|
4373
|
+
const DELTA_HEADER = 3n;
|
|
4374
|
+
const RAW_BOXES = 4n;
|
|
4375
|
+
/** @throws LocmafError when the object is malformed. */
|
|
4376
|
+
function parseObject(payload) {
|
|
4377
|
+
const buf = new FrozenByteBuffer(payload);
|
|
4378
|
+
const genBoxes = [];
|
|
4379
|
+
try {
|
|
4380
|
+
for (;;) {
|
|
4381
|
+
if (buf.remaining === 0) throw new LocmafError("The object ends before its header");
|
|
4382
|
+
const type = buf.getVI();
|
|
4383
|
+
if (type === GEN_BOX) genBoxes.push(genBox(buf));
|
|
4384
|
+
else if (type === FULL_HEADER || type === DELTA_HEADER) {
|
|
4385
|
+
const full = type === FULL_HEADER;
|
|
4386
|
+
return {
|
|
4387
|
+
genBoxes,
|
|
4388
|
+
full,
|
|
4389
|
+
fields: properties(buf.getBytes(length(buf)), full),
|
|
4390
|
+
mdat: payload.subarray(buf.offset)
|
|
4391
|
+
};
|
|
4392
|
+
} else if (type === RAW_BOXES && genBoxes.length === 0) return { raw: rawBoxes(payload.subarray(buf.offset)) };
|
|
4393
|
+
else throw new LocmafError(`Element type ${type} isn't allowed here`);
|
|
4394
|
+
}
|
|
4395
|
+
} catch (error) {
|
|
4396
|
+
if (error instanceof LocmafError) throw error;
|
|
4397
|
+
throw new LocmafError("The object is truncated", { cause: error });
|
|
4398
|
+
}
|
|
4399
|
+
}
|
|
4400
|
+
/** The vi64s of a list field, in order (§7.3). */
|
|
4401
|
+
function readList(bytes) {
|
|
4402
|
+
const buf = new FrozenByteBuffer(bytes);
|
|
4403
|
+
const values = [];
|
|
4404
|
+
try {
|
|
4405
|
+
while (buf.remaining > 0) values.push(buf.getVI());
|
|
4406
|
+
} catch (error) {
|
|
4407
|
+
throw new LocmafError("A list field is truncated", { cause: error });
|
|
4408
|
+
}
|
|
4409
|
+
return values;
|
|
4410
|
+
}
|
|
4411
|
+
/** The signed value of a zigzag vi64 (§7.4). */
|
|
4412
|
+
function unzigzag(z) {
|
|
4413
|
+
return z & 1n ? -(z + 1n) / 2n : z / 2n;
|
|
4414
|
+
}
|
|
4415
|
+
/** `box_size | box_name | payload` as the ISO box `uint32(4 + box_size) | box_name | payload` (§8.1, §8.3). */
|
|
4416
|
+
function genBox(buf) {
|
|
4417
|
+
const size = buf.getVI();
|
|
4418
|
+
if (size < 4n || size > 4294967291n) throw new LocmafError(`genBox size ${size} is out of range`);
|
|
4419
|
+
const body = buf.getBytes(Number(size));
|
|
4420
|
+
const box = new Uint8Array(4 + body.length);
|
|
4421
|
+
new DataView(box.buffer).setUint32(0, box.length);
|
|
4422
|
+
box.set(body, 4);
|
|
4423
|
+
return box;
|
|
4424
|
+
}
|
|
4425
|
+
function length(buf) {
|
|
4426
|
+
const value = buf.getVI();
|
|
4427
|
+
if (value > BigInt(buf.remaining)) throw new LocmafError(`A length of ${value} runs past the object`);
|
|
4428
|
+
return Number(value);
|
|
4429
|
+
}
|
|
4430
|
+
/** The known fields of a property block; unknown ones are skipped by their parity (§7.3). */
|
|
4431
|
+
function properties(block, full) {
|
|
4432
|
+
const buf = new FrozenByteBuffer(block);
|
|
4433
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4434
|
+
const fields = /* @__PURE__ */ new Map();
|
|
4435
|
+
while (buf.remaining > 0) {
|
|
4436
|
+
const id = buf.getVI();
|
|
4437
|
+
if (seen.has(id)) throw new LocmafError(`Field ${id} repeats`);
|
|
4438
|
+
seen.add(id);
|
|
4439
|
+
const value = id % 2n === 0n ? buf.getVI() : buf.getBytes(length(buf));
|
|
4440
|
+
if (KNOWN.has(id)) fields.set(Number(id), value);
|
|
4441
|
+
}
|
|
4442
|
+
if (full && fields.has(Field.deleted)) throw new LocmafError("A full header lists deletions");
|
|
4443
|
+
if (!full && fields.has(Field.baseMediaDecodeTime)) throw new LocmafError("A delta header carries the BMDT");
|
|
4444
|
+
return fields;
|
|
4445
|
+
}
|
|
4446
|
+
/** Complete boxes, each with its actual 32-bit size, filling the rest of the object (§9.1, §18). */
|
|
4447
|
+
function rawBoxes(boxes) {
|
|
4448
|
+
if (boxes.length === 0) throw new LocmafError("A rawBoxes element is empty");
|
|
4449
|
+
const view = new DataView(boxes.buffer, boxes.byteOffset, boxes.byteLength);
|
|
4450
|
+
for (let at = 0; at < boxes.length;) {
|
|
4451
|
+
const size = at + 8 <= boxes.length ? view.getUint32(at) : 0;
|
|
4452
|
+
if (size < 8 || at + size > boxes.length) throw new LocmafError("rawBoxes holds an incomplete box");
|
|
4453
|
+
at += size;
|
|
4454
|
+
}
|
|
4455
|
+
return boxes;
|
|
4456
|
+
}
|
|
4457
|
+
// src/player/elements/locmaf/chunk.ts
|
|
4458
|
+
/** No real chunk comes close; it bounds what a header can make the receiver allocate (§18). */
|
|
4459
|
+
const MAX_SAMPLES = 1n << 16n;
|
|
4460
|
+
const SCALARS = [
|
|
4461
|
+
Field.sampleDescriptionIndex,
|
|
4462
|
+
Field.defaultSampleDuration,
|
|
4463
|
+
Field.defaultSampleSize,
|
|
4464
|
+
Field.defaultSampleFlags,
|
|
4465
|
+
Field.firstSampleFlags,
|
|
4466
|
+
Field.perSampleIVSize
|
|
4467
|
+
];
|
|
4468
|
+
const CENC = [
|
|
4469
|
+
Field.initializationVector,
|
|
4470
|
+
Field.subsampleCount,
|
|
4471
|
+
Field.bytesOfClearData,
|
|
4472
|
+
Field.bytesOfProtectedData,
|
|
4473
|
+
Field.perSampleIVSize
|
|
4474
|
+
];
|
|
4475
|
+
/** The in-group reference of one track: the previous chunk of its group, when it can be built on. */
|
|
4476
|
+
var Reference = class {
|
|
4477
|
+
#previous;
|
|
4478
|
+
/**
|
|
4479
|
+
* The effective values of the object at `location`, or the boxes of a rawBoxes object.
|
|
4480
|
+
* @throws LocmafError when it's malformed, or a delta with nothing to build on (§3, §9.3).
|
|
4481
|
+
*/
|
|
4482
|
+
decode(location, payload, defaults) {
|
|
4483
|
+
const previous = this.#previous;
|
|
4484
|
+
const reference = previous?.location.group === location.group && previous.location.object + 1n === location.object ? previous : void 0;
|
|
4485
|
+
this.#previous = { location };
|
|
4486
|
+
const object = parseObject(payload);
|
|
4487
|
+
if ("raw" in object) return object.raw;
|
|
4488
|
+
if (!object.full && !reference?.fields) throw new LocmafError("A delta header has no in-group reference");
|
|
4489
|
+
const fields = object.full ? absolute(object.fields) : applyDelta(object.fields, reference.fields);
|
|
4490
|
+
const baseMediaDecodeTime = object.full ? fields.get(Field.baseMediaDecodeTime) : reference.nextBaseMediaDecodeTime;
|
|
4491
|
+
if (baseMediaDecodeTime >= 2n ** 64n) throw new LocmafError("The decode time overflows 64 bits");
|
|
4492
|
+
const chunk = effectiveValues(fields, defaults, {
|
|
4493
|
+
...object,
|
|
4494
|
+
baseMediaDecodeTime
|
|
4495
|
+
});
|
|
4496
|
+
const next = baseMediaDecodeTime + chunk.durations.reduce((sum, d) => sum + BigInt(d), 0n);
|
|
4497
|
+
this.#previous = {
|
|
4498
|
+
location,
|
|
4499
|
+
fields,
|
|
4500
|
+
nextBaseMediaDecodeTime: next
|
|
4501
|
+
};
|
|
4502
|
+
return chunk;
|
|
4503
|
+
}
|
|
4504
|
+
};
|
|
4505
|
+
/** A full header's represented values (§11): absolute, except the zigzag ID 5. */
|
|
4506
|
+
function absolute(fields) {
|
|
4507
|
+
if (!fields.has(Field.sampleCount) || !fields.has(Field.baseMediaDecodeTime)) throw new LocmafError("A full header needs trunSampleCount and tfdtBaseMediaDecodeTime");
|
|
4508
|
+
const represented = /* @__PURE__ */ new Map();
|
|
4509
|
+
for (const [id, value] of fields) if (typeof value === "bigint" || id === Field.initializationVector) represented.set(id, value);
|
|
4510
|
+
else represented.set(id, id === Field.compositionTimeOffsets ? readList(value).map(unzigzag) : readList(value));
|
|
4511
|
+
return represented;
|
|
4512
|
+
}
|
|
4513
|
+
/** The reference with a delta header applied: deletions, then scalar and list deltas (§12). */
|
|
4514
|
+
function applyDelta(fields, reference) {
|
|
4515
|
+
const next = new Map(reference);
|
|
4516
|
+
const deleted = fields.get(Field.deleted);
|
|
4517
|
+
if (deleted instanceof Uint8Array) for (const id of readList(deleted)) next.delete(Number(id));
|
|
4518
|
+
for (const id of [Field.sampleCount, ...SCALARS]) {
|
|
4519
|
+
const delta = fields.get(id);
|
|
4520
|
+
if (delta === void 0) continue;
|
|
4521
|
+
const value = (next.get(id) ?? 0n) + unzigzag(delta);
|
|
4522
|
+
if (value < 0n) throw new LocmafError(`Field ${id} goes negative`);
|
|
4523
|
+
next.set(id, value);
|
|
4524
|
+
}
|
|
4525
|
+
const n = sampleCount(next);
|
|
4526
|
+
for (const id of [
|
|
4527
|
+
Field.sampleDurations,
|
|
4528
|
+
Field.sampleFlags,
|
|
4529
|
+
Field.subsampleCount
|
|
4530
|
+
]) applyList(next, fields, id, n);
|
|
4531
|
+
applyList(next, fields, Field.sampleSizes, n === 0 ? 0 : n - 1);
|
|
4532
|
+
const subsamples = (next.get(Field.subsampleCount) ?? []).reduce((a, b) => a + b, 0n);
|
|
4533
|
+
if (subsamples > MAX_SAMPLES) throw new LocmafError(`${subsamples} subsamples is too many`);
|
|
4534
|
+
applyList(next, fields, Field.bytesOfClearData, Number(subsamples));
|
|
4535
|
+
applyList(next, fields, Field.bytesOfProtectedData, Number(subsamples));
|
|
4536
|
+
applyList(next, fields, Field.compositionTimeOffsets, n);
|
|
4537
|
+
const ivs = fields.get(Field.initializationVector);
|
|
4538
|
+
if (ivs) next.set(Field.initializationVector, ivs);
|
|
4539
|
+
return next;
|
|
4540
|
+
}
|
|
4541
|
+
/**
|
|
4542
|
+
* Element-wise deltas against the previous list, absolute past its end (§12.1.1). An inherited
|
|
4543
|
+
* list follows a shorter count by truncation; one too short for a longer count is malformed.
|
|
4544
|
+
*/
|
|
4545
|
+
function applyList(next, fields, id, length) {
|
|
4546
|
+
const previous = next.get(id);
|
|
4547
|
+
const bytes = fields.get(id);
|
|
4548
|
+
if (!bytes) {
|
|
4549
|
+
if (previous && previous.length < length) throw new LocmafError(`Inherited field ${id} is too short`);
|
|
4550
|
+
if (previous) next.set(id, previous.slice(0, length));
|
|
4551
|
+
return;
|
|
4552
|
+
}
|
|
4553
|
+
const deltas = readList(bytes).map(unzigzag);
|
|
4554
|
+
if (deltas.length !== length) throw new LocmafError(`Field ${id} has ${deltas.length} entries, not ${length}`);
|
|
4555
|
+
const list = deltas.map((delta, i) => (previous?.[i] ?? 0n) + delta);
|
|
4556
|
+
if (id !== Field.compositionTimeOffsets && list.some((v) => v < 0n)) throw new LocmafError(`Field ${id} goes negative`);
|
|
4557
|
+
next.set(id, list);
|
|
4558
|
+
}
|
|
4559
|
+
/** The effective values from the represented fields, the CMAF Header and the object (§11.2, §15.1). */
|
|
4560
|
+
function effectiveValues(fields, defaults, object) {
|
|
4561
|
+
const n = sampleCount(fields);
|
|
4562
|
+
const scalar = (id, fallback) => {
|
|
4563
|
+
const value = fields.get(id);
|
|
4564
|
+
return value === void 0 ? fallback : uint(value, 32);
|
|
4565
|
+
};
|
|
4566
|
+
const list = (id, length) => {
|
|
4567
|
+
const values = fields.get(id);
|
|
4568
|
+
if (values && values.length !== length) throw new LocmafError(`Field ${id} has ${values.length} entries, not ${length}`);
|
|
4569
|
+
return values;
|
|
4570
|
+
};
|
|
4571
|
+
const durations = list(Field.sampleDurations, n)?.map((d) => uint(d, 32));
|
|
4572
|
+
const flags = list(Field.sampleFlags, n)?.map((f) => uint(f, 32));
|
|
4573
|
+
const firstFlags = fields.has(Field.firstSampleFlags) ? scalar(Field.firstSampleFlags, 0) : void 0;
|
|
4574
|
+
const defaultFlags = scalar(Field.defaultSampleFlags, defaults.sampleFlags);
|
|
4575
|
+
const ctos = list(Field.compositionTimeOffsets, n)?.map((c) => {
|
|
4576
|
+
if (c < -(2n ** 31n) || c >= 2n ** 31n) throw new LocmafError(`Composition time offset ${c} is out of range`);
|
|
4577
|
+
return Number(c);
|
|
4578
|
+
});
|
|
4579
|
+
const sizes = sampleSizes(fields, n, object.mdat.length, defaults);
|
|
4580
|
+
return {
|
|
4581
|
+
baseMediaDecodeTime: object.baseMediaDecodeTime,
|
|
4582
|
+
sampleDescriptionIndex: scalar(Field.sampleDescriptionIndex, defaults.sampleDescriptionIndex),
|
|
4583
|
+
durations: durations ?? Array(n).fill(scalar(Field.defaultSampleDuration, defaults.sampleDuration)),
|
|
4584
|
+
sizes,
|
|
4585
|
+
flags: flags ?? Array.from({ length: n }, (_, i) => i === 0 ? firstFlags ?? defaultFlags : defaultFlags),
|
|
4586
|
+
compositionTimeOffsets: ctos ?? Array(n).fill(0),
|
|
4587
|
+
...cenc(fields, defaults, sizes),
|
|
4588
|
+
genBoxes: object.genBoxes,
|
|
4589
|
+
mdat: object.mdat
|
|
4590
|
+
};
|
|
4591
|
+
}
|
|
4592
|
+
/** Sample sizes (§11.2): the listed n − 1 and the rest of the mdat, a default, or the mdat for one sample. */
|
|
4593
|
+
function sampleSizes(fields, n, payload, defaults) {
|
|
4594
|
+
if (n === 0 && payload > 0) throw new LocmafError("A chunk without samples has an mdat payload");
|
|
4595
|
+
if (payload > 4294967287) throw new LocmafError("The mdat payload is too large");
|
|
4596
|
+
const listed = fields.get(Field.sampleSizes);
|
|
4597
|
+
if (listed) {
|
|
4598
|
+
if (listed.length !== Math.max(n - 1, 0)) throw new LocmafError(`${listed.length} sample sizes for ${n} samples`);
|
|
4599
|
+
const sizes = listed.map((s) => uint(s, 32));
|
|
4600
|
+
const sum = sizes.reduce((a, b) => a + b, 0);
|
|
4601
|
+
if (sum > payload) throw new LocmafError("The sample sizes exceed the mdat payload");
|
|
4602
|
+
return n === 0 ? [] : [...sizes, payload - sum];
|
|
4603
|
+
}
|
|
4604
|
+
const tfhd = fields.get(Field.defaultSampleSize);
|
|
4605
|
+
const size = tfhd !== void 0 ? uint(tfhd, 32) : n !== 1 && defaults.sampleSize !== 0 ? defaults.sampleSize : void 0;
|
|
4606
|
+
if (size !== void 0) {
|
|
4607
|
+
if (n * size !== payload) throw new LocmafError(`${n} samples of ${size} bytes don't fill ${payload}`);
|
|
4608
|
+
return Array(n).fill(size);
|
|
4609
|
+
}
|
|
4610
|
+
if (n === 1) return [payload];
|
|
4611
|
+
if (payload === 0) return Array(n).fill(0);
|
|
4612
|
+
throw new LocmafError(`No sample sizes for ${n} samples`);
|
|
4613
|
+
}
|
|
4614
|
+
/** CENC auxiliary information (§10.3, §13, §18), checked against the samples it describes. */
|
|
4615
|
+
function cenc(fields, defaults, sizes) {
|
|
4616
|
+
if (!defaults.isProtected && CENC.some((id) => fields.has(id))) throw new LocmafError("CENC fields on a track that isn’t protected");
|
|
4617
|
+
const n = sizes.length;
|
|
4618
|
+
const ivSize = fields.has(Field.perSampleIVSize) ? uint(fields.get(Field.perSampleIVSize), 8) : defaults.perSampleIVSize;
|
|
4619
|
+
const ivs = fields.get(Field.initializationVector) ?? /* @__PURE__ */ new Uint8Array();
|
|
4620
|
+
if (ivs.length > 0 ? ivs.length !== ivSize * n : ivSize > 0 && n > 0) throw new LocmafError(`${ivs.length} bytes of IVs for ${n} samples of ${ivSize}`);
|
|
4621
|
+
const counts = fields.get(Field.subsampleCount)?.map((c) => uint(c, 16));
|
|
4622
|
+
const clear = fields.get(Field.bytesOfClearData)?.map((c) => uint(c, 16)) ?? [];
|
|
4623
|
+
const protectedBytes = fields.get(Field.bytesOfProtectedData)?.map((p) => uint(p, 32)) ?? [];
|
|
4624
|
+
if (!counts) {
|
|
4625
|
+
if (clear.length > 0 || protectedBytes.length > 0) throw new LocmafError("Subsample bytes without counts");
|
|
4626
|
+
return {
|
|
4627
|
+
perSampleIVSize: ivSize,
|
|
4628
|
+
ivs
|
|
4629
|
+
};
|
|
4630
|
+
}
|
|
4631
|
+
const total = counts.reduce((a, b) => a + b, 0);
|
|
4632
|
+
if (counts.length !== n || clear.length !== total || protectedBytes.length !== total) throw new LocmafError("The subsample map doesn’t match the samples");
|
|
4633
|
+
let at = 0;
|
|
4634
|
+
counts.forEach((count, i) => {
|
|
4635
|
+
let covered = 0;
|
|
4636
|
+
for (const end = at + count; at < end; at++) covered += clear[at] + protectedBytes[at];
|
|
4637
|
+
if (count > 0 && covered !== sizes[i]) throw new LocmafError(`Subsamples cover ${covered} of ${sizes[i]} bytes`);
|
|
4638
|
+
});
|
|
4639
|
+
return {
|
|
4640
|
+
perSampleIVSize: ivSize,
|
|
4641
|
+
ivs,
|
|
4642
|
+
subsamples: {
|
|
4643
|
+
counts,
|
|
4644
|
+
clear,
|
|
4645
|
+
protected: protectedBytes
|
|
4646
|
+
}
|
|
4647
|
+
};
|
|
4648
|
+
}
|
|
4649
|
+
function sampleCount(fields) {
|
|
4650
|
+
const n = fields.get(Field.sampleCount);
|
|
4651
|
+
if (n === void 0 || n > MAX_SAMPLES) throw new LocmafError(`A sample count of ${n} can't be decoded`);
|
|
4652
|
+
return Number(n);
|
|
4653
|
+
}
|
|
4654
|
+
/** `value` as a number, when it fits an unsigned field of `bits`. */
|
|
4655
|
+
function uint(value, bits) {
|
|
4656
|
+
if (value >= 1n << BigInt(bits)) throw new LocmafError(`${value} doesn’t fit ${bits} bits`);
|
|
4657
|
+
return Number(value);
|
|
4658
|
+
}
|
|
4659
|
+
// src/player/elements/locmaf/init.ts
|
|
4660
|
+
/**
|
|
4661
|
+
* Copyright 2026 The MOQtail Authors
|
|
4662
|
+
*
|
|
4663
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4664
|
+
* you may not use this file except in compliance with the License.
|
|
4665
|
+
* You may obtain a copy of the License at
|
|
4666
|
+
*
|
|
4667
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
4668
|
+
*
|
|
4669
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
4670
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
4671
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
4672
|
+
* See the License for the specific language governing permissions and
|
|
4673
|
+
* limitations under the License.
|
|
4674
|
+
*/
|
|
4675
|
+
/** Bytes before the child boxes of a protected visual and audio sample entry (ISO 14496-12 12.1.3, 12.2.3). */
|
|
4676
|
+
const SAMPLE_ENTRY_FIELDS = {
|
|
4677
|
+
encv: 78,
|
|
4678
|
+
enca: 28
|
|
4679
|
+
};
|
|
4680
|
+
/** @throws LocmafError when `init` isn't a single-track CMAF Header with `trex`. */
|
|
4681
|
+
function trackDefaults(init) {
|
|
4682
|
+
const moov = findBox(init, ["moov"]);
|
|
4683
|
+
if (!moov || [...children(moov)].filter((b) => b.type === "trak").length !== 1) throw new LocmafError("The CMAF Header needs exactly one trak (§4.1)");
|
|
4684
|
+
const tkhd = view(findBox(moov, ["trak", "tkhd"]));
|
|
4685
|
+
const trex = view(findBox(moov, ["mvex", "trex"]));
|
|
4686
|
+
const trackIdAt = tkhd?.byteLength && tkhd.getUint8(0) === 1 ? 20 : 12;
|
|
4687
|
+
if (!tkhd || tkhd.byteLength < trackIdAt + 4 || !trex || trex.byteLength < 24) throw new LocmafError("The CMAF Header has no tkhd or trex");
|
|
4688
|
+
const tenc = view(protection(moov));
|
|
4689
|
+
return {
|
|
4690
|
+
trackId: tkhd.getUint32(trackIdAt),
|
|
4691
|
+
sampleDescriptionIndex: trex.getUint32(8),
|
|
4692
|
+
sampleDuration: trex.getUint32(12),
|
|
4693
|
+
sampleSize: trex.getUint32(16),
|
|
4694
|
+
sampleFlags: trex.getUint32(20),
|
|
4695
|
+
isProtected: tenc !== void 0 && tenc.byteLength >= 8 && tenc.getUint8(6) === 1,
|
|
4696
|
+
perSampleIVSize: tenc !== void 0 && tenc.byteLength >= 8 ? tenc.getUint8(7) : 0
|
|
4697
|
+
};
|
|
4698
|
+
}
|
|
4699
|
+
/** The `tenc` of the first sample entry, when it's a protected one. */
|
|
4700
|
+
function protection(moov) {
|
|
4701
|
+
const entry = findBox(moov, [
|
|
4702
|
+
"trak",
|
|
4703
|
+
"mdia",
|
|
4704
|
+
"minf",
|
|
4705
|
+
"stbl",
|
|
4706
|
+
"stsd"
|
|
4707
|
+
])?.subarray(8);
|
|
4708
|
+
const skip = entry && entry.length >= 8 ? SAMPLE_ENTRY_FIELDS[String.fromCharCode(...entry.subarray(4, 8))] : void 0;
|
|
4709
|
+
if (skip === void 0) return void 0;
|
|
4710
|
+
return findBox(entry.subarray(8 + skip), [
|
|
4711
|
+
"sinf",
|
|
4712
|
+
"schi",
|
|
4713
|
+
"tenc"
|
|
4714
|
+
]);
|
|
4715
|
+
}
|
|
4716
|
+
function view(data) {
|
|
4717
|
+
return data && new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
4718
|
+
}
|
|
4719
|
+
// src/player/elements/locmaf/moof.ts
|
|
4720
|
+
/** tfhd and trun flags (ISO 14496-12 8.8.7, 8.8.8). */
|
|
4721
|
+
const DEFAULT_BASE_IS_MOOF = 131072;
|
|
4722
|
+
const SAMPLE_DESCRIPTION_INDEX = 2;
|
|
4723
|
+
const DEFAULT_DURATION = 8;
|
|
4724
|
+
const DEFAULT_SIZE = 16;
|
|
4725
|
+
const DEFAULT_FLAGS = 32;
|
|
4726
|
+
const DATA_OFFSET = 1;
|
|
4727
|
+
const FIRST_SAMPLE_FLAGS = 4;
|
|
4728
|
+
const SAMPLE_DURATION = 256;
|
|
4729
|
+
const SAMPLE_SIZE = 512;
|
|
4730
|
+
const SAMPLE_FLAGS = 1024;
|
|
4731
|
+
const SAMPLE_CTO = 2048;
|
|
4732
|
+
/** moof header, mfhd and traf header, ahead of tfhd. */
|
|
4733
|
+
const TRAF_START = 32;
|
|
4734
|
+
/**
|
|
4735
|
+
* The canonical CMAF chunk (draft-einarsson-moq-locmaf-01 §15): genBoxes, moof, mdat.
|
|
4736
|
+
* @throws LocmafError when a sample's CENC auxiliary information doesn't fit `saiz`.
|
|
4737
|
+
*/
|
|
4738
|
+
function canonicalChunk(chunk, defaults) {
|
|
4739
|
+
const n = chunk.sizes.length;
|
|
4740
|
+
const tfhdDefault = (values, trex) => values.length > 0 && allEqual(values) && values[0] !== trex ? values[0] : void 0;
|
|
4741
|
+
const durationsVary = !allEqual(chunk.durations);
|
|
4742
|
+
const sizesVary = !allEqual(chunk.sizes);
|
|
4743
|
+
const firstFlags = n > 1 && !allEqual(chunk.flags) && allEqual(chunk.flags.slice(1));
|
|
4744
|
+
const flagsVary = !allEqual(chunk.flags) && !firstFlags;
|
|
4745
|
+
const ctos = chunk.compositionTimeOffsets.some((c) => c !== 0);
|
|
4746
|
+
const optional = [
|
|
4747
|
+
[SAMPLE_DESCRIPTION_INDEX, chunk.sampleDescriptionIndex !== defaults.sampleDescriptionIndex ? chunk.sampleDescriptionIndex : void 0],
|
|
4748
|
+
[DEFAULT_DURATION, tfhdDefault(chunk.durations, defaults.sampleDuration)],
|
|
4749
|
+
[DEFAULT_SIZE, tfhdDefault(chunk.sizes, defaults.sampleSize)],
|
|
4750
|
+
[DEFAULT_FLAGS, flagsVary ? void 0 : tfhdDefault(chunk.flags.slice(firstFlags ? 1 : 0), defaults.sampleFlags)]
|
|
4751
|
+
].filter((entry) => entry[1] !== void 0);
|
|
4752
|
+
const tfhd = box("tfhd", u32(optional.reduce((flags, [flag]) => flags | flag, DEFAULT_BASE_IS_MOOF), defaults.trackId, ...optional.map(([, value]) => value)));
|
|
4753
|
+
const tfdt = box("tfdt", u32(1 << 24), u64(chunk.baseMediaDecodeTime));
|
|
4754
|
+
const trFlags = DATA_OFFSET | (firstFlags ? FIRST_SAMPLE_FLAGS : 0) | (durationsVary ? SAMPLE_DURATION : 0) | (sizesVary ? SAMPLE_SIZE : 0) | (flagsVary ? SAMPLE_FLAGS : 0) | (ctos ? SAMPLE_CTO : 0);
|
|
4755
|
+
const version = chunk.compositionTimeOffsets.some((c) => c < 0) ? 1 : 0;
|
|
4756
|
+
const records = chunk.sizes.flatMap((size, i) => [
|
|
4757
|
+
...durationsVary ? [chunk.durations[i]] : [],
|
|
4758
|
+
...sizesVary ? [size] : [],
|
|
4759
|
+
...flagsVary ? [chunk.flags[i]] : [],
|
|
4760
|
+
...ctos ? [chunk.compositionTimeOffsets[i]] : []
|
|
4761
|
+
]);
|
|
4762
|
+
const trun = box("trun", u32(version << 24 | trFlags, n, 0, ...firstFlags ? [chunk.flags[0]] : [], ...records));
|
|
4763
|
+
const traf = [
|
|
4764
|
+
tfhd,
|
|
4765
|
+
tfdt,
|
|
4766
|
+
trun,
|
|
4767
|
+
...encryption(chunk, TRAF_START + tfhd.length + tfdt.length + trun.length)
|
|
4768
|
+
];
|
|
4769
|
+
const moof = box("moof", box("mfhd", u32(0, 0)), box("traf", ...traf));
|
|
4770
|
+
new DataView(moof.buffer).setUint32(TRAF_START + tfhd.length + tfdt.length + 16, moof.length + 8);
|
|
4771
|
+
return concat(...chunk.genBoxes, moof, u32(8 + chunk.mdat.length), type("mdat"), chunk.mdat);
|
|
4772
|
+
}
|
|
4773
|
+
/** saiz, saio and senc (§15.8), when the chunk has CENC auxiliary information; `at` is where saiz starts in the moof. */
|
|
4774
|
+
function encryption(chunk, at) {
|
|
4775
|
+
const { perSampleIVSize: ivSize, subsamples } = chunk;
|
|
4776
|
+
if (ivSize === 0 && !subsamples) return [];
|
|
4777
|
+
const n = chunk.sizes.length;
|
|
4778
|
+
const auxSizes = chunk.sizes.map((_, i) => ivSize + (subsamples ? 2 + 6 * subsamples.counts[i] : 0));
|
|
4779
|
+
if (auxSizes.some((size) => size > 255)) throw new LocmafError("CENC auxiliary information exceeds saiz");
|
|
4780
|
+
const uniform = allEqual(auxSizes);
|
|
4781
|
+
const saiz = box("saiz", u32(0), new Uint8Array([uniform ? auxSizes[0] ?? 0 : 0]), u32(n), ...uniform ? [] : [new Uint8Array(auxSizes)]);
|
|
4782
|
+
const saio = box("saio", u32(0, 1, at + saiz.length + 20 + 16));
|
|
4783
|
+
const entries = [];
|
|
4784
|
+
let subsample = 0;
|
|
4785
|
+
for (let i = 0; i < n; i++) {
|
|
4786
|
+
entries.push(chunk.ivs.subarray(i * ivSize, (i + 1) * ivSize));
|
|
4787
|
+
if (!subsamples) continue;
|
|
4788
|
+
const count = subsamples.counts[i];
|
|
4789
|
+
const map = /* @__PURE__ */ new DataView(/* @__PURE__ */ new ArrayBuffer(2 + 6 * count));
|
|
4790
|
+
map.setUint16(0, count);
|
|
4791
|
+
for (let j = 0; j < count; j++, subsample++) {
|
|
4792
|
+
map.setUint16(2 + 6 * j, subsamples.clear[subsample]);
|
|
4793
|
+
map.setUint32(4 + 6 * j, subsamples.protected[subsample]);
|
|
4794
|
+
}
|
|
4795
|
+
entries.push(new Uint8Array(map.buffer));
|
|
4796
|
+
}
|
|
4797
|
+
return [
|
|
4798
|
+
saiz,
|
|
4799
|
+
saio,
|
|
4800
|
+
box("senc", u32(subsamples ? 2 : 0, n), ...entries)
|
|
4801
|
+
];
|
|
4802
|
+
}
|
|
4803
|
+
function allEqual(list) {
|
|
4804
|
+
return list.every((v) => v === list[0]);
|
|
4805
|
+
}
|
|
4806
|
+
function box(name, ...parts) {
|
|
4807
|
+
return concat(u32(8 + parts.reduce((size, part) => size + part.length, 0)), type(name), ...parts);
|
|
4808
|
+
}
|
|
4809
|
+
/** Big-endian 32-bit words; a negative one is written in two's complement. */
|
|
4810
|
+
function u32(...words) {
|
|
4811
|
+
const out = new Uint8Array(4 * words.length);
|
|
4812
|
+
const view = new DataView(out.buffer);
|
|
4813
|
+
words.forEach((word, i) => view.setUint32(4 * i, word >>> 0));
|
|
4814
|
+
return out;
|
|
4815
|
+
}
|
|
4816
|
+
function u64(value) {
|
|
4817
|
+
const out = /* @__PURE__ */ new Uint8Array(8);
|
|
4818
|
+
new DataView(out.buffer).setBigUint64(0, value);
|
|
4819
|
+
return out;
|
|
4820
|
+
}
|
|
4821
|
+
function type(name) {
|
|
4822
|
+
return new TextEncoder().encode(name);
|
|
4823
|
+
}
|
|
4824
|
+
function concat(...parts) {
|
|
4825
|
+
const out = new Uint8Array(parts.reduce((size, part) => size + part.length, 0));
|
|
4826
|
+
let at = 0;
|
|
4827
|
+
for (const part of parts) {
|
|
4828
|
+
out.set(part, at);
|
|
4829
|
+
at += part.length;
|
|
4830
|
+
}
|
|
4831
|
+
return out;
|
|
4832
|
+
}
|
|
4833
|
+
// src/player/elements/locmaf/source.ts
|
|
4834
|
+
/**
|
|
4835
|
+
* Copyright 2026 The MOQtail Authors
|
|
4836
|
+
*
|
|
4837
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4838
|
+
* you may not use this file except in compliance with the License.
|
|
4839
|
+
* You may obtain a copy of the License at
|
|
4840
|
+
*
|
|
4841
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
4842
|
+
*
|
|
4843
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
4844
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
4845
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
4846
|
+
* See the License for the specific language governing permissions and
|
|
4847
|
+
* limitations under the License.
|
|
4848
|
+
*/
|
|
4849
|
+
/** `LocmafSource`: a switching set source for LOCMAF tracks, whose pad carries the CMAF they rebuild to. */
|
|
4850
|
+
/** CMAF init data and timing, with each object rebuilt into its CMAF chunk (draft-einarsson-moq-locmaf-01 §16). */
|
|
4851
|
+
var LocmafSource = class extends CmafSource {
|
|
4852
|
+
#references = /* @__PURE__ */ new Map();
|
|
4853
|
+
#defaults;
|
|
4854
|
+
/** The group a rejected object was last reported for. */
|
|
4855
|
+
#reported;
|
|
4856
|
+
packet(track, object, initData) {
|
|
4857
|
+
const key = trackKey(track);
|
|
4858
|
+
const reference = this.#references.get(key) ?? new Reference();
|
|
4859
|
+
this.#references.set(key, reference);
|
|
4860
|
+
try {
|
|
4861
|
+
if (!initData) throw new LocmafError("No CMAF Header to rebuild the chunk with");
|
|
4862
|
+
const defaults = this.#defaultsOf(initData);
|
|
4863
|
+
const decoded = reference.decode(object.location, object.payload, defaults);
|
|
4864
|
+
const payload = decoded instanceof Uint8Array ? decoded : canonicalChunk(decoded, defaults);
|
|
4865
|
+
return MoqtObject.newWithPayload(object.fullTrackName, object.location, object.publisherPriority, object.objectForwardingPreference, object.subgroupId, object.properties, payload);
|
|
4866
|
+
} catch (error) {
|
|
4867
|
+
if (!(error instanceof LocmafError)) throw error;
|
|
4868
|
+
this.#report(key, object, error);
|
|
4869
|
+
return;
|
|
4870
|
+
}
|
|
4871
|
+
}
|
|
4872
|
+
/** The CMAF Header parameters of `init`, parsed once per header. */
|
|
4873
|
+
#defaultsOf(init) {
|
|
4874
|
+
if (this.#defaults?.init !== init) this.#defaults = {
|
|
4875
|
+
init,
|
|
4876
|
+
defaults: trackDefaults(init)
|
|
4877
|
+
};
|
|
4878
|
+
return this.#defaults.defaults;
|
|
4879
|
+
}
|
|
4880
|
+
/** A warning for the first object of a group that can't be rebuilt; the rest of it fails the same way. */
|
|
4881
|
+
#report(track, object, cause) {
|
|
4882
|
+
const group = `${track}|${object.location.group}`;
|
|
4883
|
+
if (this.#reported === group) return;
|
|
4884
|
+
this.#reported = group;
|
|
4885
|
+
const message = `LOCMAF object ${object.location.group}.${object.location.object} dropped: ${cause.message}`;
|
|
4886
|
+
this.context.player.emit("warning", new PlayerError("MEDIA_FAILURE", message, {
|
|
4887
|
+
element: "locmaf",
|
|
4888
|
+
cause
|
|
4889
|
+
}));
|
|
4890
|
+
}
|
|
4891
|
+
};
|
|
4892
|
+
// src/player/elements/locmaf/index.ts
|
|
4893
|
+
/**
|
|
4894
|
+
* Copyright 2026 The MOQtail Authors
|
|
4895
|
+
*
|
|
4896
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4897
|
+
* you may not use this file except in compliance with the License.
|
|
4898
|
+
* You may obtain a copy of the License at
|
|
4899
|
+
*
|
|
4900
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
4901
|
+
*
|
|
4902
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
4903
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
4904
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
4905
|
+
* See the License for the specific language governing permissions and
|
|
4906
|
+
* limitations under the License.
|
|
4907
|
+
*/
|
|
4908
|
+
/** The LOCMAF packaging version this element decodes (draft-einarsson-moq-locmaf-01 §5). */
|
|
4909
|
+
const LOCMAF_VERSION = "0.3";
|
|
4910
|
+
/**
|
|
4911
|
+
* The built-in media source for a LOCMAF switching set. It rebuilds CMAF chunks, so its pad
|
|
4912
|
+
* carries CMAF. The player registers it, and elements reach it through `context.probe()`. Its
|
|
4913
|
+
* lower priority prefers it over a CMAF set of the same media, since LOCMAF has less overhead.
|
|
4914
|
+
*/
|
|
4915
|
+
const Locmaf = defineElement({
|
|
4916
|
+
type: "source",
|
|
4917
|
+
name: "locmaf",
|
|
4918
|
+
priority: 90,
|
|
4919
|
+
capabilities: [cmafCapabilities("out", "video"), cmafCapabilities("out", "audio")],
|
|
4920
|
+
provides: [MEDIA_SOURCE],
|
|
4921
|
+
probe: probeLocmaf,
|
|
4922
|
+
init: async (context, options) => new LocmafSource(context, options ?? {}, await planSource(context, options, "locmaf"))
|
|
4923
|
+
});
|
|
4924
|
+
/**
|
|
4925
|
+
* Accepts an unencrypted LOCMAF `video` or `audio` track of a version it decodes: a receiver
|
|
4926
|
+
* must not subscribe to another (§5). Encryption is out of scope, as for CMAF.
|
|
4927
|
+
*/
|
|
4928
|
+
function probeLocmaf(identifiers) {
|
|
4929
|
+
const { packaging, role } = identifiers;
|
|
4930
|
+
if (packaging !== "locmaf" || role !== "video" && role !== "audio") return false;
|
|
4931
|
+
if (identifiers["locmafVersion"] !== LOCMAF_VERSION) return false;
|
|
4932
|
+
return identifiers["encryptionScheme"] === void 0 && identifiers["contentProtectionRefIDs"] === void 0;
|
|
4933
|
+
}
|
|
4269
4934
|
// src/player/elements/saptimeline/index.ts
|
|
4270
4935
|
/**
|
|
4271
4936
|
* Copyright 2026 The MOQtail Authors
|
|
@@ -4315,6 +4980,7 @@ function isSapData(data, startsGroup) {
|
|
|
4315
4980
|
const BUILT_IN_SOURCES = [
|
|
4316
4981
|
CatalogSource,
|
|
4317
4982
|
Cmaf,
|
|
4983
|
+
Locmaf,
|
|
4318
4984
|
MediaTimeline,
|
|
4319
4985
|
defineElement({
|
|
4320
4986
|
type: "source",
|
|
@@ -4423,9 +5089,26 @@ function gapJumpTarget(currentTime, buffered, live, liveEdgeDelay) {
|
|
|
4423
5089
|
if (length >= STALL_THRESHOLD$1 && length >= liveEdgeDelay) return start;
|
|
4424
5090
|
}
|
|
4425
5091
|
}
|
|
5092
|
+
/** How long playback may sit still with data ahead before a nudge, in seconds. */
|
|
5093
|
+
const NUDGE_AFTER = 1;
|
|
5094
|
+
/** The first nudge; each further one at the same spot doubles. */
|
|
5095
|
+
const NUDGE = .1;
|
|
5096
|
+
/**
|
|
5097
|
+
* Where to nudge playback stuck at `currentTime` inside a range for `stuckFor` seconds after
|
|
5098
|
+
* `attempts` nudges, or `undefined` to wait. Data after a hole can start without its keyframe,
|
|
5099
|
+
* so the decoder waits on frames that never come although the range says data is there; a jump
|
|
5100
|
+
* past them gets it going (hls.js does the same). Doubling reaches a GoP away in a few tries.
|
|
5101
|
+
*/
|
|
5102
|
+
function nudgeTarget(currentTime, buffered, stuckFor, attempts) {
|
|
5103
|
+
const index = rangeAt(currentTime, buffered);
|
|
5104
|
+
if (index === -1 || stuckFor < NUDGE_AFTER) return void 0;
|
|
5105
|
+
const ahead = buffered.end(index) - currentTime;
|
|
5106
|
+
if (ahead < STALL_THRESHOLD$1) return void 0;
|
|
5107
|
+
return currentTime + Math.min(NUDGE * 2 ** attempts, ahead - STALL_THRESHOLD$1);
|
|
5108
|
+
}
|
|
4426
5109
|
/** WebKit rounds `currentTime`, so a jump to a range's start can land a hair before it. */
|
|
4427
5110
|
const START_TOLERANCE = .001;
|
|
4428
|
-
/** Index of the range containing `time`, or -1. */
|
|
5111
|
+
/** @public Index of the range containing `time`, or -1. */
|
|
4429
5112
|
function rangeAt(time, ranges) {
|
|
4430
5113
|
for (let i = 0; i < ranges.length; i++) if (ranges.start(i) - START_TOLERANCE <= time && time <= ranges.end(i)) return i;
|
|
4431
5114
|
return -1;
|
|
@@ -4454,15 +5137,17 @@ function vodDuration(pads) {
|
|
|
4454
5137
|
return ends.length > 0 ? Math.max(...ends) : void 0;
|
|
4455
5138
|
}
|
|
4456
5139
|
/**
|
|
4457
|
-
* Live: from the timeline start, else the buffered start,
|
|
4458
|
-
*
|
|
5140
|
+
* Live: from the timeline start, else the buffered start, no earlier than the accessible window
|
|
5141
|
+
* before the live edge (MSF 5.2.46), to the live edge minus the target latency, so seeking to the
|
|
5142
|
+
* end never lands right at the edge (proposal 0007).
|
|
4459
5143
|
*/
|
|
4460
5144
|
function liveSeekable(buffered, pads) {
|
|
4461
5145
|
const ranges = pads.flatMap((p) => p.seekable ?? []);
|
|
4462
5146
|
const last = buffered.length - 1;
|
|
4463
5147
|
if (ranges.length === 0 && last < 0) return void 0;
|
|
4464
|
-
const start = ranges.length > 0 ? Math.max(...ranges.map((r) => r.start)) : buffered.start(0);
|
|
4465
5148
|
const edge = Math.max(...ranges.map((r) => r.end), last >= 0 ? buffered.end(last) : -Infinity);
|
|
5149
|
+
const windows = pads.flatMap((p) => p.accessibleWindow ?? []);
|
|
5150
|
+
const start = Math.max(ranges.length > 0 ? Math.max(...ranges.map((r) => r.start)) : buffered.start(0), windows.length > 0 ? edge - Math.max(...windows) : -Infinity);
|
|
4466
5151
|
return {
|
|
4467
5152
|
start,
|
|
4468
5153
|
end: Math.max(start, edge - targetLatency(pads))
|
|
@@ -4655,6 +5340,7 @@ const RELAYED_EVENTS = [
|
|
|
4655
5340
|
"stalled",
|
|
4656
5341
|
"waiting",
|
|
4657
5342
|
"timeupdate",
|
|
5343
|
+
"seeking",
|
|
4658
5344
|
"seeked",
|
|
4659
5345
|
"ended"
|
|
4660
5346
|
];
|
|
@@ -4684,6 +5370,12 @@ var MSESink = class extends SinkElement {
|
|
|
4684
5370
|
#live = false;
|
|
4685
5371
|
/** The live seekable range last set. */
|
|
4686
5372
|
#seekable;
|
|
5373
|
+
/** Where playback last sat still, since when (ms), and the nudges tried there. */
|
|
5374
|
+
#stuck = {
|
|
5375
|
+
time: NaN,
|
|
5376
|
+
since: 0,
|
|
5377
|
+
attempts: 0
|
|
5378
|
+
};
|
|
4687
5379
|
/** Set once playback has its start position; a rebuild clears it. */
|
|
4688
5380
|
#started = false;
|
|
4689
5381
|
/** Where playback continues after a rebuild, instead of the start position. */
|
|
@@ -4751,6 +5443,10 @@ var MSESink = class extends SinkElement {
|
|
|
4751
5443
|
if (!this.#live || !this.#seekable) return NaN;
|
|
4752
5444
|
return liveEdgeStart(this.#seekable.end, this.#properties());
|
|
4753
5445
|
}
|
|
5446
|
+
get dvrRange() {
|
|
5447
|
+
const windowed = this.#properties().some((p) => p.accessibleWindow !== void 0);
|
|
5448
|
+
return this.#live && windowed ? this.#seekable : void 0;
|
|
5449
|
+
}
|
|
4754
5450
|
setPlaybackRate(rate) {
|
|
4755
5451
|
this.#video.playbackRate = rate;
|
|
4756
5452
|
}
|
|
@@ -4977,19 +5673,45 @@ var MSESink = class extends SinkElement {
|
|
|
4977
5673
|
if (video.seeking) return this.#unstrand();
|
|
4978
5674
|
const target = gapJumpTarget(video.currentTime, video.buffered, this.#live, this.#options.liveEdgeDelay);
|
|
4979
5675
|
if (target !== void 0 && target > video.currentTime) this.#seekTracker.jump(target);
|
|
5676
|
+
else this.#unstrand();
|
|
5677
|
+
}
|
|
5678
|
+
/** Playback stranded where nothing will move it on: outside the data, or stuck inside it. */
|
|
5679
|
+
#unstrand() {
|
|
5680
|
+
const { currentTime, buffered } = this.#video;
|
|
5681
|
+
if (rangeAt(currentTime, buffered) === -1) this.#jumpToData(currentTime, buffered);
|
|
5682
|
+
else this.#nudgeStuck(currentTime, buffered);
|
|
4980
5683
|
}
|
|
4981
5684
|
/**
|
|
4982
5685
|
* A seek that landed just before the data never completes: a group located a little late, or
|
|
4983
5686
|
* tracks whose data starts at different times. Once no upstream seek is pending and every
|
|
4984
5687
|
* buffer has data past it, nothing will fill the spot, so it jumps to where the data starts.
|
|
4985
5688
|
*/
|
|
4986
|
-
#
|
|
4987
|
-
const time = this.#video.currentTime;
|
|
4988
|
-
const buffered = this.#video.buffered;
|
|
4989
|
-
if (rangeAt(time, buffered) !== -1) return;
|
|
5689
|
+
#jumpToData(time, buffered) {
|
|
4990
5690
|
for (const state of this.#pads.values()) if (state.seeks > 0 || !state.queue || (bufferedEnd(state.queue.buffer) ?? 0) <= time) return;
|
|
4991
5691
|
for (let i = 0; i < buffered.length; i++) if (buffered.start(i) > time) return this.#seekTracker.jump(buffered.start(i));
|
|
4992
5692
|
}
|
|
5693
|
+
/** Playback sitting still inside the data, past a hole whose data starts without its keyframe (`nudgeTarget`). */
|
|
5694
|
+
#nudgeStuck(time, buffered) {
|
|
5695
|
+
const video = this.#video;
|
|
5696
|
+
const now = performance.now();
|
|
5697
|
+
const previous = this.#stuck;
|
|
5698
|
+
if (video.paused || video.readyState >= HTMLMediaElement.HAVE_FUTURE_DATA || time !== previous.time) {
|
|
5699
|
+
this.#stuck = {
|
|
5700
|
+
time,
|
|
5701
|
+
since: now,
|
|
5702
|
+
attempts: time === previous.time ? previous.attempts : 0
|
|
5703
|
+
};
|
|
5704
|
+
return;
|
|
5705
|
+
}
|
|
5706
|
+
const target = nudgeTarget(time, buffered, (now - previous.since) / 1e3, previous.attempts);
|
|
5707
|
+
if (target === void 0) return;
|
|
5708
|
+
this.#stuck = {
|
|
5709
|
+
time: target,
|
|
5710
|
+
since: now,
|
|
5711
|
+
attempts: previous.attempts + 1
|
|
5712
|
+
};
|
|
5713
|
+
this.#seekTracker.jump(target);
|
|
5714
|
+
}
|
|
4993
5715
|
/**
|
|
4994
5716
|
* After a switch, the old track's data plays out up to the new track's first group. Once
|
|
4995
5717
|
* playback gets there, everything before it is removed, so a seek back never shows the old
|
|
@@ -5043,8 +5765,8 @@ var MSESink = class extends SinkElement {
|
|
|
5043
5765
|
/**
|
|
5044
5766
|
* Live only. Native `seekable` is the union of the live range and `buffered` (MSE 10.1), so its
|
|
5045
5767
|
* end stays at the buffered end; `liveEdgeStart` is taken from this range. The live range is set
|
|
5046
|
-
* only from a media timeline: without
|
|
5047
|
-
* reach the relay's cache.
|
|
5768
|
+
* only from a media timeline or an accessible window: without either, `seekable` starts at 0,
|
|
5769
|
+
* and seeks before the buffer reach the relay's cache.
|
|
5048
5770
|
*/
|
|
5049
5771
|
#updateSeekable() {
|
|
5050
5772
|
const source = this.#source;
|
|
@@ -5053,7 +5775,7 @@ var MSESink = class extends SinkElement {
|
|
|
5053
5775
|
const range = liveSeekable(this.#video.buffered, properties);
|
|
5054
5776
|
if (!range || range.start === this.#seekable?.start && range.end === this.#seekable.end) return;
|
|
5055
5777
|
this.#seekable = range;
|
|
5056
|
-
if (properties.some((p) => p.seekable)) source.setLiveSeekableRange(range.start, range.end);
|
|
5778
|
+
if (properties.some((p) => p.seekable || p.accessibleWindow !== void 0)) source.setLiveSeekableRange(range.start, range.end);
|
|
5057
5779
|
}
|
|
5058
5780
|
#properties() {
|
|
5059
5781
|
return [...this.#pads.values()].map((s) => s.properties);
|
|
@@ -5293,11 +6015,29 @@ async function open(s, origin, options) {
|
|
|
5293
6015
|
...transport.protocols && { protocols: [...transport.protocols] }
|
|
5294
6016
|
} }
|
|
5295
6017
|
});
|
|
5296
|
-
|
|
5297
|
-
|
|
6018
|
+
let next = origin;
|
|
6019
|
+
let left = false;
|
|
6020
|
+
client.on("goaway", (goaway) => {
|
|
6021
|
+
next = goaway.newSessionUri ?? origin;
|
|
6022
|
+
left = true;
|
|
6023
|
+
client.disconnect("GOAWAY");
|
|
6024
|
+
});
|
|
6025
|
+
client.on("sessionterminated", () => reconnect(s, client, next, options));
|
|
6026
|
+
await client.init().catch((error) => {
|
|
6027
|
+
if (!left) throw error;
|
|
6028
|
+
});
|
|
6029
|
+
if (left) throw new Moved(next);
|
|
5298
6030
|
s.client = client;
|
|
5299
6031
|
return client;
|
|
5300
6032
|
}
|
|
6033
|
+
/** A GOAWAY arrived while the session was being set up; the next attempt goes to `uri`. */
|
|
6034
|
+
var Moved = class extends Error {
|
|
6035
|
+
uri;
|
|
6036
|
+
constructor(uri) {
|
|
6037
|
+
super(`GOAWAY during setup, moving to ${uri}`);
|
|
6038
|
+
this.uri = uri;
|
|
6039
|
+
}
|
|
6040
|
+
};
|
|
5301
6041
|
/** Recreates a lost session for every player, or fails them all with `SESSION_LOST`. */
|
|
5302
6042
|
function reconnect(s, client, origin, options) {
|
|
5303
6043
|
if (s.disposing || s.client !== client) return;
|
|
@@ -5318,15 +6058,17 @@ function reconnect(s, client, origin, options) {
|
|
|
5318
6058
|
async function retry(s, origin, options, onAttempt) {
|
|
5319
6059
|
const signal = s.abort.signal;
|
|
5320
6060
|
const attempts = options.reconnectAttempts ?? 5;
|
|
6061
|
+
let target = origin;
|
|
5321
6062
|
for (let failed = 0;;) {
|
|
5322
6063
|
onAttempt?.(failed + 1);
|
|
5323
6064
|
await untilAvailable(signal);
|
|
5324
6065
|
try {
|
|
5325
|
-
return await open(s,
|
|
6066
|
+
return await open(s, target, options);
|
|
5326
6067
|
} catch (cause) {
|
|
5327
6068
|
signal.throwIfAborted();
|
|
6069
|
+
if (cause instanceof Moved) target = cause.uri;
|
|
5328
6070
|
if (!available()) continue;
|
|
5329
|
-
if (++failed >= attempts) throw new PlayerError("SESSION_LOST", `Could not connect to ${
|
|
6071
|
+
if (++failed >= attempts) throw new PlayerError("SESSION_LOST", `Could not connect to ${target}`, { cause });
|
|
5330
6072
|
await sleep(backoffDelay(failed - 1), signal);
|
|
5331
6073
|
}
|
|
5332
6074
|
}
|
|
@@ -5858,6 +6600,55 @@ function isPinned(source) {
|
|
|
5858
6600
|
const renditions = source.getRenditions();
|
|
5859
6601
|
return renditions.length > 1 && renditions.filter((r) => r.selected).length === 1;
|
|
5860
6602
|
}
|
|
6603
|
+
// src/player/follow.ts
|
|
6604
|
+
const decoder = new TextDecoder();
|
|
6605
|
+
/**
|
|
6606
|
+
* @public
|
|
6607
|
+
* Runs `attach` on the player's current client and on every later one (reconnects replace it),
|
|
6608
|
+
* calling the cleanup `attach` returned for the one before. Returns the function that stops.
|
|
6609
|
+
*/
|
|
6610
|
+
function eachClient(player, attach) {
|
|
6611
|
+
let detach;
|
|
6612
|
+
const use = (client) => {
|
|
6613
|
+
detach?.();
|
|
6614
|
+
detach = attach(client);
|
|
6615
|
+
};
|
|
6616
|
+
if (player.client) use(player.client);
|
|
6617
|
+
player.on("client", use);
|
|
6618
|
+
return () => {
|
|
6619
|
+
player.off("client", use);
|
|
6620
|
+
detach?.();
|
|
6621
|
+
};
|
|
6622
|
+
}
|
|
6623
|
+
/**
|
|
6624
|
+
* @public
|
|
6625
|
+
* Calls `listener` with each incoming data event of the player's client, across reconnects, with
|
|
6626
|
+
* the name of the track its stream carries and the stream's header. A stream can open before the
|
|
6627
|
+
* response naming its track, so `track` stays undefined until a later event of that stream.
|
|
6628
|
+
* Streams opened before the call are skipped. Returns the function that stops listening.
|
|
6629
|
+
*/
|
|
6630
|
+
function followData(player, listener) {
|
|
6631
|
+
const streams = /* @__PURE__ */ new Map();
|
|
6632
|
+
let client;
|
|
6633
|
+
const onData = (event) => {
|
|
6634
|
+
if (event.direction !== "in") return;
|
|
6635
|
+
if (event.kind === "header") streams.set(event.streamId, { header: event });
|
|
6636
|
+
const stream = streams.get(event.streamId);
|
|
6637
|
+
if (!stream) return;
|
|
6638
|
+
if (event.kind === "end") streams.delete(event.streamId);
|
|
6639
|
+
if (stream.track === void 0) {
|
|
6640
|
+
const name = client?.trackNameOf(stream.header);
|
|
6641
|
+
if (name) stream.track = decoder.decode(name.name);
|
|
6642
|
+
}
|
|
6643
|
+
listener(event, stream.track, stream.header);
|
|
6644
|
+
};
|
|
6645
|
+
return eachClient(player, (next) => {
|
|
6646
|
+
client = next;
|
|
6647
|
+
streams.clear();
|
|
6648
|
+
next.on("dataevent", onData);
|
|
6649
|
+
return () => next.off("dataevent", onData);
|
|
6650
|
+
});
|
|
6651
|
+
}
|
|
5861
6652
|
// src/player/plugins/buffer/live.ts
|
|
5862
6653
|
const CATCHUP_RATE = 1.1;
|
|
5863
6654
|
/** How far the forward buffer may stray from its goal before the rate changes. */
|
|
@@ -5906,6 +6697,8 @@ var LiveBufferController = class {
|
|
|
5906
6697
|
#timeline;
|
|
5907
6698
|
/** Set once the controller changed the rate, so `detach()` gives 1 back. */
|
|
5908
6699
|
#adjusted = false;
|
|
6700
|
+
/** Cleared by a seek behind the live edge, set again by one to it (proposal 0007). */
|
|
6701
|
+
#following = true;
|
|
5909
6702
|
constructor(catchupRate, targetLatency) {
|
|
5910
6703
|
this.#catchupRate = catchupRate;
|
|
5911
6704
|
this.#targetLatency = targetLatency;
|
|
@@ -5920,6 +6713,7 @@ var LiveBufferController = class {
|
|
|
5920
6713
|
player.on("catalog", this.#refresh);
|
|
5921
6714
|
player.on("renditionchange", this.#refresh);
|
|
5922
6715
|
sink.addEventListener("timeupdate", () => this.#tick(sink), { signal: abort.signal });
|
|
6716
|
+
sink.addEventListener("seeking", () => this.#seeking(sink), { signal: abort.signal });
|
|
5923
6717
|
this.#refresh();
|
|
5924
6718
|
}
|
|
5925
6719
|
detach() {
|
|
@@ -5931,6 +6725,7 @@ var LiveBufferController = class {
|
|
|
5931
6725
|
attached.player.off("renditionchange", this.#refresh);
|
|
5932
6726
|
if (this.#adjusted) attached.sink.setPlaybackRate(1);
|
|
5933
6727
|
this.#adjusted = false;
|
|
6728
|
+
this.#following = true;
|
|
5934
6729
|
this.#targets = void 0;
|
|
5935
6730
|
this.#timeline = void 0;
|
|
5936
6731
|
}
|
|
@@ -5949,8 +6744,18 @@ var LiveBufferController = class {
|
|
|
5949
6744
|
if (this.#attached === attached) this.#timeline = timeline;
|
|
5950
6745
|
});
|
|
5951
6746
|
};
|
|
6747
|
+
/**
|
|
6748
|
+
* Where a seek lands decides, whoever made it: the controller's and the sink's own jumps land at
|
|
6749
|
+
* the edge, a viewer's seek back leaves the rate at 1 until a seek to the edge (the Live button).
|
|
6750
|
+
*/
|
|
6751
|
+
#seeking(sink) {
|
|
6752
|
+
this.#following = !(sink.currentTime < sink.liveEdgeStart);
|
|
6753
|
+
if (this.#following || !this.#adjusted) return;
|
|
6754
|
+
sink.setPlaybackRate(1);
|
|
6755
|
+
this.#adjusted = false;
|
|
6756
|
+
}
|
|
5952
6757
|
#tick(sink) {
|
|
5953
|
-
if (sink.paused || sink.seeking) return;
|
|
6758
|
+
if (sink.paused || sink.seeking || !this.#following) return;
|
|
5954
6759
|
const { currentTime, buffered } = sink;
|
|
5955
6760
|
const range = rangeAt(currentTime, buffered);
|
|
5956
6761
|
if (range === -1) return;
|
|
@@ -6021,12 +6826,895 @@ const liveBuffer = definePlugin({
|
|
|
6021
6826
|
item: controller
|
|
6022
6827
|
}]
|
|
6023
6828
|
});
|
|
6829
|
+
// src/player/plugins/abr/features.ts
|
|
6830
|
+
/** @public What the controller does with a tick. */
|
|
6831
|
+
const Action = {
|
|
6832
|
+
Down: 0,
|
|
6833
|
+
Stay: 1,
|
|
6834
|
+
Up: 2,
|
|
6835
|
+
Probe: 3
|
|
6836
|
+
};
|
|
6837
|
+
/** Seconds of forward buffer below which playback is about to stall. */
|
|
6838
|
+
const LOW_BUFFER = .5;
|
|
6839
|
+
/** A switch up goes to the highest rendition within this share of the probe's estimate. */
|
|
6840
|
+
const UP_MARGIN = .9;
|
|
6841
|
+
/** A switch down goes to the highest rendition within this share of `throughput`. */
|
|
6842
|
+
const DOWN_MARGIN = .8;
|
|
6843
|
+
/** Seconds without trouble or probes after which the probe backoff starts over. */
|
|
6844
|
+
const STABLE_RESET = 30;
|
|
6845
|
+
/** Seconds measured after a switch settled before the next decision. */
|
|
6846
|
+
const SETTLE_MEASURE = 1;
|
|
6847
|
+
/** The model's inputs, in order. Only their hash travels with a model. */
|
|
6848
|
+
const FEATURES = [
|
|
6849
|
+
"fwd",
|
|
6850
|
+
"rate",
|
|
6851
|
+
"stall",
|
|
6852
|
+
"qpos",
|
|
6853
|
+
"gap_up",
|
|
6854
|
+
"gap_dn",
|
|
6855
|
+
"recv",
|
|
6856
|
+
"arrival",
|
|
6857
|
+
"probe",
|
|
6858
|
+
"probe_age",
|
|
6859
|
+
"probe_busy",
|
|
6860
|
+
"since_sw",
|
|
6861
|
+
"gop"
|
|
6862
|
+
];
|
|
6863
|
+
/** Seconds before probing again after 1, 2, and 3 or more probes in a row that found no room. */
|
|
6864
|
+
const PROBE_BACKOFF = [
|
|
6865
|
+
5,
|
|
6866
|
+
10,
|
|
6867
|
+
15
|
|
6868
|
+
];
|
|
6869
|
+
/** FNV-1a over the comma-joined feature names. */
|
|
6870
|
+
function featuresHash() {
|
|
6871
|
+
let hash = 2166136261;
|
|
6872
|
+
for (const byte of new TextEncoder().encode(FEATURES.join(","))) hash = Math.imul(hash ^ byte, 16777619) >>> 0;
|
|
6873
|
+
return hash;
|
|
6874
|
+
}
|
|
6875
|
+
/**
|
|
6876
|
+
* The model's inputs. Every one is a ratio, a log ratio, or a clipped duration, so none depends on
|
|
6877
|
+
* the ladder's bitrates or the link's absolute speed.
|
|
6878
|
+
*/
|
|
6879
|
+
function features(o) {
|
|
6880
|
+
const { ladder, requested: k } = o;
|
|
6881
|
+
const top = ladder.length - 1;
|
|
6882
|
+
const bitrate = ladder[k];
|
|
6883
|
+
return [
|
|
6884
|
+
Math.log1p(clamp(o.forwardBuffer, 0, 30)),
|
|
6885
|
+
o.playbackRate - 1,
|
|
6886
|
+
clamp(o.stalledTime / 2, 0, 1),
|
|
6887
|
+
top > 0 ? Math.log(bitrate / ladder[0]) / Math.log(ladder[top] / ladder[0]) : 1,
|
|
6888
|
+
k < top ? Math.log(ladder[k + 1] / bitrate) : 0,
|
|
6889
|
+
k > 0 ? Math.log(bitrate / ladder[k - 1]) : 0,
|
|
6890
|
+
clamp(o.receivedBytes * 8 / 2 / o.activeBitrate, 0, 4),
|
|
6891
|
+
clamp(o.arrivalRate, 0, 4),
|
|
6892
|
+
o.probeEstimate === null ? 0 : clamp(Math.log(Math.max(o.probeEstimate, 1) * 8 / bitrate), -3, 3),
|
|
6893
|
+
o.probeAge === null ? 1 : clamp(o.probeAge / 30, 0, 1),
|
|
6894
|
+
o.probing ? 1 : 0,
|
|
6895
|
+
clamp(o.sinceSwitch / 10, 0, 1),
|
|
6896
|
+
o.groupInterval === null ? .25 : clamp(o.groupInterval, 0, 4) / 4
|
|
6897
|
+
];
|
|
6898
|
+
}
|
|
6899
|
+
/**
|
|
6900
|
+
* Which actions are allowed, by `Action`. Every switch costs the viewer, so:
|
|
6901
|
+
* - only stay while a switch settles and for `SETTLE_MEASURE` after: until then the old track still
|
|
6902
|
+
* shares the link, and what arrives measures neither track;
|
|
6903
|
+
* - down only on trouble: a stall, frames arriving under `SHORT` of their rate, or an emptying buffer;
|
|
6904
|
+
* - up only when the median of `CONFIRMING` fresh probes shows room for the next rendition;
|
|
6905
|
+
* - a probe only below the top, from a known group, with none in flight, while not starved, and once
|
|
6906
|
+
* the backoff after probes that found no room has passed.
|
|
6907
|
+
*/
|
|
6908
|
+
function legalActions(o) {
|
|
6909
|
+
if (o.settling || o.sinceSettled < SETTLE_MEASURE) return [
|
|
6910
|
+
false,
|
|
6911
|
+
true,
|
|
6912
|
+
false,
|
|
6913
|
+
false
|
|
6914
|
+
];
|
|
6915
|
+
const k = o.requested;
|
|
6916
|
+
const top = o.ladder.length - 1;
|
|
6917
|
+
const trouble = o.stalledTime > 0 || o.arrivalRate < .8 || o.forwardBuffer < LOW_BUFFER;
|
|
6918
|
+
const room = k < top && o.evidence !== null && o.evidence * 8 * UP_MARGIN >= o.ladder[k + 1];
|
|
6919
|
+
const starved = o.stalledTime > 0 || o.arrivalRate < .5;
|
|
6920
|
+
const calm = o.sinceTrouble >= STABLE_RESET && (o.sinceProbe === null || o.sinceProbe >= STABLE_RESET);
|
|
6921
|
+
const backingOff = o.sinceProbe !== null && o.sinceProbe < probeBackoff(calm ? 0 : o.probeFailures);
|
|
6922
|
+
const canProbe = k < top && o.probeable && !o.probing && !starved && !backingOff && !room;
|
|
6923
|
+
return [
|
|
6924
|
+
k > 0 && trouble,
|
|
6925
|
+
true,
|
|
6926
|
+
room,
|
|
6927
|
+
canProbe
|
|
6928
|
+
];
|
|
6929
|
+
}
|
|
6930
|
+
/** The highest rendition within `UP_MARGIN` of the evidence, at least one up. Only valid when up is allowed. */
|
|
6931
|
+
function climbTarget(o) {
|
|
6932
|
+
const budget = (o.evidence ?? 0) * 8 * UP_MARGIN;
|
|
6933
|
+
let target = o.requested + 1;
|
|
6934
|
+
for (let r = target; r < o.ladder.length; r++) if (o.ladder[r] <= budget) target = r;
|
|
6935
|
+
return target;
|
|
6936
|
+
}
|
|
6937
|
+
/**
|
|
6938
|
+
* The highest rendition within `DOWN_MARGIN` of `throughput`, at least one down: one switch rather
|
|
6939
|
+
* than one per rung, each waiting for the last to settle.
|
|
6940
|
+
*/
|
|
6941
|
+
function downTarget(o) {
|
|
6942
|
+
const budget = o.throughput * 8 * DOWN_MARGIN;
|
|
6943
|
+
let target = 0;
|
|
6944
|
+
for (let r = 0; r < o.requested; r++) if (o.ladder[r] <= budget) target = r;
|
|
6945
|
+
return target;
|
|
6946
|
+
}
|
|
6947
|
+
/** Seconds to wait before the next probe after `failures` probes in a row found no room. */
|
|
6948
|
+
function probeBackoff(failures) {
|
|
6949
|
+
return failures > 0 ? PROBE_BACKOFF[Math.min(failures, PROBE_BACKOFF.length) - 1] : 0;
|
|
6950
|
+
}
|
|
6951
|
+
function clamp(value, min, max) {
|
|
6952
|
+
return Math.min(Math.max(value, min), max);
|
|
6953
|
+
}
|
|
6954
|
+
// src/player/plugins/abr/probe.ts
|
|
6955
|
+
/** A probe carries the difference to the next bitrate for this many seconds. */
|
|
6956
|
+
const PROBE_SECONDS = 1;
|
|
6957
|
+
/** Seconds a probe may take; what arrived by then is measured, the rest is cancelled. */
|
|
6958
|
+
const PROBE_TIMEOUT = 2;
|
|
6959
|
+
/** Seconds of a probe's round trip before its data can arrive. */
|
|
6960
|
+
const PROBE_ALLOWANCE = .25;
|
|
6961
|
+
/** Below this share of the pace that would show room, a probe can no longer show it, so it ends early. */
|
|
6962
|
+
const PROBE_BEHIND = .5;
|
|
6963
|
+
/** Below live media's 128: the relay sends a probe only when live leaves the link idle. */
|
|
6964
|
+
const PROBE_PRIORITY = 200;
|
|
6965
|
+
/** Bytes a probe for `next` needs on top of live delivery at `current`, both in bits per second. */
|
|
6966
|
+
function probeBytes(current, next) {
|
|
6967
|
+
return Math.floor((next - current) / 8 * PROBE_SECONDS);
|
|
6968
|
+
}
|
|
6969
|
+
/**
|
|
6970
|
+
* The FETCHes that carry closest to `targetBytes`. The known groups are walked oldest first as one
|
|
6971
|
+
* run, and the run stops at the object whose total lands closest to the target, at least one
|
|
6972
|
+
* object. A target beyond the whole run repeats it: the relay still has those objects cached.
|
|
6973
|
+
*/
|
|
6974
|
+
function planProbe(groups, targetBytes) {
|
|
6975
|
+
const objects = groups.flatMap(({ group, objects }) => objects.map(({ id, bytes }) => ({
|
|
6976
|
+
group,
|
|
6977
|
+
id,
|
|
6978
|
+
bytes
|
|
6979
|
+
})));
|
|
6980
|
+
const total = objects.reduce((sum, { bytes }) => sum + bytes, 0);
|
|
6981
|
+
if (objects.length === 0 || total <= 0 || targetBytes <= 0) return [];
|
|
6982
|
+
const prefix = (count, bytes) => ({
|
|
6983
|
+
start: {
|
|
6984
|
+
group: objects[0].group,
|
|
6985
|
+
object: objects[0].id
|
|
6986
|
+
},
|
|
6987
|
+
end: {
|
|
6988
|
+
group: objects[count - 1].group,
|
|
6989
|
+
object: objects[count - 1].id
|
|
6990
|
+
},
|
|
6991
|
+
objects: count,
|
|
6992
|
+
bytes
|
|
6993
|
+
});
|
|
6994
|
+
const plan = Array.from({ length: Math.floor(targetBytes / total) }, () => prefix(objects.length, total));
|
|
6995
|
+
const rest = targetBytes - total * plan.length;
|
|
6996
|
+
let count = 0;
|
|
6997
|
+
let bytes = 0;
|
|
6998
|
+
while (count < objects.length && bytes + objects[count].bytes < rest) bytes += objects[count++].bytes;
|
|
6999
|
+
if (count < objects.length && bytes + objects[count].bytes - rest <= rest - bytes) bytes += objects[count++].bytes;
|
|
7000
|
+
if (count === 0 && plan.length === 0) bytes += objects[count++].bytes;
|
|
7001
|
+
if (count > 0) plan.push(prefix(count, bytes));
|
|
7002
|
+
return plan;
|
|
7003
|
+
}
|
|
7004
|
+
/**
|
|
7005
|
+
* Link capacity a probe saw, in bytes per second: every byte that arrived from the request to the
|
|
7006
|
+
* last probe object, the probe's and `others`', over that time. Timing from the request, not the
|
|
7007
|
+
* first object, keeps bursts out: loss recovery releases held stream data at once, which squeezes
|
|
7008
|
+
* arrivals together. It costs about one RTT of underestimate.
|
|
7009
|
+
*/
|
|
7010
|
+
function probeEstimate(probe, others, requestedAt) {
|
|
7011
|
+
if (probe.length === 0) return 0;
|
|
7012
|
+
const last = probe.reduce((latest, { at }) => Math.max(latest, at), -Infinity);
|
|
7013
|
+
const within = others.filter(({ at }) => at > requestedAt && at <= last);
|
|
7014
|
+
return [...probe, ...within].reduce((sum, arrival) => sum + arrival.bytes, 0) / Math.max(last - requestedAt, .001);
|
|
7015
|
+
}
|
|
7016
|
+
/** Runs one probe at a time and keeps what the last `CONFIRMING` probes found. */
|
|
7017
|
+
var Prober = class {
|
|
7018
|
+
#inFlight;
|
|
7019
|
+
#estimates = [];
|
|
7020
|
+
/** When the last probe ended with an estimate; unlike `#endedAt`, forgotten with the estimates. */
|
|
7021
|
+
#estimatedAt;
|
|
7022
|
+
#failures = 0;
|
|
7023
|
+
#endedAt;
|
|
7024
|
+
get probing() {
|
|
7025
|
+
return this.#inFlight !== void 0;
|
|
7026
|
+
}
|
|
7027
|
+
/** The median of the recent estimates in bytes per second, if not forgotten. */
|
|
7028
|
+
get estimate() {
|
|
7029
|
+
return this.#estimates.length > 0 ? median(this.#estimates.map((e) => e.bytesPerSecond)) : void 0;
|
|
7030
|
+
}
|
|
7031
|
+
start(client, target, ranges, now) {
|
|
7032
|
+
const inFlight = {
|
|
7033
|
+
client,
|
|
7034
|
+
track: target.name,
|
|
7035
|
+
requestedAt: now,
|
|
7036
|
+
requestIds: /* @__PURE__ */ new Set(),
|
|
7037
|
+
fetches: ranges.length,
|
|
7038
|
+
abort: new AbortController(),
|
|
7039
|
+
arrivals: [],
|
|
7040
|
+
expectedObjects: ranges.reduce((sum, range) => sum + range.objects, 0),
|
|
7041
|
+
expectedBytes: ranges.reduce((sum, range) => sum + range.bytes, 0)
|
|
7042
|
+
};
|
|
7043
|
+
this.#inFlight = inFlight;
|
|
7044
|
+
for (const range of ranges) this.#fetch(inFlight, target.fullTrackName, range);
|
|
7045
|
+
}
|
|
7046
|
+
/**
|
|
7047
|
+
* Counts an object of a fetch stream: by request id, and by track until every FETCH_OK named its
|
|
7048
|
+
* request, since on a congested link FETCH_OK can come after the objects. Fill FETCHes of the same
|
|
7049
|
+
* track are not the probe's.
|
|
7050
|
+
*/
|
|
7051
|
+
received(track, requestId, arrival) {
|
|
7052
|
+
const inFlight = this.#inFlight;
|
|
7053
|
+
if (!inFlight) return;
|
|
7054
|
+
const ours = requestId !== void 0 && inFlight.requestIds.has(requestId);
|
|
7055
|
+
const unnamed = inFlight.requestIds.size < inFlight.fetches && track !== void 0 && track === inFlight.track;
|
|
7056
|
+
if (ours || unnamed) inFlight.arrivals.push(arrival);
|
|
7057
|
+
}
|
|
7058
|
+
/**
|
|
7059
|
+
* Ends the probe in flight once its objects all arrived, it fell behind its pace, or it timed out,
|
|
7060
|
+
* and returns its estimate. `others` are the live objects received meanwhile; `nextBitrate` is the
|
|
7061
|
+
* one probed for, undefined at the top of the ladder.
|
|
7062
|
+
*/
|
|
7063
|
+
settle(now, others, nextBitrate) {
|
|
7064
|
+
const inFlight = this.#inFlight;
|
|
7065
|
+
if (!inFlight) return void 0;
|
|
7066
|
+
const deadline = inFlight.requestedAt + PROBE_TIMEOUT;
|
|
7067
|
+
const arrivals = inFlight.arrivals.filter(({ at }) => at <= deadline);
|
|
7068
|
+
if (!(arrivals.length >= inFlight.expectedObjects) && !behindPace(inFlight, arrivals, now) && now < deadline) return void 0;
|
|
7069
|
+
this.cancel();
|
|
7070
|
+
const bytesPerSecond = probeEstimate(arrivals, others, inFlight.requestedAt);
|
|
7071
|
+
const room = nextBitrate === void 0 || bytesPerSecond * 8 >= nextBitrate;
|
|
7072
|
+
this.#failures = room ? 0 : this.#failures + 1;
|
|
7073
|
+
this.#estimates = [...this.#estimates, {
|
|
7074
|
+
bytesPerSecond,
|
|
7075
|
+
age: 0
|
|
7076
|
+
}].slice(-3);
|
|
7077
|
+
this.#estimatedAt = now;
|
|
7078
|
+
this.#endedAt = now;
|
|
7079
|
+
return bytesPerSecond;
|
|
7080
|
+
}
|
|
7081
|
+
/** Drops estimates the link has since contradicted. The failures, and so the backoff, stay. */
|
|
7082
|
+
forgetEstimates() {
|
|
7083
|
+
this.#estimates = [];
|
|
7084
|
+
this.#estimatedAt = void 0;
|
|
7085
|
+
}
|
|
7086
|
+
/** Ages the estimates as evidence; the controller skips it while a switch settles. */
|
|
7087
|
+
age(seconds) {
|
|
7088
|
+
for (const estimate of this.#estimates) estimate.age += seconds;
|
|
7089
|
+
}
|
|
7090
|
+
/** A move down: the link dropped, so probing again waits, without escalating the backoff. */
|
|
7091
|
+
steppedDown(now) {
|
|
7092
|
+
this.#failures = Math.max(this.#failures, 1);
|
|
7093
|
+
this.#endedAt = now;
|
|
7094
|
+
}
|
|
7095
|
+
/** A move up the ladder: what failed below says nothing about the next rendition. */
|
|
7096
|
+
forgetFailures() {
|
|
7097
|
+
this.#failures = 0;
|
|
7098
|
+
}
|
|
7099
|
+
/**
|
|
7100
|
+
* Cancels the probe in flight: the relay stops serving its FETCHes, and the client stops reading
|
|
7101
|
+
* their streams, so data already on its way stops too.
|
|
7102
|
+
*/
|
|
7103
|
+
cancel() {
|
|
7104
|
+
const inFlight = this.#inFlight;
|
|
7105
|
+
if (!inFlight) return;
|
|
7106
|
+
this.#inFlight = void 0;
|
|
7107
|
+
inFlight.abort.abort();
|
|
7108
|
+
for (const id of inFlight.requestIds) Promise.resolve(inFlight.client.fetchCancel(id)).catch(() => {});
|
|
7109
|
+
}
|
|
7110
|
+
observe(now) {
|
|
7111
|
+
const fresh = this.#estimates.filter(({ age }) => age < 10).map((e) => e.bytesPerSecond);
|
|
7112
|
+
return {
|
|
7113
|
+
probeEstimate: this.estimate ?? null,
|
|
7114
|
+
probeAge: this.#estimatedAt === void 0 ? null : now - this.#estimatedAt,
|
|
7115
|
+
evidence: fresh.length === 3 ? median(fresh) : null,
|
|
7116
|
+
probing: this.probing,
|
|
7117
|
+
probeFailures: this.#failures,
|
|
7118
|
+
sinceProbe: this.#endedAt === void 0 ? null : now - this.#endedAt
|
|
7119
|
+
};
|
|
7120
|
+
}
|
|
7121
|
+
async #fetch(inFlight, fullTrackName, { start, end }) {
|
|
7122
|
+
const locationFilter = new LocationFilter(4, start.group, start.object, end.group, end.object);
|
|
7123
|
+
try {
|
|
7124
|
+
const result = await inFlight.client.fetch({
|
|
7125
|
+
priority: PROBE_PRIORITY,
|
|
7126
|
+
groupOrder: 1,
|
|
7127
|
+
fullTrackName,
|
|
7128
|
+
locationFilter
|
|
7129
|
+
});
|
|
7130
|
+
if (!("stream" in result)) return;
|
|
7131
|
+
if (this.#inFlight !== inFlight) {
|
|
7132
|
+
result.stream.cancel().catch(() => {});
|
|
7133
|
+
Promise.resolve(inFlight.client.fetchCancel(result.requestId)).catch(() => {});
|
|
7134
|
+
return;
|
|
7135
|
+
}
|
|
7136
|
+
inFlight.requestIds.add(result.requestId);
|
|
7137
|
+
await result.stream.pipeTo(new WritableStream(), { signal: inFlight.abort.signal });
|
|
7138
|
+
} catch {}
|
|
7139
|
+
}
|
|
7140
|
+
};
|
|
7141
|
+
/** Under `PROBE_BEHIND` of the pace that would show room, once the round trip is allowed for. */
|
|
7142
|
+
function behindPace(inFlight, arrivals, now) {
|
|
7143
|
+
const elapsed = now - inFlight.requestedAt - PROBE_ALLOWANCE;
|
|
7144
|
+
const delivered = arrivals.reduce((sum, { bytes }) => sum + bytes, 0);
|
|
7145
|
+
return elapsed > 0 && delivered < PROBE_BEHIND * inFlight.expectedBytes / PROBE_SECONDS * elapsed;
|
|
7146
|
+
}
|
|
7147
|
+
function median(values) {
|
|
7148
|
+
const ordered = [...values].sort((a, b) => a - b);
|
|
7149
|
+
const middle = Math.floor(ordered.length / 2);
|
|
7150
|
+
return ordered.length % 2 === 1 ? ordered[middle] : (ordered[middle - 1] + ordered[middle]) / 2;
|
|
7151
|
+
}
|
|
7152
|
+
// src/player/plugins/abr/signals.ts
|
|
7153
|
+
/** Seconds of history kept. */
|
|
7154
|
+
const KEEP = 8;
|
|
7155
|
+
/** Seconds of per-second object counts a frame rate is inferred from, when the catalog has none. */
|
|
7156
|
+
const RATE_HISTORY = 30;
|
|
7157
|
+
/** Seconds a start may take before waiting for it counts as a stall. */
|
|
7158
|
+
const STARTUP_GRACE = 2;
|
|
7159
|
+
/** The shortest window a rate is measured over, one tick: a settle can shorten the window to it. */
|
|
7160
|
+
const SHORTEST_WINDOW = .5;
|
|
7161
|
+
/**
|
|
7162
|
+
* What the controller learns from the player's data events, its states, and the sink's buffer,
|
|
7163
|
+
* over the last few seconds. Times are in seconds, from `performance.now()`.
|
|
7164
|
+
*/
|
|
7165
|
+
var Signals = class {
|
|
7166
|
+
/** Video objects received in each whole second, for inferring the frame rate. */
|
|
7167
|
+
#perSecond = /* @__PURE__ */ new Map();
|
|
7168
|
+
/** The group each video track is receiving, by track name. */
|
|
7169
|
+
#receiving = /* @__PURE__ */ new Map();
|
|
7170
|
+
/** Open live streams of the video tracks: stream id → track. */
|
|
7171
|
+
#openStreams = /* @__PURE__ */ new Map();
|
|
7172
|
+
/** When each video track last delivered an object. */
|
|
7173
|
+
#lastObject = /* @__PURE__ */ new Map();
|
|
7174
|
+
#videoTracks = /* @__PURE__ */ new Map();
|
|
7175
|
+
#video = [];
|
|
7176
|
+
/** Every other live object, such as audio: it shares the link. */
|
|
7177
|
+
#others = [];
|
|
7178
|
+
#firstVideo;
|
|
7179
|
+
#known = [];
|
|
7180
|
+
#groupStarts = [];
|
|
7181
|
+
#stalls = [];
|
|
7182
|
+
#autoplayBlocked = false;
|
|
7183
|
+
/** Windowed signals count from here on: the last settled switch. */
|
|
7184
|
+
#measuredFrom = -Infinity;
|
|
7185
|
+
/** The tracks that carry video, each to the rendition it delivers: only their objects are measured as live media. */
|
|
7186
|
+
set videoTracks(heads) {
|
|
7187
|
+
this.#videoTracks = heads;
|
|
7188
|
+
}
|
|
7189
|
+
/** Stream ids are per client: after a reconnect the old client's streams never end. */
|
|
7190
|
+
forgetStreams() {
|
|
7191
|
+
this.#openStreams.clear();
|
|
7192
|
+
}
|
|
7193
|
+
/** A data event of a live stream, of `track` once the client knows it. */
|
|
7194
|
+
received(event, track) {
|
|
7195
|
+
if (event.kind === "end") this.#openStreams.delete(event.streamId);
|
|
7196
|
+
if (event.kind !== "object") return;
|
|
7197
|
+
const arrival = {
|
|
7198
|
+
at: event.at / 1e3,
|
|
7199
|
+
bytes: event.payloadLength
|
|
7200
|
+
};
|
|
7201
|
+
const head = track === void 0 ? void 0 : this.#videoTracks.get(track);
|
|
7202
|
+
if (head === void 0) {
|
|
7203
|
+
this.#others.push(arrival);
|
|
7204
|
+
return;
|
|
7205
|
+
}
|
|
7206
|
+
this.#video.push(arrival);
|
|
7207
|
+
this.#firstVideo ??= arrival.at;
|
|
7208
|
+
this.#openStreams.set(event.streamId, head);
|
|
7209
|
+
this.#lastObject.set(head, arrival.at);
|
|
7210
|
+
const second = Math.floor(arrival.at);
|
|
7211
|
+
this.#perSecond.set(second, (this.#perSecond.get(second) ?? 0) + 1);
|
|
7212
|
+
if (track !== head) return;
|
|
7213
|
+
const receiving = this.#receiving.get(track);
|
|
7214
|
+
if (receiving && event.groupId < receiving.group) {
|
|
7215
|
+
this.#lateObject(track, event);
|
|
7216
|
+
return;
|
|
7217
|
+
}
|
|
7218
|
+
if (receiving?.group !== event.groupId) this.#startGroup(track, event.groupId, arrival.at);
|
|
7219
|
+
this.#receiving.get(track).objects.set(event.objectId, event.payloadLength);
|
|
7220
|
+
}
|
|
7221
|
+
/**
|
|
7222
|
+
* Stalls by the player's state. Waiting to start counts too, after a grace period: a start the
|
|
7223
|
+
* link can't feed would otherwise leave the controller waiting for playback, and playback for it.
|
|
7224
|
+
*/
|
|
7225
|
+
playerState(state, now) {
|
|
7226
|
+
if (state === "playing") this.#autoplayBlocked = false;
|
|
7227
|
+
const waiting = state === "stalled" || state === "ready" && !this.#autoplayBlocked;
|
|
7228
|
+
const current = this.#stalls.at(-1);
|
|
7229
|
+
const open = current?.end === Infinity;
|
|
7230
|
+
if (waiting && !open) this.#stalls.push({
|
|
7231
|
+
start: state === "ready" ? now + STARTUP_GRACE : now,
|
|
7232
|
+
end: Infinity
|
|
7233
|
+
});
|
|
7234
|
+
if (!waiting && open) current.end = Math.max(now, current.start);
|
|
7235
|
+
}
|
|
7236
|
+
/** The viewer has to press play: waiting for that is not the network's doing. */
|
|
7237
|
+
autoplayBlocked(state, now) {
|
|
7238
|
+
this.#autoplayBlocked = true;
|
|
7239
|
+
this.playerState(state, now);
|
|
7240
|
+
}
|
|
7241
|
+
/** Forgets what is older than any signal looks back. */
|
|
7242
|
+
prune(now) {
|
|
7243
|
+
const recent = ({ at }) => at > now - KEEP;
|
|
7244
|
+
this.#video = this.#video.filter(recent);
|
|
7245
|
+
this.#others = this.#others.filter(recent);
|
|
7246
|
+
for (const second of this.#perSecond.keys()) if (second < now - RATE_HISTORY) this.#perSecond.delete(second);
|
|
7247
|
+
this.#stalls = this.#stalls.filter(({ end }) => end > now - KEEP);
|
|
7248
|
+
}
|
|
7249
|
+
/** Whether `WINDOW` seconds of live video arrived: until then, startup reads as congestion. */
|
|
7250
|
+
warmedUp(now) {
|
|
7251
|
+
return this.#firstVideo !== void 0 && now - this.#firstVideo >= 2;
|
|
7252
|
+
}
|
|
7253
|
+
stalledTime(now) {
|
|
7254
|
+
const from = now - 2;
|
|
7255
|
+
return this.#stalls.reduce((sum, { start, end }) => sum + Math.max(0, Math.min(end, now) - Math.max(start, from)), 0);
|
|
7256
|
+
}
|
|
7257
|
+
/** Whether `track` still has a live stream open: it hasn't delivered its last group yet. */
|
|
7258
|
+
streaming(track) {
|
|
7259
|
+
return [...this.#openStreams.values()].includes(track);
|
|
7260
|
+
}
|
|
7261
|
+
/** When `track` last delivered an object; -Infinity before it did. */
|
|
7262
|
+
lastObjectAt(track) {
|
|
7263
|
+
return this.#lastObject.get(track) ?? -Infinity;
|
|
7264
|
+
}
|
|
7265
|
+
/** Windowed signals count from `now` on: a switch settled, and what came before measured another track. */
|
|
7266
|
+
measureFrom(now) {
|
|
7267
|
+
this.#measuredFrom = now;
|
|
7268
|
+
}
|
|
7269
|
+
/** Live video bytes received in the measured window, scaled to a whole `WINDOW`. */
|
|
7270
|
+
receivedBytes(now) {
|
|
7271
|
+
return this.#bytesPerSecond(now, 2) * 2;
|
|
7272
|
+
}
|
|
7273
|
+
/**
|
|
7274
|
+
* Video objects received in the measured window over what `frameRate` delivers in it. Without a
|
|
7275
|
+
* frame rate from the catalog it uses the rate they usually arrive at, so only a slowdown reads as one.
|
|
7276
|
+
*/
|
|
7277
|
+
arrivalRate(now, frameRate) {
|
|
7278
|
+
const { start, length } = this.#window(now, 2);
|
|
7279
|
+
const expected = (frameRate ?? this.#usualRate(now)) * length;
|
|
7280
|
+
const received = this.#video.filter(({ at }) => at > start && at <= now).length;
|
|
7281
|
+
return expected > 0 ? received / expected : 1;
|
|
7282
|
+
}
|
|
7283
|
+
/**
|
|
7284
|
+
* Live video bytes per second, the slower of the last `RECENT` seconds and its second half: a drop
|
|
7285
|
+
* shows in the recent half first, and the whole second keeps a short gap between objects from
|
|
7286
|
+
* reading as one.
|
|
7287
|
+
*/
|
|
7288
|
+
throughput(now) {
|
|
7289
|
+
return Math.min(...[1, 1 / 2].map((seconds) => this.#bytesPerSecond(now, seconds)));
|
|
7290
|
+
}
|
|
7291
|
+
/** Frames arriving at under half their rate: the link is far behind. */
|
|
7292
|
+
starved(now, frameRate) {
|
|
7293
|
+
return this.warmedUp(now) && this.arrivalRate(now, frameRate) < .5;
|
|
7294
|
+
}
|
|
7295
|
+
/** Mean time between group starts; null until two are seen. */
|
|
7296
|
+
groupInterval() {
|
|
7297
|
+
const starts = this.#groupStarts;
|
|
7298
|
+
return starts.length > 1 ? (starts.at(-1) - starts[0]) / (starts.length - 1) : null;
|
|
7299
|
+
}
|
|
7300
|
+
/** The last one or two complete groups of the track that completed one last, oldest first. */
|
|
7301
|
+
knownGroups() {
|
|
7302
|
+
const last = this.#known.at(-1);
|
|
7303
|
+
return last ? this.#known.slice(-2).filter(({ track }) => track === last.track) : [];
|
|
7304
|
+
}
|
|
7305
|
+
/** Every live object, video and others, for measuring a probe against. */
|
|
7306
|
+
arrivals() {
|
|
7307
|
+
return [...this.#video, ...this.#others];
|
|
7308
|
+
}
|
|
7309
|
+
/** The last `seconds`, from the last settled switch on, and its length, at least `SHORTEST_WINDOW`. */
|
|
7310
|
+
#window(now, seconds) {
|
|
7311
|
+
const start = Math.max(now - seconds, this.#measuredFrom);
|
|
7312
|
+
return {
|
|
7313
|
+
start,
|
|
7314
|
+
length: Math.max(now - start, SHORTEST_WINDOW)
|
|
7315
|
+
};
|
|
7316
|
+
}
|
|
7317
|
+
#bytesPerSecond(now, seconds) {
|
|
7318
|
+
const { start, length } = this.#window(now, seconds);
|
|
7319
|
+
return this.#video.reduce((sum, { at, bytes }) => at > start && at <= now ? sum + bytes : sum, 0) / length;
|
|
7320
|
+
}
|
|
7321
|
+
/** A new group closes the one the track was receiving: its objects' sizes are now known. */
|
|
7322
|
+
#startGroup(track, group, at) {
|
|
7323
|
+
const previous = this.#receiving.get(track);
|
|
7324
|
+
if (previous) {
|
|
7325
|
+
const objects = [...previous.objects].map(([id, bytes]) => ({
|
|
7326
|
+
id,
|
|
7327
|
+
bytes
|
|
7328
|
+
})).sort(byId);
|
|
7329
|
+
this.#known = [...this.#known, {
|
|
7330
|
+
track,
|
|
7331
|
+
group: previous.group,
|
|
7332
|
+
objects
|
|
7333
|
+
}].slice(-2);
|
|
7334
|
+
}
|
|
7335
|
+
this.#receiving.set(track, {
|
|
7336
|
+
group,
|
|
7337
|
+
objects: /* @__PURE__ */ new Map()
|
|
7338
|
+
});
|
|
7339
|
+
this.#groupStarts = [...this.#groupStarts, at].slice(-5);
|
|
7340
|
+
}
|
|
7341
|
+
/** An object of a group a newer one already closed: added to it, if it is still known. */
|
|
7342
|
+
#lateObject(track, { groupId, objectId, payloadLength }) {
|
|
7343
|
+
this.#known = this.#known.map((known) => known.track === track && known.group === groupId ? {
|
|
7344
|
+
...known,
|
|
7345
|
+
objects: [...known.objects, {
|
|
7346
|
+
id: objectId,
|
|
7347
|
+
bytes: payloadLength
|
|
7348
|
+
}].sort(byId)
|
|
7349
|
+
} : known);
|
|
7350
|
+
}
|
|
7351
|
+
/**
|
|
7352
|
+
* Objects per second in a typical whole second of the last `RATE_HISTORY`: the 75th percentile,
|
|
7353
|
+
* so a congested stretch doesn't drag it down and a catch-up burst doesn't lift it.
|
|
7354
|
+
*/
|
|
7355
|
+
#usualRate(now) {
|
|
7356
|
+
const counts = [...this.#perSecond].filter(([second]) => second < Math.floor(now)).map(([, count]) => count).sort((a, b) => a - b);
|
|
7357
|
+
return counts.length > 0 ? counts[Math.floor((counts.length - 1) * .75)] : 0;
|
|
7358
|
+
}
|
|
7359
|
+
};
|
|
7360
|
+
function byId(a, b) {
|
|
7361
|
+
return a.id < b.id ? -1 : 1;
|
|
7362
|
+
}
|
|
6024
7363
|
// src/player/plugins/abr/controller.ts
|
|
6025
|
-
/**
|
|
6026
|
-
|
|
6027
|
-
|
|
6028
|
-
|
|
7364
|
+
/** Seconds after a switch before the requested rendition follows the one playing. */
|
|
7365
|
+
const RESYNC = 4;
|
|
7366
|
+
/**
|
|
7367
|
+
* @public
|
|
7368
|
+
* ABR for live playback: every tick a small neural network picks a switch down, a switch up, a probe
|
|
7369
|
+
* or nothing, among the actions `legalActions` allows.
|
|
7370
|
+
*/
|
|
7371
|
+
var MlAbrController = class {
|
|
7372
|
+
#tickMs;
|
|
7373
|
+
#onDecision;
|
|
7374
|
+
/** By rendition `src`. */
|
|
7375
|
+
#targets = /* @__PURE__ */ new Map();
|
|
7376
|
+
#policy;
|
|
7377
|
+
#session;
|
|
7378
|
+
constructor(tickMs, policy, onDecision) {
|
|
7379
|
+
this.#tickMs = tickMs;
|
|
7380
|
+
this.#onDecision = onDecision;
|
|
7381
|
+
policy.then((loaded) => {
|
|
7382
|
+
this.#policy = loaded;
|
|
7383
|
+
}, (error) => logger.error("abr", "the model failed to load, so ABR stays idle", error));
|
|
7384
|
+
}
|
|
7385
|
+
attach(player, sink) {
|
|
7386
|
+
this.detach();
|
|
7387
|
+
const signals = new Signals();
|
|
7388
|
+
const prober = new Prober();
|
|
7389
|
+
const onState = (state) => signals.playerState(state, seconds());
|
|
7390
|
+
const onAutoplayBlocked = () => signals.autoplayBlocked(player.state, seconds());
|
|
7391
|
+
player.on("statechange", onState);
|
|
7392
|
+
player.on("autoplayblocked", onAutoplayBlocked);
|
|
7393
|
+
onState(player.state);
|
|
7394
|
+
signals.videoTracks = this.#videoTracks(player);
|
|
7395
|
+
const unfollow = followData(player, (event, track, header) => {
|
|
7396
|
+
if (header.stream === "subgroup") signals.received(event, track);
|
|
7397
|
+
else if (event.kind === "object") prober.received(track, header.requestId, {
|
|
7398
|
+
at: event.at / 1e3,
|
|
7399
|
+
bytes: event.payloadLength
|
|
7400
|
+
});
|
|
7401
|
+
});
|
|
7402
|
+
const unclient = eachClient(player, () => {
|
|
7403
|
+
signals.forgetStreams();
|
|
7404
|
+
prober.cancel();
|
|
7405
|
+
return () => {};
|
|
7406
|
+
});
|
|
7407
|
+
const timer = setInterval(() => this.#tick(), this.#tickMs);
|
|
7408
|
+
this.#session = {
|
|
7409
|
+
player,
|
|
7410
|
+
sink,
|
|
7411
|
+
signals,
|
|
7412
|
+
prober,
|
|
7413
|
+
requested: void 0,
|
|
7414
|
+
switchedAt: -Infinity,
|
|
7415
|
+
settling: void 0,
|
|
7416
|
+
settledAt: -Infinity,
|
|
7417
|
+
troubleAt: -Infinity,
|
|
7418
|
+
stop: () => {
|
|
7419
|
+
clearInterval(timer);
|
|
7420
|
+
unfollow();
|
|
7421
|
+
unclient();
|
|
7422
|
+
player.off("statechange", onState);
|
|
7423
|
+
player.off("autoplayblocked", onAutoplayBlocked);
|
|
7424
|
+
}
|
|
7425
|
+
};
|
|
7426
|
+
}
|
|
7427
|
+
detach() {
|
|
7428
|
+
const session = this.#session;
|
|
7429
|
+
if (!session) return;
|
|
7430
|
+
this.#session = void 0;
|
|
7431
|
+
session.stop();
|
|
7432
|
+
session.prober.cancel();
|
|
7433
|
+
}
|
|
7434
|
+
#tick() {
|
|
7435
|
+
const session = this.#session;
|
|
7436
|
+
const policy = this.#policy;
|
|
7437
|
+
if (!session || !policy || session.player.state === "paused") return;
|
|
7438
|
+
const ladder = videoLadder(session.player);
|
|
7439
|
+
if (!ladder) return;
|
|
7440
|
+
const now = seconds();
|
|
7441
|
+
const { signals, prober } = session;
|
|
7442
|
+
signals.videoTracks = this.#videoTracks(session.player);
|
|
7443
|
+
signals.prune(now);
|
|
7444
|
+
if (!session.settling) prober.age(this.#tickMs / 1e3);
|
|
7445
|
+
const active = ladder.renditions.findIndex((r) => r.active);
|
|
7446
|
+
this.#settle(session, now);
|
|
7447
|
+
const stalledTime = signals.stalledTime(now);
|
|
7448
|
+
const requested = this.#requested(session, ladder, active, stalledTime, now);
|
|
7449
|
+
session.requested = requested;
|
|
7450
|
+
const playing = ladder.renditions[active === -1 ? requested : active];
|
|
7451
|
+
const arrivalRate = signals.arrivalRate(now, playing.frameRate);
|
|
7452
|
+
if (this.#contradicted(session, playing, stalledTime, now)) prober.forgetEstimates();
|
|
7453
|
+
const probeResult = prober.settle(now, signals.arrivals(), ladder.renditions[requested + 1]?.bitrate);
|
|
7454
|
+
if (stalledTime > 0 || arrivalRate < .8) session.troubleAt = now;
|
|
7455
|
+
const observation = this.#observe(session, ladder, requested, playing, {
|
|
7456
|
+
stalledTime,
|
|
7457
|
+
arrivalRate
|
|
7458
|
+
}, now);
|
|
7459
|
+
const inputs = features(observation);
|
|
7460
|
+
const reported = {
|
|
7461
|
+
observation,
|
|
7462
|
+
features: inputs,
|
|
7463
|
+
...probeResult !== void 0 && { probeResult }
|
|
7464
|
+
};
|
|
7465
|
+
if (!signals.warmedUp(now) && observation.stalledTime === 0) {
|
|
7466
|
+
this.#onDecision?.({
|
|
7467
|
+
...reported,
|
|
7468
|
+
action: Action.Stay,
|
|
7469
|
+
warmingUp: true
|
|
7470
|
+
});
|
|
7471
|
+
return;
|
|
7472
|
+
}
|
|
7473
|
+
const action = policy.choose(inputs, legalActions(observation));
|
|
7474
|
+
const switchedTo = action === Action.Up ? climbTarget(observation) : action === Action.Down ? downTarget(observation) : void 0;
|
|
7475
|
+
if (switchedTo !== void 0) this.#switch(session, ladder, requested, switchedTo, now);
|
|
7476
|
+
if (action === Action.Probe) this.#probe(session, ladder, requested, now);
|
|
7477
|
+
this.#onDecision?.({
|
|
7478
|
+
...reported,
|
|
7479
|
+
action,
|
|
7480
|
+
...switchedTo !== void 0 && { switchedTo }
|
|
7481
|
+
});
|
|
7482
|
+
}
|
|
7483
|
+
/**
|
|
7484
|
+
* The rendition to count from. A superseded switch can land after the one that replaced it, so
|
|
7485
|
+
* once switches had time to land, it follows the one playing; not while stalled, where no switch
|
|
7486
|
+
* lands and asking again only supersedes more requests.
|
|
7487
|
+
*/
|
|
7488
|
+
#requested(session, ladder, active, stalledTime, now) {
|
|
7489
|
+
const { requested, signals } = session;
|
|
7490
|
+
if (requested === void 0 || requested >= ladder.renditions.length) return Math.max(active, 0);
|
|
7491
|
+
const settled = now - session.switchedAt > Math.max(RESYNC, 2 * (signals.groupInterval() ?? 0));
|
|
7492
|
+
return active !== -1 && settled && stalledTime === 0 ? active : requested;
|
|
7493
|
+
}
|
|
7494
|
+
/**
|
|
7495
|
+
* A switch settled once every stream of the old track ended and the new track delivered. From then
|
|
7496
|
+
* on the windows measure the new track alone.
|
|
7497
|
+
*/
|
|
7498
|
+
#settle(session, now) {
|
|
7499
|
+
const { settling, signals } = session;
|
|
7500
|
+
if (!settling) return;
|
|
7501
|
+
if (!(!signals.streaming(settling.from) && signals.lastObjectAt(settling.to) > settling.since) && now - settling.since < 10) return;
|
|
7502
|
+
session.settling = void 0;
|
|
7503
|
+
session.settledAt = now;
|
|
7504
|
+
signals.measureFrom(now);
|
|
7505
|
+
}
|
|
7506
|
+
/**
|
|
7507
|
+
* A stall, or frames arriving at under half their rate, shows the link no longer carries a probe
|
|
7508
|
+
* estimate above the bitrate being received. One below it agrees, and keeps its backoff.
|
|
7509
|
+
*/
|
|
7510
|
+
#contradicted({ signals, prober }, playing, stalledTime, now) {
|
|
7511
|
+
const estimate = prober.estimate;
|
|
7512
|
+
return (stalledTime > 0 || signals.starved(now, playing.frameRate)) && estimate !== void 0 && estimate * 8 > playing.bitrate;
|
|
7513
|
+
}
|
|
7514
|
+
#observe(session, ladder, requested, playing, { stalledTime, arrivalRate }, now) {
|
|
7515
|
+
const { player, sink, signals, prober } = session;
|
|
7516
|
+
const { currentTime, buffered } = sink;
|
|
7517
|
+
const range = rangeAt(currentTime, buffered);
|
|
7518
|
+
return {
|
|
7519
|
+
forwardBuffer: range === -1 ? 0 : buffered.end(range) - currentTime,
|
|
7520
|
+
playbackRate: player.state === "playing" ? sink.playbackRate : 0,
|
|
7521
|
+
stalledTime,
|
|
7522
|
+
ladder: ladder.renditions.map((r) => r.bitrate),
|
|
7523
|
+
requested,
|
|
7524
|
+
activeBitrate: playing.bitrate,
|
|
7525
|
+
receivedBytes: signals.receivedBytes(now),
|
|
7526
|
+
throughput: signals.throughput(now),
|
|
7527
|
+
arrivalRate,
|
|
7528
|
+
...prober.observe(now),
|
|
7529
|
+
sinceSwitch: now - session.switchedAt,
|
|
7530
|
+
groupInterval: signals.groupInterval(),
|
|
7531
|
+
probeable: signals.knownGroups().length > 0,
|
|
7532
|
+
settling: session.settling !== void 0,
|
|
7533
|
+
sinceSettled: now - session.settledAt,
|
|
7534
|
+
sinceTrouble: now - session.troubleAt
|
|
7535
|
+
};
|
|
7536
|
+
}
|
|
7537
|
+
#switch(session, { source, renditions }, from, to, now) {
|
|
7538
|
+
session.requested = to;
|
|
7539
|
+
session.switchedAt = now;
|
|
7540
|
+
session.settling = {
|
|
7541
|
+
from: this.#target(renditions[from]).name,
|
|
7542
|
+
to: this.#target(renditions[to]).name,
|
|
7543
|
+
since: now
|
|
7544
|
+
};
|
|
7545
|
+
if (to > from) session.prober.forgetFailures();
|
|
7546
|
+
else session.prober.steppedDown(now);
|
|
7547
|
+
source.switch(renditions[to].id).catch(() => {});
|
|
7548
|
+
}
|
|
7549
|
+
#probe({ player, signals, prober }, { source, renditions }, requested, now) {
|
|
7550
|
+
const groups = signals.knownGroups();
|
|
7551
|
+
const probed = source.getRenditions().find((r) => this.#target(r).name === groups[0]?.track);
|
|
7552
|
+
const client = player.client;
|
|
7553
|
+
if (!client || !probed) return;
|
|
7554
|
+
const bytes = probeBytes(renditions[requested].bitrate, renditions[requested + 1].bitrate);
|
|
7555
|
+
prober.start(client, this.#target(probed), planProbe(groups, bytes), now);
|
|
7556
|
+
}
|
|
7557
|
+
#videoTracks(player) {
|
|
7558
|
+
const heads = /* @__PURE__ */ new Map();
|
|
7559
|
+
for (const r of videoSource(player)?.getRenditions() ?? []) {
|
|
7560
|
+
const head = this.#target(r).name;
|
|
7561
|
+
for (const member of r.ladder ?? [head]) heads.set(member, head);
|
|
7562
|
+
}
|
|
7563
|
+
return heads;
|
|
7564
|
+
}
|
|
7565
|
+
/** The track behind `rendition`, as a probe FETCHes it. */
|
|
7566
|
+
#target(rendition) {
|
|
7567
|
+
let target = this.#targets.get(rendition.src);
|
|
7568
|
+
if (!target) {
|
|
7569
|
+
const { track, namespace, trackBytes } = parseMsfUrl(rendition.src);
|
|
7570
|
+
target = {
|
|
7571
|
+
name: track,
|
|
7572
|
+
fullTrackName: FullTrackName.tryNew(namespace, trackBytes)
|
|
7573
|
+
};
|
|
7574
|
+
this.#targets.set(rendition.src, target);
|
|
7575
|
+
}
|
|
7576
|
+
return target;
|
|
7577
|
+
}
|
|
7578
|
+
};
|
|
7579
|
+
/** The source with video renditions, else the one with the most renditions. */
|
|
7580
|
+
function videoSource(player) {
|
|
7581
|
+
const sources = player.mediaSources;
|
|
7582
|
+
return sources.find((s) => s.getRenditions().some((r) => r.width !== void 0 || r.height !== void 0)) ?? [...sources].sort((a, b) => b.getRenditions().length - a.getRenditions().length)[0];
|
|
7583
|
+
}
|
|
7584
|
+
/** Undefined when there is nothing to choose between, or a bitrate is unknown. */
|
|
7585
|
+
function videoLadder(player) {
|
|
7586
|
+
const source = videoSource(player);
|
|
7587
|
+
const renditions = (source?.getRenditions() ?? []).filter((r) => r.selected).sort((a, b) => a.bitrate - b.bitrate);
|
|
7588
|
+
if (!source || renditions.length < 2 || renditions.some((r) => !(r.bitrate > 0))) return void 0;
|
|
7589
|
+
return {
|
|
7590
|
+
source,
|
|
7591
|
+
renditions
|
|
7592
|
+
};
|
|
7593
|
+
}
|
|
7594
|
+
function seconds() {
|
|
7595
|
+
return performance.now() / 1e3;
|
|
7596
|
+
}
|
|
7597
|
+
// src/player/plugins/abr/model.ts
|
|
7598
|
+
/** The bundled model: int4 weights, 220 characters. */
|
|
7599
|
+
const BUNDLED_MODEL = {
|
|
7600
|
+
codec: "raw",
|
|
7601
|
+
data: "ASb+evACDRCGNC4xEASlMocp4fLw8QHzEPHv8D7uATAAAP8ADgAhEAEQPwCh9v7vAw3/0PAAwOLx0AHwAfASEDvf4TzR8i8jIiAtAiBPEA8yPg7Q/fAA8Q4vIiAAEl4PL08jHx/+MdYyAnM7HQAdH/8BDQDNAP8A7wAB/gJDD9+5AD7yIT/PwtY13y/+3iMwdP9UJ/RO+TMcFSD+EhL+4bwP+Us="
|
|
7602
|
+
};
|
|
7603
|
+
// src/player/plugins/abr/policy.ts
|
|
7604
|
+
/**
|
|
7605
|
+
* Copyright 2026 The MOQtail Authors
|
|
7606
|
+
*
|
|
7607
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7608
|
+
* you may not use this file except in compliance with the License.
|
|
7609
|
+
* You may obtain a copy of the License at
|
|
7610
|
+
*
|
|
7611
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
7612
|
+
*
|
|
7613
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
7614
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
7615
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
7616
|
+
* See the License for the specific language governing permissions and
|
|
7617
|
+
* limitations under the License.
|
|
7618
|
+
*/
|
|
7619
|
+
/**
|
|
7620
|
+
* The model: a small multilayer perceptron with tanh hidden layers and int4 weights, shipped as a
|
|
7621
|
+
* few hundred bytes of base64. The packed form is
|
|
7622
|
+
*
|
|
7623
|
+
* u8 FEATURES_VERSION | u32le featuresHash() | u8 layer count
|
|
7624
|
+
* | per layer: u8 inputs, u8 outputs, f16le weight scale, f16le bias scale
|
|
7625
|
+
* | int4 codes, two per byte, low nibble first: per layer the weights row by row, then the biases
|
|
7626
|
+
*
|
|
7627
|
+
* optionally deflate-raw compressed, whichever is smaller.
|
|
7628
|
+
*/
|
|
7629
|
+
/** A decoded model: logits over `Action` for a feature vector. */
|
|
7630
|
+
var Policy = class Policy {
|
|
7631
|
+
#layers;
|
|
7632
|
+
constructor(layers) {
|
|
7633
|
+
this.#layers = layers;
|
|
7634
|
+
}
|
|
7635
|
+
/**
|
|
7636
|
+
* Decodes a blob.
|
|
7637
|
+
* @throws TypeError for a model trained on another feature list, which would read the inputs wrong.
|
|
7638
|
+
*/
|
|
7639
|
+
static async load(blob) {
|
|
7640
|
+
const packed = base64Bytes(blob.data);
|
|
7641
|
+
return new Policy(parseLayers(blob.codec === "deflate-raw" ? await inflate(packed) : packed));
|
|
7642
|
+
}
|
|
7643
|
+
logits(features) {
|
|
7644
|
+
let values = features;
|
|
7645
|
+
this.#layers.forEach((layer, index) => {
|
|
7646
|
+
const hidden = index < this.#layers.length - 1;
|
|
7647
|
+
values = applyLayer(layer, values).map((v) => hidden ? Math.tanh(v) : v);
|
|
7648
|
+
});
|
|
7649
|
+
return [...values];
|
|
7650
|
+
}
|
|
7651
|
+
/**
|
|
7652
|
+
* Draws an allowed action by the model's odds instead of taking the likeliest: some choices, such
|
|
7653
|
+
* as an occasional probe on a calm link, it learned only as odds. `random` returns numbers in [0, 1).
|
|
7654
|
+
*/
|
|
7655
|
+
choose(features, legal, random = Math.random) {
|
|
7656
|
+
const logits = this.logits(features);
|
|
7657
|
+
const top = Math.max(...logits.filter((_, action) => legal[action]));
|
|
7658
|
+
const weights = logits.map((logit, action) => legal[action] ? Math.exp(logit - top) : 0);
|
|
7659
|
+
let draw = random() * weights.reduce((sum, weight) => sum + weight, 0);
|
|
7660
|
+
for (let action = 0; action < weights.length; action++) {
|
|
7661
|
+
draw -= weights[action];
|
|
7662
|
+
if (draw < 0 && weights[action] > 0) return action;
|
|
7663
|
+
}
|
|
7664
|
+
return Action.Stay;
|
|
7665
|
+
}
|
|
6029
7666
|
};
|
|
7667
|
+
function applyLayer({ inputs, outputs, weights, biases }, values) {
|
|
7668
|
+
return Array.from({ length: outputs }, (_, row) => {
|
|
7669
|
+
let sum = biases[row];
|
|
7670
|
+
for (let column = 0; column < inputs; column++) sum += weights[row * inputs + column] * values[column];
|
|
7671
|
+
return sum;
|
|
7672
|
+
});
|
|
7673
|
+
}
|
|
7674
|
+
function base64Bytes(data) {
|
|
7675
|
+
return Uint8Array.from(atob(data), (char) => char.charCodeAt(0));
|
|
7676
|
+
}
|
|
7677
|
+
async function inflate(bytes) {
|
|
7678
|
+
const stream = new Blob([bytes]).stream().pipeThrough(new DecompressionStream("deflate-raw"));
|
|
7679
|
+
return new Uint8Array(await new Response(stream).arrayBuffer());
|
|
7680
|
+
}
|
|
7681
|
+
function parseLayers(bytes) {
|
|
7682
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
7683
|
+
if (view.getUint8(0) !== 1 || view.getUint32(1, true) !== featuresHash()) throw new TypeError("The model was trained on another feature list");
|
|
7684
|
+
const shapes = Array.from({ length: view.getUint8(5) }, (_, index) => {
|
|
7685
|
+
const at = 6 + index * 6;
|
|
7686
|
+
return {
|
|
7687
|
+
inputs: view.getUint8(at),
|
|
7688
|
+
outputs: view.getUint8(at + 1),
|
|
7689
|
+
weightScale: float16(view.getUint16(at + 2, true)),
|
|
7690
|
+
biasScale: float16(view.getUint16(at + 4, true))
|
|
7691
|
+
};
|
|
7692
|
+
});
|
|
7693
|
+
const codes = int4Codes(bytes, 6 + shapes.length * 6);
|
|
7694
|
+
return shapes.map(({ inputs, outputs, weightScale, biasScale }) => ({
|
|
7695
|
+
inputs,
|
|
7696
|
+
outputs,
|
|
7697
|
+
weights: Float64Array.from({ length: inputs * outputs }, () => codes.next() * weightScale),
|
|
7698
|
+
biases: Float64Array.from({ length: outputs }, () => codes.next() * biasScale)
|
|
7699
|
+
}));
|
|
7700
|
+
}
|
|
7701
|
+
/** Reads signed 4-bit codes from `offset` on, two per byte, low nibble first. */
|
|
7702
|
+
function int4Codes(bytes, offset) {
|
|
7703
|
+
let nibble = offset * 2;
|
|
7704
|
+
return { next() {
|
|
7705
|
+
const byte = bytes[nibble >> 1];
|
|
7706
|
+
const code = nibble % 2 === 0 ? byte & 15 : byte >> 4;
|
|
7707
|
+
nibble++;
|
|
7708
|
+
return code >= 8 ? code - 16 : code;
|
|
7709
|
+
} };
|
|
7710
|
+
}
|
|
7711
|
+
/** IEEE 754 half precision; `DataView.getFloat16` is too new to rely on. Scales are finite. */
|
|
7712
|
+
function float16(bits) {
|
|
7713
|
+
const sign = bits & 32768 ? -1 : 1;
|
|
7714
|
+
const exponent = bits >> 10 & 31;
|
|
7715
|
+
const fraction = bits & 1023;
|
|
7716
|
+
return exponent === 0 ? sign * fraction * 2 ** -24 : sign * (1 + fraction / 1024) * 2 ** (exponent - 15);
|
|
7717
|
+
}
|
|
6030
7718
|
// src/player/plugins/abr/index.ts
|
|
6031
7719
|
/**
|
|
6032
7720
|
* Copyright 2026 The MOQtail Authors
|
|
@@ -6043,15 +7731,20 @@ var DefaultAbrController = class {
|
|
|
6043
7731
|
* See the License for the specific language governing permissions and
|
|
6044
7732
|
* limitations under the License.
|
|
6045
7733
|
*/
|
|
7734
|
+
/** Decoded once, then shared by every player. */
|
|
7735
|
+
let bundled;
|
|
6046
7736
|
/**
|
|
6047
7737
|
* @public
|
|
6048
|
-
* An ABR controller
|
|
7738
|
+
* An ABR controller driven by a small neural network, for live streams. Priority `Infinity`, so any
|
|
6049
7739
|
* other active ABR plugin wins over it.
|
|
6050
7740
|
*/
|
|
6051
7741
|
const abr = definePlugin({
|
|
6052
7742
|
name: "abr",
|
|
6053
7743
|
priority: Infinity,
|
|
6054
|
-
init: () =>
|
|
7744
|
+
init: ({ tickMs = 500, model, onDecision } = {}) => {
|
|
7745
|
+
if (!(tickMs > 0 && tickMs < Infinity)) throw new TypeError("tickMs must be a positive number");
|
|
7746
|
+
return new MlAbrController(tickMs, model ? Policy.load(model) : bundled ??= Policy.load(BUNDLED_MODEL), onDecision);
|
|
7747
|
+
},
|
|
6055
7748
|
features: (controller) => [{
|
|
6056
7749
|
type: "abr-controller",
|
|
6057
7750
|
item: controller
|
|
@@ -6298,10 +7991,11 @@ var SstsSource = class extends SourceElement {
|
|
|
6298
7991
|
this.#selected = new Set(context.tracks.map((t) => this.#id(t)));
|
|
6299
7992
|
this.algorithms = sstsAlgorithms(context.client.serverSetup, preferred);
|
|
6300
7993
|
this.#algorithm = this.algorithms[0] ?? 0n;
|
|
7994
|
+
const packaging = options.child.capabilities.flat().find((c) => c.direction === "out" && c.code === 0)?.value;
|
|
6301
7995
|
this.pad = this.createPad("src", [{
|
|
6302
7996
|
direction: "out",
|
|
6303
7997
|
code: 0,
|
|
6304
|
-
value: first.packaging
|
|
7998
|
+
value: packaging ?? first.packaging
|
|
6305
7999
|
}, {
|
|
6306
8000
|
direction: "out",
|
|
6307
8001
|
code: 1,
|
|
@@ -6596,4 +8290,4 @@ const ssts = definePlugin({
|
|
|
6596
8290
|
item: sstsSource(algorithms)
|
|
6597
8291
|
}]
|
|
6598
8292
|
});
|
|
6599
|
-
export {
|
|
8293
|
+
export { CapabilityCode as C, formatMsfUrl as D, hasInterface as E, parseMsfUrl as O, Pad as S, defineElement as T, BaseElement as _, MlAbrController as a, TransformElement as b, eachClient as c, definePlugin as d, MSE as f, MEDIA_SOURCE as g, MEDIA_SINK as h, abr as i, PlayerError as k, followData as l, ABR as m, SstsSource as n, Action as o, rangeAt as p, SSTS_ALGORITHMS as r, liveBuffer as s, ssts as t, MOQtailPlayer as u, SinkElement as v, DEFAULT_PRIORITY as w, CONNECTION_CAPACITY as x, SourceElement as y };
|