OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 // Flush the buffer. All packets in the buffer will be destroyed. | 61 // Flush the buffer. All packets in the buffer will be destroyed. |
62 void PacketBuffer::Flush() { | 62 void PacketBuffer::Flush() { |
63 DeleteAllPackets(&buffer_); | 63 DeleteAllPackets(&buffer_); |
64 } | 64 } |
65 | 65 |
66 bool PacketBuffer::Empty() const { | 66 bool PacketBuffer::Empty() const { |
67 return buffer_.empty(); | 67 return buffer_.empty(); |
68 } | 68 } |
69 | 69 |
70 int PacketBuffer::InsertPacket(Packet* packet) { | 70 int PacketBuffer::InsertPacket(Packet* packet) { |
71 if (!packet || packet->payload.empty()) { | 71 if (!packet || packet->empty()) { |
72 if (packet) { | 72 if (packet) { |
73 delete packet; | 73 delete packet; |
74 } | 74 } |
75 LOG(LS_WARNING) << "InsertPacket invalid packet"; | 75 LOG(LS_WARNING) << "InsertPacket invalid packet"; |
76 return kInvalidPacket; | 76 return kInvalidPacket; |
77 } | 77 } |
78 | 78 |
79 int return_val = kOK; | 79 int return_val = kOK; |
80 | 80 |
81 packet->waiting_time = tick_timer_->GetNewStopwatch(); | 81 packet->waiting_time = tick_timer_->GetNewStopwatch(); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 } | 202 } |
203 | 203 |
204 Packet* PacketBuffer::GetNextPacket(size_t* discard_count) { | 204 Packet* PacketBuffer::GetNextPacket(size_t* discard_count) { |
205 if (Empty()) { | 205 if (Empty()) { |
206 // Buffer is empty. | 206 // Buffer is empty. |
207 return NULL; | 207 return NULL; |
208 } | 208 } |
209 | 209 |
210 Packet* packet = buffer_.front(); | 210 Packet* packet = buffer_.front(); |
211 // Assert that the packet sanity checks in InsertPacket method works. | 211 // Assert that the packet sanity checks in InsertPacket method works. |
212 assert(packet && !packet->payload.empty()); | 212 RTC_DCHECK(packet && !packet->empty()); |
213 buffer_.pop_front(); | 213 buffer_.pop_front(); |
214 | 214 |
215 // Discard other packets with the same timestamp. These are duplicates or | 215 // Discard other packets with the same timestamp. These are duplicates or |
216 // redundant payloads that should not be used. | 216 // redundant payloads that should not be used. |
217 size_t discards = 0; | 217 size_t discards = 0; |
218 | 218 |
219 while (!Empty() && | 219 while (!Empty() && |
220 buffer_.front()->header.timestamp == packet->header.timestamp) { | 220 buffer_.front()->header.timestamp == packet->header.timestamp) { |
221 if (DiscardNextPacket() != kOK) { | 221 if (DiscardNextPacket() != kOK) { |
222 assert(false); // Must be ok by design. | 222 assert(false); // Must be ok by design. |
223 } | 223 } |
224 ++discards; | 224 ++discards; |
225 } | 225 } |
226 // The way of inserting packet should not cause any packet discarding here. | 226 // The way of inserting packet should not cause any packet discarding here. |
227 // TODO(minyue): remove |discard_count|. | 227 // TODO(minyue): remove |discard_count|. |
228 assert(discards == 0); | 228 assert(discards == 0); |
229 if (discard_count) | 229 if (discard_count) |
230 *discard_count = discards; | 230 *discard_count = discards; |
231 | 231 |
232 return packet; | 232 return packet; |
233 } | 233 } |
234 | 234 |
235 int PacketBuffer::DiscardNextPacket() { | 235 int PacketBuffer::DiscardNextPacket() { |
236 if (Empty()) { | 236 if (Empty()) { |
237 return kBufferEmpty; | 237 return kBufferEmpty; |
238 } | 238 } |
239 // Assert that the packet sanity checks in InsertPacket method works. | 239 // Assert that the packet sanity checks in InsertPacket method works. |
240 assert(buffer_.front()); | 240 RTC_DCHECK(buffer_.front()); |
241 assert(!buffer_.front()->payload.empty()); | 241 RTC_DCHECK(!buffer_.front()->empty()); |
242 DeleteFirstPacket(&buffer_); | 242 DeleteFirstPacket(&buffer_); |
243 return kOK; | 243 return kOK; |
244 } | 244 } |
245 | 245 |
246 int PacketBuffer::DiscardOldPackets(uint32_t timestamp_limit, | 246 int PacketBuffer::DiscardOldPackets(uint32_t timestamp_limit, |
247 uint32_t horizon_samples) { | 247 uint32_t horizon_samples) { |
248 while (!Empty() && timestamp_limit != buffer_.front()->header.timestamp && | 248 while (!Empty() && timestamp_limit != buffer_.front()->header.timestamp && |
249 IsObsoleteTimestamp(buffer_.front()->header.timestamp, | 249 IsObsoleteTimestamp(buffer_.front()->header.timestamp, |
250 timestamp_limit, | 250 timestamp_limit, |
251 horizon_samples)) { | 251 horizon_samples)) { |
252 if (DiscardNextPacket() != kOK) { | 252 if (DiscardNextPacket() != kOK) { |
253 assert(false); // Must be ok by design. | 253 assert(false); // Must be ok by design. |
254 } | 254 } |
255 } | 255 } |
256 return 0; | 256 return 0; |
257 } | 257 } |
258 | 258 |
259 int PacketBuffer::DiscardAllOldPackets(uint32_t timestamp_limit) { | 259 int PacketBuffer::DiscardAllOldPackets(uint32_t timestamp_limit) { |
260 return DiscardOldPackets(timestamp_limit, 0); | 260 return DiscardOldPackets(timestamp_limit, 0); |
261 } | 261 } |
262 | 262 |
263 size_t PacketBuffer::NumPacketsInBuffer() const { | 263 size_t PacketBuffer::NumPacketsInBuffer() const { |
264 return buffer_.size(); | 264 return buffer_.size(); |
265 } | 265 } |
266 | 266 |
267 size_t PacketBuffer::NumSamplesInBuffer(DecoderDatabase* decoder_database, | 267 size_t PacketBuffer::NumSamplesInBuffer(size_t last_decoded_length) const { |
268 size_t last_decoded_length) const { | |
269 PacketList::const_iterator it; | |
270 size_t num_samples = 0; | 268 size_t num_samples = 0; |
271 size_t last_duration = last_decoded_length; | 269 size_t last_duration = last_decoded_length; |
272 for (it = buffer_.begin(); it != buffer_.end(); ++it) { | 270 for (Packet* packet : buffer_) { |
273 Packet* packet = (*it); | 271 if (packet->frame) { |
274 AudioDecoder* decoder = | |
275 decoder_database->GetDecoder(packet->header.payloadType); | |
276 if (decoder) { | |
277 if (!packet->primary) { | 272 if (!packet->primary) { |
278 continue; | 273 continue; |
279 } | 274 } |
280 int duration = decoder->PacketDuration(packet->payload.data(), | 275 size_t duration = packet->frame->Duration(); |
281 packet->payload.size()); | 276 if (duration > 0) { |
282 if (duration >= 0) { | |
283 last_duration = duration; // Save the most up-to-date (valid) duration. | 277 last_duration = duration; // Save the most up-to-date (valid) duration. |
284 } | 278 } |
285 } | 279 } |
286 num_samples += last_duration; | 280 num_samples += last_duration; |
287 } | 281 } |
288 return num_samples; | 282 return num_samples; |
289 } | 283 } |
290 | 284 |
291 bool PacketBuffer::DeleteFirstPacket(PacketList* packet_list) { | 285 bool PacketBuffer::DeleteFirstPacket(PacketList* packet_list) { |
292 if (packet_list->empty()) { | 286 if (packet_list->empty()) { |
(...skipping 10 matching lines...) Expand all Loading... |
303 // Continue while the list is not empty. | 297 // Continue while the list is not empty. |
304 } | 298 } |
305 } | 299 } |
306 | 300 |
307 void PacketBuffer::BufferStat(int* num_packets, int* max_num_packets) const { | 301 void PacketBuffer::BufferStat(int* num_packets, int* max_num_packets) const { |
308 *num_packets = static_cast<int>(buffer_.size()); | 302 *num_packets = static_cast<int>(buffer_.size()); |
309 *max_num_packets = static_cast<int>(max_number_of_packets_); | 303 *max_num_packets = static_cast<int>(max_number_of_packets_); |
310 } | 304 } |
311 | 305 |
312 } // namespace webrtc | 306 } // namespace webrtc |
OLD | NEW |