• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
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
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "modules/audio_coding/neteq/delay_manager.h"
12 
13 #include <assert.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 
17 #include <algorithm>
18 #include <memory>
19 #include <numeric>
20 #include <string>
21 
22 #include "modules/audio_coding/neteq/histogram.h"
23 #include "modules/include/module_common_types_public.h"
24 #include "rtc_base/checks.h"
25 #include "rtc_base/logging.h"
26 #include "rtc_base/numerics/safe_conversions.h"
27 #include "rtc_base/numerics/safe_minmax.h"
28 #include "system_wrappers/include/field_trial.h"
29 
30 namespace {
31 
32 constexpr int kMinBaseMinimumDelayMs = 0;
33 constexpr int kMaxBaseMinimumDelayMs = 10000;
34 constexpr int kMaxReorderedPackets =
35     10;  // Max number of consecutive reordered packets.
36 constexpr int kMaxHistoryMs = 2000;  // Oldest packet to include in history to
37                                      // calculate relative packet arrival delay.
38 constexpr int kDelayBuckets = 100;
39 constexpr int kBucketSizeMs = 20;
40 constexpr int kDecelerationTargetLevelOffsetMs = 85 << 8;  // In Q8.
41 
PercentileToQuantile(double percentile)42 int PercentileToQuantile(double percentile) {
43   return static_cast<int>((1 << 30) * percentile / 100.0 + 0.5);
44 }
45 
46 struct DelayHistogramConfig {
47   int quantile = 1041529569;  // 0.97 in Q30.
48   int forget_factor = 32745;  // 0.9993 in Q15.
49   absl::optional<double> start_forget_weight = 2;
50 };
51 
GetDelayHistogramConfig()52 DelayHistogramConfig GetDelayHistogramConfig() {
53   constexpr char kDelayHistogramFieldTrial[] =
54       "WebRTC-Audio-NetEqDelayHistogram";
55   DelayHistogramConfig config;
56   if (webrtc::field_trial::IsEnabled(kDelayHistogramFieldTrial)) {
57     const auto field_trial_string =
58         webrtc::field_trial::FindFullName(kDelayHistogramFieldTrial);
59     double percentile = -1.0;
60     double forget_factor = -1.0;
61     double start_forget_weight = -1.0;
62     if (sscanf(field_trial_string.c_str(), "Enabled-%lf-%lf-%lf", &percentile,
63                &forget_factor, &start_forget_weight) >= 2 &&
64         percentile >= 0.0 && percentile <= 100.0 && forget_factor >= 0.0 &&
65         forget_factor <= 1.0) {
66       config.quantile = PercentileToQuantile(percentile);
67       config.forget_factor = (1 << 15) * forget_factor;
68       config.start_forget_weight =
69           start_forget_weight >= 1 ? absl::make_optional(start_forget_weight)
70                                    : absl::nullopt;
71     }
72   }
73   RTC_LOG(LS_INFO) << "Delay histogram config:"
74                       " quantile="
75                    << config.quantile
76                    << " forget_factor=" << config.forget_factor
77                    << " start_forget_weight="
78                    << config.start_forget_weight.value_or(0);
79   return config;
80 }
81 
82 }  // namespace
83 
84 namespace webrtc {
85 
DelayManager(size_t max_packets_in_buffer,int base_minimum_delay_ms,int histogram_quantile,bool enable_rtx_handling,const TickTimer * tick_timer,std::unique_ptr<Histogram> histogram)86 DelayManager::DelayManager(size_t max_packets_in_buffer,
87                            int base_minimum_delay_ms,
88                            int histogram_quantile,
89                            bool enable_rtx_handling,
90                            const TickTimer* tick_timer,
91                            std::unique_ptr<Histogram> histogram)
92     : first_packet_received_(false),
93       max_packets_in_buffer_(max_packets_in_buffer),
94       histogram_(std::move(histogram)),
95       histogram_quantile_(histogram_quantile),
96       tick_timer_(tick_timer),
97       base_minimum_delay_ms_(base_minimum_delay_ms),
98       effective_minimum_delay_ms_(base_minimum_delay_ms),
99       base_target_level_(4),                   // In Q0 domain.
100       target_level_(base_target_level_ << 8),  // In Q8 domain.
101       packet_len_ms_(0),
102       last_seq_no_(0),
103       last_timestamp_(0),
104       minimum_delay_ms_(0),
105       maximum_delay_ms_(0),
106       last_pack_cng_or_dtmf_(1),
107       enable_rtx_handling_(enable_rtx_handling) {
108   RTC_CHECK(histogram_);
109   RTC_DCHECK_GE(base_minimum_delay_ms_, 0);
110 
111   Reset();
112 }
113 
Create(size_t max_packets_in_buffer,int base_minimum_delay_ms,bool enable_rtx_handling,const TickTimer * tick_timer)114 std::unique_ptr<DelayManager> DelayManager::Create(
115     size_t max_packets_in_buffer,
116     int base_minimum_delay_ms,
117     bool enable_rtx_handling,
118     const TickTimer* tick_timer) {
119   DelayHistogramConfig config = GetDelayHistogramConfig();
120   const int quantile = config.quantile;
121   std::unique_ptr<Histogram> histogram = std::make_unique<Histogram>(
122       kDelayBuckets, config.forget_factor, config.start_forget_weight);
123   return std::make_unique<DelayManager>(
124       max_packets_in_buffer, base_minimum_delay_ms, quantile,
125       enable_rtx_handling, tick_timer, std::move(histogram));
126 }
127 
~DelayManager()128 DelayManager::~DelayManager() {}
129 
Update(uint16_t sequence_number,uint32_t timestamp,int sample_rate_hz)130 absl::optional<int> DelayManager::Update(uint16_t sequence_number,
131                                          uint32_t timestamp,
132                                          int sample_rate_hz) {
133   if (sample_rate_hz <= 0) {
134     return absl::nullopt;
135   }
136 
137   if (!first_packet_received_) {
138     // Prepare for next packet arrival.
139     packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch();
140     last_seq_no_ = sequence_number;
141     last_timestamp_ = timestamp;
142     first_packet_received_ = true;
143     return absl::nullopt;
144   }
145 
146   // Try calculating packet length from current and previous timestamps.
147   int packet_len_ms;
148   if (!IsNewerTimestamp(timestamp, last_timestamp_) ||
149       !IsNewerSequenceNumber(sequence_number, last_seq_no_)) {
150     // Wrong timestamp or sequence order; use stored value.
151     packet_len_ms = packet_len_ms_;
152   } else {
153     // Calculate timestamps per packet and derive packet length in ms.
154     int64_t packet_len_samp =
155         static_cast<uint32_t>(timestamp - last_timestamp_) /
156         static_cast<uint16_t>(sequence_number - last_seq_no_);
157     packet_len_ms =
158         rtc::saturated_cast<int>(1000 * packet_len_samp / sample_rate_hz);
159   }
160 
161   bool reordered = false;
162   absl::optional<int> relative_delay;
163   if (packet_len_ms > 0) {
164     // Cannot update statistics unless |packet_len_ms| is valid.
165 
166     // Inter-arrival time (IAT) in integer "packet times" (rounding down). This
167     // is the value added to the inter-arrival time histogram.
168     int iat_ms = packet_iat_stopwatch_->ElapsedMs();
169     // Check for discontinuous packet sequence and re-ordering.
170     if (IsNewerSequenceNumber(sequence_number, last_seq_no_ + 1)) {
171       // Compensate for gap in the sequence numbers. Reduce IAT with the
172       // expected extra time due to lost packets.
173       int packet_offset =
174           static_cast<uint16_t>(sequence_number - last_seq_no_ - 1);
175       iat_ms -= packet_offset * packet_len_ms;
176     } else if (!IsNewerSequenceNumber(sequence_number, last_seq_no_)) {
177       int packet_offset =
178           static_cast<uint16_t>(last_seq_no_ + 1 - sequence_number);
179       iat_ms += packet_offset * packet_len_ms;
180       reordered = true;
181     }
182 
183     int iat_delay = iat_ms - packet_len_ms;
184     if (reordered) {
185       relative_delay = std::max(iat_delay, 0);
186     } else {
187       UpdateDelayHistory(iat_delay, timestamp, sample_rate_hz);
188       relative_delay = CalculateRelativePacketArrivalDelay();
189     }
190 
191     const int index = relative_delay.value() / kBucketSizeMs;
192     if (index < histogram_->NumBuckets()) {
193       // Maximum delay to register is 2000 ms.
194       histogram_->Add(index);
195     }
196     // Calculate new |target_level_| based on updated statistics.
197     target_level_ = CalculateTargetLevel();
198 
199     LimitTargetLevel();
200   }  // End if (packet_len_ms > 0).
201 
202   if (enable_rtx_handling_ && reordered &&
203       num_reordered_packets_ < kMaxReorderedPackets) {
204     ++num_reordered_packets_;
205     return relative_delay;
206   }
207   num_reordered_packets_ = 0;
208   // Prepare for next packet arrival.
209   packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch();
210   last_seq_no_ = sequence_number;
211   last_timestamp_ = timestamp;
212   return relative_delay;
213 }
214 
UpdateDelayHistory(int iat_delay_ms,uint32_t timestamp,int sample_rate_hz)215 void DelayManager::UpdateDelayHistory(int iat_delay_ms,
216                                       uint32_t timestamp,
217                                       int sample_rate_hz) {
218   PacketDelay delay;
219   delay.iat_delay_ms = iat_delay_ms;
220   delay.timestamp = timestamp;
221   delay_history_.push_back(delay);
222   while (timestamp - delay_history_.front().timestamp >
223          static_cast<uint32_t>(kMaxHistoryMs * sample_rate_hz / 1000)) {
224     delay_history_.pop_front();
225   }
226 }
227 
CalculateRelativePacketArrivalDelay() const228 int DelayManager::CalculateRelativePacketArrivalDelay() const {
229   // This effectively calculates arrival delay of a packet relative to the
230   // packet preceding the history window. If the arrival delay ever becomes
231   // smaller than zero, it means the reference packet is invalid, and we
232   // move the reference.
233   int relative_delay = 0;
234   for (const PacketDelay& delay : delay_history_) {
235     relative_delay += delay.iat_delay_ms;
236     relative_delay = std::max(relative_delay, 0);
237   }
238   return relative_delay;
239 }
240 
241 // Enforces upper and lower limits for |target_level_|. The upper limit is
242 // chosen to be minimum of i) 75% of |max_packets_in_buffer_|, to leave some
243 // headroom for natural fluctuations around the target, and ii) equivalent of
244 // |maximum_delay_ms_| in packets. Note that in practice, if no
245 // |maximum_delay_ms_| is specified, this does not have any impact, since the
246 // target level is far below the buffer capacity in all reasonable cases.
247 // The lower limit is equivalent of |effective_minimum_delay_ms_| in packets.
248 // We update |least_required_level_| while the above limits are applied.
249 // TODO(hlundin): Move this check to the buffer logistics class.
LimitTargetLevel()250 void DelayManager::LimitTargetLevel() {
251   if (packet_len_ms_ > 0 && effective_minimum_delay_ms_ > 0) {
252     int minimum_delay_packet_q8 =
253         (effective_minimum_delay_ms_ << 8) / packet_len_ms_;
254     target_level_ = std::max(target_level_, minimum_delay_packet_q8);
255   }
256 
257   if (maximum_delay_ms_ > 0 && packet_len_ms_ > 0) {
258     int maximum_delay_packet_q8 = (maximum_delay_ms_ << 8) / packet_len_ms_;
259     target_level_ = std::min(target_level_, maximum_delay_packet_q8);
260   }
261 
262   // Shift to Q8, then 75%.;
263   int max_buffer_packets_q8 =
264       static_cast<int>((3 * (max_packets_in_buffer_ << 8)) / 4);
265   target_level_ = std::min(target_level_, max_buffer_packets_q8);
266 
267   // Sanity check, at least 1 packet (in Q8).
268   target_level_ = std::max(target_level_, 1 << 8);
269 }
270 
CalculateTargetLevel()271 int DelayManager::CalculateTargetLevel() {
272   int limit_probability = histogram_quantile_;
273 
274   int bucket_index = histogram_->Quantile(limit_probability);
275   int target_level = 1;
276   if (packet_len_ms_ > 0) {
277     target_level += bucket_index * kBucketSizeMs / packet_len_ms_;
278   }
279   base_target_level_ = target_level;
280 
281   // Sanity check. |target_level| must be strictly positive.
282   target_level = std::max(target_level, 1);
283   // Scale to Q8 and assign to member variable.
284   target_level_ = target_level << 8;
285   return target_level_;
286 }
287 
SetPacketAudioLength(int length_ms)288 int DelayManager::SetPacketAudioLength(int length_ms) {
289   if (length_ms <= 0) {
290     RTC_LOG_F(LS_ERROR) << "length_ms = " << length_ms;
291     return -1;
292   }
293 
294   packet_len_ms_ = length_ms;
295   packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch();
296   last_pack_cng_or_dtmf_ = 1;  // TODO(hlundin): Legacy. Remove?
297   return 0;
298 }
299 
Reset()300 void DelayManager::Reset() {
301   packet_len_ms_ = 0;  // Packet size unknown.
302   histogram_->Reset();
303   delay_history_.clear();
304   base_target_level_ = 4;
305   target_level_ = base_target_level_ << 8;
306   packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch();
307   last_pack_cng_or_dtmf_ = 1;
308 }
309 
ResetPacketIatCount()310 void DelayManager::ResetPacketIatCount() {
311   packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch();
312 }
313 
BufferLimits(int * lower_limit,int * higher_limit) const314 void DelayManager::BufferLimits(int* lower_limit, int* higher_limit) const {
315   BufferLimits(target_level_, lower_limit, higher_limit);
316 }
317 
318 // Note that |low_limit| and |higher_limit| are not assigned to
319 // |minimum_delay_ms_| and |maximum_delay_ms_| defined by the client of this
320 // class. They are computed from |target_level| in Q8 and used for decision
321 // making.
BufferLimits(int target_level,int * lower_limit,int * higher_limit) const322 void DelayManager::BufferLimits(int target_level,
323                                 int* lower_limit,
324                                 int* higher_limit) const {
325   if (!lower_limit || !higher_limit) {
326     RTC_LOG_F(LS_ERROR) << "NULL pointers supplied as input";
327     assert(false);
328     return;
329   }
330 
331   // |target_level| is in Q8 already.
332   *lower_limit = (target_level * 3) / 4;
333 
334   if (packet_len_ms_ > 0) {
335     *lower_limit =
336         std::max(*lower_limit, target_level - kDecelerationTargetLevelOffsetMs /
337                                                   packet_len_ms_);
338   }
339 
340   int window_20ms = 0x7FFF;  // Default large value for legacy bit-exactness.
341   if (packet_len_ms_ > 0) {
342     window_20ms = (20 << 8) / packet_len_ms_;
343   }
344   // |higher_limit| is equal to |target_level|, but should at
345   // least be 20 ms higher than |lower_limit|.
346   *higher_limit = std::max(target_level, *lower_limit + window_20ms);
347 }
348 
TargetLevel() const349 int DelayManager::TargetLevel() const {
350   return target_level_;
351 }
352 
LastDecodedWasCngOrDtmf(bool it_was)353 void DelayManager::LastDecodedWasCngOrDtmf(bool it_was) {
354   if (it_was) {
355     last_pack_cng_or_dtmf_ = 1;
356   } else if (last_pack_cng_or_dtmf_ != 0) {
357     last_pack_cng_or_dtmf_ = -1;
358   }
359 }
360 
RegisterEmptyPacket()361 void DelayManager::RegisterEmptyPacket() {
362   ++last_seq_no_;
363 }
364 
IsValidMinimumDelay(int delay_ms) const365 bool DelayManager::IsValidMinimumDelay(int delay_ms) const {
366   return 0 <= delay_ms && delay_ms <= MinimumDelayUpperBound();
367 }
368 
IsValidBaseMinimumDelay(int delay_ms) const369 bool DelayManager::IsValidBaseMinimumDelay(int delay_ms) const {
370   return kMinBaseMinimumDelayMs <= delay_ms &&
371          delay_ms <= kMaxBaseMinimumDelayMs;
372 }
373 
SetMinimumDelay(int delay_ms)374 bool DelayManager::SetMinimumDelay(int delay_ms) {
375   if (!IsValidMinimumDelay(delay_ms)) {
376     return false;
377   }
378 
379   minimum_delay_ms_ = delay_ms;
380   UpdateEffectiveMinimumDelay();
381   return true;
382 }
383 
SetMaximumDelay(int delay_ms)384 bool DelayManager::SetMaximumDelay(int delay_ms) {
385   // If |delay_ms| is zero then it unsets the maximum delay and target level is
386   // unconstrained by maximum delay.
387   if (delay_ms != 0 &&
388       (delay_ms < minimum_delay_ms_ || delay_ms < packet_len_ms_)) {
389     // Maximum delay shouldn't be less than minimum delay or less than a packet.
390     return false;
391   }
392 
393   maximum_delay_ms_ = delay_ms;
394   UpdateEffectiveMinimumDelay();
395   return true;
396 }
397 
SetBaseMinimumDelay(int delay_ms)398 bool DelayManager::SetBaseMinimumDelay(int delay_ms) {
399   if (!IsValidBaseMinimumDelay(delay_ms)) {
400     return false;
401   }
402 
403   base_minimum_delay_ms_ = delay_ms;
404   UpdateEffectiveMinimumDelay();
405   return true;
406 }
407 
GetBaseMinimumDelay() const408 int DelayManager::GetBaseMinimumDelay() const {
409   return base_minimum_delay_ms_;
410 }
411 
base_target_level() const412 int DelayManager::base_target_level() const {
413   return base_target_level_;
414 }
last_pack_cng_or_dtmf() const415 int DelayManager::last_pack_cng_or_dtmf() const {
416   return last_pack_cng_or_dtmf_;
417 }
418 
set_last_pack_cng_or_dtmf(int value)419 void DelayManager::set_last_pack_cng_or_dtmf(int value) {
420   last_pack_cng_or_dtmf_ = value;
421 }
422 
UpdateEffectiveMinimumDelay()423 void DelayManager::UpdateEffectiveMinimumDelay() {
424   // Clamp |base_minimum_delay_ms_| into the range which can be effectively
425   // used.
426   const int base_minimum_delay_ms =
427       rtc::SafeClamp(base_minimum_delay_ms_, 0, MinimumDelayUpperBound());
428   effective_minimum_delay_ms_ =
429       std::max(minimum_delay_ms_, base_minimum_delay_ms);
430 }
431 
MinimumDelayUpperBound() const432 int DelayManager::MinimumDelayUpperBound() const {
433   // Choose the lowest possible bound discarding 0 cases which mean the value
434   // is not set and unconstrained.
435   int q75 = MaxBufferTimeQ75();
436   q75 = q75 > 0 ? q75 : kMaxBaseMinimumDelayMs;
437   const int maximum_delay_ms =
438       maximum_delay_ms_ > 0 ? maximum_delay_ms_ : kMaxBaseMinimumDelayMs;
439   return std::min(maximum_delay_ms, q75);
440 }
441 
MaxBufferTimeQ75() const442 int DelayManager::MaxBufferTimeQ75() const {
443   const int max_buffer_time = max_packets_in_buffer_ * packet_len_ms_;
444   return rtc::dchecked_cast<int>(3 * max_buffer_time / 4);
445 }
446 
447 }  // namespace webrtc
448