Skip to main content

RTCMessage

Enum RTCMessage 

Source
pub enum RTCMessage {
    RtpPacket(MediaStreamTrackId, Packet),
    RtcpPacket(MediaStreamTrackId, Vec<Box<dyn Packet>>),
    DataChannelMessage(RTCDataChannelId, RTCDataChannelMessage),
}
Expand description

Messages that can be sent or received through a peer connection.

RTCMessage represents the different types of messages that flow through a WebRTC connection in this sans-I/O library. Applications use these messages to send and receive media data (RTP), control information (RTCP), and application data (data channels).

§Message Types

  • RTP Packets - Media data (audio/video frames)
  • RTCP Packets - Control information (statistics, feedback)
  • Data Channel Messages - Application data (text, binary)

§Usage Pattern

In a sans-I/O architecture, messages are:

  1. Outgoing: Created by the application and passed to the peer connection
  2. Incoming: Retrieved from the peer connection after processing network data

§Examples

§Sending RTP Media

use rtc::peer_connection::message::RTCMessage;
use rtc::media_stream::MediaStreamTrackId;

// Create an RTP message for a specific track
let message = RTCMessage::RtpPacket(track_id, rtp_packet);

// Send through peer connection (sans-I/O)
// peer_connection.send_message(message)?;

§Sending RTCP Feedback

use rtc::peer_connection::message::RTCMessage;
use rtc::media_stream::MediaStreamTrackId;

// Create an RTCP message with control packets
let message = RTCMessage::RtcpPacket(track_id, rtcp_packets);

// Send through peer connection
// peer_connection.send_message(message)?;

§Sending Data Channel Message

use rtc::peer_connection::message::RTCMessage;
use rtc::data_channel::{RTCDataChannelId, RTCDataChannelMessage};
use bytes::BytesMut;

// Create a text message
let text_msg = RTCDataChannelMessage {
    is_string: true,
    data: BytesMut::from("Hello, WebRTC!"),
};
let message = RTCMessage::DataChannelMessage(channel_id, text_msg);

// Or create a binary message
let binary_msg = RTCDataChannelMessage {
    is_string: false,
    data: BytesMut::from(&[0x01, 0x02, 0x03, 0x04][..]),
};
let message2 = RTCMessage::DataChannelMessage(channel_id, binary_msg);

§Processing Incoming Messages

use rtc::peer_connection::message::RTCMessage;

match message {
    RTCMessage::RtpPacket(track_id, rtp_packet) => {
        println!("Received RTP for track {:?}", track_id);
        // Process media packet (decode, render, etc.)
    }
    RTCMessage::RtcpPacket(track_id, rtcp_packets) => {
        println!("Received {} RTCP packet(s) for track {:?}",
                 rtcp_packets.len(), track_id);
        // Process control information
    }
    RTCMessage::DataChannelMessage(channel_id, data) => {
        println!("Received data on channel {}", channel_id);
        // Process application data
    }
}

§Filtering by Message Type

use rtc::peer_connection::message::RTCMessage;

fn is_media_message(message: &RTCMessage) -> bool {
    matches!(message, RTCMessage::RtpPacket(_, _) | RTCMessage::RtcpPacket(_, _))
}

fn is_data_message(message: &RTCMessage) -> bool {
    matches!(message, RTCMessage::DataChannelMessage(_, _))
}

if is_media_message(&message) {
    // Handle media
} else if is_data_message(&message) {
    // Handle data
}

§Extracting Message Content

use rtc::peer_connection::message::RTCMessage;
use rtc::media_stream::MediaStreamTrackId;

match message {
    RTCMessage::RtpPacket(track_id, packet) => {
        println!("RTP sequence: {}, timestamp: {}",
                 packet.header.sequence_number,
                 packet.header.timestamp);
    }
    RTCMessage::RtcpPacket(track_id, packets) => {
        for packet in packets {
            println!("RTCP packet type: {:?}", packet.header());
        }
    }
    RTCMessage::DataChannelMessage(channel_id, msg) => {
        if msg.is_string {
            let text = String::from_utf8_lossy(&msg.data);
            println!("Text: {}", text);
        } else {
            println!("Binary: {} bytes", msg.data.len());
        }
    }
}

§Message Flow

Application Layer
      ↓ (send)          ↑ (receive)
RTCMessage variants
      ↓                 ↑
Peer Connection (sans-I/O processing)
      ↓                 ↑
Network Layer

§Specifications

  • RFC 3550 - RTP: Real-time Transport Protocol
  • RFC 3551 - RTP Profile for Audio and Video
  • RFC 3711 - SRTP: Secure Real-time Transport Protocol
  • RFC 8831 - WebRTC Data Channels

Variants§

§

RtpPacket(MediaStreamTrackId, Packet)

RTP packet for a specific media track.

Contains media data (encoded audio or video). Each RTP packet includes:

  • Track ID: Identifies which media track this packet belongs to
  • RTP Packet: Contains sequence number, timestamp, payload type, and encoded media data

§Fields

  • MediaStreamTrackId - The track this packet belongs to
  • rtp::Packet - The RTP packet with header and payload

§Use Cases

  • Sending: Encode media frames and send as RTP packets
  • Receiving: Decode RTP packets and render media
  • Processing: Analyze sequence numbers for packet loss detection

§Example

use rtc::peer_connection::message::RTCMessage;
use rtc::media_stream::MediaStreamTrackId;

let message = RTCMessage::RtpPacket(track_id, rtp_packet);

if let RTCMessage::RtpPacket(tid, packet) = message {
    println!("Track: {:?}, Sequence: {}", tid, packet.header.sequence_number);
}
§

RtcpPacket(MediaStreamTrackId, Vec<Box<dyn Packet>>)

RTCP packet(s) for a specific media track.

Contains control information about media transmission. RTCP packets include:

  • Sender Reports (SR): Statistics from media senders
  • Receiver Reports (RR): Statistics from media receivers
  • Source Description (SDES): Additional source information
  • Bye: Indicates participant is leaving
  • Application-defined: Custom RTCP packets

§Fields

  • MediaStreamTrackId - The track these packets relate to
  • Vec<Box<dyn rtcp::Packet>> - One or more RTCP packets

§Use Cases

  • Monitoring: Track quality metrics (packet loss, jitter)
  • Feedback: Send NACK/PLI for lost/corrupted packets
  • Statistics: Report transmission statistics

§Note

Multiple RTCP packets can be combined into a single compound RTCP message for efficiency.

§Example

use rtc::peer_connection::message::RTCMessage;
use rtc::media_stream::MediaStreamTrackId;

let message = RTCMessage::RtcpPacket(track_id, rtcp_packets);

if let RTCMessage::RtcpPacket(tid, packets) = message {
    println!("Track: {:?}, RTCP packets: {}", tid, packets.len());
}
§

DataChannelMessage(RTCDataChannelId, RTCDataChannelMessage)

Data channel message.

Contains application-defined data sent over a data channel. Messages can be:

  • Text: UTF-8 encoded strings
  • Binary: Arbitrary byte arrays

§Fields

  • RTCDataChannelId - The data channel this message belongs to
  • RTCDataChannelMessage - The message content (text or binary)

§Use Cases

  • Signaling: Send application-level control messages
  • File Transfer: Send file data in chunks
  • Game State: Send game updates in real-time
  • Chat: Send text messages between peers

§Message Ordering and Reliability

Depends on data channel configuration:

  • Reliable & Ordered: Messages arrive in order, guaranteed
  • Unreliable & Unordered: Like UDP, may arrive out of order or not at all

§Example

use rtc::peer_connection::message::RTCMessage;
use rtc::data_channel::{RTCDataChannelId, RTCDataChannelMessage};
use bytes::BytesMut;

// Text message
let text_msg = RTCDataChannelMessage {
    is_string: true,
    data: BytesMut::from("Hello!"),
};
let message = RTCMessage::DataChannelMessage(channel_id, text_msg);

// Binary message
let binary_msg = RTCDataChannelMessage {
    is_string: false,
    data: BytesMut::from(&[1, 2, 3, 4][..]),
};
let message2 = RTCMessage::DataChannelMessage(channel_id, binary_msg);

Trait Implementations§

Source§

impl Clone for RTCMessage

Source§

fn clone(&self) -> RTCMessage

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 RTCMessage

Source§

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

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

impl<I> Protocol<TransportMessage<BytesMut>, RTCMessage, RTCEvent> for RTCPeerConnection<I>
where I: Interceptor,

Source§

type Rout = RTCMessage

Output read message type Read more
Source§

type Wout = TransportMessage<BytesMut>

Output write message type Read more
Source§

type Eout = RTCPeerConnectionEvent

Output event type Read more
Source§

type Error = Error

Error type for protocol operations
Source§

type Time = Instant

Time/Instant type for timeout handling Read more
Source§

fn handle_read(&mut self, msg: TaggedBytesMut) -> Result<(), Self::Error>

Handle an incoming read message. Read more
Source§

fn poll_read(&mut self) -> Option<Self::Rout>

Poll for a processed read message. Read more
Source§

fn handle_write(&mut self, msg: RTCMessage) -> Result<(), Self::Error>

Handle an outgoing write message. Read more
Source§

fn poll_write(&mut self) -> Option<Self::Wout>

Poll for a processed write message. Read more
Source§

fn handle_event(&mut self, evt: RTCEvent) -> Result<(), Self::Error>

Handle a custom event. Read more
Source§

fn poll_event(&mut self) -> Option<Self::Eout>

Poll for a generated event. Read more
Source§

fn handle_timeout(&mut self, now: Instant) -> Result<(), Self::Error>

Handle a timeout event. Read more
Source§

fn poll_timeout(&mut self) -> Option<Instant>

Poll for the next timeout deadline. Read more
Source§

fn close(&mut self) -> Result<(), Self::Error>

Close the protocol. 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> 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<'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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> 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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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, 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V