[vlc-devel] [PATCH V3 17/19] qt: use the new player timer

Thomas Guillem thomas at gllm.fr
Fri Sep 6 17:20:47 CEST 2019


Add a regular player timer with a minimum delay of 250ms. This delay could be
even higher, the difference between the audio device clock and the CPU clock
should not need to be corrected that often.

The PlayerController use a QTimer, that is called every 33ms from the
Mainthread in order to update the UI time/slider using
vlc_player_timer_value_Interpolate() from the value sent via the timer update.

Ideally, the QTimer interval should be 17ms (60fps), but the slider update is
taking a lot of CPU. This should be investigated.
---
 .../gui/qt/components/player_controller.cpp   | 132 +++++++++++-------
 .../gui/qt/components/player_controller.hpp   |   1 +
 .../gui/qt/components/player_controller_p.hpp |   9 ++
 3 files changed, 90 insertions(+), 52 deletions(-)

diff --git a/modules/gui/qt/components/player_controller.cpp b/modules/gui/qt/components/player_controller.cpp
index 1dcd4b4183..54b18f8c71 100644
--- a/modules/gui/qt/components/player_controller.cpp
+++ b/modules/gui/qt/components/player_controller.cpp
@@ -60,6 +60,7 @@ PlayerControllerPrivate::~PlayerControllerPrivate()
     vlc_player_vout_RemoveListener( m_player, m_player_vout_listener );
     vlc_player_aout_RemoveListener( m_player, m_player_aout_listener );
     vlc_player_RemoveListener( m_player, m_player_listener );
+    vlc_player_RemoveTimer( m_player, m_player_timer );
 }
 
 void PlayerControllerPrivate::UpdateName(input_item_t* media)
@@ -345,16 +346,6 @@ static void on_player_buffering(vlc_player_t *, float new_buffering, void *data)
     });
 }
 
