From 8ebfcf2785e997615877fc9c3e87377922e49373 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 11 Feb 2021 19:21:24 +0100 Subject: [PATCH] Can mark a user as ready to stream --- src/api_data/call_peer_ready.rs | 23 +++++++++++++++++++++++ src/api_data/mod.rs | 3 ++- src/controllers/calls_controller.rs | 28 ++++++++++++++++++++++++++++ src/controllers/user_ws_routes.rs | 1 + 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/api_data/call_peer_ready.rs diff --git a/src/api_data/call_peer_ready.rs b/src/api_data/call_peer_ready.rs new file mode 100644 index 0000000..e40dbe1 --- /dev/null +++ b/src/api_data/call_peer_ready.rs @@ -0,0 +1,23 @@ +//! # Call peer ready API +//! +//! @author Pierre Hubert +use serde::Serialize; + +use crate::data::conversation::ConvID; +use crate::data::user::UserID; + +#[derive(Serialize)] +#[allow(non_snake_case)] +pub struct CallPeerReadyAPI { + callID: u64, + peerID: u64, +} + +impl CallPeerReadyAPI { + pub fn new(call_id: &ConvID, user_id: &UserID) -> Self { + Self { + callID: call_id.clone(), + peerID: user_id.id(), + } + } +} \ No newline at end of file diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 96caf8d..9f371d6 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -61,4 +61,5 @@ pub mod user_calls_config; pub mod joined_call_message; pub mod call_member_info; pub mod left_call_message; -pub mod new_call_signal; \ No newline at end of file +pub mod new_call_signal; +pub mod call_peer_ready; \ No newline at end of file diff --git a/src/controllers/calls_controller.rs b/src/controllers/calls_controller.rs index 7f848b3..2fe19bc 100644 --- a/src/controllers/calls_controller.rs +++ b/src/controllers/calls_controller.rs @@ -7,6 +7,7 @@ use std::collections::HashMap; use webrtc_sdp::attribute_type::SdpAttribute; use crate::api_data::call_member_info::CallMemberInfo; +use crate::api_data::call_peer_ready::CallPeerReadyAPI; use crate::api_data::joined_call_message::JoinedCallMessage; use crate::api_data::left_call_message::LeftCallMessage; use crate::api_data::new_call_signal::NewCallSignalAPI; @@ -61,6 +62,17 @@ impl UserWsRequestHandler { Ok(peer_id) } + + /// Update call information + fn update_call(&mut self, update: F) -> Res where F: FnOnce(&mut ActiveCall) { + self.update_conn(|conn| { + if let Some(call) = &mut conn.active_call { + update(call); + } + })?; + + Ok(()) + } } /// Get legacy call configuration @@ -262,6 +274,22 @@ pub fn on_client_signal(r: &mut UserWsRequestHandler) -> RequestResult { r.success("Signal sent") } +/// Mark user ready for streaming +pub fn mark_user_ready(r: &mut UserWsRequestHandler) -> Res { + let call_id = r.post_call_id("callID")?; + let user_id = r.user_id()?; + + r.update_call(|call| call.ready = true)?; + + user_ws_controller::send_message_to_specific_connections( + |c| c.user_id != &user_id && c.is_having_call_with_conversation(&call_id), + |_| UserWsMessage::no_id_message("call_peer_ready", CallPeerReadyAPI::new(&call_id, r.user_id_ref()?)), + None:: _>, + )?; + + r.success("Information propagated.") +} + /// Make the user leave the call pub fn make_user_leave_call(conv_id: &ConvID, connection: &UserWsConnection) -> Res { diff --git a/src/controllers/user_ws_routes.rs b/src/controllers/user_ws_routes.rs index 6df648c..ef2064b 100644 --- a/src/controllers/user_ws_routes.rs +++ b/src/controllers/user_ws_routes.rs @@ -43,6 +43,7 @@ pub fn get_user_ws_routes() -> Vec { UserWsRoute::new("calls/leave", calls_controller::leave_call), UserWsRoute::new("calls/members", calls_controller::get_members_list), UserWsRoute::new("calls/signal", calls_controller::on_client_signal), + UserWsRoute::new("calls/mark_ready", calls_controller::mark_user_ready), ] }