2024-04-27 19:48:13 +03:00
|
|
|
use iced::{
|
|
|
|
widget::{button, column, Column},
|
|
|
|
Command,
|
|
|
|
};
|
2024-04-27 15:36:28 +03:00
|
|
|
use tokio::sync::broadcast::{channel, Sender};
|
|
|
|
|
|
|
|
use crate::{recording, streaming, utils::get_config, Config, BUFFER_LENGTH};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum Message {
|
|
|
|
StartStreaming,
|
2024-04-27 19:48:13 +03:00
|
|
|
StopStreaming,
|
2024-04-27 15:36:28 +03:00
|
|
|
ConfigLoad(Config),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Streamer {
|
|
|
|
config: Option<Config>,
|
2024-04-27 19:48:13 +03:00
|
|
|
sound_stream_producer: Sender<f32>,
|
|
|
|
stop_connection_producer: Sender<bool>,
|
|
|
|
stop_recording_producer: Sender<bool>,
|
2024-04-27 15:36:28 +03:00
|
|
|
}
|
|
|
|
impl Default for Streamer {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Streamer {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
config: None,
|
|
|
|
sound_stream_producer: channel(BUFFER_LENGTH).0,
|
2024-04-27 19:48:13 +03:00
|
|
|
stop_connection_producer: channel(BUFFER_LENGTH).0,
|
|
|
|
stop_recording_producer: channel(BUFFER_LENGTH).0,
|
2024-04-27 15:36:28 +03:00
|
|
|
}
|
|
|
|
}
|
2024-04-27 19:48:13 +03:00
|
|
|
pub fn update(&mut self, message: Message) {
|
2024-04-27 15:36:28 +03:00
|
|
|
match message {
|
|
|
|
Message::StartStreaming => {
|
2024-04-27 19:48:13 +03:00
|
|
|
println!("Start Stream");
|
|
|
|
tokio::spawn(streaming::connect(
|
|
|
|
self.sound_stream_producer.subscribe(),
|
|
|
|
self.config.clone().unwrap(),
|
|
|
|
self.stop_connection_producer.subscribe(),
|
|
|
|
));
|
|
|
|
tokio::spawn(recording::record(
|
|
|
|
self.sound_stream_producer.clone(),
|
|
|
|
self.stop_recording_producer.subscribe(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Message::StopStreaming => {
|
|
|
|
println!("Stop Stream");
|
|
|
|
self.stop_connection_producer.send(true).unwrap();
|
|
|
|
self.stop_recording_producer.send(true).unwrap();
|
2024-04-27 15:36:28 +03:00
|
|
|
}
|
|
|
|
Message::ConfigLoad(config) => {
|
|
|
|
self.config = Some(config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn view(&self) -> Column<Message> {
|
|
|
|
column![
|
2024-04-27 19:48:13 +03:00
|
|
|
button("Start Streaming").on_press(Message::StartStreaming),
|
|
|
|
button("Stop Streaming").on_press(Message::StopStreaming),
|
2024-04-27 15:36:28 +03:00
|
|
|
]
|
|
|
|
}
|
|
|
|
pub fn load_config() -> Command<Message> {
|
|
|
|
Command::perform(get_config(), Message::ConfigLoad)
|
|
|
|
}
|
|
|
|
}
|