moq_rtc/sdp.rs
1//! SDP plumbing.
2//!
3//! WHIP/WHEP both shovel SDP between a peer and str0m as `application/sdp`
4//! request/response bodies. The only thing we add on top of str0m's offer/answer
5//! parse/serialize is a tiny wrapper to keep the call sites readable.
6
7use std::str::FromStr;
8
9use crate::{Error, Result};
10
11/// Parse an `application/sdp` body as an offer.
12pub fn parse_offer(body: &str) -> Result<str0m::change::SdpOffer> {
13 str0m::change::SdpOffer::from_sdp_string(body).map_err(|err| Error::InvalidSdp(err.to_string()))
14}
15
16/// Serialize an SDP answer for the `application/sdp` response body.
17pub fn render_answer(answer: &str0m::change::SdpAnswer) -> String {
18 answer.to_sdp_string()
19}
20
21/// Build a stable WHIP/WHEP resource identifier from a UUID v4.
22pub fn new_resource_id() -> String {
23 uuid::Uuid::new_v4().to_string()
24}
25
26/// Parse a `Location:`-style resource path into its trailing UUID component.
27///
28/// WHIP DELETEs come back to `/<broadcast>/<resource-id>`; this strips
29/// everything but the id so the gateway can look up the session.
30pub fn parse_resource_id(path: &str) -> Result<uuid::Uuid> {
31 let last = path
32 .rsplit('/')
33 .find(|s| !s.is_empty())
34 .ok_or_else(|| Error::InvalidSdp("missing resource id".into()))?;
35 uuid::Uuid::from_str(last).map_err(|err| Error::InvalidSdp(err.to_string()))
36}