Link up the App and the AudioController in main.rs

This commit is contained in:
Savanni D'Gerinel 2024-09-04 21:37:13 -04:00
parent f555804f10
commit cbe1a90fcb
3 changed files with 30 additions and 28 deletions

View File

@ -10,7 +10,7 @@ use tokio::{
}; };
use crate::{ use crate::{
audio_control::{AudioControl}, audio_control::AudioControl,
types::{AppError, AudioControlMessage, AudioStatusMessage, TrackInfo, TrackSpec, Volume}, types::{AppError, AudioControlMessage, AudioStatusMessage, TrackInfo, TrackSpec, Volume},
}; };
@ -64,6 +64,7 @@ impl App {
state.write().unwrap().playing = true; state.write().unwrap().playing = true;
} }
Some(AudioStatusMessage::Status(track_status)) => { Some(AudioStatusMessage::Status(track_status)) => {
println!("status: {:?}", track_status);
state.write().unwrap().track_status = track_status; state.write().unwrap().track_status = track_status;
} }
msg => println!("message received from audio controller: {:?}", msg), msg => println!("message received from audio controller: {:?}", msg),

View File

@ -1,7 +1,8 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
path::PathBuf, path::PathBuf,
sync::{Arc, RwLock}, time::Duration, sync::{Arc, RwLock},
time::Duration,
}; };
use gstreamer::{prelude::*, ClockTime, MessageType, MessageView}; use gstreamer::{prelude::*, ClockTime, MessageType, MessageView};
@ -19,10 +20,7 @@ pub enum AudioError {
InvalidState, InvalidState,
} }
pub struct AudioControl { pub struct AudioControl {}
control_rx: Receiver<AudioControlMessage>,
status_tx: Sender<AudioStatusMessage>,
}
/* /*
impl Default for AudioControl { impl Default for AudioControl {
@ -33,25 +31,13 @@ impl Default for AudioControl {
*/ */
impl AudioControl { impl AudioControl {
pub fn new() -> ( pub fn new() -> Self {
Self, Self {}
Sender<AudioControlMessage>,
Receiver<AudioStatusMessage>,
) {
let (control_tx, control_rx) = tokio::sync::mpsc::channel(5);
let (status_tx, status_rx) = tokio::sync::mpsc::channel(5);
(
Self {
control_rx,
status_tx,
},
control_tx,
status_rx,
)
} }
pub async fn listen(&mut self) { pub async fn listen(&self, mut control_rx: Receiver<AudioControlMessage>) {
while let Some(msg) = self.control_rx.recv().await { println!("waiting for control message");
while let Some(msg) = control_rx.recv().await {
match msg { match msg {
AudioControlMessage::Play => { AudioControlMessage::Play => {
unimplemented!() unimplemented!()
@ -72,13 +58,14 @@ impl AudioControl {
} }
} }
pub async fn report(&self) { pub async fn report(&self, status_tx: Sender<AudioStatusMessage>) {
loop { loop {
self.status_tx println!("sending status message");
status_tx
.send(AudioStatusMessage::Status(vec![])) .send(AudioStatusMessage::Status(vec![]))
.await .await
.expect("to successfully send a message"); .expect("to successfully send a message");
tokio::time::sleep(Duration::from_secs(1)); let _ = tokio::time::sleep(Duration::from_secs(1)).await;
} }
} }
} }

View File

@ -1,13 +1,18 @@
use std::{net::{Ipv6Addr, SocketAddrV6}, path::PathBuf, sync::Arc}; use std::{
net::{Ipv6Addr, SocketAddrV6},
path::PathBuf,
sync::Arc,
};
use app::App; use app::App;
use audio_control::AudioControl;
use pipewire::{context::Context, main_loop::MainLoop}; use pipewire::{context::Context, main_loop::MainLoop};
use serde::Deserialize; use serde::Deserialize;
use tokio::task::spawn_blocking; use tokio::task::spawn_blocking;
use warp::{serve, Filter}; use warp::{serve, Filter};
mod audio_control;
mod app; mod app;
mod audio_control;
mod types; mod types;
#[derive(Deserialize)] #[derive(Deserialize)]
@ -137,6 +142,15 @@ async fn main() {
let (audio_status_tx, audio_status_rx) = tokio::sync::mpsc::channel(5); let (audio_status_tx, audio_status_rx) = tokio::sync::mpsc::channel(5);
let app = Arc::new(App::new(audio_control_tx, audio_status_rx)); let app = Arc::new(App::new(audio_control_tx, audio_status_rx));
let audio_controller = Arc::new(AudioControl::new());
tokio::spawn({
let audio_controller = audio_controller.clone();
async move { audio_controller.listen(audio_control_rx).await }
});
tokio::spawn({
let audio_controller = audio_controller.clone();
async move { audio_controller.report(audio_status_tx).await }
});
/* /*
spawn_blocking({ spawn_blocking({