Skip to main content

rtc_rtp/
lib.rs

1#![warn(rust_2018_idioms)]
2#![warn(missing_docs)]
3#![allow(dead_code)]
4
5//! RTP packets, header extensions and packetization.
6//!
7//! The Real-time Transport Protocol ([RFC 3550]) wire format, plus the pieces WebRTC needs
8//! around it: one-byte and two-byte header extensions ([RFC 8285]) and per-codec
9//! packetizers that turn encoded frames into RTP payloads.
10//!
11//! # Structure
12//!
13//! * [`Packet`] / [`Header`] — the packet and its header: `unmarshal` one off the wire,
14//!   `marshal` one back, get and set header extensions by id.
15//! * [`packetizer`] — [`Packetizer`](packetizer::Packetizer), which fragments a frame into
16//!   MTU-sized payloads, and the per-codec [`Payloader`](packetizer::Payloader)
17//!   implementations in [`codec`] (VP8, VP9, H.264, H.265, AV1, Opus, G.711).
18//! * [`sequence`] — [`Sequencer`](sequence::Sequencer), for sequence numbers that start at a
19//!   random offset as the RFC requires.
20//! * [`extension`] — the typed header extensions: audio level ([RFC 6464]), video
21//!   orientation, transport-wide CC, and the SDES stream ids used for simulcast.
22//!
23//! # Example
24//!
25//! ```
26//! use bytes::Bytes;
27//! use rtc_rtp::Packet;
28//! use shared::marshal::{Marshal, Unmarshal};
29//!
30//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
31//! // A minimal RTP packet: version 2, payload type 96, one byte of payload.
32//! let raw = Bytes::from_static(&[
33//!     0x80, 0x60, 0x00, 0x01, // V=2, PT=96, seq=1
34//!     0x00, 0x00, 0x00, 0x20, // timestamp
35//!     0xDE, 0xAD, 0xBE, 0xEF, // ssrc
36//!     0xAA, // payload
37//! ]);
38//!
39//! let mut buf = raw.clone();
40//! let packet = Packet::unmarshal(&mut buf)?;
41//! assert_eq!(packet.header.payload_type, 96);
42//! assert_eq!(packet.header.sequence_number, 1);
43//! assert_eq!(packet.header.ssrc, 0xDEAD_BEEF);
44//!
45//! // Re-encoding reproduces the original bytes.
46//! assert_eq!(packet.marshal()?, raw);
47//! # Ok(())
48//! # }
49//! ```
50//!
51//! Most applications do not depend on this crate directly — the
52//! [`rtc`](https://docs.rs/rtc) crate re-exports it as `rtc::rtp`, and an application
53//! usually meets these types when reading or writing media on a track.
54//!
55//! [RFC 3550]: https://datatracker.ietf.org/doc/html/rfc3550
56//! [RFC 8285]: https://datatracker.ietf.org/doc/html/rfc8285
57//! [RFC 6464]: https://datatracker.ietf.org/doc/html/rfc6464
58
59/// Per-codec payloaders and depacketizers (VP8, VP9, AV1, H.264, H.265, Opus, G.711).
60pub mod codec;
61/// The typed RTP header extensions ([RFC 8285]).
62///
63/// [RFC 8285]: https://datatracker.ietf.org/doc/html/rfc8285
64pub mod extension;
65/// The RTP header, its extensions, and the bit masks that encode it.
66pub mod header;
67/// A whole RTP packet: header plus payload.
68pub mod packet;
69/// Turning encoded frames into RTP packets, and back again.
70pub mod packetizer;
71/// Sequence-number generation, starting from a random offset as the RFC requires.
72pub mod sequence;
73
74pub use header::Header;
75pub use packet::Packet;