From b2169abc94e45459aa5ca4be3f2daaed13d1bf5d Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Mon, 25 Nov 2024 16:39:42 +0500 Subject: [PATCH] Add a channel for replies to Player across thread --- src/audio/mod.rs | 5 ++++- src/main.rs | 8 ++++++-- src/player/mod.rs | 8 +++++++- src/ssonic/mod.rs | 32 ++++++++++++++++++++++++++++---- 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/audio/mod.rs b/src/audio/mod.rs index 5dc30cc..558da44 100644 --- a/src/audio/mod.rs +++ b/src/audio/mod.rs @@ -8,7 +8,7 @@ use std::{ use std::sync::mpsc::Receiver; -use crate::{config::Settings, utils::Error}; +use crate::{config::Settings, player::PlayerEvent, utils::Error}; pub enum AudioEvent {} @@ -16,11 +16,13 @@ pub struct SoundManager { settings: Arc, error_chan: Sender, audio_controls: Receiver, + player_chan: Sender, } pub fn init( settings: Arc, error_chan: Sender, + player_chan: Sender, ) -> Result, Error> { let (audio_control_in, audio_control_out) = channel(); thread::spawn(|| { @@ -28,6 +30,7 @@ pub fn init( settings, error_chan, audio_controls: audio_control_out, + player_chan, }; loop {} }); diff --git a/src/main.rs b/src/main.rs index 0fa00d0..747a585 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,13 +11,17 @@ mod utils; fn init() -> Result, utils::Error> { let settings = config::init()?; let (error_in, error_out) = channel(); - let audio_event_chan = audio::init(settings.clone(), error_in.clone())?; - let api_event_chan = ssonic::init(settings.clone(), error_in.clone())?; + let (player_events_in, player_events_out) = channel(); + let audio_event_chan = + audio::init(settings.clone(), error_in.clone(), player_events_in.clone())?; + let api_event_chan = + ssonic::init(settings.clone(), error_in.clone(), player_events_in.clone())?; let mut player = player::init( settings.clone(), audio_event_chan, api_event_chan, error_in.clone(), + player_events_out, )?; player.begin()?; Ok(error_out) diff --git a/src/player/mod.rs b/src/player/mod.rs index 5b15be7..f614c20 100644 --- a/src/player/mod.rs +++ b/src/player/mod.rs @@ -1,7 +1,12 @@ -use std::sync::{mpsc::Sender, Arc}; +use std::sync::{ + mpsc::{Receiver, Sender}, + Arc, +}; use crate::{audio::AudioEvent, config::Settings, ssonic::APIEvent, utils::Error}; +pub struct PlayerEvent {} + pub struct Player {} pub fn init( @@ -9,6 +14,7 @@ pub fn init( audio_chan: Sender, api_chan: Sender, error_chan: Sender, + player_chan: Receiver, ) -> Result { unimplemented!() } diff --git a/src/ssonic/mod.rs b/src/ssonic/mod.rs index be481e0..15ccf39 100644 --- a/src/ssonic/mod.rs +++ b/src/ssonic/mod.rs @@ -1,9 +1,33 @@ -use std::sync::{mpsc::Sender, Arc}; +use std::{ + sync::{ + mpsc::{channel, Receiver, Sender}, + Arc, + }, + thread, +}; -use crate::{config::Settings, utils::Error}; +use crate::{config::Settings, player::PlayerEvent, utils::Error}; pub enum APIEvent {} -pub fn init(settings: Arc, error_chan: Sender) -> Result, Error> { - unimplemented!() +pub struct APIClient { + settings: Arc, + error_chan: Sender, + api_requests: Receiver, + player_chan: Sender, +} + +pub fn init( + settings: Arc, + error_chan: Sender, + player_chan: Sender, +) -> Result, Error> { + let (api_requests_in, api_requests_out) = channel(); + thread::spawn(|| APIClient { + settings, + error_chan, + api_requests: api_requests_out, + player_chan, + }); + Ok(api_requests_in) }