Fixed bug in PacketBuffer to correctly detect new complete frames after ClearTo has been called.

BUG=webrtc:5514
R=stefan@webrtc.org, terelius@webrtc.org

Review URL: https://codereview.webrtc.org/2527903002 .

Cr-Commit-Position: refs/heads/master@{#15269}
diff --git a/webrtc/modules/video_coding/packet_buffer.cc b/webrtc/modules/video_coding/packet_buffer.cc
index 1b6fecc..c9d787b 100644
--- a/webrtc/modules/video_coding/packet_buffer.cc
+++ b/webrtc/modules/video_coding/packet_buffer.cc
@@ -172,16 +172,8 @@
     return false;
   if (sequence_buffer_[index].frame_created)
     return false;
-  if (sequence_buffer_[index].frame_begin &&
-      (!sequence_buffer_[prev_index].used ||
-       AheadOf(seq_num, sequence_buffer_[prev_index].seq_num))) {
-    // The reason we only return true if this packet is the first packet of the
-    // frame and the sequence number is newer than the packet with the previous
-    // index is because we want to avoid an inifite loop in the case where
-    // a single frame containing more packets than the current size of the
-    // packet buffer is inserted.
+  if (sequence_buffer_[index].frame_begin)
     return true;
-  }
   if (!sequence_buffer_[prev_index].used)
     return false;
   if (sequence_buffer_[prev_index].seq_num !=
@@ -197,7 +189,8 @@
 std::vector<std::unique_ptr<RtpFrameObject>> PacketBuffer::FindFrames(
     uint16_t seq_num) {
   std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
-  while (PotentialNewFrame(seq_num)) {
+  size_t packets_tested = 0;
+  while (packets_tested < size_ && PotentialNewFrame(seq_num)) {
     size_t index = seq_num % size_;
     sequence_buffer_[index].continuous = true;
 
@@ -229,6 +222,7 @@
                              max_nack_count, clock_->TimeInMilliseconds()));
     }
     ++seq_num;
+    ++packets_tested;
   }
   return found_frames;
 }
diff --git a/webrtc/modules/video_coding/video_packet_buffer_unittest.cc b/webrtc/modules/video_coding/video_packet_buffer_unittest.cc
index 6dc4e8f..a937af6 100644
--- a/webrtc/modules/video_coding/video_packet_buffer_unittest.cc
+++ b/webrtc/modules/video_coding/video_packet_buffer_unittest.cc
@@ -425,5 +425,18 @@
   EXPECT_FALSE(frames_from_callback_.begin()->second->GetBitstream(nullptr));
 }
 
+TEST_F(TestPacketBuffer, FramesAfterClear) {
+  Insert(9025, kDeltaFrame, kFirst, kLast);
+  Insert(9024, kKeyFrame, kFirst, kLast);
+  packet_buffer_->ClearTo(9025);
+  Insert(9057, kDeltaFrame, kFirst, kLast);
+  Insert(9026, kDeltaFrame, kFirst, kLast);
+
+  CheckFrame(9024);
+  CheckFrame(9025);
+  CheckFrame(9026);
+  CheckFrame(9057);
+}
+
 }  // namespace video_coding
 }  // namespace webrtc