[gnome-music/wip/mschraal/player-factor-out-gstreamer] gstplayer: Use the gst clock for timeouts



commit f8e78314c5845722a27e67511465153a7e5efd1d
Author: Marinus Schraal <mschraal gnome org>
Date:   Sun Feb 18 00:25:12 2018 +0100

    gstplayer: Use the gst clock for timeouts

 gnomemusic/gstplayer.py           | 21 ++++++++++++++++++++-
 gnomemusic/player.py              |  4 ++--
 gnomemusic/widgets/smoothscale.py | 27 ++-------------------------
 3 files changed, 24 insertions(+), 28 deletions(-)
---
diff --git a/gnomemusic/gstplayer.py b/gnomemusic/gstplayer.py
index dc830e5..50e2858 100644
--- a/gnomemusic/gstplayer.py
+++ b/gnomemusic/gstplayer.py
@@ -48,7 +48,8 @@ class Playback(IntEnum):
 class GstPlayer(GObject.GObject):
 
     __gsignals__ = {
-        'eos': (GObject.SignalFlags.RUN_FIRST, None, ())
+        'eos': (GObject.SignalFlags.RUN_FIRST, None, ()),
+        'clock-tick': (GObject.SignalFlags.RUN_FIRST, None, ())
     }
 
     def __repr__(self):
@@ -81,6 +82,8 @@ class GstPlayer(GObject.GObject):
         self._bus.connect('message::eos', self._on_bus_eos)
         self._bus.connect(
             'message::duration-changed', self._on_duration_changed)
+        self._bus.connect('message::new-clock', self._on_new_clock)
+        self._bus.connect('message::clock-lost', self._on_clock_lost)
 
         self.state = Playback.STOPPED
 
@@ -116,6 +119,22 @@ class GstPlayer(GObject.GObject):
         else:
             self._player.set_property("audio-filter", None)
 
+    @log
+    def _on_new_clock(self, bus, message):
+        print("NEW CLOCK")
+        clock = message.parse_new_clock()
+        id = clock.new_periodic_id(0, 1 * 10**9)
+        clock.id_wait_async(id, self._on_clock_tick, None)
+
+    @log
+    def _on_clock_lost(self, bus, message):
+        print("CLOCK LOST")
+
+    @log
+    def _on_clock_tick(self, clock, time, id, data):
+        print("TICK", time / 10**9)
+        self.emit('clock-tick')
+
     @log
     def _on_bus_state_changed(self, bus, message):
         # Note: not all state changes are signaled through here, in
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index fb4f478..b482ffd 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -122,6 +122,7 @@ class Player(GObject.GObject):
         self.playlist_delete_handler = 0
 
         self._player = GstPlayer()
+        self._player.connect('clock-tick', self._on_clock_tick)
         self._player.connect('eos', self._on_eos)
         self._player.connect('notify::state', self._on_state_change)
 
@@ -602,7 +603,6 @@ class Player(GObject.GObject):
         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_scale.connect(
             'value-changed', self._on_progress_value_changed)
 
@@ -633,7 +633,7 @@ class Player(GObject.GObject):
         self._player.state = Playback.PLAYING
 
     @log
-    def _on_seconds_tick(self, klass):
+    def _on_clock_tick(self, klass):
         seconds = int(self._player.position)
         print("TICK", seconds, self._player.position)
 
diff --git a/gnomemusic/widgets/smoothscale.py b/gnomemusic/widgets/smoothscale.py
index 92cd501..f71d80a 100644
--- a/gnomemusic/widgets/smoothscale.py
+++ b/gnomemusic/widgets/smoothscale.py
@@ -41,9 +41,6 @@ class SmoothScale(Gtk.Scale):
         'seek-finished': (
             GObject.SignalFlags.RUN_FIRST, None, (float,)
         ),
-        'seconds-tick': (
-            GObject.SignalFlags.RUN_FIRST, None, ()
-        )
     }
 
     def __repr__(self):
@@ -60,9 +57,6 @@ class SmoothScale(Gtk.Scale):
         self._previous_state = None
 
         self.timeout = None
-        self._seconds_timeout = 0
-        self._seconds_period = 0
-        self.played_seconds = 0
 
         self.connect('button-press-event', self._on_progress_scale_event)
         self.connect('button-release-event', self._on_progress_scale_button_released)
@@ -153,14 +147,11 @@ class SmoothScale(Gtk.Scale):
         return False
 
     def _update_timeout(self):
-        """Update the duration for self.timeout & self._seconds_timeout
+        """Update the duration for self.timeout
 
         Sets the period of self.timeout to a value small enough to make
         the slider of self._progress_scale move smoothly based on the
         current song duration and progress_scale length.
-        self._seconds_timeout is always set to a fixed value, short
-        enough to hide irregularities in GLib event timing from the
-        user, for updating the _progress_time_label.
         """
         # Do not run until progress_scale has been realized and
         # gstreamer provides a duration.
@@ -182,19 +173,10 @@ class SmoothScale(Gtk.Scale):
         self.timeout = GLib.timeout_add(
             timeout_period, self._update_position_callback)
 
-        # Update self._seconds_timeout.
-        if not self._seconds_timeout:
-            self._seconds_period = 1000
-            self._seconds_timeout = GLib.timeout_add(
-                self._seconds_period, self._update_seconds_callback)
-
     def _remove_timeout(self):
         if self.timeout:
             GLib.source_remove(self.timeout)
             self.timeout = None
-        if self._seconds_timeout:
-            GLib.source_remove(self._seconds_timeout)
-            self._seconds_timeout = None
 
     def _progress_scale_zero(self):
         self.set_value(0)
@@ -219,9 +201,4 @@ class SmoothScale(Gtk.Scale):
         if position > 0:
             self.set_value(position * 60)
         self._update_timeout()
-        return False
-
-    @log
-    def _update_seconds_callback(self):
-        self.emit('seconds-tick')
-        return True
\ No newline at end of file
+        return False
\ No newline at end of file


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