do things wiht mpris voer art

This commit is contained in:
Erica Z 2024-11-21 23:19:35 +01:00
parent 4fd1723f37
commit c827dd4863
4 changed files with 43 additions and 23 deletions

View file

@ -1,15 +1,16 @@
pub mod globals {
pub fn art_path() -> &'static std::path::Path {
static ART_PATH: std::sync::LazyLock<std::path::PathBuf> = std::sync::LazyLock::new(|| {
let xdg_dirs =
xdg::BaseDirectories::with_prefix("audrey").expect("failed to get xdg dirs");
xdg_dirs
.place_state_file("mpris-art")
.expect("failed to create mpris art state dir")
use std::path::PathBuf;
use tokio::sync::Mutex;
pub fn xdg_dirs() -> &'static xdg::BaseDirectories {
static DIRS: std::sync::LazyLock<xdg::BaseDirectories> = std::sync::LazyLock::new(|| {
xdg::BaseDirectories::with_prefix("audrey").expect("failed to get xdg dirs")
});
&ART_PATH
&DIRS
}
pub static MPRIS_ART_PATH: Mutex<Option<PathBuf>> = Mutex::const_new(None);
pub fn runtime() -> &'static tokio::runtime::Runtime {
static RUNTIME: std::sync::LazyLock<tokio::runtime::Runtime> =
std::sync::LazyLock::new(|| {

View file

@ -30,6 +30,8 @@ mod imp {
duration: Cell<i64>,
#[property(get, set)]
track: Cell<i64>,
#[property(get, set)]
cover_art: RefCell<String>,
#[property(get, set)]
stream_url: RefCell<String>,
@ -83,6 +85,7 @@ impl Song {
.property("duration", song.duration as i64)
.property("track", song.track.map(i64::from).unwrap_or(-1))
.property("stream-url", api.stream_url(&song.id).as_str())
.property("cover-art", &song.cover_art)
.build();
song

View file

@ -92,10 +92,11 @@ 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<()> {
let metadata = self.metadata();
if Some(&track_id.into()) == metadata.get("track_id") {
self.window().set_time_pos(position as f64 / MICROSECONDS);
}
//let metadata = self.metadata();
// TODO: reimplment stale seeks
//if Some(&track_id.into()) == metadata.get("track_id") {
self.window().set_time_pos(position as f64 / MICROSECONDS);
//}
Ok(())
}
@ -153,8 +154,9 @@ impl Player {
}
#[zbus(property)]
fn metadata(&self) -> HashMap<&'static str, Value<'_>> {
async fn metadata(&self) -> HashMap<&'static str, Value<'_>> {
let mut map = HashMap::new();
let mpris_art_path = audrey::globals::MPRIS_ART_PATH.lock().await;
if let Some(song) = self.window().song() {
map.insert(
@ -165,13 +167,12 @@ impl Player {
"mpris:length",
((self.window().duration() * MICROSECONDS) as i64).into(),
);
map.insert(
"mpris:artUrl",
(url::Url::from_file_path(audrey::globals::art_path())
.unwrap()
.to_string(),)
.into(),
);
if let Some(path) = mpris_art_path.as_ref() {
map.insert(
"mpris:artUrl",
(url::Url::from_file_path(path).unwrap().to_string()).into(),
);
}
map.insert("xesam:album", song.album().into());
// TODO: use the right opensubsonic data
map.insert("xesam:artist", vec![song.artist()].into());

View file

@ -733,7 +733,7 @@ mod imp {
self.mpris_player_playback_status_changed();
let window = self.obj().clone();
let song_id = window.song().unwrap().id();
let song_id = song.id();
if let Some(handle) = self
.loading_cover_handle
.replace(Some(glib::spawn_future_local(async move {
@ -754,12 +754,27 @@ mod imp {
// we consume image below into a pixbuf so we need a copy for save
let image_copy = image.clone();
let save_path = audrey::globals::xdg_dirs()
.place_state_file(format!("{}.jpg", song.cover_art()))
.expect("failed to create mpris art state dir");
let save_path_copy = save_path.clone();
let mut mpris_art_path = audrey::globals::MPRIS_ART_PATH.lock().await;
let save_future = audrey::globals::runtime().spawn_blocking(
move || -> Result<(), image::ImageError> {
let save_path = audrey::globals::art_path();
// TODO: wipe xdg state folder on init
if let Some(path) = mpris_art_path.take() {
if let Err(err) = std::fs::remove_file(&path) {
event!(
Level::ERROR,
"could not remove previous mpris art at {}: {err}",
path.display()
);
}
}
let resized =
image_copy.resize(400, 400, image::imageops::FilterType::Lanczos3);
resized.save_with_format(save_path, image::ImageFormat::Jpeg)?;
resized.save_with_format(&save_path_copy, image::ImageFormat::Jpeg)?;
*mpris_art_path = Some(save_path_copy);
Ok(())
},
);