From 85ae8d162484cf08db31ef44e83cfabdf2f6a6e7 Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Wed, 27 Nov 2024 16:25:39 +0500 Subject: [PATCH] Main player loop --- src/player/errors.rs | 14 ++++++++++++++ src/player/mod.rs | 15 +++++++++++++-- src/player/player.rs | 19 ++++++++++++++++++- src/player/playlist.rs | 24 ++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/player/errors.rs create mode 100644 src/player/playlist.rs diff --git a/src/player/errors.rs b/src/player/errors.rs new file mode 100644 index 0000000..1c8fb90 --- /dev/null +++ b/src/player/errors.rs @@ -0,0 +1,14 @@ +#[derive(Debug)] +pub enum PlayerError { + PlaylistEmpty, +} + +impl std::error::Error for PlayerError {} + +impl std::fmt::Display for PlayerError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::PlaylistEmpty => write!(f, "Playlist empty"), + } + } +} diff --git a/src/player/mod.rs b/src/player/mod.rs index c0c20bf..9997289 100644 --- a/src/player/mod.rs +++ b/src/player/mod.rs @@ -26,6 +26,7 @@ pub struct Player { api_chan: Sender, error_chan: Sender, player_chan: Receiver, + playlist: playlist::Playlist, } pub fn init( @@ -42,12 +43,22 @@ pub fn init( settings, audio_chan, api_chan, - error_chan, + error_chan: error_chan.clone(), player_chan, + playlist: playlist::Playlist::new(), + }; + match player.begin() { + Err(err) => { + error_chan.send(err).unwrap(); + thread::park(); + return; + } + Ok(_) => (), }; - player.begin(); }); Ok(()) } +mod errors; mod player; +mod playlist; diff --git a/src/player/player.rs b/src/player/player.rs index 82b7be3..f606cd5 100644 --- a/src/player/player.rs +++ b/src/player/player.rs @@ -1,3 +1,5 @@ +use std::{thread, time::Duration}; + use crate::{ssonic::APIEvent, utils::Error}; use super::Player; @@ -17,7 +19,22 @@ impl Player { fn run(&mut self) -> Result<(), Error> { loop { - std::thread::sleep(std::time::Duration::from_millis(100)); + self.handle_events()?; + self.handle_inputs()?; + self.update()?; + thread::sleep(Duration::from_millis(100)); } } + + fn handle_events(&mut self) -> Result<(), Error> { + Ok(()) + } + + fn handle_inputs(&mut self) -> Result<(), Error> { + Ok(()) + } + + fn update(&mut self) -> Result<(), Error> { + Ok(()) + } } diff --git a/src/player/playlist.rs b/src/player/playlist.rs new file mode 100644 index 0000000..e1ad362 --- /dev/null +++ b/src/player/playlist.rs @@ -0,0 +1,24 @@ +use std::collections::VecDeque; + +use crate::{ssonic::response::Song, utils::Error}; + +use super::errors::PlayerError; + +pub struct Playlist { + song_list: VecDeque, +} + +impl Playlist { + pub fn get_next(&mut self) -> Result { + match self.song_list.pop_front() { + Some(song) => Ok(song), + None => Err(Box::new(PlayerError::PlaylistEmpty)), + } + } + + pub fn new() -> Playlist { + Playlist { + song_list: VecDeque::new(), + } + } +}