blob: 1ca488d49add376d08241e5c15da89267191a8d7 [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,
philipelc707ab72016-04-01 02:01:54 -070036 size_t max_buffer_size,
Elad Alonb4643ad2019-02-22 11:19:50 +010037 OnAssembledFrameCallback* assembled_frame_callback)
philipelb4d31082016-07-11 08:46:29 -070038 : clock_(clock),
39 size_(start_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070040 max_size_(max_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070041 first_seq_num_(0),
philipelf4139332016-04-20 10:26:34 +020042 first_packet_received_(false),
philipelaee3e0e2016-11-01 11:45:34 +010043 is_cleared_to_first_seq_num_(false),
philipelc707ab72016-04-01 02:01:54 -070044 data_buffer_(start_buffer_size),
45 sequence_buffer_(start_buffer_size),
Elad Alonb4643ad2019-02-22 11:19:50 +010046 assembled_frame_callback_(assembled_frame_callback),
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010047 unique_frames_seen_(0),
Rasmus Brandt88f080a2017-11-02 14:28:06 +010048 sps_pps_idr_is_h264_keyframe_(
49 field_trial::IsEnabled("WebRTC-SpsPpsIdrIsH264Keyframe")) {
philipelc707ab72016-04-01 02:01:54 -070050 RTC_DCHECK_LE(start_buffer_size, max_buffer_size);
51 // Buffer size must always be a power of 2.
52 RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0);
53 RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0);
54}
55
philipelaee3e0e2016-11-01 11:45:34 +010056PacketBuffer::~PacketBuffer() {
57 Clear();
58}
philipel17deeb42016-08-11 15:09:26 +020059
philipel759e0b72016-11-30 01:32:05 -080060bool PacketBuffer::InsertPacket(VCMPacket* packet) {
philipelfd5a20f2016-11-15 00:57:57 -080061 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
62 {
63 rtc::CritScope lock(&crit_);
philipel3184f8e2017-05-18 08:08:53 -070064
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010065 OnTimestampReceived(packet->timestamp);
66
philipel759e0b72016-11-30 01:32:05 -080067 uint16_t seq_num = packet->seqNum;
philipelfd5a20f2016-11-15 00:57:57 -080068 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -070069
philipelfd5a20f2016-11-15 00:57:57 -080070 if (!first_packet_received_) {
71 first_seq_num_ = seq_num;
philipelfd5a20f2016-11-15 00:57:57 -080072 first_packet_received_ = true;
73 } else if (AheadOf(first_seq_num_, seq_num)) {
74 // If we have explicitly cleared past this packet then it's old,
Johannes Kronbd3f3052019-08-01 15:45:54 +020075 // don't insert it, just silently ignore it.
philipel759e0b72016-11-30 01:32:05 -080076 if (is_cleared_to_first_seq_num_) {
77 delete[] packet->dataPtr;
78 packet->dataPtr = nullptr;
Johannes Kronbd3f3052019-08-01 15:45:54 +020079 return true;
philipel759e0b72016-11-30 01:32:05 -080080 }
philipelaee3e0e2016-11-01 11:45:34 +010081
philipelfd5a20f2016-11-15 00:57:57 -080082 first_seq_num_ = seq_num;
philipelc707ab72016-04-01 02:01:54 -070083 }
philipelc707ab72016-04-01 02:01:54 -070084
philipelfd5a20f2016-11-15 00:57:57 -080085 if (sequence_buffer_[index].used) {
philipel759e0b72016-11-30 01:32:05 -080086 // Duplicate packet, just delete the payload.
87 if (data_buffer_[index].seqNum == packet->seqNum) {
88 delete[] packet->dataPtr;
89 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -080090 return true;
philipel759e0b72016-11-30 01:32:05 -080091 }
philipelfd5a20f2016-11-15 00:57:57 -080092
93 // The packet buffer is full, try to expand the buffer.
94 while (ExpandBufferSize() && sequence_buffer_[seq_num % size_].used) {
95 }
96 index = seq_num % size_;
97
Johannes Kronbd3f3052019-08-01 15:45:54 +020098 // Packet buffer is still full since we were unable to expand the buffer.
philipel759e0b72016-11-30 01:32:05 -080099 if (sequence_buffer_[index].used) {
Johannes Kronbd3f3052019-08-01 15:45:54 +0200100 // Clear the buffer, delete payload, and return false to signal that a
101 // new keyframe is needed.
102 RTC_LOG(LS_WARNING) << "Clear PacketBuffer and request key frame.";
103 Clear();
philipel759e0b72016-11-30 01:32:05 -0800104 delete[] packet->dataPtr;
105 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -0800106 return false;
philipel759e0b72016-11-30 01:32:05 -0800107 }
philipelfd5a20f2016-11-15 00:57:57 -0800108 }
109
Niels Möllerd5e02f02019-02-20 13:12:21 +0100110 sequence_buffer_[index].frame_begin = packet->is_first_packet_in_frame();
111 sequence_buffer_[index].frame_end = packet->is_last_packet_in_frame();
philipel759e0b72016-11-30 01:32:05 -0800112 sequence_buffer_[index].seq_num = packet->seqNum;
philipelfd5a20f2016-11-15 00:57:57 -0800113 sequence_buffer_[index].continuous = false;
114 sequence_buffer_[index].frame_created = false;
115 sequence_buffer_[index].used = true;
philipel759e0b72016-11-30 01:32:05 -0800116 data_buffer_[index] = *packet;
117 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -0800118
philipel2c9f9f22017-06-13 02:47:28 -0700119 UpdateMissingPackets(packet->seqNum);
120
philipel3184f8e2017-05-18 08:08:53 -0700121 int64_t now_ms = clock_->TimeInMilliseconds();
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100122 last_received_packet_ms_ = now_ms;
Niels Möllerabbc50e2019-04-24 09:41:16 +0200123 if (packet->video_header.frame_type == VideoFrameType::kVideoFrameKey)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100124 last_received_keyframe_packet_ms_ = now_ms;
philipel3184f8e2017-05-18 08:08:53 -0700125
philipelfd5a20f2016-11-15 00:57:57 -0800126 found_frames = FindFrames(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700127 }
128
philipelfd5a20f2016-11-15 00:57:57 -0800129 for (std::unique_ptr<RtpFrameObject>& frame : found_frames)
Elad Alonb4643ad2019-02-22 11:19:50 +0100130 assembled_frame_callback_->OnAssembledFrame(std::move(frame));
philipelc707ab72016-04-01 02:01:54 -0700131
philipelc707ab72016-04-01 02:01:54 -0700132 return true;
133}
134
135void PacketBuffer::ClearTo(uint16_t seq_num) {
136 rtc::CritScope lock(&crit_);
philipelc5fb4682017-08-02 04:28:57 -0700137 // We have already cleared past this sequence number, no need to do anything.
138 if (is_cleared_to_first_seq_num_ &&
139 AheadOf<uint16_t>(first_seq_num_, seq_num)) {
140 return;
141 }
philipelaee3e0e2016-11-01 11:45:34 +0100142
143 // If the packet buffer was cleared between a frame was created and returned.
144 if (!first_packet_received_)
145 return;
146
philipelc5fb4682017-08-02 04:28:57 -0700147 // Avoid iterating over the buffer more than once by capping the number of
148 // iterations to the |size_| of the buffer.
149 ++seq_num;
150 size_t diff = ForwardDiff<uint16_t>(first_seq_num_, seq_num);
151 size_t iterations = std::min(diff, size_);
152 for (size_t i = 0; i < iterations; ++i) {
philipelaee3e0e2016-11-01 11:45:34 +0100153 size_t index = first_seq_num_ % size_;
philipelc5fb4682017-08-02 04:28:57 -0700154 RTC_DCHECK_EQ(data_buffer_[index].seqNum, sequence_buffer_[index].seq_num);
155 if (AheadOf<uint16_t>(seq_num, sequence_buffer_[index].seq_num)) {
156 delete[] data_buffer_[index].dataPtr;
157 data_buffer_[index].dataPtr = nullptr;
158 sequence_buffer_[index].used = false;
159 }
philipelaee3e0e2016-11-01 11:45:34 +0100160 ++first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700161 }
philipel2c9f9f22017-06-13 02:47:28 -0700162
philipelc5fb4682017-08-02 04:28:57 -0700163 // If |diff| is larger than |iterations| it means that we don't increment
164 // |first_seq_num_| until we reach |seq_num|, so we set it here.
165 first_seq_num_ = seq_num;
166
167 is_cleared_to_first_seq_num_ = true;
philipelbc5a4082017-12-06 10:41:08 +0100168 auto clear_to_it = missing_packets_.upper_bound(seq_num);
169 if (clear_to_it != missing_packets_.begin()) {
170 --clear_to_it;
171 missing_packets_.erase(missing_packets_.begin(), clear_to_it);
172 }
philipelc707ab72016-04-01 02:01:54 -0700173}
174
Johannes Krona3705562019-08-26 16:37:11 +0200175void PacketBuffer::ClearInterval(uint16_t start_seq_num,
176 uint16_t stop_seq_num) {
177 size_t iterations = ForwardDiff<uint16_t>(start_seq_num, stop_seq_num + 1);
178 RTC_DCHECK_LE(iterations, size_);
179 uint16_t seq_num = start_seq_num;
180 for (size_t i = 0; i < iterations; ++i) {
181 size_t index = seq_num % size_;
182 RTC_DCHECK_EQ(sequence_buffer_[index].seq_num, seq_num);
183 RTC_DCHECK_EQ(sequence_buffer_[index].seq_num, data_buffer_[index].seqNum);
184 delete[] data_buffer_[index].dataPtr;
185 data_buffer_[index].dataPtr = nullptr;
186 sequence_buffer_[index].used = false;
187
188 ++seq_num;
189 }
190}
191
philipelaee3e0e2016-11-01 11:45:34 +0100192void PacketBuffer::Clear() {
193 rtc::CritScope lock(&crit_);
194 for (size_t i = 0; i < size_; ++i) {
195 delete[] data_buffer_[i].dataPtr;
196 data_buffer_[i].dataPtr = nullptr;
197 sequence_buffer_[i].used = false;
198 }
199
200 first_packet_received_ = false;
201 is_cleared_to_first_seq_num_ = false;
philipel2c9f9f22017-06-13 02:47:28 -0700202 last_received_packet_ms_.reset();
203 last_received_keyframe_packet_ms_.reset();
204 newest_inserted_seq_num_.reset();
205 missing_packets_.clear();
206}
207
208void PacketBuffer::PaddingReceived(uint16_t seq_num) {
209 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
210 {
211 rtc::CritScope lock(&crit_);
212 UpdateMissingPackets(seq_num);
213 found_frames = FindFrames(static_cast<uint16_t>(seq_num + 1));
214 }
215
216 for (std::unique_ptr<RtpFrameObject>& frame : found_frames)
Elad Alonb4643ad2019-02-22 11:19:50 +0100217 assembled_frame_callback_->OnAssembledFrame(std::move(frame));
philipel3184f8e2017-05-18 08:08:53 -0700218}
219
Danil Chapovalov0040b662018-06-18 10:48:16 +0200220absl::optional<int64_t> PacketBuffer::LastReceivedPacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700221 rtc::CritScope lock(&crit_);
222 return last_received_packet_ms_;
223}
224
Danil Chapovalov0040b662018-06-18 10:48:16 +0200225absl::optional<int64_t> PacketBuffer::LastReceivedKeyframePacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700226 rtc::CritScope lock(&crit_);
227 return last_received_keyframe_packet_ms_;
philipelaee3e0e2016-11-01 11:45:34 +0100228}
229
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100230int PacketBuffer::GetUniqueFramesSeen() const {
231 rtc::CritScope lock(&crit_);
232 return unique_frames_seen_;
233}
234
philipelc707ab72016-04-01 02:01:54 -0700235bool PacketBuffer::ExpandBufferSize() {
philipelaee3e0e2016-11-01 11:45:34 +0100236 if (size_ == max_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100237 RTC_LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
Johannes Kronbd3f3052019-08-01 15:45:54 +0200238 << "), failed to increase size.";
philipelc707ab72016-04-01 02:01:54 -0700239 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100240 }
philipelc707ab72016-04-01 02:01:54 -0700241
242 size_t new_size = std::min(max_size_, 2 * size_);
243 std::vector<VCMPacket> new_data_buffer(new_size);
244 std::vector<ContinuityInfo> new_sequence_buffer(new_size);
245 for (size_t i = 0; i < size_; ++i) {
246 if (sequence_buffer_[i].used) {
philipelf4139332016-04-20 10:26:34 +0200247 size_t index = sequence_buffer_[i].seq_num % new_size;
philipelc707ab72016-04-01 02:01:54 -0700248 new_sequence_buffer[index] = sequence_buffer_[i];
249 new_data_buffer[index] = data_buffer_[i];
250 }
251 }
252 size_ = new_size;
253 sequence_buffer_ = std::move(new_sequence_buffer);
254 data_buffer_ = std::move(new_data_buffer);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100255 RTC_LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700256 return true;
257}
258
philipelaee3e0e2016-11-01 11:45:34 +0100259bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
philipelf4139332016-04-20 10:26:34 +0200260 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -0700261 int prev_index = index > 0 ? index - 1 : size_ - 1;
philipelf4139332016-04-20 10:26:34 +0200262
philipelc707ab72016-04-01 02:01:54 -0700263 if (!sequence_buffer_[index].used)
264 return false;
philipel2c9f9f22017-06-13 02:47:28 -0700265 if (sequence_buffer_[index].seq_num != seq_num)
266 return false;
philipelf4139332016-04-20 10:26:34 +0200267 if (sequence_buffer_[index].frame_created)
268 return false;
philipel20dce342016-11-28 16:14:57 +0100269 if (sequence_buffer_[index].frame_begin)
philipelc707ab72016-04-01 02:01:54 -0700270 return true;
271 if (!sequence_buffer_[prev_index].used)
272 return false;
philipelea142f82017-01-11 02:01:56 -0800273 if (sequence_buffer_[prev_index].frame_created)
274 return false;
philipelf4139332016-04-20 10:26:34 +0200275 if (sequence_buffer_[prev_index].seq_num !=
philipel2c2f34c2017-01-03 05:55:34 -0800276 static_cast<uint16_t>(sequence_buffer_[index].seq_num - 1)) {
philipelf4139332016-04-20 10:26:34 +0200277 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100278 }
philipel8b6995b2019-01-09 12:39:18 +0100279 if (data_buffer_[prev_index].timestamp != data_buffer_[index].timestamp)
280 return false;
philipelc707ab72016-04-01 02:01:54 -0700281 if (sequence_buffer_[prev_index].continuous)
282 return true;
283
284 return false;
285}
286
philipelfd5a20f2016-11-15 00:57:57 -0800287std::vector<std::unique_ptr<RtpFrameObject>> PacketBuffer::FindFrames(
288 uint16_t seq_num) {
289 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
philipel53910712017-05-18 02:24:40 -0700290 for (size_t i = 0; i < size_ && PotentialNewFrame(seq_num); ++i) {
philipelaee3e0e2016-11-01 11:45:34 +0100291 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -0700292 sequence_buffer_[index].continuous = true;
293
philipelf4139332016-04-20 10:26:34 +0200294 // If all packets of the frame is continuous, find the first packet of the
295 // frame and create an RtpFrameObject.
philipelc707ab72016-04-01 02:01:54 -0700296 if (sequence_buffer_[index].frame_end) {
philipel5ceaaae2016-05-24 10:20:47 +0200297 size_t frame_size = 0;
298 int max_nack_count = -1;
philipelc707ab72016-04-01 02:01:54 -0700299 uint16_t start_seq_num = seq_num;
Chen Xingf00bf422019-06-20 10:05:55 +0200300 int64_t min_recv_time = data_buffer_[index].packet_info.receive_time_ms();
301 int64_t max_recv_time = data_buffer_[index].packet_info.receive_time_ms();
302 RtpPacketInfos::vector_type packet_infos;
philipelf4139332016-04-20 10:26:34 +0200303
philipel5ceaaae2016-05-24 10:20:47 +0200304 // Find the start index by searching backward until the packet with
305 // the |frame_begin| flag is set.
306 int start_index = index;
philipel227f8b92017-08-04 06:39:31 -0700307 size_t tested_packets = 0;
philipel8c619242017-02-02 08:51:29 -0800308 int64_t frame_timestamp = data_buffer_[start_index].timestamp;
philipel53910712017-05-18 02:24:40 -0700309
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100310 // Identify H.264 keyframes by means of SPS, PPS, and IDR.
Niels Möllerd5e02f02019-02-20 13:12:21 +0100311 bool is_h264 = data_buffer_[start_index].codec() == kVideoCodecH264;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100312 bool has_h264_sps = false;
313 bool has_h264_pps = false;
314 bool has_h264_idr = false;
315 bool is_h264_keyframe = false;
316
philipel227f8b92017-08-04 06:39:31 -0700317 while (true) {
318 ++tested_packets;
philipel5ceaaae2016-05-24 10:20:47 +0200319 frame_size += data_buffer_[start_index].sizeBytes;
philipelfd5a20f2016-11-15 00:57:57 -0800320 max_nack_count =
321 std::max(max_nack_count, data_buffer_[start_index].timesNacked);
philipelf4139332016-04-20 10:26:34 +0200322 sequence_buffer_[start_index].frame_created = true;
philipel5ceaaae2016-05-24 10:20:47 +0200323
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100324 min_recv_time =
Chen Xingf00bf422019-06-20 10:05:55 +0200325 std::min(min_recv_time,
326 data_buffer_[start_index].packet_info.receive_time_ms());
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100327 max_recv_time =
Chen Xingf00bf422019-06-20 10:05:55 +0200328 std::max(max_recv_time,
329 data_buffer_[start_index].packet_info.receive_time_ms());
330
331 // Should use |push_front()| since the loop traverses backwards. But
332 // it's too inefficient to do so on a vector so we'll instead fix the
333 // order afterwards.
334 packet_infos.push_back(data_buffer_[start_index].packet_info);
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100335
philipel8c619242017-02-02 08:51:29 -0800336 if (!is_h264 && sequence_buffer_[start_index].frame_begin)
philipel5ceaaae2016-05-24 10:20:47 +0200337 break;
338
philipel2c9f9f22017-06-13 02:47:28 -0700339 if (is_h264 && !is_h264_keyframe) {
philipel7d745e52018-08-02 14:03:53 +0200340 const auto* h264_header = absl::get_if<RTPVideoHeaderH264>(
341 &data_buffer_[start_index].video_header.video_type_header);
342 if (!h264_header || h264_header->nalus_length >= kMaxNalusPerPacket)
philipel09133af2018-05-17 14:11:09 +0200343 return found_frames;
344
philipel7d745e52018-08-02 14:03:53 +0200345 for (size_t j = 0; j < h264_header->nalus_length; ++j) {
346 if (h264_header->nalus[j].type == H264::NaluType::kSps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100347 has_h264_sps = true;
philipel7d745e52018-08-02 14:03:53 +0200348 } else if (h264_header->nalus[j].type == H264::NaluType::kPps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100349 has_h264_pps = true;
philipel7d745e52018-08-02 14:03:53 +0200350 } else if (h264_header->nalus[j].type == H264::NaluType::kIdr) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100351 has_h264_idr = true;
philipel2c9f9f22017-06-13 02:47:28 -0700352 }
353 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100354 if ((sps_pps_idr_is_h264_keyframe_ && has_h264_idr && has_h264_sps &&
355 has_h264_pps) ||
356 (!sps_pps_idr_is_h264_keyframe_ && has_h264_idr)) {
357 is_h264_keyframe = true;
358 }
philipel2c9f9f22017-06-13 02:47:28 -0700359 }
360
philipel227f8b92017-08-04 06:39:31 -0700361 if (tested_packets == size_)
362 break;
363
philipelf4139332016-04-20 10:26:34 +0200364 start_index = start_index > 0 ? start_index - 1 : size_ - 1;
philipel8c619242017-02-02 08:51:29 -0800365
366 // In the case of H264 we don't have a frame_begin bit (yes,
367 // |frame_begin| might be set to true but that is a lie). So instead
368 // we traverese backwards as long as we have a previous packet and
369 // the timestamp of that packet is the same as this one. This may cause
370 // the PacketBuffer to hand out incomplete frames.
371 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
philipel53910712017-05-18 02:24:40 -0700372 if (is_h264 &&
philipel8c619242017-02-02 08:51:29 -0800373 (!sequence_buffer_[start_index].used ||
374 data_buffer_[start_index].timestamp != frame_timestamp)) {
375 break;
376 }
377
378 --start_seq_num;
philipelc707ab72016-04-01 02:01:54 -0700379 }
380
Chen Xingf00bf422019-06-20 10:05:55 +0200381 // Fix the order since the packet-finding loop traverses backwards.
382 std::reverse(packet_infos.begin(), packet_infos.end());
383
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100384 if (is_h264) {
385 // Warn if this is an unsafe frame.
386 if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) {
Jonas Olssonfc501102018-06-15 14:24:10 +0200387 RTC_LOG(LS_WARNING)
388 << "Received H.264-IDR frame "
389 << "(SPS: " << has_h264_sps << ", PPS: " << has_h264_pps
390 << "). Treating as "
391 << (sps_pps_idr_is_h264_keyframe_ ? "delta" : "key")
392 << " frame since WebRTC-SpsPpsIdrIsH264Keyframe is "
393 << (sps_pps_idr_is_h264_keyframe_ ? "enabled." : "disabled");
philipel2c9f9f22017-06-13 02:47:28 -0700394 }
395
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100396 // Now that we have decided whether to treat this frame as a key frame
397 // or delta frame in the frame buffer, we update the field that
398 // determines if the RtpFrameObject is a key frame or delta frame.
399 const size_t first_packet_index = start_seq_num % size_;
400 RTC_CHECK_LT(first_packet_index, size_);
401 if (is_h264_keyframe) {
Niels Möllerabbc50e2019-04-24 09:41:16 +0200402 data_buffer_[first_packet_index].video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100403 VideoFrameType::kVideoFrameKey;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100404 } else {
Niels Möllerabbc50e2019-04-24 09:41:16 +0200405 data_buffer_[first_packet_index].video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100406 VideoFrameType::kVideoFrameDelta;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100407 }
408
Johnny Leebc7f41b2019-05-01 14:41:32 -0400409 // With IPPP, if this is not a keyframe, make sure there are no gaps
410 // in the packet sequence numbers up until this point.
411 const uint8_t h264tid =
412 data_buffer_[start_index].video_header.frame_marking.temporal_id;
Jonas Olssona4d87372019-07-05 19:08:33 +0200413 if (h264tid == kNoTemporalIdx && !is_h264_keyframe &&
414 missing_packets_.upper_bound(start_seq_num) !=
415 missing_packets_.begin()) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100416 uint16_t stop_index = (index + 1) % size_;
417 while (start_index != stop_index) {
418 sequence_buffer_[start_index].frame_created = false;
419 start_index = (start_index + 1) % size_;
420 }
421
422 return found_frames;
423 }
philipel2c9f9f22017-06-13 02:47:28 -0700424 }
425
426 missing_packets_.erase(missing_packets_.begin(),
427 missing_packets_.upper_bound(seq_num));
428
philipel85d5c192019-09-25 17:15:37 +0200429 const VCMPacket* first_packet = GetPacket(start_seq_num);
430 const VCMPacket* last_packet = GetPacket(seq_num);
philipel76161f72019-09-19 11:22:22 +0200431 auto frame = std::make_unique<RtpFrameObject>(
philipel85d5c192019-09-25 17:15:37 +0200432 start_seq_num, seq_num, last_packet->markerBit, max_nack_count,
433 min_recv_time, max_recv_time, first_packet->timestamp,
434 first_packet->ntp_time_ms_, last_packet->video_header.video_timing,
435 first_packet->payloadType, first_packet->codec(),
436 last_packet->video_header.rotation,
437 last_packet->video_header.content_type, first_packet->video_header,
438 last_packet->video_header.color_space,
439 first_packet->generic_descriptor,
440 RtpPacketInfos(std::move(packet_infos)),
philipelb5e47852019-09-20 11:30:12 +0200441 GetEncodedImageBuffer(frame_size, start_seq_num, seq_num));
442
philipel76161f72019-09-19 11:22:22 +0200443 found_frames.emplace_back(std::move(frame));
444
Johannes Krona3705562019-08-26 16:37:11 +0200445 ClearInterval(start_seq_num, seq_num);
philipelc707ab72016-04-01 02:01:54 -0700446 }
philipelc707ab72016-04-01 02:01:54 -0700447 ++seq_num;
448 }
philipelfd5a20f2016-11-15 00:57:57 -0800449 return found_frames;
philipelc707ab72016-04-01 02:01:54 -0700450}
451
philipelb5e47852019-09-20 11:30:12 +0200452rtc::scoped_refptr<EncodedImageBuffer> PacketBuffer::GetEncodedImageBuffer(
453 size_t frame_size,
454 uint16_t first_seq_num,
455 uint16_t last_seq_num) {
456 size_t index = first_seq_num % size_;
457 size_t end = (last_seq_num + 1) % size_;
philipelc707ab72016-04-01 02:01:54 -0700458
philipelb5e47852019-09-20 11:30:12 +0200459 auto buffer = EncodedImageBuffer::Create(frame_size);
460 size_t offset = 0;
philipel227f8b92017-08-04 06:39:31 -0700461
462 do {
philipelb5e47852019-09-20 11:30:12 +0200463 RTC_DCHECK(sequence_buffer_[index].used);
philipelc707ab72016-04-01 02:01:54 -0700464
philipelc18f1d72017-08-02 04:18:02 -0700465 size_t length = data_buffer_[index].sizeBytes;
philipelb5e47852019-09-20 11:30:12 +0200466 RTC_CHECK_LE(offset + length, buffer->size());
467 memcpy(buffer->data() + offset, data_buffer_[index].dataPtr, length);
468 offset += length;
philipel227f8b92017-08-04 06:39:31 -0700469
philipelc707ab72016-04-01 02:01:54 -0700470 index = (index + 1) % size_;
philipel227f8b92017-08-04 06:39:31 -0700471 } while (index != end);
472
philipelb5e47852019-09-20 11:30:12 +0200473 return buffer;
philipelc707ab72016-04-01 02:01:54 -0700474}
475
philipel02447bc2016-05-13 06:01:03 -0700476VCMPacket* PacketBuffer::GetPacket(uint16_t seq_num) {
philipel02447bc2016-05-13 06:01:03 -0700477 size_t index = seq_num % size_;
478 if (!sequence_buffer_[index].used ||
479 seq_num != sequence_buffer_[index].seq_num) {
480 return nullptr;
philipelf4139332016-04-20 10:26:34 +0200481 }
philipel02447bc2016-05-13 06:01:03 -0700482 return &data_buffer_[index];
philipelf4139332016-04-20 10:26:34 +0200483}
484
philipel2c9f9f22017-06-13 02:47:28 -0700485void PacketBuffer::UpdateMissingPackets(uint16_t seq_num) {
486 if (!newest_inserted_seq_num_)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100487 newest_inserted_seq_num_ = seq_num;
philipel2c9f9f22017-06-13 02:47:28 -0700488
489 const int kMaxPaddingAge = 1000;
490 if (AheadOf(seq_num, *newest_inserted_seq_num_)) {
491 uint16_t old_seq_num = seq_num - kMaxPaddingAge;
492 auto erase_to = missing_packets_.lower_bound(old_seq_num);
493 missing_packets_.erase(missing_packets_.begin(), erase_to);
494
495 // Guard against inserting a large amount of missing packets if there is a
496 // jump in the sequence number.
497 if (AheadOf(old_seq_num, *newest_inserted_seq_num_))
498 *newest_inserted_seq_num_ = old_seq_num;
499
500 ++*newest_inserted_seq_num_;
501 while (AheadOf(seq_num, *newest_inserted_seq_num_)) {
502 missing_packets_.insert(*newest_inserted_seq_num_);
503 ++*newest_inserted_seq_num_;
504 }
505 } else {
506 missing_packets_.erase(seq_num);
507 }
508}
509
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100510void PacketBuffer::OnTimestampReceived(uint32_t rtp_timestamp) {
511 const size_t kMaxTimestampsHistory = 1000;
512 if (rtp_timestamps_history_set_.insert(rtp_timestamp).second) {
513 rtp_timestamps_history_queue_.push(rtp_timestamp);
514 ++unique_frames_seen_;
515 if (rtp_timestamps_history_set_.size() > kMaxTimestampsHistory) {
516 uint32_t discarded_timestamp = rtp_timestamps_history_queue_.front();
517 rtp_timestamps_history_set_.erase(discarded_timestamp);
518 rtp_timestamps_history_queue_.pop();
519 }
520 }
521}
522
philipelc707ab72016-04-01 02:01:54 -0700523} // namespace video_coding
524} // namespace webrtc