Skip to content

Function: createUuidReader() ​

ts
function createUuidReader<V>(readers): (cursor) => undefined;

Defined in: readers/createUuidReader.ts:31

Creates the reader of the uuid key of a readers map. The reader selects a reader of readers by the user type of the box.

The reader reads the 16 bytes of usertype and calls the reader of that user type with the user type as a string. For a user type with no reader, the reader returns undefined, and readIsoBoxes returns a raw box.

A package that reads its own uuid boxes can export its readers map. A caller then joins the maps of several packages in one call: createUuidReader({ ...readersA, ...readersB }).

Type Parameters ​

V ​

V extends IsoInt64 = number

Parameters ​

readers ​

IsoUuidBoxReaderMap<V>

Map of each user type to the reader of that box.

Returns ​

The reader of the uuid key.

(cursor) => undefined

Throws ​

RangeError if a key of readers is not a UUID in lowercase hexadecimal digits with hyphens.

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 }) } })
}

A Streaming Video Technology Alliance project.