blob: 58ad31e50cefda7c959cdeffb08d22b61e795c94 [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) {
60 rtc::CritScope lock(&crit_);
61 uint16_t seq_num = packet.seqNum;
philipelf4139332016-04-20 10:26:34 +020062 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -070063
philipelf4139332016-04-20 10:26:34 +020064 if (!first_packet_received_) {
philipelaee3e0e2016-11-01 11:45:34 +010065 first_seq_num_ = seq_num;
philipelc707ab72016-04-01 02:01:54 -070066 last_seq_num_ = seq_num;
philipelf4139332016-04-20 10:26:34 +020067 first_packet_received_ = true;
philipelaee3e0e2016-11-01 11:45:34 +010068 } 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.
71 if (is_cleared_to_first_seq_num_)
72 return false;
73
74 first_seq_num_ = seq_num;
philipelc707ab72016-04-01 02:01:54 -070075 }
76
77 if (sequence_buffer_[index].used) {
78 // Duplicate packet, do nothing.
79 if (data_buffer_[index].seqNum == packet.seqNum)
80 return true;
81
82 // The packet buffer is full, try to expand the buffer.
83 while (ExpandBufferSize() && sequence_buffer_[seq_num % size_].used) {
84 }
85 index = seq_num % size_;
86
87 // Packet buffer is still full.
88 if (sequence_buffer_[index].used)
89 return false;
90 }
91
92 if (AheadOf(seq_num, last_seq_num_))
93 last_seq_num_ = seq_num;
94
95 sequence_buffer_[index].frame_begin = packet.isFirstPacket;
96 sequence_buffer_[index].frame_end = packet.markerBit;
97 sequence_buffer_[index].seq_num = packet.seqNum;
98 sequence_buffer_[index].continuous = false;
philipelf4139332016-04-20 10:26:34 +020099 sequence_buffer_[index].frame_created = false;
philipelc707ab72016-04-01 02:01:54 -0700100 sequence_buffer_[index].used = true;
101 data_buffer_[index] = packet;
102
philipelf4139332016-04-20 10:26:34 +0200103 FindFrames(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700104 return true;
105}
106
107void PacketBuffer::ClearTo(uint16_t seq_num) {
108 rtc::CritScope lock(&crit_);
philipelaee3e0e2016-11-01 11:45:34 +0100109
110 // If the packet buffer was cleared between a frame was created and returned.
111 if (!first_packet_received_)
112 return;
113
114 is_cleared_to_first_seq_num_ = true;
115 while (AheadOrAt<uint16_t>(seq_num, first_seq_num_)) {
116 size_t index = first_seq_num_ % size_;
philipel1f39ba12016-09-21 11:27:47 +0200117 delete[] data_buffer_[index].dataPtr;
118 data_buffer_[index].dataPtr = nullptr;
philipelc707ab72016-04-01 02:01:54 -0700119 sequence_buffer_[index].used = false;
philipelaee3e0e2016-11-01 11:45:34 +0100120 ++first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700121 }
122}
123
philipelaee3e0e2016-11-01 11:45:34 +0100124void PacketBuffer::Clear() {
125 rtc::CritScope lock(&crit_);
126 for (size_t i = 0; i < size_; ++i) {
127 delete[] data_buffer_[i].dataPtr;
128 data_buffer_[i].dataPtr = nullptr;
129 sequence_buffer_[i].used = false;
130 }
131
132 first_packet_received_ = false;
133 is_cleared_to_first_seq_num_ = false;
134}
135
philipelc707ab72016-04-01 02:01:54 -0700136bool PacketBuffer::ExpandBufferSize() {
philipelaee3e0e2016-11-01 11:45:34 +0100137 if (size_ == max_size_) {
138 LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
139 << "), failed to increase size.";
philipelc707ab72016-04-01 02:01:54 -0700140 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100141 }
philipelc707ab72016-04-01 02:01:54 -0700142
143 size_t new_size = std::min(max_size_, 2 * size_);
144 std::vector<VCMPacket> new_data_buffer(new_size);
145 std::vector<ContinuityInfo> new_sequence_buffer(new_size);
146 for (size_t i = 0; i < size_; ++i) {
147 if (sequence_buffer_[i].used) {
philipelf4139332016-04-20 10:26:34 +0200148 size_t index = sequence_buffer_[i].seq_num % new_size;
philipelc707ab72016-04-01 02:01:54 -0700149 new_sequence_buffer[index] = sequence_buffer_[i];
150 new_data_buffer[index] = data_buffer_[i];
151 }
152 }
153 size_ = new_size;
154 sequence_buffer_ = std::move(new_sequence_buffer);
155 data_buffer_ = std::move(new_data_buffer);
philipelaee3e0e2016-11-01 11:45:34 +0100156 LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700157 return true;
158}
159
philipelaee3e0e2016-11-01 11:45:34 +0100160bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
philipelf4139332016-04-20 10:26:34 +0200161 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -0700162 int prev_index = index > 0 ? index - 1 : size_ - 1;
philipelf4139332016-04-20 10:26:34 +0200163
philipelc707ab72016-04-01 02:01:54 -0700164 if (!sequence_buffer_[index].used)
165 return false;
philipelf4139332016-04-20 10:26:34 +0200166 if (sequence_buffer_[index].frame_created)
167 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100168 if (sequence_buffer_[index].frame_begin &&
169 (!sequence_buffer_[prev_index].used ||
170 AheadOf(seq_num, sequence_buffer_[prev_index].seq_num))) {
171 // The reason we only return true if this packet is the first packet of the
172 // frame and the sequence number is newer than the packet with the previous
173 // index is because we want to avoid an inifite loop in the case where
174 // a single frame containing more packets than the current size of the
175 // packet buffer is inserted.
philipelc707ab72016-04-01 02:01:54 -0700176 return true;
philipelaee3e0e2016-11-01 11:45:34 +0100177 }
philipelc707ab72016-04-01 02:01:54 -0700178 if (!sequence_buffer_[prev_index].used)
179 return false;
philipelf4139332016-04-20 10:26:34 +0200180 if (sequence_buffer_[prev_index].seq_num !=
philipelaee3e0e2016-11-01 11:45:34 +0100181 sequence_buffer_[index].seq_num - 1) {
philipelf4139332016-04-20 10:26:34 +0200182 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100183 }
philipelc707ab72016-04-01 02:01:54 -0700184 if (sequence_buffer_[prev_index].continuous)
185 return true;
186
187 return false;
188}
189
philipelf4139332016-04-20 10:26:34 +0200190void PacketBuffer::FindFrames(uint16_t seq_num) {
philipelaee3e0e2016-11-01 11:45:34 +0100191 while (PotentialNewFrame(seq_num)) {
192 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -0700193 sequence_buffer_[index].continuous = true;
194
philipelf4139332016-04-20 10:26:34 +0200195 // If all packets of the frame is continuous, find the first packet of the
196 // frame and create an RtpFrameObject.
philipelc707ab72016-04-01 02:01:54 -0700197 if (sequence_buffer_[index].frame_end) {
philipel5ceaaae2016-05-24 10:20:47 +0200198 size_t frame_size = 0;
199 int max_nack_count = -1;
philipelc707ab72016-04-01 02:01:54 -0700200 uint16_t start_seq_num = seq_num;
philipelf4139332016-04-20 10:26:34 +0200201
philipel5ceaaae2016-05-24 10:20:47 +0200202 // Find the start index by searching backward until the packet with
203 // the |frame_begin| flag is set.
204 int start_index = index;
205 while (true) {
206 frame_size += data_buffer_[start_index].sizeBytes;
207 max_nack_count = std::max(
208 max_nack_count, data_buffer_[start_index].timesNacked);
philipelf4139332016-04-20 10:26:34 +0200209 sequence_buffer_[start_index].frame_created = true;
philipel5ceaaae2016-05-24 10:20:47 +0200210
211 if (sequence_buffer_[start_index].frame_begin)
212 break;
213
philipelf4139332016-04-20 10:26:34 +0200214 start_index = start_index > 0 ? start_index - 1 : size_ - 1;
philipelc707ab72016-04-01 02:01:54 -0700215 start_seq_num--;
216 }
217
philipelb4d31082016-07-11 08:46:29 -0700218 std::unique_ptr<RtpFrameObject> frame(
219 new RtpFrameObject(this, start_seq_num, seq_num, frame_size,
220 max_nack_count, clock_->TimeInMilliseconds()));
philipel17deeb42016-08-11 15:09:26 +0200221
222 received_frame_callback_->OnReceivedFrame(std::move(frame));
philipelc707ab72016-04-01 02:01:54 -0700223 }
224
philipelc707ab72016-04-01 02:01:54 -0700225 ++seq_num;
226 }
227}
228
229void PacketBuffer::ReturnFrame(RtpFrameObject* frame) {
230 rtc::CritScope lock(&crit_);
philipelf4139332016-04-20 10:26:34 +0200231 size_t index = frame->first_seq_num() % size_;
232 size_t end = (frame->last_seq_num() + 1) % size_;
233 uint16_t seq_num = frame->first_seq_num();
philipelc707ab72016-04-01 02:01:54 -0700234 while (index != end) {
philipel1f39ba12016-09-21 11:27:47 +0200235 if (sequence_buffer_[index].seq_num == seq_num) {
236 delete[] data_buffer_[index].dataPtr;
237 data_buffer_[index].dataPtr = nullptr;
philipelc707ab72016-04-01 02:01:54 -0700238 sequence_buffer_[index].used = false;
philipel1f39ba12016-09-21 11:27:47 +0200239 }
philipelf4139332016-04-20 10:26:34 +0200240
philipelc707ab72016-04-01 02:01:54 -0700241 index = (index + 1) % size_;
242 ++seq_num;
243 }
philipelc707ab72016-04-01 02:01:54 -0700244}
245
246bool PacketBuffer::GetBitstream(const RtpFrameObject& frame,
247 uint8_t* destination) {
248 rtc::CritScope lock(&crit_);
249
philipelf4139332016-04-20 10:26:34 +0200250 size_t index = frame.first_seq_num() % size_;
251 size_t end = (frame.last_seq_num() + 1) % size_;
252 uint16_t seq_num = frame.first_seq_num();
philipelc707ab72016-04-01 02:01:54 -0700253 while (index != end) {
254 if (!sequence_buffer_[index].used ||
255 sequence_buffer_[index].seq_num != seq_num) {
256 return false;
257 }
258
259 const uint8_t* source = data_buffer_[index].dataPtr;
260 size_t length = data_buffer_[index].sizeBytes;
261 memcpy(destination, source, length);
262 destination += length;
263 index = (index + 1) % size_;
264 ++seq_num;
265 }
266 return true;
267}
268
philipel02447bc2016-05-13 06:01:03 -0700269VCMPacket* PacketBuffer::GetPacket(uint16_t seq_num) {
270 rtc::CritScope lock(&crit_);
271 size_t index = seq_num % size_;
272 if (!sequence_buffer_[index].used ||
273 seq_num != sequence_buffer_[index].seq_num) {
274 return nullptr;
philipelf4139332016-04-20 10:26:34 +0200275 }
philipel02447bc2016-05-13 06:01:03 -0700276 return &data_buffer_[index];
philipelf4139332016-04-20 10:26:34 +0200277}
278
philipel17deeb42016-08-11 15:09:26 +0200279int PacketBuffer::AddRef() const {
280 return rtc::AtomicOps::Increment(&ref_count_);
281}
282
283int PacketBuffer::Release() const {
284 int count = rtc::AtomicOps::Decrement(&ref_count_);
285 if (!count) {
286 delete this;
287 }
288 return count;
289}
290
philipelc707ab72016-04-01 02:01:54 -0700291} // namespace video_coding
292} // namespace webrtc