cleaned some warnings and updated player trait

This commit is contained in:
MrDulfin 2024-05-20 00:00:06 -04:00
parent c8c00a765b
commit f79bf8d477
3 changed files with 16 additions and 50 deletions

View file

@ -4,29 +4,24 @@
use crossbeam_channel; use crossbeam_channel;
use crossbeam_channel::{Receiver, Sender}; use crossbeam_channel::{Receiver, Sender};
use listenbrainz::ListenBrainz;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use std::thread::spawn;
use crossbeam_channel::unbounded; use crossbeam_channel::unbounded;
use std::error::Error; use std::error::Error;
use uuid::Uuid; use uuid::Uuid;
use crate::music_controller::queue::QueueItem; use crate::music_player::player::Player;
use crate::music_player::gstreamer::GStreamer; use crate::music_storage::library::URI;
use crate::music_storage::library::{Tag, URI};
use crate::{ use crate::{
config::config::Config, config::config::Config, music_controller::queue::Queue, music_storage::library::MusicLibrary,
music_controller::queue::Queue,
music_storage::library::{MusicLibrary, Song},
}; };
pub struct Controller { pub struct Controller<P: Player> {
pub queue: Queue, pub queue: Queue,
pub config: Arc<RwLock<Config>>, pub config: Arc<RwLock<Config>>,
pub library: MusicLibrary, pub library: MusicLibrary,
player_mail: MailMan<PlayerCmd, PlayerRes>, pub player: P,
} }
#[derive(Debug)] #[derive(Debug)]
@ -69,10 +64,10 @@ enum PlayerRes {
} }
#[allow(unused_variables)] #[allow(unused_variables)]
impl Controller { impl<P> Controller<P> {
pub fn start<P>(config_path: P) -> Result<Self, Box<dyn Error>> pub fn start<T>(config_path: T) -> Result<Self, Box<dyn Error>>
where where
std::path::PathBuf: std::convert::From<P>, std::path::PathBuf: std::convert::From<T>,
{ {
let config_path = PathBuf::from(config_path); let config_path = PathBuf::from(config_path);
@ -84,26 +79,11 @@ impl Controller {
let (player_mail, in_thread) = MailMan::<PlayerCmd, PlayerRes>::double(); let (player_mail, in_thread) = MailMan::<PlayerCmd, PlayerRes>::double();
spawn(move || {
let mut player = GStreamer::new().unwrap();
while true {
match in_thread.recv().unwrap() {
PlayerCmd::Test(uri) => {
&player.set_volume(0.04);
_ = &player.enqueue_next(&uri).unwrap();
_ = &player.play();
in_thread.send(PlayerRes::Test).unwrap();
}
}
}
});
Ok(Controller { Ok(Controller {
queue: Queue::new(), queue: Queue::new(),
config: config_.clone(), config: config_.clone(),
library, library,
player_mail, player: P::new(),
}) })
} }

View file

@ -1,10 +1,9 @@
use chrono::Duration; use chrono::Duration;
use thiserror::Error;
use gstreamer as gst; use gstreamer as gst;
use thiserror::Error;
use crate::music_storage::library::URI; use crate::music_storage::library::URI;
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum PlayerError { pub enum PlayerError {
#[error("player initialization failed")] #[error("player initialization failed")]
@ -24,6 +23,7 @@ pub enum PlayerError {
} }
pub trait Player { pub trait Player {
fn new() -> Self;
fn source(&self) -> &Option<URI>; fn source(&self) -> &Option<URI>;
fn enqueue_next(&mut self, next_track: &URI) -> Result<(), PlayerError>; fn enqueue_next(&mut self, next_track: &URI) -> Result<(), PlayerError>;
@ -53,5 +53,4 @@ pub trait Player {
fn seek_by(&mut self, seek_amount: Duration) -> Result<(), PlayerError>; fn seek_by(&mut self, seek_amount: Duration) -> Result<(), PlayerError>;
fn seek_to(&mut self, target_pos: Duration) -> Result<(), PlayerError>; fn seek_to(&mut self, target_pos: Duration) -> Result<(), PlayerError>;
} }

View file

@ -6,7 +6,6 @@ use crate::config::config::Config;
// Various std things // Various std things
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::error::Error; use std::error::Error;
use std::io::Write;
use std::ops::ControlFlow::{Break, Continue}; use std::ops::ControlFlow::{Break, Continue};
// Files // Files
@ -14,9 +13,8 @@ use file_format::{FileFormat, Kind};
use glib::filename_to_uri; use glib::filename_to_uri;
use lofty::{AudioFile, ItemKey, ItemValue, ParseOptions, Probe, TagType, TaggedFileExt}; use lofty::{AudioFile, ItemKey, ItemValue, ParseOptions, Probe, TagType, TaggedFileExt};
use rcue::parser::parse_from_file; use rcue::parser::parse_from_file;
use std::fs::{self, File}; use std::fs;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use tempfile::TempDir;
use uuid::Uuid; use uuid::Uuid;
use walkdir::WalkDir; use walkdir::WalkDir;
@ -144,22 +142,17 @@ pub enum DoNotTrack {
Discord, Discord,
} }
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive] #[non_exhaustive]
pub enum SongType { pub enum SongType {
// TODO: add MORE?! song types // TODO: add MORE?! song types
#[default]
Main, Main,
Instrumental, Instrumental,
Remix, Remix,
Custom(String), Custom(String),
} }
impl Default for SongType {
fn default() -> Self {
SongType::Main
}
}
/// Stores information about a single song /// Stores information about a single song
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct Song { pub struct Song {
@ -1127,18 +1120,12 @@ impl MusicLibrary {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use std::{ use std::{
path::{Path, PathBuf}, path::PathBuf,
sync::{Arc, RwLock}, sync::{Arc, RwLock},
thread::sleep,
time::{Duration, Instant},
}; };
use tempfile::TempDir;
use crate::{config::config::Config, music_storage::library::MusicLibrary}; use crate::{config::config::Config, music_storage::library::MusicLibrary};
use super::Song;
#[test] #[test]
fn library_init() { fn library_init() {
let config = Config::read_file(PathBuf::from("test_config/config_test.json")).unwrap(); let config = Config::read_file(PathBuf::from("test_config/config_test.json")).unwrap();