playdoobs 0.1.0

Service and matching CLI application for controlling the currently active MPRIS2 player
Documentation
use doobs_mpris::binding::PlayerProvider;
use doobs_mpris::types::{Metadata, MprisDuration, PlaybackStatus, TrackId};
use log::error;
use tokio::sync::oneshot;

use crate::daemon::{ActivityLock, PlayerCommand};
use crate::no_track_metadata;

pub struct PlaydoobsdPlayerProvider {
    pub(crate) activity: ActivityLock,
    pub(crate) can_seek: bool,
    pub(crate) metadata: Metadata,
    pub(crate) playback_status: PlaybackStatus,
}

impl PlaydoobsdPlayerProvider {
    pub(crate) fn new(activity: ActivityLock) -> Self {
        Self {
            activity,
            can_seek: false,
            metadata: no_track_metadata(),
            playback_status: PlaybackStatus::Stopped,
        }
    }

    pub(crate) fn reset_properties(&mut self) {
        *self = Self::new(self.activity.clone());
    }

    fn send_command(&self, cmd: PlayerCommand) -> zbus::fdo::Result<()> {
        let cmd_name = cmd.to_string();
        self.activity
            .read_arc()
            .send_command_to_active(cmd)
            .map_err(|err| {
                let msg = format!("Could not call {cmd_name} on player");
                error!("{msg}: {err}");
                zbus::fdo::Error::Failed(msg)
            })
    }
}

impl PlayerProvider for PlaydoobsdPlayerProvider {
    // all Can* properties are true for now, so we don't have to watch them on the active player
    // if we were to watch them we'd have to make sure that we return an error when trying to Play
    // while CanPlay is false

    /// CanControl property
    async fn can_control(&self) -> zbus::fdo::Result<bool> {
        // This property is not expected to change, as it describes an intrinsic capability of the implementation.
        Ok(true)
    }

    /// CanGoNext property
    async fn can_go_next(&self) -> zbus::fdo::Result<bool> {
        Ok(true)
    }

    /// CanGoPrevious property
    async fn can_go_previous(&self) -> zbus::fdo::Result<bool> {
        Ok(true)
    }

    /// CanPause property
    async fn can_pause(&self) -> zbus::fdo::Result<bool> {
        Ok(true)
    }

    /// CanPlay property
    async fn can_play(&self) -> zbus::fdo::Result<bool> {
        Ok(true)
    }

    /// CanSeek property
    async fn can_seek(&self) -> zbus::fdo::Result<bool> {
        Ok(self.can_seek)
    }

    /// MaximumRate property
    async fn maximum_rate(&self) -> zbus::fdo::Result<f64> {
        Ok(1.0)
    }

    /// Metadata property
    async fn metadata(&self) -> zbus::fdo::Result<Metadata> {
        Ok(self.metadata.clone())
    }

    /// MinimumRate property
    async fn minimum_rate(&self) -> zbus::fdo::Result<f64> {
        // see Rate
        Ok(1.0)
    }

    /// Next method
    async fn next(&self) -> zbus::fdo::Result<()> {
        self.send_command(PlayerCommand::Next)
    }

    /// Pause method
    async fn pause(&self) -> zbus::fdo::Result<()> {
        self.send_command(PlayerCommand::Pause)
    }

    /// Play method
    async fn play(&self) -> zbus::fdo::Result<()> {
        self.send_command(PlayerCommand::Play)
    }

    /// PlayPause method
    async fn play_pause(&self) -> zbus::fdo::Result<()> {
        self.send_command(PlayerCommand::PlayPause)
    }

    /// PlaybackStatus property
    async fn playback_status(&self) -> zbus::fdo::Result<PlaybackStatus> {
        Ok(self.playback_status)
    }

    /// Position property
    async fn position(&self) -> zbus::fdo::Result<MprisDuration> {
        let (tx, rx) = oneshot::channel::<MprisDuration>();
        self.send_command(PlayerCommand::FetchPosition(tx))?;

        rx.await
            .map_err(|_| zbus::fdo::Error::Failed("Could not fetch position".to_string()))
    }

    /// Previous method
    async fn previous(&self) -> zbus::fdo::Result<()> {
        self.send_command(PlayerCommand::Previous)
    }

    /// Rate property
    async fn rate(&self) -> zbus::fdo::Result<f64> {
        // If the media player has no ability to play at speeds other than the normal playback rate,
        // this must still be implemented, and must return 1.0.
        // The MinimumRate and MaximumRate properties must also be set to 1.0.

        // since we don't watch the property on the player, return 1.0 for now
        Ok(1.0)
    }
    async fn set_rate(&self, _value: f64) -> zbus::Result<()> {
        Err(zbus::Error::Unsupported)
    }

    /// Seek method
    async fn seek(&self, offset: MprisDuration) -> zbus::fdo::Result<()> {
        if self.can_seek {
            self.send_command(PlayerCommand::Seek { offset })
        } else {
            // If the CanSeek property is false, this has no effect.
            Ok(())
        }
    }

    /// SetPosition method
    async fn set_position(
        &self,
        track_id: TrackId,
        position: MprisDuration,
    ) -> zbus::fdo::Result<()> {
        if self.can_seek
            && !position.is_negative()
            && let Some(length) = self.metadata.length()
            && *position <= length
        {
            self.send_command(PlayerCommand::SetPosition { track_id, position })
        } else {
            // If the Position argument is less than 0, do nothing.
            // If the Position argument is greater than the track length, do nothing.
            // If the CanSeek property is false, this has no effect.
            Ok(())
        }
    }

    /// Stop method
    async fn stop(&self) -> zbus::fdo::Result<()> {
        self.send_command(PlayerCommand::Stop)
    }
}