blob: 0e4ef6ad6ee42333bc57719f1457440e7891355b [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>
Rasmus Brandt88f080a2017-11-02 14:28:06 +010015#include <sstream>
philipel17deeb42016-08-11 15:09:26 +020016#include <utility>
philipelc707ab72016-04-01 02:01:54 -070017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "common_video/h264/h264_common.h"
19#include "modules/video_coding/frame_object.h"
20#include "rtc_base/atomicops.h"
21#include "rtc_base/checks.h"
22#include "rtc_base/logging.h"
23#include "system_wrappers/include/clock.h"
Rasmus Brandt88f080a2017-11-02 14:28:06 +010024#include "system_wrappers/include/field_trial.h"
philipelc707ab72016-04-01 02:01:54 -070025
26namespace webrtc {
27namespace video_coding {
28
philipel17deeb42016-08-11 15:09:26 +020029rtc::scoped_refptr<PacketBuffer> PacketBuffer::Create(
30 Clock* clock,
31 size_t start_buffer_size,
32 size_t max_buffer_size,
33 OnReceivedFrameCallback* received_frame_callback) {
34 return rtc::scoped_refptr<PacketBuffer>(new PacketBuffer(
35 clock, start_buffer_size, max_buffer_size, received_frame_callback));
36}
37
philipelb4d31082016-07-11 08:46:29 -070038PacketBuffer::PacketBuffer(Clock* clock,
39 size_t start_buffer_size,
philipelc707ab72016-04-01 02:01:54 -070040 size_t max_buffer_size,
philipel17deeb42016-08-11 15:09:26 +020041 OnReceivedFrameCallback* received_frame_callback)
philipelb4d31082016-07-11 08:46:29 -070042 : clock_(clock),
43 size_(start_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070044 max_size_(max_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070045 first_seq_num_(0),
philipelf4139332016-04-20 10:26:34 +020046 first_packet_received_(false),
philipelaee3e0e2016-11-01 11:45:34 +010047 is_cleared_to_first_seq_num_(false),
philipelc707ab72016-04-01 02:01:54 -070048 data_buffer_(start_buffer_size),
49 sequence_buffer_(start_buffer_size),
Rasmus Brandt88f080a2017-11-02 14:28:06 +010050 received_frame_callback_(received_frame_callback),
51 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
philipel759e0b72016-11-30 01:32:05 -080068 uint16_t seq_num = packet->seqNum;
philipelfd5a20f2016-11-15 00:57:57 -080069 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -070070
philipelfd5a20f2016-11-15 00:57:57 -080071 if (!first_packet_received_) {
72 first_seq_num_ = seq_num;
philipelfd5a20f2016-11-15 00:57:57 -080073 first_packet_received_ = true;
74 } else if (AheadOf(first_seq_num_, seq_num)) {
75 // If we have explicitly cleared past this packet then it's old,
76 // don't insert it.
philipel759e0b72016-11-30 01:32:05 -080077 if (is_cleared_to_first_seq_num_) {
78 delete[] packet->dataPtr;
79 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -080080 return false;
philipel759e0b72016-11-30 01:32:05 -080081 }
philipelaee3e0e2016-11-01 11:45:34 +010082
philipelfd5a20f2016-11-15 00:57:57 -080083 first_seq_num_ = seq_num;
philipelc707ab72016-04-01 02:01:54 -070084 }
philipelc707ab72016-04-01 02:01:54 -070085
philipelfd5a20f2016-11-15 00:57:57 -080086 if (sequence_buffer_[index].used) {
philipel759e0b72016-11-30 01:32:05 -080087 // Duplicate packet, just delete the payload.
88 if (data_buffer_[index].seqNum == packet->seqNum) {
89 delete[] packet->dataPtr;
90 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -080091 return true;
philipel759e0b72016-11-30 01:32:05 -080092 }
philipelfd5a20f2016-11-15 00:57:57 -080093
94 // The packet buffer is full, try to expand the buffer.
95 while (ExpandBufferSize() && sequence_buffer_[seq_num % size_].used) {
96 }
97 index = seq_num % size_;
98
99 // Packet buffer is still full.
philipel759e0b72016-11-30 01:32:05 -0800100 if (sequence_buffer_[index].used) {
101 delete[] packet->dataPtr;
102 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -0800103 return false;
philipel759e0b72016-11-30 01:32:05 -0800104 }
philipelfd5a20f2016-11-15 00:57:57 -0800105 }
106
johan0d1b2b62017-01-10 04:21:35 -0800107 sequence_buffer_[index].frame_begin = packet->is_first_packet_in_frame;
philipel759e0b72016-11-30 01:32:05 -0800108 sequence_buffer_[index].frame_end = packet->markerBit;
109 sequence_buffer_[index].seq_num = packet->seqNum;
philipelfd5a20f2016-11-15 00:57:57 -0800110 sequence_buffer_[index].continuous = false;
111 sequence_buffer_[index].frame_created = false;
112 sequence_buffer_[index].used = true;
philipel759e0b72016-11-30 01:32:05 -0800113 data_buffer_[index] = *packet;
114 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -0800115
philipel2c9f9f22017-06-13 02:47:28 -0700116 UpdateMissingPackets(packet->seqNum);
117
philipel3184f8e2017-05-18 08:08:53 -0700118 int64_t now_ms = clock_->TimeInMilliseconds();
119 last_received_packet_ms_ = rtc::Optional<int64_t>(now_ms);
120 if (packet->frameType == kVideoFrameKey)
121 last_received_keyframe_packet_ms_ = rtc::Optional<int64_t>(now_ms);
122
philipelfd5a20f2016-11-15 00:57:57 -0800123 found_frames = FindFrames(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700124 }
125
philipelfd5a20f2016-11-15 00:57:57 -0800126 for (std::unique_ptr<RtpFrameObject>& frame : found_frames)
127 received_frame_callback_->OnReceivedFrame(std::move(frame));
philipelc707ab72016-04-01 02:01:54 -0700128
philipelc707ab72016-04-01 02:01:54 -0700129 return true;
130}
131
132void PacketBuffer::ClearTo(uint16_t seq_num) {
133 rtc::CritScope lock(&crit_);
philipelc5fb4682017-08-02 04:28:57 -0700134 // We have already cleared past this sequence number, no need to do anything.
135 if (is_cleared_to_first_seq_num_ &&
136 AheadOf<uint16_t>(first_seq_num_, seq_num)) {
137 return;
138 }
philipelaee3e0e2016-11-01 11:45:34 +0100139
140 // If the packet buffer was cleared between a frame was created and returned.
141 if (!first_packet_received_)
142 return;
143
philipelc5fb4682017-08-02 04:28:57 -0700144 // Avoid iterating over the buffer more than once by capping the number of
145 // iterations to the |size_| of the buffer.
146 ++seq_num;
147 size_t diff = ForwardDiff<uint16_t>(first_seq_num_, seq_num);
148 size_t iterations = std::min(diff, size_);
149 for (size_t i = 0; i < iterations; ++i) {
philipelaee3e0e2016-11-01 11:45:34 +0100150 size_t index = first_seq_num_ % size_;
philipelc5fb4682017-08-02 04:28:57 -0700151 RTC_DCHECK_EQ(data_buffer_[index].seqNum, sequence_buffer_[index].seq_num);
152 if (AheadOf<uint16_t>(seq_num, sequence_buffer_[index].seq_num)) {
153 delete[] data_buffer_[index].dataPtr;
154 data_buffer_[index].dataPtr = nullptr;
155 sequence_buffer_[index].used = false;
156 }
philipelaee3e0e2016-11-01 11:45:34 +0100157 ++first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700158 }
philipel2c9f9f22017-06-13 02:47:28 -0700159
philipelc5fb4682017-08-02 04:28:57 -0700160 // If |diff| is larger than |iterations| it means that we don't increment
161 // |first_seq_num_| until we reach |seq_num|, so we set it here.
162 first_seq_num_ = seq_num;
163
164 is_cleared_to_first_seq_num_ = true;
philipel2c9f9f22017-06-13 02:47:28 -0700165 missing_packets_.erase(missing_packets_.begin(),
166 missing_packets_.upper_bound(seq_num));
philipelc707ab72016-04-01 02:01:54 -0700167}
168
philipelaee3e0e2016-11-01 11:45:34 +0100169void PacketBuffer::Clear() {
170 rtc::CritScope lock(&crit_);
171 for (size_t i = 0; i < size_; ++i) {
172 delete[] data_buffer_[i].dataPtr;
173 data_buffer_[i].dataPtr = nullptr;
174 sequence_buffer_[i].used = false;
175 }
176
177 first_packet_received_ = false;
178 is_cleared_to_first_seq_num_ = false;
philipel2c9f9f22017-06-13 02:47:28 -0700179 last_received_packet_ms_.reset();
180 last_received_keyframe_packet_ms_.reset();
181 newest_inserted_seq_num_.reset();
182 missing_packets_.clear();
183}
184
185void PacketBuffer::PaddingReceived(uint16_t seq_num) {
186 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
187 {
188 rtc::CritScope lock(&crit_);
189 UpdateMissingPackets(seq_num);
190 found_frames = FindFrames(static_cast<uint16_t>(seq_num + 1));
191 }
192
193 for (std::unique_ptr<RtpFrameObject>& frame : found_frames)
194 received_frame_callback_->OnReceivedFrame(std::move(frame));
philipel3184f8e2017-05-18 08:08:53 -0700195}
196
197rtc::Optional<int64_t> PacketBuffer::LastReceivedPacketMs() const {
198 rtc::CritScope lock(&crit_);
199 return last_received_packet_ms_;
200}
201
202rtc::Optional<int64_t> PacketBuffer::LastReceivedKeyframePacketMs() const {
203 rtc::CritScope lock(&crit_);
204 return last_received_keyframe_packet_ms_;
philipelaee3e0e2016-11-01 11:45:34 +0100205}
206
philipelc707ab72016-04-01 02:01:54 -0700207bool PacketBuffer::ExpandBufferSize() {
philipelaee3e0e2016-11-01 11:45:34 +0100208 if (size_ == max_size_) {
209 LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
philipelc703dc22017-03-23 06:50:37 -0700210 << "), failed to increase size. Clearing PacketBuffer.";
211 Clear();
philipelc707ab72016-04-01 02:01:54 -0700212 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100213 }
philipelc707ab72016-04-01 02:01:54 -0700214
215 size_t new_size = std::min(max_size_, 2 * size_);
216 std::vector<VCMPacket> new_data_buffer(new_size);
217 std::vector<ContinuityInfo> new_sequence_buffer(new_size);
218 for (size_t i = 0; i < size_; ++i) {
219 if (sequence_buffer_[i].used) {
philipelf4139332016-04-20 10:26:34 +0200220 size_t index = sequence_buffer_[i].seq_num % new_size;
philipelc707ab72016-04-01 02:01:54 -0700221 new_sequence_buffer[index] = sequence_buffer_[i];
222 new_data_buffer[index] = data_buffer_[i];
223 }
224 }
225 size_ = new_size;
226 sequence_buffer_ = std::move(new_sequence_buffer);
227 data_buffer_ = std::move(new_data_buffer);
philipelaee3e0e2016-11-01 11:45:34 +0100228 LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700229 return true;
230}
231
philipelaee3e0e2016-11-01 11:45:34 +0100232bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
philipelf4139332016-04-20 10:26:34 +0200233 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -0700234 int prev_index = index > 0 ? index - 1 : size_ - 1;
philipelf4139332016-04-20 10:26:34 +0200235
philipelc707ab72016-04-01 02:01:54 -0700236 if (!sequence_buffer_[index].used)
237 return false;
philipel2c9f9f22017-06-13 02:47:28 -0700238 if (sequence_buffer_[index].seq_num != seq_num)
239 return false;
philipelf4139332016-04-20 10:26:34 +0200240 if (sequence_buffer_[index].frame_created)
241 return false;
philipel20dce342016-11-28 16:14:57 +0100242 if (sequence_buffer_[index].frame_begin)
philipelc707ab72016-04-01 02:01:54 -0700243 return true;
244 if (!sequence_buffer_[prev_index].used)
245 return false;
philipelea142f82017-01-11 02:01:56 -0800246 if (sequence_buffer_[prev_index].frame_created)
247 return false;
philipelf4139332016-04-20 10:26:34 +0200248 if (sequence_buffer_[prev_index].seq_num !=
philipel2c2f34c2017-01-03 05:55:34 -0800249 static_cast<uint16_t>(sequence_buffer_[index].seq_num - 1)) {
philipelf4139332016-04-20 10:26:34 +0200250 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100251 }
philipelc707ab72016-04-01 02:01:54 -0700252 if (sequence_buffer_[prev_index].continuous)
253 return true;
254
255 return false;
256}
257
philipelfd5a20f2016-11-15 00:57:57 -0800258std::vector<std::unique_ptr<RtpFrameObject>> PacketBuffer::FindFrames(
259 uint16_t seq_num) {
260 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
philipel53910712017-05-18 02:24:40 -0700261 for (size_t i = 0; i < size_ && PotentialNewFrame(seq_num); ++i) {
philipelaee3e0e2016-11-01 11:45:34 +0100262 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -0700263 sequence_buffer_[index].continuous = true;
264
philipelf4139332016-04-20 10:26:34 +0200265 // If all packets of the frame is continuous, find the first packet of the
266 // frame and create an RtpFrameObject.
philipelc707ab72016-04-01 02:01:54 -0700267 if (sequence_buffer_[index].frame_end) {
philipel5ceaaae2016-05-24 10:20:47 +0200268 size_t frame_size = 0;
269 int max_nack_count = -1;
philipelc707ab72016-04-01 02:01:54 -0700270 uint16_t start_seq_num = seq_num;
philipelf4139332016-04-20 10:26:34 +0200271
philipel5ceaaae2016-05-24 10:20:47 +0200272 // Find the start index by searching backward until the packet with
273 // the |frame_begin| flag is set.
274 int start_index = index;
philipel227f8b92017-08-04 06:39:31 -0700275 size_t tested_packets = 0;
philipel8c619242017-02-02 08:51:29 -0800276 int64_t frame_timestamp = data_buffer_[start_index].timestamp;
philipel53910712017-05-18 02:24:40 -0700277
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100278 // Identify H.264 keyframes by means of SPS, PPS, and IDR.
279 bool is_h264 = data_buffer_[start_index].codec == kVideoCodecH264;
280 bool has_h264_sps = false;
281 bool has_h264_pps = false;
282 bool has_h264_idr = false;
283 bool is_h264_keyframe = false;
284
philipel227f8b92017-08-04 06:39:31 -0700285 while (true) {
286 ++tested_packets;
philipel5ceaaae2016-05-24 10:20:47 +0200287 frame_size += data_buffer_[start_index].sizeBytes;
philipelfd5a20f2016-11-15 00:57:57 -0800288 max_nack_count =
289 std::max(max_nack_count, data_buffer_[start_index].timesNacked);
philipelf4139332016-04-20 10:26:34 +0200290 sequence_buffer_[start_index].frame_created = true;
philipel5ceaaae2016-05-24 10:20:47 +0200291
philipel8c619242017-02-02 08:51:29 -0800292 if (!is_h264 && sequence_buffer_[start_index].frame_begin)
philipel5ceaaae2016-05-24 10:20:47 +0200293 break;
294
philipel2c9f9f22017-06-13 02:47:28 -0700295 if (is_h264 && !is_h264_keyframe) {
296 const RTPVideoHeaderH264& header =
297 data_buffer_[start_index].video_header.codecHeader.H264;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100298 for (size_t j = 0; j < header.nalus_length; ++j) {
299 if (header.nalus[j].type == H264::NaluType::kSps) {
300 has_h264_sps = true;
301 } else if (header.nalus[j].type == H264::NaluType::kPps) {
302 has_h264_pps = true;
303 } else if (header.nalus[j].type == H264::NaluType::kIdr) {
304 has_h264_idr = true;
philipel2c9f9f22017-06-13 02:47:28 -0700305 }
306 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100307 if ((sps_pps_idr_is_h264_keyframe_ && has_h264_idr && has_h264_sps &&
308 has_h264_pps) ||
309 (!sps_pps_idr_is_h264_keyframe_ && has_h264_idr)) {
310 is_h264_keyframe = true;
311 }
philipel2c9f9f22017-06-13 02:47:28 -0700312 }
313
philipel227f8b92017-08-04 06:39:31 -0700314 if (tested_packets == size_)
315 break;
316
philipelf4139332016-04-20 10:26:34 +0200317 start_index = start_index > 0 ? start_index - 1 : size_ - 1;
philipel8c619242017-02-02 08:51:29 -0800318
319 // In the case of H264 we don't have a frame_begin bit (yes,
320 // |frame_begin| might be set to true but that is a lie). So instead
321 // we traverese backwards as long as we have a previous packet and
322 // the timestamp of that packet is the same as this one. This may cause
323 // the PacketBuffer to hand out incomplete frames.
324 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
philipel53910712017-05-18 02:24:40 -0700325 if (is_h264 &&
philipel8c619242017-02-02 08:51:29 -0800326 (!sequence_buffer_[start_index].used ||
327 data_buffer_[start_index].timestamp != frame_timestamp)) {
328 break;
329 }
330
331 --start_seq_num;
philipelc707ab72016-04-01 02:01:54 -0700332 }
333
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100334 if (is_h264) {
335 // Warn if this is an unsafe frame.
336 if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) {
337 std::stringstream ss;
338 ss << "Received H.264-IDR frame "
339 << "(SPS: " << has_h264_sps << ", PPS: " << has_h264_pps << "). ";
340 if (sps_pps_idr_is_h264_keyframe_) {
341 ss << "Treating as delta frame since "
342 "WebRTC-SpsPpsIdrIsH264Keyframe is enabled.";
343 } else {
344 ss << "Treating as key frame since "
345 "WebRTC-SpsPpsIdrIsH264Keyframe is disabled.";
346 }
347 LOG(LS_WARNING) << ss.str();
philipel2c9f9f22017-06-13 02:47:28 -0700348 }
349
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100350 // Now that we have decided whether to treat this frame as a key frame
351 // or delta frame in the frame buffer, we update the field that
352 // determines if the RtpFrameObject is a key frame or delta frame.
353 const size_t first_packet_index = start_seq_num % size_;
354 RTC_CHECK_LT(first_packet_index, size_);
355 if (is_h264_keyframe) {
356 data_buffer_[first_packet_index].frameType = kVideoFrameKey;
357 } else {
358 data_buffer_[first_packet_index].frameType = kVideoFrameDelta;
359 }
360
361 // If this is not a keyframe, make sure there are no gaps in the
362 // packet sequence numbers up until this point.
363 if (!is_h264_keyframe && missing_packets_.upper_bound(start_seq_num) !=
364 missing_packets_.begin()) {
365 uint16_t stop_index = (index + 1) % size_;
366 while (start_index != stop_index) {
367 sequence_buffer_[start_index].frame_created = false;
368 start_index = (start_index + 1) % size_;
369 }
370
371 return found_frames;
372 }
philipel2c9f9f22017-06-13 02:47:28 -0700373 }
374
375 missing_packets_.erase(missing_packets_.begin(),
376 missing_packets_.upper_bound(seq_num));
377
philipelfd5a20f2016-11-15 00:57:57 -0800378 found_frames.emplace_back(
philipelb4d31082016-07-11 08:46:29 -0700379 new RtpFrameObject(this, start_seq_num, seq_num, frame_size,
380 max_nack_count, clock_->TimeInMilliseconds()));
philipelc707ab72016-04-01 02:01:54 -0700381 }
philipelc707ab72016-04-01 02:01:54 -0700382 ++seq_num;
383 }
philipelfd5a20f2016-11-15 00:57:57 -0800384 return found_frames;
philipelc707ab72016-04-01 02:01:54 -0700385}
386
387void PacketBuffer::ReturnFrame(RtpFrameObject* frame) {
388 rtc::CritScope lock(&crit_);
philipelf4139332016-04-20 10:26:34 +0200389 size_t index = frame->first_seq_num() % size_;
390 size_t end = (frame->last_seq_num() + 1) % size_;
391 uint16_t seq_num = frame->first_seq_num();
philipelc707ab72016-04-01 02:01:54 -0700392 while (index != end) {
philipel1f39ba12016-09-21 11:27:47 +0200393 if (sequence_buffer_[index].seq_num == seq_num) {
394 delete[] data_buffer_[index].dataPtr;
395 data_buffer_[index].dataPtr = nullptr;
philipelc707ab72016-04-01 02:01:54 -0700396 sequence_buffer_[index].used = false;
philipel1f39ba12016-09-21 11:27:47 +0200397 }
philipelf4139332016-04-20 10:26:34 +0200398
philipelc707ab72016-04-01 02:01:54 -0700399 index = (index + 1) % size_;
400 ++seq_num;
401 }
philipelc707ab72016-04-01 02:01:54 -0700402}
403
404bool PacketBuffer::GetBitstream(const RtpFrameObject& frame,
405 uint8_t* destination) {
406 rtc::CritScope lock(&crit_);
407
philipelf4139332016-04-20 10:26:34 +0200408 size_t index = frame.first_seq_num() % size_;
409 size_t end = (frame.last_seq_num() + 1) % size_;
410 uint16_t seq_num = frame.first_seq_num();
philipel227f8b92017-08-04 06:39:31 -0700411 uint8_t* destination_end = destination + frame.size();
412
413 do {
philipelc707ab72016-04-01 02:01:54 -0700414 if (!sequence_buffer_[index].used ||
415 sequence_buffer_[index].seq_num != seq_num) {
416 return false;
417 }
418
philipel227f8b92017-08-04 06:39:31 -0700419 RTC_DCHECK_EQ(data_buffer_[index].seqNum, sequence_buffer_[index].seq_num);
philipelc18f1d72017-08-02 04:18:02 -0700420 size_t length = data_buffer_[index].sizeBytes;
philipel227f8b92017-08-04 06:39:31 -0700421 if (destination + length > destination_end) {
422 LOG(LS_WARNING) << "Frame (" << frame.picture_id << ":"
423 << static_cast<int>(frame.spatial_layer) << ")"
424 << " bitstream buffer is not large enough.";
425 return false;
426 }
427
428 const uint8_t* source = data_buffer_[index].dataPtr;
philipelc707ab72016-04-01 02:01:54 -0700429 memcpy(destination, source, length);
430 destination += length;
431 index = (index + 1) % size_;
432 ++seq_num;
philipel227f8b92017-08-04 06:39:31 -0700433 } while (index != end);
434
philipelc707ab72016-04-01 02:01:54 -0700435 return true;
436}
437
philipel02447bc2016-05-13 06:01:03 -0700438VCMPacket* PacketBuffer::GetPacket(uint16_t seq_num) {
philipel02447bc2016-05-13 06:01:03 -0700439 size_t index = seq_num % size_;
440 if (!sequence_buffer_[index].used ||
441 seq_num != sequence_buffer_[index].seq_num) {
442 return nullptr;
philipelf4139332016-04-20 10:26:34 +0200443 }
philipel02447bc2016-05-13 06:01:03 -0700444 return &data_buffer_[index];
philipelf4139332016-04-20 10:26:34 +0200445}
446
philipel17deeb42016-08-11 15:09:26 +0200447int PacketBuffer::AddRef() const {
448 return rtc::AtomicOps::Increment(&ref_count_);
449}
450
451int PacketBuffer::Release() const {
452 int count = rtc::AtomicOps::Decrement(&ref_count_);
453 if (!count) {
454 delete this;
455 }
456 return count;
457}
458
philipel2c9f9f22017-06-13 02:47:28 -0700459void PacketBuffer::UpdateMissingPackets(uint16_t seq_num) {
460 if (!newest_inserted_seq_num_)
461 newest_inserted_seq_num_ = rtc::Optional<uint16_t>(seq_num);
462
463 const int kMaxPaddingAge = 1000;
464 if (AheadOf(seq_num, *newest_inserted_seq_num_)) {
465 uint16_t old_seq_num = seq_num - kMaxPaddingAge;
466 auto erase_to = missing_packets_.lower_bound(old_seq_num);
467 missing_packets_.erase(missing_packets_.begin(), erase_to);
468
469 // Guard against inserting a large amount of missing packets if there is a
470 // jump in the sequence number.
471 if (AheadOf(old_seq_num, *newest_inserted_seq_num_))
472 *newest_inserted_seq_num_ = old_seq_num;
473
474 ++*newest_inserted_seq_num_;
475 while (AheadOf(seq_num, *newest_inserted_seq_num_)) {
476 missing_packets_.insert(*newest_inserted_seq_num_);
477 ++*newest_inserted_seq_num_;
478 }
479 } else {
480 missing_packets_.erase(seq_num);
481 }
482}
483
philipelc707ab72016-04-01 02:01:54 -0700484} // namespace video_coding
485} // namespace webrtc