[gnome-music/wip/mschraal/player-factor-out-gstreamer] smoothscale: Internalize update calls



commit 5f8cfe45c712df3133172d73f48e9479e06b9eec
Author: Marinus Schraal <mschraal gnome org>
Date:   Sat Feb 17 15:24:02 2018 +0100

    smoothscale: Internalize update calls

 gnomemusic/gstplayer.py           |  5 +++++
 gnomemusic/player.py              | 43 +++++++++++++++++++++++++++++++--------
 gnomemusic/widgets/smoothscale.py | 34 +++++++++++++++++++++++++++++++
 3 files changed, 74 insertions(+), 8 deletions(-)
---
diff --git a/gnomemusic/gstplayer.py b/gnomemusic/gstplayer.py
index 1541164..55a3cf9 100644
--- a/gnomemusic/gstplayer.py
+++ b/gnomemusic/gstplayer.py
@@ -60,6 +60,8 @@ class GstPlayer(GObject.GObject):
 
         Gst.init(None)
 
+        self._duration = None
+
         self._missing_plugin_messages = []
         self._settings = Gio.Settings.new('org.gnome.Music')
 
@@ -225,6 +227,9 @@ class GstPlayer(GObject.GObject):
     @log
     def duration(self):
         """Total duration in seconds (float)"""
+        if self._duration == None:
+            self._duration = self._player.query_duration(
+                Gst.Format.TIME)[1] / 10**9
         print("duration ", self._duration)
         return self._duration
 
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index 07a3b1e..8dabbad 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -106,6 +106,8 @@ class Player(GObject.GObject):
         Gst.init(None)
         GstPbutils.pb_utils_init()
 
+        self._seconds_period = 0
+
         self._discoverer = GstPbutils.Discoverer()
         self._discoverer.connect('discovered', self._on_discovered)
         self._discoverer.start()
@@ -329,6 +331,8 @@ class Player(GObject.GObject):
     def _on_state_change(self, klass, arguments):
         self._sync_playing()
 
+        return True
+
     @log
     def _sync_playing(self):
         if self._player.state == Playback.PLAYING:
@@ -487,25 +491,19 @@ class Player(GObject.GObject):
 
         self._player.state = Playback.PLAYING
 
-        self._progress_scale._update_position_callback()
+        # self._progress_scale._update_position_callback()
         if media:
             self._lastfm.now_playing(media)
-        # if not self._progress_scale.timeout and self._progress_scale.get_realized():
-        self._progress_scale._update_timeout()
 
         self.emit('playback-status-changed')
 
     @log
     def pause(self):
-        self._progress_scale._remove_timeout()
-
         self._player.state = Playback.PAUSED
         self.emit('playback-status-changed')
 
     @log
     def stop(self):
-        self._progress_scale._remove_timeout()
-
         self._player.state = Playback.STOPPED
         self.emit('playback-status-changed')
 
@@ -608,9 +606,10 @@ class Player(GObject.GObject):
         self._pause_image = self._ui.get_object('pause_image')
 
         self._progress_scale = self._ui.get_object('smooth_scale')
-        self._progress_scale._player = self._player
+        self._progress_scale.player = self._player
 
         self._progress_scale.connect('seek-finished', self._on_seek_finished)
+        self._progress_scale.connect('seconds-tick', self._on_seconds_tick)
 
         self._progress_time_label = self._ui.get_object('playback')
         self._total_time_label = self._ui.get_object('duration')
@@ -634,6 +633,34 @@ class Player(GObject.GObject):
     def _on_seek_finished(self, klass, time):
         self._player.state = Playback.PLAYING
 
+    @log
+    def _on_seconds_tick(self, klass):
+        seconds = int(self._player.position)
+        print("TICK", seconds, self._player.position)
+
+        self._progress_time_label.set_label(
+            utils.seconds_to_string(seconds))
+
+        position = self._player.position
+        if position > 0:
+            self.played_seconds += self._seconds_period / 1000
+            try:
+                percentage = self.played_seconds / self.duration
+                if (not self._lastfm.scrobbled
+                        and percentage > 0.4):
+                    current_media = self.get_current_media()
+                    if current_media:
+                        # FIXME: we should not need to update static
+                        # playlists here but removing it may introduce
+                        # a bug. So, we keep it for the time being.
+                        playlists.update_all_static_playlists()
+                        grilo.bump_play_count(current_media)
+                        grilo.set_last_played(current_media)
+                        self._lastfm.scrobble(current_media, self._time_stamp)
+
+            except Exception as e:
+                logger.warn("Error: %s, %s", e.__class__, e)
+
     @log
     def _on_play_button_clicked(self, button):
         if self._player.state == Playback.PLAYING:
diff --git a/gnomemusic/widgets/smoothscale.py b/gnomemusic/widgets/smoothscale.py
index 6c6a562..6cae907 100644
--- a/gnomemusic/widgets/smoothscale.py
+++ b/gnomemusic/widgets/smoothscale.py
@@ -28,6 +28,7 @@ from gettext import gettext as _
 from gi.repository import Gdk, GLib, GObject, Gtk
 
 from gnomemusic import log
+from gnomemusic.gstplayer import Playback
 
 logger = logging.getLogger(__name__)
 
@@ -40,6 +41,9 @@ class SmoothScale(Gtk.Scale):
         'seek-finished': (
             GObject.SignalFlags.RUN_FIRST, None, (float,)
         ),
+        'seconds-tick': (
+            GObject.SignalFlags.RUN_FIRST, None, ()
+        )
     }
 
     def __repr__(self):
@@ -53,6 +57,7 @@ class SmoothScale(Gtk.Scale):
         self._old_progress_scale_value = 0.0
         self.set_increments(300, 600)
         self._seek_timeout = None
+        self._previous_state = None
 
         self.timeout = None
         self._seconds_timeout = 0
@@ -65,6 +70,33 @@ class SmoothScale(Gtk.Scale):
         self.connect('change-value', self._on_progress_scale_seek)
         self._ps_draw = self.connect('draw', self._on_progress_scale_draw)
 
+    @GObject.property
+    @log
+    def player(self):
+        return self._player
+
+    @player.setter
+    @log
+    def player(self, player):
+        self._player = player
+
+        self._player.connect('notify::state', self._on_state_change)
+
+    @log
+    def _on_state_change(self, klass, arguments):
+        state = self._player.state
+
+        if self._previous_state == state:
+            return
+
+        self._previous_state = state
+
+        if state == Playback.PLAYING:
+            self._update_timeout()
+        else:
+            self._remove_timeout()
+
+        return True
 
     def _on_progress_scale_seek_finish(self, value):
         """Prevent stutters when seeking with infinitesimal amounts"""
@@ -195,6 +227,8 @@ class SmoothScale(Gtk.Scale):
     @log
     def _update_seconds_callback(self):
         self._on_progress_value_changed(None)
+        self.emit('seconds-tick')
+        return True
 
         position = self._player.position
         if position > 0:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]