rtci_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), with the
18//! payload formats an application drives itself: telephone-event ([RFC 4733]), comfort noise
19//! ([RFC 3389]) and RED ([RFC 2198]).
20//! * [`sequence`] — [`Sequencer`](sequence::Sequencer), for sequence numbers that start at a
21//! random offset as the RFC requires.
22//! * [`extension`] — the typed header extensions: audio level ([RFC 6464]), video
23//! orientation, transport-wide CC, and the SDES stream ids used for simulcast.
24//! * [`header::validity`] and [`sequence::SourceSequence`] — the validity checks of RFC 3550
25//! Appendix A.1.
26//! * [`clock_rate`] — the rules for a stream that changes clock rate ([RFC 7160]).
27//!
28//! # Example
29//!
30//! ```
31//! use bytes::Bytes;
32//! use rtci_rtp::Packet;
33//! use shared::marshal::{Marshal, Unmarshal};
34//!
35//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
36//! // A minimal RTP packet: version 2, payload type 96, one byte of payload.
37//! let raw = Bytes::from_static(&[
38//! 0x80, 0x60, 0x00, 0x01, // V=2, PT=96, seq=1
39//! 0x00, 0x00, 0x00, 0x20, // timestamp
40//! 0xDE, 0xAD, 0xBE, 0xEF, // ssrc
41//! 0xAA, // payload
42//! ]);
43//!
44//! let mut buf = raw.clone();
45//! let packet = Packet::unmarshal(&mut buf)?;
46//! assert_eq!(packet.header.payload_type, 96);
47//! assert_eq!(packet.header.sequence_number, 1);
48//! assert_eq!(packet.header.ssrc, 0xDEAD_BEEF);
49//!
50//! // Re-encoding reproduces the original bytes.
51//! assert_eq!(packet.marshal()?, raw);
52//! # Ok(())
53//! # }
54//! ```
55//!
56//! Most applications do not depend on this crate directly — the
57//! [`rtci`](https://docs.rs/rtc) crate re-exports it as `rtci::rtp`, and an application
58//! usually meets these types when reading or writing media on a track.
59//!
60//! [RFC 3550]: https://datatracker.ietf.org/doc/html/rfc3550
61//! [RFC 8285]: https://datatracker.ietf.org/doc/html/rfc8285
62//! [RFC 6464]: https://datatracker.ietf.org/doc/html/rfc6464
63//! [RFC 4733]: https://datatracker.ietf.org/doc/html/rfc4733
64//! [RFC 3389]: https://datatracker.ietf.org/doc/html/rfc3389
65//! [RFC 2198]: https://datatracker.ietf.org/doc/html/rfc2198
66//! [RFC 7160]: https://datatracker.ietf.org/doc/html/rfc7160
67
68/// Several clock rates in one RTP session ([RFC 7160]): the SSRC, timestamp and jitter rules.
69///
70/// [RFC 7160]: https://datatracker.ietf.org/doc/html/rfc7160
71pub mod clock_rate;
72/// Per-codec payloaders and depacketizers (VP8, VP9, AV1, H.264, H.265, Opus, G.711), and
73/// telephone-event, comfort noise and RED.
74pub mod codec;
75/// The typed RTP header extensions ([RFC 8285]).
76///
77/// [RFC 8285]: https://datatracker.ietf.org/doc/html/rfc8285
78pub mod extension;
79/// Entry points for stateful fuzz targets. Gated behind `cfg(fuzzing)`; not part of the
80/// supported API.
81#[cfg(fuzzing)]
82pub mod fuzzing;
83pub mod header;
84/// A whole RTP packet: header plus payload.
85pub mod packet;
86/// Turning encoded frames into RTP packets, and back again.
87pub mod packetizer;
88/// Sequence-number generation, starting from a random offset as the RFC requires.
89pub mod sequence;
90
91pub use header::Header;
92pub use packet::Packet;