From cbe1a90fcb2ee911b14bde74a896f78e37ae6441 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Wed, 4 Sep 2024 21:37:13 -0400 Subject: [PATCH] Link up the App and the AudioController in main.rs --- gm-dash/server/src/app/mod.rs | 3 +- gm-dash/server/src/audio_control/mod.rs | 37 ++++++++----------------- gm-dash/server/src/main.rs | 18 ++++++++++-- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/gm-dash/server/src/app/mod.rs b/gm-dash/server/src/app/mod.rs index 6d97ecf..aa5496d 100644 --- a/gm-dash/server/src/app/mod.rs +++ b/gm-dash/server/src/app/mod.rs @@ -10,7 +10,7 @@ use tokio::{ }; use crate::{ - audio_control::{AudioControl}, + audio_control::AudioControl, types::{AppError, AudioControlMessage, AudioStatusMessage, TrackInfo, TrackSpec, Volume}, }; @@ -64,6 +64,7 @@ impl App { state.write().unwrap().playing = true; } Some(AudioStatusMessage::Status(track_status)) => { + println!("status: {:?}", track_status); state.write().unwrap().track_status = track_status; } msg => println!("message received from audio controller: {:?}", msg), diff --git a/gm-dash/server/src/audio_control/mod.rs b/gm-dash/server/src/audio_control/mod.rs index d29dab3..9570d88 100644 --- a/gm-dash/server/src/audio_control/mod.rs +++ b/gm-dash/server/src/audio_control/mod.rs @@ -1,7 +1,8 @@ use std::{ collections::HashMap, path::PathBuf, - sync::{Arc, RwLock}, time::Duration, + sync::{Arc, RwLock}, + time::Duration, }; use gstreamer::{prelude::*, ClockTime, MessageType, MessageView}; @@ -19,10 +20,7 @@ pub enum AudioError { InvalidState, } -pub struct AudioControl { - control_rx: Receiver, - status_tx: Sender, -} +pub struct AudioControl {} /* impl Default for AudioControl { @@ -33,25 +31,13 @@ impl Default for AudioControl { */ impl AudioControl { - pub fn new() -> ( - Self, - Sender, - Receiver, - ) { - 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 fn new() -> Self { + Self {} } - pub async fn listen(&mut self) { - while let Some(msg) = self.control_rx.recv().await { + pub async fn listen(&self, mut control_rx: Receiver) { + println!("waiting for control message"); + while let Some(msg) = control_rx.recv().await { match msg { AudioControlMessage::Play => { unimplemented!() @@ -72,13 +58,14 @@ impl AudioControl { } } - pub async fn report(&self) { + pub async fn report(&self, status_tx: Sender) { loop { - self.status_tx + println!("sending status message"); + status_tx .send(AudioStatusMessage::Status(vec![])) .await .expect("to successfully send a message"); - tokio::time::sleep(Duration::from_secs(1)); + let _ = tokio::time::sleep(Duration::from_secs(1)).await; } } } diff --git a/gm-dash/server/src/main.rs b/gm-dash/server/src/main.rs index ac85ecd..7087a2b 100644 --- a/gm-dash/server/src/main.rs +++ b/gm-dash/server/src/main.rs @@ -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 audio_control::AudioControl; use pipewire::{context::Context, main_loop::MainLoop}; use serde::Deserialize; use tokio::task::spawn_blocking; use warp::{serve, Filter}; -mod audio_control; mod app; +mod audio_control; mod types; #[derive(Deserialize)] @@ -137,6 +142,15 @@ async fn main() { 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 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({