[vlc-devel] [PATCH 2/2] dbus: replace discontinuity detection by player timer

Alexandre Janniaux ajanni at videolabs.io
Sat Apr 4 18:33:28 CEST 2020


The new Player timer API is able to signal discontinuities so there is
no need for the previous convoluted threshold detection.

When the timer callback `on_discontinuity` is called, either a
discontinuity has been signalled, coming from a seek or the media
itself, or the playback has been paused or stopped.

Pause can be detected because it's the only case where `system_date`
is a valid tick but for stopped we use the VLC_PLAYER_STATE_STOPPING
state which sets i_playback_state to PLAYBACK_STATE_STOPPED.

To clarify that VLC_PLAYER_STATE_STOPPING is used, it is added to the
switch case even if there is no behaviour change.

In addition, it removes the warning on llabs usage with float.
---
 modules/control/dbus/dbus.c        | 77 +++++++++++++++---------------
 modules/control/dbus/dbus_common.h |  4 +-
 2 files changed, 40 insertions(+), 41 deletions(-)

diff --git a/modules/control/dbus/dbus.c b/modules/control/dbus/dbus.c
index b497be4656..b968ba9e94 100644
--- a/modules/control/dbus/dbus.c
+++ b/modules/control/dbus/dbus.c
@@ -146,8 +146,6 @@ static void player_on_error_changed(vlc_player_t *,
                                     enum vlc_player_error, void *);
 static void player_on_rate_changed(vlc_player_t *, float, void *);
 static void player_on_capabilities_changed(vlc_player_t *, int, int, void *);
-static void player_on_position_changed(vlc_player_t *,
-                                       vlc_tick_t, float, void *);
 static void player_on_media_meta_changed(vlc_player_t *,
                                          input_item_t *, void *);
 
@@ -155,6 +153,8 @@ static void player_aout_on_volume_changed(audio_output_t *, float, void *);
 static void player_aout_on_mute_changed(audio_output_t *, bool, void *);
 
 static void player_vout_on_fullscreen_changed(vout_thread_t *, bool, void *);
+static void player_timer_on_discontinuity(vlc_tick_t system_dae, void *data);
+static void player_timer_on_update(const struct vlc_player_timer_point *, void *);
 
 /*****************************************************************************
  * Module descriptor
@@ -278,7 +278,6 @@ static int Open( vlc_object_t *p_this )
         .on_error_changed = player_on_error_changed,
         .on_rate_changed = player_on_rate_changed,
         .on_capabilities_changed = player_on_capabilities_changed,
-        .on_position_changed = player_on_position_changed,
         .on_media_meta_changed = player_on_media_meta_changed,
     };
     p_sys->player_listener =
@@ -305,6 +304,16 @@ static int Open( vlc_object_t *p_this )
     if (!p_sys->player_vout_listener)
         goto player_vout_listener_failure;
 
+    static struct vlc_player_timer_cbs const player_timer_cbs =
+    {
+        .on_update = player_timer_on_update,
+        .on_discontinuity = player_timer_on_discontinuity,
+    };
+    p_sys->player_timer =
+        vlc_player_AddTimer(player, VLC_TICK_INVALID, &player_timer_cbs, p_intf);
+    if (!p_sys->player_timer)
+        goto player_timer_failure;
+
     vlc_playlist_Unlock(playlist);
 
     if( !dbus_connection_set_timeout_functions( p_conn,
@@ -328,6 +337,8 @@ static int Open( vlc_object_t *p_this )
 
 late_failure:
     vlc_playlist_Lock(playlist);
+    vlc_player_RemoveTimer(player, p_sys->player_timer);
+player_timer_failure:
     vlc_player_vout_RemoveListener(player, p_sys->player_vout_listener);
 player_vout_listener_failure:
     vlc_player_aout_RemoveListener(player, p_sys->player_aout_listener);
@@ -368,6 +379,7 @@ static void Close   ( vlc_object_t *p_this )
 
     vlc_player_t *player = vlc_playlist_GetPlayer(playlist);
     vlc_playlist_Lock(playlist);
+    vlc_player_RemoveTimer(player, p_sys->player_timer);
     vlc_player_vout_RemoveListener(player, p_sys->player_vout_listener);
     vlc_player_aout_RemoveListener(player, p_sys->player_aout_listener);
     vlc_player_RemoveListener(player, p_sys->player_listener);
@@ -1039,6 +1051,9 @@ player_on_state_changed(vlc_player_t *player, enum vlc_player_state state,
         case VLC_PLAYER_STATE_PAUSED:
             playing_state = PLAYBACK_STATE_PAUSED;
             break;
+        /* STOPPING is used to detect time discontinuities that are coming
+         * from stopping the playback in the vlc player timer callbacks. */
+        case VLC_PLAYER_STATE_STOPPING:
         case VLC_PLAYER_STATE_STOPPED:
         default:
             playing_state = PLAYBACK_STATE_STOPPED;
