blob: 58afab4e7b452e5ac2622d4242c35fdeae8505be [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>
philipel17deeb42016-08-11 15:09:26 +020017#include <utility>
philipelc707ab72016-04-01 02:01:54 -070018
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "absl/types/variant.h"
20#include "api/video/encoded_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "common_video/h264/h264_common.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "modules/rtp_rtcp/source/rtp_video_header.h"
23#include "modules/video_coding/codecs/h264/include/h264_globals.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/video_coding/frame_object.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/checks.h"
26#include "rtc_base/logging.h"
Yves Gerey3e707812018-11-28 16:47:49 +010027#include "rtc_base/numerics/mod_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "system_wrappers/include/clock.h"
Rasmus Brandt88f080a2017-11-02 14:28:06 +010029#include "system_wrappers/include/field_trial.h"
philipelc707ab72016-04-01 02:01:54 -070030
31namespace webrtc {
32namespace video_coding {
33
philipelb4d31082016-07-11 08:46:29 -070034PacketBuffer::PacketBuffer(Clock* clock,
35 size_t start_buffer_size,
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020036 size_t max_buffer_size)
philipelb4d31082016-07-11 08:46:29 -070037 : clock_(clock),
philipelc707ab72016-04-01 02:01:54 -070038 max_size_(max_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070039 first_seq_num_(0),
philipelf4139332016-04-20 10:26:34 +020040 first_packet_received_(false),
philipelaee3e0e2016-11-01 11:45:34 +010041 is_cleared_to_first_seq_num_(false),
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020042 buffer_(start_buffer_size),
Rasmus Brandt88f080a2017-11-02 14:28:06 +010043 sps_pps_idr_is_h264_keyframe_(
44 field_trial::IsEnabled("WebRTC-SpsPpsIdrIsH264Keyframe")) {
philipelc707ab72016-04-01 02:01:54 -070045 RTC_DCHECK_LE(start_buffer_size, max_buffer_size);
46 // Buffer size must always be a power of 2.
47 RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0);
48 RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0);
49}
50
philipelaee3e0e2016-11-01 11:45:34 +010051PacketBuffer::~PacketBuffer() {
52 Clear();
53}
philipel17deeb42016-08-11 15:09:26 +020054
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020055PacketBuffer::InsertResult PacketBuffer::InsertPacket(VCMPacket* packet) {
56 PacketBuffer::InsertResult result;
57 rtc::CritScope lock(&crit_);
philipel3184f8e2017-05-18 08:08:53 -070058
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020059 uint16_t seq_num = packet->seqNum;
60 size_t index = seq_num % buffer_.size();
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010061
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020062 if (!first_packet_received_) {
63 first_seq_num_ = seq_num;
64 first_packet_received_ = true;
65 } else if (AheadOf(first_seq_num_, seq_num)) {
66 // If we have explicitly cleared past this packet then it's old,
67 // don't insert it, just silently ignore it.
68 if (is_cleared_to_first_seq_num_) {
69 delete[] packet->dataPtr;
70 packet->dataPtr = nullptr;
71 return result;
philipelc707ab72016-04-01 02:01:54 -070072 }
philipelc707ab72016-04-01 02:01:54 -070073
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020074 first_seq_num_ = seq_num;
philipelc707ab72016-04-01 02:01:54 -070075 }
76
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020077 if (buffer_[index].used) {
78 // Duplicate packet, just delete the payload.
79 if (buffer_[index].seq_num() == packet->seqNum) {
80 delete[] packet->dataPtr;
81 packet->dataPtr = nullptr;
82 return result;
83 }
philipelc707ab72016-04-01 02:01:54 -070084
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020085 // The packet buffer is full, try to expand the buffer.
86 while (ExpandBufferSize() && buffer_[seq_num % buffer_.size()].used) {
87 }
88 index = seq_num % buffer_.size();
89
90 // Packet buffer is still full since we were unable to expand the buffer.
91 if (buffer_[index].used) {
92 // Clear the buffer, delete payload, and return false to signal that a
93 // new keyframe is needed.
94 RTC_LOG(LS_WARNING) << "Clear PacketBuffer and request key frame.";
95 Clear();
96 delete[] packet->dataPtr;
97 packet->dataPtr = nullptr;
98 result.buffer_cleared = true;
99 return result;
100 }
101 }
102
103 StoredPacket& new_entry = buffer_[index];
104 new_entry.continuous = false;
105 new_entry.used = true;
106 new_entry.data = *packet;
107 packet->dataPtr = nullptr;
108
109 UpdateMissingPackets(packet->seqNum);
110
111 int64_t now_ms = clock_->TimeInMilliseconds();
112 last_received_packet_ms_ = now_ms;
113 if (packet->video_header.frame_type == VideoFrameType::kVideoFrameKey)
114 last_received_keyframe_packet_ms_ = now_ms;
115
116 result.frames = FindFrames(seq_num);
117 return result;
philipelc707ab72016-04-01 02:01:54 -0700118}
119
120void PacketBuffer::ClearTo(uint16_t seq_num) {
121 rtc::CritScope lock(&crit_);
philipelc5fb4682017-08-02 04:28:57 -0700122 // We have already cleared past this sequence number, no need to do anything.
123 if (is_cleared_to_first_seq_num_ &&
124 AheadOf<uint16_t>(first_seq_num_, seq_num)) {
125 return;
126 }
philipelaee3e0e2016-11-01 11:45:34 +0100127
128 // If the packet buffer was cleared between a frame was created and returned.
129 if (!first_packet_received_)
130 return;
131
philipelc5fb4682017-08-02 04:28:57 -0700132 // Avoid iterating over the buffer more than once by capping the number of
133 // iterations to the |size_| of the buffer.
134 ++seq_num;
135 size_t diff = ForwardDiff<uint16_t>(first_seq_num_, seq_num);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200136 size_t iterations = std::min(diff, buffer_.size());
philipelc5fb4682017-08-02 04:28:57 -0700137 for (size_t i = 0; i < iterations; ++i) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200138 size_t index = first_seq_num_ % buffer_.size();
139 if (AheadOf<uint16_t>(seq_num, buffer_[index].seq_num())) {
140 delete[] buffer_[index].data.dataPtr;
141 buffer_[index].data.dataPtr = nullptr;
142 buffer_[index].used = false;
philipelc5fb4682017-08-02 04:28:57 -0700143 }
philipelaee3e0e2016-11-01 11:45:34 +0100144 ++first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700145 }
philipel2c9f9f22017-06-13 02:47:28 -0700146
philipelc5fb4682017-08-02 04:28:57 -0700147 // If |diff| is larger than |iterations| it means that we don't increment
148 // |first_seq_num_| until we reach |seq_num|, so we set it here.
149 first_seq_num_ = seq_num;
150
151 is_cleared_to_first_seq_num_ = true;
philipelbc5a4082017-12-06 10:41:08 +0100152 auto clear_to_it = missing_packets_.upper_bound(seq_num);
153 if (clear_to_it != missing_packets_.begin()) {
154 --clear_to_it;
155 missing_packets_.erase(missing_packets_.begin(), clear_to_it);
156 }
philipelc707ab72016-04-01 02:01:54 -0700157}
158
Johannes Krona3705562019-08-26 16:37:11 +0200159void PacketBuffer::ClearInterval(uint16_t start_seq_num,
160 uint16_t stop_seq_num) {
161 size_t iterations = ForwardDiff<uint16_t>(start_seq_num, stop_seq_num + 1);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200162 RTC_DCHECK_LE(iterations, buffer_.size());
Johannes Krona3705562019-08-26 16:37:11 +0200163 uint16_t seq_num = start_seq_num;
164 for (size_t i = 0; i < iterations; ++i) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200165 size_t index = seq_num % buffer_.size();
166 RTC_DCHECK_EQ(buffer_[index].seq_num(), seq_num);
167 delete[] buffer_[index].data.dataPtr;
168 buffer_[index].data.dataPtr = nullptr;
169 buffer_[index].used = false;
Johannes Krona3705562019-08-26 16:37:11 +0200170
171 ++seq_num;
172 }
173}
174
philipelaee3e0e2016-11-01 11:45:34 +0100175void PacketBuffer::Clear() {
176 rtc::CritScope lock(&crit_);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200177 for (StoredPacket& entry : buffer_) {
178 delete[] entry.data.dataPtr;
179 entry.data.dataPtr = nullptr;
180 entry.used = false;
philipelaee3e0e2016-11-01 11:45:34 +0100181 }
182
183 first_packet_received_ = false;
184 is_cleared_to_first_seq_num_ = false;
philipel2c9f9f22017-06-13 02:47:28 -0700185 last_received_packet_ms_.reset();
186 last_received_keyframe_packet_ms_.reset();
187 newest_inserted_seq_num_.reset();
188 missing_packets_.clear();
189}
190
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200191PacketBuffer::InsertResult PacketBuffer::InsertPadding(uint16_t seq_num) {
192 PacketBuffer::InsertResult result;
193 rtc::CritScope lock(&crit_);
194 UpdateMissingPackets(seq_num);
195 result.frames = FindFrames(static_cast<uint16_t>(seq_num + 1));
196 return result;
philipel3184f8e2017-05-18 08:08:53 -0700197}
198
Danil Chapovalov0040b662018-06-18 10:48:16 +0200199absl::optional<int64_t> PacketBuffer::LastReceivedPacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700200 rtc::CritScope lock(&crit_);
201 return last_received_packet_ms_;
202}
203
Danil Chapovalov0040b662018-06-18 10:48:16 +0200204absl::optional<int64_t> PacketBuffer::LastReceivedKeyframePacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700205 rtc::CritScope lock(&crit_);
206 return last_received_keyframe_packet_ms_;
philipelaee3e0e2016-11-01 11:45:34 +0100207}
208
philipelc707ab72016-04-01 02:01:54 -0700209bool PacketBuffer::ExpandBufferSize() {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200210 if (buffer_.size() == max_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100211 RTC_LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
Johannes Kronbd3f3052019-08-01 15:45:54 +0200212 << "), failed to increase size.";
philipelc707ab72016-04-01 02:01:54 -0700213 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100214 }
philipelc707ab72016-04-01 02:01:54 -0700215
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200216 size_t new_size = std::min(max_size_, 2 * buffer_.size());
217 std::vector<StoredPacket> new_buffer(new_size);
218 for (StoredPacket& entry : buffer_) {
219 if (entry.used) {
220 new_buffer[entry.seq_num() % new_size] = entry;
philipelc707ab72016-04-01 02:01:54 -0700221 }
222 }
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200223 buffer_ = std::move(new_buffer);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100224 RTC_LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700225 return true;
226}
227
philipelaee3e0e2016-11-01 11:45:34 +0100228bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200229 size_t index = seq_num % buffer_.size();
230 int prev_index = index > 0 ? index - 1 : buffer_.size() - 1;
231 const StoredPacket& entry = buffer_[index];
232 const StoredPacket& prev_entry = buffer_[prev_index];
philipelf4139332016-04-20 10:26:34 +0200233
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200234 if (!entry.used)
philipelc707ab72016-04-01 02:01:54 -0700235 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200236 if (entry.seq_num() != seq_num)
philipel2c9f9f22017-06-13 02:47:28 -0700237 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200238 if (entry.frame_begin())
philipelc707ab72016-04-01 02:01:54 -0700239 return true;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200240 if (!prev_entry.used)
philipelc707ab72016-04-01 02:01:54 -0700241 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200242 if (prev_entry.seq_num() != static_cast<uint16_t>(entry.seq_num() - 1))
philipelea142f82017-01-11 02:01:56 -0800243 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200244 if (prev_entry.data.timestamp != entry.data.timestamp)
philipelf4139332016-04-20 10:26:34 +0200245 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200246 if (prev_entry.continuous)
philipelc707ab72016-04-01 02:01:54 -0700247 return true;
248
249 return false;
250}
251
philipelfd5a20f2016-11-15 00:57:57 -0800252std::vector<std::unique_ptr<RtpFrameObject>> PacketBuffer::FindFrames(
253 uint16_t seq_num) {
254 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200255 for (size_t i = 0; i < buffer_.size() && PotentialNewFrame(seq_num); ++i) {
256 size_t index = seq_num % buffer_.size();
257 buffer_[index].continuous = true;
philipelc707ab72016-04-01 02:01:54 -0700258
philipelf4139332016-04-20 10:26:34 +0200259 // If all packets of the frame is continuous, find the first packet of the
260 // frame and create an RtpFrameObject.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200261 if (buffer_[index].frame_end()) {
philipel5ceaaae2016-05-24 10:20:47 +0200262 size_t frame_size = 0;
263 int max_nack_count = -1;
philipelc707ab72016-04-01 02:01:54 -0700264 uint16_t start_seq_num = seq_num;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200265 int64_t min_recv_time = buffer_[index].data.packet_info.receive_time_ms();
266 int64_t max_recv_time = buffer_[index].data.packet_info.receive_time_ms();
Chen Xingf00bf422019-06-20 10:05:55 +0200267 RtpPacketInfos::vector_type packet_infos;
philipelf4139332016-04-20 10:26:34 +0200268
philipel5ceaaae2016-05-24 10:20:47 +0200269 // Find the start index by searching backward until the packet with
270 // the |frame_begin| flag is set.
271 int start_index = index;
philipel227f8b92017-08-04 06:39:31 -0700272 size_t tested_packets = 0;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200273 int64_t frame_timestamp = buffer_[start_index].data.timestamp;
philipel53910712017-05-18 02:24:40 -0700274
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100275 // Identify H.264 keyframes by means of SPS, PPS, and IDR.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200276 bool is_h264 = buffer_[start_index].data.codec() == kVideoCodecH264;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100277 bool has_h264_sps = false;
278 bool has_h264_pps = false;
279 bool has_h264_idr = false;
280 bool is_h264_keyframe = false;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700281 int idr_width = -1;
282 int idr_height = -1;
philipel227f8b92017-08-04 06:39:31 -0700283 while (true) {
284 ++tested_packets;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200285 frame_size += buffer_[start_index].data.sizeBytes;
philipelfd5a20f2016-11-15 00:57:57 -0800286 max_nack_count =
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200287 std::max(max_nack_count, buffer_[start_index].data.timesNacked);
philipel5ceaaae2016-05-24 10:20:47 +0200288
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100289 min_recv_time =
Chen Xingf00bf422019-06-20 10:05:55 +0200290 std::min(min_recv_time,
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200291 buffer_[start_index].data.packet_info.receive_time_ms());
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100292 max_recv_time =
Chen Xingf00bf422019-06-20 10:05:55 +0200293 std::max(max_recv_time,
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200294 buffer_[start_index].data.packet_info.receive_time_ms());
Chen Xingf00bf422019-06-20 10:05:55 +0200295
296 // Should use |push_front()| since the loop traverses backwards. But
297 // it's too inefficient to do so on a vector so we'll instead fix the
298 // order afterwards.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200299 packet_infos.push_back(buffer_[start_index].data.packet_info);
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100300
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200301 if (!is_h264 && buffer_[start_index].frame_begin())
philipel5ceaaae2016-05-24 10:20:47 +0200302 break;
303
Shyam Sadhwani2b84dad2019-10-02 17:22:33 -0700304 if (is_h264) {
philipel7d745e52018-08-02 14:03:53 +0200305 const auto* h264_header = absl::get_if<RTPVideoHeaderH264>(
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200306 &buffer_[start_index].data.video_header.video_type_header);
philipel7d745e52018-08-02 14:03:53 +0200307 if (!h264_header || h264_header->nalus_length >= kMaxNalusPerPacket)
philipel09133af2018-05-17 14:11:09 +0200308 return found_frames;
309
philipel7d745e52018-08-02 14:03:53 +0200310 for (size_t j = 0; j < h264_header->nalus_length; ++j) {
311 if (h264_header->nalus[j].type == H264::NaluType::kSps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100312 has_h264_sps = true;
philipel7d745e52018-08-02 14:03:53 +0200313 } else if (h264_header->nalus[j].type == H264::NaluType::kPps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100314 has_h264_pps = true;
philipel7d745e52018-08-02 14:03:53 +0200315 } else if (h264_header->nalus[j].type == H264::NaluType::kIdr) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100316 has_h264_idr = true;
philipel2c9f9f22017-06-13 02:47:28 -0700317 }
318 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100319 if ((sps_pps_idr_is_h264_keyframe_ && has_h264_idr && has_h264_sps &&
320 has_h264_pps) ||
321 (!sps_pps_idr_is_h264_keyframe_ && has_h264_idr)) {
322 is_h264_keyframe = true;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700323 // Store the resolution of key frame which is the packet with
324 // smallest index and valid resolution; typically its IDR or SPS
325 // packet; there may be packet preceeding this packet, IDR's
326 // resolution will be applied to them.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200327 if (buffer_[start_index].data.width() > 0 &&
328 buffer_[start_index].data.height() > 0) {
329 idr_width = buffer_[start_index].data.width();
330 idr_height = buffer_[start_index].data.height();
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700331 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100332 }
philipel2c9f9f22017-06-13 02:47:28 -0700333 }
334
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200335 if (tested_packets == buffer_.size())
philipel227f8b92017-08-04 06:39:31 -0700336 break;
337
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200338 start_index = start_index > 0 ? start_index - 1 : buffer_.size() - 1;
philipel8c619242017-02-02 08:51:29 -0800339
340 // In the case of H264 we don't have a frame_begin bit (yes,
341 // |frame_begin| might be set to true but that is a lie). So instead
342 // we traverese backwards as long as we have a previous packet and
343 // the timestamp of that packet is the same as this one. This may cause
344 // the PacketBuffer to hand out incomplete frames.
345 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
philipel53910712017-05-18 02:24:40 -0700346 if (is_h264 &&
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200347 (!buffer_[start_index].used ||
348 buffer_[start_index].data.timestamp != frame_timestamp)) {
philipel8c619242017-02-02 08:51:29 -0800349 break;
350 }
351
352 --start_seq_num;
philipelc707ab72016-04-01 02:01:54 -0700353 }
354
Chen Xingf00bf422019-06-20 10:05:55 +0200355 // Fix the order since the packet-finding loop traverses backwards.
356 std::reverse(packet_infos.begin(), packet_infos.end());
357
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100358 if (is_h264) {
359 // Warn if this is an unsafe frame.
360 if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) {
Jonas Olssonfc501102018-06-15 14:24:10 +0200361 RTC_LOG(LS_WARNING)
362 << "Received H.264-IDR frame "
363 << "(SPS: " << has_h264_sps << ", PPS: " << has_h264_pps
364 << "). Treating as "
365 << (sps_pps_idr_is_h264_keyframe_ ? "delta" : "key")
366 << " frame since WebRTC-SpsPpsIdrIsH264Keyframe is "
367 << (sps_pps_idr_is_h264_keyframe_ ? "enabled." : "disabled");
philipel2c9f9f22017-06-13 02:47:28 -0700368 }
369
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100370 // Now that we have decided whether to treat this frame as a key frame
371 // or delta frame in the frame buffer, we update the field that
372 // determines if the RtpFrameObject is a key frame or delta frame.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200373 const size_t first_packet_index = start_seq_num % buffer_.size();
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100374 if (is_h264_keyframe) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200375 buffer_[first_packet_index].data.video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100376 VideoFrameType::kVideoFrameKey;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700377 if (idr_width > 0 && idr_height > 0) {
378 // IDR frame was finalized and we have the correct resolution for
379 // IDR; update first packet to have same resolution as IDR.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200380 buffer_[first_packet_index].data.video_header.width = idr_width;
381 buffer_[first_packet_index].data.video_header.height = idr_height;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700382 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100383 } else {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200384 buffer_[first_packet_index].data.video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100385 VideoFrameType::kVideoFrameDelta;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100386 }
387
Johnny Leebc7f41b2019-05-01 14:41:32 -0400388 // With IPPP, if this is not a keyframe, make sure there are no gaps
389 // in the packet sequence numbers up until this point.
390 const uint8_t h264tid =
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200391 buffer_[start_index].data.video_header.frame_marking.temporal_id;
Jonas Olssona4d87372019-07-05 19:08:33 +0200392 if (h264tid == kNoTemporalIdx && !is_h264_keyframe &&
393 missing_packets_.upper_bound(start_seq_num) !=
394 missing_packets_.begin()) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100395 return found_frames;
396 }
philipel2c9f9f22017-06-13 02:47:28 -0700397 }
398
399 missing_packets_.erase(missing_packets_.begin(),
400 missing_packets_.upper_bound(seq_num));
401
philipel85d5c192019-09-25 17:15:37 +0200402 const VCMPacket* first_packet = GetPacket(start_seq_num);
403 const VCMPacket* last_packet = GetPacket(seq_num);
philipel76161f72019-09-19 11:22:22 +0200404 auto frame = std::make_unique<RtpFrameObject>(
philipel85d5c192019-09-25 17:15:37 +0200405 start_seq_num, seq_num, last_packet->markerBit, max_nack_count,
406 min_recv_time, max_recv_time, first_packet->timestamp,
407 first_packet->ntp_time_ms_, last_packet->video_header.video_timing,
408 first_packet->payloadType, first_packet->codec(),
409 last_packet->video_header.rotation,
410 last_packet->video_header.content_type, first_packet->video_header,
411 last_packet->video_header.color_space,
412 first_packet->generic_descriptor,
413 RtpPacketInfos(std::move(packet_infos)),
philipelb5e47852019-09-20 11:30:12 +0200414 GetEncodedImageBuffer(frame_size, start_seq_num, seq_num));
415
philipel76161f72019-09-19 11:22:22 +0200416 found_frames.emplace_back(std::move(frame));
417
Johannes Krona3705562019-08-26 16:37:11 +0200418 ClearInterval(start_seq_num, seq_num);
philipelc707ab72016-04-01 02:01:54 -0700419 }
philipelc707ab72016-04-01 02:01:54 -0700420 ++seq_num;
421 }
philipelfd5a20f2016-11-15 00:57:57 -0800422 return found_frames;
philipelc707ab72016-04-01 02:01:54 -0700423}
424
philipelb5e47852019-09-20 11:30:12 +0200425rtc::scoped_refptr<EncodedImageBuffer> PacketBuffer::GetEncodedImageBuffer(
426 size_t frame_size,
427 uint16_t first_seq_num,
428 uint16_t last_seq_num) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200429 size_t index = first_seq_num % buffer_.size();
430 size_t end = (last_seq_num + 1) % buffer_.size();
philipelc707ab72016-04-01 02:01:54 -0700431
philipelb5e47852019-09-20 11:30:12 +0200432 auto buffer = EncodedImageBuffer::Create(frame_size);
433 size_t offset = 0;
philipel227f8b92017-08-04 06:39:31 -0700434
435 do {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200436 RTC_DCHECK(buffer_[index].used);
philipelc707ab72016-04-01 02:01:54 -0700437
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200438 size_t length = buffer_[index].data.sizeBytes;
philipelb5e47852019-09-20 11:30:12 +0200439 RTC_CHECK_LE(offset + length, buffer->size());
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200440 memcpy(buffer->data() + offset, buffer_[index].data.dataPtr, length);
philipelb5e47852019-09-20 11:30:12 +0200441 offset += length;
philipel227f8b92017-08-04 06:39:31 -0700442
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200443 index = (index + 1) % buffer_.size();
philipel227f8b92017-08-04 06:39:31 -0700444 } while (index != end);
445
philipelb5e47852019-09-20 11:30:12 +0200446 return buffer;
philipelc707ab72016-04-01 02:01:54 -0700447}
448
philipel02447bc2016-05-13 06:01:03 -0700449VCMPacket* PacketBuffer::GetPacket(uint16_t seq_num) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200450 StoredPacket& entry = buffer_[seq_num % buffer_.size()];
451 if (!entry.used || seq_num != entry.seq_num()) {
philipel02447bc2016-05-13 06:01:03 -0700452 return nullptr;
philipelf4139332016-04-20 10:26:34 +0200453 }
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200454 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