From 378eb0761d733dcad8afc234ed7838175b2163ea Mon Sep 17 00:00:00 2001 From: Erica Z Date: Tue, 19 Nov 2024 21:07:05 +0100 Subject: [PATCH] mpris player metadata --- src/mpris/player.rs | 12 ++++++------ src/ui/window.rs | 26 ++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/mpris/player.rs b/src/mpris/player.rs index 9488cc2..c654cc9 100644 --- a/src/mpris/player.rs +++ b/src/mpris/player.rs @@ -9,7 +9,7 @@ use zbus::zvariant::{ObjectPath, OwnedObjectPath, OwnedValue, Value}; const MICROSECONDS: f64 = 1e6; // in a second #[derive(Default)] -struct MetadataMap { +pub struct MetadataMap { // mpris track_id: Option, length: Option, @@ -37,7 +37,7 @@ struct MetadataMap { } impl MetadataMap { - fn from_playbin_song(song: Option<&Song>) -> Self { + pub fn from_playbin_song(song: Option<&Song>) -> Self { song.map(|song| MetadataMap { // use a unique growing counter to identify tracks track_id: Some({ @@ -115,7 +115,6 @@ impl MetadataMap { } pub struct Player { - metadata: MetadataMap, window: SendWeakRef, } @@ -125,7 +124,6 @@ impl Player { playbin: &Window, ) -> Result, zbus::Error> { let player = Self { - metadata: MetadataMap::from_playbin_song(None), window: playbin.downgrade().into(), }; @@ -268,7 +266,8 @@ impl Player { #[tracing::instrument(skip(self), parent = None, target = "audrey::mpris", level = Level::DEBUG, ret)] fn set_position(&self, track_id: ObjectPath<'_>, position: i64) -> zbus::fdo::Result<()> { - if Some(track_id) == self.metadata.track_id.as_ref().map(|x| x.as_ref()) { + let metadata = self.metadata(); + if Some(&track_id.into()) == metadata.get("track_id") { self.window().set_time_pos(position as f64 / MICROSECONDS); } Ok(()) @@ -329,7 +328,8 @@ impl Player { #[zbus(property)] fn metadata(&self) -> HashMap<&'static str, OwnedValue> { - self.metadata.as_hash_map() + // TODO: no need to MetadataMap wrapper anymore + MetadataMap::from_playbin_song(self.window().song().as_ref()).as_hash_map() } #[zbus(property)] diff --git a/src/ui/window.rs b/src/ui/window.rs index 6be0249..875bb7a 100644 --- a/src/ui/window.rs +++ b/src/ui/window.rs @@ -416,7 +416,10 @@ mod imp { if let Some(iface_ref) = self.mpris_player() { glib::spawn_future_local(async move { let iface = iface_ref.get().await; - match iface.playback_status_changed(iface_ref.signal_emitter()).await { + match iface + .playback_status_changed(iface_ref.signal_emitter()) + .await + { Ok(()) => {} Err(err) => event!( Level::ERROR, @@ -427,6 +430,21 @@ mod imp { } } + fn mpris_player_metadata_changed(&self) { + if let Some(iface_ref) = self.mpris_player() { + glib::spawn_future_local(async move { + let iface = iface_ref.get().await; + match iface.metadata_changed(iface_ref.signal_emitter()).await { + Ok(()) => {} + Err(err) => event!( + Level::ERROR, + "could not notify new metadata through mpris interface: {err}" + ), + } + }); + } + } + fn volume(&self) -> i64 { self.mpv.get_property("volume").unwrap() } @@ -663,6 +681,7 @@ mod imp { self.obj().set_playing_cover_art(None::); self.obj().set_background(None::); + self.mpris_player_metadata_changed(); self.mpris_player_playback_status_changed(); } @@ -686,11 +705,14 @@ mod imp { self.obj().notify("song"); self.buffering_start(); - let duration = self.obj().song().unwrap().duration() as f64; + let song = self.obj().song().unwrap(); + + let duration = song.duration() as f64; self.duration.set(duration); event!(target: "audrey::playback", Level::DEBUG, "duration is now {duration} (from subsonic)"); self.obj().notify("duration"); + self.mpris_player_metadata_changed(); self.mpris_player_playback_status_changed(); let window = self.obj().clone();