blob: f30a3ea722e5ccf0721bf0743635abcd281226cc [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
11#include "webrtc/modules/video_coding/packet_buffer.h"
12
13#include <algorithm>
14#include <limits>
philipel17deeb42016-08-11 15:09:26 +020015#include <utility>
philipelc707ab72016-04-01 02:01:54 -070016
philipel17deeb42016-08-11 15:09:26 +020017#include "webrtc/base/atomicops.h"
philipelc707ab72016-04-01 02:01:54 -070018#include "webrtc/base/checks.h"
philipela1059872016-05-09 11:41:48 +020019#include "webrtc/base/logging.h"
philipelc707ab72016-04-01 02:01:54 -070020#include "webrtc/modules/video_coding/frame_object.h"
philipelb4d31082016-07-11 08:46:29 -070021#include "webrtc/system_wrappers/include/clock.h"
philipelc707ab72016-04-01 02:01:54 -070022
23namespace webrtc {
24namespace video_coding {
25
philipel17deeb42016-08-11 15:09:26 +020026rtc::scoped_refptr<PacketBuffer> PacketBuffer::Create(
27 Clock* clock,
28 size_t start_buffer_size,
29 size_t max_buffer_size,
30 OnReceivedFrameCallback* received_frame_callback) {
31 return rtc::scoped_refptr<PacketBuffer>(new PacketBuffer(
32 clock, start_buffer_size, max_buffer_size, received_frame_callback));
33}
34
philipelb4d31082016-07-11 08:46:29 -070035PacketBuffer::PacketBuffer(Clock* clock,
36 size_t start_buffer_size,
philipelc707ab72016-04-01 02:01:54 -070037 size_t max_buffer_size,
philipel17deeb42016-08-11 15:09:26 +020038 OnReceivedFrameCallback* received_frame_callback)
philipelb4d31082016-07-11 08:46:29 -070039 : clock_(clock),
40 size_(start_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070041 max_size_(max_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070042 first_seq_num_(0),
philipelf4139332016-04-20 10:26:34 +020043 first_packet_received_(false),
philipelaee3e0e2016-11-01 11:45:34 +010044 is_cleared_to_first_seq_num_(false),
philipelc707ab72016-04-01 02:01:54 -070045 data_buffer_(start_buffer_size),
46 sequence_buffer_(start_buffer_size),
philipel17deeb42016-08-11 15:09:26 +020047 received_frame_callback_(received_frame_callback) {
philipelc707ab72016-04-01 02:01:54 -070048 RTC_DCHECK_LE(start_buffer_size, max_buffer_size);
49 // Buffer size must always be a power of 2.
50 RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0);
51 RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0);
52}
53
philipelaee3e0e2016-11-01 11:45:34 +010054PacketBuffer::~PacketBuffer() {
55 Clear();
56}
philipel17deeb42016-08-11 15:09:26 +020057
philipel759e0b72016-11-30 01:32:05 -080058bool PacketBuffer::InsertPacket(VCMPacket* packet) {
philipelfd5a20f2016-11-15 00:57:57 -080059 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
60 {
61 rtc::CritScope lock(&crit_);
philipel759e0b72016-11-30 01:32:05 -080062 uint16_t seq_num = packet->seqNum;
philipelfd5a20f2016-11-15 00:57:57 -080063 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -070064
philipelfd5a20f2016-11-15 00:57:57 -080065 if (!first_packet_received_) {
66 first_seq_num_ = seq_num;
philipelfd5a20f2016-11-15 00:57:57 -080067 first_packet_received_ = true;
68 } else if (AheadOf(first_seq_num_, seq_num)) {
69 // If we have explicitly cleared past this packet then it's old,
70 // don't insert it.
philipel759e0b72016-11-30 01:32:05 -080071 if (is_cleared_to_first_seq_num_) {
72 delete[] packet->dataPtr;
73 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -080074 return false;
philipel759e0b72016-11-30 01:32:05 -080075 }
philipelaee3e0e2016-11-01 11:45:34 +010076
philipelfd5a20f2016-11-15 00:57:57 -080077 first_seq_num_ = seq_num;
philipelc707ab72016-04-01 02:01:54 -070078 }
philipelc707ab72016-04-01 02:01:54 -070079
philipelfd5a20f2016-11-15 00:57:57 -080080 if (sequence_buffer_[index].used) {
philipel759e0b72016-11-30 01:32:05 -080081 // Duplicate packet, just delete the payload.
82 if (data_buffer_[index].seqNum == packet->seqNum) {
83 delete[] packet->dataPtr;
84 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -080085 return true;
philipel759e0b72016-11-30 01:32:05 -080086 }
philipelfd5a20f2016-11-15 00:57:57 -080087
88 // The packet buffer is full, try to expand the buffer.
89 while (ExpandBufferSize() && sequence_buffer_[seq_num % size_].used) {
90 }
91 index = seq_num % size_;
92
93 // Packet buffer is still full.
philipel759e0b72016-11-30 01:32:05 -080094 if (sequence_buffer_[index].used) {
95 delete[] packet->dataPtr;
96 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -080097 return false;
philipel759e0b72016-11-30 01:32:05 -080098 }
philipelfd5a20f2016-11-15 00:57:57 -080099 }
100
johan0d1b2b62017-01-10 04:21:35 -0800101 sequence_buffer_[index].frame_begin = packet->is_first_packet_in_frame;
philipel759e0b72016-11-30 01:32:05 -0800102 sequence_buffer_[index].frame_end = packet->markerBit;
103 sequence_buffer_[index].seq_num = packet->seqNum;
philipelfd5a20f2016-11-15 00:57:57 -0800104 sequence_buffer_[index].continuous = false;
105 sequence_buffer_[index].frame_created = false;
106 sequence_buffer_[index].used = true;
philipel759e0b72016-11-30 01:32:05 -0800107 data_buffer_[index] = *packet;
108 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -0800109
110 found_frames = FindFrames(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700111 }
112
philipelfd5a20f2016-11-15 00:57:57 -0800113 for (std::unique_ptr<RtpFrameObject>& frame : found_frames)
114 received_frame_callback_->OnReceivedFrame(std::move(frame));
philipelc707ab72016-04-01 02:01:54 -0700115
philipelc707ab72016-04-01 02:01:54 -0700116 return true;
117}
118
119void PacketBuffer::ClearTo(uint16_t seq_num) {
120 rtc::CritScope lock(&crit_);
philipelaee3e0e2016-11-01 11:45:34 +0100121
122 // If the packet buffer was cleared between a frame was created and returned.
123 if (!first_packet_received_)
124 return;
125
126 is_cleared_to_first_seq_num_ = true;
127 while (AheadOrAt<uint16_t>(seq_num, first_seq_num_)) {
128 size_t index = first_seq_num_ % size_;
philipel1f39ba12016-09-21 11:27:47 +0200129 delete[] data_buffer_[index].dataPtr;
130 data_buffer_[index].dataPtr = nullptr;
philipelc707ab72016-04-01 02:01:54 -0700131 sequence_buffer_[index].used = false;
philipelaee3e0e2016-11-01 11:45:34 +0100132 ++first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700133 }
134}
135
philipelaee3e0e2016-11-01 11:45:34 +0100136void PacketBuffer::Clear() {
137 rtc::CritScope lock(&crit_);
138 for (size_t i = 0; i < size_; ++i) {
139 delete[] data_buffer_[i].dataPtr;
140 data_buffer_[i].dataPtr = nullptr;
141 sequence_buffer_[i].used = false;
142 }
143
144 first_packet_received_ = false;
145 is_cleared_to_first_seq_num_ = false;
146}
147
philipelc707ab72016-04-01 02:01:54 -0700148bool PacketBuffer::ExpandBufferSize() {
philipelaee3e0e2016-11-01 11:45:34 +0100149 if (size_ == max_size_) {
150 LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
151 << "), failed to increase size.";
philipelc707ab72016-04-01 02:01:54 -0700152 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100153 }
philipelc707ab72016-04-01 02:01:54 -0700154
155 size_t new_size = std::min(max_size_, 2 * size_);
156 std::vector<VCMPacket> new_data_buffer(new_size);
157 std::vector<ContinuityInfo> new_sequence_buffer(new_size);
158 for (size_t i = 0; i < size_; ++i) {
159 if (sequence_buffer_[i].used) {
philipelf4139332016-04-20 10:26:34 +0200160 size_t index = sequence_buffer_[i].seq_num % new_size;
philipelc707ab72016-04-01 02:01:54 -0700161 new_sequence_buffer[index] = sequence_buffer_[i];
162 new_data_buffer[index] = data_buffer_[i];
163 }
164 }
165 size_ = new_size;
166 sequence_buffer_ = std::move(new_sequence_buffer);
167 data_buffer_ = std::move(new_data_buffer);
philipelaee3e0e2016-11-01 11:45:34 +0100168 LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700169 return true;
170}
171
philipelaee3e0e2016-11-01 11:45:34 +0100172bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
philipelf4139332016-04-20 10:26:34 +0200173 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -0700174 int prev_index = index > 0 ? index - 1 : size_ - 1;
philipelf4139332016-04-20 10:26:34 +0200175
philipelc707ab72016-04-01 02:01:54 -0700176 if (!sequence_buffer_[index].used)
177 return false;
philipelf4139332016-04-20 10:26:34 +0200178 if (sequence_buffer_[index].frame_created)
179 return false;
philipel20dce342016-11-28 16:14:57 +0100180 if (sequence_buffer_[index].frame_begin)
philipelc707ab72016-04-01 02:01:54 -0700181 return true;
182 if (!sequence_buffer_[prev_index].used)
183 return false;
philipelea142f82017-01-11 02:01:56 -0800184 if (sequence_buffer_[prev_index].frame_created)
185 return false;
philipelf4139332016-04-20 10:26:34 +0200186 if (sequence_buffer_[prev_index].seq_num !=
philipel2c2f34c2017-01-03 05:55:34 -0800187 static_cast<uint16_t>(sequence_buffer_[index].seq_num - 1)) {
philipelf4139332016-04-20 10:26:34 +0200188 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100189 }
philipelc707ab72016-04-01 02:01:54 -0700190 if (sequence_buffer_[prev_index].continuous)
191 return true;
192
193 return false;
194}
195
philipelfd5a20f2016-11-15 00:57:57 -0800196std::vector<std::unique_ptr<RtpFrameObject>> PacketBuffer::FindFrames(
197 uint16_t seq_num) {
198 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
philipel20dce342016-11-28 16:14:57 +0100199 size_t packets_tested = 0;
200 while (packets_tested < size_ && PotentialNewFrame(seq_num)) {
philipelaee3e0e2016-11-01 11:45:34 +0100201 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -0700202 sequence_buffer_[index].continuous = true;
203
philipelf4139332016-04-20 10:26:34 +0200204 // If all packets of the frame is continuous, find the first packet of the
205 // frame and create an RtpFrameObject.
philipelc707ab72016-04-01 02:01:54 -0700206 if (sequence_buffer_[index].frame_end) {
philipel5ceaaae2016-05-24 10:20:47 +0200207 size_t frame_size = 0;
208 int max_nack_count = -1;
philipelc707ab72016-04-01 02:01:54 -0700209 uint16_t start_seq_num = seq_num;
philipelf4139332016-04-20 10:26:34 +0200210
philipel5ceaaae2016-05-24 10:20:47 +0200211 // Find the start index by searching backward until the packet with
212 // the |frame_begin| flag is set.
213 int start_index = index;
philipel8c619242017-02-02 08:51:29 -0800214
215 bool is_h264 = data_buffer_[start_index].codec == kVideoCodecH264;
216 int64_t frame_timestamp = data_buffer_[start_index].timestamp;
philipel5ceaaae2016-05-24 10:20:47 +0200217 while (true) {
218 frame_size += data_buffer_[start_index].sizeBytes;
philipelfd5a20f2016-11-15 00:57:57 -0800219 max_nack_count =
220 std::max(max_nack_count, data_buffer_[start_index].timesNacked);
philipelf4139332016-04-20 10:26:34 +0200221 sequence_buffer_[start_index].frame_created = true;
philipel5ceaaae2016-05-24 10:20:47 +0200222
philipel8c619242017-02-02 08:51:29 -0800223 if (!is_h264 && sequence_buffer_[start_index].frame_begin)
philipel5ceaaae2016-05-24 10:20:47 +0200224 break;
225
philipelf4139332016-04-20 10:26:34 +0200226 start_index = start_index > 0 ? start_index - 1 : size_ - 1;
philipel8c619242017-02-02 08:51:29 -0800227
228 // In the case of H264 we don't have a frame_begin bit (yes,
229 // |frame_begin| might be set to true but that is a lie). So instead
230 // we traverese backwards as long as we have a previous packet and
231 // the timestamp of that packet is the same as this one. This may cause
232 // the PacketBuffer to hand out incomplete frames.
233 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
234 //
235 // Since we ignore the |frame_begin| flag of the inserted packets
236 // we check that |start_index != static_cast<int>(index)| to make sure
237 // that we don't get stuck in a loop if the packet buffer is filled
238 // with packets of the same timestamp.
239 if (is_h264 && start_index != static_cast<int>(index) &&
240 (!sequence_buffer_[start_index].used ||
241 data_buffer_[start_index].timestamp != frame_timestamp)) {
242 break;
243 }
244
245 --start_seq_num;
philipelc707ab72016-04-01 02:01:54 -0700246 }
247
philipelfd5a20f2016-11-15 00:57:57 -0800248 found_frames.emplace_back(
philipelb4d31082016-07-11 08:46:29 -0700249 new RtpFrameObject(this, start_seq_num, seq_num, frame_size,
250 max_nack_count, clock_->TimeInMilliseconds()));
philipelc707ab72016-04-01 02:01:54 -0700251 }
philipelc707ab72016-04-01 02:01:54 -0700252 ++seq_num;
philipel20dce342016-11-28 16:14:57 +0100253 ++packets_tested;
philipelc707ab72016-04-01 02:01:54 -0700254 }
philipelfd5a20f2016-11-15 00:57:57 -0800255 return found_frames;
philipelc707ab72016-04-01 02:01:54 -0700256}
257
258void PacketBuffer::ReturnFrame(RtpFrameObject* frame) {
259 rtc::CritScope lock(&crit_);
philipelf4139332016-04-20 10:26:34 +0200260 size_t index = frame->first_seq_num() % size_;
261 size_t end = (frame->last_seq_num() + 1) % size_;
262 uint16_t seq_num = frame->first_seq_num();
philipelc707ab72016-04-01 02:01:54 -0700263 while (index != end) {
philipel1f39ba12016-09-21 11:27:47 +0200264 if (sequence_buffer_[index].seq_num == seq_num) {
265 delete[] data_buffer_[index].dataPtr;
266 data_buffer_[index].dataPtr = nullptr;
philipelc707ab72016-04-01 02:01:54 -0700267 sequence_buffer_[index].used = false;
philipel1f39ba12016-09-21 11:27:47 +0200268 }
philipelf4139332016-04-20 10:26:34 +0200269
philipelc707ab72016-04-01 02:01:54 -0700270 index = (index + 1) % size_;
271 ++seq_num;
272 }
philipelc707ab72016-04-01 02:01:54 -0700273}
274
275bool PacketBuffer::GetBitstream(const RtpFrameObject& frame,
276 uint8_t* destination) {
277 rtc::CritScope lock(&crit_);
278
philipelf4139332016-04-20 10:26:34 +0200279 size_t index = frame.first_seq_num() % size_;
280 size_t end = (frame.last_seq_num() + 1) % size_;
281 uint16_t seq_num = frame.first_seq_num();
philipelc707ab72016-04-01 02:01:54 -0700282 while (index != end) {
283 if (!sequence_buffer_[index].used ||
284 sequence_buffer_[index].seq_num != seq_num) {
285 return false;
286 }
287
288 const uint8_t* source = data_buffer_[index].dataPtr;
289 size_t length = data_buffer_[index].sizeBytes;
290 memcpy(destination, source, length);
291 destination += length;
292 index = (index + 1) % size_;
293 ++seq_num;
294 }
295 return true;
296}
297
philipel02447bc2016-05-13 06:01:03 -0700298VCMPacket* PacketBuffer::GetPacket(uint16_t seq_num) {
philipel02447bc2016-05-13 06:01:03 -0700299 size_t index = seq_num % size_;
300 if (!sequence_buffer_[index].used ||
301 seq_num != sequence_buffer_[index].seq_num) {
302 return nullptr;
philipelf4139332016-04-20 10:26:34 +0200303 }
philipel02447bc2016-05-13 06:01:03 -0700304 return &data_buffer_[index];
philipelf4139332016-04-20 10:26:34 +0200305}
306
philipel17deeb42016-08-11 15:09:26 +0200307int PacketBuffer::AddRef() const {
308 return rtc::AtomicOps::Increment(&ref_count_);
309}
310
311int PacketBuffer::Release() const {
312 int count = rtc::AtomicOps::Decrement(&ref_count_);
313 if (!count) {
314 delete this;
315 }
316 return count;
317}
318
philipelc707ab72016-04-01 02:01:54 -0700319} // namespace video_coding
320} // namespace webrtc