pub struct RTCSessionDescription {
pub sdp_type: RTCSdpType,
pub sdp: String,
/* private fields */
}Expand description
Represents a session description in the SDP offer/answer model.
RTCSessionDescription is used to expose local and remote session descriptions
in WebRTC. It contains the SDP (Session Description Protocol) text that describes
media capabilities, transport addresses, codecs, and other session parameters.
§Structure
The session description consists of:
sdp_type: The type of description (RTCSdpType)sdp: The SDP content as a string (text format defined in RFC 8866)parsed: Internal cached parsed representation (not serialized)
§Usage Pattern
Session descriptions are typically:
- Created using
offer(),answer(), orpranswer()constructor methods - Serialized to JSON for transmission over the signaling channel
- Deserialized on the remote peer
- Applied to the peer connection to establish media
§Examples
§Creating an Offer Description
use rtc::peer_connection::sdp::{RTCSessionDescription, RTCSdpType};
// In a real application, this SDP would come from create_offer()
let sdp_text = r#"v=0
o=- 123456789 2 IN IP4 127.0.0.1
s=-
t=0 0
m=audio 9 UDP/TLS/RTP/SAVPF 111
"#.to_string();
// Wrap in RTCSessionDescription
let offer = RTCSessionDescription::offer(sdp_text)?;
assert_eq!(offer.sdp_type, RTCSdpType::Offer);
println!("Offer ready to send: {}", offer);§Creating an Answer Description
use rtc::peer_connection::sdp::RTCSessionDescription;
let sdp_text = "v=0\r\no=- 987654321 2 IN IP4 127.0.0.1\r\n...".to_string();
let answer = RTCSessionDescription::answer(sdp_text)?;
println!("Answer SDP type: {}", answer.sdp_type);§Signaling Exchange via JSON
use rtc::peer_connection::sdp::{RTCSessionDescription, RTCSdpType};
// Peer A: Create and serialize offer
let offer = RTCSessionDescription::offer("v=0...".to_string())?;
let json = serde_json::to_string(&offer)?;
// Send json over signaling channel (WebSocket, HTTP, etc.)
// Peer B: Receive and deserialize offer
let received_offer: RTCSessionDescription = serde_json::from_str(&json)?;
assert_eq!(received_offer.sdp_type, RTCSdpType::Offer);
// Peer B: Create answer (after set_remote_description and create_answer)
let answer = RTCSessionDescription::answer("v=0...".to_string())?;
let answer_json = serde_json::to_string(&answer)?;
// Send answer_json back to Peer A§Parsing SDP Content
use rtc::peer_connection::sdp::RTCSessionDescription;
let description = RTCSessionDescription::offer("v=0\r\no=- 123 456 IN IP4 0.0.0.0\r\ns=-\r\nt=0 0\r\n".to_string())?;
// Access the parsed SDP structure
let parsed = description.unmarshal()?;
println!("Session version: {}", parsed.version);
println!("Session name: {}", parsed.session_name);
println!("Media sections: {}", parsed.media_descriptions.len());§Using Provisional Answers
use rtc::peer_connection::sdp::RTCSessionDescription;
// Send a provisional answer with limited codecs
let sdp_text = "v=0\r\no=- 111 222 IN IP4 0.0.0.0\r\n...".to_string();
let pranswer = RTCSessionDescription::pranswer(sdp_text.clone())?;
// Later, send final answer with all negotiated parameters
let final_answer = RTCSessionDescription::answer(sdp_text)?;§Displaying SDP for Debugging
use rtc::peer_connection::sdp::RTCSessionDescription;
let offer = RTCSessionDescription::offer("v=0\r\no=- 123 456 IN IP4 0.0.0.0\r\ns=-\r\n".to_string())?;
// Display formats SDP with CRLF converted to LF for readability
println!("{}", offer);
// Output: type: offer, sdp:
// v=0
// o=- 123 456 IN IP4 0.0.0.0
// s=-§Specifications
- W3C RTCSessionDescription
- MDN RTCSessionDescription
- RFC 8866 - SDP: Session Description Protocol
- RFC 3264 - Offer/Answer Model with SDP
Fields§
§sdp_type: RTCSdpTypeThe type of this session description (offer, answer, pranswer, or rollback).
sdp: StringThe SDP content as a string.
This is the raw SDP text conforming to RFC 8866 format. It contains session-level and media-level descriptions including codecs, transport addresses, ICE candidates, and DTLS fingerprints.
Implementations§
Source§impl RTCSessionDescription
impl RTCSessionDescription
Sourcepub fn answer(sdp: String) -> Result<RTCSessionDescription, Error>
pub fn answer(sdp: String) -> Result<RTCSessionDescription, Error>
Creates an answer session description from SDP text.
This constructor validates and parses the SDP, wrapping it in an
RTCSessionDescription with type RTCSdpType::Answer. Use this
when creating the final response to an offer.
§Parameters
sdp: The SDP content as a string (RFC 8866 format)
§Returns
Returns Ok(RTCSessionDescription) if the SDP is valid, or Err if
parsing fails.
§Examples
use rtc::peer_connection::sdp::{RTCSessionDescription, RTCSdpType};
// Typically this SDP comes from create_answer()
let sdp = r#"v=0
o=- 987654321 2 IN IP4 192.168.1.100
s=-
t=0 0
m=audio 9 UDP/TLS/RTP/SAVPF 111
"#.to_string();
let answer = RTCSessionDescription::answer(sdp)?;
assert_eq!(answer.sdp_type, RTCSdpType::Answer);
// The SDP is automatically validated and parsed (internally)
// Access parsed structure with unmarshal()
let parsed = answer.unmarshal()?;
println!("Answer has {} media section(s)", parsed.media_descriptions.len());Sourcepub fn offer(sdp: String) -> Result<RTCSessionDescription, Error>
pub fn offer(sdp: String) -> Result<RTCSessionDescription, Error>
Creates an offer session description from SDP text.
This constructor validates and parses the SDP, wrapping it in an
RTCSessionDescription with type RTCSdpType::Offer. Use this
when creating the initial offer to start negotiation.
§Parameters
sdp: The SDP content as a string (RFC 8866 format)
§Returns
Returns Ok(RTCSessionDescription) if the SDP is valid, or Err if
parsing fails.
§Examples
use rtc::peer_connection::sdp::{RTCSessionDescription, RTCSdpType};
// Typically this SDP comes from create_offer()
let sdp = r#"v=0
o=- 123456789 2 IN IP4 192.168.1.1
s=-
t=0 0
m=video 9 UDP/TLS/RTP/SAVPF 96
"#.to_string();
let offer = RTCSessionDescription::offer(sdp)?;
assert_eq!(offer.sdp_type, RTCSdpType::Offer);
// Parsed structure is available immediately
let parsed = offer.unmarshal()?;
println!("Offer has {} media section(s)", parsed.media_descriptions.len());Sourcepub fn pranswer(sdp: String) -> Result<RTCSessionDescription, Error>
pub fn pranswer(sdp: String) -> Result<RTCSessionDescription, Error>
Creates a provisional answer session description from SDP text.
This constructor validates and parses the SDP, wrapping it in an
RTCSessionDescription with type RTCSdpType::Pranswer. Use this
when you want to send a preliminary answer before the final answer,
allowing early media to flow.
§Parameters
sdp: The SDP content as a string (RFC 8866 format)
§Returns
Returns Ok(RTCSessionDescription) if the SDP is valid, or Err if
parsing fails.
§Examples
use rtc::peer_connection::sdp::{RTCSessionDescription, RTCSdpType};
// Send provisional answer with a subset of codecs
let early_sdp = r#"v=0
o=- 555555555 2 IN IP4 192.168.1.50
s=-
t=0 0
m=audio 9 UDP/TLS/RTP/SAVPF 0
"#.to_string();
let pranswer = RTCSessionDescription::pranswer(early_sdp)?;
assert_eq!(pranswer.sdp_type, RTCSdpType::Pranswer);
// Later, send final answer with all negotiated parameters
// let final_answer = RTCSessionDescription::answer(final_sdp)?;§Note
Provisional answers are less commonly used in modern WebRTC. Consider whether sending a final answer immediately is more appropriate for your use case.
Sourcepub fn rollback(sdp: Option<String>) -> Result<RTCSessionDescription, Error>
pub fn rollback(sdp: Option<String>) -> Result<RTCSessionDescription, Error>
Creates a rollback session description.
This constructor creates an RTCSessionDescription with type
RTCSdpType::Rollback. Rollback is used to revert a pending
local or remote description and return the signaling state to Stable.
Per WebRTC specification (RFC 8829 §5.7), rollback descriptions typically have empty SDP content. This is used to abort an in-progress negotiation, such as when implementing Perfect Negotiation collision resolution.
§Parameters
sdp: Optional SDP content. Per spec, this should typically beNoneor an empty string. If provided and non-empty, the SDP will be parsed and validated.
§Returns
Returns Ok(RTCSessionDescription) with type Rollback. If non-empty
SDP is provided and parsing fails, returns Err.
§Use Cases
-
Perfect Negotiation (Polite Peer): When a collision is detected, the polite peer calls
set_local_description(rollback)to abort its pending offer before accepting the remote offer. -
Perfect Negotiation (Offer Rejection): When rejecting a remote offer, call
set_remote_description(rollback)to return to Stable state without completing negotiation. -
Signaling State Transitions: Enables these rollback transitions:
- HaveLocalOffer → Stable (SetLocal with rollback)
- HaveRemoteOffer → Stable (SetRemote with rollback)
§Examples
use rtc::peer_connection::sdp::{RTCSessionDescription, RTCSdpType};
use rtc::peer_connection::RTCPeerConnectionBuilder;
// Create a rollback description (typically with empty SDP)
let rollback = RTCSessionDescription::rollback(None)?;
assert_eq!(rollback.sdp_type, RTCSdpType::Rollback);
assert_eq!(rollback.sdp, "");
// Use case: Polite peer rolling back local offer on collision
// (assumes peer is currently in HaveLocalOffer state)
pc.set_local_description(rollback)?;
// Now back in Stable state, ready to accept remote offer§Specification
Implements rollback as specified in:
- RFC 8829 (JSEP) §5.7: Rollback
- W3C WebRTC 1.0 §4.4.1.6: Set the RTCSessionDescription
Sourcepub fn unmarshal(&self) -> Result<SessionDescription, Error>
pub fn unmarshal(&self) -> Result<SessionDescription, Error>
Parses the SDP text into a structured format.
This method deserializes the SDP string into a parsed
SessionDescription
structure that provides programmatic access to session and media
attributes. The parsed structure is also cached internally for
performance.
§Returns
Returns Ok(SessionDescription) containing the parsed SDP structure,
or Err if the SDP is malformed.
§Examples
use rtc::peer_connection::sdp::RTCSessionDescription;
let offer = RTCSessionDescription::offer(
"v=0\r\no=- 123 456 IN IP4 0.0.0.0\r\ns=WebRTC Session\r\nt=0 0\r\n".to_string()
)?;
// Parse SDP to access structure
let parsed = offer.unmarshal()?;
println!("SDP version: {}", parsed.version);
println!("Session name: {}", parsed.session_name);
println!("Number of media sections: {}", parsed.media_descriptions.len());
// Can be called multiple times (returns same result)
let parsed_again = offer.unmarshal()?;
assert_eq!(parsed.version, parsed_again.version);§Performance
This method parses the SDP each time it’s called. For repeated access, consider caching the result.
Trait Implementations§
Source§impl Clone for RTCSessionDescription
impl Clone for RTCSessionDescription
Source§fn clone(&self) -> RTCSessionDescription
fn clone(&self) -> RTCSessionDescription
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for RTCSessionDescription
impl Debug for RTCSessionDescription
Source§impl Default for RTCSessionDescription
impl Default for RTCSessionDescription
Source§fn default() -> RTCSessionDescription
fn default() -> RTCSessionDescription
Source§impl<'de> Deserialize<'de> for RTCSessionDescription
impl<'de> Deserialize<'de> for RTCSessionDescription
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<RTCSessionDescription, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<RTCSessionDescription, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Display for RTCSessionDescription
impl Display for RTCSessionDescription
Source§impl Serialize for RTCSessionDescription
impl Serialize for RTCSessionDescription
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Auto Trait Implementations§
impl Freeze for RTCSessionDescription
impl RefUnwindSafe for RTCSessionDescription
impl Send for RTCSessionDescription
impl Sync for RTCSessionDescription
impl Unpin for RTCSessionDescription
impl UnsafeUnpin for RTCSessionDescription
impl UnwindSafe for RTCSessionDescription
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.