From c1bd6677c6a7b7dcc3e402089ddf6b01b1cf6c1f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= <sebastian@centricular.com>
Date: Thu, 8 Sep 2016 15:19:38 +0300
Subject: [PATCH] basesink: Use the average durations based on timestamps for
 the QoS proportion when doing trickmodes

The durations of the buffers are (usually) assuming that no frames are being
dropped and are just the durations coming from the stream. However if we do
trickmodes, frames are being dropped regularly especially if only key units
are supposed to be played.

Fixes completely bogus QoS proportion values in the above case.
---
 libs/gst/base/gstbasesink.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/libs/gst/base/gstbasesink.c b/libs/gst/base/gstbasesink.c
index 1d82787a23..42c6221bcd 100644
--- a/libs/gst/base/gstbasesink.c
+++ b/libs/gst/base/gstbasesink.c
@@ -2668,8 +2668,13 @@ gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
     left = start + jitter;
   }
 
-  /* calculate duration of the buffer */
-  if (GST_CLOCK_TIME_IS_VALID (stop) && stop != start)
+  /* calculate duration of the buffer, only use buffer durations if not in
+   * trick mode or key-unit mode. Otherwise the buffer durations will be
+   * meaningless as frames are being dropped in-between without updating the
+   * durations. */
+  if (GST_CLOCK_TIME_IS_VALID (stop)
+      && !(sink->segment.flags & (GST_SEGMENT_FLAG_TRICKMODE |
+              GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS)) && stop != start)
     duration = stop - start;
   else
     duration = priv->avg_in_diff;