Skip to content
Class

EncodedPacket ​

Represents an encoded chunk of media. Mainly used as an expressive wrapper around WebCodecs API's EncodedVideoChunk and EncodedAudioChunk, but can also be used standalone.

Used by ​

Constructor ​

ts
constructor(
	data: Uint8Array,
	type: PacketType,
	timestamp: number,
	duration: number,
	sequenceNumber: number = -1,
	byteLength?: number,
	sideData?: EncodedPacketSideData,
): EncodedPacket;

Creates a new EncodedPacket from raw bytes and timing information.

See PacketType and EncodedPacketSideData.

Static methods ​

fromEncodedChunk() ​

ts
static fromEncodedChunk(
	chunk: EncodedVideoChunk | EncodedAudioChunk,
	sideData?: EncodedPacketSideData,
): EncodedPacket;

Creates an EncodedPacket from an EncodedVideoChunk or EncodedAudioChunk. This method is useful for converting chunks from the WebCodecs API to EncodedPacket instances.

See EncodedPacketSideData.

Properties ​

byteLength ​

ts
readonly byteLength: number;

The actual byte length of the data in this packet. This field is useful for metadata-only packets where the data field contains no bytes.

data ​

ts
readonly data: Uint8Array;

The encoded data of this packet. For any given codec, this data must adhere to the format specified in the Mediabunny Codec Registry.

duration ​

ts
readonly duration: number;

The duration of this packet in seconds.

isMetadataOnly ​

ts
get isMetadataOnly(): boolean;

If this packet is a metadata-only packet. Metadata-only packets don't contain their packet data. They are the result of retrieving packets with PacketRetrievalOptions.metadataOnly set to true.

microsecondDuration ​

ts
get microsecondDuration(): number;

The duration of this packet in microseconds.

microsecondTimestamp ​

ts
get microsecondTimestamp(): number;

The timestamp of this packet in microseconds.

sequenceNumber ​

ts
readonly sequenceNumber: number;

The sequence number indicates the decode order of the packets. Packet A must be decoded before packet B if A has a lower sequence number than B. If two packets have the same sequence number, they are the same packet. Otherwise, sequence numbers are arbitrary and are not guaranteed to have any meaning besides their relative ordering. Negative sequence numbers mean the sequence number is undefined.

sideData ​

ts
readonly sideData: EncodedPacketSideData;

Additional data carried with this packet.

See EncodedPacketSideData.

timestamp ​

ts
readonly timestamp: number;

The presentation timestamp of this packet in seconds. May be negative. Samples with negative end timestamps should not be presented.

type ​

ts
readonly type: PacketType;

The type of this packet.

See PacketType.

Methods ​

alphaToEncodedVideoChunk() ​

ts
alphaToEncodedVideoChunk(
	type: PacketType = this.type,
): EncodedVideoChunk;

Converts this packet to an EncodedVideoChunk for use with the WebCodecs API, using the alpha side data instead of the color data. Throws if no alpha side data is defined.

See PacketType.

clone() ​

ts
clone(
	options?: {
		/** The data of the cloned packet. */
		data?: Uint8Array;
		/** The type of the cloned packet. */
		type?: PacketType;
		/** The timestamp of the cloned packet in seconds. */
		timestamp?: number;
		/** The duration of the cloned packet in seconds. */
		duration?: number;
		/** The sequence number of the cloned packet. */
		sequenceNumber?: number;
		/** The side data of the cloned packet. */
		sideData?: EncodedPacketSideData;
	},
): EncodedPacket;

Clones this packet while optionally modifying the new packet's data.

See PacketType and EncodedPacketSideData.

toEncodedAudioChunk() ​

ts
toEncodedAudioChunk(): EncodedAudioChunk;

Converts this packet to an EncodedAudioChunk for use with the WebCodecs API.

toEncodedVideoChunk() ​

ts
toEncodedVideoChunk(): EncodedVideoChunk;

Converts this packet to an EncodedVideoChunk for use with the WebCodecs API.

Released under the Mozilla Public License 2.0.