Skip to main content

RTCSessionDescription

Struct RTCSessionDescription 

Source
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:

  1. Created using offer(), answer(), or pranswer() constructor methods
  2. Serialized to JSON for transmission over the signaling channel
  3. Deserialized on the remote peer
  4. 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

Fields§

§sdp_type: RTCSdpType

The type of this session description (offer, answer, pranswer, or rollback).

§sdp: String

The 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

Source

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());
Source

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());
Source

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.

Source

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 be None or 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
Source

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

Source§

fn clone(&self) -> RTCSessionDescription

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RTCSessionDescription

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for RTCSessionDescription

Source§

fn default() -> RTCSessionDescription

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for RTCSessionDescription

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<RTCSessionDescription, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for RTCSessionDescription

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Serialize for RTCSessionDescription

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.