Repository navigation
feat(hang)!: one continuous broadcast clock at the catalog root #3675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e8fb88d
claim quest/m1/broadcast-clock
kixelated 511b29b
feat(hang)!: one continuous broadcast clock at the catalog root
kixelated 8ebc50e
fix(hang): share the advertised clock and refuse unrepresentable inputs
kixelated c15b6cc
fix(hang): refuse wrapping clock maps and u32-only timescales
kixelated File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { ClockSchema, MOQ_EPOCH_UNIX_MILLIS, u53, wallClockTime } from "./index.ts"; | ||
| import { RootSchema } from "./root.ts"; | ||
|
|
||
| // Mirrors rs/hang/fixtures/catalog-clock.json: the canonical new shape, a root clock plus an | ||
| // archive without a wall field. A reader maps archive timestamps through the root clock after | ||
| // timescale conversion. | ||
| const FIXTURE = { | ||
| video: { | ||
| renditions: { | ||
| video: { codec: "avc1.64001f", container: { kind: "legacy" } }, | ||
| }, | ||
| }, | ||
| audio: { | ||
| renditions: { | ||
| audio: { codec: "opus", container: { kind: "legacy" }, sampleRate: 48000, numberOfChannels: 2 }, | ||
| }, | ||
| }, | ||
| clock: { wall: 1_751_846_400_000_000, timescale: 1_000_000 }, | ||
| archive: { track: "timeline.z", timescale: 1000, durationMax: 2000 }, | ||
| }; | ||
|
|
||
| test("timescale defaults to microseconds", () => { | ||
| const parsed = ClockSchema.parse({ wall: 1000 }); | ||
| expect(parsed.timescale).toBe(u53(1_000_000)); | ||
| }); | ||
|
|
||
| test("a zero timescale is refused", () => { | ||
| expect(() => ClockSchema.parse({ wall: 0, timescale: 0 })).toThrow(); | ||
| }); | ||
|
|
||
| test("an explicit null timescale is refused", () => { | ||
| expect(() => ClockSchema.parse({ wall: 0, timescale: null })).toThrow(); | ||
| }); | ||
|
|
||
| test("a timescale past u32 is refused", () => { | ||
| expect(() => ClockSchema.parse({ wall: 0, timescale: 4_294_967_296 })).toThrow(); | ||
| }); | ||
|
|
||
| test("a wall past the JSON-safe integers is refused", () => { | ||
| expect(() => ClockSchema.parse({ wall: Number.MAX_SAFE_INTEGER + 1 })).toThrow(); | ||
| }); | ||
|
|
||
| test("wallClockTime converts across timescales", () => { | ||
| const clock = ClockSchema.parse({ wall: 1_000_000 }); | ||
| const epoch = MOQ_EPOCH_UNIX_MILLIS + 1000; | ||
|
|
||
| // PTS zero is the wall epoch itself. | ||
| expect(wallClockTime(clock, 0, 1000).getTime()).toBe(epoch); | ||
|
|
||
| // One media second later, whatever timescale names it. | ||
| for (const [pts, scale] of [ | ||
| [1000, 1000], | ||
| [48_000, 48_000], | ||
| [90_000, 90_000], | ||
| ] as const) { | ||
| expect(wallClockTime(clock, pts, scale).getTime()).toBe(epoch + 1000); | ||
| } | ||
| }); | ||
|
|
||
| test("wallClockTime refuses bad inputs", () => { | ||
| const clock = ClockSchema.parse({ wall: 0 }); | ||
| expect(() => wallClockTime(clock, 0, 0)).toThrow(); | ||
| // A zero timescale that slipped past parsing still refuses at conversion. | ||
| const bad = JSON.parse('{"wall":0,"timescale":0}'); | ||
| expect(() => wallClockTime(bad, 0, 1000)).toThrow(); | ||
| expect(() => wallClockTime(clock, Number.MAX_SAFE_INTEGER, 1)).toThrow(); | ||
| }); | ||
|
|
||
| test("the packaged shape maps archive timestamps through the root clock", () => { | ||
| const catalog = RootSchema.parse(FIXTURE); | ||
| if (!catalog.clock) throw new Error("fixture lost its clock"); | ||
| if (!catalog.archive) throw new Error("fixture lost its archive"); | ||
|
|
||
| // Archive PTS 2000 (ms) lands 2s after the wall epoch. | ||
| expect(wallClockTime(catalog.clock, 2000, catalog.archive.timescale).getTime()).toBe( | ||
| MOQ_EPOCH_UNIX_MILLIS + 1_751_846_402_000, | ||
| ); | ||
| expect("wall" in catalog.archive).toBe(false); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import * as z from "@zod/mini"; | ||
| import { u53Schema } from "./integers"; | ||
| import { MOQ_EPOCH_UNIX_MILLIS } from "./timeline"; | ||
|
|
||
| /** Units per second for a catalog clock. Matches Rust `u32`; zero is refused. */ | ||
| const clockTimescaleSchema = z.number().check(z.int(), z.positive(), z.lte(4_294_967_295)); | ||
|
|
||
| /** | ||
| * The broadcast's one continuous clock, advertised at the catalog root. | ||
| * | ||
| * `wall` is the wall-clock time of PTS zero, in `timescale` units since the moq epoch | ||
| * ({@link MOQ_EPOCH_UNIX_MILLIS}, 2020-01-01). Every media track and the archive index refer | ||
| * to this one mapping after timescale conversion, so there are no competing wall epochs. The | ||
| * publisher fixes it once and never overwrites it: a discontinuity marker is a delivery event, | ||
| * not a new epoch, and a system-clock adjustment never retimes it. It is independent of the | ||
| * `archive` entry: a live-only publisher exposes its clock without creating a segment index. | ||
| */ | ||
| export const ClockSchema = z.object({ | ||
| // The wall-clock time of PTS zero, in `timescale` units since the moq epoch. Must fit in a | ||
| // JSON-safe integer so browsers read back exactly what the publisher wrote. | ||
| wall: u53Schema, | ||
|
|
||
| // Units per second for `wall`. Defaults to 1_000_000 (microseconds), the broadcast clock's | ||
| // own timescale. Zero is refused: no timestamp can be expressed in it. Bounded to u32 so | ||
| // Rust and JS accept the same catalogs. | ||
| timescale: z._default(clockTimescaleSchema, 1_000_000), | ||
| }); | ||
|
|
||
| /** The broadcast's one continuous clock. */ | ||
| export type Clock = z.infer<typeof ClockSchema>; | ||
|
|
||
| /** | ||
| * The wall-clock time of `pts`, given in `ptsTimescale` units per second, under the broadcast's | ||
| * fixed clock mapping (`wall + pts` after conversion into the clock's timescale). | ||
| * | ||
| * Throws on a zero timescale, a non-integer or unsafe value, or a result outside the JSON-safe | ||
| * integer range, rather than truncating. | ||
| */ | ||
| export function wallClockTime(clock: Clock, pts: number, ptsTimescale: number): Date { | ||
| if (!Number.isSafeInteger(clock.wall)) throw new RangeError(`invalid wall clock: ${clock.wall}`); | ||
| if (!Number.isSafeInteger(clock.timescale) || clock.timescale <= 0 || clock.timescale > 4_294_967_295) | ||
| throw new RangeError(`invalid timescale: ${clock.timescale}`); | ||
| if (!Number.isSafeInteger(pts) || pts < 0) throw new RangeError(`invalid pts: ${pts}`); | ||
| if (!Number.isSafeInteger(ptsTimescale) || ptsTimescale <= 0) | ||
| throw new RangeError(`invalid timescale: ${ptsTimescale}`); | ||
|
|
||
| const units = Math.floor((pts * clock.timescale) / ptsTimescale); | ||
| const total = clock.wall + units; | ||
| if (!Number.isSafeInteger(total)) throw new RangeError(`invalid wall clock: ${total}`); | ||
|
|
||
| const unixMillis = MOQ_EPOCH_UNIX_MILLIS + Math.floor((total * 1000) / clock.timescale); | ||
| return new Date(unixMillis); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.