Repository navigation
fix(hang)!: CMAF decoders time samples from the frame timestamp #4826
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
16 commits
Select commit
Hold shift + click to select a range
89ab505
quest: claim cmaf-frame-timestamp
Dryvnt b92f129
fix(hang)!: CMAF decoders time samples from the frame timestamp
Dryvnt 899af89
docs: list the CMAF decode change in the upgrade guide
Dryvnt b2feacf
fix(hang): anchor CMAF samples across every trun, and drop decodeTime…
Dryvnt cf87b34
fix(hang): find the earliest CMAF sample without spreading every PTS
Dryvnt 9f2bd4c
Merge remote-tracking branch 'origin/main' into pr-4826
kixelated 6c5eb95
quest: restore CMAF prerequisites while held
kixelated 6871e7c
Merge remote-tracking branch 'origin/main' into quest/m2/cmaf-frame-t…
Dryvnt 9313af6
fix(hang): decode an untimed CMAF frame at its tfdt
Dryvnt fef4566
quest: finish cmaf-frame-timestamp
Dryvnt 5198094
fix(hang): a CMAF track counts in mdhd ticks, and runs lie back to back
Dryvnt 6657086
quest: drop shared-clock's tfdt decode test, which #4826 covers
Dryvnt 81405c1
fix(mux): accept a CMAF rendition's track at its init's timescale
Dryvnt 6427e31
Merge remote-tracking branch 'origin/main' into merge-4826
kixelated c4640c2
Merge remote-tracking branch 'origin/main' into merge-4826
kixelated 79f5acd
Merge remote-tracking branch 'origin/main' into merge-4826
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
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,125 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { Time } from "@moq/net"; | ||
| import type { InitSegment } from "./decode.ts"; | ||
| import { Format } from "./format.ts"; | ||
|
|
||
| const TIMESCALE = 90_000; | ||
| const INIT: InitSegment = { | ||
| timescale: TIMESCALE, | ||
| trackId: 1, | ||
| kind: "video", | ||
| defaultSampleDuration: 0, | ||
| defaultSampleSize: 0, | ||
| defaultSampleFlags: 0, | ||
| }; | ||
|
|
||
| function box(type: string, ...parts: Uint8Array[]): Uint8Array { | ||
| const size = 8 + parts.reduce((sum, part) => sum + part.byteLength, 0); | ||
| const out = new Uint8Array(size); | ||
| const view = new DataView(out.buffer); | ||
| view.setUint32(0, size); | ||
| out.set(new TextEncoder().encode(type), 4); | ||
| let offset = 8; | ||
| for (const part of parts) { | ||
| out.set(part, offset); | ||
| offset += part.byteLength; | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| function u32(...values: number[]): Uint8Array { | ||
| const out = new Uint8Array(values.length * 4); | ||
| const view = new DataView(out.buffer); | ||
| for (const [i, value] of values.entries()) view.setInt32(i * 4, value); | ||
| return out; | ||
| } | ||
|
|
||
| type Run = [duration: number, cts: number][]; | ||
|
|
||
| /** A moof+mdat at `tfdt` ticks whose runs list one-byte samples in decode order. */ | ||
| function fragment(tfdt: number, ...runs: Run[]): Uint8Array { | ||
| const count = runs.reduce((sum, run) => sum + run.length, 0); | ||
| const moof = (dataOffset: number) => { | ||
| let offset = dataOffset; | ||
| const truns = runs.map((run) => { | ||
| // Version 1 (signed CTS): data-offset, sample-duration, sample-size, and CTS present. | ||
| const trun = box("trun", u32(0x01000b01, run.length, offset), ...run.map(([d, cts]) => u32(d, 1, cts))); | ||
| offset += run.length; | ||
| return trun; | ||
| }); | ||
| return box( | ||
| "moof", | ||
| box("mfhd", u32(0, 0)), | ||
| box( | ||
| "traf", | ||
| box("tfhd", u32(0x020000, 1)), | ||
| box("tfdt", u32(0x01000000, Math.floor(tfdt / 2 ** 32), tfdt % 2 ** 32)), | ||
| ...truns, | ||
| ), | ||
| ); | ||
| }; | ||
| const header = moof(0); | ||
| return new Uint8Array([...moof(header.byteLength + 8), ...box("mdat", new Uint8Array(count))]); | ||
| } | ||
|
|
||
| // The frame timestamp is the broadcast timeline: a passthrough fragment whose `tfdt` still | ||
| // carries its source PTS decodes at the frame timestamp, keeping its B-frame order. | ||
| test("CmafFormat times samples from the frame timestamp", () => { | ||
| const source = 3_600 * TIMESCALE; | ||
| // I, P, B in decode order, presenting at +0, +2, and +1 frames. | ||
| const segment = fragment(source, [ | ||
| [3000, 3000], | ||
| [3000, 6000], | ||
| [3000, 0], | ||
| ]); | ||
|
|
||
| const frames = new Format(INIT).decode(segment, Time.Timestamp.fromMillis(10_000)); | ||
| expect(frames.map((f) => f.timestamp)).toEqual([10_000_000, 10_066_667, 10_033_333] as Time.Micro[]); | ||
| }); | ||
|
|
||
| // A fragment can open on a sample that presents after a later one (open-GOP leading pictures, | ||
| // or a cut mid-GOP). The earliest presentation time is the anchor, not the first sample. | ||
| test("CmafFormat anchors the earliest presentation time", () => { | ||
| const segment = fragment(0, [ | ||
| [3000, 6000], | ||
| [3000, 0], | ||
| ]); | ||
|
|
||
| const frames = new Format(INIT).decode(segment, Time.Timestamp.fromMillis(5_000)); | ||
| expect(frames.map((f) => f.timestamp)).toEqual([5_033_333, 5_000_000] as Time.Micro[]); | ||
| }); | ||
|
|
||
| // Runs continue one decode timeline, so the anchor is the earliest sample across all of them. | ||
| test("CmafFormat anchors the earliest sample across runs", () => { | ||
| const segment = fragment(0, [[3000, 6000]], [[3000, 0]]); | ||
|
|
||
| const frames = new Format(INIT).decode(segment, at(3000)); | ||
| expect(frames.map((f) => f.timestamp)).toEqual([66_667, 33_333] as Time.Micro[]); | ||
| }); | ||
|
|
||
| // An untimed track's frames carry no broadcast time, so their samples present at `tfdt`. | ||
| test("CmafFormat times an untimed fragment from its tfdt", () => { | ||
| const segment = fragment(TIMESCALE, [ | ||
| [3000, 3000], | ||
| [3000, 6000], | ||
| [3000, 0], | ||
| ]); | ||
|
|
||
| const frames = new Format(INIT).decode(segment, undefined); | ||
| expect(frames.map((f) => f.timestamp)).toEqual([1_033_333, 1_100_000, 1_066_667] as Time.Micro[]); | ||
| }); | ||
|
|
||
| // Samples are read front to back, so a run that skips bytes is refused rather than sliced wrong. | ||
| test("CmafFormat refuses a run that doesn't start at the next sample", () => { | ||
| const segment = fragment(0, [[3000, 0]], [[3000, 0]]); | ||
| const view = new DataView(segment.buffer); | ||
| // The second trun's data_offset follows its type, version/flags, and sample count. | ||
| const trun = segment.findLastIndex((_, i) => new TextDecoder().decode(segment.subarray(i, i + 4)) === "trun"); | ||
| view.setInt32(trun + 12, view.getInt32(trun + 12) + 1); | ||
|
|
||
| expect(() => new Format(INIT).decode(segment, undefined)).toThrow(/data_offset/); | ||
| }); | ||
|
|
||
| function at(ticks: number): Time.Timestamp { | ||
| return new Time.Timestamp(ticks, Time.Timescale(TIMESCALE)); | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: moq-dev/moq
Length of output: 12170
🏁 Script executed:
Repository: moq-dev/moq
Length of output: 37996
Honor each run’s data offset.
When a later run’s
dataOffsetskips bytes inmdat, the new loop can read from the preceding cursor and return bytes from the wrong location. Use each run’sdataOffsetand applicabletfhdbase when selecting its samples.🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not in this PR. Both decoders walk
mdatsequentially and ignoredata_offset; Rust'sfmp4::decodesays so, and the JS TODO above the loop flags it. That behavior predates this change, which only extends it from the first run to all of them, matching Rust. A run that skips bytes would need both fixed together, so it's better as a follow-up.(Written by Claude Opus 5.5)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Dryvnt, thanks for clarifying the scope. The JS TODO documents the existing sequentialmdatbehavior. This PR extends that behavior to all runs; it does not introducedataOffsetsupport.A follow-up should add offset handling to both decoders. Would you like me to open a GitHub issue for that work?
✏️ Learnings added
You are interacting with an AI system.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kixelated, your call on whether this needs tracking. Both CMAF decoders (Rust
fmp4::decodeand JSdecodeDataSegment) readmdatfront to back and ignore each run'strun.data_offset. That matters only for a producer whose runs skip bytes, and we know of none in-tree. If you want it fixed, the follow-up would honordata_offset(relative to thetfhdbase) in both decoders together; this PR leaves it as is.(Written by Claude Opus 5.5)