@blockcast/mmt-container 2.2.0-main.ea9223dc11de → 2.2.0-main.ec45bee1b9cc
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 +125 -2
- package/dist/__tests__/ad-creative-signer.d.ts +11 -0
- package/dist/__tests__/ad-creative-signer.d.ts.map +1 -0
- package/dist/__tests__/test-utils.d.ts +1 -0
- package/dist/__tests__/test-utils.d.ts.map +1 -1
- package/dist/ad-beacon.d.ts +39 -0
- package/dist/ad-beacon.d.ts.map +1 -0
- package/dist/ad-beacon.js +23 -0
- package/dist/ad-beacon.js.map +7 -0
- package/dist/ad-creative-record.d.ts +65 -0
- package/dist/ad-creative-record.d.ts.map +1 -0
- package/dist/ad-creative-record.js +2281 -0
- package/dist/ad-creative-record.js.map +7 -0
- package/dist/ad-creative-timeline.d.ts +103 -0
- package/dist/ad-creative-timeline.d.ts.map +1 -0
- package/dist/ad-creative-timeline.js +2550 -0
- package/dist/ad-creative-timeline.js.map +7 -0
- package/dist/block-policy.d.ts +128 -14
- package/dist/block-policy.d.ts.map +1 -1
- package/dist/block-policy.js +472 -27
- package/dist/block-policy.js.map +2 -2
- package/dist/browser-runtime.js +1039 -120
- package/dist/browser-runtime.js.map +4 -4
- package/dist/carp-catalog.d.ts +1 -0
- package/dist/carp-catalog.d.ts.map +1 -1
- package/dist/carp-catalog.js +34 -0
- package/dist/carp-catalog.js.map +4 -4
- package/dist/catalog-fec-params.js.map +1 -1
- package/dist/catalog-network-source.d.ts +1 -17
- package/dist/catalog-network-source.d.ts.map +1 -1
- package/dist/catalog-network-source.js +25 -19
- package/dist/catalog-network-source.js.map +3 -3
- package/dist/cmaf-block-assembler.d.ts +10 -2
- package/dist/cmaf-block-assembler.d.ts.map +1 -1
- package/dist/cmaf-block-assembler.js +30 -8
- package/dist/cmaf-block-assembler.js.map +3 -3
- package/dist/codec-utils.d.ts.map +1 -1
- package/dist/codec-utils.js +16 -3
- package/dist/codec-utils.js.map +2 -2
- package/dist/conceal-timeline.d.ts +24 -1
- package/dist/conceal-timeline.d.ts.map +1 -1
- package/dist/conceal-timeline.js +201 -4
- package/dist/conceal-timeline.js.map +3 -3
- package/dist/datagram-leg-deadline.d.ts +4 -0
- package/dist/datagram-leg-deadline.d.ts.map +1 -1
- package/dist/datagram-leg-deadline.js.map +2 -2
- package/dist/event-timeline-mmtp.d.ts +17 -0
- package/dist/event-timeline-mmtp.d.ts.map +1 -0
- package/dist/event-timeline-mmtp.js +3117 -0
- package/dist/event-timeline-mmtp.js.map +7 -0
- package/dist/fec-manager.d.ts +24 -3
- package/dist/fec-manager.d.ts.map +1 -1
- package/dist/fec-manager.js +531 -38
- package/dist/fec-manager.js.map +2 -2
- package/dist/feed-gate.d.ts +114 -0
- package/dist/feed-gate.d.ts.map +1 -0
- package/dist/feed-gate.js +175 -0
- package/dist/feed-gate.js.map +7 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -1
- package/dist/index.js.map +2 -2
- package/dist/iso-bmff-timing.d.ts +16 -0
- package/dist/iso-bmff-timing.d.ts.map +1 -1
- package/dist/iso-bmff-timing.js +28 -0
- package/dist/iso-bmff-timing.js.map +2 -2
- package/dist/mfu-reassembler.d.ts +2 -0
- package/dist/mfu-reassembler.d.ts.map +1 -1
- package/dist/mfu-reassembler.js +4 -2
- package/dist/mfu-reassembler.js.map +2 -2
- package/dist/mmtp-container-parser.d.ts +12 -0
- package/dist/mmtp-container-parser.d.ts.map +1 -1
- package/dist/mmtp-container-parser.js +73 -3
- package/dist/mmtp-container-parser.js.map +2 -2
- package/dist/mmtp-container-parser.no-modules.js +103 -25
- package/dist/mmtp-container-parser.no-modules.js.map +4 -4
- package/dist/mmtp-decode-pipeline.js +73 -3
- package/dist/mmtp-decode-pipeline.js.map +2 -2
- package/dist/mmtp-router.d.ts +20 -2
- package/dist/mmtp-router.d.ts.map +1 -1
- package/dist/mmtp-router.js +150 -23
- package/dist/mmtp-router.js.map +3 -3
- package/dist/multicast-auth-catalog.js.map +1 -1
- package/dist/multicast-input.js.map +1 -1
- package/dist/multicast-manager.d.ts +23 -2
- package/dist/multicast-manager.d.ts.map +1 -1
- package/dist/multicast-manager.js +749 -87
- package/dist/multicast-manager.js.map +3 -3
- package/dist/multicast-transport.d.ts +8 -0
- package/dist/multicast-transport.d.ts.map +1 -1
- package/dist/multicast-transport.js +56 -23
- package/dist/multicast-transport.js.map +2 -2
- package/dist/no-modules.js +98 -22
- package/dist/no-modules.js.map +4 -4
- package/dist/repair-fetch.d.ts +15 -7
- package/dist/repair-fetch.d.ts.map +1 -1
- package/dist/repair-fetch.js +34 -7
- package/dist/repair-fetch.js.map +2 -2
- package/dist/wasm-mfu-reassembler.js.map +2 -2
- package/dist/wasm-mmtp-container-parser.js +73 -3
- package/dist/wasm-mmtp-container-parser.js.map +2 -2
- package/package.json +17 -5
- package/schemas/catalog.schema.json +7 -2
- package/src/ad-beacon.ts +54 -0
- package/src/ad-creative-record.ts +276 -0
- package/src/ad-creative-timeline.ts +410 -0
- package/src/block-policy.ts +673 -42
- package/src/carp-catalog.ts +9 -0
- package/src/catalog-network-source.ts +3 -37
- package/src/cmaf-block-assembler.ts +17 -0
- package/src/codec-utils.ts +19 -3
- package/src/conceal-timeline.ts +60 -5
- package/src/datagram-leg-deadline.ts +4 -0
- package/src/event-timeline-mmtp.ts +55 -0
- package/src/fec-manager.ts +84 -12
- package/src/feed-gate.ts +295 -0
- package/src/index.ts +21 -0
- package/src/iso-bmff-timing.ts +33 -0
- package/src/mfu-reassembler.ts +4 -0
- package/src/mmtp-container-parser.ts +51 -5
- package/src/mmtp-router.ts +116 -4
- package/src/multicast-manager.ts +31 -0
- package/src/multicast-transport.ts +27 -2
- package/src/repair-fetch.ts +70 -19
- package/test-vectors/catalog/README.md +1 -0
- package/test-vectors/catalog/negative/event-timeline-empty-event-type.json +41 -0
- package/test-vectors/catalog/positive/event-timeline-ad-creative.json +41 -0
- package/test-vectors/catalog/positive/event-timeline-wallclock-no-depends.json +38 -0
- package/test-vectors/catalog/positive/multicast-event-timeline.json +81 -0
- package/test-vectors/catalog/section-codes.json +1 -0
- package/test-vectors/frame-needs-v1.json +295 -0
- package/test-vectors/repair-policy-v1.json +98 -18
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ad creative timeline (draft-riedl-moq-ad-creative-signaling-01): verified records placed on
|
|
3
|
+
* the depends track's media time (riedl 5.1, 5.4) and fired at most once from the player's
|
|
4
|
+
* presented time (riedl 11). No clock and no I/O. Imports ad-creative-record (noble): subpath only.
|
|
5
|
+
*/
|
|
6
|
+
import { serial32Distance, serial32Precedes } from '@blockcast/mmt-base'
|
|
7
|
+
import {
|
|
8
|
+
canonicalJson, parseAdCreativeObject,
|
|
9
|
+
type AdCreativeKeys, type AdCreativeRecord, type AdIndex, type AdObjectDrop, type AdRecordDrop,
|
|
10
|
+
} from './ad-creative-record.js'
|
|
11
|
+
|
|
12
|
+
export * from './ad-creative-record.js'
|
|
13
|
+
export * from './ad-beacon.js'
|
|
14
|
+
|
|
15
|
+
export interface AdScheduledEvent {
|
|
16
|
+
/** Unique within a record: container key, tracking type and its occurrence. */
|
|
17
|
+
readonly eventKey: string
|
|
18
|
+
/** The slot's key (riedl 10 item 8: identifiers plus occurrence), or null for a pod or record event. */
|
|
19
|
+
readonly slotKey: string | null
|
|
20
|
+
readonly eventType: string
|
|
21
|
+
readonly urls: readonly string[]
|
|
22
|
+
readonly dueMs: number
|
|
23
|
+
/** podStart only: the pod's end, so a joiner inside the pod still fires it (riedl 11). Absent when the pod has no duration. */
|
|
24
|
+
readonly podEndMs?: number
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface AdSlotWindow {
|
|
28
|
+
readonly slotKey: string
|
|
29
|
+
readonly startMs: number
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface AdSchedule {
|
|
33
|
+
/** Sorted by dueMs; among equal times podStart first and podEnd last. */
|
|
34
|
+
readonly events: readonly AdScheduledEvent[]
|
|
35
|
+
readonly slots: readonly AdSlotWindow[]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Time-driven tracking types without an `offset`, as a fraction of the container's duration
|
|
40
|
+
* (VAST 4.2 semantics; SVTA 2053-1 4.4.5 not read). Only these and `progress`, which needs an
|
|
41
|
+
* explicit `offset` (VAST 4.2), are scheduled. Interaction and error types (pause, skip,
|
|
42
|
+
* clickTracking, error) belong to the player, offset or not: fired from a shared schedule, a whole
|
|
43
|
+
* audience would report them.
|
|
44
|
+
*/
|
|
45
|
+
const FRACTION = new Map<string, number>([
|
|
46
|
+
['podStart', 0], ['impression', 0], ['start', 0], ['firstQuartile', 0.25],
|
|
47
|
+
['midpoint', 0.5], ['thirdQuartile', 0.75], ['complete', 1], ['podEnd', 1],
|
|
48
|
+
])
|
|
49
|
+
const TIMED: ReadonlySet<string> = new Set([...FRACTION.keys(), 'progress'])
|
|
50
|
+
const rank = (type: string): number => (type === 'podStart' ? 0 : type === 'podEnd' ? 2 : 1)
|
|
51
|
+
/** SVTA MediaTime: seconds (riedl 4.3). */
|
|
52
|
+
const seconds = (value: unknown): number | undefined =>
|
|
53
|
+
typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : undefined
|
|
54
|
+
|
|
55
|
+
function containerEvents(tracking: unknown, startMs: number, durationS: number | undefined, prefix: string, slotKey: string | null): AdScheduledEvent[] {
|
|
56
|
+
const events: AdScheduledEvent[] = []
|
|
57
|
+
const occurrences = new Map<string, number>()
|
|
58
|
+
for (const entry of Array.isArray(tracking) ? tracking : []) {
|
|
59
|
+
if (typeof entry !== 'object' || entry === null) continue
|
|
60
|
+
const event = entry as Record<string, unknown>
|
|
61
|
+
const type = event['type']
|
|
62
|
+
const urls = event['urls']
|
|
63
|
+
if (typeof type !== 'string' || !TIMED.has(type) || !Array.isArray(urls)) continue
|
|
64
|
+
const n = occurrences.get(type) ?? 0
|
|
65
|
+
occurrences.set(type, n + 1)
|
|
66
|
+
const fraction = FRACTION.get(type)
|
|
67
|
+
// A present but malformed offset drops the entry: it states a time, and the fraction default would fire at one nobody authored.
|
|
68
|
+
if (event['offset'] !== undefined && seconds(event['offset']) === undefined) continue
|
|
69
|
+
const atS = seconds(event['offset']) ??
|
|
70
|
+
(fraction === 0 ? 0 : fraction !== undefined && durationS !== undefined ? fraction * durationS : undefined)
|
|
71
|
+
if (atS === undefined) continue
|
|
72
|
+
events.push({
|
|
73
|
+
eventKey: `${prefix}|${type}#${n}`,
|
|
74
|
+
slotKey,
|
|
75
|
+
eventType: type,
|
|
76
|
+
urls: Object.freeze([...(urls as string[])]),
|
|
77
|
+
dueMs: startMs + Math.round(atS * 1000),
|
|
78
|
+
// A joiner fires podStart late only inside a pod of known length; without one it fires on time or not at all.
|
|
79
|
+
...(type === 'podStart' && slotKey === null && durationS !== undefined
|
|
80
|
+
? { podEndMs: startMs + Math.round(durationS * 1000) }
|
|
81
|
+
: {}),
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
return events
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function slotEvents(slots: unknown, containerStartMs: number, prefix: string, events: AdScheduledEvent[], windows: AdSlotWindow[]): void {
|
|
88
|
+
const occurrences = new Map<string, number>()
|
|
89
|
+
for (const entry of Array.isArray(slots) ? slots : []) {
|
|
90
|
+
if (typeof entry !== 'object' || entry === null) continue
|
|
91
|
+
const slot = entry as Record<string, unknown>
|
|
92
|
+
// riedl 10 item 8: a slot's key is its identifiers plus how many earlier slots carry the same ones.
|
|
93
|
+
const ids = canonicalJson(slot['identifiers'] ?? null)
|
|
94
|
+
const n = occurrences.get(ids) ?? 0
|
|
95
|
+
occurrences.set(ids, n + 1)
|
|
96
|
+
const slotKey = `${prefix}/${ids}#${n}`
|
|
97
|
+
const startMs = containerStartMs + Math.round((seconds(slot['start']) ?? 0) * 1000)
|
|
98
|
+
windows.push({ slotKey, startMs })
|
|
99
|
+
events.push(...containerEvents(slot['tracking'], startMs, seconds(slot['duration']), slotKey, slotKey))
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Place a record's events. `startMs` is the record index on the depends track's media time (for
|
|
105
|
+
* `m`, the index itself; riedl 5.1). Interior times are seconds relative to their container
|
|
106
|
+
* (riedl 4.3, 5.4); in a slot or trackingEvent envelope the record index stands in for the Pod.
|
|
107
|
+
*/
|
|
108
|
+
export function scheduleAdRecord(record: AdCreativeRecord, startMs: number): AdSchedule {
|
|
109
|
+
const events: AdScheduledEvent[] = []
|
|
110
|
+
const slots: AdSlotWindow[] = []
|
|
111
|
+
const { type, payload } = record.data
|
|
112
|
+
if (type === 'pod') {
|
|
113
|
+
payload.forEach((pod, i) => {
|
|
114
|
+
// A pod with a non-zero start never gets here: parseAdCreativeRecord ignores it as 'timing' (Question 13).
|
|
115
|
+
events.push(...containerEvents(pod['tracking'], startMs, seconds(pod['duration']), `pod${i}`, null))
|
|
116
|
+
slotEvents(pod['slots'], startMs, `pod${i}`, events, slots)
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
if (type === 'slot') slotEvents(payload, startMs, 'slot', events, slots)
|
|
120
|
+
if (type === 'trackingEvent') events.push(...containerEvents(payload, startMs, undefined, 'record', null))
|
|
121
|
+
// 'verification' schedules nothing: the verified record event hands it to the player (riedl 12.3).
|
|
122
|
+
events.sort((a, b) => a.dueMs - b.dueMs || rank(a.eventType) - rank(b.eventType))
|
|
123
|
+
return { events, slots }
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Where an Object came from: its MoQ Location, or on multicast its MPU sequence number and Item_ID. */
|
|
127
|
+
export interface AdObjectLocation {
|
|
128
|
+
readonly groupId: number
|
|
129
|
+
readonly objectId: number
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export type AdTimelineEvent =
|
|
133
|
+
| {
|
|
134
|
+
readonly type: 'record'
|
|
135
|
+
readonly status: 'added' | 'superseded' | 'removed'
|
|
136
|
+
readonly key: string
|
|
137
|
+
readonly verified: true
|
|
138
|
+
/** Only a verified record's contents ever leave the timeline (riedl 4.5). */
|
|
139
|
+
readonly record: AdCreativeRecord
|
|
140
|
+
}
|
|
141
|
+
/** observeUnverified only: an unverified record's key, never its contents (riedl 4.5: it is absent). */
|
|
142
|
+
| { readonly type: 'record'; readonly status: 'added' | 'superseded' | 'removed'; readonly key: string; readonly verified: false }
|
|
143
|
+
| {
|
|
144
|
+
readonly type: 'rejected'
|
|
145
|
+
readonly reason: AdObjectDrop | AdRecordDrop | 'unmappable' | 'stale-group' | 'replayed'
|
|
146
|
+
readonly key?: string
|
|
147
|
+
readonly location: AdObjectLocation
|
|
148
|
+
}
|
|
149
|
+
| {
|
|
150
|
+
readonly type: 'track'
|
|
151
|
+
readonly key: string
|
|
152
|
+
readonly slotKey: string | null
|
|
153
|
+
readonly eventType: string
|
|
154
|
+
readonly urls: readonly string[]
|
|
155
|
+
readonly dueMs: number
|
|
156
|
+
readonly mediaTimeMs: number
|
|
157
|
+
}
|
|
158
|
+
| {
|
|
159
|
+
/** observeUnverified only: an event of an unsigned or unknown-key record. It carries no URL. */
|
|
160
|
+
readonly type: 'observed'
|
|
161
|
+
readonly key: string
|
|
162
|
+
readonly slotKey: string | null
|
|
163
|
+
readonly eventType: string
|
|
164
|
+
readonly dueMs: number
|
|
165
|
+
readonly mediaTimeMs: number
|
|
166
|
+
}
|
|
167
|
+
export type AdTrackEvent = Extract<AdTimelineEvent, { type: 'track' }>
|
|
168
|
+
export type AdTimelineObserver = (event: AdTimelineEvent) => void
|
|
169
|
+
|
|
170
|
+
export interface AdCreativeTimelineOptions {
|
|
171
|
+
/** The channel's key set (importAdCreativeJwks with the channel's kids). */
|
|
172
|
+
keys: AdCreativeKeys
|
|
173
|
+
/** Media ms of a t or l index on the depends track's timeline, or null. Default: only m maps (riedl 4.2). */
|
|
174
|
+
mapIndex?: (index: AdIndex) => number | null
|
|
175
|
+
/** An event due longer ago than this is skipped, never burst; a forward jump this long is a seek. Default 2000. */
|
|
176
|
+
maxLateMs?: number
|
|
177
|
+
/** Schedule unsigned and unknown-key records as 'observed' events, which carry no URL (live interop). Default false. */
|
|
178
|
+
observeUnverified?: boolean
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
interface Entry {
|
|
182
|
+
record: AdCreativeRecord
|
|
183
|
+
schedule: AdSchedule
|
|
184
|
+
verified: boolean
|
|
185
|
+
/** Events fired or skipped: at most once per (index, slot, type) (riedl 10 item 3). Kept across verified supersedes. */
|
|
186
|
+
readonly done: Set<string>
|
|
187
|
+
/** Canonical forms of the verified versions this index replaced: a re-served one is 'replayed' (riedl 12.1). */
|
|
188
|
+
readonly replaced: Set<string>
|
|
189
|
+
joined: boolean
|
|
190
|
+
/** The latest end over every verified version this index held, so a tombstone outlives each one it can refuse. */
|
|
191
|
+
endMs: number
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** What a removed verified index leaves behind, so a re-add can neither replay nor re-fire (riedl 12.1, 10 item 3). */
|
|
195
|
+
interface Tombstone {
|
|
196
|
+
readonly done: Set<string>
|
|
197
|
+
readonly replaced: Set<string>
|
|
198
|
+
readonly endMs: number
|
|
199
|
+
/** Dropped once the presented time passes this: endMs plus maxLateMs. */
|
|
200
|
+
readonly untilMs: number
|
|
201
|
+
/** Playback never stopped, so a restore keeps the join; a seek clears it. */
|
|
202
|
+
joined: boolean
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Where a version stops mattering: its last event, or its index for a record with none. */
|
|
206
|
+
const versionEndMs = (schedule: AdSchedule, startMs: number) =>
|
|
207
|
+
Math.max(startMs, ...schedule.events.map((e) => e.podEndMs ?? e.dueMs))
|
|
208
|
+
|
|
209
|
+
/** A late Group up to this many behind is stale; one further away in either direction is a new epoch (about 50 min of ~3 s Groups). */
|
|
210
|
+
const EPOCH_GROUPS = 1024
|
|
211
|
+
const utf8 = new TextDecoder()
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Takes Object payloads only. The MSF_EVENT_TIMELINE_HEADER track property (msf-02 8.1, 0x7B) is
|
|
215
|
+
* the subscriber's to read: riedl -01 defines no header, so a present one is a diagnostic there and
|
|
216
|
+
* never a reason to accept a record the signature check drops. It assumes the msf-02 8.4 sequence mapping
|
|
217
|
+
* (Object 0 holds every accessible record, later Objects add); under the "independent of its history"
|
|
218
|
+
* mapping every Group's Object 0 would remove the previous Group's records.
|
|
219
|
+
*/
|
|
220
|
+
export class AdCreativeTimeline {
|
|
221
|
+
/** Replace to rotate keys (riedl 4.5); a verified record stays until an independent Object drops it. */
|
|
222
|
+
keys: AdCreativeKeys
|
|
223
|
+
readonly #mapIndex: (index: AdIndex) => number | null
|
|
224
|
+
readonly #maxLateMs: number
|
|
225
|
+
readonly #observeUnverified: boolean
|
|
226
|
+
readonly #observers = new Set<AdTimelineObserver>()
|
|
227
|
+
readonly #entries = new Map<string, Entry>()
|
|
228
|
+
readonly #tombstones = new Map<string, Tombstone>()
|
|
229
|
+
/** This Group's Objects already applied, by objectId: the payloads seen, so only an identical copy is skipped. */
|
|
230
|
+
readonly #objects = new Map<number, Set<string>>()
|
|
231
|
+
#group: number | undefined
|
|
232
|
+
#lastMs: number | undefined
|
|
233
|
+
|
|
234
|
+
constructor(options: AdCreativeTimelineOptions) {
|
|
235
|
+
this.keys = options.keys
|
|
236
|
+
this.#mapIndex = options.mapIndex ?? ((index) => (index.kind === 'm' ? index.ms : null))
|
|
237
|
+
this.#maxLateMs = options.maxLateMs ?? 2000
|
|
238
|
+
this.#observeUnverified = options.observeUnverified ?? false
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
addObserver(observer: AdTimelineObserver): () => void {
|
|
242
|
+
this.#observers.add(observer)
|
|
243
|
+
return () => {
|
|
244
|
+
this.#observers.delete(observer)
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** One Object of the track, from MoQ or from the multicast data path (EventTimelineMmtpReader). */
|
|
249
|
+
ingest(bytes: Uint8Array, location: AdObjectLocation): void {
|
|
250
|
+
// Multicast carries the Group ID mod 2^32: both legs compare in that space (RFC 1982). A Group further than
|
|
251
|
+
// EPOCH_GROUPS away replaces the set even where RFC 1982 calls it older: a publisher restart (msf-02 6.1,
|
|
252
|
+
// unchanged from msf-01 6.1; a wallclock-ms first Group, riedl 11), or an injected Group, whose damage then
|
|
253
|
+
// lasts one heartbeat.
|
|
254
|
+
const group = location.groupId >>> 0
|
|
255
|
+
if (this.#group !== undefined && serial32Precedes(group, this.#group) && serial32Distance(group, this.#group) <= EPOCH_GROUPS) {
|
|
256
|
+
this.#emit({ type: 'rejected', reason: 'stale-group', location })
|
|
257
|
+
return
|
|
258
|
+
}
|
|
259
|
+
// The other leg's copy, or a redelivery. Compare content, not location: a corrupted or injected copy that
|
|
260
|
+
// arrives first must not mask the authentic one.
|
|
261
|
+
const payload = utf8.decode(bytes)
|
|
262
|
+
if (group === this.#group && this.#objects.get(location.objectId)?.has(payload)) return
|
|
263
|
+
const result = parseAdCreativeObject(bytes, this.keys)
|
|
264
|
+
if (result.verdict !== 'accepted') {
|
|
265
|
+
this.#emit({ type: 'rejected', reason: result.verdict, location })
|
|
266
|
+
return
|
|
267
|
+
}
|
|
268
|
+
if (group !== this.#group) {
|
|
269
|
+
this.#group = group
|
|
270
|
+
this.#objects.clear()
|
|
271
|
+
}
|
|
272
|
+
// riedl -01 inherits msf-01 8.3, which msf-02 8.4 keeps as its sequence case: Object 0 is independent and
|
|
273
|
+
// replaces the set; later Objects only add. An Object 0 that arrives after a later Object of its Group adds
|
|
274
|
+
// too, or it would drop that Object's records.
|
|
275
|
+
const replace = location.objectId === 0 && [...this.#objects.keys()].every((id) => id === 0)
|
|
276
|
+
const seen = this.#objects.get(location.objectId) ?? new Set<string>()
|
|
277
|
+
this.#objects.set(location.objectId, seen.add(payload))
|
|
278
|
+
const present = new Set<string>()
|
|
279
|
+
// A record whose signature fails or is malformed means this copy was altered after signing, so it is not the
|
|
280
|
+
// publisher's Object 0 and removes nothing: the next heartbeat's Object 0 does (riedl 11). Policy drops an honest
|
|
281
|
+
// publisher can send (feature, timing, an unknown key during rotation) still sweep; a relay can sweep with [] anyway.
|
|
282
|
+
let intact = true
|
|
283
|
+
for (const parsed of result.records) {
|
|
284
|
+
const verified = parsed.verdict === 'verified'
|
|
285
|
+
if (parsed.verdict === 'signature' || parsed.verdict === 'sig') intact = false
|
|
286
|
+
if (!verified) this.#emit({ type: 'rejected', reason: parsed.verdict as AdRecordDrop, key: parsed.key, location })
|
|
287
|
+
const record = parsed.record
|
|
288
|
+
// riedl 4.5: an unverified record is absent. Observe-only keeps unsigned and unknown-key ones, never their URLs.
|
|
289
|
+
if (!record || (!verified && !this.#observeUnverified)) continue
|
|
290
|
+
const startMs = this.#mapIndex(record.index)
|
|
291
|
+
if (startMs === null) {
|
|
292
|
+
this.#emit({ type: 'rejected', reason: 'unmappable', key: record.key, location })
|
|
293
|
+
continue
|
|
294
|
+
}
|
|
295
|
+
present.add(record.key)
|
|
296
|
+
const entry = this.#entries.get(record.key)
|
|
297
|
+
if (entry && entry.verified && !verified) continue // an unverified copy never displaces a verified record
|
|
298
|
+
if (entry && entry.verified === verified && entry.record.canonical === record.canonical) continue // heartbeat (riedl 11)
|
|
299
|
+
// riedl 12.1: a relay can re-serve, in a newer Group, a version a supersede replaced. Never go back to one.
|
|
300
|
+
// A removed index keeps its replaced versions as a tombstone, so remove-then-re-add cannot replay either.
|
|
301
|
+
const tombstone = this.#tombstones.get(record.key)
|
|
302
|
+
if (verified && (entry?.verified ? entry.replaced : tombstone?.replaced)?.has(record.canonical)) {
|
|
303
|
+
this.#emit({ type: 'rejected', reason: 'replayed', key: record.key, location })
|
|
304
|
+
continue
|
|
305
|
+
}
|
|
306
|
+
const schedule = scheduleAdRecord(record, startMs)
|
|
307
|
+
const endMs = versionEndMs(schedule, startMs)
|
|
308
|
+
// riedl 10 item 2: last writer wins at an index; fired events stay fired.
|
|
309
|
+
if (entry) {
|
|
310
|
+
if (entry.verified) entry.replaced.add(entry.record.canonical)
|
|
311
|
+
if (!entry.verified && verified) {
|
|
312
|
+
// Observed fires sent nothing, so the verified copy starts its events afresh (with the late-join rule),
|
|
313
|
+
// except those a removed verified version of this index already fired.
|
|
314
|
+
entry.done.clear()
|
|
315
|
+
for (const done of tombstone?.done ?? []) entry.done.add(done)
|
|
316
|
+
for (const version of tombstone?.replaced ?? []) entry.replaced.add(version)
|
|
317
|
+
entry.endMs = tombstone?.endMs ?? -Infinity
|
|
318
|
+
this.#tombstones.delete(record.key)
|
|
319
|
+
entry.joined = false
|
|
320
|
+
}
|
|
321
|
+
entry.endMs = verified ? Math.max(entry.endMs, endMs) : endMs
|
|
322
|
+
entry.record = record
|
|
323
|
+
entry.schedule = schedule
|
|
324
|
+
entry.verified = verified
|
|
325
|
+
} else {
|
|
326
|
+
const fromTombstone = verified ? tombstone : undefined
|
|
327
|
+
if (fromTombstone) this.#tombstones.delete(record.key)
|
|
328
|
+
this.#entries.set(record.key, {
|
|
329
|
+
record, schedule, verified, joined: fromTombstone?.joined ?? false,
|
|
330
|
+
endMs: Math.max(endMs, fromTombstone?.endMs ?? -Infinity),
|
|
331
|
+
done: new Set(fromTombstone?.done), replaced: new Set(fromTombstone?.replaced),
|
|
332
|
+
})
|
|
333
|
+
}
|
|
334
|
+
this.#emitRecord(entry ? 'superseded' : 'added', record, verified)
|
|
335
|
+
}
|
|
336
|
+
if (!replace || !intact) return
|
|
337
|
+
for (const [key, entry] of this.#entries) {
|
|
338
|
+
if (present.has(key)) continue
|
|
339
|
+
this.#entries.delete(key)
|
|
340
|
+
if (entry.verified) {
|
|
341
|
+
const { done, replaced, endMs, joined } = entry
|
|
342
|
+
this.#tombstones.set(key, { done, replaced, endMs, joined, untilMs: endMs + this.#maxLateMs })
|
|
343
|
+
}
|
|
344
|
+
this.#emitRecord('removed', entry.record, entry.verified)
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** Drive with the media time being presented, in ms on the depends track's timeline (riedl 5.1, 11). */
|
|
349
|
+
advance(mediaTimeMs: number): void {
|
|
350
|
+
// A forward jump past maxLateMs is a seek: re-join there instead of bursting what it crossed.
|
|
351
|
+
if (this.#lastMs !== undefined && mediaTimeMs - this.#lastMs > this.#maxLateMs) {
|
|
352
|
+
for (const entry of this.#entries.values()) entry.joined = false
|
|
353
|
+
for (const tombstone of this.#tombstones.values()) tombstone.joined = false
|
|
354
|
+
}
|
|
355
|
+
this.#lastMs = mediaTimeMs
|
|
356
|
+
for (const [key, tombstone] of this.#tombstones) if (tombstone.untilMs < mediaTimeMs) this.#tombstones.delete(key)
|
|
357
|
+
for (const [key, entry] of this.#entries) {
|
|
358
|
+
if (!entry.joined) this.#join(key, entry, mediaTimeMs)
|
|
359
|
+
for (const event of entry.schedule.events) {
|
|
360
|
+
if (entry.done.has(event.eventKey) || event.dueMs > mediaTimeMs) continue
|
|
361
|
+
entry.done.add(event.eventKey)
|
|
362
|
+
// Due longer ago than maxLateMs: a supersede moved it behind the playhead. Skip it.
|
|
363
|
+
if (mediaTimeMs - event.dueMs <= this.#maxLateMs) this.#fire(key, entry, event, mediaTimeMs)
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// riedl 11 joiner policy: podStart on joining, nothing for a slot already in progress, full chains after it.
|
|
369
|
+
#join(key: string, entry: Entry, t: number): void {
|
|
370
|
+
entry.joined = true
|
|
371
|
+
for (const slot of entry.schedule.slots) {
|
|
372
|
+
if (t - slot.startMs <= this.#maxLateMs) continue
|
|
373
|
+
for (const event of entry.schedule.events) if (event.slotKey === slot.slotKey) entry.done.add(event.eventKey)
|
|
374
|
+
}
|
|
375
|
+
for (const event of entry.schedule.events) {
|
|
376
|
+
if (event.podEndMs === undefined || entry.done.has(event.eventKey) || event.dueMs > t || t >= event.podEndMs) continue
|
|
377
|
+
entry.done.add(event.eventKey)
|
|
378
|
+
this.#fire(key, entry, event, t)
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
#fire(key: string, entry: Entry, event: AdScheduledEvent, mediaTimeMs: number): void {
|
|
383
|
+
const { slotKey, eventType, dueMs } = event
|
|
384
|
+
if (entry.verified) {
|
|
385
|
+
this.#emit({ type: 'track', key, slotKey, eventType, urls: event.urls, dueMs, mediaTimeMs })
|
|
386
|
+
return
|
|
387
|
+
}
|
|
388
|
+
// An unverified slotKey would carry the record's identifiers (riedl 4.5: its contents are absent): an ordinal.
|
|
389
|
+
const ordinal = slotKey === null ? null : `slot#${entry.schedule.slots.findIndex((slot) => slot.slotKey === slotKey)}`
|
|
390
|
+
this.#emit({ type: 'observed', key, slotKey: ordinal, eventType, dueMs, mediaTimeMs })
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// riedl 4.5: an unverified record is absent, so observe-only names its key and never its contents.
|
|
394
|
+
#emitRecord(status: 'added' | 'superseded' | 'removed', record: AdCreativeRecord, verified: boolean): void {
|
|
395
|
+
this.#emit(verified
|
|
396
|
+
? { type: 'record', status, key: record.key, verified: true, record }
|
|
397
|
+
: { type: 'record', status, key: record.key, verified: false })
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
#emit(event: AdTimelineEvent): void {
|
|
401
|
+
Object.freeze(event)
|
|
402
|
+
for (const observer of this.#observers) {
|
|
403
|
+
try {
|
|
404
|
+
observer(event)
|
|
405
|
+
} catch (error) {
|
|
406
|
+
console.warn('[AdCreativeTimeline] observer failed', error)
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|