adaptivedemux2: Fix early seeking
authorEdward Hervey <edward@centricular.com>
Wed, 8 Feb 2023 11:02:45 +0000 (12:02 +0100)
committerEdward Hervey <bilboed@bilboed.com>
Fri, 23 Jun 2023 06:42:03 +0000 (08:42 +0200)
When seeking is handled by the collection posting thread, there is a possibility
that some leftover data will be pushed by the stream thread.

Properly detect and reject those early segments (and buffers) by comparing it to
the main segment seqnum

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/4922>

subprojects/gst-plugins-good/ext/adaptivedemux2/gstadaptivedemux-track.c

index b620772..d3fe0dd 100644 (file)
@@ -510,6 +510,18 @@ _track_sink_chain_function (GstPad * pad, GstObject * parent,
 
   TRACKS_LOCK (demux);
 
+  /* Discard buffers that are received outside of a valid segment. This can
+   * happen if a flushing seek (which resets the track segment seqnums) was
+   * received but the stream is still providing buffers before returning.
+   */
+  if (track->input_segment_seqnum == GST_SEQNUM_INVALID) {
+    GST_DEBUG_OBJECT (pad,
+        "Dropping buffer because we do not have a valid input segment");
+    gst_buffer_unref (buffer);
+    TRACKS_UNLOCK (demux);
+    return GST_FLOW_OK;
+  }
+
   ts = GST_BUFFER_DTS_OR_PTS (buffer);
 
   /* Buffers coming out of parsebin *should* always be timestamped (it's the
@@ -659,6 +671,14 @@ _track_sink_event_function (GstPad * pad, GstObject * parent, GstEvent * event)
         return TRUE;
       }
 
+      if (seg_seqnum != demux->priv->segment_seqnum) {
+        GST_DEBUG_OBJECT (pad, "Ignoring non-current segment");
+        gst_event_unref (event);
+        TRACKS_UNLOCK (demux);
+
+        return TRUE;
+      }
+
       track->input_segment_seqnum = seg_seqnum;
       gst_event_copy_segment (event, &track->input_segment);
       if (track->input_segment.rate >= 0)