@@ -1093,41 +1108,6 @@ player_on_capabilities_changed(vlc_player_t *player, int old_caps, int new_caps,
     (void) player; (void) old_caps; (void) new_caps;
 }
 
-static void
-player_on_position_changed(vlc_player_t *player, vlc_tick_t time, float pos,
-                           void *data)
-{
-    intf_thread_t *intf = data;
-    intf_sys_t *sys = intf->p_sys;
-    vlc_tick_t i_now = vlc_tick_now(), i_projected_pos, i_interval;
-    float f_current_rate;
-
-    /* Detect seeks: moved from 1857cab238c
-     * XXX: This is way more convoluted than it should be... */
-
-    if( !sys->i_last_input_pos_event ||
-        vlc_player_GetState(player) != VLC_PLAYER_STATE_PLAYING )
-    {
-        sys->i_last_input_pos_event = i_now;
-        sys->i_last_input_pos = pos;
-        return;
-    }
-
-    f_current_rate = vlc_player_GetRate(player);
-    i_interval = ( i_now - sys->i_last_input_pos_event );
-
-    i_projected_pos = sys->i_last_input_pos + ( i_interval * f_current_rate );
-
-    sys->i_last_input_pos_event = i_now;
-    sys->i_last_input_pos = pos;
-
-    if( llabs( pos - i_projected_pos ) < SEEK_THRESHOLD )
-        return;
-
-    add_event_signal(intf, &(callback_info_t){ .signal = SIGNAL_SEEK });
-    (void) time;
-}
-
 static void
 player_on_media_meta_changed(vlc_player_t *player,
                              input_item_t *item, void *data)
@@ -1161,6 +1141,27 @@ player_vout_on_fullscreen_changed(vout_thread_t *vout, bool enabled,
     (void) vout; (void) enabled;
 }
 
+static void
+player_timer_on_update(const struct vlc_player_timer_point *value, void *data)
+{
+    /* The callback is not used by mandatory in the vlc_player_timer API */
+    VLC_UNUSED(value);
+    VLC_UNUSED(data);
+}
+
+static void
+player_timer_on_discontinuity(vlc_tick_t system_date, void *data)
+{
+    intf_thread_t *intf = data;
+    intf_sys_t *sys = intf->p_sys;
+
+    bool stopping = sys->i_playing_state == PLAYBACK_STATE_STOPPED;
+    bool paused = system_date != VLC_TICK_INVALID;
+
+    if( !paused && !stopping )
+        add_event_signal(intf, &(callback_info_t){ .signal = SIGNAL_SEEK });
+}
+
 /*****************************************************************************
  * TrackChange: callback on playlist_on_current_index_changed
  *****************************************************************************/
diff --git a/modules/control/dbus/dbus_common.h b/modules/control/dbus/dbus_common.h
index c505d4ee1b..7cb82212bc 100644
--- a/modules/control/dbus/dbus_common.h
+++ b/modules/control/dbus/dbus_common.h
@@ -96,6 +96,7 @@ struct intf_sys_t
     vlc_player_listener_id      *player_listener;
     vlc_player_aout_listener_id *player_aout_listener;
     vlc_player_vout_listener_id *player_vout_listener;
+    vlc_player_timer_id         *player_timer;
 
     DBusConnection *p_conn;
     bool            b_meta_read;
@@ -110,9 +111,6 @@ struct intf_sys_t
     vlc_mutex_t     lock;
     vlc_thread_t    thread;
     bool            has_input;
-
-    vlc_tick_t      i_last_input_pos; /* Only access from input thread */
-    vlc_tick_t      i_last_input_pos_event; /* Same as above */
 };
 
 enum
-- 
2.26.0



More information about the vlc-devel mailing list