diff --git a/modules/video_coding/frame_buffer2.cc b/modules/video_coding/frame_buffer2.cc index 8a5d56f72a..b041018bfd 100644 --- a/modules/video_coding/frame_buffer2.cc +++ b/modules/video_coding/frame_buffer2.cc @@ -319,12 +319,21 @@ int64_t FrameBuffer::InsertFrame(std::unique_ptr frame) { } if (num_frames_buffered_ >= kMaxFramesBuffered) { - RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" - << key.picture_id << ":" - << static_cast(key.spatial_layer) - << ") could not be inserted due to the frame " - << "buffer being full, dropping frame."; - return last_continuous_picture_id; + if (frame->is_keyframe()) { + RTC_LOG(LS_WARNING) << "Inserting keyframe (picture_id:spatial_id) (" + << key.picture_id << ":" + << static_cast(key.spatial_layer) + << ") but buffer is full, clearing" + << " buffer and inserting the frame."; + ClearFramesAndHistory(); + } else { + RTC_LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" + << key.picture_id << ":" + << static_cast(key.spatial_layer) + << ") could not be inserted due to the frame " + << "buffer being full, dropping frame."; + return last_continuous_picture_id; + } } if (last_decoded_frame_it_ != frames_.end() && diff --git a/modules/video_coding/frame_buffer2_unittest.cc b/modules/video_coding/frame_buffer2_unittest.cc index 807520c4ed..ffc02e845b 100644 --- a/modules/video_coding/frame_buffer2_unittest.cc +++ b/modules/video_coding/frame_buffer2_unittest.cc @@ -559,5 +559,20 @@ TEST_F(TestFrameBuffer2, KeyframeRequired) { CheckNoFrame(2); } +TEST_F(TestFrameBuffer2, KeyframeClearsFullBuffer) { + const int kMaxBufferSize = 600; + + for (int i = 1; i <= kMaxBufferSize; ++i) + EXPECT_EQ(-1, InsertFrame(i, 0, i * 1000, false, i - 1)); + ExtractFrame(); + CheckNoFrame(0); + + EXPECT_EQ( + kMaxBufferSize + 1, + InsertFrame(kMaxBufferSize + 1, 0, (kMaxBufferSize + 1) * 1000, false)); + ExtractFrame(); + CheckFrame(1, kMaxBufferSize + 1, 0); +} + } // namespace video_coding } // namespace webrtc