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 24 matching lines...) Expand all Loading... | |
35 PacketBuffer::PacketBuffer(Clock* clock, | 35 PacketBuffer::PacketBuffer(Clock* clock, |
36 size_t start_buffer_size, | 36 size_t start_buffer_size, |
37 size_t max_buffer_size, | 37 size_t max_buffer_size, |
38 OnReceivedFrameCallback* received_frame_callback) | 38 OnReceivedFrameCallback* received_frame_callback) |
39 : clock_(clock), | 39 : clock_(clock), |
40 size_(start_buffer_size), | 40 size_(start_buffer_size), |
41 max_size_(max_buffer_size), | 41 max_size_(max_buffer_size), |
42 first_seq_num_(0), | 42 first_seq_num_(0), |
43 last_seq_num_(0), | 43 last_seq_num_(0), |
44 first_packet_received_(false), | 44 first_packet_received_(false), |
45 has_cleared_to_(false), | |
45 data_buffer_(start_buffer_size), | 46 data_buffer_(start_buffer_size), |
46 sequence_buffer_(start_buffer_size), | 47 sequence_buffer_(start_buffer_size), |
47 received_frame_callback_(received_frame_callback) { | 48 received_frame_callback_(received_frame_callback) { |
48 RTC_DCHECK_LE(start_buffer_size, max_buffer_size); | 49 RTC_DCHECK_LE(start_buffer_size, max_buffer_size); |
49 // Buffer size must always be a power of 2. | 50 // Buffer size must always be a power of 2. |
50 RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0); | 51 RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0); |
51 RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0); | 52 RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0); |
52 } | 53 } |
53 | 54 |
54 PacketBuffer::~PacketBuffer() {} | 55 PacketBuffer::~PacketBuffer() { |
56 Clear(); | |
57 } | |
55 | 58 |
56 bool PacketBuffer::InsertPacket(const VCMPacket& packet) { | 59 bool PacketBuffer::InsertPacket(const VCMPacket& packet) { |
57 rtc::CritScope lock(&crit_); | 60 rtc::CritScope lock(&crit_); |
58 uint16_t seq_num = packet.seqNum; | 61 uint16_t seq_num = packet.seqNum; |
59 size_t index = seq_num % size_; | 62 size_t index = seq_num % size_; |
60 | 63 |
61 if (!first_packet_received_) { | 64 if (!first_packet_received_) { |
62 first_seq_num_ = seq_num - 1; | 65 first_seq_num_ = seq_num; |
63 last_seq_num_ = seq_num; | 66 last_seq_num_ = seq_num; |
64 first_packet_received_ = true; | 67 first_packet_received_ = true; |
68 } 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 (has_cleared_to_) | |
72 return false; | |
73 | |
74 first_seq_num_ = seq_num; | |
65 } | 75 } |
66 | 76 |
67 if (sequence_buffer_[index].used) { | 77 if (sequence_buffer_[index].used) { |
68 // Duplicate packet, do nothing. | 78 // Duplicate packet, do nothing. |
69 if (data_buffer_[index].seqNum == packet.seqNum) | 79 if (data_buffer_[index].seqNum == packet.seqNum) |
70 return true; | 80 return true; |
71 | 81 |
72 // The packet buffer is full, try to expand the buffer. | 82 // The packet buffer is full, try to expand the buffer. |
73 while (ExpandBufferSize() && sequence_buffer_[seq_num % size_].used) { | 83 while (ExpandBufferSize() && sequence_buffer_[seq_num % size_].used) { |
74 } | 84 } |
(...skipping 24 matching lines...) Expand all Loading... | |
99 memcpy(payload, packet.dataPtr, packet.sizeBytes); | 109 memcpy(payload, packet.dataPtr, packet.sizeBytes); |
100 data_buffer_[index].dataPtr = payload; | 110 data_buffer_[index].dataPtr = payload; |
101 } | 111 } |
102 | 112 |
103 FindFrames(seq_num); | 113 FindFrames(seq_num); |
104 return true; | 114 return true; |
105 } | 115 } |
106 | 116 |
107 void PacketBuffer::ClearTo(uint16_t seq_num) { | 117 void PacketBuffer::ClearTo(uint16_t seq_num) { |
108 rtc::CritScope lock(&crit_); | 118 rtc::CritScope lock(&crit_); |
109 size_t index = first_seq_num_ % size_; | 119 |
110 while (AheadOf<uint16_t>(seq_num, first_seq_num_ + 1)) { | 120 // If the packet buffer was cleared between a frame was created and returned. |
111 index = (index + 1) % size_; | 121 if (!first_packet_received_) |
112 ++first_seq_num_; | 122 return; |
123 | |
124 has_cleared_to_ = true; | |
125 while (AheadOrAt<uint16_t>(seq_num, first_seq_num_)) { | |
126 size_t index = first_seq_num_ % size_; | |
113 delete[] data_buffer_[index].dataPtr; | 127 delete[] data_buffer_[index].dataPtr; |
114 data_buffer_[index].dataPtr = nullptr; | 128 data_buffer_[index].dataPtr = nullptr; |
115 sequence_buffer_[index].used = false; | 129 sequence_buffer_[index].used = false; |
130 ++first_seq_num_; | |
116 } | 131 } |
117 } | 132 } |
118 | 133 |
134 void PacketBuffer::Clear() { | |
135 rtc::CritScope lock(&crit_); | |
136 for (size_t i = 0; i < size_; ++i) { | |
137 if (sequence_buffer_[i].used) | |
danilchap
2016/10/19 09:25:10
needed?
if you prefer to keep it, may be have all
philipel
2016/10/19 13:08:50
Removed if case.
| |
138 delete[] data_buffer_[i].dataPtr; | |
139 data_buffer_[i].dataPtr = nullptr; | |
140 sequence_buffer_[i].used = false; | |
141 } | |
142 | |
143 first_packet_received_ = false; | |
144 has_cleared_to_ = false; | |
145 } | |
146 | |
119 bool PacketBuffer::ExpandBufferSize() { | 147 bool PacketBuffer::ExpandBufferSize() { |
120 if (size_ == max_size_) | 148 if (size_ == max_size_) { |
149 LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_ | |
150 << "), failed to increase size."; | |
121 return false; | 151 return false; |
152 } | |
122 | 153 |
123 size_t new_size = std::min(max_size_, 2 * size_); | 154 size_t new_size = std::min(max_size_, 2 * size_); |
124 std::vector<VCMPacket> new_data_buffer(new_size); | 155 std::vector<VCMPacket> new_data_buffer(new_size); |
125 std::vector<ContinuityInfo> new_sequence_buffer(new_size); | 156 std::vector<ContinuityInfo> new_sequence_buffer(new_size); |
126 for (size_t i = 0; i < size_; ++i) { | 157 for (size_t i = 0; i < size_; ++i) { |
127 if (sequence_buffer_[i].used) { | 158 if (sequence_buffer_[i].used) { |
128 size_t index = sequence_buffer_[i].seq_num % new_size; | 159 size_t index = sequence_buffer_[i].seq_num % new_size; |
129 new_sequence_buffer[index] = sequence_buffer_[i]; | 160 new_sequence_buffer[index] = sequence_buffer_[i]; |
130 new_data_buffer[index] = data_buffer_[i]; | 161 new_data_buffer[index] = data_buffer_[i]; |
131 } | 162 } |
132 } | 163 } |
133 size_ = new_size; | 164 size_ = new_size; |
134 sequence_buffer_ = std::move(new_sequence_buffer); | 165 sequence_buffer_ = std::move(new_sequence_buffer); |
135 data_buffer_ = std::move(new_data_buffer); | 166 data_buffer_ = std::move(new_data_buffer); |
167 LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size; | |
136 return true; | 168 return true; |
137 } | 169 } |
138 | 170 |
139 bool PacketBuffer::IsContinuous(uint16_t seq_num) const { | 171 bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const { |
140 size_t index = seq_num % size_; | 172 size_t index = seq_num % size_; |
141 int prev_index = index > 0 ? index - 1 : size_ - 1; | 173 int prev_index = index > 0 ? index - 1 : size_ - 1; |
142 | 174 |
143 if (!sequence_buffer_[index].used) | 175 if (!sequence_buffer_[index].used) |
144 return false; | 176 return false; |
145 if (sequence_buffer_[index].frame_created) | 177 if (sequence_buffer_[index].frame_created) |
146 return false; | 178 return false; |
147 if (sequence_buffer_[index].frame_begin) | 179 if (sequence_buffer_[index].frame_begin && |
180 (!sequence_buffer_[prev_index].used || | |
181 AheadOf(seq_num, sequence_buffer_[prev_index].seq_num))) { | |
148 return true; | 182 return true; |
danilchap
2016/10/19 09:25:10
may be add a comment why true is returned, special
philipel
2016/10/19 13:08:50
Done.
| |
183 } | |
149 if (!sequence_buffer_[prev_index].used) | 184 if (!sequence_buffer_[prev_index].used) |
150 return false; | 185 return false; |
151 if (sequence_buffer_[prev_index].seq_num != | 186 if (sequence_buffer_[prev_index].seq_num != |
152 static_cast<uint16_t>(seq_num - 1)) | 187 sequence_buffer_[index].seq_num - 1) { |
153 return false; | 188 return false; |
189 } | |
154 if (sequence_buffer_[prev_index].continuous) | 190 if (sequence_buffer_[prev_index].continuous) |
155 return true; | 191 return true; |
156 | 192 |
157 return false; | 193 return false; |
158 } | 194 } |
159 | 195 |
160 void PacketBuffer::FindFrames(uint16_t seq_num) { | 196 void PacketBuffer::FindFrames(uint16_t seq_num) { |
161 size_t index = seq_num % size_; | 197 while (PotentialNewFrame(seq_num)) { |
162 while (IsContinuous(seq_num)) { | 198 size_t index = seq_num % size_; |
163 sequence_buffer_[index].continuous = true; | 199 sequence_buffer_[index].continuous = true; |
164 | 200 |
165 // If all packets of the frame is continuous, find the first packet of the | 201 // If all packets of the frame is continuous, find the first packet of the |
166 // frame and create an RtpFrameObject. | 202 // frame and create an RtpFrameObject. |
167 if (sequence_buffer_[index].frame_end) { | 203 if (sequence_buffer_[index].frame_end) { |
168 size_t frame_size = 0; | 204 size_t frame_size = 0; |
169 int max_nack_count = -1; | 205 int max_nack_count = -1; |
170 uint16_t start_seq_num = seq_num; | 206 uint16_t start_seq_num = seq_num; |
171 | 207 |
172 // Find the start index by searching backward until the packet with | 208 // Find the start index by searching backward until the packet with |
(...skipping 12 matching lines...) Expand all Loading... | |
185 start_seq_num--; | 221 start_seq_num--; |
186 } | 222 } |
187 | 223 |
188 std::unique_ptr<RtpFrameObject> frame( | 224 std::unique_ptr<RtpFrameObject> frame( |
189 new RtpFrameObject(this, start_seq_num, seq_num, frame_size, | 225 new RtpFrameObject(this, start_seq_num, seq_num, frame_size, |
190 max_nack_count, clock_->TimeInMilliseconds())); | 226 max_nack_count, clock_->TimeInMilliseconds())); |
191 | 227 |
192 received_frame_callback_->OnReceivedFrame(std::move(frame)); | 228 received_frame_callback_->OnReceivedFrame(std::move(frame)); |
193 } | 229 } |
194 | 230 |
195 index = (index + 1) % size_; | |
196 ++seq_num; | 231 ++seq_num; |
197 } | 232 } |
198 } | 233 } |
199 | 234 |
200 void PacketBuffer::ReturnFrame(RtpFrameObject* frame) { | 235 void PacketBuffer::ReturnFrame(RtpFrameObject* frame) { |
201 rtc::CritScope lock(&crit_); | 236 rtc::CritScope lock(&crit_); |
202 size_t index = frame->first_seq_num() % size_; | 237 size_t index = frame->first_seq_num() % size_; |
203 size_t end = (frame->last_seq_num() + 1) % size_; | 238 size_t end = (frame->last_seq_num() + 1) % size_; |
204 uint16_t seq_num = frame->first_seq_num(); | 239 uint16_t seq_num = frame->first_seq_num(); |
205 while (index != end) { | 240 while (index != end) { |
206 if (sequence_buffer_[index].seq_num == seq_num) { | 241 if (sequence_buffer_[index].seq_num == seq_num) { |
207 delete[] data_buffer_[index].dataPtr; | 242 delete[] data_buffer_[index].dataPtr; |
208 data_buffer_[index].dataPtr = nullptr; | 243 data_buffer_[index].dataPtr = nullptr; |
209 sequence_buffer_[index].used = false; | 244 sequence_buffer_[index].used = false; |
210 } | 245 } |
211 | 246 |
212 index = (index + 1) % size_; | 247 index = (index + 1) % size_; |
213 ++seq_num; | 248 ++seq_num; |
214 } | 249 } |
215 | |
216 index = first_seq_num_ % size_; | |
217 while (AheadOf<uint16_t>(last_seq_num_, first_seq_num_) && | |
218 !sequence_buffer_[index].used) { | |
219 ++first_seq_num_; | |
220 index = (index + 1) % size_; | |
221 } | |
222 } | 250 } |
223 | 251 |
224 bool PacketBuffer::GetBitstream(const RtpFrameObject& frame, | 252 bool PacketBuffer::GetBitstream(const RtpFrameObject& frame, |
225 uint8_t* destination) { | 253 uint8_t* destination) { |
226 rtc::CritScope lock(&crit_); | 254 rtc::CritScope lock(&crit_); |
227 | 255 |
228 size_t index = frame.first_seq_num() % size_; | 256 size_t index = frame.first_seq_num() % size_; |
229 size_t end = (frame.last_seq_num() + 1) % size_; | 257 size_t end = (frame.last_seq_num() + 1) % size_; |
230 uint16_t seq_num = frame.first_seq_num(); | 258 uint16_t seq_num = frame.first_seq_num(); |
231 while (index != end) { | 259 while (index != end) { |
(...skipping 15 matching lines...) Expand all Loading... | |
247 VCMPacket* PacketBuffer::GetPacket(uint16_t seq_num) { | 275 VCMPacket* PacketBuffer::GetPacket(uint16_t seq_num) { |
248 rtc::CritScope lock(&crit_); | 276 rtc::CritScope lock(&crit_); |
249 size_t index = seq_num % size_; | 277 size_t index = seq_num % size_; |
250 if (!sequence_buffer_[index].used || | 278 if (!sequence_buffer_[index].used || |
251 seq_num != sequence_buffer_[index].seq_num) { | 279 seq_num != sequence_buffer_[index].seq_num) { |
252 return nullptr; | 280 return nullptr; |
253 } | 281 } |
254 return &data_buffer_[index]; | 282 return &data_buffer_[index]; |
255 } | 283 } |
256 | 284 |
257 void PacketBuffer::Clear() { | |
258 rtc::CritScope lock(&crit_); | |
259 for (size_t i = 0; i < size_; ++i) | |
260 sequence_buffer_[i].used = false; | |
261 | |
262 first_packet_received_ = false; | |
263 } | |
264 | |
265 int PacketBuffer::AddRef() const { | 285 int PacketBuffer::AddRef() const { |
266 return rtc::AtomicOps::Increment(&ref_count_); | 286 return rtc::AtomicOps::Increment(&ref_count_); |
267 } | 287 } |
268 | 288 |
269 int PacketBuffer::Release() const { | 289 int PacketBuffer::Release() const { |
270 int count = rtc::AtomicOps::Decrement(&ref_count_); | 290 int count = rtc::AtomicOps::Decrement(&ref_count_); |
271 if (!count) { | 291 if (!count) { |
272 delete this; | 292 delete this; |
273 } | 293 } |
274 return count; | 294 return count; |
275 } | 295 } |
276 | 296 |
277 } // namespace video_coding | 297 } // namespace video_coding |
278 } // namespace webrtc | 298 } // namespace webrtc |
OLD | NEW |