From e13b8b882711da805a9463a320841943747b1221 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Sun, 30 Sep 2018 06:42:53 +0300 Subject: [PATCH 1/3] Player: Tweak the smart rewind behavior Check if time interval passed since the last pause, and only rewind if the delta to indicates that the user had switched their focus. In other words, avoid rewinding if the track was just paused and resumed. --- podcasts-gtk/src/widgets/player.rs | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/podcasts-gtk/src/widgets/player.rs b/podcasts-gtk/src/widgets/player.rs index e2d994f..9d1c5e9 100644 --- a/podcasts-gtk/src/widgets/player.rs +++ b/podcasts-gtk/src/widgets/player.rs @@ -8,7 +8,7 @@ use gtk::prelude::*; use gio::{File, FileExt}; use glib::{SignalHandlerId, WeakRef}; -use chrono::NaiveTime; +use chrono::{prelude::*, NaiveTime}; use crossbeam_channel::Sender; use failure::Error; use fragile::Fragile; @@ -22,6 +22,7 @@ use utils::set_image_from_path; use std::ops::Deref; use std::path::Path; use std::rc::Rc; +use std::cell::RefCell; use i18n::i18n; @@ -169,6 +170,7 @@ struct PlayerControls { pause: gtk::Button, forward: gtk::Button, rewind: gtk::Button, + last_pause: RefCell>>, } #[derive(Debug, Clone)] @@ -219,6 +221,7 @@ impl Default for PlayerWidget { pause, forward, rewind, + last_pause: RefCell::new(None), }; let timer_container = builder.get_object("timer").unwrap(); @@ -457,6 +460,23 @@ impl PlayerWidget { player.seek(ClockTime::from_seconds(value)); }) } + + fn smart_rewind(&self) -> Option<()> { + let now = Local::now(); + let last: &Option> = &*self.controls.last_pause.borrow(); + let last = last.clone()?; + let delta = (now - last).num_seconds(); + + // Only rewind on pause if the stream position is passed a certain point, + // And the player has been paused for more than a minute. + let seconds_passed = self.player.get_position().seconds()?; + // FIXME: also check episode id + if seconds_passed >= 90 && delta >= 60 { + self.seek(ClockTime::from_seconds(5), SeekDirection::Backwards); + } + + Some(()) + } } impl PlayerExt for PlayerWidget { @@ -466,6 +486,7 @@ impl PlayerExt for PlayerWidget { self.controls.pause.show(); self.controls.play.hide(); + self.smart_rewind(); self.player.play(); self.info.mpris.set_playback_status(PlaybackStatus::Playing); } @@ -477,12 +498,7 @@ impl PlayerExt for PlayerWidget { self.player.pause(); self.info.mpris.set_playback_status(PlaybackStatus::Paused); - // Only rewind on pause if the stream position is passed a certain point. - if let Some(sec) = self.player.get_position().seconds() { - if sec >= 90 { - self.seek(ClockTime::from_seconds(5), SeekDirection::Backwards); - } - } + self.controls.last_pause.replace(Some(Local::now())); } #[cfg_attr(rustfmt, rustfmt_skip)] From ef2940142caf1e8cb3c5cc7f51dd747d455f3f00 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Thu, 4 Oct 2018 05:41:51 +0300 Subject: [PATCH 2/3] PlayerInfo: Store the id of the current playing episode --- podcasts-gtk/src/widgets/player.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/podcasts-gtk/src/widgets/player.rs b/podcasts-gtk/src/widgets/player.rs index 9d1c5e9..4d8d9c8 100644 --- a/podcasts-gtk/src/widgets/player.rs +++ b/podcasts-gtk/src/widgets/player.rs @@ -52,11 +52,13 @@ struct PlayerInfo { episode: gtk::Label, cover: gtk::Image, mpris: Arc, + episode_id: RefCell>, } impl PlayerInfo { // FIXME: create a Diesel Model of the joined episode and podcast query instead fn init(&self, episode: &EpisodeWidgetModel, podcast: &ShowCoverModel) { + self.episode_id.replace(Some(episode.rowid())); self.set_cover_image(podcast); self.set_show_title(podcast); self.set_episode_title(episode); @@ -251,6 +253,7 @@ impl Default for PlayerWidget { show, episode, cover, + episode_id: RefCell::new(None), }; let radio150 = builder.get_object("rate_1_50").unwrap(); From cd2b087006cf6f529fb95aafc10f2d1632cf5955 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Thu, 4 Oct 2018 06:23:59 +0300 Subject: [PATCH 3/3] Player: Check the episode id before triggering a smart rewind --- podcasts-gtk/src/widgets/player.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/podcasts-gtk/src/widgets/player.rs b/podcasts-gtk/src/widgets/player.rs index 4d8d9c8..dba3232 100644 --- a/podcasts-gtk/src/widgets/player.rs +++ b/podcasts-gtk/src/widgets/player.rs @@ -23,6 +23,7 @@ use std::ops::Deref; use std::path::Path; use std::rc::Rc; use std::cell::RefCell; +use std::sync::Mutex; use i18n::i18n; @@ -465,19 +466,32 @@ impl PlayerWidget { } fn smart_rewind(&self) -> Option<()> { + lazy_static! { + static ref LAST_KNOWN_EPISODE: Mutex> = Mutex::new(None); + }; + + // Figure out the time delta, in seconds, between the last pause and now let now = Local::now(); let last: &Option> = &*self.controls.last_pause.borrow(); let last = last.clone()?; let delta = (now - last).num_seconds(); - // Only rewind on pause if the stream position is passed a certain point, - // And the player has been paused for more than a minute. + // Get interval passed in the gst stream let seconds_passed = self.player.get_position().seconds()?; - // FIXME: also check episode id - if seconds_passed >= 90 && delta >= 60 { + // get the last known episode id + let mut last = LAST_KNOWN_EPISODE.lock().unwrap(); + // get the current playing episode id + let current_id = *self.info.episode_id.borrow(); + // Only rewind on pause if the stream position is passed a certain point, + // and the player has been paused for more than a minute, + // and the episode id is the same + if seconds_passed >= 90 && delta >= 60 && current_id == *last { self.seek(ClockTime::from_seconds(5), SeekDirection::Backwards); } + // Set the last knows episode to the current one + *last = current_id; + Some(()) } }