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 |
11 // This is the implementation of the PacketBuffer class. It is mostly based on | 11 // This is the implementation of the PacketBuffer class. It is mostly based on |
12 // an STL list. The list is kept sorted at all times so that the next packet to | 12 // an STL list. The list is kept sorted at all times so that the next packet to |
13 // decode is at the beginning of the list. | 13 // decode is at the beginning of the list. |
14 | 14 |
15 #include "webrtc/modules/audio_coding/neteq/packet_buffer.h" | 15 #include "webrtc/modules/audio_coding/neteq/packet_buffer.h" |
16 | 16 |
17 #include <algorithm> // find_if() | 17 #include <algorithm> // find_if() |
18 | 18 |
19 #include "webrtc/base/logging.h" | 19 #include "webrtc/base/logging.h" |
20 #include "webrtc/modules/audio_coding/codecs/audio_decoder.h" | 20 #include "webrtc/modules/audio_coding/codecs/audio_decoder.h" |
21 #include "webrtc/modules/audio_coding/neteq/decoder_database.h" | 21 #include "webrtc/modules/audio_coding/neteq/decoder_database.h" |
| 22 #include "webrtc/modules/audio_coding/neteq/tick_timer.h" |
22 | 23 |
23 namespace webrtc { | 24 namespace webrtc { |
24 | 25 |
25 // Predicate used when inserting packets in the buffer list. | 26 // Predicate used when inserting packets in the buffer list. |
26 // Operator() returns true when |packet| goes before |new_packet|. | 27 // Operator() returns true when |packet| goes before |new_packet|. |
27 class NewTimestampIsLarger { | 28 class NewTimestampIsLarger { |
28 public: | 29 public: |
29 explicit NewTimestampIsLarger(const Packet* new_packet) | 30 explicit NewTimestampIsLarger(const Packet* new_packet) |
30 : new_packet_(new_packet) { | 31 : new_packet_(new_packet) { |
31 } | 32 } |
32 bool operator()(Packet* packet) { | 33 bool operator()(Packet* packet) { |
33 return (*new_packet_ >= *packet); | 34 return (*new_packet_ >= *packet); |
34 } | 35 } |
35 | 36 |
36 private: | 37 private: |
37 const Packet* new_packet_; | 38 const Packet* new_packet_; |
38 }; | 39 }; |
39 | 40 |
40 PacketBuffer::PacketBuffer(size_t max_number_of_packets) | 41 PacketBuffer::PacketBuffer(size_t max_number_of_packets, |
41 : max_number_of_packets_(max_number_of_packets) {} | 42 const TickTimer& tick_timer) |
| 43 : max_number_of_packets_(max_number_of_packets), tick_timer_(tick_timer) {} |
42 | 44 |
43 // Destructor. All packets in the buffer will be destroyed. | 45 // Destructor. All packets in the buffer will be destroyed. |
44 PacketBuffer::~PacketBuffer() { | 46 PacketBuffer::~PacketBuffer() { |
45 Flush(); | 47 Flush(); |
46 } | 48 } |
47 | 49 |
48 // Flush the buffer. All packets in the buffer will be destroyed. | 50 // Flush the buffer. All packets in the buffer will be destroyed. |
49 void PacketBuffer::Flush() { | 51 void PacketBuffer::Flush() { |
50 DeleteAllPackets(&buffer_); | 52 DeleteAllPackets(&buffer_); |
51 } | 53 } |
52 | 54 |
53 bool PacketBuffer::Empty() const { | 55 bool PacketBuffer::Empty() const { |
54 return buffer_.empty(); | 56 return buffer_.empty(); |
55 } | 57 } |
56 | 58 |
57 int PacketBuffer::InsertPacket(Packet* packet) { | 59 int PacketBuffer::InsertPacket(Packet* packet) { |
58 if (!packet || !packet->payload) { | 60 if (!packet || !packet->payload) { |
59 if (packet) { | 61 if (packet) { |
60 delete packet; | 62 delete packet; |
61 } | 63 } |
62 LOG(LS_WARNING) << "InsertPacket invalid packet"; | 64 LOG(LS_WARNING) << "InsertPacket invalid packet"; |
63 return kInvalidPacket; | 65 return kInvalidPacket; |
64 } | 66 } |
65 | 67 |
66 int return_val = kOK; | 68 int return_val = kOK; |
67 | 69 |
| 70 packet->waiting_time = tick_timer_.GetNewStopwatch(); |
| 71 |
68 if (buffer_.size() >= max_number_of_packets_) { | 72 if (buffer_.size() >= max_number_of_packets_) { |
69 // Buffer is full. Flush it. | 73 // Buffer is full. Flush it. |
70 Flush(); | 74 Flush(); |
71 LOG(LS_WARNING) << "Packet buffer flushed"; | 75 LOG(LS_WARNING) << "Packet buffer flushed"; |
72 return_val = kFlushed; | 76 return_val = kFlushed; |
73 } | 77 } |
74 | 78 |
75 // Get an iterator pointing to the place in the buffer where the new packet | 79 // Get an iterator pointing to the place in the buffer where the new packet |
76 // should be inserted. The list is searched from the back, since the most | 80 // should be inserted. The list is searched from the back, since the most |
77 // likely case is that the new packet should be near the end of the list. | 81 // likely case is that the new packet should be near the end of the list. |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 decoder->PacketDuration(packet->payload, packet->payload_length); | 265 decoder->PacketDuration(packet->payload, packet->payload_length); |
262 if (duration >= 0) { | 266 if (duration >= 0) { |
263 last_duration = duration; // Save the most up-to-date (valid) duration. | 267 last_duration = duration; // Save the most up-to-date (valid) duration. |
264 } | 268 } |
265 } | 269 } |
266 num_samples += last_duration; | 270 num_samples += last_duration; |
267 } | 271 } |
268 return num_samples; | 272 return num_samples; |
269 } | 273 } |
270 | 274 |
271 void PacketBuffer::IncrementWaitingTimes(int inc) { | |
272 PacketList::iterator it; | |
273 for (it = buffer_.begin(); it != buffer_.end(); ++it) { | |
274 (*it)->waiting_time += inc; | |
275 } | |
276 } | |
277 | |
278 bool PacketBuffer::DeleteFirstPacket(PacketList* packet_list) { | 275 bool PacketBuffer::DeleteFirstPacket(PacketList* packet_list) { |
279 if (packet_list->empty()) { | 276 if (packet_list->empty()) { |
280 return false; | 277 return false; |
281 } | 278 } |
282 Packet* first_packet = packet_list->front(); | 279 Packet* first_packet = packet_list->front(); |
283 delete [] first_packet->payload; | 280 delete [] first_packet->payload; |
284 delete first_packet; | 281 delete first_packet; |
285 packet_list->pop_front(); | 282 packet_list->pop_front(); |
286 return true; | 283 return true; |
287 } | 284 } |
288 | 285 |
289 void PacketBuffer::DeleteAllPackets(PacketList* packet_list) { | 286 void PacketBuffer::DeleteAllPackets(PacketList* packet_list) { |
290 while (DeleteFirstPacket(packet_list)) { | 287 while (DeleteFirstPacket(packet_list)) { |
291 // Continue while the list is not empty. | 288 // Continue while the list is not empty. |
292 } | 289 } |
293 } | 290 } |
294 | 291 |
295 void PacketBuffer::BufferStat(int* num_packets, int* max_num_packets) const { | 292 void PacketBuffer::BufferStat(int* num_packets, int* max_num_packets) const { |
296 *num_packets = static_cast<int>(buffer_.size()); | 293 *num_packets = static_cast<int>(buffer_.size()); |
297 *max_num_packets = static_cast<int>(max_number_of_packets_); | 294 *max_num_packets = static_cast<int>(max_number_of_packets_); |
298 } | 295 } |
299 | 296 |
300 } // namespace webrtc | 297 } // namespace webrtc |
OLD | NEW |