-static void on_player_rate_changed(vlc_player_t *, float new_rate, void *data)
-{
-    PlayerControllerPrivate* that = static_cast<PlayerControllerPrivate*>(data);
-    msg_Dbg( that->p_intf, "on_player_rate_changed %f", new_rate);
-    that->callAsync([that,new_rate](){
-        that->m_rate = new_rate;
-        emit that->q_func()->rateChanged( new_rate );
-    });
-}
-
 static void on_player_capabilities_changed(vlc_player_t *, int old_caps, int new_caps, void *data)
 {
     PlayerControllerPrivate* that = static_cast<PlayerControllerPrivate*>(data);
@@ -387,45 +378,6 @@ static void on_player_capabilities_changed(vlc_player_t *, int old_caps, int new
     //FIXME other events?
 }
 
-static void on_player_position_changed(vlc_player_t *player, vlc_tick_t time, float pos, void *data)
-{
-    PlayerControllerPrivate* that = static_cast<PlayerControllerPrivate*>(data);
-    vlc_tick_t length =  vlc_player_GetLength( player );
-    that->callAsync([that,time,pos,length] () {
-        PlayerController* q = that->q_func();
-        that->m_position = pos;
-        emit q->positionChanged(pos);
-        that->m_time = time;
-        emit q->timeChanged(time);
-        if ( time != VLC_TICK_INVALID && length != VLC_TICK_INVALID )
-            that->m_remainingTime = length - time;
-        else
-            that->m_remainingTime = VLC_TICK_INVALID;
-        emit q->remainingTimeChanged(that->m_remainingTime);
-        emit that->q_func()->positionUpdated(pos, time, SEC_FROM_VLC_TICK(length) );
-    });
-}
-
-static void on_player_length_changed(vlc_player_t *player, vlc_tick_t new_length, void *data)
-{
-    PlayerControllerPrivate* that = static_cast<PlayerControllerPrivate*>(data);
-    vlc_tick_t time = vlc_player_GetTime( player );
-    float pos = vlc_player_GetPosition( player );
-    that->callAsync([that,new_length,time,pos] () {
-        PlayerController* q = that->q_func();
-        that->m_length = new_length;
-        emit q->lengthChanged(new_length);
-
-        if ( time != VLC_TICK_INVALID && new_length != VLC_TICK_INVALID )
-            that->m_remainingTime = new_length - time;
-        else
-            that->m_remainingTime = VLC_TICK_INVALID;
-        emit q->remainingTimeChanged(that->m_remainingTime);
-        emit that->q_func()->positionUpdated( pos, time, SEC_FROM_VLC_TICK(new_length) );
-    });
-
-}
-
 static void on_player_track_list_changed(vlc_player_t *, enum vlc_player_list_action action, const struct vlc_player_track *track, void *data)
 {
     PlayerControllerPrivate* that = static_cast<PlayerControllerPrivate*>(data);
@@ -865,6 +817,53 @@ static void on_player_corks_changed(vlc_player_t *, unsigned, void *data)
     msg_Dbg( that->p_intf, "on_player_corks_changed");
 }
 
+static void on_player_timer_update(enum vlc_player_timer_state state,
+                                   const struct vlc_player_timer_point *point,
+                                   void *data)
+{
+    PlayerControllerPrivate* that = static_cast<PlayerControllerPrivate*>(data);
+    that->callAsync([that,state,point_copy = *point](){
+        PlayerController* q = that->q_func();
+        bool changed = false;
+
+        that->m_player_time = point_copy;
+
+        if (that->m_length != that->m_player_time.length)
+        {
+            that->m_length = that->m_player_time.length;
+            emit q->lengthChanged(that->m_length);
+            changed = true;
+        }
+        if (that->m_rate != that->m_player_time.rate)
+        {
+            that->m_rate = that->m_player_time.rate;
+            emit q->rateChanged(that->m_rate);
+            changed = true;
+        }
+
+        switch (state)
+        {
+            case VLC_PLAYER_TIMER_STATE_PLAYING:
+                if (that->m_player_time_state != state)
+                {
+                    that->m_interpolate_timer.start();
+                    changed = true;
+                }
+                break;
+            case VLC_PLAYER_TIMER_STATE_PAUSED:
+            case VLC_PLAYER_TIMER_STATE_DISCONTINUITY:
+                that->m_interpolate_timer.stop();
+                break;
+            default:
+                break;
+        }
+        that->m_player_time_state = state;
+
+        if (changed)
+            q->interpolateTime();
+    });
+}
+
 } //extern "C"
 
 static const struct vlc_player_cbs player_cbs = {
@@ -872,10 +871,10 @@ static const struct vlc_player_cbs player_cbs = {
     on_player_state_changed,
     on_player_error_changed,
     on_player_buffering,
-    on_player_rate_changed,
+    nullptr, // on_player_rate_changed: handled by on_player_timer_update
     on_player_capabilities_changed,
-    on_player_position_changed,
-    on_player_length_changed,
+    nullptr, // on_player_position_changed: handled by on_player_timer_update
+    nullptr, // on_player_length_changed: handled by on_player_timer_update
     on_player_track_list_changed,
     on_player_track_selection_changed,
     on_player_track_delay_changed,
@@ -914,6 +913,10 @@ static const struct vlc_player_aout_cbs player_aout_cbs = {
     nullptr
 };
 
+static const struct vlc_player_timer_cbs player_timer_cbs = {
+    on_player_timer_update,
+};
+
 PlayerControllerPrivate::PlayerControllerPrivate(PlayerController *playercontroller, intf_thread_t *p_intf)
     : q_ptr(playercontroller)
     , p_intf(p_intf)
@@ -939,10 +942,13 @@ PlayerControllerPrivate::PlayerControllerPrivate(PlayerController *playercontrol
         m_player_listener = vlc_player_AddListener( m_player, &player_cbs, this );
         m_player_aout_listener = vlc_player_aout_AddListener( m_player, &player_aout_cbs, this );
         m_player_vout_listener = vlc_player_vout_AddListener( m_player, &player_vout_cbs, this );
+        m_player_timer = vlc_player_AddTimer( m_player, VLC_TICK_FROM_MS(250), &player_timer_cbs, this );
     }
 
     QObject::connect( &m_autoscale, &QVLCBool::valueChanged, q_ptr, &PlayerController::autoscaleChanged );
     QObject::connect( &m_audioVisualization, &VLCVarChoiceModel::hasCurrentChanged, q_ptr, &PlayerController::hasAudioVisualizationChanged );
+
+    m_interpolate_timer.setInterval( 33 );
 }
 
 PlayerController::PlayerController( intf_thread_t *_p_intf )
@@ -952,6 +958,7 @@ PlayerController::PlayerController( intf_thread_t *_p_intf )
     /* Audio Menu */
     menusAudioMapper = new QSignalMapper(this);
     CONNECT( menusAudioMapper, mapped(const QString&), this, menusUpdateAudio(const QString&) );
+    CONNECT( &d_ptr->m_interpolate_timer, timeout(), this, interpolateTime() );
 
     input_fetcher_cbs.on_art_fetch_ended = onArtFetchEnded_callback;
 }
