blob: e1172b374968031ff2273c33d2b1fef5d8eba7d5 [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
13#include <algorithm>
14#include <limits>
philipel17deeb42016-08-11 15:09:26 +020015#include <utility>
philipelc707ab72016-04-01 02:01:54 -070016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "common_video/h264/h264_common.h"
18#include "modules/video_coding/frame_object.h"
19#include "rtc_base/atomicops.h"
20#include "rtc_base/checks.h"
21#include "rtc_base/logging.h"
22#include "system_wrappers/include/clock.h"
Rasmus Brandt88f080a2017-11-02 14:28:06 +010023#include "system_wrappers/include/field_trial.h"
philipelc707ab72016-04-01 02:01:54 -070024
25namespace webrtc {
26namespace video_coding {
27
philipel17deeb42016-08-11 15:09:26 +020028rtc::scoped_refptr<PacketBuffer> PacketBuffer::Create(
29 Clock* clock,
30 size_t start_buffer_size,
31 size_t max_buffer_size,
32 OnReceivedFrameCallback* received_frame_callback) {
33 return rtc::scoped_refptr<PacketBuffer>(new PacketBuffer(
34 clock, start_buffer_size, max_buffer_size, received_frame_callback));
35}
36
philipelb4d31082016-07-11 08:46:29 -070037PacketBuffer::PacketBuffer(Clock* clock,
38 size_t start_buffer_size,
philipelc707ab72016-04-01 02:01:54 -070039 size_t max_buffer_size,
philipel17deeb42016-08-11 15:09:26 +020040 OnReceivedFrameCallback* received_frame_callback)
philipelb4d31082016-07-11 08:46:29 -070041 : clock_(clock),
42 size_(start_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070043 max_size_(max_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070044 first_seq_num_(0),
philipelf4139332016-04-20 10:26:34 +020045 first_packet_received_(false),
philipelaee3e0e2016-11-01 11:45:34 +010046 is_cleared_to_first_seq_num_(false),
philipelc707ab72016-04-01 02:01:54 -070047 data_buffer_(start_buffer_size),
48 sequence_buffer_(start_buffer_size),
Rasmus Brandt88f080a2017-11-02 14:28:06 +010049 received_frame_callback_(received_frame_callback),
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010050 unique_frames_seen_(0),
Rasmus Brandt88f080a2017-11-02 14:28:06 +010051 sps_pps_idr_is_h264_keyframe_(
52 field_trial::IsEnabled("WebRTC-SpsPpsIdrIsH264Keyframe")) {
philipelc707ab72016-04-01 02:01:54 -070053 RTC_DCHECK_LE(start_buffer_size, max_buffer_size);
54 // Buffer size must always be a power of 2.
55 RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0);
56 RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0);
57}
58
philipelaee3e0e2016-11-01 11:45:34 +010059PacketBuffer::~PacketBuffer() {
60 Clear();
61}
philipel17deeb42016-08-11 15:09:26 +020062
philipel759e0b72016-11-30 01:32:05 -080063bool PacketBuffer::InsertPacket(VCMPacket* packet) {
philipelfd5a20f2016-11-15 00:57:57 -080064 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
65 {
66 rtc::CritScope lock(&crit_);
philipel3184f8e2017-05-18 08:08:53 -070067
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010068 OnTimestampReceived(packet->timestamp);
69
philipel759e0b72016-11-30 01:32:05 -080070 uint16_t seq_num = packet->seqNum;
philipelfd5a20f2016-11-15 00:57:57 -080071 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -070072
philipelfd5a20f2016-11-15 00:57:57 -080073 if (!first_packet_received_) {
74 first_seq_num_ = seq_num;
philipelfd5a20f2016-11-15 00:57:57 -080075 first_packet_received_ = true;
76 } else if (AheadOf(first_seq_num_, seq_num)) {
77 // If we have explicitly cleared past this packet then it's old,
78 // don't insert it.
philipel759e0b72016-11-30 01:32:05 -080079 if (is_cleared_to_first_seq_num_) {
80 delete[] packet->dataPtr;
81 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -080082 return false;
philipel759e0b72016-11-30 01:32:05 -080083 }
philipelaee3e0e2016-11-01 11:45:34 +010084
philipelfd5a20f2016-11-15 00:57:57 -080085 first_seq_num_ = seq_num;
philipelc707ab72016-04-01 02:01:54 -070086 }
philipelc707ab72016-04-01 02:01:54 -070087
philipelfd5a20f2016-11-15 00:57:57 -080088 if (sequence_buffer_[index].used) {
philipel759e0b72016-11-30 01:32:05 -080089 // Duplicate packet, just delete the payload.
90 if (data_buffer_[index].seqNum == packet->seqNum) {
91 delete[] packet->dataPtr;
92 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -080093 return true;
philipel759e0b72016-11-30 01:32:05 -080094 }
philipelfd5a20f2016-11-15 00:57:57 -080095
96 // The packet buffer is full, try to expand the buffer.
97 while (ExpandBufferSize() && sequence_buffer_[seq_num % size_].used) {
98 }
99 index = seq_num % size_;
100
101 // Packet buffer is still full.
philipel759e0b72016-11-30 01:32:05 -0800102 if (sequence_buffer_[index].used) {
103 delete[] packet->dataPtr;
104 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -0800105 return false;
philipel759e0b72016-11-30 01:32:05 -0800106 }
philipelfd5a20f2016-11-15 00:57:57 -0800107 }
108
johan0d1b2b62017-01-10 04:21:35 -0800109 sequence_buffer_[index].frame_begin = packet->is_first_packet_in_frame;
philipel759e0b72016-11-30 01:32:05 -0800110 sequence_buffer_[index].frame_end = packet->markerBit;
111 sequence_buffer_[index].seq_num = packet->seqNum;
philipelfd5a20f2016-11-15 00:57:57 -0800112 sequence_buffer_[index].continuous = false;
113 sequence_buffer_[index].frame_created = false;
114 sequence_buffer_[index].used = true;
philipel759e0b72016-11-30 01:32:05 -0800115 data_buffer_[index] = *packet;
116 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -0800117
philipel2c9f9f22017-06-13 02:47:28 -0700118 UpdateMissingPackets(packet->seqNum);
119
philipel3184f8e2017-05-18 08:08:53 -0700120 int64_t now_ms = clock_->TimeInMilliseconds();
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100121 last_received_packet_ms_ = now_ms;
philipel3184f8e2017-05-18 08:08:53 -0700122 if (packet->frameType == kVideoFrameKey)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100123 last_received_keyframe_packet_ms_ = now_ms;
philipel3184f8e2017-05-18 08:08:53 -0700124
philipelfd5a20f2016-11-15 00:57:57 -0800125 found_frames = FindFrames(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700126 }
127
philipelfd5a20f2016-11-15 00:57:57 -0800128 for (std::unique_ptr<RtpFrameObject>& frame : found_frames)
129 received_frame_callback_->OnReceivedFrame(std::move(frame));
philipelc707ab72016-04-01 02:01:54 -0700130
philipelc707ab72016-04-01 02:01:54 -0700131 return true;
132}
133
134void PacketBuffer::ClearTo(uint16_t seq_num) {
135 rtc::CritScope lock(&crit_);
philipelc5fb4682017-08-02 04:28:57 -0700136 // We have already cleared past this sequence number, no need to do anything.
137 if (is_cleared_to_first_seq_num_ &&
138 AheadOf<uint16_t>(first_seq_num_, seq_num)) {
139 return;
140 }
philipelaee3e0e2016-11-01 11:45:34 +0100141
142 // If the packet buffer was cleared between a frame was created and returned.
143 if (!first_packet_received_)
144 return;
145
philipelc5fb4682017-08-02 04:28:57 -0700146 // Avoid iterating over the buffer more than once by capping the number of
147 // iterations to the |size_| of the buffer.
148 ++seq_num;
149 size_t diff = ForwardDiff<uint16_t>(first_seq_num_, seq_num);
150 size_t iterations = std::min(diff, size_);
151 for (size_t i = 0; i < iterations; ++i) {
philipelaee3e0e2016-11-01 11:45:34 +0100152 size_t index = first_seq_num_ % size_;
philipelc5fb4682017-08-02 04:28:57 -0700153 RTC_DCHECK_EQ(data_buffer_[index].seqNum, sequence_buffer_[index].seq_num);
154 if (AheadOf<uint16_t>(seq_num, sequence_buffer_[index].seq_num)) {
155 delete[] data_buffer_[index].dataPtr;
156 data_buffer_[index].dataPtr = nullptr;
157 sequence_buffer_[index].used = false;
158 }
philipelaee3e0e2016-11-01 11:45:34 +0100159 ++first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700160 }
philipel2c9f9f22017-06-13 02:47:28 -0700161
philipelc5fb4682017-08-02 04:28:57 -0700162 // If |diff| is larger than |iterations| it means that we don't increment
163 // |first_seq_num_| until we reach |seq_num|, so we set it here.
164 first_seq_num_ = seq_num;
165
166 is_cleared_to_first_seq_num_ = true;
philipelbc5a4082017-12-06 10:41:08 +0100167 auto clear_to_it = missing_packets_.upper_bound(seq_num);
168 if (clear_to_it != missing_packets_.begin()) {
169 --clear_to_it;
170 missing_packets_.erase(missing_packets_.begin(), clear_to_it);
171 }
philipelc707ab72016-04-01 02:01:54 -0700172}
173
philipelaee3e0e2016-11-01 11:45:34 +0100174void PacketBuffer::Clear() {
175 rtc::CritScope lock(&crit_);
176 for (size_t i = 0; i < size_; ++i) {
177 delete[] data_buffer_[i].dataPtr;
178 data_buffer_[i].dataPtr = nullptr;
179 sequence_buffer_[i].used = false;
180 }
181
182 first_packet_received_ = false;
183 is_cleared_to_first_seq_num_ = false;
philipel2c9f9f22017-06-13 02:47:28 -0700184 last_received_packet_ms_.reset();
185 last_received_keyframe_packet_ms_.reset();
186 newest_inserted_seq_num_.reset();
187 missing_packets_.clear();
188}
189
190void PacketBuffer::PaddingReceived(uint16_t seq_num) {
191 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
192 {
193 rtc::CritScope lock(&crit_);
194 UpdateMissingPackets(seq_num);
195 found_frames = FindFrames(static_cast<uint16_t>(seq_num + 1));
196 }
197
198 for (std::unique_ptr<RtpFrameObject>& frame : found_frames)
199 received_frame_callback_->OnReceivedFrame(std::move(frame));
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
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100212int PacketBuffer::GetUniqueFramesSeen() const {
213 rtc::CritScope lock(&crit_);
214 return unique_frames_seen_;
215}
216
philipelc707ab72016-04-01 02:01:54 -0700217bool PacketBuffer::ExpandBufferSize() {
philipelaee3e0e2016-11-01 11:45:34 +0100218 if (size_ == max_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100219 RTC_LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
220 << "), failed to increase size. Clearing PacketBuffer.";
philipelc703dc22017-03-23 06:50:37 -0700221 Clear();
philipelc707ab72016-04-01 02:01:54 -0700222 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100223 }
philipelc707ab72016-04-01 02:01:54 -0700224
225 size_t new_size = std::min(max_size_, 2 * size_);
226 std::vector<VCMPacket> new_data_buffer(new_size);
227 std::vector<ContinuityInfo> new_sequence_buffer(new_size);
228 for (size_t i = 0; i < size_; ++i) {
229 if (sequence_buffer_[i].used) {
philipelf4139332016-04-20 10:26:34 +0200230 size_t index = sequence_buffer_[i].seq_num % new_size;
philipelc707ab72016-04-01 02:01:54 -0700231 new_sequence_buffer[index] = sequence_buffer_[i];
232 new_data_buffer[index] = data_buffer_[i];
233 }
234 }
235 size_ = new_size;
236 sequence_buffer_ = std::move(new_sequence_buffer);
237 data_buffer_ = std::move(new_data_buffer);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100238 RTC_LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700239 return true;
240}
241
philipelaee3e0e2016-11-01 11:45:34 +0100242bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
philipelf4139332016-04-20 10:26:34 +0200243 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -0700244 int prev_index = index > 0 ? index - 1 : size_ - 1;
philipelf4139332016-04-20 10:26:34 +0200245
philipelc707ab72016-04-01 02:01:54 -0700246 if (!sequence_buffer_[index].used)
247 return false;
philipel2c9f9f22017-06-13 02:47:28 -0700248 if (sequence_buffer_[index].seq_num != seq_num)
249 return false;
philipelf4139332016-04-20 10:26:34 +0200250 if (sequence_buffer_[index].frame_created)
251 return false;
philipel20dce342016-11-28 16:14:57 +0100252 if (sequence_buffer_[index].frame_begin)
philipelc707ab72016-04-01 02:01:54 -0700253 return true;
254 if (!sequence_buffer_[prev_index].used)
255 return false;
philipelea142f82017-01-11 02:01:56 -0800256 if (sequence_buffer_[prev_index].frame_created)
257 return false;
philipelf4139332016-04-20 10:26:34 +0200258 if (sequence_buffer_[prev_index].seq_num !=
philipel2c2f34c2017-01-03 05:55:34 -0800259 static_cast<uint16_t>(sequence_buffer_[index].seq_num - 1)) {
philipelf4139332016-04-20 10:26:34 +0200260 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100261 }
philipelc707ab72016-04-01 02:01:54 -0700262 if (sequence_buffer_[prev_index].continuous)
263 return true;
264
265 return false;
266}
267
philipelfd5a20f2016-11-15 00:57:57 -0800268std::vector<std::unique_ptr<RtpFrameObject>> PacketBuffer::FindFrames(
269 uint16_t seq_num) {
270 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
philipel53910712017-05-18 02:24:40 -0700271 for (size_t i = 0; i < size_ && PotentialNewFrame(seq_num); ++i) {
philipelaee3e0e2016-11-01 11:45:34 +0100272 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -0700273 sequence_buffer_[index].continuous = true;
274
philipelf4139332016-04-20 10:26:34 +0200275 // If all packets of the frame is continuous, find the first packet of the
276 // frame and create an RtpFrameObject.
philipelc707ab72016-04-01 02:01:54 -0700277 if (sequence_buffer_[index].frame_end) {
philipel5ceaaae2016-05-24 10:20:47 +0200278 size_t frame_size = 0;
279 int max_nack_count = -1;
philipelc707ab72016-04-01 02:01:54 -0700280 uint16_t start_seq_num = seq_num;
philipelf4139332016-04-20 10:26:34 +0200281
philipel5ceaaae2016-05-24 10:20:47 +0200282 // Find the start index by searching backward until the packet with
283 // the |frame_begin| flag is set.
284 int start_index = index;
philipel227f8b92017-08-04 06:39:31 -0700285 size_t tested_packets = 0;
philipel8c619242017-02-02 08:51:29 -0800286 int64_t frame_timestamp = data_buffer_[start_index].timestamp;
philipel53910712017-05-18 02:24:40 -0700287
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100288 // Identify H.264 keyframes by means of SPS, PPS, and IDR.
289 bool is_h264 = data_buffer_[start_index].codec == kVideoCodecH264;
290 bool has_h264_sps = false;
291 bool has_h264_pps = false;
292 bool has_h264_idr = false;
293 bool is_h264_keyframe = false;
294
philipel227f8b92017-08-04 06:39:31 -0700295 while (true) {
296 ++tested_packets;
philipel5ceaaae2016-05-24 10:20:47 +0200297 frame_size += data_buffer_[start_index].sizeBytes;
philipelfd5a20f2016-11-15 00:57:57 -0800298 max_nack_count =
299 std::max(max_nack_count, data_buffer_[start_index].timesNacked);
philipelf4139332016-04-20 10:26:34 +0200300 sequence_buffer_[start_index].frame_created = true;
philipel5ceaaae2016-05-24 10:20:47 +0200301
philipel8c619242017-02-02 08:51:29 -0800302 if (!is_h264 && sequence_buffer_[start_index].frame_begin)
philipel5ceaaae2016-05-24 10:20:47 +0200303 break;
304
philipel2c9f9f22017-06-13 02:47:28 -0700305 if (is_h264 && !is_h264_keyframe) {
philipel7d745e52018-08-02 14:03:53 +0200306 const auto* h264_header = absl::get_if<RTPVideoHeaderH264>(
307 &data_buffer_[start_index].video_header.video_type_header);
308 if (!h264_header || h264_header->nalus_length >= kMaxNalusPerPacket)
philipel09133af2018-05-17 14:11:09 +0200309 return found_frames;
310
philipel7d745e52018-08-02 14:03:53 +0200311 for (size_t j = 0; j < h264_header->nalus_length; ++j) {
312 if (h264_header->nalus[j].type == H264::NaluType::kSps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100313 has_h264_sps = true;
philipel7d745e52018-08-02 14:03:53 +0200314 } else if (h264_header->nalus[j].type == H264::NaluType::kPps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100315 has_h264_pps = true;
philipel7d745e52018-08-02 14:03:53 +0200316 } else if (h264_header->nalus[j].type == H264::NaluType::kIdr) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100317 has_h264_idr = true;
philipel2c9f9f22017-06-13 02:47:28 -0700318 }
319 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100320 if ((sps_pps_idr_is_h264_keyframe_ && has_h264_idr && has_h264_sps &&
321 has_h264_pps) ||
322 (!sps_pps_idr_is_h264_keyframe_ && has_h264_idr)) {
323 is_h264_keyframe = true;
324 }
philipel2c9f9f22017-06-13 02:47:28 -0700325 }
326
philipel227f8b92017-08-04 06:39:31 -0700327 if (tested_packets == size_)
328 break;
329
philipelf4139332016-04-20 10:26:34 +0200330 start_index = start_index > 0 ? start_index - 1 : size_ - 1;
philipel8c619242017-02-02 08:51:29 -0800331
332 // In the case of H264 we don't have a frame_begin bit (yes,
333 // |frame_begin| might be set to true but that is a lie). So instead
334 // we traverese backwards as long as we have a previous packet and
335 // the timestamp of that packet is the same as this one. This may cause
336 // the PacketBuffer to hand out incomplete frames.
337 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
philipel53910712017-05-18 02:24:40 -0700338 if (is_h264 &&
philipel8c619242017-02-02 08:51:29 -0800339 (!sequence_buffer_[start_index].used ||
340 data_buffer_[start_index].timestamp != frame_timestamp)) {
341 break;
342 }
343
344 --start_seq_num;
philipelc707ab72016-04-01 02:01:54 -0700345 }
346
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100347 if (is_h264) {
348 // Warn if this is an unsafe frame.
349 if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) {
Jonas Olssonfc501102018-06-15 14:24:10 +0200350 RTC_LOG(LS_WARNING)
351 << "Received H.264-IDR frame "
352 << "(SPS: " << has_h264_sps << ", PPS: " << has_h264_pps
353 << "). Treating as "
354 << (sps_pps_idr_is_h264_keyframe_ ? "delta" : "key")
355 << " frame since WebRTC-SpsPpsIdrIsH264Keyframe is "
356 << (sps_pps_idr_is_h264_keyframe_ ? "enabled." : "disabled");
philipel2c9f9f22017-06-13 02:47:28 -0700357 }
358
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100359 // Now that we have decided whether to treat this frame as a key frame
360 // or delta frame in the frame buffer, we update the field that
361 // determines if the RtpFrameObject is a key frame or delta frame.
362 const size_t first_packet_index = start_seq_num % size_;
363 RTC_CHECK_LT(first_packet_index, size_);
364 if (is_h264_keyframe) {
365 data_buffer_[first_packet_index].frameType = kVideoFrameKey;
366 } else {
367 data_buffer_[first_packet_index].frameType = kVideoFrameDelta;
368 }
369
370 // If this is not a keyframe, make sure there are no gaps in the
371 // packet sequence numbers up until this point.
372 if (!is_h264_keyframe && missing_packets_.upper_bound(start_seq_num) !=
373 missing_packets_.begin()) {
374 uint16_t stop_index = (index + 1) % size_;
375 while (start_index != stop_index) {
376 sequence_buffer_[start_index].frame_created = false;
377 start_index = (start_index + 1) % size_;
378 }
379
380 return found_frames;
381 }
philipel2c9f9f22017-06-13 02:47:28 -0700382 }
383
384 missing_packets_.erase(missing_packets_.begin(),
385 missing_packets_.upper_bound(seq_num));
386
philipelfd5a20f2016-11-15 00:57:57 -0800387 found_frames.emplace_back(
philipelb4d31082016-07-11 08:46:29 -0700388 new RtpFrameObject(this, start_seq_num, seq_num, frame_size,
389 max_nack_count, clock_->TimeInMilliseconds()));
philipelc707ab72016-04-01 02:01:54 -0700390 }
philipelc707ab72016-04-01 02:01:54 -0700391 ++seq_num;
392 }
philipelfd5a20f2016-11-15 00:57:57 -0800393 return found_frames;
philipelc707ab72016-04-01 02:01:54 -0700394}
395
396void PacketBuffer::ReturnFrame(RtpFrameObject* frame) {
397 rtc::CritScope lock(&crit_);
philipelf4139332016-04-20 10:26:34 +0200398 size_t index = frame->first_seq_num() % size_;
399 size_t end = (frame->last_seq_num() + 1) % size_;
400 uint16_t seq_num = frame->first_seq_num();
philipelc707ab72016-04-01 02:01:54 -0700401 while (index != end) {
philipel1f39ba12016-09-21 11:27:47 +0200402 if (sequence_buffer_[index].seq_num == seq_num) {
403 delete[] data_buffer_[index].dataPtr;
404 data_buffer_[index].dataPtr = nullptr;
philipelc707ab72016-04-01 02:01:54 -0700405 sequence_buffer_[index].used = false;
philipel1f39ba12016-09-21 11:27:47 +0200406 }
philipelf4139332016-04-20 10:26:34 +0200407
philipelc707ab72016-04-01 02:01:54 -0700408 index = (index + 1) % size_;
409 ++seq_num;
410 }
philipelc707ab72016-04-01 02:01:54 -0700411}
412
413bool PacketBuffer::GetBitstream(const RtpFrameObject& frame,
414 uint8_t* destination) {
415 rtc::CritScope lock(&crit_);
416
philipelf4139332016-04-20 10:26:34 +0200417 size_t index = frame.first_seq_num() % size_;
418 size_t end = (frame.last_seq_num() + 1) % size_;
419 uint16_t seq_num = frame.first_seq_num();
philipel227f8b92017-08-04 06:39:31 -0700420 uint8_t* destination_end = destination + frame.size();
421
422 do {
philipelc707ab72016-04-01 02:01:54 -0700423 if (!sequence_buffer_[index].used ||
424 sequence_buffer_[index].seq_num != seq_num) {
425 return false;
426 }
427
philipel227f8b92017-08-04 06:39:31 -0700428 RTC_DCHECK_EQ(data_buffer_[index].seqNum, sequence_buffer_[index].seq_num);
philipelc18f1d72017-08-02 04:18:02 -0700429 size_t length = data_buffer_[index].sizeBytes;
philipel227f8b92017-08-04 06:39:31 -0700430 if (destination + length > destination_end) {
philipel0fa82a62018-03-19 15:34:53 +0100431 RTC_LOG(LS_WARNING) << "Frame (" << frame.id.picture_id << ":"
432 << static_cast<int>(frame.id.spatial_layer) << ")"
Mirko Bonadei675513b2017-11-09 11:09:25 +0100433 << " bitstream buffer is not large enough.";
philipel227f8b92017-08-04 06:39:31 -0700434 return false;
435 }
436
437 const uint8_t* source = data_buffer_[index].dataPtr;
philipelc707ab72016-04-01 02:01:54 -0700438 memcpy(destination, source, length);
439 destination += length;
440 index = (index + 1) % size_;
441 ++seq_num;
philipel227f8b92017-08-04 06:39:31 -0700442 } while (index != end);
443
philipelc707ab72016-04-01 02:01:54 -0700444 return true;
445}
446
philipel02447bc2016-05-13 06:01:03 -0700447VCMPacket* PacketBuffer::GetPacket(uint16_t seq_num) {
philipel02447bc2016-05-13 06:01:03 -0700448 size_t index = seq_num % size_;
449 if (!sequence_buffer_[index].used ||
450 seq_num != sequence_buffer_[index].seq_num) {
451 return nullptr;
philipelf4139332016-04-20 10:26:34 +0200452 }
philipel02447bc2016-05-13 06:01:03 -0700453 return &data_buffer_[index];
philipelf4139332016-04-20 10:26:34 +0200454}
455
philipel17deeb42016-08-11 15:09:26 +0200456int PacketBuffer::AddRef() const {
457 return rtc::AtomicOps::Increment(&ref_count_);
458}
459
460int PacketBuffer::Release() const {
461 int count = rtc::AtomicOps::Decrement(&ref_count_);
462 if (!count) {
463 delete this;
464 }
465 return count;
466}
467
philipel2c9f9f22017-06-13 02:47:28 -0700468void PacketBuffer::UpdateMissingPackets(uint16_t seq_num) {
469 if (!newest_inserted_seq_num_)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100470 newest_inserted_seq_num_ = seq_num;
philipel2c9f9f22017-06-13 02:47:28 -0700471
472 const int kMaxPaddingAge = 1000;
473 if (AheadOf(seq_num, *newest_inserted_seq_num_)) {
474 uint16_t old_seq_num = seq_num - kMaxPaddingAge;
475 auto erase_to = missing_packets_.lower_bound(old_seq_num);
476 missing_packets_.erase(missing_packets_.begin(), erase_to);
477
478 // Guard against inserting a large amount of missing packets if there is a
479 // jump in the sequence number.
480 if (AheadOf(old_seq_num, *newest_inserted_seq_num_))
481 *newest_inserted_seq_num_ = old_seq_num;
482
483 ++*newest_inserted_seq_num_;
484 while (AheadOf(seq_num, *newest_inserted_seq_num_)) {
485 missing_packets_.insert(*newest_inserted_seq_num_);
486 ++*newest_inserted_seq_num_;
487 }
488 } else {
489 missing_packets_.erase(seq_num);
490 }
491}
492
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100493void PacketBuffer::OnTimestampReceived(uint32_t rtp_timestamp) {
494 const size_t kMaxTimestampsHistory = 1000;
495 if (rtp_timestamps_history_set_.insert(rtp_timestamp).second) {
496 rtp_timestamps_history_queue_.push(rtp_timestamp);
497 ++unique_frames_seen_;
498 if (rtp_timestamps_history_set_.size() > kMaxTimestampsHistory) {
499 uint32_t discarded_timestamp = rtp_timestamps_history_queue_.front();
500 rtp_timestamps_history_set_.erase(discarded_timestamp);
501 rtp_timestamps_history_queue_.pop();
502 }
503 }
504}
505
philipelc707ab72016-04-01 02:01:54 -0700506} // namespace video_coding
507} // namespace webrtc