blob: 80c66661fb95d5ae483e9e66664501855b22296a [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>
philipelc707ab72016-04-01 02:01:54 -070014#include <algorithm>
Yves Gerey3e707812018-11-28 16:47:49 +010015#include <cstdint>
philipel17deeb42016-08-11 15:09:26 +020016#include <utility>
philipelc707ab72016-04-01 02:01:54 -070017
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "absl/types/variant.h"
19#include "api/video/encoded_frame.h"
20#include "common_types.h" // NOLINT(build/include)
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"
25#include "rtc_base/atomicops.h"
26#include "rtc_base/checks.h"
27#include "rtc_base/logging.h"
Yves Gerey3e707812018-11-28 16:47:49 +010028#include "rtc_base/numerics/mod_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "system_wrappers/include/clock.h"
Rasmus Brandt88f080a2017-11-02 14:28:06 +010030#include "system_wrappers/include/field_trial.h"
philipelc707ab72016-04-01 02:01:54 -070031
32namespace webrtc {
33namespace video_coding {
34
philipel17deeb42016-08-11 15:09:26 +020035rtc::scoped_refptr<PacketBuffer> PacketBuffer::Create(
36 Clock* clock,
37 size_t start_buffer_size,
38 size_t max_buffer_size,
39 OnReceivedFrameCallback* received_frame_callback) {
40 return rtc::scoped_refptr<PacketBuffer>(new PacketBuffer(
41 clock, start_buffer_size, max_buffer_size, received_frame_callback));
42}
43
philipelb4d31082016-07-11 08:46:29 -070044PacketBuffer::PacketBuffer(Clock* clock,
45 size_t start_buffer_size,
philipelc707ab72016-04-01 02:01:54 -070046 size_t max_buffer_size,
philipel17deeb42016-08-11 15:09:26 +020047 OnReceivedFrameCallback* received_frame_callback)
philipelb4d31082016-07-11 08:46:29 -070048 : clock_(clock),
49 size_(start_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070050 max_size_(max_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070051 first_seq_num_(0),
philipelf4139332016-04-20 10:26:34 +020052 first_packet_received_(false),
philipelaee3e0e2016-11-01 11:45:34 +010053 is_cleared_to_first_seq_num_(false),
philipelc707ab72016-04-01 02:01:54 -070054 data_buffer_(start_buffer_size),
55 sequence_buffer_(start_buffer_size),
Rasmus Brandt88f080a2017-11-02 14:28:06 +010056 received_frame_callback_(received_frame_callback),
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010057 unique_frames_seen_(0),
Rasmus Brandt88f080a2017-11-02 14:28:06 +010058 sps_pps_idr_is_h264_keyframe_(
59 field_trial::IsEnabled("WebRTC-SpsPpsIdrIsH264Keyframe")) {
philipelc707ab72016-04-01 02:01:54 -070060 RTC_DCHECK_LE(start_buffer_size, max_buffer_size);
61 // Buffer size must always be a power of 2.
62 RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0);
63 RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0);
64}
65
philipelaee3e0e2016-11-01 11:45:34 +010066PacketBuffer::~PacketBuffer() {
67 Clear();
68}
philipel17deeb42016-08-11 15:09:26 +020069
philipel759e0b72016-11-30 01:32:05 -080070bool PacketBuffer::InsertPacket(VCMPacket* packet) {
philipelfd5a20f2016-11-15 00:57:57 -080071 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
72 {
73 rtc::CritScope lock(&crit_);
philipel3184f8e2017-05-18 08:08:53 -070074
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010075 OnTimestampReceived(packet->timestamp);
76
philipel759e0b72016-11-30 01:32:05 -080077 uint16_t seq_num = packet->seqNum;
philipelfd5a20f2016-11-15 00:57:57 -080078 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -070079
philipelfd5a20f2016-11-15 00:57:57 -080080 if (!first_packet_received_) {
81 first_seq_num_ = seq_num;
philipelfd5a20f2016-11-15 00:57:57 -080082 first_packet_received_ = true;
83 } else if (AheadOf(first_seq_num_, seq_num)) {
84 // If we have explicitly cleared past this packet then it's old,
85 // don't insert it.
philipel759e0b72016-11-30 01:32:05 -080086 if (is_cleared_to_first_seq_num_) {
87 delete[] packet->dataPtr;
88 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -080089 return false;
philipel759e0b72016-11-30 01:32:05 -080090 }
philipelaee3e0e2016-11-01 11:45:34 +010091
philipelfd5a20f2016-11-15 00:57:57 -080092 first_seq_num_ = seq_num;
philipelc707ab72016-04-01 02:01:54 -070093 }
philipelc707ab72016-04-01 02:01:54 -070094
philipelfd5a20f2016-11-15 00:57:57 -080095 if (sequence_buffer_[index].used) {
philipel759e0b72016-11-30 01:32:05 -080096 // Duplicate packet, just delete the payload.
97 if (data_buffer_[index].seqNum == packet->seqNum) {
98 delete[] packet->dataPtr;
99 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -0800100 return true;
philipel759e0b72016-11-30 01:32:05 -0800101 }
philipelfd5a20f2016-11-15 00:57:57 -0800102
103 // The packet buffer is full, try to expand the buffer.
104 while (ExpandBufferSize() && sequence_buffer_[seq_num % size_].used) {
105 }
106 index = seq_num % size_;
107
108 // Packet buffer is still full.
philipel759e0b72016-11-30 01:32:05 -0800109 if (sequence_buffer_[index].used) {
110 delete[] packet->dataPtr;
111 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -0800112 return false;
philipel759e0b72016-11-30 01:32:05 -0800113 }
philipelfd5a20f2016-11-15 00:57:57 -0800114 }
115
johan0d1b2b62017-01-10 04:21:35 -0800116 sequence_buffer_[index].frame_begin = packet->is_first_packet_in_frame;
philipelef615ea2018-09-13 11:07:48 +0200117 sequence_buffer_[index].frame_end = packet->is_last_packet_in_frame;
philipel759e0b72016-11-30 01:32:05 -0800118 sequence_buffer_[index].seq_num = packet->seqNum;
philipelfd5a20f2016-11-15 00:57:57 -0800119 sequence_buffer_[index].continuous = false;
120 sequence_buffer_[index].frame_created = false;
121 sequence_buffer_[index].used = true;
philipel759e0b72016-11-30 01:32:05 -0800122 data_buffer_[index] = *packet;
123 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -0800124
philipel2c9f9f22017-06-13 02:47:28 -0700125 UpdateMissingPackets(packet->seqNum);
126
philipel3184f8e2017-05-18 08:08:53 -0700127 int64_t now_ms = clock_->TimeInMilliseconds();
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100128 last_received_packet_ms_ = now_ms;
philipel3184f8e2017-05-18 08:08:53 -0700129 if (packet->frameType == kVideoFrameKey)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100130 last_received_keyframe_packet_ms_ = now_ms;
philipel3184f8e2017-05-18 08:08:53 -0700131
philipelfd5a20f2016-11-15 00:57:57 -0800132 found_frames = FindFrames(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700133 }
134
philipelfd5a20f2016-11-15 00:57:57 -0800135 for (std::unique_ptr<RtpFrameObject>& frame : found_frames)
136 received_frame_callback_->OnReceivedFrame(std::move(frame));
philipelc707ab72016-04-01 02:01:54 -0700137
philipelc707ab72016-04-01 02:01:54 -0700138 return true;
139}
140
141void PacketBuffer::ClearTo(uint16_t seq_num) {
142 rtc::CritScope lock(&crit_);
philipelc5fb4682017-08-02 04:28:57 -0700143 // We have already cleared past this sequence number, no need to do anything.
144 if (is_cleared_to_first_seq_num_ &&
145 AheadOf<uint16_t>(first_seq_num_, seq_num)) {
146 return;
147 }
philipelaee3e0e2016-11-01 11:45:34 +0100148
149 // If the packet buffer was cleared between a frame was created and returned.
150 if (!first_packet_received_)
151 return;
152
philipelc5fb4682017-08-02 04:28:57 -0700153 // Avoid iterating over the buffer more than once by capping the number of
154 // iterations to the |size_| of the buffer.
155 ++seq_num;
156 size_t diff = ForwardDiff<uint16_t>(first_seq_num_, seq_num);
157 size_t iterations = std::min(diff, size_);
158 for (size_t i = 0; i < iterations; ++i) {
philipelaee3e0e2016-11-01 11:45:34 +0100159 size_t index = first_seq_num_ % size_;
philipelc5fb4682017-08-02 04:28:57 -0700160 RTC_DCHECK_EQ(data_buffer_[index].seqNum, sequence_buffer_[index].seq_num);
161 if (AheadOf<uint16_t>(seq_num, sequence_buffer_[index].seq_num)) {
162 delete[] data_buffer_[index].dataPtr;
163 data_buffer_[index].dataPtr = nullptr;
164 sequence_buffer_[index].used = false;
165 }
philipelaee3e0e2016-11-01 11:45:34 +0100166 ++first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700167 }
philipel2c9f9f22017-06-13 02:47:28 -0700168
philipelc5fb4682017-08-02 04:28:57 -0700169 // If |diff| is larger than |iterations| it means that we don't increment
170 // |first_seq_num_| until we reach |seq_num|, so we set it here.
171 first_seq_num_ = seq_num;
172
173 is_cleared_to_first_seq_num_ = true;
philipelbc5a4082017-12-06 10:41:08 +0100174 auto clear_to_it = missing_packets_.upper_bound(seq_num);
175 if (clear_to_it != missing_packets_.begin()) {
176 --clear_to_it;
177 missing_packets_.erase(missing_packets_.begin(), clear_to_it);
178 }
philipelc707ab72016-04-01 02:01:54 -0700179}
180
philipelaee3e0e2016-11-01 11:45:34 +0100181void PacketBuffer::Clear() {
182 rtc::CritScope lock(&crit_);
183 for (size_t i = 0; i < size_; ++i) {
184 delete[] data_buffer_[i].dataPtr;
185 data_buffer_[i].dataPtr = nullptr;
186 sequence_buffer_[i].used = false;
187 }
188
189 first_packet_received_ = false;
190 is_cleared_to_first_seq_num_ = false;
philipel2c9f9f22017-06-13 02:47:28 -0700191 last_received_packet_ms_.reset();
192 last_received_keyframe_packet_ms_.reset();
193 newest_inserted_seq_num_.reset();
194 missing_packets_.clear();
195}
196
197void PacketBuffer::PaddingReceived(uint16_t seq_num) {
198 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
199 {
200 rtc::CritScope lock(&crit_);
201 UpdateMissingPackets(seq_num);
202 found_frames = FindFrames(static_cast<uint16_t>(seq_num + 1));
203 }
204
205 for (std::unique_ptr<RtpFrameObject>& frame : found_frames)
206 received_frame_callback_->OnReceivedFrame(std::move(frame));
philipel3184f8e2017-05-18 08:08:53 -0700207}
208
Danil Chapovalov0040b662018-06-18 10:48:16 +0200209absl::optional<int64_t> PacketBuffer::LastReceivedPacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700210 rtc::CritScope lock(&crit_);
211 return last_received_packet_ms_;
212}
213
Danil Chapovalov0040b662018-06-18 10:48:16 +0200214absl::optional<int64_t> PacketBuffer::LastReceivedKeyframePacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700215 rtc::CritScope lock(&crit_);
216 return last_received_keyframe_packet_ms_;
philipelaee3e0e2016-11-01 11:45:34 +0100217}
218
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100219int PacketBuffer::GetUniqueFramesSeen() const {
220 rtc::CritScope lock(&crit_);
221 return unique_frames_seen_;
222}
223
philipelc707ab72016-04-01 02:01:54 -0700224bool PacketBuffer::ExpandBufferSize() {
philipelaee3e0e2016-11-01 11:45:34 +0100225 if (size_ == max_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100226 RTC_LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
227 << "), failed to increase size. Clearing PacketBuffer.";
philipelc703dc22017-03-23 06:50:37 -0700228 Clear();
philipelc707ab72016-04-01 02:01:54 -0700229 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100230 }
philipelc707ab72016-04-01 02:01:54 -0700231
232 size_t new_size = std::min(max_size_, 2 * size_);
233 std::vector<VCMPacket> new_data_buffer(new_size);
234 std::vector<ContinuityInfo> new_sequence_buffer(new_size);
235 for (size_t i = 0; i < size_; ++i) {
236 if (sequence_buffer_[i].used) {
philipelf4139332016-04-20 10:26:34 +0200237 size_t index = sequence_buffer_[i].seq_num % new_size;
philipelc707ab72016-04-01 02:01:54 -0700238 new_sequence_buffer[index] = sequence_buffer_[i];
239 new_data_buffer[index] = data_buffer_[i];
240 }
241 }
242 size_ = new_size;
243 sequence_buffer_ = std::move(new_sequence_buffer);
244 data_buffer_ = std::move(new_data_buffer);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100245 RTC_LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700246 return true;
247}
248
philipelaee3e0e2016-11-01 11:45:34 +0100249bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
philipelf4139332016-04-20 10:26:34 +0200250 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -0700251 int prev_index = index > 0 ? index - 1 : size_ - 1;
philipelf4139332016-04-20 10:26:34 +0200252
philipelc707ab72016-04-01 02:01:54 -0700253 if (!sequence_buffer_[index].used)
254 return false;
philipel2c9f9f22017-06-13 02:47:28 -0700255 if (sequence_buffer_[index].seq_num != seq_num)
256 return false;
philipelf4139332016-04-20 10:26:34 +0200257 if (sequence_buffer_[index].frame_created)
258 return false;
philipel20dce342016-11-28 16:14:57 +0100259 if (sequence_buffer_[index].frame_begin)
philipelc707ab72016-04-01 02:01:54 -0700260 return true;
261 if (!sequence_buffer_[prev_index].used)
262 return false;
philipelea142f82017-01-11 02:01:56 -0800263 if (sequence_buffer_[prev_index].frame_created)
264 return false;
philipelf4139332016-04-20 10:26:34 +0200265 if (sequence_buffer_[prev_index].seq_num !=
philipel2c2f34c2017-01-03 05:55:34 -0800266 static_cast<uint16_t>(sequence_buffer_[index].seq_num - 1)) {
philipelf4139332016-04-20 10:26:34 +0200267 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100268 }
philipelc707ab72016-04-01 02:01:54 -0700269 if (sequence_buffer_[prev_index].continuous)
270 return true;
271
272 return false;
273}
274
philipelfd5a20f2016-11-15 00:57:57 -0800275std::vector<std::unique_ptr<RtpFrameObject>> PacketBuffer::FindFrames(
276 uint16_t seq_num) {
277 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
philipel53910712017-05-18 02:24:40 -0700278 for (size_t i = 0; i < size_ && PotentialNewFrame(seq_num); ++i) {
philipelaee3e0e2016-11-01 11:45:34 +0100279 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -0700280 sequence_buffer_[index].continuous = true;
281
philipelf4139332016-04-20 10:26:34 +0200282 // If all packets of the frame is continuous, find the first packet of the
283 // frame and create an RtpFrameObject.
philipelc707ab72016-04-01 02:01:54 -0700284 if (sequence_buffer_[index].frame_end) {
philipel5ceaaae2016-05-24 10:20:47 +0200285 size_t frame_size = 0;
286 int max_nack_count = -1;
philipelc707ab72016-04-01 02:01:54 -0700287 uint16_t start_seq_num = seq_num;
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100288 int64_t min_recv_time = data_buffer_[index].receive_time_ms;
289 int64_t max_recv_time = data_buffer_[index].receive_time_ms;
philipelf4139332016-04-20 10:26:34 +0200290
philipel5ceaaae2016-05-24 10:20:47 +0200291 // Find the start index by searching backward until the packet with
292 // the |frame_begin| flag is set.
293 int start_index = index;
philipel227f8b92017-08-04 06:39:31 -0700294 size_t tested_packets = 0;
philipel8c619242017-02-02 08:51:29 -0800295 int64_t frame_timestamp = data_buffer_[start_index].timestamp;
philipel53910712017-05-18 02:24:40 -0700296
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100297 // Identify H.264 keyframes by means of SPS, PPS, and IDR.
298 bool is_h264 = data_buffer_[start_index].codec == kVideoCodecH264;
299 bool has_h264_sps = false;
300 bool has_h264_pps = false;
301 bool has_h264_idr = false;
302 bool is_h264_keyframe = false;
303
philipel227f8b92017-08-04 06:39:31 -0700304 while (true) {
305 ++tested_packets;
philipel5ceaaae2016-05-24 10:20:47 +0200306 frame_size += data_buffer_[start_index].sizeBytes;
philipelfd5a20f2016-11-15 00:57:57 -0800307 max_nack_count =
308 std::max(max_nack_count, data_buffer_[start_index].timesNacked);
philipelf4139332016-04-20 10:26:34 +0200309 sequence_buffer_[start_index].frame_created = true;
philipel5ceaaae2016-05-24 10:20:47 +0200310
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100311 min_recv_time =
312 std::min(min_recv_time, data_buffer_[start_index].receive_time_ms);
313 max_recv_time =
314 std::max(max_recv_time, data_buffer_[start_index].receive_time_ms);
315
philipel8c619242017-02-02 08:51:29 -0800316 if (!is_h264 && sequence_buffer_[start_index].frame_begin)
philipel5ceaaae2016-05-24 10:20:47 +0200317 break;
318
philipel2c9f9f22017-06-13 02:47:28 -0700319 if (is_h264 && !is_h264_keyframe) {
philipel7d745e52018-08-02 14:03:53 +0200320 const auto* h264_header = absl::get_if<RTPVideoHeaderH264>(
321 &data_buffer_[start_index].video_header.video_type_header);
322 if (!h264_header || h264_header->nalus_length >= kMaxNalusPerPacket)
philipel09133af2018-05-17 14:11:09 +0200323 return found_frames;
324
philipel7d745e52018-08-02 14:03:53 +0200325 for (size_t j = 0; j < h264_header->nalus_length; ++j) {
326 if (h264_header->nalus[j].type == H264::NaluType::kSps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100327 has_h264_sps = true;
philipel7d745e52018-08-02 14:03:53 +0200328 } else if (h264_header->nalus[j].type == H264::NaluType::kPps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100329 has_h264_pps = true;
philipel7d745e52018-08-02 14:03:53 +0200330 } else if (h264_header->nalus[j].type == H264::NaluType::kIdr) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100331 has_h264_idr = true;
philipel2c9f9f22017-06-13 02:47:28 -0700332 }
333 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100334 if ((sps_pps_idr_is_h264_keyframe_ && has_h264_idr && has_h264_sps &&
335 has_h264_pps) ||
336 (!sps_pps_idr_is_h264_keyframe_ && has_h264_idr)) {
337 is_h264_keyframe = true;
338 }
philipel2c9f9f22017-06-13 02:47:28 -0700339 }
340
philipel227f8b92017-08-04 06:39:31 -0700341 if (tested_packets == size_)
342 break;
343
philipelf4139332016-04-20 10:26:34 +0200344 start_index = start_index > 0 ? start_index - 1 : size_ - 1;
philipel8c619242017-02-02 08:51:29 -0800345
346 // In the case of H264 we don't have a frame_begin bit (yes,
347 // |frame_begin| might be set to true but that is a lie). So instead
348 // we traverese backwards as long as we have a previous packet and
349 // the timestamp of that packet is the same as this one. This may cause
350 // the PacketBuffer to hand out incomplete frames.
351 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
philipel53910712017-05-18 02:24:40 -0700352 if (is_h264 &&
philipel8c619242017-02-02 08:51:29 -0800353 (!sequence_buffer_[start_index].used ||
354 data_buffer_[start_index].timestamp != frame_timestamp)) {
355 break;
356 }
357
358 --start_seq_num;
philipelc707ab72016-04-01 02:01:54 -0700359 }
360
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100361 if (is_h264) {
362 // Warn if this is an unsafe frame.
363 if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) {
Jonas Olssonfc501102018-06-15 14:24:10 +0200364 RTC_LOG(LS_WARNING)
365 << "Received H.264-IDR frame "
366 << "(SPS: " << has_h264_sps << ", PPS: " << has_h264_pps
367 << "). Treating as "
368 << (sps_pps_idr_is_h264_keyframe_ ? "delta" : "key")
369 << " frame since WebRTC-SpsPpsIdrIsH264Keyframe is "
370 << (sps_pps_idr_is_h264_keyframe_ ? "enabled." : "disabled");
philipel2c9f9f22017-06-13 02:47:28 -0700371 }
372
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100373 // Now that we have decided whether to treat this frame as a key frame
374 // or delta frame in the frame buffer, we update the field that
375 // determines if the RtpFrameObject is a key frame or delta frame.
376 const size_t first_packet_index = start_seq_num % size_;
377 RTC_CHECK_LT(first_packet_index, size_);
378 if (is_h264_keyframe) {
379 data_buffer_[first_packet_index].frameType = kVideoFrameKey;
380 } else {
381 data_buffer_[first_packet_index].frameType = kVideoFrameDelta;
382 }
383
384 // If this is not a keyframe, make sure there are no gaps in the
385 // packet sequence numbers up until this point.
386 if (!is_h264_keyframe && missing_packets_.upper_bound(start_seq_num) !=
387 missing_packets_.begin()) {
388 uint16_t stop_index = (index + 1) % size_;
389 while (start_index != stop_index) {
390 sequence_buffer_[start_index].frame_created = false;
391 start_index = (start_index + 1) % size_;
392 }
393
394 return found_frames;
395 }
philipel2c9f9f22017-06-13 02:47:28 -0700396 }
397
398 missing_packets_.erase(missing_packets_.begin(),
399 missing_packets_.upper_bound(seq_num));
400
philipelfd5a20f2016-11-15 00:57:57 -0800401 found_frames.emplace_back(
philipelb4d31082016-07-11 08:46:29 -0700402 new RtpFrameObject(this, start_seq_num, seq_num, frame_size,
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100403 max_nack_count, min_recv_time, max_recv_time));
philipelc707ab72016-04-01 02:01:54 -0700404 }
philipelc707ab72016-04-01 02:01:54 -0700405 ++seq_num;
406 }
philipelfd5a20f2016-11-15 00:57:57 -0800407 return found_frames;
philipelc707ab72016-04-01 02:01:54 -0700408}
409
410void PacketBuffer::ReturnFrame(RtpFrameObject* frame) {
411 rtc::CritScope lock(&crit_);
philipelf4139332016-04-20 10:26:34 +0200412 size_t index = frame->first_seq_num() % size_;
413 size_t end = (frame->last_seq_num() + 1) % size_;
414 uint16_t seq_num = frame->first_seq_num();
Johannes Kron957c62e2018-10-01 14:53:01 +0200415 uint32_t timestamp = frame->Timestamp();
philipelc707ab72016-04-01 02:01:54 -0700416 while (index != end) {
Johannes Kron957c62e2018-10-01 14:53:01 +0200417 // Check both seq_num and timestamp to handle the case when seq_num wraps
418 // around too quickly for high packet rates.
419 if (sequence_buffer_[index].seq_num == seq_num &&
420 data_buffer_[index].timestamp == timestamp) {
philipel1f39ba12016-09-21 11:27:47 +0200421 delete[] data_buffer_[index].dataPtr;
422 data_buffer_[index].dataPtr = nullptr;
philipelc707ab72016-04-01 02:01:54 -0700423 sequence_buffer_[index].used = false;
philipel1f39ba12016-09-21 11:27:47 +0200424 }
philipelf4139332016-04-20 10:26:34 +0200425
philipelc707ab72016-04-01 02:01:54 -0700426 index = (index + 1) % size_;
427 ++seq_num;
428 }
philipelc707ab72016-04-01 02:01:54 -0700429}
430
431bool PacketBuffer::GetBitstream(const RtpFrameObject& frame,
432 uint8_t* destination) {
433 rtc::CritScope lock(&crit_);
434
philipelf4139332016-04-20 10:26:34 +0200435 size_t index = frame.first_seq_num() % size_;
436 size_t end = (frame.last_seq_num() + 1) % size_;
437 uint16_t seq_num = frame.first_seq_num();
Johannes Kron957c62e2018-10-01 14:53:01 +0200438 uint32_t timestamp = frame.Timestamp();
philipel227f8b92017-08-04 06:39:31 -0700439 uint8_t* destination_end = destination + frame.size();
440
441 do {
Johannes Kron957c62e2018-10-01 14:53:01 +0200442 // Check both seq_num and timestamp to handle the case when seq_num wraps
443 // around too quickly for high packet rates.
philipelc707ab72016-04-01 02:01:54 -0700444 if (!sequence_buffer_[index].used ||
Johannes Kron957c62e2018-10-01 14:53:01 +0200445 sequence_buffer_[index].seq_num != seq_num ||
446 data_buffer_[index].timestamp != timestamp) {
philipelc707ab72016-04-01 02:01:54 -0700447 return false;
448 }
449
philipel227f8b92017-08-04 06:39:31 -0700450 RTC_DCHECK_EQ(data_buffer_[index].seqNum, sequence_buffer_[index].seq_num);
philipelc18f1d72017-08-02 04:18:02 -0700451 size_t length = data_buffer_[index].sizeBytes;
philipel227f8b92017-08-04 06:39:31 -0700452 if (destination + length > destination_end) {
philipel0fa82a62018-03-19 15:34:53 +0100453 RTC_LOG(LS_WARNING) << "Frame (" << frame.id.picture_id << ":"
454 << static_cast<int>(frame.id.spatial_layer) << ")"
Mirko Bonadei675513b2017-11-09 11:09:25 +0100455 << " bitstream buffer is not large enough.";
philipel227f8b92017-08-04 06:39:31 -0700456 return false;
457 }
458
459 const uint8_t* source = data_buffer_[index].dataPtr;
philipelc707ab72016-04-01 02:01:54 -0700460 memcpy(destination, source, length);
461 destination += length;
462 index = (index + 1) % size_;
463 ++seq_num;
philipel227f8b92017-08-04 06:39:31 -0700464 } while (index != end);
465
philipelc707ab72016-04-01 02:01:54 -0700466 return true;
467}
468
philipel02447bc2016-05-13 06:01:03 -0700469VCMPacket* PacketBuffer::GetPacket(uint16_t seq_num) {
philipel02447bc2016-05-13 06:01:03 -0700470 size_t index = seq_num % size_;
471 if (!sequence_buffer_[index].used ||
472 seq_num != sequence_buffer_[index].seq_num) {
473 return nullptr;
philipelf4139332016-04-20 10:26:34 +0200474 }
philipel02447bc2016-05-13 06:01:03 -0700475 return &data_buffer_[index];
philipelf4139332016-04-20 10:26:34 +0200476}
477
philipel17deeb42016-08-11 15:09:26 +0200478int PacketBuffer::AddRef() const {
479 return rtc::AtomicOps::Increment(&ref_count_);
480}
481
482int PacketBuffer::Release() const {
483 int count = rtc::AtomicOps::Decrement(&ref_count_);
484 if (!count) {
485 delete this;
486 }
487 return count;
488}
489
philipel2c9f9f22017-06-13 02:47:28 -0700490void PacketBuffer::UpdateMissingPackets(uint16_t seq_num) {
491 if (!newest_inserted_seq_num_)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100492 newest_inserted_seq_num_ = seq_num;
philipel2c9f9f22017-06-13 02:47:28 -0700493
494 const int kMaxPaddingAge = 1000;
495 if (AheadOf(seq_num, *newest_inserted_seq_num_)) {
496 uint16_t old_seq_num = seq_num - kMaxPaddingAge;
497 auto erase_to = missing_packets_.lower_bound(old_seq_num);
498 missing_packets_.erase(missing_packets_.begin(), erase_to);
499
500 // Guard against inserting a large amount of missing packets if there is a
501 // jump in the sequence number.
502 if (AheadOf(old_seq_num, *newest_inserted_seq_num_))
503 *newest_inserted_seq_num_ = old_seq_num;
504
505 ++*newest_inserted_seq_num_;
506 while (AheadOf(seq_num, *newest_inserted_seq_num_)) {
507 missing_packets_.insert(*newest_inserted_seq_num_);
508 ++*newest_inserted_seq_num_;
509 }
510 } else {
511 missing_packets_.erase(seq_num);
512 }
513}
514
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100515void PacketBuffer::OnTimestampReceived(uint32_t rtp_timestamp) {
516 const size_t kMaxTimestampsHistory = 1000;
517 if (rtp_timestamps_history_set_.insert(rtp_timestamp).second) {
518 rtp_timestamps_history_queue_.push(rtp_timestamp);
519 ++unique_frames_seen_;
520 if (rtp_timestamps_history_set_.size() > kMaxTimestampsHistory) {
521 uint32_t discarded_timestamp = rtp_timestamps_history_queue_.front();
522 rtp_timestamps_history_set_.erase(discarded_timestamp);
523 rtp_timestamps_history_queue_.pop();
524 }
525 }
526}
527
philipelc707ab72016-04-01 02:01:54 -0700528} // namespace video_coding
529} // namespace webrtc