rtci_sdp/lib.rs
1#![warn(rust_2018_idioms)]
2#![warn(missing_docs)]
3#![allow(dead_code)]
4
5//! SDP parsing and serialization.
6//!
7//! The Session Description Protocol ([RFC 8866], superseding [RFC 4566]) as WebRTC uses
8//! it: offers and answers, media sections, and the attributes that carry codecs, ICE
9//! candidates, DTLS fingerprints and header-extension mappings ([RFC 8285]).
10//!
11//! # Structure
12//!
13//! * [`SessionDescription`] — a whole session description: `unmarshal` one from a string,
14//! `marshal` it back, or build one up section by section. `unmarshal` is the RFC 8866
15//! section 9 grammar; [`ParseOptions`] names what else an application may ask it to
16//! accept.
17//! * [`MediaDescription`] — one `m=` section, with its attributes, formats and connection
18//! data.
19//! * [`attribute`] — typed, validated attributes: one type per attribute the WebRTC
20//! specifications use (`mid`, `msid`, `rid`, `simulcast`, `rtcp-fb`, `candidate`,
21//! `fingerprint`, ...), each parsed against the ABNF of its RFC, with an accessor on
22//! the two description types; [`SessionDescription::validate`] to check a whole
23//! description; and [`attribute::mux`], the RFC 8859 multiplexing category of every
24//! attribute.
25//! * [`extmap`] — `a=extmap` header-extension declarations and the well-known extension
26//! URIs.
27//! * [`direction`] — `sendrecv`/`sendonly`/`recvonly`/`inactive`.
28//!
29//! # Example
30//!
31//! ```
32//! use rtci_sdp::SessionDescription;
33//! use std::io::Cursor;
34//!
35//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
36//! let sdp = "v=0\r\n\
37//! o=- 0 0 IN IP4 127.0.0.1\r\n\
38//! s=-\r\n\
39//! t=0 0\r\n\
40//! m=audio 9 UDP/TLS/RTP/SAVPF 111\r\n\
41//! a=mid:0\r\n\
42//! a=sendrecv\r\n";
43//!
44//! let desc = SessionDescription::unmarshal(&mut Cursor::new(sdp))?;
45//! for media in &desc.media_descriptions {
46//! assert_eq!(media.media_name.media, "audio");
47//! assert_eq!(media.attribute("mid").flatten(), Some("0"));
48//! }
49//!
50//! // Printing it back yields valid SDP.
51//! assert!(desc.marshal().starts_with("v=0"));
52//! # Ok(())
53//! # }
54//! ```
55//!
56//! Attribute values are kept as text by the parser and get their grammar from the typed
57//! accessors:
58//!
59//! ```
60//! use rtci_sdp::SessionDescription;
61//! use rtci_sdp::attribute::RtcpFbKind;
62//!
63//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
64//! let desc: SessionDescription = "v=0\r\n\
65//! o=- 0 0 IN IP4 127.0.0.1\r\n\
66//! s=-\r\n\
67//! t=0 0\r\n\
68//! a=group:BUNDLE 0\r\n\
69//! m=video 9 UDP/TLS/RTP/SAVPF 96\r\n\
70//! c=IN IP4 0.0.0.0\r\n\
71//! a=mid:0\r\n\
72//! a=rtpmap:96 VP8/90000\r\n\
73//! a=rtcp-fb:96 nack pli\r\n\
74//! a=rid:hi send max-width=1280\r\n\
75//! a=simulcast:send hi\r\n"
76//! .parse()?;
77//! desc.validate()?;
78//! assert!(desc.bundle_groups()?[0].contains("0"));
79//!
80//! let video = &desc.media_descriptions[0];
81//! assert_eq!(video.rtpmaps()?[0].clock_rate, 90000);
82//! assert_eq!(video.rtcp_fbs()?[0].kind(), RtcpFbKind::NackPli);
83//! assert_eq!(video.rids()?[0].max_width(), Some(1280));
84//! assert!(video.simulcast()?.is_some());
85//! # Ok(())
86//! # }
87//! # example().unwrap();
88//! ```
89//!
90//! This crate is deliberately a *syntax* layer: it parses, checks and prints SDP faithfully
91//! and leaves negotiation semantics ([RFC 9429]) to the [`rtci`](https://docs.rs/rtci) crate,
92//! which re-exports it as `rtci::sdp`.
93//!
94//! [RFC 8866]: https://datatracker.ietf.org/doc/html/rfc8866
95//! [RFC 4566]: https://datatracker.ietf.org/doc/html/rfc4566
96//! [RFC 8285]: https://datatracker.ietf.org/doc/html/rfc8285
97//! [RFC 9429]: https://datatracker.ietf.org/doc/html/rfc9429
98
99/// Typed, validated attributes, and the RFC 8859 multiplexing categories.
100pub mod attribute;
101/// Session and media descriptions — the `v=`/`m=` structure of an SDP document.
102pub mod description;
103/// Transmission direction (`sendrecv`, `sendonly`, `recvonly`, `inactive`).
104pub mod direction;
105/// `a=extmap` RTP header-extension declarations and the well-known extension URIs.
106pub mod extmap;
107/// The checks the fuzz targets in `fuzz/` run, shared with the replay test of
108/// `fuzz/regressions/`. Built with `cfg(fuzzing)` (cargo-fuzz) or for this crate's tests; not
109/// part of the supported API.
110#[cfg(any(fuzzing, test))]
111#[doc(hidden)]
112pub mod fuzz_oracles;
113#[cfg(test)]
114mod fuzz_regressions_test;
115/// Parsing helpers plus the codec and connection-role types shared across descriptions.
116pub mod util;
117
118pub(crate) mod grammar;
119pub(crate) mod lexer;
120
121pub use description::{
122 media::MediaDescription,
123 session::{ParseOptions, SdpDecimal, SessionDescription},
124};