diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 00cf520..96caf8d 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -60,4 +60,5 @@ pub mod res_get_ws_token; pub mod user_calls_config; pub mod joined_call_message; pub mod call_member_info; -pub mod left_call_message; \ No newline at end of file +pub mod left_call_message; +pub mod new_call_signal; \ No newline at end of file diff --git a/src/api_data/new_call_signal.rs b/src/api_data/new_call_signal.rs new file mode 100644 index 0000000..fb8c779 --- /dev/null +++ b/src/api_data/new_call_signal.rs @@ -0,0 +1,27 @@ +//! # New call signal +//! +//! @author Pierre Hubert + +use serde::Serialize; + +use crate::data::conversation::ConvID; +use crate::data::error::Res; +use crate::data::user::UserID; + +#[derive(Serialize)] +#[allow(non_snake_case)] +pub struct NewCallSignalAPI { + callID: u64, + peerID: u64, + data: serde_json::Value, +} + +impl NewCallSignalAPI { + pub fn new(call_id: &ConvID, peer_id: &UserID, data: &str) -> Res { + Ok(Self { + callID: call_id.clone(), + peerID: peer_id.id(), + data: serde_json::from_str(data)?, + }) + } +} \ No newline at end of file diff --git a/src/controllers/calls_controller.rs b/src/controllers/calls_controller.rs index 1c751f3..7f848b3 100644 --- a/src/controllers/calls_controller.rs +++ b/src/controllers/calls_controller.rs @@ -9,6 +9,7 @@ use webrtc_sdp::attribute_type::SdpAttribute; use crate::api_data::call_member_info::CallMemberInfo; use crate::api_data::joined_call_message::JoinedCallMessage; use crate::api_data::left_call_message::LeftCallMessage; +use crate::api_data::new_call_signal::NewCallSignalAPI; use crate::api_data::user_calls_config::UserCallsConfig; use crate::controllers::routes::RequestResult; use crate::controllers::user_ws_controller; @@ -306,6 +307,30 @@ pub fn handle_event(e: &events_helper::Event) -> Res { } } + Event::NewRTCRelayMessage(msg) => { + // Get call hash + let split: Vec<&str> = msg.call_hash.split("-").collect(); + if split.len() != 2 { + return Ok(()); + } + + let call_id = split[0].parse::()?; + let peer_id = UserID::new(split[1].parse::()?); + + let target_user = UserID::new(msg.peer_id.parse::()?); + let target_user = match target_user.is_valid() { + true => target_user, + false => peer_id.clone() + }; + + + user_ws_controller::send_message_to_specific_connections( + |c| c.user_id == target_user && c.is_having_call_with_conversation(&call_id), + |_| UserWsMessage::no_id_message("new_call_signal", NewCallSignalAPI::new(&call_id, &peer_id, &msg.data)?), + None:: _>, + )?; + } + _ => {} } diff --git a/src/controllers/rtc_relay_controller.rs b/src/controllers/rtc_relay_controller.rs index 7774dc2..5d26b69 100644 --- a/src/controllers/rtc_relay_controller.rs +++ b/src/controllers/rtc_relay_controller.rs @@ -5,9 +5,10 @@ use actix::{ActorContext, Addr, AsyncContext, Handler, StreamHandler}; use actix::prelude::*; use actix_web_actors::ws::{Message, ProtocolError}; -use serde::Serialize; +use serde::{Deserialize, Serialize}; +use serde_json::Value; -use crate::data::call_signal::CallSignal; +use crate::data::call_signal::{CallSignal, NewRtcRelayMessage}; use crate::data::config::conf; use crate::data::error::{ExecError, Res}; use crate::helpers::events_helper; @@ -15,6 +16,15 @@ use crate::helpers::events_helper::Event; struct RtcRelayActor {} +#[allow(non_snake_case)] +#[derive(Deserialize)] +struct RTCSocketMessage { + title: String, + callHash: Option, + peerId: Option, + data: Option, +} + #[derive(Message)] #[rtype(result = "()")] enum RTCMessages { @@ -98,10 +108,62 @@ impl StreamHandler { + match serde_json::from_str::(&txt) { + Err(e) => { + eprintln!("Failed to parse a message from RTC proxy! {:#?}", e); + } + + Ok(msg) => { + if let Err(e) = process_message_from_relay(&msg) { + eprintln!("Failed to process signal from RTC Relay! {:#?}", e); + } + } + } + } + Message::Binary(_) => { + eprintln!("RTC WS Message::Binary"); + ctx.stop(); + } + Message::Continuation(_) => { + eprintln!("RTC WS Message::Continuation"); + ctx.stop(); + } + Message::Ping(data) => { + ctx.pong(&data); + } + Message::Pong(_) => {} + Message::Close(_) => { + eprintln!("RTC WS Message::Close"); + ctx.stop(); + } + Message::Nop => {} + } } } +/// Process a message from the server +fn process_message_from_relay(msg: &RTCSocketMessage) -> Res { + match msg.title.as_str() { + "signal" => { + events_helper::propagate_event(&Event::NewRTCRelayMessage(&NewRtcRelayMessage { + call_hash: msg.callHash.clone().unwrap_or(String::new()), + peer_id: msg.peerId.clone().unwrap_or("0".to_string()), + data: serde_json::to_string(msg.data.as_ref().unwrap_or(&Value::Null)) + .unwrap_or("failed to serialize signal data".to_string()), + }))?; + } + + title => { + eprintln!("Unknown message '{}' from RTC proxy!", title); + } + } + + Ok(()) +} + impl Handler for RtcRelayActor { type Result = (); diff --git a/src/data/call_signal.rs b/src/data/call_signal.rs index 79c3581..3490dc4 100644 --- a/src/data/call_signal.rs +++ b/src/data/call_signal.rs @@ -24,6 +24,7 @@ pub enum CallSignal { Candidate(IceCandidate, webrtc_sdp::attribute_type::SdpAttribute), } +/// New call signal from client pub struct NewUserCallSignal { pub call_hash: String, @@ -34,6 +35,13 @@ pub struct NewUserCallSignal { pub raw_data: String, } +/// New call signal from RTC relay +pub struct NewRtcRelayMessage { + pub call_hash: String, + pub peer_id: String, + pub data: String, +} + impl SdpType { pub fn from_str(val: &str) -> Res { match val { diff --git a/src/helpers/events_helper.rs b/src/helpers/events_helper.rs index c744b8b..e773361 100644 --- a/src/helpers/events_helper.rs +++ b/src/helpers/events_helper.rs @@ -6,7 +6,7 @@ use crate::controllers::{calls_controller, comments_controller, conversations_controller, notifications_controller, rtc_relay_controller, user_ws_controller}; use crate::data::api_client::APIClient; -use crate::data::call_signal::NewUserCallSignal; +use crate::data::call_signal::{NewRtcRelayMessage, NewUserCallSignal}; use crate::data::comment::Comment; use crate::data::conversation::ConvID; use crate::data::conversation_message::ConversationMessage; @@ -56,9 +56,12 @@ pub enum Event<'a> { /// User left call UserLeftCall(&'a ConvID, &'a UserID), - /// Got new user call signal + /// Got a new user call signal NewUserCallSignal(&'a NewUserCallSignal), + /// Got a new RTC relay message + NewRTCRelayMessage(&'a NewRtcRelayMessage), + /// No event None, }