diff --git a/Source/WebCore/platform/mediastream/RealtimeMediaSource.cpp b/Source/WebCore/platform/mediastream/RealtimeMediaSource.cpp index f894fef450c6..c3b89e455d2c 100644 --- a/Source/WebCore/platform/mediastream/RealtimeMediaSource.cpp +++ b/Source/WebCore/platform/mediastream/RealtimeMediaSource.cpp @@ -338,6 +338,26 @@ void RealtimeMediaSource::videoFrameAvailable(VideoFrame& videoFrame, VideoFrame updateHasStartedProducingData(); Locker locker { m_videoFrameObserversLock }; + if (m_videoFrameObservers.isEmpty()) { + if (m_pendingVideoFrames.size() < maxPendingVideoFramesBeforeAddTrack) { + m_pendingVideoFrames.append(PendingVideoFrame { &videoFrame, metadata }); + } + else { + WTFLogAlways("RealtimeMediaSource: Dropping video frame (queue is full) %zu frames", m_pendingVideoFrames.size()); + } + return; + } + if (!m_pendingVideoFrames.isEmpty()) { + WTFLogAlways("RealtimeMediaSource: Delivering %zu queued frame(s) (pipeline ready)", m_pendingVideoFrames.size()); + for (auto& pending : m_pendingVideoFrames) { + if (pending.frame) { + for (auto* obs : m_videoFrameObservers) + obs->videoFrameAvailable(*pending.frame, pending.metadata); + } + } + m_pendingVideoFrames.clear(); + } + for (auto& [key, value] : m_videoFrameObservers) { if (auto* adaptor = value.get()) { if (adaptor->frameDecimation > 1 && ++adaptor->frameDecimationCounter % adaptor->frameDecimation) diff --git a/Source/WebCore/platform/mediastream/RealtimeMediaSource.h b/Source/WebCore/platform/mediastream/RealtimeMediaSource.h index d5fbb8b487a0..be1fb0416602 100644 --- a/Source/WebCore/platform/mediastream/RealtimeMediaSource.h +++ b/Source/WebCore/platform/mediastream/RealtimeMediaSource.h @@ -391,6 +391,13 @@ class WEBCORE_EXPORT RealtimeMediaSource mutable Lock m_videoFrameObserversLock; HashMap> m_videoFrameObservers WTF_GUARDED_BY_LOCK(m_videoFrameObserversLock); + struct PendingVideoFrame { + RefPtr frame; + VideoFrameTimeMetadata metadata; + }; + static constexpr size_t maxPendingVideoFramesBeforeAddTrack = 30; + Vector m_pendingVideoFrames WTF_GUARDED_BY_LOCK(m_videoFrameObserversLock); + CaptureDevice m_device; // Set on the main thread from constraints.