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 #include "webrtc/modules/audio_coding/neteq/delay_manager.h" | 11 #include "webrtc/modules/audio_coding/neteq/delay_manager.h" |
12 | 12 |
13 #include <assert.h> | 13 #include <assert.h> |
14 #include <math.h> | 14 #include <math.h> |
15 | 15 |
16 #include <algorithm> // max, min | 16 #include <algorithm> // max, min |
17 | 17 |
18 #include "webrtc/base/safe_conversions.h" | 18 #include "webrtc/base/safe_conversions.h" |
19 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar
y.h" | 19 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar
y.h" |
20 #include "webrtc/modules/audio_coding/neteq/delay_peak_detector.h" | 20 #include "webrtc/modules/audio_coding/neteq/delay_peak_detector.h" |
21 #include "webrtc/modules/include/module_common_types.h" | 21 #include "webrtc/modules/include/module_common_types.h" |
22 #include "webrtc/system_wrappers/include/logging.h" | 22 #include "webrtc/system_wrappers/include/logging.h" |
23 | 23 |
24 namespace webrtc { | 24 namespace webrtc { |
25 | 25 |
26 DelayManager::DelayManager(size_t max_packets_in_buffer, | 26 DelayManager::DelayManager(size_t max_packets_in_buffer, |
27 DelayPeakDetector* peak_detector) | 27 DelayPeakDetector* peak_detector, |
| 28 const TickTimer* tick_timer) |
28 : first_packet_received_(false), | 29 : first_packet_received_(false), |
29 max_packets_in_buffer_(max_packets_in_buffer), | 30 max_packets_in_buffer_(max_packets_in_buffer), |
30 iat_vector_(kMaxIat + 1, 0), | 31 iat_vector_(kMaxIat + 1, 0), |
31 iat_factor_(0), | 32 iat_factor_(0), |
32 packet_iat_count_ms_(0), | 33 tick_timer_(tick_timer), |
33 base_target_level_(4), // In Q0 domain. | 34 base_target_level_(4), // In Q0 domain. |
34 target_level_(base_target_level_ << 8), // In Q8 domain. | 35 target_level_(base_target_level_ << 8), // In Q8 domain. |
35 packet_len_ms_(0), | 36 packet_len_ms_(0), |
36 streaming_mode_(false), | 37 streaming_mode_(false), |
37 last_seq_no_(0), | 38 last_seq_no_(0), |
38 last_timestamp_(0), | 39 last_timestamp_(0), |
39 minimum_delay_ms_(0), | 40 minimum_delay_ms_(0), |
40 least_required_delay_ms_(target_level_), | 41 least_required_delay_ms_(target_level_), |
41 maximum_delay_ms_(target_level_), | 42 maximum_delay_ms_(target_level_), |
42 iat_cumulative_sum_(0), | 43 iat_cumulative_sum_(0), |
43 max_iat_cumulative_sum_(0), | 44 max_iat_cumulative_sum_(0), |
44 max_timer_ms_(0), | |
45 peak_detector_(*peak_detector), | 45 peak_detector_(*peak_detector), |
46 last_pack_cng_or_dtmf_(1) { | 46 last_pack_cng_or_dtmf_(1) { |
47 assert(peak_detector); // Should never be NULL. | 47 assert(peak_detector); // Should never be NULL. |
48 Reset(); | 48 Reset(); |
49 } | 49 } |
50 | 50 |
51 DelayManager::~DelayManager() {} | 51 DelayManager::~DelayManager() {} |
52 | 52 |
53 const DelayManager::IATVector& DelayManager::iat_vector() const { | 53 const DelayManager::IATVector& DelayManager::iat_vector() const { |
54 return iat_vector_; | 54 return iat_vector_; |
(...skipping 17 matching lines...) Expand all Loading... |
72 | 72 |
73 int DelayManager::Update(uint16_t sequence_number, | 73 int DelayManager::Update(uint16_t sequence_number, |
74 uint32_t timestamp, | 74 uint32_t timestamp, |
75 int sample_rate_hz) { | 75 int sample_rate_hz) { |
76 if (sample_rate_hz <= 0) { | 76 if (sample_rate_hz <= 0) { |
77 return -1; | 77 return -1; |
78 } | 78 } |
79 | 79 |
80 if (!first_packet_received_) { | 80 if (!first_packet_received_) { |
81 // Prepare for next packet arrival. | 81 // Prepare for next packet arrival. |
82 packet_iat_count_ms_ = 0; | 82 packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
83 last_seq_no_ = sequence_number; | 83 last_seq_no_ = sequence_number; |
84 last_timestamp_ = timestamp; | 84 last_timestamp_ = timestamp; |
85 first_packet_received_ = true; | 85 first_packet_received_ = true; |
86 return 0; | 86 return 0; |
87 } | 87 } |
88 | 88 |
89 // Try calculating packet length from current and previous timestamps. | 89 // Try calculating packet length from current and previous timestamps. |
90 int packet_len_ms; | 90 int packet_len_ms; |
91 if (!IsNewerTimestamp(timestamp, last_timestamp_) || | 91 if (!IsNewerTimestamp(timestamp, last_timestamp_) || |
92 !IsNewerSequenceNumber(sequence_number, last_seq_no_)) { | 92 !IsNewerSequenceNumber(sequence_number, last_seq_no_)) { |
93 // Wrong timestamp or sequence order; use stored value. | 93 // Wrong timestamp or sequence order; use stored value. |
94 packet_len_ms = packet_len_ms_; | 94 packet_len_ms = packet_len_ms_; |
95 } else { | 95 } else { |
96 // Calculate timestamps per packet and derive packet length in ms. | 96 // Calculate timestamps per packet and derive packet length in ms. |
97 int64_t packet_len_samp = | 97 int64_t packet_len_samp = |
98 static_cast<uint32_t>(timestamp - last_timestamp_) / | 98 static_cast<uint32_t>(timestamp - last_timestamp_) / |
99 static_cast<uint16_t>(sequence_number - last_seq_no_); | 99 static_cast<uint16_t>(sequence_number - last_seq_no_); |
100 packet_len_ms = | 100 packet_len_ms = |
101 rtc::checked_cast<int>(1000 * packet_len_samp / sample_rate_hz); | 101 rtc::checked_cast<int>(1000 * packet_len_samp / sample_rate_hz); |
102 } | 102 } |
103 | 103 |
104 if (packet_len_ms > 0) { | 104 if (packet_len_ms > 0) { |
105 // Cannot update statistics unless |packet_len_ms| is valid. | 105 // Cannot update statistics unless |packet_len_ms| is valid. |
106 // Calculate inter-arrival time (IAT) in integer "packet times" | 106 // Calculate inter-arrival time (IAT) in integer "packet times" |
107 // (rounding down). This is the value used as index to the histogram | 107 // (rounding down). This is the value used as index to the histogram |
108 // vector |iat_vector_|. | 108 // vector |iat_vector_|. |
109 int iat_packets = packet_iat_count_ms_ / packet_len_ms; | 109 int iat_packets = packet_iat_stopwatch_->ElapsedMs() / packet_len_ms; |
110 | 110 |
111 if (streaming_mode_) { | 111 if (streaming_mode_) { |
112 UpdateCumulativeSums(packet_len_ms, sequence_number); | 112 UpdateCumulativeSums(packet_len_ms, sequence_number); |
113 } | 113 } |
114 | 114 |
115 // Check for discontinuous packet sequence and re-ordering. | 115 // Check for discontinuous packet sequence and re-ordering. |
116 if (IsNewerSequenceNumber(sequence_number, last_seq_no_ + 1)) { | 116 if (IsNewerSequenceNumber(sequence_number, last_seq_no_ + 1)) { |
117 // Compensate for gap in the sequence numbers. Reduce IAT with the | 117 // Compensate for gap in the sequence numbers. Reduce IAT with the |
118 // expected extra time due to lost packets, but ensure that the IAT is | 118 // expected extra time due to lost packets, but ensure that the IAT is |
119 // not negative. | 119 // not negative. |
(...skipping 10 matching lines...) Expand all Loading... |
130 // Calculate new |target_level_| based on updated statistics. | 130 // Calculate new |target_level_| based on updated statistics. |
131 target_level_ = CalculateTargetLevel(iat_packets); | 131 target_level_ = CalculateTargetLevel(iat_packets); |
132 if (streaming_mode_) { | 132 if (streaming_mode_) { |
133 target_level_ = std::max(target_level_, max_iat_cumulative_sum_); | 133 target_level_ = std::max(target_level_, max_iat_cumulative_sum_); |
134 } | 134 } |
135 | 135 |
136 LimitTargetLevel(); | 136 LimitTargetLevel(); |
137 } // End if (packet_len_ms > 0). | 137 } // End if (packet_len_ms > 0). |
138 | 138 |
139 // Prepare for next packet arrival. | 139 // Prepare for next packet arrival. |
140 packet_iat_count_ms_ = 0; | 140 packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
141 last_seq_no_ = sequence_number; | 141 last_seq_no_ = sequence_number; |
142 last_timestamp_ = timestamp; | 142 last_timestamp_ = timestamp; |
143 return 0; | 143 return 0; |
144 } | 144 } |
145 | 145 |
146 void DelayManager::UpdateCumulativeSums(int packet_len_ms, | 146 void DelayManager::UpdateCumulativeSums(int packet_len_ms, |
147 uint16_t sequence_number) { | 147 uint16_t sequence_number) { |
148 // Calculate IAT in Q8, including fractions of a packet (i.e., more | 148 // Calculate IAT in Q8, including fractions of a packet (i.e., more |
149 // accurate than |iat_packets|. | 149 // accurate than |iat_packets|. |
150 int iat_packets_q8 = (packet_iat_count_ms_ << 8) / packet_len_ms; | 150 int iat_packets_q8 = |
| 151 (packet_iat_stopwatch_->ElapsedMs() << 8) / packet_len_ms; |
151 // Calculate cumulative sum IAT with sequence number compensation. The sum | 152 // Calculate cumulative sum IAT with sequence number compensation. The sum |
152 // is zero if there is no clock-drift. | 153 // is zero if there is no clock-drift. |
153 iat_cumulative_sum_ += (iat_packets_q8 - | 154 iat_cumulative_sum_ += (iat_packets_q8 - |
154 (static_cast<int>(sequence_number - last_seq_no_) << 8)); | 155 (static_cast<int>(sequence_number - last_seq_no_) << 8)); |
155 // Subtract drift term. | 156 // Subtract drift term. |
156 iat_cumulative_sum_ -= kCumulativeSumDrift; | 157 iat_cumulative_sum_ -= kCumulativeSumDrift; |
157 // Ensure not negative. | 158 // Ensure not negative. |
158 iat_cumulative_sum_ = std::max(iat_cumulative_sum_, 0); | 159 iat_cumulative_sum_ = std::max(iat_cumulative_sum_, 0); |
159 if (iat_cumulative_sum_ > max_iat_cumulative_sum_) { | 160 if (iat_cumulative_sum_ > max_iat_cumulative_sum_) { |
160 // Found a new maximum. | 161 // Found a new maximum. |
161 max_iat_cumulative_sum_ = iat_cumulative_sum_; | 162 max_iat_cumulative_sum_ = iat_cumulative_sum_; |
162 max_timer_ms_ = 0; | 163 max_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
163 } | 164 } |
164 if (max_timer_ms_ > kMaxStreamingPeakPeriodMs) { | 165 if (max_iat_stopwatch_->ElapsedMs() > kMaxStreamingPeakPeriodMs) { |
165 // Too long since the last maximum was observed; decrease max value. | 166 // Too long since the last maximum was observed; decrease max value. |
166 max_iat_cumulative_sum_ -= kCumulativeSumDrift; | 167 max_iat_cumulative_sum_ -= kCumulativeSumDrift; |
167 } | 168 } |
168 } | 169 } |
169 | 170 |
170 // Each element in the vector is first multiplied by the forgetting factor | 171 // Each element in the vector is first multiplied by the forgetting factor |
171 // |iat_factor_|. Then the vector element indicated by |iat_packets| is then | 172 // |iat_factor_|. Then the vector element indicated by |iat_packets| is then |
172 // increased (additive) by 1 - |iat_factor_|. This way, the probability of | 173 // increased (additive) by 1 - |iat_factor_|. This way, the probability of |
173 // |iat_packets| is slightly increased, while the sum of the histogram remains | 174 // |iat_packets| is slightly increased, while the sum of the histogram remains |
174 // constant (=1). | 175 // constant (=1). |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 return target_level_; | 293 return target_level_; |
293 } | 294 } |
294 | 295 |
295 int DelayManager::SetPacketAudioLength(int length_ms) { | 296 int DelayManager::SetPacketAudioLength(int length_ms) { |
296 if (length_ms <= 0) { | 297 if (length_ms <= 0) { |
297 LOG_F(LS_ERROR) << "length_ms = " << length_ms; | 298 LOG_F(LS_ERROR) << "length_ms = " << length_ms; |
298 return -1; | 299 return -1; |
299 } | 300 } |
300 packet_len_ms_ = length_ms; | 301 packet_len_ms_ = length_ms; |
301 peak_detector_.SetPacketAudioLength(packet_len_ms_); | 302 peak_detector_.SetPacketAudioLength(packet_len_ms_); |
302 packet_iat_count_ms_ = 0; | 303 packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
303 last_pack_cng_or_dtmf_ = 1; // TODO(hlundin): Legacy. Remove? | 304 last_pack_cng_or_dtmf_ = 1; // TODO(hlundin): Legacy. Remove? |
304 return 0; | 305 return 0; |
305 } | 306 } |
306 | 307 |
307 | 308 |
308 void DelayManager::Reset() { | 309 void DelayManager::Reset() { |
309 packet_len_ms_ = 0; // Packet size unknown. | 310 packet_len_ms_ = 0; // Packet size unknown. |
310 streaming_mode_ = false; | 311 streaming_mode_ = false; |
311 peak_detector_.Reset(); | 312 peak_detector_.Reset(); |
312 ResetHistogram(); // Resets target levels too. | 313 ResetHistogram(); // Resets target levels too. |
313 iat_factor_ = 0; // Adapt the histogram faster for the first few packets. | 314 iat_factor_ = 0; // Adapt the histogram faster for the first few packets. |
314 packet_iat_count_ms_ = 0; | 315 packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
315 max_timer_ms_ = 0; | 316 max_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
316 iat_cumulative_sum_ = 0; | 317 iat_cumulative_sum_ = 0; |
317 max_iat_cumulative_sum_ = 0; | 318 max_iat_cumulative_sum_ = 0; |
318 last_pack_cng_or_dtmf_ = 1; | 319 last_pack_cng_or_dtmf_ = 1; |
319 } | 320 } |
320 | 321 |
321 int DelayManager::AverageIAT() const { | 322 int DelayManager::AverageIAT() const { |
322 int32_t sum_q24 = 0; | 323 int32_t sum_q24 = 0; |
323 // Using an int for the upper limit of the following for-loop so the | 324 // Using an int for the upper limit of the following for-loop so the |
324 // loop-counter can be int. Otherwise we need a cast where |sum_q24| is | 325 // loop-counter can be int. Otherwise we need a cast where |sum_q24| is |
325 // updated. | 326 // updated. |
326 const int iat_vec_size = static_cast<int>(iat_vector_.size()); | 327 const int iat_vec_size = static_cast<int>(iat_vector_.size()); |
327 assert(iat_vector_.size() == 65); // Algorithm is hard-coded for this size. | 328 assert(iat_vector_.size() == 65); // Algorithm is hard-coded for this size. |
328 for (int i = 0; i < iat_vec_size; ++i) { | 329 for (int i = 0; i < iat_vec_size; ++i) { |
329 // Shift 6 to fit worst case: 2^30 * 64. | 330 // Shift 6 to fit worst case: 2^30 * 64. |
330 sum_q24 += (iat_vector_[i] >> 6) * i; | 331 sum_q24 += (iat_vector_[i] >> 6) * i; |
331 } | 332 } |
332 // Subtract the nominal inter-arrival time 1 = 2^24 in Q24. | 333 // Subtract the nominal inter-arrival time 1 = 2^24 in Q24. |
333 sum_q24 -= (1 << 24); | 334 sum_q24 -= (1 << 24); |
334 // Multiply with 1000000 / 2^24 = 15625 / 2^18 to get in parts-per-million. | 335 // Multiply with 1000000 / 2^24 = 15625 / 2^18 to get in parts-per-million. |
335 // Shift 7 to Q17 first, then multiply with 15625 and shift another 11. | 336 // Shift 7 to Q17 first, then multiply with 15625 and shift another 11. |
336 return ((sum_q24 >> 7) * 15625) >> 11; | 337 return ((sum_q24 >> 7) * 15625) >> 11; |
337 } | 338 } |
338 | 339 |
339 bool DelayManager::PeakFound() const { | 340 bool DelayManager::PeakFound() const { |
340 return peak_detector_.peak_found(); | 341 return peak_detector_.peak_found(); |
341 } | 342 } |
342 | 343 |
343 void DelayManager::UpdateCounters(int elapsed_time_ms) { | 344 void DelayManager::ResetPacketIatCount() { |
344 packet_iat_count_ms_ += elapsed_time_ms; | 345 packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
345 max_timer_ms_ += elapsed_time_ms; | |
346 } | 346 } |
347 | 347 |
348 void DelayManager::ResetPacketIatCount() { packet_iat_count_ms_ = 0; } | |
349 | |
350 // Note that |low_limit| and |higher_limit| are not assigned to | 348 // Note that |low_limit| and |higher_limit| are not assigned to |
351 // |minimum_delay_ms_| and |maximum_delay_ms_| defined by the client of this | 349 // |minimum_delay_ms_| and |maximum_delay_ms_| defined by the client of this |
352 // class. They are computed from |target_level_| and used for decision making. | 350 // class. They are computed from |target_level_| and used for decision making. |
353 void DelayManager::BufferLimits(int* lower_limit, int* higher_limit) const { | 351 void DelayManager::BufferLimits(int* lower_limit, int* higher_limit) const { |
354 if (!lower_limit || !higher_limit) { | 352 if (!lower_limit || !higher_limit) { |
355 LOG_F(LS_ERROR) << "NULL pointers supplied as input"; | 353 LOG_F(LS_ERROR) << "NULL pointers supplied as input"; |
356 assert(false); | 354 assert(false); |
357 return; | 355 return; |
358 } | 356 } |
359 | 357 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 int DelayManager::base_target_level() const { return base_target_level_; } | 417 int DelayManager::base_target_level() const { return base_target_level_; } |
420 void DelayManager::set_streaming_mode(bool value) { streaming_mode_ = value; } | 418 void DelayManager::set_streaming_mode(bool value) { streaming_mode_ = value; } |
421 int DelayManager::last_pack_cng_or_dtmf() const { | 419 int DelayManager::last_pack_cng_or_dtmf() const { |
422 return last_pack_cng_or_dtmf_; | 420 return last_pack_cng_or_dtmf_; |
423 } | 421 } |
424 | 422 |
425 void DelayManager::set_last_pack_cng_or_dtmf(int value) { | 423 void DelayManager::set_last_pack_cng_or_dtmf(int value) { |
426 last_pack_cng_or_dtmf_ = value; | 424 last_pack_cng_or_dtmf_ = value; |
427 } | 425 } |
428 } // namespace webrtc | 426 } // namespace webrtc |
OLD | NEW |