Test FrameBuffer::Clear and FrameBuffer::Stop

* Clearing while waiting for a frame should return a new frame
entering the buffer.
* Stopping while waiting for a frame should cancel the wait.

Bug: webrtc:13343
Change-Id: Ife9abfa8b6ea56141c9f32ff37d3b2a2e62a44f0
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/236849
Reviewed-by: Philip Eliasson <philipel@webrtc.org>
Commit-Queue: Evan Shrubsole <eshr@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35314}
diff --git a/modules/video_coding/frame_buffer2_unittest.cc b/modules/video_coding/frame_buffer2_unittest.cc
index 129c25f..e498afd 100644
--- a/modules/video_coding/frame_buffer2_unittest.cc
+++ b/modules/video_coding/frame_buffer2_unittest.cc
@@ -29,7 +29,9 @@
 #include "test/time_controller/simulated_time_controller.h"
 
 using ::testing::_;
+using ::testing::IsEmpty;
 using ::testing::Return;
+using ::testing::SizeIs;
 
 namespace webrtc {
 namespace video_coding {
@@ -259,6 +261,29 @@
   CheckFrame(0, pid, 0);
 }
 
+TEST_F(TestFrameBuffer2, ClearWhileWaitingForFrame) {
+  const uint16_t pid = Rand();
+
+  // Insert a frame and wait for it for max 100ms.
+  InsertFrame(pid, 0, 25, true, kFrameSize);
+  ExtractFrame(100);
+  // After 10ms, clear the buffer.
+  time_controller_.AdvanceTime(TimeDelta::Millis(10));
+  buffer_->Clear();
+  // Confirm that the frame was not sent for rendering.
+  time_controller_.AdvanceTime(TimeDelta::Millis(15));
+  EXPECT_THAT(frames_, IsEmpty());
+
+  // We are still waiting for a frame, since 100ms has not passed. Insert a new
+  // frame. This new frame should be the one that is returned as the old frame
+  // was cleared.
+  const uint16_t new_pid = pid + 1;
+  InsertFrame(new_pid, 0, 50, true, kFrameSize);
+  time_controller_.AdvanceTime(TimeDelta::Millis(25));
+  ASSERT_THAT(frames_, SizeIs(1));
+  CheckFrame(0, new_pid, 0);
+}
+
 TEST_F(TestFrameBuffer2, OneSuperFrame) {
   uint16_t pid = Rand();
   uint32_t ts = Rand();
@@ -663,5 +688,20 @@
   CheckFrame(2, pid + 4, 1);
 }
 
+TEST_F(TestFrameBuffer2, StopWhileWaitingForFrame) {
+  uint16_t pid = Rand();
+  uint32_t ts = Rand();
+
+  InsertFrame(pid, 0, ts, true, kFrameSize);
+  ExtractFrame(10);
+  buffer_->Stop();
+  time_controller_.AdvanceTime(TimeDelta::Millis(10));
+  EXPECT_THAT(frames_, IsEmpty());
+
+  // A new frame request should exit immediately and return no new frame.
+  ExtractFrame(0);
+  EXPECT_THAT(frames_, IsEmpty());
+}
+
 }  // namespace video_coding
 }  // namespace webrtc