Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Add MPRIS support #1341

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
28 changes: 28 additions & 0 deletions connect/src/spirc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pub enum SpircCommand {
Repeat(bool),
Disconnect,
SetPosition(u32),
SeekOffset(i32),
SetVolume(u16),
Activate,
Load(SpircLoadCommand),
Expand Down Expand Up @@ -160,6 +161,7 @@ const CONTEXT_FETCH_THRESHOLD: u32 = 5;
const VOLUME_STEPS: i64 = 64;
const VOLUME_STEP_SIZE: u16 = 1024; // (u16::MAX + 1) / VOLUME_STEPS

#[derive(Clone)]
pub struct Spirc {
commands: mpsc::UnboundedSender<SpircCommand>,
}
Expand Down Expand Up @@ -438,6 +440,9 @@ impl Spirc {
pub fn set_position_ms(&self, position_ms: u32) -> Result<(), Error> {
Ok(self.commands.send(SpircCommand::SetPosition(position_ms))?)
}
pub fn seek_offset(&self, offset_ms: i32) -> Result<(), Error> {
Ok(self.commands.send(SpircCommand::SeekOffset(offset_ms))?)
}
pub fn disconnect(&self) -> Result<(), Error> {
Ok(self.commands.send(SpircCommand::Disconnect)?)
}
Expand Down Expand Up @@ -655,6 +660,10 @@ impl SpircTask {
self.handle_seek(position);
self.notify(None)
}
SpircCommand::SeekOffset(offset) => {
self.handle_seek_offset(offset);
self.notify(None)
}
SpircCommand::SetVolume(volume) => {
self.set_volume(volume);
self.notify(None)
Expand Down Expand Up @@ -1171,6 +1180,25 @@ impl SpircTask {
};
}

fn handle_seek_offset(&mut self, offset_ms: i32) {
let position_ms = match self.play_status {
SpircPlayStatus::Stopped => return,
SpircPlayStatus::LoadingPause { position_ms }
| SpircPlayStatus::LoadingPlay { position_ms }
| SpircPlayStatus::Paused { position_ms, .. } => position_ms,
SpircPlayStatus::Playing {
nominal_start_time, ..
} => {
let now = self.now_ms();
(now - nominal_start_time) as u32
}
};

let position_ms = ((position_ms as i32) + offset_ms).max(0) as u32;

self.handle_seek(position_ms);
}

fn consume_queued_track(&mut self) -> usize {
// Removes current track if it is queued
// Returns the index of the next track
Expand Down
4 changes: 4 additions & 0 deletions metadata/src/audio/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub enum UniqueFields {
Track {
artists: ArtistsWithRole,
album: String,
album_date: Date,
album_artists: Vec<String>,
popularity: u8,
number: u32,
Expand Down Expand Up @@ -83,6 +84,8 @@ impl AudioItem {
let uri = track_id.to_uri()?;
let album = track.album.name;

let album_date = track.album.date;

let album_artists = track
.album
.artists
Expand Down Expand Up @@ -116,6 +119,7 @@ impl AudioItem {
let unique_fields = UniqueFields::Track {
artists: track.artists_with_role,
album,
album_date,
album_artists,
popularity,
number,
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