Interface: IsoCustomUuidBoxMap
Defined in: IsoCustomUuidBoxMap.ts:30
Map of each user type of a uuid box to the type of that box. The map is empty.
A key is a UUID in lowercase hexadecimal digits with hyphens, as in 6d1d9b05-42d5-44e6-80e2-141daff757b2. A key with an uppercase digit compiles, but createUuidReader and createUuidWriter throw a RangeError for that key. A box type has type: 'uuid' and the key as its userType field. To add a user type, declare the interface again in a declare module block:
ts
const TFXD = '6d1d9b05-42d5-44e6-80e2-141daff757b2'
type TfxdBox = { type: 'uuid'; userType: typeof TFXD; fragmentDuration: number }
declare module '@svta/cml-iso-bmff' {
interface IsoCustomUuidBoxMap {
[TFXD]: TfxdBox
}
}IsoBoxMap['uuid'] is then the union of the declared box types. The test box.userType === key narrows a uuid box to one box type. The declaration applies to every file of the program. Two declarations of one user type need the same type.
Example
ts
// The tfxd box of Smooth Streaming.
const TFXD = '6d1d9b05-42d5-44e6-80e2-141daff757b2'
type TfxdBox = {
type: 'uuid'
userType: typeof TFXD
version: number
flags: number
fragmentAbsoluteTime: number
fragmentDuration: number
}
declare module '@svta/cml-iso-bmff' {
// oxlint-disable-next-line typescript/consistent-type-definitions
interface IsoCustomUuidBoxMap {
[TFXD]: TfxdBox
}
}
const readTfxd = (cursor: IsoBoxReadCursor, userType: typeof TFXD): TfxdBox => {
const version = readUint8(cursor)
const flags = readUint24(cursor)
const size = version === 1 ? 8 : 4
return {
type: 'uuid',
userType,
version,
flags,
fragmentAbsoluteTime: readWideUint(cursor, size),
fragmentDuration: readWideUint(cursor, size),
}
}
const writeTfxd = (cursor: IsoBoxWriteCursor, box: TfxdBox): void => {
const size = box.version === 1 ? 8 : 4
writeUint8(cursor, box.version)
writeUint24(cursor, box.flags)
writeWideUint(cursor, box.fragmentAbsoluteTime, size)
writeWideUint(cursor, box.fragmentDuration, size)
}
function readFragmentDurations(segment: Uint8Array): number[] {
const boxes = readIsoBoxes(segment, {
readers: { tfdt: readTfdt, uuid: createUuidReader({ [TFXD]: readTfxd }) },
})
const durations: number[] = []
for (const box of boxes) {
// The guard excludes a raw box, and the test of `userType` narrows the box to `TfxdBox`.
if (isIsoBoxType('uuid', box) && box.userType === TFXD) {
durations.push(box.fragmentDuration)
}
}
return durations
}
function writeTfxdBox(box: TfxdBox): Uint8Array {
return writeIsoBoxes([box], { writers: { uuid: createUuidWriter({ [TFXD]: writeTfxd }) } })
}