Skip to content

Commit

Permalink
Added some time management. It not perfect. At all.
Browse files Browse the repository at this point in the history
  • Loading branch information
morosanmihail committed Jan 2, 2025
1 parent 547d77c commit d62fe04
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
14 changes: 7 additions & 7 deletions src/homeassistant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,13 @@ pub async fn listen_for_events(
Some((entity_id, msg)) = mpris_rx.recv() => {
let media = media_players.get_mut(&entity_id);
if let Some(mp) = media {
match msg {
HAEvent::Play=> mp.play().await?,
HAEvent::Pause=> mp.pause().await?,
HAEvent::MetadataUpdated(_)=>todo!(),
HAEvent::Next => mp.next().await?,
HAEvent::Previous => mp.previous().await?,
};
match msg {
HAEvent::Play=> mp.play().await?,
HAEvent::Pause=> mp.pause().await?,
HAEvent::MetadataUpdated(_)=>todo!(),
HAEvent::Next => mp.next().await?,
HAEvent::Previous => mp.previous().await?,
};
}
}
}
Expand Down
54 changes: 35 additions & 19 deletions src/mpris.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
use std::sync::Arc;

use eyre::OptionExt;
use mpris_server::{
zbus::fdo, LoopStatus, Metadata, PlaybackRate, PlaybackStatus, PlayerInterface, Property,
RootInterface, Server, Time, TrackId, Volume,
};
use tokio::sync::mpsc::{Receiver, Sender};
use serde_json::json;
use tokio::sync::{
mpsc::{Receiver, Sender},
Mutex,
};
use url::Url;

use crate::homeassistant::{HAEvent, MediaPlayer};
Expand All @@ -13,6 +20,7 @@ pub struct MyPlayer {
entity_id: String,
ha_sender: tokio::sync::mpsc::Sender<(String, HAEvent)>,
pub start_state: MediaPlayer,
position: Arc<Mutex<i64>>,
}

impl RootInterface for MyPlayer {
Expand Down Expand Up @@ -53,15 +61,15 @@ impl RootInterface for MyPlayer {
}

async fn desktop_entry(&self) -> fdo::Result<String> {
Ok("AAAAH".to_string())
Ok("HomeAssistantPlayer".to_string())
}

async fn supported_uri_schemes(&self) -> fdo::Result<Vec<String>> {
Ok(vec!["uh".to_string()])
Ok(vec![])
}

async fn supported_mime_types(&self) -> fdo::Result<Vec<String>> {
Ok(vec!["uh".to_string()])
Ok(vec![])
}
}

Expand Down Expand Up @@ -139,7 +147,7 @@ impl PlayerInterface for MyPlayer {
}

async fn rate(&self) -> fdo::Result<PlaybackRate> {
Ok(PlaybackRate::NEG_INFINITY)
Ok(1.0)
}

async fn set_rate(&self, _rate: PlaybackRate) -> mpris_server::zbus::Result<()> {
Expand Down Expand Up @@ -203,15 +211,15 @@ impl PlayerInterface for MyPlayer {
}

async fn position(&self) -> fdo::Result<Time> {
Ok(Time::ZERO)
Ok(Time::from_secs(*self.position.lock().await))
}

async fn minimum_rate(&self) -> fdo::Result<PlaybackRate> {
Ok(PlaybackRate::MIN_POSITIVE)
Ok(1.0)
}

async fn maximum_rate(&self) -> fdo::Result<PlaybackRate> {
Ok(PlaybackRate::MIN_POSITIVE)
Ok(1.0)
}

async fn can_go_next(&self) -> fdo::Result<bool> {
Expand Down Expand Up @@ -246,16 +254,21 @@ pub async fn new_mpris_player(
mut rx: Receiver<HAEvent>,
ha_sender: Sender<(String, HAEvent)>,
) -> eyre::Result<()> {
let player = Server::new(
&entity_id.clone(),
MyPlayer {
base_url: base_url.clone(),
entity_id,
start_state,
ha_sender,
},
)
.await?;
let duration = start_state
.attributes
.get("media_position")
.unwrap_or(&json!(0))
.as_i64()
.ok_or_eyre("Could not convert Number to i64")?;
let position_lock = Arc::new(Mutex::new(duration));
let media_player = MyPlayer {
base_url: base_url.clone(),
entity_id: entity_id.clone(),
start_state,
ha_sender,
position: position_lock.clone(),
};
let player = Server::new(&entity_id.clone(), media_player).await?;

loop {
if let Some(i) = rx.recv().await {
Expand Down Expand Up @@ -290,7 +303,10 @@ pub async fn new_mpris_player(
),
])
.await?;

{
let mut pos = position_lock.lock().await;
*pos = position;
}
player
.emit(mpris_server::Signal::Seeked {
position: Time::from_secs(position),
Expand Down

0 comments on commit d62fe04

Please sign in to comment.