@blockcast/mmt-base 2.2.0-main.2c4d96d3ffc2 → 2.2.0-main.31094c688fee

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.
@@ -0,0 +1,161 @@
1
+ // The mmtp track modes of draft-ramadan-moq-mmt-02 13.1 and the pairing of
2
+ // an "mfu-datagram" track with the "mfu" track it mirrors (4.3.1).
3
+ //
4
+ // A publisher MUST NOT publish an "mfu-datagram" track without an "mfu" track
5
+ // of the same namespace whose objects are identical in Group ID, Object ID
6
+ // and payload. The catalog carries no sibling pointer, so the pair is read
7
+ // from what the two entries state: they are siblings when their whole
8
+ // identity set is equal, field by field or absent on both. The identity set
9
+ // is the hard set (what the Group and Object IDs are derived from) and the
10
+ // rendition fields of mmt-02 13.1 (one payload has one codec, size, bitrate
11
+ // and language; the rungs of a ladder share the hard set, so nothing else
12
+ // tells them apart). A datagram track with no sibling, or more than one, is
13
+ // rejected and the player plays "mfu". The soft set is reported, never a
14
+ // fault. Names, roles and labels are not read: fec-catalog-00 makes names
15
+ // non-normative. Known limit: two "mfu" tracks with one description cannot
16
+ // carry datagram siblings until the draft defines a pointer.
17
+ //
18
+ // The rule is a per-pair predicate: it answers the same over the whole
19
+ // catalog and over any subset of it a consumer kept.
20
+
21
+ /** Every mmtpMode this library accepts (mmt-02 13.1). Anything else rejects the track. */
22
+ export const MMTP_MODES = Object.freeze(['mfu', 'mfu-datagram'] as const)
23
+
24
+ export type MmtpMode = (typeof MMTP_MODES)[number]
25
+
26
+ export function isMmtpMode(value: unknown): value is MmtpMode {
27
+ return typeof value === 'string' && (MMTP_MODES as readonly string[]).includes(value)
28
+ }
29
+
30
+ /**
31
+ * Fields the Group and Object IDs of an mmtp track are derived from: two
32
+ * tracks whose values differ cannot carry identical ids (mmt-02 4.4.2, 4.6).
33
+ * Compared equal-or-both-absent, group duration as resolved ticks.
34
+ */
35
+ export const MMTP_SIBLING_HARD_FIELDS = Object.freeze([
36
+ 'namespace',
37
+ 'timescale',
38
+ 'presentationAnchorTicks',
39
+ 'groupDuration',
40
+ ] as const)
41
+
42
+ /**
43
+ * Fields that describe the media a track carries. The pair carries one
44
+ * payload (mmt-02 4.3.1), so it has one description: an "mfu" track whose
45
+ * description differs is another rendition. The rungs of a ladder share the
46
+ * hard set and nothing else in the catalog tells them apart. Compared
47
+ * equal-or-both-absent, like the hard set.
48
+ */
49
+ export const MMTP_SIBLING_RENDITION_FIELDS = Object.freeze([
50
+ 'codec',
51
+ 'width',
52
+ 'height',
53
+ 'framerate',
54
+ 'bitrate',
55
+ 'samplerate',
56
+ 'channelConfig',
57
+ 'lang',
58
+ ] as const)
59
+
60
+ /**
61
+ * Fields the pair SHOULD share; a mismatch is reported, not a fault. A soft
62
+ * field is read from the entry of the track subscribed, never borrowed from
63
+ * its sibling: a leg takes the keyframe interval its "mfu-datagram" entry
64
+ * states, and none when that entry states none (mmt-02 4.4.3).
65
+ */
66
+ export const MMTP_SIBLING_SOFT_FIELDS = Object.freeze([
67
+ 'initRef',
68
+ 'keyframeIntervalMs',
69
+ 'keyframeIntervalTicks',
70
+ 'isLive',
71
+ 'altGroup',
72
+ ] as const)
73
+
74
+ export interface MmtpSiblingPair<T> {
75
+ /** The "mfu" track. */
76
+ readonly stream: T
77
+ /** Its "mfu-datagram" sibling, when the catalog carries exactly one. */
78
+ readonly datagram?: T
79
+ /** Soft-set fields on which the two differ (empty without a datagram sibling). */
80
+ readonly softMismatches: readonly string[]
81
+ }
82
+
83
+ type Fields = Record<string, unknown>
84
+
85
+ function fields(track: object): Fields {
86
+ return track as Fields
87
+ }
88
+
89
+ function groupDurationTicks(track: Fields): bigint | undefined {
90
+ const ticks = track['groupDurationTicks']
91
+ if (Number.isSafeInteger(ticks)) return BigInt(ticks as number)
92
+ const ms = track['groupDurationMs']
93
+ const timescale = track['timescale']
94
+ if (!Number.isSafeInteger(ms) || !Number.isSafeInteger(timescale)) return undefined
95
+ return (BigInt(ms as number) * BigInt(timescale as number)) / 1000n
96
+ }
97
+
98
+ function identityEqual(a: Fields, b: Fields): boolean {
99
+ return (
100
+ a['namespace'] === b['namespace'] &&
101
+ a['timescale'] === b['timescale'] &&
102
+ a['presentationAnchorTicks'] === b['presentationAnchorTicks'] &&
103
+ groupDurationTicks(a) === groupDurationTicks(b) &&
104
+ MMTP_SIBLING_RENDITION_FIELDS.every((field) => a[field] === b[field])
105
+ )
106
+ }
107
+
108
+ function isMmtpTrack(track: unknown, mode: MmtpMode): track is object {
109
+ return (
110
+ typeof track === 'object' && track !== null && !Array.isArray(track) &&
111
+ fields(track)['packaging'] === 'mmtp' && fields(track)['mmtpMode'] === mode
112
+ )
113
+ }
114
+
115
+ /**
116
+ * The "mfu" tracks of `tracks` an "mfu-datagram" track could be the sibling
117
+ * of: the ones whose identity set (hard set and rendition fields) equals its own. Exactly one
118
+ * pairs it; zero or more than one rejects the datagram track (mmt-02 4.3.1).
119
+ */
120
+ export function mmtpSiblingCandidates<T extends object>(tracks: readonly T[], datagram: T): T[] {
121
+ const own = fields(datagram)
122
+ return tracks.filter((track) => isMmtpTrack(track, 'mfu') && identityEqual(fields(track), own))
123
+ }
124
+
125
+ /**
126
+ * Every "mfu" track of `tracks` by name, each with its "mfu-datagram" sibling
127
+ * when exactly one matches its identity set. A datagram track that matches no
128
+ * "mfu" track or more than one pairs with none, and two datagram tracks that
129
+ * match the same "mfu" track pair with neither (each is rejected by
130
+ * `mmtpTrackIssues` in the container validator, which first sets aside a
131
+ * track it rejects for its delivery mode); the "mfu" tracks are listed
132
+ * whatever happens to their siblings. Never throws.
133
+ */
134
+ export function pairMmtpSiblings<T extends object>(tracks: readonly T[]): ReadonlyMap<string, MmtpSiblingPair<T>> {
135
+ const pairs = new Map<string, MmtpSiblingPair<T>>()
136
+ for (const track of tracks) {
137
+ if (!isMmtpTrack(track, 'mfu')) continue
138
+ const name = fields(track)['name']
139
+ if (typeof name === 'string' && !pairs.has(name)) pairs.set(name, { stream: track, softMismatches: [] })
140
+ }
141
+ // Two datagram tracks matching one "mfu" track pair with neither: the
142
+ // catalog says nothing about which mirrors it.
143
+ const datagrams = new Map<string, T[]>()
144
+ for (const datagram of tracks) {
145
+ if (!isMmtpTrack(datagram, 'mfu-datagram')) continue
146
+ const candidates = mmtpSiblingCandidates(tracks, datagram)
147
+ if (candidates.length !== 1) continue
148
+ const name = fields(candidates[0]!)['name']
149
+ if (typeof name !== 'string' || pairs.get(name)?.stream !== candidates[0]) continue
150
+ datagrams.set(name, [...(datagrams.get(name) ?? []), datagram])
151
+ }
152
+ for (const [name, matched] of datagrams) {
153
+ if (matched.length !== 1) continue
154
+ const { stream } = pairs.get(name)!
155
+ const a = fields(stream)
156
+ const b = fields(matched[0]!)
157
+ const softMismatches = MMTP_SIBLING_SOFT_FIELDS.filter((field) => a[field] !== b[field])
158
+ pairs.set(name, { stream, datagram: matched[0]!, softMismatches })
159
+ }
160
+ return pairs
161
+ }
@@ -255,8 +255,11 @@ export function multicastCatalogIssues(
255
255
 
256
256
  const tracks = Array.isArray(catalog["tracks"]) ? catalog["tracks"] : [];
257
257
  const namespaceOf = new Map<string, unknown>();
258
+ const modeOf = new Map<string, unknown>();
258
259
  for (const track of tracks) {
259
- if (isObject(track) && typeof track["name"] === "string") namespaceOf.set(track["name"], track["namespace"]);
260
+ if (!isObject(track) || typeof track["name"] !== "string") continue;
261
+ namespaceOf.set(track["name"], track["namespace"]);
262
+ modeOf.set(track["name"], track["mmtpMode"]);
260
263
  }
261
264
  const tagged = (catalog as { [CATALOG_NAMESPACE]?: unknown })[CATALOG_NAMESPACE];
262
265
  let referenceNamespace = options.catalogNamespace ?? (typeof tagged === "string" ? tagged : undefined);
@@ -337,6 +340,12 @@ export function multicastCatalogIssues(
337
340
  const ref = refs[refIndex];
338
341
  const name = isObject(ref) ? ref["name"] : ref;
339
342
  if (typeof name !== "string") continue;
343
+ // mmt-02 4.3.1: the "mfu" and "mfu-datagram" tracks carry the same
344
+ // MMTP sub-flow, so an endpoint lists the "mfu" track and never the
345
+ // datagram one; a receiver ignores an endpoint that does.
346
+ if (modeOf.get(name) === "mfu-datagram") {
347
+ fail("multicast_track", `${at}.tracks[${refIndex}].name`, `track ${JSON.stringify(name)} is an "mfu-datagram" track, which rides no multicast endpoint; list its "mfu" sibling (draft-ramadan-moq-mmt-02 4.3.1)`);
348
+ }
340
349
  const namespace = namespaceOf.get(name);
341
350
  if (typeof namespace !== "string") continue;
342
351
  referenceNamespace ??= namespace;
@@ -387,3 +396,39 @@ export function admittedMulticastEndpoints(
387
396
  const ignored = new Set(issues.map(issue => issue.endpointIndex));
388
397
  return new Set(endpoints.map((_, index) => index).filter(index => !ignored.has(index)));
389
398
  }
399
+
400
+ /** Resolved ATSC 3.0 tuning (draft -01 4.2.2/4.2.3). Only `frequency` is REQUIRED. */
401
+ export type Atsc3NetworkSource = {
402
+ /** RF centre frequency in kHz. */
403
+ frequency: number;
404
+ plpId?: number;
405
+ serviceId?: number;
406
+ /** SLS of the MMTP service; libmmt does not fetch it. */
407
+ slsUri?: string;
408
+ };
409
+
410
+ /**
411
+ * Resolve ATSC 3.0 tuning from a catalog `networkSource` (array or bare-object
412
+ * form): the first `type: "atsc3"` entry with a positive integer `frequency`
413
+ * (kHz). Optional fields are carried only when in range; an entry with no
414
+ * usable frequency is skipped, since it names nothing a tuner can lock to.
415
+ * Draft -01 3 puts the tuner first, so callers prefer this over AMT.
416
+ *
417
+ * Lives here, not in `@blockcast/mmt-container`, so the transport's
418
+ * `endpointToMulticastConfig` and the container read the field one way.
419
+ */
420
+ export function catalogAtsc3NetworkSource(networkSource: unknown): Atsc3NetworkSource | undefined {
421
+ const entries = Array.isArray(networkSource) ? networkSource : networkSource ? [networkSource] : [];
422
+ for (const ns of entries) {
423
+ if (!isObject(ns)) continue;
424
+ if (ns.type !== "atsc3" || !isIntegerIn(ns.frequency, 1, Number.MAX_SAFE_INTEGER)) continue;
425
+ const slsUri = typeof ns.slsUri === "string" ? ns.slsUri.trim() : "";
426
+ return {
427
+ frequency: ns.frequency,
428
+ ...(isIntegerIn(ns.plpId, 0, 63) ? { plpId: ns.plpId } : {}),
429
+ ...(isIntegerIn(ns.serviceId, 0, 65535) ? { serviceId: ns.serviceId } : {}),
430
+ ...(slsUri ? { slsUri } : {}),
431
+ };
432
+ }
433
+ return undefined;
434
+ }