blob: ff013e00102894a9b5621dd083b57e656e52f5ab [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_
philipelc703dc22017-03-23 06:50:37 -0700151 << "), failed to increase size. Clearing PacketBuffer.";
152 Clear();
philipelc707ab72016-04-01 02:01:54 -0700153 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100154 }
philipelc707ab72016-04-01 02:01:54 -0700155
156 size_t new_size = std::min(max_size_, 2 * size_);
157 std::vector<VCMPacket> new_data_buffer(new_size);
158 std::vector<ContinuityInfo> new_sequence_buffer(new_size);
159 for (size_t i = 0; i < size_; ++i) {
160 if (sequence_buffer_[i].used) {
philipelf4139332016-04-20 10:26:34 +0200161 size_t index = sequence_buffer_[i].seq_num % new_size;
philipelc707ab72016-04-01 02:01:54 -0700162 new_sequence_buffer[index] = sequence_buffer_[i];
163 new_data_buffer[index] = data_buffer_[i];
164 }
165 }
166 size_ = new_size;
167 sequence_buffer_ = std::move(new_sequence_buffer);
168 data_buffer_ = std::move(new_data_buffer);
philipelaee3e0e2016-11-01 11:45:34 +0100169 LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700170 return true;
171}
172
philipelaee3e0e2016-11-01 11:45:34 +0100173bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
philipelf4139332016-04-20 10:26:34 +0200174 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -0700175 int prev_index = index > 0 ? index - 1 : size_ - 1;
philipelf4139332016-04-20 10:26:34 +0200176
philipelc707ab72016-04-01 02:01:54 -0700177 if (!sequence_buffer_[index].used)
178 return false;
philipelf4139332016-04-20 10:26:34 +0200179 if (sequence_buffer_[index].frame_created)
180 return false;
philipel20dce342016-11-28 16:14:57 +0100181 if (sequence_buffer_[index].frame_begin)
philipelc707ab72016-04-01 02:01:54 -0700182 return true;
183 if (!sequence_buffer_[prev_index].used)
184 return false;
philipelea142f82017-01-11 02:01:56 -0800185 if (sequence_buffer_[prev_index].frame_created)
186 return false;
philipelf4139332016-04-20 10:26:34 +0200187 if (sequence_buffer_[prev_index].seq_num !=
philipel2c2f34c2017-01-03 05:55:34 -0800188 static_cast<uint16_t>(sequence_buffer_[index].seq_num - 1)) {
philipelf4139332016-04-20 10:26:34 +0200189 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100190 }
philipelc707ab72016-04-01 02:01:54 -0700191 if (sequence_buffer_[prev_index].continuous)
192 return true;
193
194 return false;
195}
196
philipelfd5a20f2016-11-15 00:57:57 -0800197std::vector<std::unique_ptr<RtpFrameObject>> PacketBuffer::FindFrames(
198 uint16_t seq_num) {
199 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
philipel53910712017-05-18 02:24:40 -0700200 for (size_t i = 0; i < size_ && PotentialNewFrame(seq_num); ++i) {
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;
philipel53910712017-05-18 02:24:40 -0700217
218 // Since packet at |data_buffer_[index]| is already part of the frame
219 // we will have at most |size_ - 1| packets left to check.
220 for (size_t j = 0; j < size_ - 1; ++j) {
philipel5ceaaae2016-05-24 10:20:47 +0200221 frame_size += data_buffer_[start_index].sizeBytes;
philipelfd5a20f2016-11-15 00:57:57 -0800222 max_nack_count =
223 std::max(max_nack_count, data_buffer_[start_index].timesNacked);
philipelf4139332016-04-20 10:26:34 +0200224 sequence_buffer_[start_index].frame_created = true;
philipel5ceaaae2016-05-24 10:20:47 +0200225
philipel8c619242017-02-02 08:51:29 -0800226 if (!is_h264 && sequence_buffer_[start_index].frame_begin)
philipel5ceaaae2016-05-24 10:20:47 +0200227 break;
228
philipelf4139332016-04-20 10:26:34 +0200229 start_index = start_index > 0 ? start_index - 1 : size_ - 1;
philipel8c619242017-02-02 08:51:29 -0800230
231 // In the case of H264 we don't have a frame_begin bit (yes,
232 // |frame_begin| might be set to true but that is a lie). So instead
233 // we traverese backwards as long as we have a previous packet and
234 // the timestamp of that packet is the same as this one. This may cause
235 // the PacketBuffer to hand out incomplete frames.
236 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
philipel53910712017-05-18 02:24:40 -0700237 if (is_h264 &&
philipel8c619242017-02-02 08:51:29 -0800238 (!sequence_buffer_[start_index].used ||
239 data_buffer_[start_index].timestamp != frame_timestamp)) {
240 break;
241 }
242
243 --start_seq_num;
philipelc707ab72016-04-01 02:01:54 -0700244 }
245
philipelfd5a20f2016-11-15 00:57:57 -0800246 found_frames.emplace_back(
philipelb4d31082016-07-11 08:46:29 -0700247 new RtpFrameObject(this, start_seq_num, seq_num, frame_size,
248 max_nack_count, clock_->TimeInMilliseconds()));
philipelc707ab72016-04-01 02:01:54 -0700249 }
philipelc707ab72016-04-01 02:01:54 -0700250 ++seq_num;
251 }
philipelfd5a20f2016-11-15 00:57:57 -0800252 return found_frames;
philipelc707ab72016-04-01 02:01:54 -0700253}
254
255void PacketBuffer::ReturnFrame(RtpFrameObject* frame) {
256 rtc::CritScope lock(&crit_);
philipelf4139332016-04-20 10:26:34 +0200257 size_t index = frame->first_seq_num() % size_;
258 size_t end = (frame->last_seq_num() + 1) % size_;
259 uint16_t seq_num = frame->first_seq_num();
philipelc707ab72016-04-01 02:01:54 -0700260 while (index != end) {
philipel1f39ba12016-09-21 11:27:47 +0200261 if (sequence_buffer_[index].seq_num == seq_num) {
262 delete[] data_buffer_[index].dataPtr;
263 data_buffer_[index].dataPtr = nullptr;
philipelc707ab72016-04-01 02:01:54 -0700264 sequence_buffer_[index].used = false;
philipel1f39ba12016-09-21 11:27:47 +0200265 }
philipelf4139332016-04-20 10:26:34 +0200266
philipelc707ab72016-04-01 02:01:54 -0700267 index = (index + 1) % size_;
268 ++seq_num;
269 }
philipelc707ab72016-04-01 02:01:54 -0700270}
271
272bool PacketBuffer::GetBitstream(const RtpFrameObject& frame,
273 uint8_t* destination) {
274 rtc::CritScope lock(&crit_);
275
philipelf4139332016-04-20 10:26:34 +0200276 size_t index = frame.first_seq_num() % size_;
277 size_t end = (frame.last_seq_num() + 1) % size_;
278 uint16_t seq_num = frame.first_seq_num();
philipelc707ab72016-04-01 02:01:54 -0700279 while (index != end) {
280 if (!sequence_buffer_[index].used ||
281 sequence_buffer_[index].seq_num != seq_num) {
282 return false;
283 }
284
285 const uint8_t* source = data_buffer_[index].dataPtr;
286 size_t length = data_buffer_[index].sizeBytes;
287 memcpy(destination, source, length);
288 destination += length;
289 index = (index + 1) % size_;
290 ++seq_num;
291 }
292 return true;
293}
294
philipel02447bc2016-05-13 06:01:03 -0700295VCMPacket* PacketBuffer::GetPacket(uint16_t seq_num) {
philipel02447bc2016-05-13 06:01:03 -0700296 size_t index = seq_num % size_;
297 if (!sequence_buffer_[index].used ||
298 seq_num != sequence_buffer_[index].seq_num) {
299 return nullptr;
philipelf4139332016-04-20 10:26:34 +0200300 }
philipel02447bc2016-05-13 06:01:03 -0700301 return &data_buffer_[index];
philipelf4139332016-04-20 10:26:34 +0200302}
303
philipel17deeb42016-08-11 15:09:26 +0200304int PacketBuffer::AddRef() const {
305 return rtc::AtomicOps::Increment(&ref_count_);
306}
307
308int PacketBuffer::Release() const {
309 int count = rtc::AtomicOps::Decrement(&ref_count_);
310 if (!count) {
311 delete this;
312 }
313 return count;
314}
315
philipelc707ab72016-04-01 02:01:54 -0700316} // namespace video_coding
317} // namespace webrtc