Skip to main content

rtc/data_channel/
init.rs

1use sctp::StreamId;
2
3/// Dictionary for configuring properties of an RTCDataChannel.
4///
5/// The `RTCDataChannelInit` dictionary is used to configure the properties of a data channel
6/// when creating it via [`RTCPeerConnection::create_data_channel`].
7///
8/// ## Specifications
9///
10/// * [W3C RTCDataChannelInit Dictionary]
11///
12/// [W3C RTCDataChannelInit Dictionary]: https://www.w3.org/TR/webrtc/#dom-rtcdatachannelinit
13/// [`RTCPeerConnection::create_data_channel`]: crate::peer_connection::RTCPeerConnection::create_data_channel
14///
15/// ## Examples
16///
17/// ```
18/// use rtc::data_channel::RTCDataChannelInit;
19///
20/// let init = RTCDataChannelInit {
21///     ordered: false,
22///     max_retransmits: Some(3),
23///     ..Default::default()
24/// };
25/// ```
26#[derive(Clone)]
27pub struct RTCDataChannelInit {
28    /// If set to `false`, data is allowed to be delivered out of order.
29    ///
30    /// The default value of `true` guarantees that data will be delivered in order.
31    /// Setting this to `false` may improve latency at the cost of ordering guarantees.
32    ///
33    /// Corresponds to the `ordered` attribute in the [W3C specification].
34    ///
35    /// [W3C specification]: https://www.w3.org/TR/webrtc/#dom-rtcdatachannelinit-ordered
36    pub ordered: bool,
37
38    /// Limits the time (in milliseconds) during which the channel will transmit or retransmit
39    /// data if not acknowledged.
40    ///
41    /// If set, this value may be clamped to the maximum value supported by the user agent.
42    /// This option cannot be used together with `max_retransmits`.
43    ///
44    /// Corresponds to the `maxPacketLifeTime` attribute in the [W3C specification].
45    ///
46    /// [W3C specification]: https://www.w3.org/TR/webrtc/#dom-rtcdatachannelinit-maxpacketlifetime
47    pub max_packet_life_time: Option<u16>,
48
49    /// Limits the number of times the channel will retransmit data if not successfully delivered.
50    ///
51    /// If set, this value may be clamped to the maximum value supported by the user agent.
52    /// This option cannot be used together with `max_packet_life_time`.
53    ///
54    /// Corresponds to the `maxRetransmits` attribute in the [W3C specification].
55    ///
56    /// [W3C specification]: https://www.w3.org/TR/webrtc/#dom-rtcdatachannelinit-maxretransmits
57    pub max_retransmits: Option<u16>,
58
59    /// The name of the subprotocol used for this channel.
60    ///
61    /// An empty string indicates no subprotocol is being used. The default is an empty string.
62    ///
63    /// Corresponds to the `protocol` attribute in the [W3C specification].
64    ///
65    /// [W3C specification]: https://www.w3.org/TR/webrtc/#dom-rtcdatachannelinit-protocol
66    pub protocol: String,
67
68    /// If `Some(id)`, the data channel is negotiated out-of-band with the given stream identifier.
69    ///
70    /// When set to `Some(id)`, the application is responsible for negotiating the channel and
71    /// creating a matching data channel with the same `id` on the remote peer. The data channel
72    /// will not be announced in-band.
73    ///
74    /// When set to `None` (the default), the user agent will announce the channel in-band and
75    /// automatically create a corresponding data channel on the remote peer.
76    ///
77    /// Corresponds to the `negotiated` and `id` attributes in the [W3C specification].
78    ///
79    /// [W3C specification]: https://www.w3.org/TR/webrtc/#dom-rtcdatachannelinit-negotiated
80    pub negotiated: Option<StreamId>,
81}
82
83impl Default for RTCDataChannelInit {
84    /// The defaults defined by [W3C `RTCDataChannelInit`].
85    ///
86    /// Note `ordered`: it defaults to `true`, so `..Default::default()` yields an ordered,
87    /// reliable channel — what an application asking for "a plain data channel" expects.
88    /// A derived `Default` would make it `false`, and an unordered channel does more than
89    /// reorder application messages: unordered chunks bypass SCTP's ordered-delivery queue,
90    /// so a first message can overtake the `DATA_CHANNEL_OPEN` sent on the same stream and
91    /// be dropped by the peer as data on a stream it has not accepted yet.
92    ///
93    /// [W3C `RTCDataChannelInit`]: https://www.w3.org/TR/webrtc/#dom-rtcdatachannelinit
94    fn default() -> Self {
95        Self {
96            ordered: true,
97            max_packet_life_time: None,
98            max_retransmits: None,
99            protocol: String::new(),
100            negotiated: None,
101        }
102    }
103}