From deb6af9dea0ef70ccfdaf8ec721a9805ddbc8fdd Mon Sep 17 00:00:00 2001 From: Bruno Date: Wed, 26 May 2021 23:31:34 +0200 Subject: [PATCH] Fixed and improved TimeCounter --- MediaPlayer.cpp | 58 +++++++++++-------------------------------------- MediaPlayer.h | 10 +++------ Stream.cpp | 58 ++++++++++--------------------------------------- Stream.h | 10 +++------ 4 files changed, 31 insertions(+), 105 deletions(-) diff --git a/MediaPlayer.cpp b/MediaPlayer.cpp index 61602f5..5abe87d 100644 --- a/MediaPlayer.cpp +++ b/MediaPlayer.cpp @@ -605,9 +605,6 @@ void MediaPlayer::play(bool on) Log::Info("MediaPlayer %s Stop [%ld]", std::to_string(id_).c_str(), position()); #endif - // reset time counter - timecount_.reset(); - } bool MediaPlayer::isPlaying(bool testpipeline) const @@ -944,6 +941,7 @@ void MediaPlayer::update() if (need_loop) { execute_loop_command(); } + } void MediaPlayer::execute_loop_command() @@ -1233,54 +1231,24 @@ GstFlowReturn MediaPlayer::callback_new_sample (GstAppSink *sink, gpointer p) -MediaPlayer::TimeCounter::TimeCounter() { +MediaPlayer::TimeCounter::TimeCounter() +{ + timer = g_timer_new (); +} - reset(); +MediaPlayer::TimeCounter::~TimeCounter() +{ + g_free(timer); } void MediaPlayer::TimeCounter::tic () { - // how long since last time - GstClockTime t = gst_util_get_timestamp (); - GstClockTime dt = t - last_time -1; - - // one more frame since last time - nbFrames++; + double dt = g_timer_elapsed (timer, NULL) * 1000.0; + g_timer_start(timer); // calculate instantaneous framerate - // Exponential moving averate with previous framerate to filter jitter (70/30) - // The divition of frame/time is done on long integer GstClockTime, counting in microsecond - // NB: factor 100 to get 0.01 precision - fps = 0.7 * fps + 0.003 * static_cast( ( 100 * GST_SECOND * nbFrames ) / dt ); - - // reset counter every second - if ( dt >= GST_SECOND) - { - last_time = t; - nbFrames = 0; - } -} - -GstClockTime MediaPlayer::TimeCounter::dt () -{ - GstClockTime t = gst_util_get_timestamp (); - GstClockTime dt = t - tic_time; - tic_time = t; - - // return the instantaneous delta t - return dt; -} - -void MediaPlayer::TimeCounter::reset () -{ - last_time = gst_util_get_timestamp ();; - tic_time = last_time; - nbFrames = 0; - fps = 0.0; -} - -double MediaPlayer::TimeCounter::frameRate() const -{ - return fps; + // Exponential moving averate with previous framerate to filter jitter + if (dt > 1.0) + fps = 0.5 * fps + 500.0 / dt ; } diff --git a/MediaPlayer.h b/MediaPlayer.h index 3efd921..1c8bc32 100644 --- a/MediaPlayer.h +++ b/MediaPlayer.h @@ -296,17 +296,13 @@ private: // fps counter struct TimeCounter { - - GstClockTime last_time; - GstClockTime tic_time; - long nbFrames; + GTimer *timer; gdouble fps; public: TimeCounter(); - GstClockTime dt(); + ~TimeCounter(); void tic(); - void reset(); - gdouble frameRate() const; + inline gdouble frameRate() const { return fps; } }; TimeCounter timecount_; diff --git a/Stream.cpp b/Stream.cpp index 6d0fe6b..3ea5d8c 100644 --- a/Stream.cpp +++ b/Stream.cpp @@ -349,9 +349,6 @@ void Stream::play(bool on) if (live_) gst_element_get_state (pipeline_, NULL, NULL, GST_CLOCK_TIME_NONE); - // reset time counter - timecount_.reset(); - } bool Stream::isPlaying(bool testpipeline) const @@ -737,54 +734,23 @@ GstFlowReturn Stream::callback_new_sample (GstAppSink *sink, gpointer p) -Stream::TimeCounter::TimeCounter() { +Stream::TimeCounter::TimeCounter() +{ + timer = g_timer_new (); +} - reset(); +Stream::TimeCounter::~TimeCounter() +{ + g_free(timer); } void Stream::TimeCounter::tic () { - // how long since last time - GstClockTime t = gst_util_get_timestamp (); - GstClockTime dt = t - last_time - 1; - - // one more frame since last time - ++nbFrames; + double dt = g_timer_elapsed (timer, NULL) * 1000.0; + g_timer_start(timer); // calculate instantaneous framerate - // Exponential moving averate with previous framerate to filter jitter (70/30) - // The divition of frame/time is done on long integer GstClockTime, counting in microsecond - // NB: factor 100 to get 0.01 precision - fps = 0.7 * fps + 0.003 * static_cast( ( 100 * GST_SECOND * nbFrames ) / dt ); - - // reset counter every second - if ( dt >= GST_SECOND) - { - last_time = t; - nbFrames = 0; - } + // Exponential moving averate with previous framerate to filter jitter + if (dt > 1.0) + fps = 0.5 * fps + 500.0 / dt ; } - -GstClockTime Stream::TimeCounter::dt () -{ - GstClockTime t = gst_util_get_timestamp (); - GstClockTime dt = t - tic_time; - tic_time = t; - - // return the instantaneous delta t - return dt; -} - -void Stream::TimeCounter::reset () -{ - last_time = gst_util_get_timestamp ();; - tic_time = last_time; - nbFrames = 0; - fps = 0.0; -} - -double Stream::TimeCounter::frameRate() const -{ - return fps; -} - diff --git a/Stream.h b/Stream.h index 10e6ac2..de9a246 100644 --- a/Stream.h +++ b/Stream.h @@ -144,17 +144,13 @@ protected: // fps counter struct TimeCounter { - - GstClockTime last_time; - GstClockTime tic_time; - long nbFrames; + GTimer *timer; gdouble fps; public: TimeCounter(); - GstClockTime dt(); + ~TimeCounter(); void tic(); - void reset(); - gdouble frameRate() const; + inline gdouble frameRate() const { return fps; } }; TimeCounter timecount_;