Data channels
Data channels run over SCTP (RFC 9260) over DTLS, announced in-band with DCEP (RFC 8832) or negotiated out-of-band. The channel's handle borrows the connection, so keep its id.
Create, open, send
use rtci::data_channel::{RTCDataChannelId, RTCDataChannelInit};
use rtci::peer_connection::RTCPeerConnection;
use rtci::peer_connection::event::{RTCDataChannelEvent, RTCPeerConnectionEvent};
use rtci::sansio::Protocol;
use std::time::Instant;
fn open(pc: &mut RTCPeerConnection) -> Result<RTCDataChannelId, Box<dyn std::error::Error>> {
let init = RTCDataChannelInit {
ordered: true,
max_retransmits: None,
..Default::default()
};
// The handle borrows `pc`, so keep the id to reach the channel again later.
let id = pc.create_data_channel("my-channel", Some(init))?.id();
Ok(id)
}
fn on_open(pc: &mut RTCPeerConnection, id: RTCDataChannelId) -> Result<(), Box<dyn std::error::Error>> {
// Only send once the channel is open: an in-band channel's SCTP stream is
// established by the DCEP handshake, and sending before then is rejected.
if let Some(mut dc) = pc.data_channel(id) {
dc.send_text(Instant::now(), "Hello, WebRTC!")?;
}
Ok(())
}
fn drive(pc: &mut RTCPeerConnection) -> Result<(), Box<dyn std::error::Error>> {
while let Some(event) = pc.poll_event() {
if let RTCPeerConnectionEvent::OnDataChannel(RTCDataChannelEvent::OnOpen(id)) = event {
on_open(pc, id)?;
}
}
Ok(())
}
From README.md, “Data Channels” at v0.5.0.
create_data_channel(label, init) returns a handle that borrows the connection; keep its id() and reach the channel again with data_channel(id). Send only once the channel is open: an in-band channel's SCTP stream is established by the DCEP handshake, and sending before then is rejected. send(now, bytes) sends binary, send_text(now, text) a string; inbound messages arrive from poll_read() as RTCMessage::DataChannelMessage(channel_id, message).
Channel options
RTCDataChannelInit carries the W3C RTCDataChannelInit members:
| Field | W3C member | Meaning |
|---|---|---|
ordered | ordered | false lets data be delivered out of order; the default true keeps order |
max_packet_life_time | maxPacketLifeTime | milliseconds during which the channel transmits or retransmits unacknowledged data; not together with max_retransmits |
max_retransmits | maxRetransmits | how many times the channel retransmits; not together with max_packet_life_time |
protocol | protocol | the subprotocol name; empty by default |
negotiated | negotiated + id | Some(id): negotiated out-of-band on that stream id, not announced in-band; None (default): announced with DCEP |
priority | priority (WebRTC Priority Control) | the DCEP priority: 128 very-low, 256 (default) low, 512 medium, 1024 high |
From the field documentation of RTCDataChannelInit (src/data_channel/init.rs) at v0.5.0.
Events
poll_event() yields RTCPeerConnectionEvent::OnDataChannel(event), where the RTCDataChannelEvent is one of OnDataChannel(id) (the peer opened one), OnOpen(id), OnError(id), OnClosing(id), OnClose(id), OnBufferedAmountLow(id) and OnBufferedAmountHigh(id).
Message size: 16 KiB toward browsers by default
RFC 8831 section 6.6: without user message interleaving (RFC 8260, I-DATA) "the sender SHOULD limit the maximum message size to 16 KB". No current browser announces I-DATA, so toward a browser RTCSctpTransport::max_message_size (W3C maxMessageSize) is 16384 and send refuses a larger message with Error::ErrMessageTooLargeWithoutInterleaving, whose text names the limit and the setting. Between two instances of this engine the limit is the peer's a=max-message-size. To send larger messages to browsers, as browsers themselves do:
SettingEngineBuilder::new().with_sctp_message_size_limit_without_interleaving(false)
From FORK.md, "Read this first" at v0.5.0.
The Chrome behaviour profile's value for this setting is in the profiles table.
Back-pressure
Receiving: poll_read is the throttle. Undrained messages leave bytes in SCTP's reassembly queue, which lowers the receive window advertised to the peer; declining to call poll_read is how you apply back-pressure.
Sending: send queues; it does not mean the data is on the wire. Watch buffered_amount(), set set_buffered_amount_low_threshold, and resume on OnBufferedAmountLow. The repository's data-channels-flow-control example applies back-pressure to a fast sender.
Out of the box
Interop cells at the release run ordered, unordered, maxRetransmits 0, maxPacketLifeTime 3000 ms, 64 KiB and 256 KiB messages and a negotiated channel (id 5) against Pion, str0m and aiortc, in both directions (Interoperability).
Reference
rtci::data_channeland docs.rs.- The protocol crates: rtci-datachannel (DCEP) and rtci-sctp.