From ed8bc7c04d8d01dab28eb0742a5cc4d7ec9657d4 Mon Sep 17 00:00:00 2001 From: Daniel Golle Date: Wed, 4 Nov 2026 02:03:11 +0200 Subject: [PATCH] [GStreamer] Detect live streams via the adaptive demuxer https://bugs.webkit.org/show_bug.cgi?id=330106 Reviewed by NOBODY (OOPS!). For an adaptive stream (DASH, HLS) the resource webkitwebsrc fetches is the manifest: a small, finite file whose HTTP transport properties carry no information about whether the media is live. m_isLiveStream was derived from those properties, in the Content-Length response-header handler, in totalBytes(), and from webKitWebSrc via setLiveStream(), so a live adaptive stream was classified as finite. duration() then returned zeroTime() where an unbounded live resource must be positiveInfiniteTime(), and the seek, playback-rate and buffering paths that read m_isLiveStream behaved as for a finite resource. The adaptive demuxer is the element that knows the stream is live, and it reports this by answering the pipeline duration query with TRUE and GST_CLOCK_TIME_NONE. Take the verdict from there: configureElement() records the presence of an adaptive demuxer, updateLiveStreamFromDuration() derives m_isLiveStream from the duration answer on preroll and on DURATION_CHANGED, and the HTTP-transport writers defer while an adaptive demuxer is present. A TRUE answer with a finite value yields a finite stream, so an ended event (MPD dynamic to static) reverts correctly. Progressive streams and WebM, where the transport heuristics are the right source, keep their current behaviour. * Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp: (WebCore::MediaPlayerPrivateGStreamer::load): (WebCore::MediaPlayerPrivateGStreamer::totalBytes const): (WebCore::MediaPlayerPrivateGStreamer::durationChanged): (WebCore::MediaPlayerPrivateGStreamer::updateLiveStreamFromDuration): (WebCore::MediaPlayerPrivateGStreamer::handleMessage): (WebCore::MediaPlayerPrivateGStreamer::configureElement): (WebCore::MediaPlayerPrivateGStreamer::didPreroll): * Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.h: (WebCore::MediaPlayerPrivateGStreamer::setLiveStream): --- .../gstreamer/MediaPlayerPrivateGStreamer.cpp | 37 +++++++++++++++++-- .../gstreamer/MediaPlayerPrivateGStreamer.h | 9 ++++- 2 files changed, 42 insertions(+), 4 deletions(-) --- a/Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp +++ b/Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp @@ -373,6 +373,8 @@ void MediaPlayerPrivateGStreamer::load(c return; } + m_hasAdaptiveDemuxer = false; + registerWebKitGStreamerElements(); if (!m_pipeline) @@ -1092,7 +1094,8 @@ unsigned long long MediaPlayerPrivateGSt if (gst_element_query_duration(m_source.get(), fmt, &length)) { GST_INFO_OBJECT(pipeline(), "totalBytes %" G_GINT64_FORMAT, length); m_totalBytes = static_cast(length); - m_isLiveStream = !length; + if (!m_hasAdaptiveDemuxer) + m_isLiveStream = !length; return m_totalBytes; } @@ -1126,7 +1129,8 @@ unsigned long long MediaPlayerPrivateGSt GST_INFO_OBJECT(pipeline(), "totalBytes %" G_GINT64_FORMAT, length); m_totalBytes = static_cast(length); - m_isLiveStream = !length; + if (!m_hasAdaptiveDemuxer) + m_isLiveStream = !length; return m_totalBytes; } @@ -1162,6 +1166,9 @@ void MediaPlayerPrivateGStreamer::durati MediaTime previousDuration = duration(); m_cachedDuration = MediaTime::invalidTime(); + if (m_hasAdaptiveDemuxer) + updateLiveStreamFromDuration(); + // Avoid emitting durationChanged in the case where the previous // duration was 0 because that case is already handled by the // HTMLMediaElement. @@ -1521,6 +1528,22 @@ MediaTime MediaPlayerPrivateGStreamer::p return MediaTime(duration, GST_SECOND); } +void MediaPlayerPrivateGStreamer::updateLiveStreamFromDuration() +{ + if (!m_pipeline || m_didErrorOccur) + return; + + int64_t duration = 0; + bool isLive = gst_element_query_duration(m_pipeline.get(), GST_FORMAT_TIME, &duration) && !GST_CLOCK_TIME_IS_VALID(duration); + if (m_isLiveStream && *m_isLiveStream == isLive) + return; + + GST_INFO_OBJECT(pipeline(), "Adaptive demuxer reports a %s stream", isLive ? "live" : "non-live"); + m_isLiveStream = isLive; + m_cachedDuration = MediaTime::invalidTime(); + updateDownloadBufferingFlag(); +} + bool MediaPlayerPrivateGStreamer::isMuted() const { bool isMuted = false; @@ -2442,7 +2465,7 @@ void MediaPlayerPrivateGStreamer::handle } } else contentLength = *contentLengthFromResponse; - if (!isRangeRequest) { + if (!isRangeRequest && !m_hasAdaptiveDemuxer) { m_isLiveStream = !contentLength; if (*m_isLiveStream && WEBKIT_IS_WEB_SRC(m_source.get()) && webKitSrcIsSeekable(WEBKIT_WEB_SRC_CAST(m_source.get()))) m_isLiveStream = false; @@ -2809,6 +2832,11 @@ void MediaPlayerPrivateGStreamer::config String elementClass(unsafeSpan(gst_element_get_metadata(element, GST_ELEMENT_METADATA_KLASS))); auto classifiers = elementClass.split('/'); + // An adaptive demuxer is the only element that knows whether the stream is live, so once one is + // plugged it owns the liveness verdict and the HTTP-transport heuristics must stop deciding it. + if (classifiers.contains("Adaptive"_s)) + m_hasAdaptiveDemuxer = true; + // In GStreamer 1.20 and older urisourcebin mishandles source elements with dynamic pads. This // is not an issue in 1.22. Streams parsing is not needed for MediaStream cases because we do it // upfront for incoming WebRTC MediaStreams. It is however needed for MSE, otherwise decodebin3 @@ -2988,6 +3016,9 @@ void MediaPlayerPrivateGStreamer::didPre // committed via doSeek()). if (m_isSeeking && !m_isSeekPending) finishSeek(); + + if (m_hasAdaptiveDemuxer) + updateLiveStreamFromDuration(); } void MediaPlayerPrivateGStreamer::updateStates() --- a/Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.h +++ b/Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.h @@ -258,7 +258,12 @@ public: return m_quirkStates.get(owner); } - void setLiveStream(bool isLiveStream) { m_isLiveStream = isLiveStream; } + void setLiveStream(bool isLiveStream) + { + if (m_hasAdaptiveDemuxer) + return; + m_isLiveStream = isLiveStream; + } bool requiresVideoSinkCapsNotifications() const; void videoSinkCapsChanged(GstPad*); @@ -379,8 +384,10 @@ protected: bool m_didErrorOccur { false }; mutable bool m_isEndReached { false }; mutable std::optional m_isLiveStream; + bool m_hasAdaptiveDemuxer { false }; bool isSeamlessSeekingEnabled() const; + void updateLiveStreamFromDuration(); void armInitialSegmentSeek(); bool m_isSegmentSeekAllowed { true };