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