fix: better reliability for microphone state

This commit is contained in:
Ahmet Kaan Gümüş 2025-05-16 05:16:52 +03:00
parent 1e9808579a
commit d930888abb
3 changed files with 24 additions and 8 deletions

View file

@ -55,7 +55,7 @@ impl Default for State {
#[derive(Debug, Clone, Copy)]
pub enum Message {
State,
None,
JoinRoom,
LeaveRoom,
UnmuteMicrophone,
@ -105,7 +105,7 @@ impl App {
}
pub fn update(&mut self, message: Message) -> Task<Message> {
match message {
Message::State => Task::none(),
Message::None => Task::none(),
Message::JoinRoom => {
self.gui_status.write().unwrap().room = State::Loading;
let client_config = self.client_config.clone();
@ -124,7 +124,7 @@ impl App {
}
},
)
.map(|_| Message::State)
.map(|_| Message::None)
}
Message::LeaveRoom => {
self.gui_status.write().unwrap().room = State::Loading;
@ -143,13 +143,25 @@ impl App {
Task::none()
}
Message::UnmuteMicrophone => {
self.gui_status.write().unwrap().microphone = State::Active;
self.gui_status.write().unwrap().microphone = State::Loading;
let microphone_sender = self.channel.microphone.clone();
let microphone_stop_signal = oneshot::channel();
self.signal.microphone = Some(microphone_stop_signal.0);
Task::perform(record(microphone_sender, microphone_stop_signal.1), |_| {
Message::State
})
let is_microphone_started_signal = oneshot::channel();
tokio::spawn(record(
microphone_sender,
is_microphone_started_signal.0,
microphone_stop_signal.1,
));
let gui_status = self.gui_status.clone();
Task::perform(
async move {
if let Ok(_) = is_microphone_started_signal.1.await {
gui_status.write().unwrap().microphone = State::Active;
}
},
|_| Message::None,
)
}
Message::MuteMicrophone => {
self.gui_status.write().unwrap().microphone = State::Loading;

View file

@ -12,7 +12,7 @@ impl ClientConfig {
fn new() -> Self {
Self {
certificate_path: "./client/certificates/cert.pem".to_string(),
server_address: "localhost:4546".to_string(),
server_address: "127.0.0.1:4546".to_string(),
}
}
}

View file

@ -3,6 +3,7 @@ use tokio::sync::{broadcast, oneshot};
pub async fn record(
microphone_sender: broadcast::Sender<f32>,
is_microphone_started_signal: oneshot::Sender<bool>,
microphone_stop_signal: oneshot::Receiver<bool>,
) {
let host = cpal::default_host();
@ -24,6 +25,9 @@ pub async fn record(
.unwrap();
input_stream.play().unwrap();
println!("Recording Started");
if let Err(_) = is_microphone_started_signal.send(true) {
eprintln!("Error: Is Microphone Started | Send");
}
tokio::task::block_in_place(|| {
let _ = microphone_stop_signal.blocking_recv();