Skip to content

Function: createUuidWriter() ​

ts
function createUuidWriter(writers): IsoBoxWriter<never>;

Defined in: writers/createUuidWriter.ts:29

Creates the writer of the uuid key of a writers map. The writer selects a writer of writers by the userType field of the box.

The writer writes the box header and the 16 bytes of usertype. It then calls the writer of that user type, and writes the size.

Parameters ​

writers ​

IsoUuidBoxWriterMap

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

Returns ​

IsoBoxWriter<never>

The writer of the uuid key.

Throws ​

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

Throws ​

Error if the writer is called with a box whose user type has no writer.

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.