Skip to content

Commit

Permalink
add initial MPRIS support using zbus
Browse files Browse the repository at this point in the history
- following the spec at https://specifications.freedesktop.org/mpris-spec/latest/

- some properties/commands are not fully supported, yet
  • Loading branch information
wisp3rwind committed Oct 27, 2024
1 parent 8093981 commit 71ad70b
Show file tree
Hide file tree
Showing 4 changed files with 1,222 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ sha1 = "0.10"
sysinfo = { version = "0.31.3", default-features = false, features = ["system"] }
thiserror = "1.0"
tokio = { version = "1.40", features = ["rt", "macros", "signal", "sync", "parking_lot", "process"] }
time = { version = "0.3", features = ["formatting"] }
url = "2.2"
zbus = { version = "4", default-features = false, features = ["tokio"], optional = true }
zvariant = { version = "4", default-features = false, optional = true }

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

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

default = ["rodio-backend", "with-libmdns"]
with-mpris = ["dep:zbus", "dep:zvariant"]

default = ["rodio-backend", "with-libmdns", "with-mpris"]

[package.metadata.deb]
maintainer = "librespot-org"
Expand Down
20 changes: 20 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ use librespot::playback::mixer::alsamixer::AlsaMixer;
mod player_event_handler;
use player_event_handler::{run_program_on_sink_events, EventHandler};

#[cfg(feature = "with-mpris")]
mod mpris_event_handler;
#[cfg(feature = "with-mpris")]
use mpris_event_handler::MprisEventHandler;

fn device_id(name: &str) -> String {
HEXLOWER.encode(&Sha1::digest(name.as_bytes()))
}
Expand Down Expand Up @@ -1937,6 +1942,14 @@ async fn main() {
}
}

#[cfg(feature = "with-mpris")]
let mpris = MprisEventHandler::spawn(player.clone())
.await
.unwrap_or_else(|e| {
error!("could not initialize MPRIS: {}", e);
exit(1);
});

loop {
tokio::select! {
credentials = async {
Expand Down Expand Up @@ -1990,6 +2003,10 @@ async fn main() {
exit(1);
}
};

#[cfg(feature = "with-mpris")]
mpris.set_spirc(spirc_.clone());

spirc = Some(spirc_);
spirc_task = Some(Box::pin(spirc_task_));

Expand Down Expand Up @@ -2035,6 +2052,9 @@ async fn main() {

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

#[cfg(feature = "with-mpris")]
shutdown_tasks.spawn(mpris.quit_and_join());

// Shutdown spirc if necessary
if let Some(spirc) = spirc {
if let Err(e) = spirc.shutdown() {
Expand Down
Loading

0 comments on commit 71ad70b

Please sign in to comment.