Make MPD connection failure reporting less janky

This commit is contained in:
NGnius (Graham) 2022-05-30 20:55:48 -04:00
parent fe7962b229
commit e4535399f9
4 changed files with 28 additions and 7 deletions

View file

@ -14,6 +14,8 @@ pub(crate) mod uri;
pub use controller::MpsController; pub use controller::MpsController;
pub use errors::{PlaybackError, UriError, PlayerError}; pub use errors::{PlaybackError, UriError, PlayerError};
pub use player::MpsPlayer; pub use player::MpsPlayer;
#[cfg(feature = "mpd")]
pub use player::mpd_connection;
//pub use utility::{play_script}; //pub use utility::{play_script};
#[cfg(test)] #[cfg(test)]

View file

@ -6,7 +6,7 @@ use rodio::{decoder::Decoder, OutputStream, OutputStreamHandle, Sink};
use m3u8_rs::{MediaPlaylist, MediaSegment}; use m3u8_rs::{MediaPlaylist, MediaSegment};
#[cfg(feature = "mpd")] #[cfg(feature = "mpd")]
use mpd::{Client, Song}; use mpd::{Client, Song, error};
use super::uri::Uri; use super::uri::Uri;
@ -48,6 +48,11 @@ impl<'a, T: MpsTokenReader + 'a> MpsPlayer<'a, T> {
Ok(()) Ok(())
} }
#[cfg(feature = "mpd")]
pub fn set_mpd(&mut self, client: Client<std::net::TcpStream>) {
self.mpd_connection = Some(client);
}
pub fn play_all(&mut self) -> Result<(), PlayerError> { pub fn play_all(&mut self) -> Result<(), PlayerError> {
while let Some(item) = self.runner.next() { while let Some(item) = self.runner.next() {
self.sink.sleep_until_end(); self.sink.sleep_until_end();
@ -243,6 +248,11 @@ impl<'a, T: MpsTokenReader + 'a> MpsPlayer<'a, T> {
} }
} }
#[cfg(feature = "mpd")]
pub fn mpd_connection(addr: std::net::SocketAddr) -> error::Result<Client<std::net::TcpStream>> {
Client::connect(addr)
}
#[inline] #[inline]
fn music_title(item: &MpsItem) -> Option<String> { fn music_title(item: &MpsItem) -> Option<String> {
item.field("title").and_then(|x| x.to_owned().to_str()) item.field("title").and_then(|x| x.to_owned().to_str())

View file

@ -81,7 +81,10 @@ fn main() {
// build playback controller // build playback controller
let script_file2 = script_file.clone(); let script_file2 = script_file.clone();
let volume = args.volume.clone(); let volume = args.volume.clone();
let mpd = args.mpd.clone(); let mpd = match args.mpd.clone().map(|a| mps_player::mpd_connection(a.parse().unwrap())).transpose() {
Ok(mpd) => mpd,
Err(e) => panic!("Abort: Cannot connect to MPD: {}", e),
};
let player_builder = move || { let player_builder = move || {
let script_reader = io::BufReader::new( let script_reader = io::BufReader::new(
std::fs::File::open(&script_file2) std::fs::File::open(&script_file2)
@ -94,7 +97,7 @@ fn main() {
player.set_volume(vol); player.set_volume(vol);
} }
if let Some(mpd) = mpd { if let Some(mpd) = mpd {
player.connect_mpd(mpd.parse().unwrap()).unwrap(); player.set_mpd(mpd);
} }
player player
}; };
@ -107,7 +110,7 @@ fn main() {
})); }));
match player.save_m3u8(&mut writer) { match player.save_m3u8(&mut writer) {
Ok(_) => println!( Ok(_) => println!(
"Succes: Finished playlist `{}` from script `{}`", "Success: Finished playlist `{}` from script `{}`",
playlist_file, script_file playlist_file, script_file
), ),
Err(e) => eprintln!("{}", e), Err(e) => eprintln!("{}", e),
@ -116,7 +119,7 @@ fn main() {
// live playback // live playback
let ctrl = MpsController::create(player_builder); let ctrl = MpsController::create(player_builder);
match ctrl.wait_for_done() { match ctrl.wait_for_done() {
Ok(_) => println!("Succes: Finished playback from script `{}`", script_file), Ok(_) => println!("Success: Finished playback from script `{}`", script_file),
Err(e) => eprintln!("{}", e), Err(e) => eprintln!("{}", e),
} }
} }

View file

@ -47,7 +47,13 @@ pub fn repl(args: CliArgs) {
term.set_title("mps"); term.set_title("mps");
let (writer, reader) = channel_io(); let (writer, reader) = channel_io();
let volume = args.volume.clone(); let volume = args.volume.clone();
let mpd = args.mpd.clone(); let mpd = match args.mpd.clone().map(|a| mps_player::mpd_connection(a.parse().unwrap())).transpose() {
Ok(mpd) => mpd,
Err(e) => {
eprintln!("Cannot connect to MPD address `{}`: {}", args.mpd.unwrap(), e);
return;
}
};
let player_builder = move || { let player_builder = move || {
let runner = MpsFaye::with_stream(reader); let runner = MpsFaye::with_stream(reader);
@ -56,7 +62,7 @@ pub fn repl(args: CliArgs) {
player.set_volume(vol); player.set_volume(vol);
} }
if let Some(mpd) = mpd { if let Some(mpd) = mpd {
player.connect_mpd(mpd.parse().unwrap()).unwrap(); player.set_mpd(mpd);
} }
player player
}; };