Main player loop

This commit is contained in:
Muaz Ahmad 2024-11-27 16:25:39 +05:00
parent d6c9bf4d3b
commit 85ae8d1624
4 changed files with 69 additions and 3 deletions

14
src/player/errors.rs Normal file
View file

@ -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"),
}
}
}

View file

@ -26,6 +26,7 @@ pub struct Player {
api_chan: Sender<APIEvent>, api_chan: Sender<APIEvent>,
error_chan: Sender<Error>, error_chan: Sender<Error>,
player_chan: Receiver<PlayerEvent>, player_chan: Receiver<PlayerEvent>,
playlist: playlist::Playlist,
} }
pub fn init( pub fn init(
@ -42,12 +43,22 @@ pub fn init(
settings, settings,
audio_chan, audio_chan,
api_chan, api_chan,
error_chan, error_chan: error_chan.clone(),
player_chan, player_chan,
playlist: playlist::Playlist::new(),
};
match player.begin() {
Err(err) => {
error_chan.send(err).unwrap();
thread::park();
return;
}
Ok(_) => (),
}; };
player.begin();
}); });
Ok(()) Ok(())
} }
mod errors;
mod player; mod player;
mod playlist;

View file

@ -1,3 +1,5 @@
use std::{thread, time::Duration};
use crate::{ssonic::APIEvent, utils::Error}; use crate::{ssonic::APIEvent, utils::Error};
use super::Player; use super::Player;
@ -17,7 +19,22 @@ impl Player {
fn run(&mut self) -> Result<(), Error> { fn run(&mut self) -> Result<(), Error> {
loop { 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(())
}
} }

24
src/player/playlist.rs Normal file
View file

@ -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<Song>,
}
impl Playlist {
pub fn get_next(&mut self) -> Result<Song, Error> {
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(),
}
}
}