blob: 53f289c79c876f24769a728623f362cd119ee42d [file] [log] [blame]
philipelc707ab72016-04-01 02:01:54 -07001/*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/packet_buffer.h"
philipelc707ab72016-04-01 02:01:54 -070012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
philipelc707ab72016-04-01 02:01:54 -070015#include <algorithm>
Yves Gerey3e707812018-11-28 16:47:49 +010016#include <cstdint>
Danil Chapovalov3527a4f2019-11-08 17:30:29 +010017#include <limits>
philipel17deeb42016-08-11 15:09:26 +020018#include <utility>
Danil Chapovalov3527a4f2019-11-08 17:30:29 +010019#include <vector>
philipelc707ab72016-04-01 02:01:54 -070020
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "absl/types/variant.h"
Danil Chapovalov3527a4f2019-11-08 17:30:29 +010022#include "api/array_view.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "api/video/encoded_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "common_video/h264/h264_common.h"
Yves Gerey3e707812018-11-28 16:47:49 +010025#include "modules/rtp_rtcp/source/rtp_video_header.h"
26#include "modules/video_coding/codecs/h264/include/h264_globals.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/video_coding/frame_object.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/checks.h"
29#include "rtc_base/logging.h"
Yves Gerey3e707812018-11-28 16:47:49 +010030#include "rtc_base/numerics/mod_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "system_wrappers/include/clock.h"
Rasmus Brandt88f080a2017-11-02 14:28:06 +010032#include "system_wrappers/include/field_trial.h"
philipelc707ab72016-04-01 02:01:54 -070033
34namespace webrtc {
35namespace video_coding {
36
philipelb4d31082016-07-11 08:46:29 -070037PacketBuffer::PacketBuffer(Clock* clock,
38 size_t start_buffer_size,
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020039 size_t max_buffer_size)
philipelb4d31082016-07-11 08:46:29 -070040 : clock_(clock),
philipelc707ab72016-04-01 02:01:54 -070041 max_size_(max_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070042 first_seq_num_(0),
philipelf4139332016-04-20 10:26:34 +020043 first_packet_received_(false),
philipelaee3e0e2016-11-01 11:45:34 +010044 is_cleared_to_first_seq_num_(false),
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020045 buffer_(start_buffer_size),
Rasmus Brandt88f080a2017-11-02 14:28:06 +010046 sps_pps_idr_is_h264_keyframe_(
47 field_trial::IsEnabled("WebRTC-SpsPpsIdrIsH264Keyframe")) {
philipelc707ab72016-04-01 02:01:54 -070048 RTC_DCHECK_LE(start_buffer_size, max_buffer_size);
49 // Buffer size must always be a power of 2.
50 RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0);
51 RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0);
52}
53
philipelaee3e0e2016-11-01 11:45:34 +010054PacketBuffer::~PacketBuffer() {
55 Clear();
56}
philipel17deeb42016-08-11 15:09:26 +020057
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020058PacketBuffer::InsertResult PacketBuffer::InsertPacket(VCMPacket* packet) {
59 PacketBuffer::InsertResult result;
60 rtc::CritScope lock(&crit_);
philipel3184f8e2017-05-18 08:08:53 -070061
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020062 uint16_t seq_num = packet->seqNum;
63 size_t index = seq_num % buffer_.size();
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010064
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020065 if (!first_packet_received_) {
66 first_seq_num_ = seq_num;
67 first_packet_received_ = true;
68 } else if (AheadOf(first_seq_num_, seq_num)) {
69 // If we have explicitly cleared past this packet then it's old,
70 // don't insert it, just silently ignore it.
71 if (is_cleared_to_first_seq_num_) {
72 delete[] packet->dataPtr;
73 packet->dataPtr = nullptr;
74 return result;
philipelc707ab72016-04-01 02:01:54 -070075 }
philipelc707ab72016-04-01 02:01:54 -070076
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020077 first_seq_num_ = seq_num;
philipelc707ab72016-04-01 02:01:54 -070078 }
79
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020080 if (buffer_[index].used) {
81 // Duplicate packet, just delete the payload.
82 if (buffer_[index].seq_num() == packet->seqNum) {
83 delete[] packet->dataPtr;
84 packet->dataPtr = nullptr;
85 return result;
86 }
philipelc707ab72016-04-01 02:01:54 -070087
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020088 // The packet buffer is full, try to expand the buffer.
89 while (ExpandBufferSize() && buffer_[seq_num % buffer_.size()].used) {
90 }
91 index = seq_num % buffer_.size();
92
93 // Packet buffer is still full since we were unable to expand the buffer.
94 if (buffer_[index].used) {
95 // Clear the buffer, delete payload, and return false to signal that a
96 // new keyframe is needed.
97 RTC_LOG(LS_WARNING) << "Clear PacketBuffer and request key frame.";
98 Clear();
99 delete[] packet->dataPtr;
100 packet->dataPtr = nullptr;
101 result.buffer_cleared = true;
102 return result;
103 }
104 }
105
106 StoredPacket& new_entry = buffer_[index];
107 new_entry.continuous = false;
108 new_entry.used = true;
109 new_entry.data = *packet;
110 packet->dataPtr = nullptr;
111
112 UpdateMissingPackets(packet->seqNum);
113
114 int64_t now_ms = clock_->TimeInMilliseconds();
115 last_received_packet_ms_ = now_ms;
116 if (packet->video_header.frame_type == VideoFrameType::kVideoFrameKey)
117 last_received_keyframe_packet_ms_ = now_ms;
118
119 result.frames = FindFrames(seq_num);
120 return result;
philipelc707ab72016-04-01 02:01:54 -0700121}
122
123void PacketBuffer::ClearTo(uint16_t seq_num) {
124 rtc::CritScope lock(&crit_);
philipelc5fb4682017-08-02 04:28:57 -0700125 // We have already cleared past this sequence number, no need to do anything.
126 if (is_cleared_to_first_seq_num_ &&
127 AheadOf<uint16_t>(first_seq_num_, seq_num)) {
128 return;
129 }
philipelaee3e0e2016-11-01 11:45:34 +0100130
131 // If the packet buffer was cleared between a frame was created and returned.
132 if (!first_packet_received_)
133 return;
134
philipelc5fb4682017-08-02 04:28:57 -0700135 // Avoid iterating over the buffer more than once by capping the number of
136 // iterations to the |size_| of the buffer.
137 ++seq_num;
138 size_t diff = ForwardDiff<uint16_t>(first_seq_num_, seq_num);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200139 size_t iterations = std::min(diff, buffer_.size());
philipelc5fb4682017-08-02 04:28:57 -0700140 for (size_t i = 0; i < iterations; ++i) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200141 size_t index = first_seq_num_ % buffer_.size();
142 if (AheadOf<uint16_t>(seq_num, buffer_[index].seq_num())) {
143 delete[] buffer_[index].data.dataPtr;
144 buffer_[index].data.dataPtr = nullptr;
145 buffer_[index].used = false;
philipelc5fb4682017-08-02 04:28:57 -0700146 }
philipelaee3e0e2016-11-01 11:45:34 +0100147 ++first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700148 }
philipel2c9f9f22017-06-13 02:47:28 -0700149
philipelc5fb4682017-08-02 04:28:57 -0700150 // If |diff| is larger than |iterations| it means that we don't increment
151 // |first_seq_num_| until we reach |seq_num|, so we set it here.
152 first_seq_num_ = seq_num;
153
154 is_cleared_to_first_seq_num_ = true;
philipelbc5a4082017-12-06 10:41:08 +0100155 auto clear_to_it = missing_packets_.upper_bound(seq_num);
156 if (clear_to_it != missing_packets_.begin()) {
157 --clear_to_it;
158 missing_packets_.erase(missing_packets_.begin(), clear_to_it);
159 }
philipelc707ab72016-04-01 02:01:54 -0700160}
161
Johannes Krona3705562019-08-26 16:37:11 +0200162void PacketBuffer::ClearInterval(uint16_t start_seq_num,
163 uint16_t stop_seq_num) {
164 size_t iterations = ForwardDiff<uint16_t>(start_seq_num, stop_seq_num + 1);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200165 RTC_DCHECK_LE(iterations, buffer_.size());
Johannes Krona3705562019-08-26 16:37:11 +0200166 uint16_t seq_num = start_seq_num;
167 for (size_t i = 0; i < iterations; ++i) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200168 size_t index = seq_num % buffer_.size();
169 RTC_DCHECK_EQ(buffer_[index].seq_num(), seq_num);
170 delete[] buffer_[index].data.dataPtr;
171 buffer_[index].data.dataPtr = nullptr;
172 buffer_[index].used = false;
Johannes Krona3705562019-08-26 16:37:11 +0200173
174 ++seq_num;
175 }
176}
177
philipelaee3e0e2016-11-01 11:45:34 +0100178void PacketBuffer::Clear() {
179 rtc::CritScope lock(&crit_);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200180 for (StoredPacket& entry : buffer_) {
181 delete[] entry.data.dataPtr;
182 entry.data.dataPtr = nullptr;
183 entry.used = false;
philipelaee3e0e2016-11-01 11:45:34 +0100184 }
185
186 first_packet_received_ = false;
187 is_cleared_to_first_seq_num_ = false;
philipel2c9f9f22017-06-13 02:47:28 -0700188 last_received_packet_ms_.reset();
189 last_received_keyframe_packet_ms_.reset();
190 newest_inserted_seq_num_.reset();
191 missing_packets_.clear();
192}
193
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200194PacketBuffer::InsertResult PacketBuffer::InsertPadding(uint16_t seq_num) {
195 PacketBuffer::InsertResult result;
196 rtc::CritScope lock(&crit_);
197 UpdateMissingPackets(seq_num);
198 result.frames = FindFrames(static_cast<uint16_t>(seq_num + 1));
199 return result;
philipel3184f8e2017-05-18 08:08:53 -0700200}
201
Danil Chapovalov0040b662018-06-18 10:48:16 +0200202absl::optional<int64_t> PacketBuffer::LastReceivedPacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700203 rtc::CritScope lock(&crit_);
204 return last_received_packet_ms_;
205}
206
Danil Chapovalov0040b662018-06-18 10:48:16 +0200207absl::optional<int64_t> PacketBuffer::LastReceivedKeyframePacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700208 rtc::CritScope lock(&crit_);
209 return last_received_keyframe_packet_ms_;
philipelaee3e0e2016-11-01 11:45:34 +0100210}
211
philipelc707ab72016-04-01 02:01:54 -0700212bool PacketBuffer::ExpandBufferSize() {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200213 if (buffer_.size() == max_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100214 RTC_LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
Johannes Kronbd3f3052019-08-01 15:45:54 +0200215 << "), failed to increase size.";
philipelc707ab72016-04-01 02:01:54 -0700216 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100217 }
philipelc707ab72016-04-01 02:01:54 -0700218
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200219 size_t new_size = std::min(max_size_, 2 * buffer_.size());
220 std::vector<StoredPacket> new_buffer(new_size);
221 for (StoredPacket& entry : buffer_) {
222 if (entry.used) {
223 new_buffer[entry.seq_num() % new_size] = entry;
philipelc707ab72016-04-01 02:01:54 -0700224 }
225 }
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200226 buffer_ = std::move(new_buffer);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100227 RTC_LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700228 return true;
229}
230
philipelaee3e0e2016-11-01 11:45:34 +0100231bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200232 size_t index = seq_num % buffer_.size();
233 int prev_index = index > 0 ? index - 1 : buffer_.size() - 1;
234 const StoredPacket& entry = buffer_[index];
235 const StoredPacket& prev_entry = buffer_[prev_index];
philipelf4139332016-04-20 10:26:34 +0200236
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200237 if (!entry.used)
philipelc707ab72016-04-01 02:01:54 -0700238 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200239 if (entry.seq_num() != seq_num)
philipel2c9f9f22017-06-13 02:47:28 -0700240 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200241 if (entry.frame_begin())
philipelc707ab72016-04-01 02:01:54 -0700242 return true;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200243 if (!prev_entry.used)
philipelc707ab72016-04-01 02:01:54 -0700244 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200245 if (prev_entry.seq_num() != static_cast<uint16_t>(entry.seq_num() - 1))
philipelea142f82017-01-11 02:01:56 -0800246 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200247 if (prev_entry.data.timestamp != entry.data.timestamp)
philipelf4139332016-04-20 10:26:34 +0200248 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200249 if (prev_entry.continuous)
philipelc707ab72016-04-01 02:01:54 -0700250 return true;
251
252 return false;
253}
254
philipelfd5a20f2016-11-15 00:57:57 -0800255std::vector<std::unique_ptr<RtpFrameObject>> PacketBuffer::FindFrames(
256 uint16_t seq_num) {
257 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200258 for (size_t i = 0; i < buffer_.size() && PotentialNewFrame(seq_num); ++i) {
259 size_t index = seq_num % buffer_.size();
260 buffer_[index].continuous = true;
philipelc707ab72016-04-01 02:01:54 -0700261
philipelf4139332016-04-20 10:26:34 +0200262 // If all packets of the frame is continuous, find the first packet of the
263 // frame and create an RtpFrameObject.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200264 if (buffer_[index].frame_end()) {
philipelc707ab72016-04-01 02:01:54 -0700265 uint16_t start_seq_num = seq_num;
philipelf4139332016-04-20 10:26:34 +0200266
philipel5ceaaae2016-05-24 10:20:47 +0200267 // Find the start index by searching backward until the packet with
268 // the |frame_begin| flag is set.
269 int start_index = index;
philipel227f8b92017-08-04 06:39:31 -0700270 size_t tested_packets = 0;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200271 int64_t frame_timestamp = buffer_[start_index].data.timestamp;
philipel53910712017-05-18 02:24:40 -0700272
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100273 // Identify H.264 keyframes by means of SPS, PPS, and IDR.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200274 bool is_h264 = buffer_[start_index].data.codec() == kVideoCodecH264;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100275 bool has_h264_sps = false;
276 bool has_h264_pps = false;
277 bool has_h264_idr = false;
278 bool is_h264_keyframe = false;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700279 int idr_width = -1;
280 int idr_height = -1;
philipel227f8b92017-08-04 06:39:31 -0700281 while (true) {
282 ++tested_packets;
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100283
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200284 if (!is_h264 && buffer_[start_index].frame_begin())
philipel5ceaaae2016-05-24 10:20:47 +0200285 break;
286
Shyam Sadhwani2b84dad2019-10-02 17:22:33 -0700287 if (is_h264) {
philipel7d745e52018-08-02 14:03:53 +0200288 const auto* h264_header = absl::get_if<RTPVideoHeaderH264>(
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200289 &buffer_[start_index].data.video_header.video_type_header);
philipel7d745e52018-08-02 14:03:53 +0200290 if (!h264_header || h264_header->nalus_length >= kMaxNalusPerPacket)
philipel09133af2018-05-17 14:11:09 +0200291 return found_frames;
292
philipel7d745e52018-08-02 14:03:53 +0200293 for (size_t j = 0; j < h264_header->nalus_length; ++j) {
294 if (h264_header->nalus[j].type == H264::NaluType::kSps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100295 has_h264_sps = true;
philipel7d745e52018-08-02 14:03:53 +0200296 } else if (h264_header->nalus[j].type == H264::NaluType::kPps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100297 has_h264_pps = true;
philipel7d745e52018-08-02 14:03:53 +0200298 } else if (h264_header->nalus[j].type == H264::NaluType::kIdr) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100299 has_h264_idr = true;
philipel2c9f9f22017-06-13 02:47:28 -0700300 }
301 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100302 if ((sps_pps_idr_is_h264_keyframe_ && has_h264_idr && has_h264_sps &&
303 has_h264_pps) ||
304 (!sps_pps_idr_is_h264_keyframe_ && has_h264_idr)) {
305 is_h264_keyframe = true;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700306 // Store the resolution of key frame which is the packet with
307 // smallest index and valid resolution; typically its IDR or SPS
308 // packet; there may be packet preceeding this packet, IDR's
309 // resolution will be applied to them.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200310 if (buffer_[start_index].data.width() > 0 &&
311 buffer_[start_index].data.height() > 0) {
312 idr_width = buffer_[start_index].data.width();
313 idr_height = buffer_[start_index].data.height();
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700314 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100315 }
philipel2c9f9f22017-06-13 02:47:28 -0700316 }
317
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200318 if (tested_packets == buffer_.size())
philipel227f8b92017-08-04 06:39:31 -0700319 break;
320
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200321 start_index = start_index > 0 ? start_index - 1 : buffer_.size() - 1;
philipel8c619242017-02-02 08:51:29 -0800322
323 // In the case of H264 we don't have a frame_begin bit (yes,
324 // |frame_begin| might be set to true but that is a lie). So instead
325 // we traverese backwards as long as we have a previous packet and
326 // the timestamp of that packet is the same as this one. This may cause
327 // the PacketBuffer to hand out incomplete frames.
328 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
philipel53910712017-05-18 02:24:40 -0700329 if (is_h264 &&
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200330 (!buffer_[start_index].used ||
331 buffer_[start_index].data.timestamp != frame_timestamp)) {
philipel8c619242017-02-02 08:51:29 -0800332 break;
333 }
334
335 --start_seq_num;
philipelc707ab72016-04-01 02:01:54 -0700336 }
337
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100338 if (is_h264) {
339 // Warn if this is an unsafe frame.
340 if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) {
Jonas Olssonfc501102018-06-15 14:24:10 +0200341 RTC_LOG(LS_WARNING)
342 << "Received H.264-IDR frame "
343 << "(SPS: " << has_h264_sps << ", PPS: " << has_h264_pps
344 << "). Treating as "
345 << (sps_pps_idr_is_h264_keyframe_ ? "delta" : "key")
346 << " frame since WebRTC-SpsPpsIdrIsH264Keyframe is "
347 << (sps_pps_idr_is_h264_keyframe_ ? "enabled." : "disabled");
philipel2c9f9f22017-06-13 02:47:28 -0700348 }
349
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100350 // Now that we have decided whether to treat this frame as a key frame
351 // or delta frame in the frame buffer, we update the field that
352 // determines if the RtpFrameObject is a key frame or delta frame.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200353 const size_t first_packet_index = start_seq_num % buffer_.size();
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100354 if (is_h264_keyframe) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200355 buffer_[first_packet_index].data.video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100356 VideoFrameType::kVideoFrameKey;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700357 if (idr_width > 0 && idr_height > 0) {
358 // IDR frame was finalized and we have the correct resolution for
359 // IDR; update first packet to have same resolution as IDR.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200360 buffer_[first_packet_index].data.video_header.width = idr_width;
361 buffer_[first_packet_index].data.video_header.height = idr_height;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700362 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100363 } else {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200364 buffer_[first_packet_index].data.video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100365 VideoFrameType::kVideoFrameDelta;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100366 }
367
Johnny Leebc7f41b2019-05-01 14:41:32 -0400368 // With IPPP, if this is not a keyframe, make sure there are no gaps
369 // in the packet sequence numbers up until this point.
370 const uint8_t h264tid =
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200371 buffer_[start_index].data.video_header.frame_marking.temporal_id;
Jonas Olssona4d87372019-07-05 19:08:33 +0200372 if (h264tid == kNoTemporalIdx && !is_h264_keyframe &&
373 missing_packets_.upper_bound(start_seq_num) !=
374 missing_packets_.begin()) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100375 return found_frames;
376 }
philipel2c9f9f22017-06-13 02:47:28 -0700377 }
378
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100379 found_frames.push_back(AssembleFrame(start_seq_num, seq_num));
380
philipel2c9f9f22017-06-13 02:47:28 -0700381 missing_packets_.erase(missing_packets_.begin(),
382 missing_packets_.upper_bound(seq_num));
Johannes Krona3705562019-08-26 16:37:11 +0200383 ClearInterval(start_seq_num, seq_num);
philipelc707ab72016-04-01 02:01:54 -0700384 }
philipelc707ab72016-04-01 02:01:54 -0700385 ++seq_num;
386 }
philipelfd5a20f2016-11-15 00:57:57 -0800387 return found_frames;
philipelc707ab72016-04-01 02:01:54 -0700388}
389
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100390std::unique_ptr<RtpFrameObject> PacketBuffer::AssembleFrame(
philipelb5e47852019-09-20 11:30:12 +0200391 uint16_t first_seq_num,
392 uint16_t last_seq_num) {
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100393 const uint16_t end_seq_num = last_seq_num + 1;
394 const uint16_t num_packets = end_seq_num - first_seq_num;
395 int max_nack_count = -1;
396 int64_t min_recv_time = std::numeric_limits<int64_t>::max();
397 int64_t max_recv_time = std::numeric_limits<int64_t>::min();
398 size_t frame_size = 0;
philipelc707ab72016-04-01 02:01:54 -0700399
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100400 std::vector<rtc::ArrayView<const uint8_t>> payloads;
401 RtpPacketInfos::vector_type packet_infos;
402 payloads.reserve(num_packets);
403 packet_infos.reserve(num_packets);
philipel227f8b92017-08-04 06:39:31 -0700404
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100405 for (uint16_t seq_num = first_seq_num; seq_num != end_seq_num; ++seq_num) {
406 const VCMPacket& packet = GetPacket(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700407
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100408 max_nack_count = std::max(max_nack_count, packet.timesNacked);
409 min_recv_time =
410 std::min(min_recv_time, packet.packet_info.receive_time_ms());
411 max_recv_time =
412 std::max(max_recv_time, packet.packet_info.receive_time_ms());
413 frame_size += packet.sizeBytes;
414 payloads.emplace_back(packet.dataPtr, packet.sizeBytes);
415 packet_infos.push_back(packet.packet_info);
416 }
philipel227f8b92017-08-04 06:39:31 -0700417
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100418 auto bitstream = EncodedImageBuffer::Create(frame_size);
philipel227f8b92017-08-04 06:39:31 -0700419
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100420 uint8_t* write_at = bitstream->data();
421 for (rtc::ArrayView<const uint8_t> payload : payloads) {
422 memcpy(write_at, payload.data(), payload.size());
423 write_at += payload.size();
424 }
425 RTC_DCHECK_EQ(write_at - bitstream->data(), bitstream->size());
426
427 const VCMPacket& first_packet = GetPacket(first_seq_num);
428 const VCMPacket& last_packet = GetPacket(last_seq_num);
429 return std::make_unique<RtpFrameObject>(
430 first_seq_num, //
431 last_seq_num, //
432 last_packet.markerBit, //
433 max_nack_count, //
434 min_recv_time, //
435 max_recv_time, //
436 first_packet.timestamp, //
437 first_packet.ntp_time_ms_, //
438 last_packet.video_header.video_timing, //
439 first_packet.payloadType, //
440 first_packet.codec(), //
441 last_packet.video_header.rotation, //
442 last_packet.video_header.content_type, //
443 first_packet.video_header, //
444 last_packet.video_header.color_space, //
445 first_packet.generic_descriptor, //
446 RtpPacketInfos(std::move(packet_infos)), //
447 std::move(bitstream));
philipelc707ab72016-04-01 02:01:54 -0700448}
449
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100450const VCMPacket& PacketBuffer::GetPacket(uint16_t seq_num) const {
451 const StoredPacket& entry = buffer_[seq_num % buffer_.size()];
452 RTC_DCHECK(entry.used);
453 RTC_DCHECK_EQ(seq_num, entry.seq_num());
454 return entry.data;
philipelf4139332016-04-20 10:26:34 +0200455}
456
philipel2c9f9f22017-06-13 02:47:28 -0700457void PacketBuffer::UpdateMissingPackets(uint16_t seq_num) {
458 if (!newest_inserted_seq_num_)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100459 newest_inserted_seq_num_ = seq_num;
philipel2c9f9f22017-06-13 02:47:28 -0700460
461 const int kMaxPaddingAge = 1000;
462 if (AheadOf(seq_num, *newest_inserted_seq_num_)) {
463 uint16_t old_seq_num = seq_num - kMaxPaddingAge;
464 auto erase_to = missing_packets_.lower_bound(old_seq_num);
465 missing_packets_.erase(missing_packets_.begin(), erase_to);
466
467 // Guard against inserting a large amount of missing packets if there is a
468 // jump in the sequence number.
469 if (AheadOf(old_seq_num, *newest_inserted_seq_num_))
470 *newest_inserted_seq_num_ = old_seq_num;
471
472 ++*newest_inserted_seq_num_;
473 while (AheadOf(seq_num, *newest_inserted_seq_num_)) {
474 missing_packets_.insert(*newest_inserted_seq_num_);
475 ++*newest_inserted_seq_num_;
476 }
477 } else {
478 missing_packets_.erase(seq_num);
479 }
480}
481
philipelc707ab72016-04-01 02:01:54 -0700482} // namespace video_coding
483} // namespace webrtc