Skip to content

Commit

Permalink
Support for priority (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
npmenard authored Oct 31, 2023
1 parent bfc0bc4 commit c67fff1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
3 changes: 1 addition & 2 deletions examples/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ CONFIG_ESP32S3_SPIRAM_SUPPORT=n
CONFIG_SPIRAM_SPEED_80M=y
CONFIG_SPIRAM_IGNORE_NOTFOUND=y
CONFIG_SPIRAM_USE_MALLOC=y
CONFIG_HEAP_ABORT_WHEN_ALLOCATION_FAILS=y
CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=4096
CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=4090
#CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP=y

#CONFIG_MBEDTLS_DEBUG=y
Expand Down
33 changes: 21 additions & 12 deletions src/common/conn/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,9 @@ where
.unwrap();
let _ = self.app_client.insert(app_client);
let cloned_robot = robot.clone();

let mut current_prio = None;
loop {
let _ = smol::Timer::after(std::time::Duration::from_millis(100)).await;
let _ = smol::Timer::after(std::time::Duration::from_millis(300)).await;

let sig = if let Some(webrtc_config) = self.webrtc_config.as_ref() {
let ip = self.app_config.get_ip();
Expand Down Expand Up @@ -345,22 +345,31 @@ where
},
async {
let mut api = sig.await.map_err(|e| ServerError::Other(Box::new(e)))?;
if let Some(task) = self.webtrc_conn.as_ref() {
if !task.is_finished() {
log::info!(
"a webrtc connection is active ignoring further signaling requests"
);
return Err(ServerError::ServerConnectionNotConfigured);
}
}

let prio = self
.webtrc_conn
.as_ref()
.and_then(|f| (!f.is_finished()).then_some(&current_prio))
.unwrap_or(&None);

let sdp = api
.answer()
.answer(prio)
.await
.map_err(|e| ServerError::Other(Box::new(e)))?;

// When the current priority is lower than the priority of the incoming connection then
// we cancel and close the current webrtc connection (if any)
if let Some(task) = self.webtrc_conn.take() {
if !task.is_finished() {
let _ = task.cancel().await;
}
}

let _ = current_prio.insert(sdp.1);

Ok(IncomingConnection::WebRtcConnection(WebRTCConnection {
webrtc_api: api,
sdp,
sdp: sdp.0,
server: None,
robot: cloned_robot.clone(),
}))
Expand Down
28 changes: 23 additions & 5 deletions src/common/webrtc/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ pub enum WebRtcError {
Other(#[from] anyhow::Error),
#[error(transparent)]
DtlsError(#[from] Box<dyn std::error::Error + Send + Sync>),
#[error("the active webrtc connection has a higher priority")]
CurrentConnectionHigherPrority(),
}

pub(crate) struct WebRtcSignalingChannel {
Expand Down Expand Up @@ -410,7 +412,10 @@ where
Err(WebRtcError::DataChannelOpenError())
}

pub async fn answer(&mut self) -> Result<Box<WebRtcSdp>, WebRtcError> {
pub async fn answer(
&mut self,
current_prio: &Option<u32>,
) -> Result<(Box<WebRtcSdp>, u32), WebRtcError> {
let offer = self
.signaling
.as_mut()
Expand All @@ -426,6 +431,19 @@ where
.get(0)
.ok_or_else(|| WebRtcError::InvalidSDPOffer("no media description".to_owned()))?;

let caller_prio = attribute
.attribute("x-priority")
.flatten()
.map_or(Ok(u32::MAX), |a| a.parse::<u32>())
.unwrap_or(u32::MAX);

let current_prio = current_prio.unwrap_or(0);

// TODO use is_some_then when rust min version reach 1.70
if current_prio >= caller_prio {
return Err(WebRtcError::CurrentConnectionHigherPrority());
}

let remote_creds = ICECredentials::new(
attribute
.attribute("ice-ufrag")
Expand Down Expand Up @@ -486,9 +504,9 @@ where

let answer = answer.with_media(media);

Ok(Box::new(WebRtcSdp::new(
answer,
self.uuid.as_ref().unwrap().clone(),
)))
Ok((
Box::new(WebRtcSdp::new(answer, self.uuid.as_ref().unwrap().clone())),
caller_prio,
))
}
}

0 comments on commit c67fff1

Please sign in to comment.