rtpPacket(): Coder<RtpPacket>
Creates a coder for an RTP packet's fixed header, CSRC list, and payload (RFC 3550).
The CSRC list's length is derived from versionFlags.csrcCount, so a
packet with no contributing sources needs csrcCount: 0 and csrc: [].
Nothing is validated on encode beyond what bitStruct enforces (field
values must fit their bit widths) — csrcCount and csrc.length are not
cross-checked, matching this repo's no-defensive-programming stance.
Header extensions are not parsed; the rest of the buffer after the CSRC
list — extension block included, if extension is set — is surfaced
verbatim as payload.
Round-trip a packet with two CSRC identifiers
Round-trip a packet with two CSRC identifiers
import { assertEquals } from "@std/assert"; import { rtpPacket, RTP_HEADER_MIN_SIZE, RTP_VERSION } from "@binstruct/rtp"; const coder = rtpPacket(); const payload = new Uint8Array([0x01, 0x02, 0x03]); const packet = { versionFlags: { version: RTP_VERSION, padding: 0, extension: 0, csrcCount: 2 }, markerPayloadType: { marker: 1, payloadType: 8 }, sequenceNumber: 4321, timestamp: 160000, ssrc: 0xcafebabe, csrc: [0x11111111, 0x22222222], payload, }; const buffer = new Uint8Array(RTP_HEADER_MIN_SIZE + 8 + payload.length); const written = coder.encode(packet, buffer); const [decoded, read] = coder.decode(buffer.subarray(0, written)); assertEquals(written, RTP_HEADER_MIN_SIZE + 8 + payload.length); assertEquals(read, written); assertEquals(decoded.csrc, packet.csrc); assertEquals(decoded.markerPayloadType.marker, 1); assertEquals(decoded.payload, payload);
Decode a known-wire minimal packet (no CSRC, no payload)
Decode a known-wire minimal packet (no CSRC, no payload)
import { assertEquals } from "@std/assert"; import { rtpPacket, RTP_HEADER_MIN_SIZE, RTP_VERSION } from "@binstruct/rtp"; // deno-fmt-ignore const wire = new Uint8Array([ 0x80, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a, ]); const [decoded, read] = rtpPacket().decode(wire); assertEquals(read, RTP_HEADER_MIN_SIZE); assertEquals(decoded.versionFlags.version, RTP_VERSION); assertEquals(decoded.versionFlags.csrcCount, 0); assertEquals(decoded.sequenceNumber, 1); assertEquals(decoded.ssrc, 0x2a); assertEquals(decoded.csrc, []); assertEquals(decoded.payload.length, 0);