blob: 1b6fecc3856883bb24c8bf14f21e29241e86cf27 [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 last_seq_num_(0),
44 first_packet_received_(false),
philipelaee3e0e2016-11-01 11:45:34 +010045 is_cleared_to_first_seq_num_(false),
philipelc707ab72016-04-01 02:01:54 -070046 data_buffer_(start_buffer_size),
47 sequence_buffer_(start_buffer_size),
philipel17deeb42016-08-11 15:09:26 +020048 received_frame_callback_(received_frame_callback) {
philipelc707ab72016-04-01 02:01:54 -070049 RTC_DCHECK_LE(start_buffer_size, max_buffer_size);
50 // Buffer size must always be a power of 2.
51 RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0);
52 RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0);
53}
54
philipelaee3e0e2016-11-01 11:45:34 +010055PacketBuffer::~PacketBuffer() {
56 Clear();
57}
philipel17deeb42016-08-11 15:09:26 +020058
philipelc707ab72016-04-01 02:01:54 -070059bool PacketBuffer::InsertPacket(const VCMPacket& packet) {
philipelfd5a20f2016-11-15 00:57:57 -080060 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
61 {
62 rtc::CritScope lock(&crit_);
63 uint16_t seq_num = packet.seqNum;
64 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -070065
philipelfd5a20f2016-11-15 00:57:57 -080066 if (!first_packet_received_) {
67 first_seq_num_ = seq_num;
68 last_seq_num_ = seq_num;
69 first_packet_received_ = true;
70 } else if (AheadOf(first_seq_num_, seq_num)) {
71 // If we have explicitly cleared past this packet then it's old,
72 // don't insert it.
73 if (is_cleared_to_first_seq_num_)
74 return false;
philipelaee3e0e2016-11-01 11:45:34 +010075
philipelfd5a20f2016-11-15 00:57:57 -080076 first_seq_num_ = seq_num;
philipelc707ab72016-04-01 02:01:54 -070077 }
philipelc707ab72016-04-01 02:01:54 -070078
philipelfd5a20f2016-11-15 00:57:57 -080079 if (sequence_buffer_[index].used) {
80 // Duplicate packet, do nothing.
81 if (data_buffer_[index].seqNum == packet.seqNum)
82 return true;
83
84 // The packet buffer is full, try to expand the buffer.
85 while (ExpandBufferSize() && sequence_buffer_[seq_num % size_].used) {
86 }
87 index = seq_num % size_;
88
89 // Packet buffer is still full.
90 if (sequence_buffer_[index].used)
91 return false;
92 }
93
94 if (AheadOf(seq_num, last_seq_num_))
95 last_seq_num_ = seq_num;
96
97 sequence_buffer_[index].frame_begin = packet.isFirstPacket;
98 sequence_buffer_[index].frame_end = packet.markerBit;
99 sequence_buffer_[index].seq_num = packet.seqNum;
100 sequence_buffer_[index].continuous = false;
101 sequence_buffer_[index].frame_created = false;
102 sequence_buffer_[index].used = true;
103 data_buffer_[index] = packet;
104
105 found_frames = FindFrames(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700106 }
107
philipelfd5a20f2016-11-15 00:57:57 -0800108 for (std::unique_ptr<RtpFrameObject>& frame : found_frames)
109 received_frame_callback_->OnReceivedFrame(std::move(frame));
philipelc707ab72016-04-01 02:01:54 -0700110
philipelc707ab72016-04-01 02:01:54 -0700111 return true;
112}
113
114void PacketBuffer::ClearTo(uint16_t seq_num) {
115 rtc::CritScope lock(&crit_);
philipelaee3e0e2016-11-01 11:45:34 +0100116
117 // If the packet buffer was cleared between a frame was created and returned.
118 if (!first_packet_received_)
119 return;
120
121 is_cleared_to_first_seq_num_ = true;
122 while (AheadOrAt<uint16_t>(seq_num, first_seq_num_)) {
123 size_t index = first_seq_num_ % size_;
philipel1f39ba12016-09-21 11:27:47 +0200124 delete[] data_buffer_[index].dataPtr;
125 data_buffer_[index].dataPtr = nullptr;
philipelc707ab72016-04-01 02:01:54 -0700126 sequence_buffer_[index].used = false;
philipelaee3e0e2016-11-01 11:45:34 +0100127 ++first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700128 }
129}
130
philipelaee3e0e2016-11-01 11:45:34 +0100131void PacketBuffer::Clear() {
132 rtc::CritScope lock(&crit_);
133 for (size_t i = 0; i < size_; ++i) {
134 delete[] data_buffer_[i].dataPtr;
135 data_buffer_[i].dataPtr = nullptr;
136 sequence_buffer_[i].used = false;
137 }
138
139 first_packet_received_ = false;
140 is_cleared_to_first_seq_num_ = false;
141}
142
philipelc707ab72016-04-01 02:01:54 -0700143bool PacketBuffer::ExpandBufferSize() {
philipelaee3e0e2016-11-01 11:45:34 +0100144 if (size_ == max_size_) {
145 LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
146 << "), failed to increase size.";
philipelc707ab72016-04-01 02:01:54 -0700147 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100148 }
philipelc707ab72016-04-01 02:01:54 -0700149
150 size_t new_size = std::min(max_size_, 2 * size_);
151 std::vector<VCMPacket> new_data_buffer(new_size);
152 std::vector<ContinuityInfo> new_sequence_buffer(new_size);
153 for (size_t i = 0; i < size_; ++i) {
154 if (sequence_buffer_[i].used) {
philipelf4139332016-04-20 10:26:34 +0200155 size_t index = sequence_buffer_[i].seq_num % new_size;
philipelc707ab72016-04-01 02:01:54 -0700156 new_sequence_buffer[index] = sequence_buffer_[i];
157 new_data_buffer[index] = data_buffer_[i];
158 }
159 }
160 size_ = new_size;
161 sequence_buffer_ = std::move(new_sequence_buffer);
162 data_buffer_ = std::move(new_data_buffer);
philipelaee3e0e2016-11-01 11:45:34 +0100163 LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700164 return true;
165}
166
philipelaee3e0e2016-11-01 11:45:34 +0100167bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
philipelf4139332016-04-20 10:26:34 +0200168 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -0700169 int prev_index = index > 0 ? index - 1 : size_ - 1;
philipelf4139332016-04-20 10:26:34 +0200170
philipelc707ab72016-04-01 02:01:54 -0700171 if (!sequence_buffer_[index].used)
172 return false;
philipelf4139332016-04-20 10:26:34 +0200173 if (sequence_buffer_[index].frame_created)
174 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100175 if (sequence_buffer_[index].frame_begin &&
176 (!sequence_buffer_[prev_index].used ||
177 AheadOf(seq_num, sequence_buffer_[prev_index].seq_num))) {
178 // The reason we only return true if this packet is the first packet of the
179 // frame and the sequence number is newer than the packet with the previous
180 // index is because we want to avoid an inifite loop in the case where
181 // a single frame containing more packets than the current size of the
182 // packet buffer is inserted.
philipelc707ab72016-04-01 02:01:54 -0700183 return true;
philipelaee3e0e2016-11-01 11:45:34 +0100184 }
philipelc707ab72016-04-01 02:01:54 -0700185 if (!sequence_buffer_[prev_index].used)
186 return false;
philipelf4139332016-04-20 10:26:34 +0200187 if (sequence_buffer_[prev_index].seq_num !=
philipelaee3e0e2016-11-01 11:45:34 +0100188 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;
philipelaee3e0e2016-11-01 11:45:34 +0100200 while (PotentialNewFrame(seq_num)) {
201 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;
214 while (true) {
215 frame_size += data_buffer_[start_index].sizeBytes;
philipelfd5a20f2016-11-15 00:57:57 -0800216 max_nack_count =
217 std::max(max_nack_count, data_buffer_[start_index].timesNacked);
philipelf4139332016-04-20 10:26:34 +0200218 sequence_buffer_[start_index].frame_created = true;
philipel5ceaaae2016-05-24 10:20:47 +0200219
220 if (sequence_buffer_[start_index].frame_begin)
221 break;
222
philipelf4139332016-04-20 10:26:34 +0200223 start_index = start_index > 0 ? start_index - 1 : size_ - 1;
philipelc707ab72016-04-01 02:01:54 -0700224 start_seq_num--;
225 }
226
philipelfd5a20f2016-11-15 00:57:57 -0800227 found_frames.emplace_back(
philipelb4d31082016-07-11 08:46:29 -0700228 new RtpFrameObject(this, start_seq_num, seq_num, frame_size,
229 max_nack_count, clock_->TimeInMilliseconds()));
philipelc707ab72016-04-01 02:01:54 -0700230 }
philipelc707ab72016-04-01 02:01:54 -0700231 ++seq_num;
232 }
philipelfd5a20f2016-11-15 00:57:57 -0800233 return found_frames;
philipelc707ab72016-04-01 02:01:54 -0700234}
235
236void PacketBuffer::ReturnFrame(RtpFrameObject* frame) {
237 rtc::CritScope lock(&crit_);
philipelf4139332016-04-20 10:26:34 +0200238 size_t index = frame->first_seq_num() % size_;
239 size_t end = (frame->last_seq_num() + 1) % size_;
240 uint16_t seq_num = frame->first_seq_num();
philipelc707ab72016-04-01 02:01:54 -0700241 while (index != end) {
philipel1f39ba12016-09-21 11:27:47 +0200242 if (sequence_buffer_[index].seq_num == seq_num) {
243 delete[] data_buffer_[index].dataPtr;
244 data_buffer_[index].dataPtr = nullptr;
philipelc707ab72016-04-01 02:01:54 -0700245 sequence_buffer_[index].used = false;
philipel1f39ba12016-09-21 11:27:47 +0200246 }
philipelf4139332016-04-20 10:26:34 +0200247
philipelc707ab72016-04-01 02:01:54 -0700248 index = (index + 1) % size_;
249 ++seq_num;
250 }
philipelc707ab72016-04-01 02:01:54 -0700251}
252
253bool PacketBuffer::GetBitstream(const RtpFrameObject& frame,
254 uint8_t* destination) {
255 rtc::CritScope lock(&crit_);
256
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) {
261 if (!sequence_buffer_[index].used ||
262 sequence_buffer_[index].seq_num != seq_num) {
263 return false;
264 }
265
266 const uint8_t* source = data_buffer_[index].dataPtr;
267 size_t length = data_buffer_[index].sizeBytes;
268 memcpy(destination, source, length);
269 destination += length;
270 index = (index + 1) % size_;
271 ++seq_num;
272 }
273 return true;
274}
275
philipel02447bc2016-05-13 06:01:03 -0700276VCMPacket* PacketBuffer::GetPacket(uint16_t seq_num) {
philipel02447bc2016-05-13 06:01:03 -0700277 size_t index = seq_num % size_;
278 if (!sequence_buffer_[index].used ||
279 seq_num != sequence_buffer_[index].seq_num) {
280 return nullptr;
philipelf4139332016-04-20 10:26:34 +0200281 }
philipel02447bc2016-05-13 06:01:03 -0700282 return &data_buffer_[index];
philipelf4139332016-04-20 10:26:34 +0200283}
284
philipel17deeb42016-08-11 15:09:26 +0200285int PacketBuffer::AddRef() const {
286 return rtc::AtomicOps::Increment(&ref_count_);
287}
288
289int PacketBuffer::Release() const {
290 int count = rtc::AtomicOps::Decrement(&ref_count_);
291 if (!count) {
292 delete this;
293 }
294 return count;
295}
296
philipelc707ab72016-04-01 02:01:54 -0700297} // namespace video_coding
298} // namespace webrtc