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 #include "webrtc/modules/audio_coding/neteq/tick_timer.h" |
23 | 23 |
24 namespace webrtc { | 24 namespace webrtc { |
25 | 25 namespace { |
26 // Predicate used when inserting packets in the buffer list. | 26 // Predicate used when inserting packets in the buffer list. |
27 // Operator() returns true when |packet| goes before |new_packet|. | 27 // Operator() returns true when |packet| goes before |new_packet|. |
28 class NewTimestampIsLarger { | 28 class NewTimestampIsLarger { |
29 public: | 29 public: |
30 explicit NewTimestampIsLarger(const Packet* new_packet) | 30 explicit NewTimestampIsLarger(const Packet* new_packet) |
31 : new_packet_(new_packet) { | 31 : new_packet_(new_packet) { |
32 } | 32 } |
33 bool operator()(Packet* packet) { | 33 bool operator()(Packet* packet) { |
34 return (*new_packet_ >= *packet); | 34 return (*new_packet_ >= *packet); |
35 } | 35 } |
36 | 36 |
37 private: | 37 private: |
38 const Packet* new_packet_; | 38 const Packet* new_packet_; |
39 }; | 39 }; |
40 | 40 |
| 41 // Returns true if both payload types are known to the decoder database, and |
| 42 // have the same sample rate. |
| 43 bool EqualSampleRates(uint8_t pt1, |
| 44 uint8_t pt2, |
| 45 const DecoderDatabase& decoder_database) { |
| 46 auto di1 = decoder_database.GetDecoderInfo(pt1); |
| 47 auto di2 = decoder_database.GetDecoderInfo(pt2); |
| 48 return di1 && di2 && di1->SampleRateHz() == di2->SampleRateHz(); |
| 49 } |
| 50 } // namespace |
| 51 |
41 PacketBuffer::PacketBuffer(size_t max_number_of_packets, | 52 PacketBuffer::PacketBuffer(size_t max_number_of_packets, |
42 const TickTimer* tick_timer) | 53 const TickTimer* tick_timer) |
43 : max_number_of_packets_(max_number_of_packets), tick_timer_(tick_timer) {} | 54 : max_number_of_packets_(max_number_of_packets), tick_timer_(tick_timer) {} |
44 | 55 |
45 // Destructor. All packets in the buffer will be destroyed. | 56 // Destructor. All packets in the buffer will be destroyed. |
46 PacketBuffer::~PacketBuffer() { | 57 PacketBuffer::~PacketBuffer() { |
47 Flush(); | 58 Flush(); |
48 } | 59 } |
49 | 60 |
50 // Flush the buffer. All packets in the buffer will be destroyed. | 61 // Flush the buffer. All packets in the buffer will be destroyed. |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 **current_cng_rtp_payload_type != packet->header.payloadType) { | 130 **current_cng_rtp_payload_type != packet->header.payloadType) { |
120 // New CNG payload type implies new codec type. | 131 // New CNG payload type implies new codec type. |
121 *current_rtp_payload_type = rtc::Optional<uint8_t>(); | 132 *current_rtp_payload_type = rtc::Optional<uint8_t>(); |
122 Flush(); | 133 Flush(); |
123 flushed = true; | 134 flushed = true; |
124 } | 135 } |
125 *current_cng_rtp_payload_type = | 136 *current_cng_rtp_payload_type = |
126 rtc::Optional<uint8_t>(packet->header.payloadType); | 137 rtc::Optional<uint8_t>(packet->header.payloadType); |
127 } else if (!decoder_database.IsDtmf(packet->header.payloadType)) { | 138 } else if (!decoder_database.IsDtmf(packet->header.payloadType)) { |
128 // This must be speech. | 139 // This must be speech. |
129 if (*current_rtp_payload_type && | 140 if ((*current_rtp_payload_type && |
130 **current_rtp_payload_type != packet->header.payloadType) { | 141 **current_rtp_payload_type != packet->header.payloadType) || |
| 142 (*current_cng_rtp_payload_type && |
| 143 !EqualSampleRates(packet->header.payloadType, |
| 144 **current_cng_rtp_payload_type, |
| 145 decoder_database))) { |
131 *current_cng_rtp_payload_type = rtc::Optional<uint8_t>(); | 146 *current_cng_rtp_payload_type = rtc::Optional<uint8_t>(); |
132 Flush(); | 147 Flush(); |
133 flushed = true; | 148 flushed = true; |
134 } | 149 } |
135 *current_rtp_payload_type = | 150 *current_rtp_payload_type = |
136 rtc::Optional<uint8_t>(packet->header.payloadType); | 151 rtc::Optional<uint8_t>(packet->header.payloadType); |
137 } | 152 } |
138 int return_val = InsertPacket(packet); | 153 int return_val = InsertPacket(packet); |
139 packet_list->pop_front(); | 154 packet_list->pop_front(); |
140 if (return_val == kFlushed) { | 155 if (return_val == kFlushed) { |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 // Continue while the list is not empty. | 303 // Continue while the list is not empty. |
289 } | 304 } |
290 } | 305 } |
291 | 306 |
292 void PacketBuffer::BufferStat(int* num_packets, int* max_num_packets) const { | 307 void PacketBuffer::BufferStat(int* num_packets, int* max_num_packets) const { |
293 *num_packets = static_cast<int>(buffer_.size()); | 308 *num_packets = static_cast<int>(buffer_.size()); |
294 *max_num_packets = static_cast<int>(max_number_of_packets_); | 309 *max_num_packets = static_cast<int>(max_number_of_packets_); |
295 } | 310 } |
296 | 311 |
297 } // namespace webrtc | 312 } // namespace webrtc |
OLD | NEW |