|
1 |
| -use std::{collections::HashMap, sync::Arc}; |
| 1 | +use std::{collections::HashMap, process, sync::Arc}; |
2 | 2 |
|
3 | 3 | use librespot_connect::Spirc;
|
4 | 4 | use log::{debug, warn};
|
@@ -838,30 +838,43 @@ pub struct MprisEventHandler {
|
838 | 838 | }
|
839 | 839 |
|
840 | 840 | impl MprisEventHandler {
|
841 |
| - pub async fn spawn(player: Arc<Player>, name: &str) -> Result<MprisEventHandler, MprisError> { |
842 |
| - let (cmd_tx, cmd_rx) = mpsc::unbounded_channel(); |
843 |
| - |
| 841 | + fn connection_builder<'a>(identity: &str, name: &str) -> zbus::Result<connection::Builder<'a>> { |
844 | 842 | let mpris_service = MprisService {
|
845 |
| - identity: name.to_string(), |
| 843 | + identity: identity.to_string(), |
846 | 844 | };
|
847 | 845 | let mpris_player_service = MprisPlayerService {
|
848 | 846 | spirc: None,
|
849 |
| - // FIXME: obtain current values from Player |
| 847 | + // Values are updated upon reception of first player state, right after MprisTask event |
| 848 | + // handler registration |
850 | 849 | repeat: LoopStatus::None,
|
851 | 850 | shuffle: false,
|
852 | 851 | playback_status: PlaybackStatus::Stopped,
|
853 | 852 | volume: 1.0,
|
854 | 853 | metadata: HashMap::new(),
|
855 | 854 | };
|
856 | 855 |
|
857 |
| - let connection = connection::Builder::session()? |
858 |
| - // FIXME: retry with "org.mpris.MediaPlayer2.librespot.instance<pid>" |
859 |
| - // on error |
860 |
| - .name("org.mpris.MediaPlayer2.librespot")? |
| 856 | + connection::Builder::session()? |
| 857 | + .name(name.to_string())? |
861 | 858 | .serve_at("/org/mpris/MediaPlayer2", mpris_service)?
|
862 |
| - .serve_at("/org/mpris/MediaPlayer2", mpris_player_service)? |
| 859 | + .serve_at("/org/mpris/MediaPlayer2", mpris_player_service) |
| 860 | + } |
| 861 | + |
| 862 | + pub async fn spawn(player: Arc<Player>, name: &str) -> Result<MprisEventHandler, MprisError> { |
| 863 | + let (cmd_tx, cmd_rx) = mpsc::unbounded_channel(); |
| 864 | + |
| 865 | + let connection = Self::connection_builder(name, "org.mpris.MediaPlayer2.librespot")? |
863 | 866 | .build()
|
864 |
| - .await?; |
| 867 | + .await; |
| 868 | + let connection = match connection { |
| 869 | + Err(zbus::Error::NameTaken) => { |
| 870 | + let pid_name = |
| 871 | + format!("org.mpris.MediaPlayer2.librespot.instance{}", process::id()); |
| 872 | + log::warn!("MPRIS: zbus name taken, trying with pid specific name: {pid_name}"); |
| 873 | + |
| 874 | + Self::connection_builder(name, &pid_name)?.build().await |
| 875 | + } |
| 876 | + _ => connection, |
| 877 | + }?; |
865 | 878 |
|
866 | 879 | let mpris_task = MprisTask {
|
867 | 880 | player,
|
|
0 commit comments