OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 // Buffer size must always be a power of 2. | 50 // Buffer size must always be a power of 2. |
51 RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0); | 51 RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0); |
52 RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0); | 52 RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0); |
53 } | 53 } |
54 | 54 |
55 PacketBuffer::~PacketBuffer() { | 55 PacketBuffer::~PacketBuffer() { |
56 Clear(); | 56 Clear(); |
57 } | 57 } |
58 | 58 |
59 bool PacketBuffer::InsertPacket(const VCMPacket& packet) { | 59 bool PacketBuffer::InsertPacket(const VCMPacket& packet) { |
60 rtc::CritScope lock(&crit_); | 60 std::vector<std::unique_ptr<RtpFrameObject>> found_frames; |
61 uint16_t seq_num = packet.seqNum; | 61 { |
62 size_t index = seq_num % size_; | 62 rtc::CritScope lock(&crit_); |
| 63 uint16_t seq_num = packet.seqNum; |
| 64 size_t index = seq_num % size_; |
63 | 65 |
64 if (!first_packet_received_) { | 66 if (!first_packet_received_) { |
65 first_seq_num_ = seq_num; | 67 first_seq_num_ = seq_num; |
66 last_seq_num_ = seq_num; | 68 last_seq_num_ = seq_num; |
67 first_packet_received_ = true; | 69 first_packet_received_ = true; |
68 } else if (AheadOf(first_seq_num_, seq_num)) { | 70 } else if (AheadOf(first_seq_num_, seq_num)) { |
69 // If we have explicitly cleared past this packet then it's old, | 71 // If we have explicitly cleared past this packet then it's old, |
70 // don't insert it. | 72 // don't insert it. |
71 if (is_cleared_to_first_seq_num_) | 73 if (is_cleared_to_first_seq_num_) |
72 return false; | 74 return false; |
73 | 75 |
74 first_seq_num_ = seq_num; | 76 first_seq_num_ = seq_num; |
| 77 } |
| 78 |
| 79 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); |
75 } | 106 } |
76 | 107 |
77 if (sequence_buffer_[index].used) { | 108 for (std::unique_ptr<RtpFrameObject>& frame : found_frames) |
78 // Duplicate packet, do nothing. | 109 received_frame_callback_->OnReceivedFrame(std::move(frame)); |
79 if (data_buffer_[index].seqNum == packet.seqNum) | |
80 return true; | |
81 | 110 |
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; | |
99 sequence_buffer_[index].frame_created = false; | |
100 sequence_buffer_[index].used = true; | |
101 data_buffer_[index] = packet; | |
102 | |
103 FindFrames(seq_num); | |
104 return true; | 111 return true; |
105 } | 112 } |
106 | 113 |
107 void PacketBuffer::ClearTo(uint16_t seq_num) { | 114 void PacketBuffer::ClearTo(uint16_t seq_num) { |
108 rtc::CritScope lock(&crit_); | 115 rtc::CritScope lock(&crit_); |
109 | 116 |
110 // If the packet buffer was cleared between a frame was created and returned. | 117 // If the packet buffer was cleared between a frame was created and returned. |
111 if (!first_packet_received_) | 118 if (!first_packet_received_) |
112 return; | 119 return; |
113 | 120 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 if (sequence_buffer_[prev_index].seq_num != | 187 if (sequence_buffer_[prev_index].seq_num != |
181 sequence_buffer_[index].seq_num - 1) { | 188 sequence_buffer_[index].seq_num - 1) { |
182 return false; | 189 return false; |
183 } | 190 } |
184 if (sequence_buffer_[prev_index].continuous) | 191 if (sequence_buffer_[prev_index].continuous) |
185 return true; | 192 return true; |
186 | 193 |
187 return false; | 194 return false; |
188 } | 195 } |
189 | 196 |
190 void PacketBuffer::FindFrames(uint16_t seq_num) { | 197 std::vector<std::unique_ptr<RtpFrameObject>> PacketBuffer::FindFrames( |
| 198 uint16_t seq_num) { |
| 199 std::vector<std::unique_ptr<RtpFrameObject>> found_frames; |
191 while (PotentialNewFrame(seq_num)) { | 200 while (PotentialNewFrame(seq_num)) { |
192 size_t index = seq_num % size_; | 201 size_t index = seq_num % size_; |
193 sequence_buffer_[index].continuous = true; | 202 sequence_buffer_[index].continuous = true; |
194 | 203 |
195 // If all packets of the frame is continuous, find the first packet of the | 204 // If all packets of the frame is continuous, find the first packet of the |
196 // frame and create an RtpFrameObject. | 205 // frame and create an RtpFrameObject. |
197 if (sequence_buffer_[index].frame_end) { | 206 if (sequence_buffer_[index].frame_end) { |
198 size_t frame_size = 0; | 207 size_t frame_size = 0; |
199 int max_nack_count = -1; | 208 int max_nack_count = -1; |
200 uint16_t start_seq_num = seq_num; | 209 uint16_t start_seq_num = seq_num; |
201 | 210 |
202 // Find the start index by searching backward until the packet with | 211 // Find the start index by searching backward until the packet with |
203 // the |frame_begin| flag is set. | 212 // the |frame_begin| flag is set. |
204 int start_index = index; | 213 int start_index = index; |
205 while (true) { | 214 while (true) { |
206 frame_size += data_buffer_[start_index].sizeBytes; | 215 frame_size += data_buffer_[start_index].sizeBytes; |
207 max_nack_count = std::max( | 216 max_nack_count = |
208 max_nack_count, data_buffer_[start_index].timesNacked); | 217 std::max(max_nack_count, data_buffer_[start_index].timesNacked); |
209 sequence_buffer_[start_index].frame_created = true; | 218 sequence_buffer_[start_index].frame_created = true; |
210 | 219 |
211 if (sequence_buffer_[start_index].frame_begin) | 220 if (sequence_buffer_[start_index].frame_begin) |
212 break; | 221 break; |
213 | 222 |
214 start_index = start_index > 0 ? start_index - 1 : size_ - 1; | 223 start_index = start_index > 0 ? start_index - 1 : size_ - 1; |
215 start_seq_num--; | 224 start_seq_num--; |
216 } | 225 } |
217 | 226 |
218 std::unique_ptr<RtpFrameObject> frame( | 227 found_frames.emplace_back( |
219 new RtpFrameObject(this, start_seq_num, seq_num, frame_size, | 228 new RtpFrameObject(this, start_seq_num, seq_num, frame_size, |
220 max_nack_count, clock_->TimeInMilliseconds())); | 229 max_nack_count, clock_->TimeInMilliseconds())); |
221 | |
222 received_frame_callback_->OnReceivedFrame(std::move(frame)); | |
223 } | 230 } |
224 | |
225 ++seq_num; | 231 ++seq_num; |
226 } | 232 } |
| 233 return found_frames; |
227 } | 234 } |
228 | 235 |
229 void PacketBuffer::ReturnFrame(RtpFrameObject* frame) { | 236 void PacketBuffer::ReturnFrame(RtpFrameObject* frame) { |
230 rtc::CritScope lock(&crit_); | 237 rtc::CritScope lock(&crit_); |
231 size_t index = frame->first_seq_num() % size_; | 238 size_t index = frame->first_seq_num() % size_; |
232 size_t end = (frame->last_seq_num() + 1) % size_; | 239 size_t end = (frame->last_seq_num() + 1) % size_; |
233 uint16_t seq_num = frame->first_seq_num(); | 240 uint16_t seq_num = frame->first_seq_num(); |
234 while (index != end) { | 241 while (index != end) { |
235 if (sequence_buffer_[index].seq_num == seq_num) { | 242 if (sequence_buffer_[index].seq_num == seq_num) { |
236 delete[] data_buffer_[index].dataPtr; | 243 delete[] data_buffer_[index].dataPtr; |
(...skipping 23 matching lines...) Expand all Loading... |
260 size_t length = data_buffer_[index].sizeBytes; | 267 size_t length = data_buffer_[index].sizeBytes; |
261 memcpy(destination, source, length); | 268 memcpy(destination, source, length); |
262 destination += length; | 269 destination += length; |
263 index = (index + 1) % size_; | 270 index = (index + 1) % size_; |
264 ++seq_num; | 271 ++seq_num; |
265 } | 272 } |
266 return true; | 273 return true; |
267 } | 274 } |
268 | 275 |
269 VCMPacket* PacketBuffer::GetPacket(uint16_t seq_num) { | 276 VCMPacket* PacketBuffer::GetPacket(uint16_t seq_num) { |
270 rtc::CritScope lock(&crit_); | |
271 size_t index = seq_num % size_; | 277 size_t index = seq_num % size_; |
272 if (!sequence_buffer_[index].used || | 278 if (!sequence_buffer_[index].used || |
273 seq_num != sequence_buffer_[index].seq_num) { | 279 seq_num != sequence_buffer_[index].seq_num) { |
274 return nullptr; | 280 return nullptr; |
275 } | 281 } |
276 return &data_buffer_[index]; | 282 return &data_buffer_[index]; |
277 } | 283 } |
278 | 284 |
279 int PacketBuffer::AddRef() const { | 285 int PacketBuffer::AddRef() const { |
280 return rtc::AtomicOps::Increment(&ref_count_); | 286 return rtc::AtomicOps::Increment(&ref_count_); |
281 } | 287 } |
282 | 288 |
283 int PacketBuffer::Release() const { | 289 int PacketBuffer::Release() const { |
284 int count = rtc::AtomicOps::Decrement(&ref_count_); | 290 int count = rtc::AtomicOps::Decrement(&ref_count_); |
285 if (!count) { | 291 if (!count) { |
286 delete this; | 292 delete this; |
287 } | 293 } |
288 return count; | 294 return count; |
289 } | 295 } |
290 | 296 |
291 } // namespace video_coding | 297 } // namespace video_coding |
292 } // namespace webrtc | 298 } // namespace webrtc |
OLD | NEW |