Skip to content

Commit

Permalink
Fix datachannel id setting for 0.5.0 release (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuqdog committed Sep 2, 2022
1 parent 86255e8 commit b43e05b
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 27 deletions.
2 changes: 1 addition & 1 deletion examples/examples/ortc/ortc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ async fn main() -> Result<()> {

let dc_params = DataChannelParameters {
label: "Foo".to_owned(),
id,
negotiated: Some(id),
..Default::default()
};

Expand Down
9 changes: 3 additions & 6 deletions webrtc/src/data_channel/data_channel_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ pub struct RTCDataChannelInit {
pub protocol: Option<String>,

/// negotiated describes if the data channel is created by the local peer or
/// the remote peer. The default value of false tells the user agent to
/// the remote peer. The default value of None tells the user agent to
/// announce the channel in-band and instruct the other peer to dispatch a
/// corresponding DataChannel. If set to true, it is up to the application
/// corresponding DataChannel. If set to Some(id), it is up to the application
/// to negotiate the channel and create an DataChannel with the same id
/// at the other peer.
pub negotiated: Option<bool>,

/// id overrides the default selection of ID for this channel.
pub id: Option<u16>,
pub negotiated: Option<u16>,
}
3 changes: 1 addition & 2 deletions webrtc/src/data_channel/data_channel_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use serde::{Deserialize, Serialize};
pub struct DataChannelParameters {
pub label: String,
pub protocol: String,
pub id: u16,
pub ordered: bool,
pub max_packet_life_time: u16,
pub max_retransmits: u16,
pub negotiated: bool,
pub negotiated: Option<u16>,
}
7 changes: 2 additions & 5 deletions webrtc/src/data_channel/data_channel_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,9 @@ async fn test_data_channel_parameters_negotiated_exchange() -> Result<()> {

const EXPECTED_MESSAGE: &str = "Hello World";

let negotiated = true;
let id = 500u16;
let options = RTCDataChannelInit {
negotiated: Some(negotiated),
id: Some(id),
negotiated: Some(id),
..Default::default()
};

Expand Down Expand Up @@ -1523,10 +1521,9 @@ async fn test_data_channel_ortc_e2e() -> Result<()> {

signal_ortc_pair(Arc::clone(&stack_a), Arc::clone(&stack_b)).await?;

let id = 1u16;
let dc_params = DataChannelParameters {
label: "Foo".to_owned(),
id,
negotiated: None,
..Default::default()
};

Expand Down
9 changes: 6 additions & 3 deletions webrtc/src/data_channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ pub struct RTCDataChannel {
impl RTCDataChannel {
// create the DataChannel object before the networking is set up.
pub(crate) fn new(params: DataChannelParameters, setting_engine: Arc<SettingEngine>) -> Self {
// the id value if non-negotiated doesn't matter, since it will be overwritten
// on opening
let id = params.negotiated.unwrap_or(0);
RTCDataChannel {
stats_id: format!(
"DataChannel-{}",
Expand All @@ -95,8 +98,8 @@ impl RTCDataChannel {
),
label: params.label,
protocol: params.protocol,
negotiated: params.negotiated,
id: AtomicU16::new(params.id),
negotiated: params.negotiated.is_some(),
id: AtomicU16::new(id),
ordered: params.ordered,
max_packet_lifetime: params.max_packet_life_time,
max_retransmits: params.max_retransmits,
Expand Down Expand Up @@ -157,7 +160,7 @@ impl RTCDataChannel {
negotiated: self.negotiated,
};

if self.id.load(Ordering::SeqCst) == 0 {
if !self.negotiated {
self.id.store(
sctp_transport
.generate_and_set_data_channel_id(
Expand Down
8 changes: 1 addition & 7 deletions webrtc/src/peer_connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1857,10 +1857,6 @@ impl RTCPeerConnection {

// https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #19)
if let Some(options) = options {
if let Some(id) = options.id {
params.id = id;
}

// Ordered indicates if data is allowed to be delivered out of order. The
// default value of true, guarantees that data will be delivered in order.
// https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #9)
Expand Down Expand Up @@ -1889,9 +1885,7 @@ impl RTCPeerConnection {
}

// https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #12)
if let Some(negotiated) = options.negotiated {
params.negotiated = negotiated;
}
params.negotiated = options.negotiated;
}

let d = Arc::new(RTCDataChannel::new(
Expand Down
9 changes: 6 additions & 3 deletions webrtc/src/sctp_transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,16 @@ impl RTCSctpTransport {
}
};

let id = dc.stream_identifier();
let negotiated = if dc.config.negotiated {
Some(dc.stream_identifier())
} else {
None
};
let rtc_dc = Arc::new(RTCDataChannel::new(
DataChannelParameters {
id,
label: dc.config.label.clone(),
protocol: dc.config.protocol.clone(),
negotiated: dc.config.negotiated,
negotiated,
ordered,
max_packet_life_time: max_packet_lifetime,
max_retransmits,
Expand Down

0 comments on commit b43e05b

Please sign in to comment.