diff --git a/docs/engine/gst-mse-patches.md b/docs/engine/gst-mse-patches.md index 5ed768f..276fdba 100644 --- a/docs/engine/gst-mse-patches.md +++ b/docs/engine/gst-mse-patches.md @@ -157,6 +157,18 @@ seek to 3.5 back to 1.0. The spec only raises the duration from coded frames. **Fix.** The handler returns when the current duration is valid and not below the new maximum. **Test:** mse-player smoke case `seekUnbuffered`. +## 12. Coded frames don't raise the duration (S028) + +**What.** `on_duration_changed()` took only the append pipeline's (demuxer's) duration. For fragmented MP4 with an +empty `moov` that is 0 or the first fragment's length, so after appending a 5 s clip `MediaSource.duration` read 1.0 +(Chromium: 5.08), and nothing re-ran the handler after later fragments. + +**Fix.** The handler also takes each source buffer's highest buffered end, and every successful append calls it +(outside the source buffer's object lock, before update-end). **Test:** C test `/mse/duration-from-frames`. +**Seen:** `/mse/split-audio-video` failed its final ready-state check (HAVE_METADATA after playing past 1 s) 2 times +in ~70 rounds after this patch, 0 in 24 before (not conclusive). msesrc reads its position from a pad's +`segment.position` (the feed's push position, not playback); Nacre's DOM computes readyState itself. + ## Found, not fixed here - `gst_source_buffer_change_content_type()` always fails (NotSupported). Nacre's backend accepts a change within the diff --git a/tests/gst-mse/mse-fixes.c b/tests/gst-mse/mse-fixes.c index d5af3e8..a7ba123 100644 --- a/tests/gst-mse/mse-fixes.c +++ b/tests/gst-mse/mse-fixes.c @@ -17,6 +17,9 @@ * in the pipeline and the next one crashed on a name clash) * /mse/reopen-append an append after endOfStream() reopens the source and completes (patch 10's * reopen took the appending buffer's tracks lock twice: a deadlock) + * /mse/duration-from-frames appended coded frames raise the duration to the buffered end (1.28.9 took + * only the demuxer's duration: 0 or the first fragment's for fragmented MP4); + * end of stream keeps the duration the page set * * Built and run by `tools/gst-mse.sh test` against target/gst-mse (GST_PLUGIN_PATH, own registry). * Clips in data/ (TESTS_DATA_DIR), made with ffmpeg: @@ -435,6 +438,11 @@ split_audio_video_round (void) g_test_message ("position after play: %" GST_TIME_FORMAT, GST_TIME_ARGS (pos)); g_assert_true (GST_CLOCK_TIME_IS_VALID (pos) && pos > GST_SECOND); g_object_get (p.src, "ready-state", &ready, NULL); + gint64 src_pos = -1; + gst_element_query_position (p.src, GST_FORMAT_TIME, &src_pos); + g_test_message ("ready state %d, msesrc position %" GST_TIME_FORMAT + ", duration %" GST_TIME_FORMAT, ready, GST_TIME_ARGS (src_pos), + GST_TIME_ARGS (gst_media_source_get_duration (p.source))); g_assert_cmpint (ready, >=, GST_MSE_SRC_READY_STATE_HAVE_CURRENT_DATA); g_assert_cmpint (cv.errors + ca.errors, ==, 0); @@ -512,6 +520,42 @@ test_reopen_append (void) counter_clear (&c); } +static void +test_duration_from_frames (void) +{ + Lone lone; + Counter c; + counter_init (&c); + lone_setup (&lone, AUDIO_TYPE); + watch (lone.buffer, &c); + + g_assert_true (gst_source_buffer_append_buffer (lone.buffer, + clip ("audio-aac.mp4"), NULL)); + g_assert_true (wait_update_ends (&c, 1, 10)); + guint n; + gdouble span = buffered_span (lone.buffer, &n); + GstClockTime duration = gst_media_source_get_duration (lone.source); + g_test_message ("buffered %.3f s, duration %" GST_TIME_FORMAT, span, + GST_TIME_ARGS (duration)); + /* Coded frames raise the duration to the highest end timestamp. */ + g_assert_true (GST_CLOCK_TIME_IS_VALID (duration)); + g_assert_cmpfloat ((gdouble) duration / GST_SECOND, >=, span - 0.001); + + /* End of stream keeps the duration the page set (the buffered end). */ + GstClockTime end = (GstClockTime) (span * GST_SECOND); + g_assert_true (gst_media_source_set_duration (lone.source, end, NULL)); + GError *error = NULL; + g_assert_true (gst_media_source_end_of_stream (lone.source, + GST_MEDIA_SOURCE_EOS_ERROR_NONE, &error)); + g_assert_no_error (error); + duration = gst_media_source_get_duration (lone.source); + g_test_message ("after end of stream %" GST_TIME_FORMAT, GST_TIME_ARGS (duration)); + g_assert_cmpuint (duration, ==, end); + + lone_teardown (&lone); + counter_clear (&c); +} + int main (int argc, char **argv) { @@ -522,6 +566,7 @@ main (int argc, char **argv) g_test_add_func ("/mse/remove", test_remove); g_test_add_func ("/mse/abort-then-append", test_abort_then_append); g_test_add_func ("/mse/reopen-append", test_reopen_append); + g_test_add_func ("/mse/duration-from-frames", test_duration_from_frames); g_test_add_func ("/mse/split-audio-video", test_split_audio_video); return g_test_run (); } diff --git a/vendor/gst-mse/gst-libs/gst/mse/gstmediasource.c b/vendor/gst-mse/gst-libs/gst/mse/gstmediasource.c index cc58449..1cfcb4b 100644 --- a/vendor/gst-mse/gst-libs/gst/mse/gstmediasource.c +++ b/vendor/gst-mse/gst-libs/gst/mse/gstmediasource.c @@ -770,6 +770,14 @@ on_duration_changed (G_GNUC_UNUSED GstSourceBuffer * source_buffer, if (GST_CLOCK_TIME_IS_VALID (duration)) { max = MAX (max, duration); } + /* Nacre patch: the demuxer's duration is 0 or the first fragment's for + * fragmented MP4 (empty moov); the buffered end counts too. */ + GArray *ranges = gst_source_buffer_get_buffered (buf, NULL); + if (ranges != NULL && ranges->len > 0) { + max = MAX (max, g_array_index (ranges, GstMediaSourceRange, + ranges->len - 1).end); + } + g_clear_pointer (&ranges, g_array_unref); gst_object_unref (buf); } /* Nacre patch: coded frames only extend the duration (MSE coded frame diff --git a/vendor/gst-mse/gst-libs/gst/mse/gstsourcebuffer.c b/vendor/gst-mse/gst-libs/gst/mse/gstsourcebuffer.c index 503e8ac..5c8b8b0 100644 --- a/vendor/gst-mse/gst-libs/gst/mse/gstsourcebuffer.c +++ b/vendor/gst-mse/gst-libs/gst/mse/gstsourcebuffer.c @@ -1846,6 +1846,13 @@ append_to_buffer_task_func (AppendToBufferTask * task) goto done; } + /* Nacre patch: coded frame processing raises the duration to the highest + * end timestamp, before update-end. The media source reads every buffer's + * ranges, so call it outside this object's lock; `updating` is still set. */ + GST_OBJECT_UNLOCK (self); + call_duration_changed (self); + GST_OBJECT_LOCK (self); + append_successful_unlocked (self, is_ended_unlocked (self)); done: diff --git a/vendor/gst-mse/nacre.patch b/vendor/gst-mse/nacre.patch index fc1dd85..a5bbd49 100644 --- a/vendor/gst-mse/nacre.patch +++ b/vendor/gst-mse/nacre.patch @@ -582,3 +582,43 @@ index 34e0f50..503e8ac 100644 } GstMediaSource *source = get_media_source_unlocked (self); + +S028 patch 12 (coded frames raise the duration to the buffered end): + +diff --git a/gst-libs/gst/mse/gstmediasource.c b/gst-libs/gst/mse/gstmediasource.c +index cc58449..1cfcb4b 100644 +--- a/gst-libs/gst/mse/gstmediasource.c ++++ b/gst-libs/gst/mse/gstmediasource.c +@@ -770,6 +770,14 @@ on_duration_changed (G_GNUC_UNUSED GstSourceBuffer * source_buffer, + if (GST_CLOCK_TIME_IS_VALID (duration)) { + max = MAX (max, duration); + } ++ /* Nacre patch: the demuxer's duration is 0 or the first fragment's for ++ * fragmented MP4 (empty moov); the buffered end counts too. */ ++ GArray *ranges = gst_source_buffer_get_buffered (buf, NULL); ++ if (ranges != NULL && ranges->len > 0) { ++ max = MAX (max, g_array_index (ranges, GstMediaSourceRange, ++ ranges->len - 1).end); ++ } ++ g_clear_pointer (&ranges, g_array_unref); + gst_object_unref (buf); + } + /* Nacre patch: coded frames only extend the duration (MSE coded frame +diff --git a/gst-libs/gst/mse/gstsourcebuffer.c b/gst-libs/gst/mse/gstsourcebuffer.c +index 503e8ac..5c8b8b0 100644 +--- a/gst-libs/gst/mse/gstsourcebuffer.c ++++ b/gst-libs/gst/mse/gstsourcebuffer.c +@@ -1846,6 +1846,13 @@ append_to_buffer_task_func (AppendToBufferTask * task) + goto done; + } + ++ /* Nacre patch: coded frame processing raises the duration to the highest ++ * end timestamp, before update-end. The media source reads every buffer's ++ * ranges, so call it outside this object's lock; `updating` is still set. */ ++ GST_OBJECT_UNLOCK (self); ++ call_duration_changed (self); ++ GST_OBJECT_LOCK (self); ++ + append_successful_unlocked (self, is_ended_unlocked (self)); + + done: