Skip to content

Commit 71ad70b

Browse files
committed
add initial MPRIS support using zbus
- following the spec at https://specifications.freedesktop.org/mpris-spec/latest/ - some properties/commands are not fully supported, yet
1 parent 8093981 commit 71ad70b

4 files changed

Lines changed: 1222 additions & 1 deletion

File tree

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ sha1 = "0.10"
6464
sysinfo = { version = "0.31.3", default-features = false, features = ["system"] }
6565
thiserror = "1.0"
6666
tokio = { version = "1.40", features = ["rt", "macros", "signal", "sync", "parking_lot", "process"] }
67+
time = { version = "0.3", features = ["formatting"] }
6768
url = "2.2"
69+
zbus = { version = "4", default-features = false, features = ["tokio"], optional = true }
70+
zvariant = { version = "4", default-features = false, optional = true }
6871

6972
[features]
7073
alsa-backend = ["librespot-playback/alsa-backend"]
@@ -82,7 +85,9 @@ with-libmdns = ["librespot-discovery/with-libmdns"]
8285

8386
passthrough-decoder = ["librespot-playback/passthrough-decoder"]
8487

85-
default = ["rodio-backend", "with-libmdns"]
88+
with-mpris = ["dep:zbus", "dep:zvariant"]
89+
90+
default = ["rodio-backend", "with-libmdns", "with-mpris"]
8691

8792
[package.metadata.deb]
8893
maintainer = "librespot-org"

src/main.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ use librespot::playback::mixer::alsamixer::AlsaMixer;
4040
mod player_event_handler;
4141
use player_event_handler::{run_program_on_sink_events, EventHandler};
4242

43+
#[cfg(feature = "with-mpris")]
44+
mod mpris_event_handler;
45+
#[cfg(feature = "with-mpris")]
46+
use mpris_event_handler::MprisEventHandler;
47+
4348
fn device_id(name: &str) -> String {
4449
HEXLOWER.encode(&Sha1::digest(name.as_bytes()))
4550
}
@@ -1937,6 +1942,14 @@ async fn main() {
19371942
}
19381943
}
19391944

1945+
#[cfg(feature = "with-mpris")]
1946+
let mpris = MprisEventHandler::spawn(player.clone())
1947+
.await
1948+
.unwrap_or_else(|e| {
1949+
error!("could not initialize MPRIS: {}", e);
1950+
exit(1);
1951+
});
1952+
19401953
loop {
19411954
tokio::select! {
19421955
credentials = async {
@@ -1990,6 +2003,10 @@ async fn main() {
19902003
exit(1);
19912004
}
19922005
};
2006+
2007+
#[cfg(feature = "with-mpris")]
2008+
mpris.set_spirc(spirc_.clone());
2009+
19932010
spirc = Some(spirc_);
19942011
spirc_task = Some(Box::pin(spirc_task_));
19952012

@@ -2035,6 +2052,9 @@ async fn main() {
20352052

20362053
let mut shutdown_tasks = tokio::task::JoinSet::new();
20372054

2055+
#[cfg(feature = "with-mpris")]
2056+
shutdown_tasks.spawn(mpris.quit_and_join());
2057+
20382058
// Shutdown spirc if necessary
20392059
if let Some(spirc) = spirc {
20402060
if let Err(e) = spirc.shutdown() {

0 commit comments

Comments
 (0)