Skip to content

Commit

Permalink
src: Add eof-reached to detect stopped playback
Browse files Browse the repository at this point in the history
  • Loading branch information
eNV25 committed Aug 25, 2023
1 parent fdd5a56 commit b5b9d20
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 36 deletions.
3 changes: 1 addition & 2 deletions src/mpris/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ impl PlayerProxy {
fn playback_status(&self) -> &str {
if mpv::get_property_bool!(self.ctx(), "idle-active\0")
|| (mpv::get_property_string!(self.ctx(), "keep-open\0") != "no"
&& (mpv::get_property_int!(self.ctx(), "percent-pos\0") == 100
|| mpv::get_property_int!(self.ctx(), "duration\0") == 0))
&& mpv::get_property_bool!(self.ctx(), "eof-reached\0"))
{
"Stopped"
} else if mpv::get_property_bool!(self.ctx(), "pause\0") {
Expand Down
55 changes: 21 additions & 34 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,18 @@ pub extern "C" fn mpv_open_cplugin(ctx: *mut mpv_handle) -> c_int {
})
.expect("dbus session connection and server setup");

let root_ref = smol::block_on(
let root = smol::block_on(
connection
.object_server()
.interface::<_, mpris::RootProxy>("/org/mpris/MediaPlayer2"),
)
.expect("MediaPlayer2 interface reference");
let root_sigctxt = root_ref.signal_context();

let player_ref = smol::block_on(
let player = smol::block_on(
connection
.object_server()
.interface::<_, mpris::PlayerProxy>("/org/mpris/MediaPlayer2"),
)
.expect("MediaPlayer2.Player interface reference");
let player_sigctxt = player_ref.signal_context();

// These properties and those handled in the main loop
// must be kept in sync with the implementations in the
Expand All @@ -52,6 +49,7 @@ pub extern "C" fn mpv_open_cplugin(ctx: *mut mpv_handle) -> c_int {
ctx,
"seekable\0",
"idle-active\0",
"eof-reached\0",
"pause\0",
"loop-file\0",
"loop-playlist\0",
Expand All @@ -76,7 +74,7 @@ pub extern "C" fn mpv_open_cplugin(ctx: *mut mpv_handle) -> c_int {
MPV_EVENT_PLAYBACK_RESTART if mp_seeking => smol::block_on(async {
mp_seeking = false;
_ = mpris::PlayerProxy::seeked(
player_ref.signal_context(),
player.signal_context(),
(mpv::get_property_float!(ctx, "playback-time\0") * 1E6) as i64,
)
.await;
Expand All @@ -90,35 +88,24 @@ pub extern "C" fn mpv_open_cplugin(ctx: *mut mpv_handle) -> c_int {
let prop = unsafe { CStr::from_ptr(data.name) }
.to_str()
.unwrap_or_default();
let root = root_ref.get().await;
let player = player_ref.get().await;
match prop {
"seekable" => {
_ = player.can_seek_changed(player_sigctxt).await;
}
"idle-active" | "pause" => {
_ = player.playback_status_changed(player_sigctxt).await;
}
"loop-file" | "loop-playlist" => {
_ = player.loop_status_changed(player_sigctxt).await;
}
"speed" => {
_ = player.rate_changed(player_sigctxt).await;
}
"shuffle" => {
_ = player.shuffle_changed(player_sigctxt).await;
}
"metadata" => {
_ = player.metadata_changed(player_sigctxt).await;
}
"volume" => {
_ = player.volume_changed(player_sigctxt).await;
}
"fullscreen" => {
_ = root.fullscreen_changed(root_sigctxt).await;
}
_ => {}
macro_rules! changed {
($iface:expr, $method:ident) => {
$iface.get().await.$method($iface.signal_context()).await
};
}
_ = match prop {
"seekable" => changed!(player, can_seek_changed),
"idle-active" | "eof-reached" | "pause" => {
changed!(player, playback_status_changed)
}
"loop-file" | "loop-playlist" => changed!(player, loop_status_changed),
"speed" => changed!(player, rate_changed),
"shuffle" => changed!(player, shuffle_changed),
"metadata" => changed!(player, metadata_changed),
"volume" => changed!(player, volume_changed),
"fullscreen" => changed!(root, fullscreen_changed),
_ => Ok(()),
};
}),
_ => {}
}
Expand Down

0 comments on commit b5b9d20

Please sign in to comment.