| 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 |
| 11 #include "webrtc/modules/video_coding/packet_buffer.h" | 11 #include "webrtc/modules/video_coding/packet_buffer.h" |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 #include <limits> | 14 #include <limits> |
| 15 #include <utility> |
| 15 | 16 |
| 17 #include "webrtc/base/atomicops.h" |
| 16 #include "webrtc/base/checks.h" | 18 #include "webrtc/base/checks.h" |
| 17 #include "webrtc/base/logging.h" | 19 #include "webrtc/base/logging.h" |
| 18 #include "webrtc/modules/video_coding/frame_object.h" | 20 #include "webrtc/modules/video_coding/frame_object.h" |
| 19 #include "webrtc/system_wrappers/include/clock.h" | 21 #include "webrtc/system_wrappers/include/clock.h" |
| 20 | 22 |
| 21 namespace webrtc { | 23 namespace webrtc { |
| 22 namespace video_coding { | 24 namespace video_coding { |
| 23 | 25 |
| 26 rtc::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 |
| 24 PacketBuffer::PacketBuffer(Clock* clock, | 35 PacketBuffer::PacketBuffer(Clock* clock, |
| 25 size_t start_buffer_size, | 36 size_t start_buffer_size, |
| 26 size_t max_buffer_size, | 37 size_t max_buffer_size, |
| 27 OnCompleteFrameCallback* frame_callback) | 38 OnReceivedFrameCallback* received_frame_callback) |
| 28 : clock_(clock), | 39 : clock_(clock), |
| 29 size_(start_buffer_size), | 40 size_(start_buffer_size), |
| 30 max_size_(max_buffer_size), | 41 max_size_(max_buffer_size), |
| 31 first_seq_num_(0), | 42 first_seq_num_(0), |
| 32 last_seq_num_(0), | 43 last_seq_num_(0), |
| 33 first_packet_received_(false), | 44 first_packet_received_(false), |
| 34 data_buffer_(start_buffer_size), | 45 data_buffer_(start_buffer_size), |
| 35 sequence_buffer_(start_buffer_size), | 46 sequence_buffer_(start_buffer_size), |
| 36 reference_finder_(frame_callback) { | 47 received_frame_callback_(received_frame_callback) { |
| 37 RTC_DCHECK_LE(start_buffer_size, max_buffer_size); | 48 RTC_DCHECK_LE(start_buffer_size, max_buffer_size); |
| 38 // Buffer size must always be a power of 2. | 49 // Buffer size must always be a power of 2. |
| 39 RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0); | 50 RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0); |
| 40 RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0); | 51 RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0); |
| 41 } | 52 } |
| 42 | 53 |
| 54 PacketBuffer::~PacketBuffer() {} |
| 55 |
| 43 bool PacketBuffer::InsertPacket(const VCMPacket& packet) { | 56 bool PacketBuffer::InsertPacket(const VCMPacket& packet) { |
| 44 rtc::CritScope lock(&crit_); | 57 rtc::CritScope lock(&crit_); |
| 45 uint16_t seq_num = packet.seqNum; | 58 uint16_t seq_num = packet.seqNum; |
| 46 size_t index = seq_num % size_; | 59 size_t index = seq_num % size_; |
| 47 | 60 |
| 48 if (!first_packet_received_) { | 61 if (!first_packet_received_) { |
| 49 first_seq_num_ = seq_num - 1; | 62 first_seq_num_ = seq_num - 1; |
| 50 last_seq_num_ = seq_num; | 63 last_seq_num_ = seq_num; |
| 51 first_packet_received_ = true; | 64 first_packet_received_ = true; |
| 52 } | 65 } |
| 53 | 66 |
| 54 if (sequence_buffer_[index].used) { | 67 if (sequence_buffer_[index].used) { |
| 55 // Duplicate packet, do nothing. | 68 // Duplicate packet, do nothing. |
| 56 if (data_buffer_[index].seqNum == packet.seqNum) | 69 if (data_buffer_[index].seqNum == packet.seqNum) |
| 57 return true; | 70 return true; |
| 58 | 71 |
| 59 // The packet buffer is full, try to expand the buffer. | 72 // The packet buffer is full, try to expand the buffer. |
| 60 while (ExpandBufferSize() && sequence_buffer_[seq_num % size_].used) { | 73 while (ExpandBufferSize() && sequence_buffer_[seq_num % size_].used) { |
| 61 } | 74 } |
| 62 index = seq_num % size_; | 75 index = seq_num % size_; |
| 63 | 76 |
| 64 // Packet buffer is still full. | 77 // Packet buffer is still full. |
| 65 if (sequence_buffer_[index].used) | 78 if (sequence_buffer_[index].used) |
| 66 return false; | 79 return false; |
| 67 } | 80 } |
| 68 | 81 |
| 69 if (AheadOf(seq_num, last_seq_num_)) | 82 if (AheadOf(seq_num, last_seq_num_)) |
| 70 last_seq_num_ = seq_num; | 83 last_seq_num_ = seq_num; |
| 71 | 84 |
| 72 // If this is a padding or FEC packet, don't insert it. | |
| 73 if (packet.sizeBytes == 0) { | |
| 74 reference_finder_.PaddingReceived(packet.seqNum); | |
| 75 return true; | |
| 76 } | |
| 77 | |
| 78 sequence_buffer_[index].frame_begin = packet.isFirstPacket; | 85 sequence_buffer_[index].frame_begin = packet.isFirstPacket; |
| 79 sequence_buffer_[index].frame_end = packet.markerBit; | 86 sequence_buffer_[index].frame_end = packet.markerBit; |
| 80 sequence_buffer_[index].seq_num = packet.seqNum; | 87 sequence_buffer_[index].seq_num = packet.seqNum; |
| 81 sequence_buffer_[index].continuous = false; | 88 sequence_buffer_[index].continuous = false; |
| 82 sequence_buffer_[index].frame_created = false; | 89 sequence_buffer_[index].frame_created = false; |
| 83 sequence_buffer_[index].used = true; | 90 sequence_buffer_[index].used = true; |
| 84 data_buffer_[index] = packet; | 91 data_buffer_[index] = packet; |
| 85 | 92 |
| 86 FindFrames(seq_num); | 93 FindFrames(seq_num); |
| 87 return true; | 94 return true; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 if (sequence_buffer_[start_index].frame_begin) | 169 if (sequence_buffer_[start_index].frame_begin) |
| 163 break; | 170 break; |
| 164 | 171 |
| 165 start_index = start_index > 0 ? start_index - 1 : size_ - 1; | 172 start_index = start_index > 0 ? start_index - 1 : size_ - 1; |
| 166 start_seq_num--; | 173 start_seq_num--; |
| 167 } | 174 } |
| 168 | 175 |
| 169 std::unique_ptr<RtpFrameObject> frame( | 176 std::unique_ptr<RtpFrameObject> frame( |
| 170 new RtpFrameObject(this, start_seq_num, seq_num, frame_size, | 177 new RtpFrameObject(this, start_seq_num, seq_num, frame_size, |
| 171 max_nack_count, clock_->TimeInMilliseconds())); | 178 max_nack_count, clock_->TimeInMilliseconds())); |
| 172 reference_finder_.ManageFrame(std::move(frame)); | 179 |
| 180 received_frame_callback_->OnReceivedFrame(std::move(frame)); |
| 173 } | 181 } |
| 174 | 182 |
| 175 index = (index + 1) % size_; | 183 index = (index + 1) % size_; |
| 176 ++seq_num; | 184 ++seq_num; |
| 177 } | 185 } |
| 178 } | 186 } |
| 179 | 187 |
| 180 void PacketBuffer::ReturnFrame(RtpFrameObject* frame) { | 188 void PacketBuffer::ReturnFrame(RtpFrameObject* frame) { |
| 181 rtc::CritScope lock(&crit_); | 189 rtc::CritScope lock(&crit_); |
| 182 size_t index = frame->first_seq_num() % size_; | 190 size_t index = frame->first_seq_num() % size_; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 } | 240 } |
| 233 | 241 |
| 234 void PacketBuffer::Clear() { | 242 void PacketBuffer::Clear() { |
| 235 rtc::CritScope lock(&crit_); | 243 rtc::CritScope lock(&crit_); |
| 236 for (size_t i = 0; i < size_; ++i) | 244 for (size_t i = 0; i < size_; ++i) |
| 237 sequence_buffer_[i].used = false; | 245 sequence_buffer_[i].used = false; |
| 238 | 246 |
| 239 first_packet_received_ = false; | 247 first_packet_received_ = false; |
| 240 } | 248 } |
| 241 | 249 |
| 250 int PacketBuffer::AddRef() const { |
| 251 return rtc::AtomicOps::Increment(&ref_count_); |
| 252 } |
| 253 |
| 254 int PacketBuffer::Release() const { |
| 255 int count = rtc::AtomicOps::Decrement(&ref_count_); |
| 256 if (!count) { |
| 257 delete this; |
| 258 } |
| 259 return count; |
| 260 } |
| 261 |
| 242 } // namespace video_coding | 262 } // namespace video_coding |
| 243 } // namespace webrtc | 263 } // namespace webrtc |
| OLD | NEW |