@blockcast/mmt-transport 2.1.0 → 2.2.0-main.01db56d91e37
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/dist/amt-gateway.d.ts +3 -0
- package/dist/amt-gateway.d.ts.map +1 -1
- package/dist/amt-gateway.js +30 -17
- package/dist/amt-gateway.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/mmt-transport.no-modules.js +267 -5
- package/dist/mmt-transport.no-modules.js.map +3 -3
- package/dist/multicast-receiver-gateway.d.ts +43 -0
- package/dist/multicast-receiver-gateway.d.ts.map +1 -0
- package/dist/multicast-receiver-gateway.js +251 -0
- package/dist/multicast-receiver-gateway.js.map +1 -0
- package/dist/multicast-signaling-reconciler.js +7 -2
- package/dist/multicast-signaling-reconciler.js.map +1 -1
- package/dist/no-modules.d.ts +6 -0
- package/dist/no-modules.d.ts.map +1 -1
- package/dist/no-modules.js +6 -0
- package/dist/no-modules.js.map +1 -1
- package/package.json +2 -2
- package/src/amt-gateway.ts +31 -19
- package/src/index.ts +7 -0
- package/src/multicast-receiver-gateway.ts +291 -0
- package/src/multicast-signaling-reconciler.ts +7 -2
- package/src/no-modules.ts +12 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The browser's own `MulticastReceiver` (Chromium and Firefox, branch
|
|
3
|
+
* `mcast-hackathon`) behind the `window.multicast` gateway surface.
|
|
4
|
+
*
|
|
5
|
+
* The user agent verifies the in-band bc-provenance v2 (`MHM2`) records
|
|
6
|
+
* itself and delivers only verified data, so the records never reach the
|
|
7
|
+
* page. A datagram from this provider is therefore already admitted: the
|
|
8
|
+
* consumer marks its ingress with {@link userAgentVerifiedIngress} and the
|
|
9
|
+
* MMTP router in `@blockcast/mmt-container` reads it as admitted.
|
|
10
|
+
*
|
|
11
|
+
* libmmt bundles this file into several copies that one document loads (each
|
|
12
|
+
* container `dist` entry inlines it, beside transport's own `dist` and its
|
|
13
|
+
* no-modules IIFE). The provider identity therefore lives in one realm-wide
|
|
14
|
+
* slot keyed by `Symbol.for`, installed by whichever copy runs first, so every
|
|
15
|
+
* copy sees one provider, and one open gate, per scope.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { readMMTPPacketId } from "@blockcast/mmt-base";
|
|
19
|
+
import type {
|
|
20
|
+
WindowMulticastGateway,
|
|
21
|
+
WindowMulticastSubscribeConfig,
|
|
22
|
+
WindowMulticastSubscription,
|
|
23
|
+
WindowMulticastSubscriptionState,
|
|
24
|
+
} from "./types.js";
|
|
25
|
+
|
|
26
|
+
// `/v1` is the provider contract's version: a copy with a changed contract
|
|
27
|
+
// takes a new key, so it never shares another version's provider.
|
|
28
|
+
// ponytail: one version per page in the player origin (all its bundles come
|
|
29
|
+
// from pim's one libmmt gitlink); a version handshake only if mixed versions
|
|
30
|
+
// ever share a page.
|
|
31
|
+
const REGISTRY = Symbol.for("@blockcast/mmt-transport/native-provider-registry/v1");
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The mark an ingress carries in `admittedBy` when its datagram came from the
|
|
35
|
+
* user agent's own `MulticastReceiver`. A symbol, so no structured-cloned or
|
|
36
|
+
* serialised data can carry it.
|
|
37
|
+
*/
|
|
38
|
+
export const USER_AGENT_VERIFIED: unique symbol = Symbol.for(
|
|
39
|
+
"@blockcast/mmt-transport/user-agent-verified/v1",
|
|
40
|
+
) as never;
|
|
41
|
+
|
|
42
|
+
/** The one integrity scheme this provider vouches for. */
|
|
43
|
+
const INTEGRITY_SCHEME = "manifest-ed25519-2";
|
|
44
|
+
|
|
45
|
+
/** A multicast-contract error: `code` names the outcome, `cause` the DOMException. */
|
|
46
|
+
export class MulticastError extends Error {
|
|
47
|
+
readonly code: "capability-unsupported" | "subscription-failed" | "origin-not-trusted";
|
|
48
|
+
constructor(code: MulticastError["code"], message: string, cause?: unknown) {
|
|
49
|
+
super(message, cause === undefined ? undefined : { cause });
|
|
50
|
+
this.name = "MulticastError";
|
|
51
|
+
this.code = code;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ponytail: the chunk and close-reason shapes are read tolerantly (array or
|
|
56
|
+
// `{datagrams}`, `Uint8Array` or `{data}`, string or `{reason}`); pin them to
|
|
57
|
+
// the prototype IDL once it is published.
|
|
58
|
+
function datagramsOf(chunk: unknown): Uint8Array[] {
|
|
59
|
+
const list = Array.isArray(chunk) ? chunk : (chunk as { datagrams?: unknown[] } | null)?.datagrams ?? [];
|
|
60
|
+
const out: Uint8Array[] = [];
|
|
61
|
+
for (const d of list) {
|
|
62
|
+
const data = d instanceof Uint8Array || d instanceof ArrayBuffer ? d : (d as { data?: unknown } | null)?.data;
|
|
63
|
+
if (data instanceof Uint8Array) out.push(data);
|
|
64
|
+
else if (data instanceof ArrayBuffer) out.push(new Uint8Array(data));
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function reasonOf(value: unknown): unknown {
|
|
70
|
+
return typeof value === "object" && value !== null && "reason" in value ? (value as { reason: unknown }).reason : value;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** MulticastReceiverOpenInfo: the scheme lives here, not on the receiver (BLO-54074). */
|
|
74
|
+
interface ReceiverOpenInfo {
|
|
75
|
+
readonly readable: ReadableStream<unknown>;
|
|
76
|
+
readonly maxBitrate?: number;
|
|
77
|
+
readonly integrityScheme?: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface Receiver {
|
|
81
|
+
readonly opened: Promise<ReceiverOpenInfo>;
|
|
82
|
+
readonly closed: Promise<unknown>;
|
|
83
|
+
close(): Promise<void> | void;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
type ReceiverConstructor = new (options: {
|
|
87
|
+
sourceAddress: string;
|
|
88
|
+
groupAddress: string;
|
|
89
|
+
localPort: number;
|
|
90
|
+
}) => Receiver;
|
|
91
|
+
|
|
92
|
+
interface NativeProviderRegistry {
|
|
93
|
+
isProvider(gateway: unknown): boolean;
|
|
94
|
+
providerFor(scope: object): WindowMulticastGateway;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function registry(): NativeProviderRegistry {
|
|
98
|
+
const g = globalThis as unknown as Record<symbol, NativeProviderRegistry | undefined>;
|
|
99
|
+
if (g[REGISTRY] === undefined) {
|
|
100
|
+
const providers = new WeakSet<object>(); // closure-private: no add() is ever exposed
|
|
101
|
+
const byScope = new WeakMap<object, WindowMulticastGateway>();
|
|
102
|
+
Object.defineProperty(g, REGISTRY, {
|
|
103
|
+
value: Object.freeze({
|
|
104
|
+
isProvider: (x: unknown) => typeof x === "object" && x !== null && providers.has(x),
|
|
105
|
+
providerFor: (scope: object) => {
|
|
106
|
+
let provider = byScope.get(scope);
|
|
107
|
+
if (provider === undefined) {
|
|
108
|
+
provider = createProvider(scope);
|
|
109
|
+
providers.add(provider);
|
|
110
|
+
byScope.set(scope, provider);
|
|
111
|
+
}
|
|
112
|
+
return provider;
|
|
113
|
+
},
|
|
114
|
+
}),
|
|
115
|
+
writable: false,
|
|
116
|
+
configurable: false,
|
|
117
|
+
enumerable: false,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
return g[REGISTRY]!;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The multicast gateway for `scope`: the user agent's `MulticastReceiver`
|
|
125
|
+
* when it exists (one provider per scope for the realm), else
|
|
126
|
+
* `scope.multicast`, else undefined.
|
|
127
|
+
*/
|
|
128
|
+
export function resolveMulticastGateway(scope: object = globalThis): WindowMulticastGateway | undefined {
|
|
129
|
+
const s = scope as { MulticastReceiver?: unknown; multicast?: WindowMulticastGateway };
|
|
130
|
+
if (typeof s.MulticastReceiver === "function") return registry().providerFor(scope);
|
|
131
|
+
if (typeof s.multicast?.subscribe === "function") return s.multicast;
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Whether `gateway` is a native `MulticastReceiver` provider of this realm. */
|
|
136
|
+
export function isUserAgentVerifiedGateway(gateway: unknown): boolean {
|
|
137
|
+
return registry().isProvider(gateway);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* `ingress` marked as admitted by the user agent, when `gateway` is a native
|
|
142
|
+
* provider; otherwise `ingress` without an `admittedBy` field (itself when it
|
|
143
|
+
* has none). Never copies a mark from anywhere: this is its only source.
|
|
144
|
+
*/
|
|
145
|
+
export function userAgentVerifiedIngress<T extends object>(ingress: T, gateway: unknown): T {
|
|
146
|
+
if (isUserAgentVerifiedGateway(gateway)) return { ...ingress, admittedBy: USER_AGENT_VERIFIED };
|
|
147
|
+
if (!("admittedBy" in ingress)) return ingress;
|
|
148
|
+
const { admittedBy: _, ...rest } = ingress as T & { admittedBy?: unknown };
|
|
149
|
+
return rest as T;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function unsupported(message: string, cause?: unknown): MulticastError {
|
|
153
|
+
return new MulticastError("capability-unsupported", message, cause);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function createProvider(scope: object): WindowMulticastGateway {
|
|
157
|
+
// Captured once: a later reassignment never reaches this provider.
|
|
158
|
+
const Receiver = (scope as { MulticastReceiver: ReceiverConstructor }).MulticastReceiver;
|
|
159
|
+
// The first construction runs alone: Gecko consumes transient activation
|
|
160
|
+
// on every open, and after the first grant neither browser needs it.
|
|
161
|
+
let gate: Promise<unknown> | null = null;
|
|
162
|
+
return {
|
|
163
|
+
capabilities: () => Promise.resolve({ consumeModes: ["bytes"] }),
|
|
164
|
+
subscribe: async (config: WindowMulticastSubscribeConfig) => {
|
|
165
|
+
const c = config as WindowMulticastSubscribeConfig & {
|
|
166
|
+
relay?: string;
|
|
167
|
+
discovery?: string;
|
|
168
|
+
senderAddress?: string;
|
|
169
|
+
};
|
|
170
|
+
if (c.consume !== "bytes") throw unsupported(`consume mode ${String(c.consume)} is not supported`);
|
|
171
|
+
const origin = c.origin ?? "ip-multicast";
|
|
172
|
+
if (origin !== "ip-multicast" && !(origin === "amt" && c.discovery === "driad")) {
|
|
173
|
+
throw unsupported(`origin ${String(origin)} is not supported by MulticastReceiver`);
|
|
174
|
+
}
|
|
175
|
+
if (c.relay !== undefined) throw unsupported("MulticastReceiver picks its own AMT relay");
|
|
176
|
+
if (!c.source) throw unsupported("MulticastReceiver needs an SSM source");
|
|
177
|
+
if (c.senderAddress !== undefined && c.senderAddress !== c.source) {
|
|
178
|
+
throw unsupported("MulticastReceiver filters by source only");
|
|
179
|
+
}
|
|
180
|
+
const options = { sourceAddress: c.source, groupAddress: c.group, localPort: c.port };
|
|
181
|
+
if (gate !== null) await gate;
|
|
182
|
+
let rx: Receiver;
|
|
183
|
+
try {
|
|
184
|
+
rx = new Receiver(options);
|
|
185
|
+
} catch (error) {
|
|
186
|
+
throw unsupported("MulticastReceiver refused the request", error);
|
|
187
|
+
}
|
|
188
|
+
const opened = rx.opened;
|
|
189
|
+
if (gate === null) gate = opened.then(() => undefined, () => undefined);
|
|
190
|
+
return openSubscription(rx, opened, options, c.packetIds);
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async function openSubscription(
|
|
196
|
+
rx: Receiver,
|
|
197
|
+
openedPromise: Receiver["opened"],
|
|
198
|
+
options: { sourceAddress: string; groupAddress: string; localPort: number },
|
|
199
|
+
packetIds: readonly number[] | undefined,
|
|
200
|
+
): Promise<WindowMulticastSubscription> {
|
|
201
|
+
let opened: Awaited<Receiver["opened"]>;
|
|
202
|
+
try {
|
|
203
|
+
opened = await openedPromise;
|
|
204
|
+
} catch (error) {
|
|
205
|
+
throw new MulticastError("subscription-failed", "MulticastReceiver did not open", error);
|
|
206
|
+
}
|
|
207
|
+
if (opened.integrityScheme !== INTEGRITY_SCHEME) {
|
|
208
|
+
await rx.close();
|
|
209
|
+
throw unsupported(`integrity scheme ${String(opened.integrityScheme)} is not ${INTEGRITY_SCHEME}`);
|
|
210
|
+
}
|
|
211
|
+
const events = new EventTarget();
|
|
212
|
+
let state: WindowMulticastSubscriptionState = "connected";
|
|
213
|
+
let filter = packetIds?.length ? new Set(packetIds) : null;
|
|
214
|
+
let closing = false;
|
|
215
|
+
const setState = (next: WindowMulticastSubscriptionState) => {
|
|
216
|
+
if (state === next) return;
|
|
217
|
+
state = next;
|
|
218
|
+
events.dispatchEvent(new CustomEvent("state", { detail: next }));
|
|
219
|
+
};
|
|
220
|
+
const sourceAddress = options.sourceAddress;
|
|
221
|
+
const subscription = {
|
|
222
|
+
id: `${options.sourceAddress}|${options.groupAddress}|${options.localPort}`,
|
|
223
|
+
get state() {
|
|
224
|
+
return state;
|
|
225
|
+
},
|
|
226
|
+
delivery: "datagram" as const,
|
|
227
|
+
eventTypes: Object.freeze(["bytes", "packets", "state", "error"]),
|
|
228
|
+
setPacketIds(ids: readonly number[]) {
|
|
229
|
+
filter = ids.length ? new Set(ids) : null;
|
|
230
|
+
},
|
|
231
|
+
addEventListener: events.addEventListener.bind(events),
|
|
232
|
+
removeEventListener: events.removeEventListener.bind(events),
|
|
233
|
+
async unsubscribe() {
|
|
234
|
+
closing = true;
|
|
235
|
+
await rx.close();
|
|
236
|
+
setState("closed");
|
|
237
|
+
},
|
|
238
|
+
} as unknown as WindowMulticastSubscription;
|
|
239
|
+
|
|
240
|
+
void (async () => {
|
|
241
|
+
const reader = opened.readable.getReader();
|
|
242
|
+
try {
|
|
243
|
+
for (;;) {
|
|
244
|
+
const { value, done } = await reader.read();
|
|
245
|
+
if (done) break;
|
|
246
|
+
const packets: ArrayBuffer[] = [];
|
|
247
|
+
for (const data of datagramsOf(value)) {
|
|
248
|
+
if (filter !== null) {
|
|
249
|
+
const id = readMMTPPacketId(data);
|
|
250
|
+
if (id === null || !filter.has(id)) continue;
|
|
251
|
+
}
|
|
252
|
+
// A chunk's datagrams are views into one ArrayBuffer.
|
|
253
|
+
// ponytail: one copy per datagram; zero-copy only if a profile shows it.
|
|
254
|
+
const copy = data.slice().buffer as ArrayBuffer;
|
|
255
|
+
packets.push(copy);
|
|
256
|
+
events.dispatchEvent(Object.assign(new CustomEvent("bytes", { detail: copy }), { sourceAddress }));
|
|
257
|
+
}
|
|
258
|
+
if (packets.length > 0) events.dispatchEvent(new CustomEvent("packets", { detail: packets }));
|
|
259
|
+
}
|
|
260
|
+
} catch {
|
|
261
|
+
// The receiver closed; `closed` reports why.
|
|
262
|
+
}
|
|
263
|
+
})();
|
|
264
|
+
|
|
265
|
+
void rx.closed.then(
|
|
266
|
+
(value) => {
|
|
267
|
+
const reason = reasonOf(value);
|
|
268
|
+
if (!closing) {
|
|
269
|
+
if (reason === "consent-withdrawn") {
|
|
270
|
+
events.dispatchEvent(new CustomEvent("error", {
|
|
271
|
+
detail: new MulticastError("origin-not-trusted", "multicast consent withdrawn"),
|
|
272
|
+
}));
|
|
273
|
+
} else if (reason === "oversubscribed" || reason === "network") {
|
|
274
|
+
events.dispatchEvent(new CustomEvent("error", {
|
|
275
|
+
detail: new MulticastError("subscription-failed", `MulticastReceiver closed: ${reason}`),
|
|
276
|
+
}));
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
setState("closed");
|
|
280
|
+
},
|
|
281
|
+
(error) => {
|
|
282
|
+
if (!closing) {
|
|
283
|
+
events.dispatchEvent(new CustomEvent("error", {
|
|
284
|
+
detail: new MulticastError("subscription-failed", "MulticastReceiver failed", error),
|
|
285
|
+
}));
|
|
286
|
+
}
|
|
287
|
+
setState("closed");
|
|
288
|
+
},
|
|
289
|
+
);
|
|
290
|
+
return subscription;
|
|
291
|
+
}
|
|
@@ -183,11 +183,16 @@ function isValidEndpoint(endpoint: MulticastSignalingEndpoint, catalog?: Catalog
|
|
|
183
183
|
// it, and "amt" / "atsc3" are bearers, not protocols.
|
|
184
184
|
if (!multicastEndpointSource(endpoint)) return false;
|
|
185
185
|
if (!isValidMulticastGroupAddress(endpoint.groupAddress) || !Number.isInteger(endpoint.port) || endpoint.port <= 0 || endpoint.port > 65535 || !endpoint.tracks.length) return false;
|
|
186
|
-
const
|
|
186
|
+
const renditions = [catalog?.video?.renditions, catalog?.audio?.renditions].map(value => value ?? {});
|
|
187
|
+
const catalogTracks = new Set(renditions.flatMap(value => Object.keys(value)));
|
|
188
|
+
// A repair track a rendition's FEC instance names -- base or the keyframe
|
|
189
|
+
// overlay, whose `<track>/repair/keyframe` (BLO-52083) the suffix rule
|
|
190
|
+
// below does not strip -- is a catalog track too.
|
|
191
|
+
const fecRepairTracks = new Set(renditions.flatMap(value => Object.values(value).flatMap((rendition: any) => [rendition?.fec].flat().map(instance => instance?.repairTrack))).filter(name => typeof name === "string"));
|
|
187
192
|
// Every track must carry a valid packet_id (isMulticastTrackPacketId); one
|
|
188
193
|
// that does not -- including packet_id 0, the MMTP signaling flow --
|
|
189
194
|
// rejects the whole endpoint rather than silently dropping that track.
|
|
190
|
-
return endpoint.tracks.every(track => !!track.name && isMulticastTrackPacketId(track.packetId) && (catalogTracks.size === 0 || catalogTracks.has(track.name) || catalogTracks.has(track.name.replace(/\/repair(?:\/\d+)?$/, ""))));
|
|
195
|
+
return endpoint.tracks.every(track => !!track.name && isMulticastTrackPacketId(track.packetId) && (catalogTracks.size === 0 || catalogTracks.has(track.name) || fecRepairTracks.has(track.name) || catalogTracks.has(track.name.replace(/\/repair(?:\/\d+)?$/, ""))));
|
|
191
196
|
}
|
|
192
197
|
|
|
193
198
|
function copyEndpoint(endpoint: MulticastSignalingEndpoint): MulticastSignalingEndpoint {
|
package/src/no-modules.ts
CHANGED
|
@@ -33,6 +33,13 @@ import {
|
|
|
33
33
|
createMulticastSignalingReconciler,
|
|
34
34
|
normalizeCatalogEndpoints,
|
|
35
35
|
} from "./multicast-signaling-reconciler.js";
|
|
36
|
+
import {
|
|
37
|
+
isUserAgentVerifiedGateway,
|
|
38
|
+
MulticastError,
|
|
39
|
+
resolveMulticastGateway,
|
|
40
|
+
USER_AGENT_VERIFIED,
|
|
41
|
+
userAgentVerifiedIngress,
|
|
42
|
+
} from "./multicast-receiver-gateway.js";
|
|
36
43
|
import { MulticastSubscriptionPool } from "./multicast-subscription-pool.js";
|
|
37
44
|
import { endpointKey, selectMulticastTier } from "./multicast-tier.js";
|
|
38
45
|
import {
|
|
@@ -58,6 +65,8 @@ const api = {
|
|
|
58
65
|
hasUnicastMediaRenditions,
|
|
59
66
|
isRecoverableMulticastSignalReadError,
|
|
60
67
|
isMulticastOnlyCatalog,
|
|
68
|
+
isUserAgentVerifiedGateway,
|
|
69
|
+
MulticastError,
|
|
61
70
|
MulticastOrchestrator,
|
|
62
71
|
MulticastSubscriptionPool,
|
|
63
72
|
MOQT_PUBLISHER_PRIORITY,
|
|
@@ -78,6 +87,7 @@ const api = {
|
|
|
78
87
|
normalizeMulticastEndpoint,
|
|
79
88
|
readPlayerMulticastConfig,
|
|
80
89
|
readUrlMulticastConfig,
|
|
90
|
+
resolveMulticastGateway,
|
|
81
91
|
toWindowMulticastSubscribeConfig,
|
|
82
92
|
requiresMulticastCapability,
|
|
83
93
|
runUnicastFallback,
|
|
@@ -87,6 +97,8 @@ const api = {
|
|
|
87
97
|
selectMulticastTier,
|
|
88
98
|
selectRepairEndpointsForTracks,
|
|
89
99
|
urlOverrideBlocksMulticastCandidate,
|
|
100
|
+
USER_AGENT_VERIFIED,
|
|
101
|
+
userAgentVerifiedIngress,
|
|
90
102
|
withCatalogNamespace,
|
|
91
103
|
withMulticastEndpoints,
|
|
92
104
|
wireStarvationDetector,
|