-
Notifications
You must be signed in to change notification settings - Fork 135
feat: Add raw container format with fixed u64 timestamp encoding (JS) #784
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 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a0669c7
add raw container support for encoding/decoding the PTS direclty wit…
gdavila 4833764
fix DEFAULT_CONTAINER ussage across pub/sub
gdavila 1e6a5cc
minor formating fixes
gdavila 0074dd5
fix container initialization
gdavila 099028d
fix minor formating isssues
gdavila 44bd418
move container into encoderProps
gdavila 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| /** | ||
| * Container format for frame timestamp encoding. | ||
| * | ||
| * - "legacy": Uses QUIC VarInt encoding (1-8 bytes, variable length) | ||
| * - "raw": Uses fixed u64 encoding (8 bytes, big-endian) | ||
| * - "fmp4": Fragmented MP4 container (future) | ||
| */ | ||
| export const ContainerSchema = z.enum(["legacy", "raw", "fmp4"]); | ||
|
|
||
| export type Container = z.infer<typeof ContainerSchema>; | ||
|
|
||
| /** | ||
| * Default container format when not specified. | ||
| * Set to legacy for backward compatibility. | ||
| */ | ||
| export const DEFAULT_CONTAINER: Container = "legacy"; |
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,155 @@ | ||
| import type * as Catalog from "../catalog"; | ||
| import { DEFAULT_CONTAINER } from "../catalog"; | ||
| import type * as Time from "../time"; | ||
|
|
||
| /** | ||
| * Encodes a timestamp according to the specified container format. | ||
| * | ||
| * @param timestamp - The timestamp in microseconds | ||
| * @param container - The container format to use | ||
| * @returns The encoded timestamp as a Uint8Array | ||
| */ | ||
| export function encodeTimestamp(timestamp: Time.Micro, container: Catalog.Container = DEFAULT_CONTAINER): Uint8Array { | ||
| switch (container) { | ||
| case "legacy": | ||
| return encodeVarInt(timestamp); | ||
| case "raw": | ||
| return encodeU64(timestamp); | ||
| case "fmp4": | ||
| throw new Error("fmp4 container not yet implemented"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Decodes a timestamp from a buffer according to the specified container format. | ||
| * | ||
| * @param buffer - The buffer containing the encoded timestamp | ||
| * @param container - The container format to use | ||
| * @returns [timestamp in microseconds, remaining buffer after timestamp] | ||
| */ | ||
| export function decodeTimestamp( | ||
| buffer: Uint8Array, | ||
| container: Catalog.Container = DEFAULT_CONTAINER, | ||
| ): [Time.Micro, Uint8Array] { | ||
| switch (container) { | ||
| case "legacy": { | ||
| const [value, remaining] = decodeVarInt(buffer); | ||
| return [value as Time.Micro, remaining]; | ||
| } | ||
| case "raw": { | ||
| const [value, remaining] = decodeU64(buffer); | ||
| return [value as Time.Micro, remaining]; | ||
| } | ||
| case "fmp4": | ||
| throw new Error("fmp4 container not yet implemented"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets the size in bytes of an encoded timestamp for the given container format. | ||
| * For variable-length formats, returns the maximum size. | ||
| * | ||
| * @param container - The container format | ||
| * @returns Size in bytes | ||
| */ | ||
| export function getTimestampSize(container: Catalog.Container = DEFAULT_CONTAINER): number { | ||
| switch (container) { | ||
| case "legacy": | ||
| return 8; // VarInt maximum size | ||
| case "raw": | ||
| return 8; // u64 fixed size | ||
| case "fmp4": | ||
| throw new Error("fmp4 container not yet implemented"); | ||
| } | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // LEGACY VARINT IMPLEMENTATION | ||
| // ============================================================================ | ||
|
|
||
| const MAX_U6 = 2 ** 6 - 1; | ||
| const MAX_U14 = 2 ** 14 - 1; | ||
| const MAX_U30 = 2 ** 30 - 1; | ||
| const MAX_U53 = Number.MAX_SAFE_INTEGER; | ||
|
|
||
| function decodeVarInt(buf: Uint8Array): [number, Uint8Array] { | ||
| const size = 1 << ((buf[0] & 0xc0) >> 6); | ||
|
|
||
| const view = new DataView(buf.buffer, buf.byteOffset, size); | ||
| const remain = new Uint8Array(buf.buffer, buf.byteOffset + size, buf.byteLength - size); | ||
| let v: number; | ||
|
|
||
| if (size === 1) { | ||
| v = buf[0] & 0x3f; | ||
| } else if (size === 2) { | ||
| v = view.getUint16(0) & 0x3fff; | ||
| } else if (size === 4) { | ||
| v = view.getUint32(0) & 0x3fffffff; | ||
| } else if (size === 8) { | ||
| // NOTE: Precision loss above 2^52 | ||
| v = Number(view.getBigUint64(0) & 0x3fffffffffffffffn); | ||
| } else { | ||
| throw new Error("impossible"); | ||
| } | ||
|
|
||
| return [v, remain]; | ||
| } | ||
|
|
||
| function encodeVarInt(v: number): Uint8Array { | ||
| const dst = new Uint8Array(8); | ||
|
|
||
| if (v <= MAX_U6) { | ||
| dst[0] = v; | ||
| return new Uint8Array(dst.buffer, dst.byteOffset, 1); | ||
| } | ||
|
|
||
| if (v <= MAX_U14) { | ||
| const view = new DataView(dst.buffer, dst.byteOffset, 2); | ||
| view.setUint16(0, v | 0x4000); | ||
| return new Uint8Array(view.buffer, view.byteOffset, view.byteLength); | ||
| } | ||
|
|
||
| if (v <= MAX_U30) { | ||
| const view = new DataView(dst.buffer, dst.byteOffset, 4); | ||
| view.setUint32(0, v | 0x80000000); | ||
| return new Uint8Array(view.buffer, view.byteOffset, view.byteLength); | ||
| } | ||
|
|
||
| if (v <= MAX_U53) { | ||
| const view = new DataView(dst.buffer, dst.byteOffset, 8); | ||
| view.setBigUint64(0, BigInt(v) | 0xc000000000000000n); | ||
| return new Uint8Array(view.buffer, view.byteOffset, view.byteLength); | ||
| } | ||
|
|
||
| throw new Error(`overflow, value larger than 53-bits: ${v}`); | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // RAW U64 IMPLEMENTATION | ||
| // ============================================================================ | ||
|
|
||
| /** | ||
| * Decodes a fixed 8-byte big-endian unsigned 64-bit integer. | ||
| */ | ||
| function decodeU64(buf: Uint8Array): [number, Uint8Array] { | ||
| if (buf.byteLength < 8) { | ||
| throw new Error("Buffer too short for u64 decode"); | ||
| } | ||
|
|
||
| const view = new DataView(buf.buffer, buf.byteOffset, 8); | ||
| const value = Number(view.getBigUint64(0)); | ||
| const remain = new Uint8Array(buf.buffer, buf.byteOffset + 8, buf.byteLength - 8); | ||
|
|
||
| return [value, remain]; | ||
| } | ||
|
|
||
| /** | ||
| * Encodes a number as a fixed 8-byte big-endian unsigned 64-bit integer. | ||
| * Much simpler than VarInt! | ||
| */ | ||
| function encodeU64(v: number): Uint8Array { | ||
| const dst = new Uint8Array(8); | ||
| const view = new DataView(dst.buffer, dst.byteOffset, 8); | ||
| view.setBigUint64(0, BigInt(v)); | ||
| return dst; | ||
| } | ||
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 @@ | ||
| export * from "./codec"; |
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.
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.
Is this used? I don't think it's useful.
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.
Yeah. true. Removed.