blob: 741c11e2aac3b64fcc3fbebe2176de7b2b4e55e1 [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();
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100119 last_received_packet_ms_ = now_ms;
philipel3184f8e2017-05-18 08:08:53 -0700120 if (packet->frameType == kVideoFrameKey)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100121 last_received_keyframe_packet_ms_ = now_ms;
philipel3184f8e2017-05-18 08:08:53 -0700122
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;
philipelbc5a4082017-12-06 10:41:08 +0100165 auto clear_to_it = missing_packets_.upper_bound(seq_num);
166 if (clear_to_it != missing_packets_.begin()) {
167 --clear_to_it;
168 missing_packets_.erase(missing_packets_.begin(), clear_to_it);
169 }
philipelc707ab72016-04-01 02:01:54 -0700170}
171
philipelaee3e0e2016-11-01 11:45:34 +0100172void PacketBuffer::Clear() {
173 rtc::CritScope lock(&crit_);
174 for (size_t i = 0; i < size_; ++i) {
175 delete[] data_buffer_[i].dataPtr;
176 data_buffer_[i].dataPtr = nullptr;
177 sequence_buffer_[i].used = false;
178 }
179
180 first_packet_received_ = false;
181 is_cleared_to_first_seq_num_ = false;
philipel2c9f9f22017-06-13 02:47:28 -0700182 last_received_packet_ms_.reset();
183 last_received_keyframe_packet_ms_.reset();
184 newest_inserted_seq_num_.reset();
185 missing_packets_.clear();
186}
187
188void PacketBuffer::PaddingReceived(uint16_t seq_num) {
189 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
190 {
191 rtc::CritScope lock(&crit_);
192 UpdateMissingPackets(seq_num);
193 found_frames = FindFrames(static_cast<uint16_t>(seq_num + 1));
194 }
195
196 for (std::unique_ptr<RtpFrameObject>& frame : found_frames)
197 received_frame_callback_->OnReceivedFrame(std::move(frame));
philipel3184f8e2017-05-18 08:08:53 -0700198}
199
200rtc::Optional<int64_t> PacketBuffer::LastReceivedPacketMs() const {
201 rtc::CritScope lock(&crit_);
202 return last_received_packet_ms_;
203}
204
205rtc::Optional<int64_t> PacketBuffer::LastReceivedKeyframePacketMs() const {
206 rtc::CritScope lock(&crit_);
207 return last_received_keyframe_packet_ms_;
philipelaee3e0e2016-11-01 11:45:34 +0100208}
209
philipelc707ab72016-04-01 02:01:54 -0700210bool PacketBuffer::ExpandBufferSize() {
philipelaee3e0e2016-11-01 11:45:34 +0100211 if (size_ == max_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100212 RTC_LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
213 << "), failed to increase size. Clearing PacketBuffer.";
philipelc703dc22017-03-23 06:50:37 -0700214 Clear();
philipelc707ab72016-04-01 02:01:54 -0700215 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100216 }
philipelc707ab72016-04-01 02:01:54 -0700217
218 size_t new_size = std::min(max_size_, 2 * size_);
219 std::vector<VCMPacket> new_data_buffer(new_size);
220 std::vector<ContinuityInfo> new_sequence_buffer(new_size);
221 for (size_t i = 0; i < size_; ++i) {
222 if (sequence_buffer_[i].used) {
philipelf4139332016-04-20 10:26:34 +0200223 size_t index = sequence_buffer_[i].seq_num % new_size;
philipelc707ab72016-04-01 02:01:54 -0700224 new_sequence_buffer[index] = sequence_buffer_[i];
225 new_data_buffer[index] = data_buffer_[i];
226 }
227 }
228 size_ = new_size;
229 sequence_buffer_ = std::move(new_sequence_buffer);
230 data_buffer_ = std::move(new_data_buffer);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100231 RTC_LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700232 return true;
233}
234
philipelaee3e0e2016-11-01 11:45:34 +0100235bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
philipelf4139332016-04-20 10:26:34 +0200236 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -0700237 int prev_index = index > 0 ? index - 1 : size_ - 1;
philipelf4139332016-04-20 10:26:34 +0200238
philipelc707ab72016-04-01 02:01:54 -0700239 if (!sequence_buffer_[index].used)
240 return false;
philipel2c9f9f22017-06-13 02:47:28 -0700241 if (sequence_buffer_[index].seq_num != seq_num)
242 return false;
philipelf4139332016-04-20 10:26:34 +0200243 if (sequence_buffer_[index].frame_created)
244 return false;
philipel20dce342016-11-28 16:14:57 +0100245 if (sequence_buffer_[index].frame_begin)
philipelc707ab72016-04-01 02:01:54 -0700246 return true;
247 if (!sequence_buffer_[prev_index].used)
248 return false;
philipelea142f82017-01-11 02:01:56 -0800249 if (sequence_buffer_[prev_index].frame_created)
250 return false;
philipelf4139332016-04-20 10:26:34 +0200251 if (sequence_buffer_[prev_index].seq_num !=
philipel2c2f34c2017-01-03 05:55:34 -0800252 static_cast<uint16_t>(sequence_buffer_[index].seq_num - 1)) {
philipelf4139332016-04-20 10:26:34 +0200253 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100254 }
philipelc707ab72016-04-01 02:01:54 -0700255 if (sequence_buffer_[prev_index].continuous)
256 return true;
257
258 return false;
259}
260
philipelfd5a20f2016-11-15 00:57:57 -0800261std::vector<std::unique_ptr<RtpFrameObject>> PacketBuffer::FindFrames(
262 uint16_t seq_num) {
263 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
philipel53910712017-05-18 02:24:40 -0700264 for (size_t i = 0; i < size_ && PotentialNewFrame(seq_num); ++i) {
philipelaee3e0e2016-11-01 11:45:34 +0100265 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -0700266 sequence_buffer_[index].continuous = true;
267
philipelf4139332016-04-20 10:26:34 +0200268 // If all packets of the frame is continuous, find the first packet of the
269 // frame and create an RtpFrameObject.
philipelc707ab72016-04-01 02:01:54 -0700270 if (sequence_buffer_[index].frame_end) {
philipel5ceaaae2016-05-24 10:20:47 +0200271 size_t frame_size = 0;
272 int max_nack_count = -1;
philipelc707ab72016-04-01 02:01:54 -0700273 uint16_t start_seq_num = seq_num;
philipelf4139332016-04-20 10:26:34 +0200274
philipel5ceaaae2016-05-24 10:20:47 +0200275 // Find the start index by searching backward until the packet with
276 // the |frame_begin| flag is set.
277 int start_index = index;
philipel227f8b92017-08-04 06:39:31 -0700278 size_t tested_packets = 0;
philipel8c619242017-02-02 08:51:29 -0800279 int64_t frame_timestamp = data_buffer_[start_index].timestamp;
philipel53910712017-05-18 02:24:40 -0700280
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100281 // Identify H.264 keyframes by means of SPS, PPS, and IDR.
282 bool is_h264 = data_buffer_[start_index].codec == kVideoCodecH264;
283 bool has_h264_sps = false;
284 bool has_h264_pps = false;
285 bool has_h264_idr = false;
286 bool is_h264_keyframe = false;
287
philipel227f8b92017-08-04 06:39:31 -0700288 while (true) {
289 ++tested_packets;
philipel5ceaaae2016-05-24 10:20:47 +0200290 frame_size += data_buffer_[start_index].sizeBytes;
philipelfd5a20f2016-11-15 00:57:57 -0800291 max_nack_count =
292 std::max(max_nack_count, data_buffer_[start_index].timesNacked);
philipelf4139332016-04-20 10:26:34 +0200293 sequence_buffer_[start_index].frame_created = true;
philipel5ceaaae2016-05-24 10:20:47 +0200294
philipel8c619242017-02-02 08:51:29 -0800295 if (!is_h264 && sequence_buffer_[start_index].frame_begin)
philipel5ceaaae2016-05-24 10:20:47 +0200296 break;
297
philipel2c9f9f22017-06-13 02:47:28 -0700298 if (is_h264 && !is_h264_keyframe) {
299 const RTPVideoHeaderH264& header =
300 data_buffer_[start_index].video_header.codecHeader.H264;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100301 for (size_t j = 0; j < header.nalus_length; ++j) {
302 if (header.nalus[j].type == H264::NaluType::kSps) {
303 has_h264_sps = true;
304 } else if (header.nalus[j].type == H264::NaluType::kPps) {
305 has_h264_pps = true;
306 } else if (header.nalus[j].type == H264::NaluType::kIdr) {
307 has_h264_idr = true;
philipel2c9f9f22017-06-13 02:47:28 -0700308 }
309 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100310 if ((sps_pps_idr_is_h264_keyframe_ && has_h264_idr && has_h264_sps &&
311 has_h264_pps) ||
312 (!sps_pps_idr_is_h264_keyframe_ && has_h264_idr)) {
313 is_h264_keyframe = true;
314 }
philipel2c9f9f22017-06-13 02:47:28 -0700315 }
316
philipel227f8b92017-08-04 06:39:31 -0700317 if (tested_packets == size_)
318 break;
319
philipelf4139332016-04-20 10:26:34 +0200320 start_index = start_index > 0 ? start_index - 1 : size_ - 1;
philipel8c619242017-02-02 08:51:29 -0800321
322 // In the case of H264 we don't have a frame_begin bit (yes,
323 // |frame_begin| might be set to true but that is a lie). So instead
324 // we traverese backwards as long as we have a previous packet and
325 // the timestamp of that packet is the same as this one. This may cause
326 // the PacketBuffer to hand out incomplete frames.
327 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
philipel53910712017-05-18 02:24:40 -0700328 if (is_h264 &&
philipel8c619242017-02-02 08:51:29 -0800329 (!sequence_buffer_[start_index].used ||
330 data_buffer_[start_index].timestamp != frame_timestamp)) {
331 break;
332 }
333
334 --start_seq_num;
philipelc707ab72016-04-01 02:01:54 -0700335 }
336
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100337 if (is_h264) {
338 // Warn if this is an unsafe frame.
339 if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) {
340 std::stringstream ss;
341 ss << "Received H.264-IDR frame "
342 << "(SPS: " << has_h264_sps << ", PPS: " << has_h264_pps << "). ";
343 if (sps_pps_idr_is_h264_keyframe_) {
344 ss << "Treating as delta frame since "
345 "WebRTC-SpsPpsIdrIsH264Keyframe is enabled.";
346 } else {
347 ss << "Treating as key frame since "
348 "WebRTC-SpsPpsIdrIsH264Keyframe is disabled.";
349 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100350 RTC_LOG(LS_WARNING) << ss.str();
philipel2c9f9f22017-06-13 02:47:28 -0700351 }
352
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100353 // Now that we have decided whether to treat this frame as a key frame
354 // or delta frame in the frame buffer, we update the field that
355 // determines if the RtpFrameObject is a key frame or delta frame.
356 const size_t first_packet_index = start_seq_num % size_;
357 RTC_CHECK_LT(first_packet_index, size_);
358 if (is_h264_keyframe) {
359 data_buffer_[first_packet_index].frameType = kVideoFrameKey;
360 } else {
361 data_buffer_[first_packet_index].frameType = kVideoFrameDelta;
362 }
363
364 // If this is not a keyframe, make sure there are no gaps in the
365 // packet sequence numbers up until this point.
366 if (!is_h264_keyframe && missing_packets_.upper_bound(start_seq_num) !=
367 missing_packets_.begin()) {
368 uint16_t stop_index = (index + 1) % size_;
369 while (start_index != stop_index) {
370 sequence_buffer_[start_index].frame_created = false;
371 start_index = (start_index + 1) % size_;
372 }
373
374 return found_frames;
375 }
philipel2c9f9f22017-06-13 02:47:28 -0700376 }
377
378 missing_packets_.erase(missing_packets_.begin(),
379 missing_packets_.upper_bound(seq_num));
380
philipelfd5a20f2016-11-15 00:57:57 -0800381 found_frames.emplace_back(
philipelb4d31082016-07-11 08:46:29 -0700382 new RtpFrameObject(this, start_seq_num, seq_num, frame_size,
383 max_nack_count, clock_->TimeInMilliseconds()));
philipelc707ab72016-04-01 02:01:54 -0700384 }
philipelc707ab72016-04-01 02:01:54 -0700385 ++seq_num;
386 }
philipelfd5a20f2016-11-15 00:57:57 -0800387 return found_frames;
philipelc707ab72016-04-01 02:01:54 -0700388}
389
390void PacketBuffer::ReturnFrame(RtpFrameObject* frame) {
391 rtc::CritScope lock(&crit_);
philipelf4139332016-04-20 10:26:34 +0200392 size_t index = frame->first_seq_num() % size_;
393 size_t end = (frame->last_seq_num() + 1) % size_;
394 uint16_t seq_num = frame->first_seq_num();
philipelc707ab72016-04-01 02:01:54 -0700395 while (index != end) {
philipel1f39ba12016-09-21 11:27:47 +0200396 if (sequence_buffer_[index].seq_num == seq_num) {
397 delete[] data_buffer_[index].dataPtr;
398 data_buffer_[index].dataPtr = nullptr;
philipelc707ab72016-04-01 02:01:54 -0700399 sequence_buffer_[index].used = false;
philipel1f39ba12016-09-21 11:27:47 +0200400 }
philipelf4139332016-04-20 10:26:34 +0200401
philipelc707ab72016-04-01 02:01:54 -0700402 index = (index + 1) % size_;
403 ++seq_num;
404 }
philipelc707ab72016-04-01 02:01:54 -0700405}
406
407bool PacketBuffer::GetBitstream(const RtpFrameObject& frame,
408 uint8_t* destination) {
409 rtc::CritScope lock(&crit_);
410
philipelf4139332016-04-20 10:26:34 +0200411 size_t index = frame.first_seq_num() % size_;
412 size_t end = (frame.last_seq_num() + 1) % size_;
413 uint16_t seq_num = frame.first_seq_num();
philipel227f8b92017-08-04 06:39:31 -0700414 uint8_t* destination_end = destination + frame.size();
415
416 do {
philipelc707ab72016-04-01 02:01:54 -0700417 if (!sequence_buffer_[index].used ||
418 sequence_buffer_[index].seq_num != seq_num) {
419 return false;
420 }
421
philipel227f8b92017-08-04 06:39:31 -0700422 RTC_DCHECK_EQ(data_buffer_[index].seqNum, sequence_buffer_[index].seq_num);
philipelc18f1d72017-08-02 04:18:02 -0700423 size_t length = data_buffer_[index].sizeBytes;
philipel227f8b92017-08-04 06:39:31 -0700424 if (destination + length > destination_end) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100425 RTC_LOG(LS_WARNING) << "Frame (" << frame.picture_id << ":"
426 << static_cast<int>(frame.spatial_layer) << ")"
427 << " bitstream buffer is not large enough.";
philipel227f8b92017-08-04 06:39:31 -0700428 return false;
429 }
430
431 const uint8_t* source = data_buffer_[index].dataPtr;
philipelc707ab72016-04-01 02:01:54 -0700432 memcpy(destination, source, length);
433 destination += length;
434 index = (index + 1) % size_;
435 ++seq_num;
philipel227f8b92017-08-04 06:39:31 -0700436 } while (index != end);
437
philipelc707ab72016-04-01 02:01:54 -0700438 return true;
439}
440
philipel02447bc2016-05-13 06:01:03 -0700441VCMPacket* PacketBuffer::GetPacket(uint16_t seq_num) {
philipel02447bc2016-05-13 06:01:03 -0700442 size_t index = seq_num % size_;
443 if (!sequence_buffer_[index].used ||
444 seq_num != sequence_buffer_[index].seq_num) {
445 return nullptr;
philipelf4139332016-04-20 10:26:34 +0200446 }
philipel02447bc2016-05-13 06:01:03 -0700447 return &data_buffer_[index];
philipelf4139332016-04-20 10:26:34 +0200448}
449
philipel17deeb42016-08-11 15:09:26 +0200450int PacketBuffer::AddRef() const {
451 return rtc::AtomicOps::Increment(&ref_count_);
452}
453
454int PacketBuffer::Release() const {
455 int count = rtc::AtomicOps::Decrement(&ref_count_);
456 if (!count) {
457 delete this;
458 }
459 return count;
460}
461
philipel2c9f9f22017-06-13 02:47:28 -0700462void PacketBuffer::UpdateMissingPackets(uint16_t seq_num) {
463 if (!newest_inserted_seq_num_)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100464 newest_inserted_seq_num_ = seq_num;
philipel2c9f9f22017-06-13 02:47:28 -0700465
466 const int kMaxPaddingAge = 1000;
467 if (AheadOf(seq_num, *newest_inserted_seq_num_)) {
468 uint16_t old_seq_num = seq_num - kMaxPaddingAge;
469 auto erase_to = missing_packets_.lower_bound(old_seq_num);
470 missing_packets_.erase(missing_packets_.begin(), erase_to);
471
472 // Guard against inserting a large amount of missing packets if there is a
473 // jump in the sequence number.
474 if (AheadOf(old_seq_num, *newest_inserted_seq_num_))
475 *newest_inserted_seq_num_ = old_seq_num;
476
477 ++*newest_inserted_seq_num_;
478 while (AheadOf(seq_num, *newest_inserted_seq_num_)) {
479 missing_packets_.insert(*newest_inserted_seq_num_);
480 ++*newest_inserted_seq_num_;
481 }
482 } else {
483 missing_packets_.erase(seq_num);
484 }
485}
486
philipelc707ab72016-04-01 02:01:54 -0700487} // namespace video_coding
488} // namespace webrtc