RtpFrameObject now takes an EncodedImageBuffer in its ctor.

Bug: webrtc:10979
Change-Id: Ibc8b4a524ca95b5faa8850a41df8f2f0136a2969
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/153666
Commit-Queue: Philip Eliasson <philipel@webrtc.org>
Reviewed-by: Sam Zackrisson <saza@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29251}
diff --git a/modules/video_coding/packet_buffer.cc b/modules/video_coding/packet_buffer.cc
index ea38ca6..b4de82f 100644
--- a/modules/video_coding/packet_buffer.cc
+++ b/modules/video_coding/packet_buffer.cc
@@ -18,7 +18,6 @@
 
 #include "absl/types/variant.h"
 #include "api/video/encoded_frame.h"
-#include "api/video/encoded_image.h"
 #include "common_video/h264/h264_common.h"
 #include "modules/rtp_rtcp/source/rtp_video_header.h"
 #include "modules/video_coding/codecs/h264/include/h264_globals.h"
@@ -439,10 +438,9 @@
 
       auto frame = std::make_unique<RtpFrameObject>(
           this, start_seq_num, seq_num, frame_size, max_nack_count,
-          min_recv_time, max_recv_time,
-          RtpPacketInfos(std::move(packet_infos)));
-      frame->SetEncodedData(EncodedImageBuffer::Create(frame_size));
-      GetBitstream(*frame, frame->data());
+          min_recv_time, max_recv_time, RtpPacketInfos(std::move(packet_infos)),
+          GetEncodedImageBuffer(frame_size, start_seq_num, seq_num));
+
       found_frames.emplace_back(std::move(frame));
 
       ClearInterval(start_seq_num, seq_num);
@@ -452,43 +450,28 @@
   return found_frames;
 }
 
-// TODO(philipel): Update function to not accept an RtpFrameObject.
-bool PacketBuffer::GetBitstream(const RtpFrameObject& frame,
-                                uint8_t* destination) {
-  rtc::CritScope lock(&crit_);
+rtc::scoped_refptr<EncodedImageBuffer> PacketBuffer::GetEncodedImageBuffer(
+    size_t frame_size,
+    uint16_t first_seq_num,
+    uint16_t last_seq_num) {
+  size_t index = first_seq_num % size_;
+  size_t end = (last_seq_num + 1) % size_;
 
-  size_t index = frame.first_seq_num() % size_;
-  size_t end = (frame.last_seq_num() + 1) % size_;
-  uint16_t seq_num = frame.first_seq_num();
-  uint32_t timestamp = frame.Timestamp();
-  uint8_t* destination_end = destination + frame.size();
+  auto buffer = EncodedImageBuffer::Create(frame_size);
+  size_t offset = 0;
 
   do {
-    // Check both seq_num and timestamp to handle the case when seq_num wraps
-    // around too quickly for high packet rates.
-    if (!sequence_buffer_[index].used ||
-        sequence_buffer_[index].seq_num != seq_num ||
-        data_buffer_[index].timestamp != timestamp) {
-      return false;
-    }
+    RTC_DCHECK(sequence_buffer_[index].used);
 
-    RTC_DCHECK_EQ(data_buffer_[index].seqNum, sequence_buffer_[index].seq_num);
     size_t length = data_buffer_[index].sizeBytes;
-    if (destination + length > destination_end) {
-      RTC_LOG(LS_WARNING) << "Frame (" << frame.id.picture_id << ":"
-                          << static_cast<int>(frame.id.spatial_layer) << ")"
-                          << " bitstream buffer is not large enough.";
-      return false;
-    }
+    RTC_CHECK_LE(offset + length, buffer->size());
+    memcpy(buffer->data() + offset, data_buffer_[index].dataPtr, length);
+    offset += length;
 
-    const uint8_t* source = data_buffer_[index].dataPtr;
-    memcpy(destination, source, length);
-    destination += length;
     index = (index + 1) % size_;
-    ++seq_num;
   } while (index != end);
 
-  return true;
+  return buffer;
 }
 
 VCMPacket* PacketBuffer::GetPacket(uint16_t seq_num) {