feat: cm interface

This commit is contained in:
Kingtous
2023-02-07 16:11:55 +08:00
parent 5e21a81a5c
commit 2943d2d0cc
12 changed files with 143 additions and 44 deletions

View File

@@ -754,6 +754,7 @@ impl<T: InvokeUiSession> Remote<T> {
Data::CloseVoiceCall => {
self.stop_voice_call();
let msg = new_voice_call_request(false);
self.handler.on_voice_call_closed("Closed manually by the peer");
allow_err!(peer.send(&msg).await);
}
_ => {}

View File

@@ -538,16 +538,9 @@ pub mod connection_manager {
self.push_event("show_elevation", vec![("show", &show.to_string())]);
}
fn voice_call_started(&self, id: i32) {
self.push_event("voice_call_started", vec![("show", &id.to_string())]);
}
fn voice_call_incoming(&self, id: i32) {
self.push_event("voice_call_incoming", vec![("id", &id.to_string())]);
}
fn voice_call_closed(&self, id: i32, reason: &str) {
self.push_event("voice_call_closed", vec![("id", &id.to_string()), ("reason", &reason.to_string())]);
fn update_voice_call_state(&self, client: &crate::ui_cm_interface::Client) {
let client_json = serde_json::to_string(&client).unwrap_or("".into());
self.push_event("update_voice_call_state", vec![("client", &client_json)]);
}
}

View File

@@ -838,6 +838,14 @@ pub fn session_close_voice_call(id: String) {
}
}
pub fn cm_handle_incoming_voice_call(id: i32, accept: bool) {
crate::ui_cm_interface::handle_incoming_voice_call(id, accept);
}
pub fn cm_close_voice_call(id: i32) {
crate::ui_cm_interface::close_voice_call(id);
}
pub fn main_get_last_remote_id() -> String {
LocalConfig::get_remote_id()
}

View File

@@ -1636,7 +1636,7 @@ impl Connection {
if let Some(sound_input) = std::mem::replace(&mut self.audio_input_device_before_voice_call, None) {
set_sound_input(sound_input);
// Notify the connection manager.
self.send_to_cm(Data::CloseVoiceCall("Closed manually by the peer".to_owned()));
self.send_to_cm(Data::CloseVoiceCall("".to_owned()));
}
}

View File

@@ -56,16 +56,15 @@ impl InvokeUiCM for SciterHandler {
self.call("showElevation", &make_args!(show));
}
fn voice_call_started(&self, id: i32) {
self.call("voice_call_started", &make_args!(id));
}
fn voice_call_incoming(&self, id: i32) {
self.call("voice_call_incoming", &make_args!(id));
}
fn voice_call_closed(&self, id: i32, reason: &str) {
self.call("voice_call_incoming", &make_args!(id, reason));
fn update_voice_call_state(&self, client: &crate::ui_cm_interface::Client) {
self.call(
"updateVoiceCallState",
&make_args!(
client.id,
client.in_voice_call,
client.incoming_voice_call
),
);
}
}

View File

@@ -49,6 +49,8 @@ pub struct Client {
pub restart: bool,
pub recording: bool,
pub from_switch: bool,
pub in_voice_call: bool,
pub incoming_voice_call: bool,
#[serde(skip)]
tx: UnboundedSender<Data>,
}
@@ -89,11 +91,7 @@ pub trait InvokeUiCM: Send + Clone + 'static + Sized {
fn show_elevation(&self, show: bool);
fn voice_call_started(&self, id: i32);
fn voice_call_incoming(&self, id: i32);
fn voice_call_closed(&self, id: i32, reason: &str);
fn update_voice_call_state(&self, client: &Client);
}
impl<T: InvokeUiCM> Deref for ConnectionManager<T> {
@@ -144,6 +142,8 @@ impl<T: InvokeUiCM> ConnectionManager<T> {
recording,
from_switch,
tx,
in_voice_call: false,
incoming_voice_call: false
};
CLIENTS
.write()
@@ -188,15 +188,27 @@ impl<T: InvokeUiCM> ConnectionManager<T> {
}
fn voice_call_started(&self, id: i32) {
self.ui_handler.voice_call_started(id);
if let Some(client) = CLIENTS.write().unwrap().get_mut(&id) {
client.incoming_voice_call = false;
client.in_voice_call = true;
self.ui_handler.update_voice_call_state(client);
}
}
fn voice_call_incoming(&self, id: i32) {
self.ui_handler.voice_call_incoming(id);
if let Some(client) = CLIENTS.write().unwrap().get_mut(&id) {
client.incoming_voice_call = true;
client.in_voice_call = false;
self.ui_handler.update_voice_call_state(client);
}
}
fn voice_call_closed(&self, id: i32, reason: &str) {
self.ui_handler.voice_call_closed(id, reason);
fn voice_call_closed(&self, id: i32, _reason: &str) {
if let Some(client) = CLIENTS.write().unwrap().get_mut(&id) {
client.incoming_voice_call = false;
client.in_voice_call = false;
self.ui_handler.update_voice_call_state(client);
}
}
}
@@ -832,3 +844,17 @@ pub fn elevate_portable(_id: i32) {
}
}
}
#[inline]
pub fn handle_incoming_voice_call(id: i32, accept: bool) {
if let Some(client) = CLIENTS.write().unwrap().get_mut(&id) {
allow_err!(client.tx.send(Data::VoiceCallResponse(accept)));
};
}
#[inline]
pub fn close_voice_call(id: i32) {
if let Some(client) = CLIENTS.write().unwrap().get_mut(&id) {
allow_err!(client.tx.send(Data::CloseVoiceCall("".to_owned())));
};
}

View File

@@ -661,7 +661,7 @@ impl<T: InvokeUiSession> Session<T> {
pub fn request_voice_call(&self) {
self.send(Data::NewVoiceCall);
}
pub fn close_voice_call(&self) {
self.send(Data::CloseVoiceCall);
}