@@ -1374,6 +1381,27 @@ void PlayerController::menusUpdateAudio( const QString& data )
         aout_DeviceSet( aout.get(), qtu(data) );
 }
 
+void PlayerController::interpolateTime()
+{
+    Q_D(PlayerController);
+    vlc_tick_t new_time;
+    if (vlc_player_timer_point_Interpolate(&d->m_player_time, vlc_tick_now(),
+                                           &new_time, &d->m_position) == VLC_SUCCESS)
+    {
+        d->m_time = new_time != VLC_TICK_INVALID ? new_time - VLC_TICK_0 : 0;
+        emit positionChanged(d->m_position);
+        emit timeChanged(d->m_time);
+        emit positionUpdated(d->m_position, d->m_time,
+                             SEC_FROM_VLC_TICK(d->m_length));
+
+        if (d->m_time != VLC_TICK_INVALID && d->m_length != VLC_TICK_INVALID)
+            d->m_remainingTime = d->m_length - d->m_time;
+        else
+            d->m_remainingTime = VLC_TICK_INVALID;
+        emit remainingTimeChanged(d->m_remainingTime);
+    }
+}
+
 //MISC
 
 void PlayerController::setABloopState(ABLoopState state)
diff --git a/modules/gui/qt/components/player_controller.hpp b/modules/gui/qt/components/player_controller.hpp
index 7fb4384886..7fa0d97547 100644
--- a/modules/gui/qt/components/player_controller.hpp
+++ b/modules/gui/qt/components/player_controller.hpp
@@ -269,6 +269,7 @@ public slots:
     bool isRewindable() const;
     bool isPausable() const;
     bool isRateChangable() const;
+    void interpolateTime();
 
     //tracks
     TrackListModel* getVideoTracks();
diff --git a/modules/gui/qt/components/player_controller_p.hpp b/modules/gui/qt/components/player_controller_p.hpp
index dcc28c929f..ea82ad5c61 100644
--- a/modules/gui/qt/components/player_controller_p.hpp
+++ b/modules/gui/qt/components/player_controller_p.hpp
@@ -24,6 +24,8 @@
 #include "util/input_models.hpp"
 #include "adapters/var_choice_model.hpp"
 
+#include <QTimer>
+
 class PlayerControllerPrivate {
     Q_DISABLE_COPY(PlayerControllerPrivate)
 public:
@@ -93,6 +95,13 @@ public:
     VLCTick      m_secondarySubtitleDelay = 0;
     float        m_subtitleFPS = 1.0;
 
+    //timer
+    vlc_player_timer_id* m_player_timer = nullptr;
+    vlc_player_timer_id* m_player_timer_smpte = nullptr;
+    struct vlc_player_timer_point m_player_time;
+    enum vlc_player_timer_state m_player_time_state = VLC_PLAYER_TIMER_STATE_DISCONTINUITY;
+    QTimer m_interpolate_timer;
+
     //title/chapters/menu
     TitleListModel m_titleList;
     ChapterListModel m_chapterList;
-- 
2.20.1



More information about the vlc-devel mailing list