use rtci::peer_connection::RTCPeerConnection;
use rtci::peer_connection::sdp::{RTCSdpType, RTCSessionDescription};
use rtci::peer_connection::state::RTCSignalingState;
use rtci::peer_connection::transport::RTCIceCandidateInit;
use rtci::shared::error::Result;
use std::time::Instant;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PerfectNegotiation {
polite: bool,
ignore_offer: bool,
collisions: u64,
}
impl PerfectNegotiation {
pub fn new(polite: bool) -> Self {
Self {
polite,
ignore_offer: false,
collisions: 0,
}
}
pub fn polite(&self) -> bool {
self.polite
}
pub fn collisions(&self) -> u64 {
self.collisions
}
pub fn on_negotiation_needed(
&mut self,
pc: &mut RTCPeerConnection,
now: Instant,
) -> Result<Option<RTCSessionDescription>> {
if pc.signaling_state() != RTCSignalingState::Stable {
return Ok(None);
}
let offer = pc.create_offer(None)?;
pc.set_local_description(now, offer.clone())?;
Ok(Some(pc.local_description().unwrap_or(offer)))
}
pub fn on_remote_description(
&mut self,
pc: &mut RTCPeerConnection,
now: Instant,
description: RTCSessionDescription,
) -> Result<Option<RTCSessionDescription>> {
let offer = description.sdp_type == RTCSdpType::Offer;
let collision = offer && pc.signaling_state() != RTCSignalingState::Stable;
if collision {
self.collisions += 1;
}
self.ignore_offer = !self.polite && collision;
if self.ignore_offer {
return Ok(None);
}
if collision {
pc.set_local_description(now, RTCSessionDescription::rollback(None)?)?;
}
pc.set_remote_description(now, description)?;
if !offer {
return Ok(None);
}
let answer = pc.create_answer(None)?;
pc.set_local_description(now, answer.clone())?;
Ok(Some(pc.local_description().unwrap_or(answer)))
}
pub fn on_remote_candidate(
&mut self,
pc: &mut RTCPeerConnection,
candidate: RTCIceCandidateInit,
) -> Result<()> {
match pc.add_remote_candidate(candidate) {
Err(e) if !self.ignore_offer => Err(e),
_ => Ok(()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use rtci::peer_connection::RTCPeerConnectionBuilder;
fn pc(now: Instant) -> RTCPeerConnection {
RTCPeerConnectionBuilder::new()
.build(now)
.expect("a connection")
}
#[test]
fn crossing_offers_settle_with_the_polite_side_rolling_back() {
let now = Instant::now();
let (mut polite_pc, mut impolite_pc) = (pc(now), pc(now));
let (mut polite, mut impolite) = (
PerfectNegotiation::new(true),
PerfectNegotiation::new(false),
);
polite_pc.create_data_channel("p", None).unwrap();
impolite_pc.create_data_channel("i", None).unwrap();
let from_polite = polite
.on_negotiation_needed(&mut polite_pc, now)
.unwrap()
.expect("an offer");
let from_impolite = impolite
.on_negotiation_needed(&mut impolite_pc, now)
.unwrap()
.expect("an offer");
assert!(
impolite
.on_remote_description(&mut impolite_pc, now, from_polite)
.unwrap()
.is_none(),
"the impolite side ignores the colliding offer"
);
let answer = polite
.on_remote_description(&mut polite_pc, now, from_impolite)
.unwrap()
.expect("the polite side answers");
assert_eq!(polite_pc.signaling_state(), RTCSignalingState::Stable);
assert!(
impolite
.on_remote_description(&mut impolite_pc, now, answer)
.unwrap()
.is_none()
);
assert_eq!(impolite_pc.signaling_state(), RTCSignalingState::Stable);
assert_eq!((polite.collisions(), impolite.collisions()), (1, 1));
let again = polite
.on_negotiation_needed(&mut polite_pc, now)
.unwrap()
.expect("the polite side offers again");
let answer = impolite
.on_remote_description(&mut impolite_pc, now, again)
.unwrap()
.expect("taken: no collision");
polite
.on_remote_description(&mut polite_pc, now, answer)
.unwrap();
assert_eq!(polite_pc.signaling_state(), RTCSignalingState::Stable);
assert_eq!(impolite_pc.signaling_state(), RTCSignalingState::Stable);
}
#[test]
fn negotiation_needed_waits_for_stable() {
let now = Instant::now();
let mut a = pc(now);
let mut side = PerfectNegotiation::new(false);
a.create_data_channel("a", None).unwrap();
assert!(side.on_negotiation_needed(&mut a, now).unwrap().is_some());
assert!(side.on_negotiation_needed(&mut a, now).unwrap().is_none());
}
}