#[cfg(all(feature = "mpris", not(freedesktop)))]
use std::sync::mpsc::Receiver;
#[cfg(feature = "mpris")]
use crate::mpris::MprisCommand;
use crate::mpris::{MprisConfig, PlayerSnapshot};
#[cfg(all(feature = "mpris", freedesktop))]
use crate::mpris::{OBJECT_PATH, track_object_path};
pub struct DesktopMediaSessions;
pub fn start_desktop_session(
config: MprisConfig,
initial: PlayerSnapshot,
) -> Option<crate::process::MediaSession> {
#[cfg(feature = "mpris")]
{
struct Worker(tokio::sync::mpsc::UnboundedSender<PlayerSnapshot>);
impl crate::process::MediaSessionPublisher for Worker {
fn publish(&mut self, snapshot: PlayerSnapshot) {
let _ = self.0.send(snapshot);
}
}
if let Some(session) = now_playing::start(&initial) {
return Some(session);
}
let (commands, publish_tx) = imp::spawn(config, initial)?;
Some(crate::process::MediaSession {
commands,
publisher: Box::new(Worker(publish_tx)),
})
}
#[cfg(not(feature = "mpris"))]
{
let _ = (config, initial);
None
}
}
#[cfg(feature = "mpris")]
mod now_playing {
use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;
use tilo_media::{NowPlaying, NowPlayingAction, NowPlayingSession, NowPlayingState};
use crate::mpris::{MprisCommand, PlaybackStatus, PlayerSnapshot};
thread_local! {
#[allow(
clippy::zero_sized_map_values,
reason = "a session holds nothing where the platform has no controls"
)]
static SESSIONS: RefCell<HashMap<u64, NowPlayingSession>> = RefCell::new(HashMap::new());
}
static NEXT: AtomicU64 = AtomicU64::new(1);
pub(super) fn start(initial: &PlayerSnapshot) -> Option<crate::process::MediaSession> {
let (tx, commands) = std::sync::mpsc::channel();
let wake = tilo_rt::wake::WakeHandle::new();
let session = NowPlayingSession::start(move |action| {
if tx.send(command(action)).is_ok() {
wake.wake();
}
})?;
let id = NEXT.fetch_add(1, Ordering::Relaxed);
SESSIONS.with(|sessions| sessions.borrow_mut().insert(id, session));
let mut publisher = Publisher { id };
crate::process::MediaSessionPublisher::publish(&mut publisher, initial.clone());
Some(crate::process::MediaSession {
commands,
publisher: Box::new(publisher),
})
}
struct Publisher {
id: u64,
}
impl crate::process::MediaSessionPublisher for Publisher {
fn publish(&mut self, snapshot: PlayerSnapshot) {
let (now, state) = describe(&snapshot);
SESSIONS.with(|sessions| {
if let Some(session) = sessions.borrow_mut().get_mut(&self.id) {
session.update(&now, &state);
}
});
}
}
impl Drop for Publisher {
fn drop(&mut self) {
let _ = SESSIONS.try_with(|sessions| sessions.borrow_mut().remove(&self.id));
}
}
pub(super) fn command(action: NowPlayingAction) -> MprisCommand {
match action {
NowPlayingAction::Play => MprisCommand::Play,
NowPlayingAction::Pause => MprisCommand::Pause,
NowPlayingAction::Stop => MprisCommand::Stop,
NowPlayingAction::Next => MprisCommand::Next,
NowPlayingAction::Previous => MprisCommand::Previous,
NowPlayingAction::SeekTo(position) => {
MprisCommand::SetPosition(i64::try_from(position.as_micros()).unwrap_or(i64::MAX))
}
}
}
pub(super) fn describe(snapshot: &PlayerSnapshot) -> (NowPlaying, NowPlayingState) {
let micros = |value: i64| Duration::from_micros(u64::try_from(value).unwrap_or(0));
(
NowPlaying {
title: snapshot.title.clone(),
artist: snapshot.artist.clone(),
album: snapshot.album.clone(),
artwork_url: (!snapshot.art_url.is_empty()).then(|| snapshot.art_url.clone()),
},
NowPlayingState {
status: match snapshot.status {
PlaybackStatus::Playing => tilo_media::PlaybackStatus::Playing,
PlaybackStatus::Paused => tilo_media::PlaybackStatus::Paused,
PlaybackStatus::Stopped => tilo_media::PlaybackStatus::Stopped,
},
position: micros(snapshot.position_us),
duration: (snapshot.length_us > 0).then(|| micros(snapshot.length_us)),
rate: 1.0,
},
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_player_reaches_the_now_playing_controls_and_their_requests_come_back() {
let snapshot = PlayerSnapshot {
status: PlaybackStatus::Playing,
title: "Aria".into(),
length_us: 180_000_000,
position_us: 3_000_000,
..PlayerSnapshot::default()
};
let (now, state) = describe(&snapshot);
assert_eq!(now.title, "Aria");
assert_eq!(state.duration, Some(Duration::from_secs(180)));
assert_eq!(state.status, tilo_media::PlaybackStatus::Playing);
assert_eq!(
command(NowPlayingAction::SeekTo(Duration::from_secs(2))),
MprisCommand::SetPosition(2_000_000)
);
let session = start(&snapshot);
assert_eq!(
session.is_some(),
cfg!(target_os = "macos"),
"macOS has a now-playing surface; elsewhere MPRIS serves"
);
if let Some(session) = session {
assert!(session.commands.try_recv().is_err());
drop(session);
}
SESSIONS.with(|sessions| assert!(sessions.borrow().is_empty()));
}
}
}
#[cfg(all(feature = "mpris", not(freedesktop)))]
mod imp {
use super::{MprisCommand, MprisConfig, PlayerSnapshot, Receiver};
use tokio::sync::mpsc::UnboundedSender;
pub(super) fn spawn(
_config: MprisConfig,
_initial: PlayerSnapshot,
) -> Option<(Receiver<MprisCommand>, UnboundedSender<PlayerSnapshot>)> {
None
}
}
#[cfg(all(feature = "mpris", freedesktop))]
mod imp {
#![allow(
clippy::used_underscore_binding,
reason = "zbus's #[interface] macro re-binds set_position's _track_id and open_uri's \
_uri by their literal names in generated dispatch code that lands in this \
module but outside the annotated impl/fn, so neither an impl- nor fn-level \
allow reaches it; the method bodies never read either parameter"
)]
use std::collections::HashMap;
use std::sync::mpsc::{Receiver, Sender};
use tokio::sync::mpsc::UnboundedSender;
use zbus::interface;
use zbus::object_server::SignalEmitter;
use zbus::zvariant::{ObjectPath, OwnedValue, Value};
use super::{MprisCommand, MprisConfig, OBJECT_PATH, PlayerSnapshot};
pub(super) fn build_metadata(snap: &PlayerSnapshot) -> HashMap<String, OwnedValue> {
let mut m: HashMap<String, OwnedValue> = HashMap::new();
let path = super::track_object_path(&snap.track_id);
if let Ok(op) = ObjectPath::try_from(path)
&& let Ok(v) = OwnedValue::try_from(Value::from(op))
{
m.insert("mpris:trackid".into(), v);
}
if snap.length_us > 0
&& let Ok(v) = OwnedValue::try_from(Value::from(snap.length_us))
{
m.insert("mpris:length".into(), v);
}
if !snap.art_url.is_empty()
&& let Ok(v) = OwnedValue::try_from(Value::from(snap.art_url.clone()))
{
m.insert("mpris:artUrl".into(), v);
}
if !snap.title.is_empty()
&& let Ok(v) = OwnedValue::try_from(Value::from(snap.title.clone()))
{
m.insert("xesam:title".into(), v);
}
if !snap.artist.is_empty() {
let artists = vec![snap.artist.clone()];
if let Ok(v) = OwnedValue::try_from(Value::from(artists)) {
m.insert("xesam:artist".into(), v);
}
}
if !snap.album.is_empty()
&& let Ok(v) = OwnedValue::try_from(Value::from(snap.album.clone()))
{
m.insert("xesam:album".into(), v);
}
m
}
pub(super) struct RootIface {
pub cmd: Sender<MprisCommand>,
pub identity: String,
pub desktop_entry: String,
pub supported_uri_schemes: Vec<String>,
pub supported_mime_types: Vec<String>,
}
impl RootIface {
pub(super) fn new(config: MprisConfig, cmd: Sender<MprisCommand>) -> Self {
Self {
cmd,
identity: config.identity,
desktop_entry: config.desktop_entry,
supported_uri_schemes: config.supported_uri_schemes,
supported_mime_types: config.supported_mime_types,
}
}
}
#[interface(name = "org.mpris.MediaPlayer2")]
#[allow(
clippy::unused_self,
reason = "the interface dispatches by exact method signature, so a constant property keeps its receiver"
)]
impl RootIface {
fn raise(&self) {
let _ = self.cmd.send(MprisCommand::Raise);
}
fn quit(&self) {
let _ = self.cmd.send(MprisCommand::Quit);
}
#[zbus(property)]
fn can_quit(&self) -> bool {
true
}
#[zbus(property)]
fn can_raise(&self) -> bool {
true
}
#[zbus(property)]
fn has_track_list(&self) -> bool {
false
}
#[zbus(property)]
fn identity(&self) -> String {
self.identity.clone()
}
#[zbus(property)]
fn desktop_entry(&self) -> String {
self.desktop_entry.clone()
}
#[zbus(property)]
fn supported_uri_schemes(&self) -> Vec<String> {
self.supported_uri_schemes.clone()
}
#[zbus(property)]
fn supported_mime_types(&self) -> Vec<String> {
self.supported_mime_types.clone()
}
}
pub(super) struct PlayerIface {
pub snap: PlayerSnapshot,
pub cmd: Sender<MprisCommand>,
}
#[interface(name = "org.mpris.MediaPlayer2.Player")]
#[allow(
clippy::unused_self,
reason = "the interface dispatches by exact method signature, so a constant property keeps its receiver"
)]
impl PlayerIface {
fn play(&self) {
let _ = self.cmd.send(MprisCommand::Play);
}
fn pause(&self) {
let _ = self.cmd.send(MprisCommand::Pause);
}
fn play_pause(&self) {
let _ = self.cmd.send(MprisCommand::PlayPause);
}
fn stop(&self) {
let _ = self.cmd.send(MprisCommand::Stop);
}
fn next(&self) {
let _ = self.cmd.send(MprisCommand::Next);
}
fn previous(&self) {
let _ = self.cmd.send(MprisCommand::Previous);
}
fn seek(&self, offset: i64) {
let _ = self.cmd.send(MprisCommand::Seek(offset));
}
fn set_position(&self, _track_id: ObjectPath<'_>, position: i64) {
let _ = self.cmd.send(MprisCommand::SetPosition(position));
}
fn open_uri(&self, _uri: String) {}
#[zbus(signal)]
async fn seeked(ctxt: &SignalEmitter<'_>, position: i64) -> zbus::Result<()>;
#[zbus(property)]
fn playback_status(&self) -> String {
self.snap.status.as_mpris().into()
}
#[zbus(property)]
fn metadata(&self) -> HashMap<String, OwnedValue> {
build_metadata(&self.snap)
}
#[zbus(property)]
fn position(&self) -> i64 {
self.snap.position_us
}
#[zbus(property)]
fn volume(&self) -> f64 {
self.snap.volume
}
#[zbus(property)]
fn set_volume(&mut self, volume: f64) {
let v = volume.clamp(0.0, 1.0);
self.snap.volume = v;
let _ = self.cmd.send(MprisCommand::SetVolume(v));
}
#[zbus(property)]
fn rate(&self) -> f64 {
1.0
}
#[zbus(property)]
#[allow(
clippy::needless_pass_by_ref_mut,
reason = "the interface dispatches by exact method signature, so a no-op setter keeps `&mut`"
)]
fn set_rate(&mut self, _rate: f64) {}
#[zbus(property)]
fn minimum_rate(&self) -> f64 {
1.0
}
#[zbus(property)]
fn maximum_rate(&self) -> f64 {
1.0
}
#[zbus(property)]
fn can_control(&self) -> bool {
true
}
#[zbus(property)]
fn can_go_next(&self) -> bool {
self.snap.can_next
}
#[zbus(property)]
fn can_go_previous(&self) -> bool {
self.snap.can_prev
}
#[zbus(property)]
fn can_play(&self) -> bool {
self.snap.can_play
}
#[zbus(property)]
fn can_pause(&self) -> bool {
self.snap.can_pause
}
#[zbus(property)]
fn can_seek(&self) -> bool {
self.snap.can_seek
}
}
#[allow(
clippy::significant_drop_tightening,
reason = "`guard` must stay held across every `_changed` emission below: \
they all report state relative to the one `new` just written, \
and releasing the lock between signals would let a concurrent \
update interleave, leaving MPRIS clients briefly observing an \
inconsistent mix of old and new properties"
)]
pub(super) async fn apply_update(
connection: &zbus::Connection,
old: &PlayerSnapshot,
new: &PlayerSnapshot,
) -> zbus::Result<()> {
let iref = connection
.object_server()
.interface::<_, PlayerIface>(OBJECT_PATH)
.await?;
let ctxt = iref.signal_emitter().clone();
{
let mut guard = iref.get_mut().await;
guard.snap = new.clone();
if old.status != new.status {
guard.playback_status_changed(&ctxt).await?;
}
if old.metadata_differs(new) {
guard.metadata_changed(&ctxt).await?;
}
if (old.volume - new.volume).abs() > f64::EPSILON {
guard.volume_changed(&ctxt).await?;
}
if old.can_next != new.can_next {
guard.can_go_next_changed(&ctxt).await?;
}
if old.can_prev != new.can_prev {
guard.can_go_previous_changed(&ctxt).await?;
}
if old.can_play != new.can_play {
guard.can_play_changed(&ctxt).await?;
}
if old.can_pause != new.can_pause {
guard.can_pause_changed(&ctxt).await?;
}
if old.can_seek != new.can_seek {
guard.can_seek_changed(&ctxt).await?;
}
}
if old.track_id == new.track_id
&& new.position_us.saturating_sub(old.position_us).abs() > 2_000_000
{
let _ = PlayerIface::seeked(&ctxt, new.position_us).await;
}
Ok(())
}
pub(super) fn spawn(
config: MprisConfig,
initial: PlayerSnapshot,
) -> Option<(Receiver<MprisCommand>, UnboundedSender<PlayerSnapshot>)> {
let runtime = tilo_rt::try_app().ok()?;
let (cmd_tx, cmd_rx) = std::sync::mpsc::channel::<MprisCommand>();
let (pub_tx, mut pub_rx) = tokio::sync::mpsc::unbounded_channel::<PlayerSnapshot>();
runtime.spawn(async move {
let bus_name = config.bus_name.clone();
let root = RootIface::new(config, cmd_tx.clone());
let player = PlayerIface {
snap: initial.clone(),
cmd: cmd_tx,
};
let connected = async {
zbus::connection::Builder::session()?
.serve_at(OBJECT_PATH, player)?
.serve_at(OBJECT_PATH, root)?
.name(bus_name.clone())?
.build()
.await
};
let connection = match connected.await {
Ok(connection) => connection,
Err(error) => {
tracing::warn!(
%error,
%bus_name,
"MPRIS: could not register the player; media controls are off"
);
return;
}
};
let mut last = initial;
while let Some(snap) = pub_rx.recv().await {
if apply_update(&connection, &last, &snap).await.is_ok() {
last = snap;
}
}
});
Some((cmd_rx, pub_tx))
}
}
#[cfg(test)]
#[cfg(feature = "mpris")]
mod tests {
use super::*;
#[cfg(freedesktop)]
use crate::mpris::PlaybackStatus;
#[cfg(all(feature = "mpris", not(freedesktop)))]
#[test]
fn unsupported_platform_starts_no_session() {
assert!(
start_desktop_session(
MprisConfig::new("tilo-player", "Tilo Player"),
PlayerSnapshot::default(),
)
.is_none()
);
}
#[cfg(all(feature = "mpris", freedesktop))]
#[tokio::test]
async fn mpris_interface_responds_and_reflects_state() {
use std::sync::mpsc::channel;
use tokio::net::UnixStream;
use zbus::connection::Builder;
const GUID: &str = "0123456789abcdef0123456789abcdef";
let (cmd_tx, cmd_rx) = channel::<MprisCommand>();
let cmd_rx = std::sync::Mutex::new(cmd_rx);
let snap = PlayerSnapshot {
status: PlaybackStatus::Playing,
track_id: "yt:abc123".into(),
title: "Runaway".into(),
artist: "Aurora".into(),
length_us: 240_000_000,
can_next: true,
can_pause: true,
..PlayerSnapshot::default()
};
let (server_sock, client_sock) = UnixStream::pair().expect("socket pair");
let player = imp::PlayerIface {
snap: snap.clone(),
cmd: cmd_tx.clone(),
};
let mut config = MprisConfig::new("tilo-player", "Tilo Player");
config.supported_uri_schemes = vec!["http".into(), "https".into()];
config.supported_mime_types = vec!["audio/ogg".into(), "audio/mpeg".into()];
let root = imp::RootIface::new(config, cmd_tx.clone());
let server_builder = Builder::unix_stream(server_sock)
.server(GUID)
.expect("guid")
.p2p()
.serve_at(OBJECT_PATH, player)
.expect("serve player")
.serve_at(OBJECT_PATH, root)
.expect("serve root");
let client_builder = Builder::unix_stream(client_sock).p2p();
let (server_conn, client_conn) =
tokio::join!(server_builder.build(), client_builder.build());
let server_conn = server_conn.expect("server conn");
let client_conn = client_conn.expect("client conn");
let root_proxy = zbus::Proxy::new(
&client_conn,
"org.mpris.MediaPlayer2.tilo-player",
OBJECT_PATH,
"org.mpris.MediaPlayer2",
)
.await
.expect("root proxy");
assert_eq!(
root_proxy
.get_property::<Vec<String>>("SupportedUriSchemes")
.await
.expect("URI schemes"),
["http", "https"],
);
assert_eq!(
root_proxy
.get_property::<Vec<String>>("SupportedMimeTypes")
.await
.expect("MIME types"),
["audio/ogg", "audio/mpeg"],
);
let player_proxy: zbus::Proxy = zbus::proxy::Builder::new(&client_conn)
.destination("org.mpris.MediaPlayer2.tilo-player")
.expect("proxy destination")
.path(OBJECT_PATH)
.expect("proxy path")
.interface("org.mpris.MediaPlayer2.Player")
.expect("proxy interface")
.cache_properties(zbus::proxy::CacheProperties::No)
.build()
.await
.expect("player proxy");
assert_served_state_matches(&player_proxy).await;
assert_commands_forwarded(&player_proxy, &cmd_rx).await;
assert_update_reflected(&server_conn, &player_proxy, &snap).await;
drop(client_conn);
drop(server_conn);
}
#[cfg(all(feature = "mpris", freedesktop))]
async fn assert_served_state_matches(player_proxy: &zbus::Proxy<'_>) {
let status: String = player_proxy
.get_property("PlaybackStatus")
.await
.expect("read PlaybackStatus");
assert_eq!(status, "Playing", "served status reflected over D-Bus");
let metadata: std::collections::HashMap<String, zbus::zvariant::OwnedValue> = player_proxy
.get_property("Metadata")
.await
.expect("read Metadata");
let title: String = metadata
.get("xesam:title")
.and_then(|v| v.downcast_ref::<&str>().ok().map(str::to_string))
.or_else(|| {
metadata
.get("xesam:title")
.and_then(|v| String::try_from(v.try_clone().unwrap()).ok())
})
.expect("title in metadata");
assert_eq!(title, "Runaway", "served now-playing reflected over D-Bus");
}
#[cfg(all(feature = "mpris", freedesktop))]
async fn assert_commands_forwarded(
player_proxy: &zbus::Proxy<'_>,
cmd_rx: &std::sync::Mutex<std::sync::mpsc::Receiver<MprisCommand>>,
) {
player_proxy
.call_method("Pause", &())
.await
.expect("Pause call");
player_proxy
.call_method("Next", &())
.await
.expect("Next call");
player_proxy
.call_method("Play", &())
.await
.expect("Play call");
let mut got = Vec::new();
for _ in 0..3 {
got.push(
cmd_rx
.lock()
.expect("test-local mutex is never poisoned")
.recv()
.expect("command delivered"),
);
}
assert_eq!(
got,
vec![MprisCommand::Pause, MprisCommand::Next, MprisCommand::Play],
"external control reaches the widget as commands"
);
}
#[cfg(all(feature = "mpris", freedesktop))]
async fn assert_update_reflected(
server_conn: &zbus::Connection,
player_proxy: &zbus::Proxy<'_>,
snap: &PlayerSnapshot,
) {
let mut updated = snap.clone();
updated.status = PlaybackStatus::Paused;
imp::apply_update(server_conn, snap, &updated)
.await
.expect("apply update");
let status: String = player_proxy
.get_property("PlaybackStatus")
.await
.expect("re-read PlaybackStatus");
assert_eq!(status, "Paused", "a Pause reflects on PlaybackStatus");
}
}