[gnome-music/wip/mschraal/gst-handle-async-done-3-32-backport: 1/3] gstplayer: Improve state handling



commit a465b5a5cea263b7700c10d20195ad7658d68641
Author: Marinus Schraal <mschraal gnome org>
Date:   Sun Apr 14 23:17:59 2019 +0200

    gstplayer: Improve state handling
    
    The initial run of a pipeline does not trigger a state-change bus message.
    This resulted in several workarounds to trigger a duration signal.
    Turns out async-done is a more reliable way to get relevant state changes.
    Use async-done to trigger state and duration changes in GstPlayer.
    
    Closes: #274

 gnomemusic/gstplayer.py           | 46 +++++++++++----------------------------
 gnomemusic/widgets/smoothscale.py |  1 -
 2 files changed, 13 insertions(+), 34 deletions(-)
---
diff --git a/gnomemusic/gstplayer.py b/gnomemusic/gstplayer.py
index 16435c15..4e048337 100644
--- a/gnomemusic/gstplayer.py
+++ b/gnomemusic/gstplayer.py
@@ -81,15 +81,12 @@ class GstPlayer(GObject.GObject):
         self._on_replaygain_setting_changed(
             None, self._settings.get_value('replaygain'))
 
-        self._bus.connect('message::state-changed', self._on_bus_state_changed)
+        self._bus.connect('message::async-done', self._on_async_done)
         self._bus.connect('message::error', self._on_bus_error)
         self._bus.connect('message::element', self._on_bus_element)
         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._previous_state = Playback.STOPPED
         self.props.state = Playback.STOPPED
 
     @log
@@ -124,46 +121,29 @@ class GstPlayer(GObject.GObject):
         else:
             self._player.set_property("audio-filter", None)
 
+    @log
+    def _on_async_done(self, bus, message):
+        success, duration = self._player.query_duration(
+            Gst.Format.TIME)
+
+        if success:
+            self.props.duration = duration / Gst.SECOND
+        else:
+            self.props.duration = duration
+
+        self.notify('state')
+
     @log
     def _on_new_clock(self, bus, message):
         clock = message.parse_new_clock()
         id_ = clock.new_periodic_id(0, 1 * Gst.SECOND)
         clock.id_wait_async(id_, self._on_clock_tick, None)
 
-        # TODO: Workaround the first duration change not being emitted
-        # and hence smoothscale not being initialized properly.
-        if self.props.duration == -1.:
-            self._on_duration_changed(None, None)
-
     @log
     def _on_clock_tick(self, clock, time, id, data):
         tick = time / Gst.SECOND
         self.emit('clock-tick', tick)
 
-    @log
-    def _on_bus_state_changed(self, bus, message):
-        # Note: not all state changes are signaled through here, in
-        # particular transitions between Gst.State.READY and
-        # Gst.State.NULL are never async and thus don't cause a
-        # message. In practice, self means only Gst.State.PLAYING and
-        # Gst.State.PAUSED are.
-        current_state = self.props.state
-        if current_state == self._previous_state:
-            return
-
-        self._previous_state = current_state
-        self.notify('state')
-
-    @log
-    def _on_duration_changed(self, bus, message):
-        success, duration = self._player.query_duration(
-            Gst.Format.TIME)
-
-        if success:
-            self.props.duration = duration / Gst.SECOND
-        else:
-            self.props.duration = -1.
-
     @log
     def _on_bus_element(self, bus, message):
         if GstPbutils.is_missing_plugin_message(message):
diff --git a/gnomemusic/widgets/smoothscale.py b/gnomemusic/widgets/smoothscale.py
index 7cd40bc9..103ff800 100644
--- a/gnomemusic/widgets/smoothscale.py
+++ b/gnomemusic/widgets/smoothscale.py
@@ -116,7 +116,6 @@ class SmoothScale(Gtk.Scale):
         if duration != -1.:
             self.set_range(0.0, duration * 60)
             self.set_increments(300, 600)
-            self._on_state_change(None, None)
 
     @log
     def _on_smooth_scale_seek_finish(self, value):


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