From 5583718e7cf960426b4c780808dd80845deed98e Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Tue, 31 Oct 2023 10:40:29 -0700 Subject: [PATCH] Fix mpris Album Info (#213) --- Cargo.lock | 2 +- hifirs/src/mpris.rs | 57 +++++++++++++++++----------------- hifirs/src/player/queue/mod.rs | 10 +++++- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8e8b95b..a7460ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1562,7 +1562,7 @@ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" [[package]] name = "hifi-rs" -version = "0.3.4" +version = "0.3.5" dependencies = [ "async-broadcast 0.6.0", "async-trait", diff --git a/hifirs/src/mpris.rs b/hifirs/src/mpris.rs index 2b96ecc..7906d5b 100644 --- a/hifirs/src/mpris.rs +++ b/hifirs/src/mpris.rs @@ -19,7 +19,6 @@ pub async fn init(controls: Controls) -> Connection { let mpris_player = MprisPlayer { controls: controls.clone(), status: GstState::Null, - current_track: None, total_tracks: 0, position: ClockTime::default(), position_ts: chrono::offset::Local::now(), @@ -28,6 +27,7 @@ pub async fn init(controls: Controls) -> Connection { can_stop: true, can_next: true, can_previous: true, + track_list: TrackListValue::default(), }; let mpris_tracklist = MprisTrackList { controls, @@ -148,41 +148,37 @@ pub async fn receive_notifications(conn: Connection) { } } Notification::CurrentTrackList { list } => { - if let Some(current) = list.current_track() { - let player_ref = object_server - .interface::<_, MprisPlayer>("/org/mpris/MediaPlayer2") - .await - .expect("failed to get object server"); - - let mut player_iface = player_ref.get_mut().await; + let player_ref = object_server + .interface::<_, MprisPlayer>("/org/mpris/MediaPlayer2") + .await + .expect("failed to get object server"); - player_iface.can_previous = current.position != 0; + let list_ref = object_server + .interface::<_, MprisTrackList>("/org/mpris/MediaPlayer2") + .await + .expect("failed to get object server"); - player_iface.can_next = !(player_iface.total_tracks != 0 - && current.position == player_iface.total_tracks - 1); + let mut player_iface = player_ref.get_mut().await; + player_iface.track_list = list.clone(); - if let Some(album) = ¤t.album { - player_iface.total_tracks = album.total_tracks; - } + let mut list_iface = list_ref.get_mut().await; + list_iface.track_list = list.clone(); - player_iface.current_track = Some(current.clone()); + if let Some(album) = list.get_album() { + player_iface.total_tracks = album.total_tracks; + } - player_iface - .metadata_changed(player_ref.signal_context()) - .await - .expect("failed to signal metadata change"); + if let Some(current) = list.current_track() { + player_iface.can_previous = current.position != 0; - let list_ref = object_server - .interface::<_, MprisTrackList>("/org/mpris/MediaPlayer2") - .await - .expect("failed to get object server"); + player_iface.can_next = !(player_iface.total_tracks != 0 + && current.position == player_iface.total_tracks - 1); let tracks = list .cursive_list() .iter() .map(|t| t.0.clone()) .collect::>(); - let mut list_iface = list_ref.get_mut().await; MprisTrackList::track_list_replaced( list_ref.signal_context(), @@ -191,9 +187,12 @@ pub async fn receive_notifications(conn: Connection) { ) .await .expect("failed to send track list replaced signal"); - - list_iface.track_list = list; } + + player_iface + .metadata_changed(player_ref.signal_context()) + .await + .expect("failed to signal metadata change"); } Notification::Error { error: _ } => {} Notification::AudioQuality { @@ -244,12 +243,12 @@ impl Mpris { #[derive(Debug)] pub struct MprisPlayer { + track_list: TrackListValue, controls: Controls, status: GstState, position: ClockTime, position_ts: DateTime, total_tracks: u32, - current_track: Option, can_play: bool, can_pause: bool, can_stop: bool, @@ -305,8 +304,8 @@ impl MprisPlayer { #[dbus_interface(property, name = "Metadata")] async fn metadata(&self) -> HashMap<&'static str, zvariant::Value> { debug!("signal metadata refresh"); - if let Some(current_track) = &self.current_track { - track_to_meta(current_track, current_track.album.as_ref()) + if let Some(current_track) = &self.track_list.current_track() { + track_to_meta(current_track, self.track_list.get_album()) } else { HashMap::default() } diff --git a/hifirs/src/player/queue/mod.rs b/hifirs/src/player/queue/mod.rs index ccb2530..e273c9d 100644 --- a/hifirs/src/player/queue/mod.rs +++ b/hifirs/src/player/queue/mod.rs @@ -98,7 +98,15 @@ impl TrackListValue { #[instrument(skip(self))] pub fn get_album(&self) -> Option<&Album> { - self.album.as_ref() + if let Some(c) = self.current_track() { + if let Some(album) = &c.album { + Some(album) + } else { + self.album.as_ref() + } + } else { + self.album.as_ref() + } } #[instrument(skip(self))]