1d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org/*
2d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *
4d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *  Use of this source code is governed by a BSD-style license
5d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *  that can be found in the LICENSE file in the root of the source
6d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *  tree. An additional intellectual property rights grant can be found
7d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *  in the file PATENTS.  All contributing project authors may
8d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org */
10d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
119c55f0f957534144d2b8a64154f0a479249b34behenrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/delay_manager.h"
12d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
13d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org#include <assert.h>
14d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org#include <math.h>
15d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
16d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org#include <algorithm>  // max, min
17d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
18d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
199c55f0f957534144d2b8a64154f0a479249b34behenrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/delay_peak_detector.h"
20ff761fba8274d93bd73e76c8b8a1f2d0776dd840Henrik Kjellander#include "webrtc/modules/include/module_common_types.h"
2198f53510b222f71fdd8b799b2f33737ceeb28c61Henrik Kjellander#include "webrtc/system_wrappers/include/logging.h"
22d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
23d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgnamespace webrtc {
24d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
25dce40cf804019a9898b6ab8d8262466b697c56e0Peter KastingDelayManager::DelayManager(size_t max_packets_in_buffer,
26d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org                           DelayPeakDetector* peak_detector)
27d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    : first_packet_received_(false),
28d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      max_packets_in_buffer_(max_packets_in_buffer),
29d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      iat_vector_(kMaxIat + 1, 0),
30d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      iat_factor_(0),
31d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      packet_iat_count_ms_(0),
32d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      base_target_level_(4),  // In Q0 domain.
33d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      target_level_(base_target_level_ << 8),  // In Q8 domain.
34d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      packet_len_ms_(0),
35d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      streaming_mode_(false),
36d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      last_seq_no_(0),
37d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      last_timestamp_(0),
38f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org      minimum_delay_ms_(0),
39f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org      least_required_delay_ms_(target_level_),
40f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org      maximum_delay_ms_(target_level_),
41d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      iat_cumulative_sum_(0),
42d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      max_iat_cumulative_sum_(0),
43d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      max_timer_ms_(0),
44d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      peak_detector_(*peak_detector),
45d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      last_pack_cng_or_dtmf_(1) {
46d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  assert(peak_detector);  // Should never be NULL.
47d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  Reset();
48d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}
49d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
502d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.orgDelayManager::~DelayManager() {}
512d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.org
522d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.orgconst DelayManager::IATVector& DelayManager::iat_vector() const {
532d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.org  return iat_vector_;
542d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.org}
552d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.org
56d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// Set the histogram vector to an exponentially decaying distribution
57d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// iat_vector_[i] = 0.5^(i+1), i = 0, 1, 2, ...
58d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// iat_vector_ is in Q30.
59d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgvoid DelayManager::ResetHistogram() {
60d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Set temp_prob to (slightly more than) 1 in Q14. This ensures that the sum
61d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // of iat_vector_ is 1.
62d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  uint16_t temp_prob = 0x4002;  // 16384 + 2 = 100000000000010 binary.
63d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  IATVector::iterator it = iat_vector_.begin();
64d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  for (; it < iat_vector_.end(); it++) {
65d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    temp_prob >>= 1;
66d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    (*it) = temp_prob << 16;
67d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  }
68d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  base_target_level_ = 4;
69d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  target_level_ = base_target_level_ << 8;
70d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}
71d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
72d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgint DelayManager::Update(uint16_t sequence_number,
73d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org                         uint32_t timestamp,
74d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org                         int sample_rate_hz) {
75d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  if (sample_rate_hz <= 0) {
76d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    return -1;
77d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  }
78d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
79d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  if (!first_packet_received_) {
80d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // Prepare for next packet arrival.
81d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    packet_iat_count_ms_ = 0;
82d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    last_seq_no_ = sequence_number;
83d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    last_timestamp_ = timestamp;
84d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    first_packet_received_ = true;
85d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    return 0;
86d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  }
87d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
88d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Try calculating packet length from current and previous timestamps.
89d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  int packet_len_ms;
9078b41a09bd87e5be674ed854e8cc6c00e62ee108turaj@webrtc.org  if (!IsNewerTimestamp(timestamp, last_timestamp_) ||
9178b41a09bd87e5be674ed854e8cc6c00e62ee108turaj@webrtc.org      !IsNewerSequenceNumber(sequence_number, last_seq_no_)) {
92d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // Wrong timestamp or sequence order; use stored value.
93d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    packet_len_ms = packet_len_ms_;
94d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  } else {
95d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // Calculate timestamps per packet and derive packet length in ms.
96d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    int packet_len_samp =
97d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        static_cast<uint32_t>(timestamp - last_timestamp_) /
98d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        static_cast<uint16_t>(sequence_number - last_seq_no_);
99d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    packet_len_ms = (1000 * packet_len_samp) / sample_rate_hz;
100d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  }
101d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
102d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  if (packet_len_ms > 0) {
103d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // Cannot update statistics unless |packet_len_ms| is valid.
104d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // Calculate inter-arrival time (IAT) in integer "packet times"
105d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // (rounding down). This is the value used as index to the histogram
106d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // vector |iat_vector_|.
107d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    int iat_packets = packet_iat_count_ms_ / packet_len_ms;
108d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
109d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    if (streaming_mode_) {
110d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      UpdateCumulativeSums(packet_len_ms, sequence_number);
111d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    }
112d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
113d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // Check for discontinuous packet sequence and re-ordering.
11478b41a09bd87e5be674ed854e8cc6c00e62ee108turaj@webrtc.org    if (IsNewerSequenceNumber(sequence_number, last_seq_no_ + 1)) {
115d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      // Compensate for gap in the sequence numbers. Reduce IAT with the
116d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      // expected extra time due to lost packets, but ensure that the IAT is
117d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      // not negative.
11878b41a09bd87e5be674ed854e8cc6c00e62ee108turaj@webrtc.org      iat_packets -= static_cast<uint16_t>(sequence_number - last_seq_no_ - 1);
119d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      iat_packets = std::max(iat_packets, 0);
12078b41a09bd87e5be674ed854e8cc6c00e62ee108turaj@webrtc.org    } else if (!IsNewerSequenceNumber(sequence_number, last_seq_no_)) {
12178b41a09bd87e5be674ed854e8cc6c00e62ee108turaj@webrtc.org      iat_packets += static_cast<uint16_t>(last_seq_no_ + 1 - sequence_number);
122d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    }
123d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
124d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // Saturate IAT at maximum value.
125d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    const int max_iat = kMaxIat;
126d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    iat_packets = std::min(iat_packets, max_iat);
127d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    UpdateHistogram(iat_packets);
128d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // Calculate new |target_level_| based on updated statistics.
129d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    target_level_ = CalculateTargetLevel(iat_packets);
130d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    if (streaming_mode_) {
131d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      target_level_ = std::max(target_level_, max_iat_cumulative_sum_);
132d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    }
133d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
134d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    LimitTargetLevel();
135d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  }  // End if (packet_len_ms > 0).
136d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
137d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Prepare for next packet arrival.
138d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  packet_iat_count_ms_ = 0;
139d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  last_seq_no_ = sequence_number;
140d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  last_timestamp_ = timestamp;
141d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  return 0;
142d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}
143d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
144d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgvoid DelayManager::UpdateCumulativeSums(int packet_len_ms,
145d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org                                        uint16_t sequence_number) {
146d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Calculate IAT in Q8, including fractions of a packet (i.e., more
147d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // accurate than |iat_packets|.
148d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  int iat_packets_q8 = (packet_iat_count_ms_ << 8) / packet_len_ms;
149d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Calculate cumulative sum IAT with sequence number compensation. The sum
150d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // is zero if there is no clock-drift.
151d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  iat_cumulative_sum_ += (iat_packets_q8 -
152d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      (static_cast<int>(sequence_number - last_seq_no_) << 8));
153d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Subtract drift term.
154d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  iat_cumulative_sum_ -= kCumulativeSumDrift;
155d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Ensure not negative.
156d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  iat_cumulative_sum_ = std::max(iat_cumulative_sum_, 0);
157d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  if (iat_cumulative_sum_ > max_iat_cumulative_sum_) {
158d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // Found a new maximum.
159d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    max_iat_cumulative_sum_ = iat_cumulative_sum_;
160d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    max_timer_ms_ = 0;
161d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  }
162d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  if (max_timer_ms_ > kMaxStreamingPeakPeriodMs) {
163d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // Too long since the last maximum was observed; decrease max value.
164d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    max_iat_cumulative_sum_ -= kCumulativeSumDrift;
165d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  }
166d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}
167d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
168d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// Each element in the vector is first multiplied by the forgetting factor
169d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// |iat_factor_|. Then the vector element indicated by |iat_packets| is then
170d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// increased (additive) by 1 - |iat_factor_|. This way, the probability of
171d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// |iat_packets| is slightly increased, while the sum of the histogram remains
172d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// constant (=1).
173d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// Due to inaccuracies in the fixed-point arithmetic, the histogram may no
174d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// longer sum up to 1 (in Q30) after the update. To correct this, a correction
175d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// term is added or subtracted from the first element (or elements) of the
176d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// vector.
177d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// The forgetting factor |iat_factor_| is also updated. When the DelayManager
178d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// is reset, the factor is set to 0 to facilitate rapid convergence in the
179d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// beginning. With each update of the histogram, the factor is increased towards
180d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// the steady-state value |kIatFactor_|.
181d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgvoid DelayManager::UpdateHistogram(size_t iat_packets) {
182d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  assert(iat_packets < iat_vector_.size());
183d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  int vector_sum = 0;  // Sum up the vector elements as they are processed.
184d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Multiply each element in |iat_vector_| with |iat_factor_|.
185d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  for (IATVector::iterator it = iat_vector_.begin();
186d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      it != iat_vector_.end(); ++it) {
187d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    *it = (static_cast<int64_t>(*it) * iat_factor_) >> 15;
188d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    vector_sum += *it;
189d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  }
190d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
191d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Increase the probability for the currently observed inter-arrival time
192d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // by 1 - |iat_factor_|. The factor is in Q15, |iat_vector_| in Q30.
193d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Thus, left-shift 15 steps to obtain result in Q30.
194d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  iat_vector_[iat_packets] += (32768 - iat_factor_) << 15;
195d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  vector_sum += (32768 - iat_factor_) << 15;  // Add to vector sum.
196d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
197d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // |iat_vector_| should sum up to 1 (in Q30), but it may not due to
198d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // fixed-point rounding errors.
199d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  vector_sum -= 1 << 30;  // Should be zero. Compensate if not.
200d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  if (vector_sum != 0) {
201d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // Modify a few values early in |iat_vector_|.
202d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    int flip_sign = vector_sum > 0 ? -1 : 1;
203d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    IATVector::iterator it = iat_vector_.begin();
204d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    while (it != iat_vector_.end() && abs(vector_sum) > 0) {
205d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      // Add/subtract 1/16 of the element, but not more than |vector_sum|.
206d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      int correction = flip_sign * std::min(abs(vector_sum), (*it) >> 4);
207d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      *it += correction;
208d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      vector_sum += correction;
209d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      ++it;
210d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    }
211d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  }
212d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  assert(vector_sum == 0);  // Verify that the above is correct.
213d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
214d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Update |iat_factor_| (changes only during the first seconds after a reset).
215d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // The factor converges to |kIatFactor_|.
216d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  iat_factor_ += (kIatFactor_ - iat_factor_ + 3) >> 2;
217d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}
218d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
219f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org// Enforces upper and lower limits for |target_level_|. The upper limit is
220f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org// chosen to be minimum of i) 75% of |max_packets_in_buffer_|, to leave some
221f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org// headroom for natural fluctuations around the target, and ii) equivalent of
222f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org// |maximum_delay_ms_| in packets. Note that in practice, if no
223f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org// |maximum_delay_ms_| is specified, this does not have any impact, since the
224f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org// target level is far below the buffer capacity in all reasonable cases.
225f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org// The lower limit is equivalent of |minimum_delay_ms_| in packets. We update
226f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org// |least_required_level_| while the above limits are applied.
227d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// TODO(hlundin): Move this check to the buffer logistics class.
228d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgvoid DelayManager::LimitTargetLevel() {
229f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  least_required_delay_ms_ = (target_level_ * packet_len_ms_) >> 8;
230f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org
231f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  if (packet_len_ms_ > 0 && minimum_delay_ms_ > 0) {
232f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org    int minimum_delay_packet_q8 =  (minimum_delay_ms_ << 8) / packet_len_ms_;
233f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org    target_level_ = std::max(target_level_, minimum_delay_packet_q8);
234f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  }
235f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org
236f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  if (maximum_delay_ms_ > 0 && packet_len_ms_ > 0) {
237f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org    int maximum_delay_packet_q8 = (maximum_delay_ms_ << 8) / packet_len_ms_;
238f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org    target_level_ = std::min(target_level_, maximum_delay_packet_q8);
239d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  }
240f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org
241f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  // Shift to Q8, then 75%.;
242dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting  int max_buffer_packets_q8 =
243dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting      static_cast<int>((3 * (max_packets_in_buffer_ << 8)) / 4);
244f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  target_level_ = std::min(target_level_, max_buffer_packets_q8);
245f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org
246f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  // Sanity check, at least 1 packet (in Q8).
247f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  target_level_ = std::max(target_level_, 1 << 8);
248d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}
249d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
250d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgint DelayManager::CalculateTargetLevel(int iat_packets) {
251d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  int limit_probability = kLimitProbability;
252d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  if (streaming_mode_) {
253d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    limit_probability = kLimitProbabilityStreaming;
254d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  }
255d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
256d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Calculate target buffer level from inter-arrival time histogram.
257d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Find the |iat_index| for which the probability of observing an
258d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // inter-arrival time larger than or equal to |iat_index| is less than or
259d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // equal to |limit_probability|. The sought probability is estimated using
260d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // the histogram as the reverse cumulant PDF, i.e., the sum of elements from
261d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // the end up until |iat_index|. Now, since the sum of all elements is 1
262d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // (in Q30) by definition, and since the solution is often a low value for
263d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // |iat_index|, it is more efficient to start with |sum| = 1 and subtract
264d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // elements from the start of the histogram.
265d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  size_t index = 0;  // Start from the beginning of |iat_vector_|.
266d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  int sum = 1 << 30;  // Assign to 1 in Q30.
267d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  sum -= iat_vector_[index];  // Ensure that target level is >= 1.
268d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
269d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  do {
270d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // Subtract the probabilities one by one until the sum is no longer greater
271d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // than limit_probability.
272d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    ++index;
273d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    sum -= iat_vector_[index];
274d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  } while ((sum > limit_probability) && (index < iat_vector_.size() - 1));
275d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
276d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // This is the base value for the target buffer level.
277362a55e7b0852a7be95f0d627321503258152551turaj@webrtc.org  int target_level = static_cast<int>(index);
278362a55e7b0852a7be95f0d627321503258152551turaj@webrtc.org  base_target_level_ = static_cast<int>(index);
279d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
280d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Update detector for delay peaks.
281d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  bool delay_peak_found = peak_detector_.Update(iat_packets, target_level);
282d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  if (delay_peak_found) {
283362a55e7b0852a7be95f0d627321503258152551turaj@webrtc.org    target_level = std::max(target_level, peak_detector_.MaxPeakHeight());
284d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  }
285d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
286d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Sanity check. |target_level| must be strictly positive.
287d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  target_level = std::max(target_level, 1);
288d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Scale to Q8 and assign to member variable.
289d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  target_level_ = target_level << 8;
290d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  return target_level_;
291d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}
292d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
293d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgint DelayManager::SetPacketAudioLength(int length_ms) {
294d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  if (length_ms <= 0) {
295d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    LOG_F(LS_ERROR) << "length_ms = " << length_ms;
296d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    return -1;
297d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  }
298d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  packet_len_ms_ = length_ms;
299d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  peak_detector_.SetPacketAudioLength(packet_len_ms_);
300d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  packet_iat_count_ms_ = 0;
301d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  last_pack_cng_or_dtmf_ = 1;  // TODO(hlundin): Legacy. Remove?
302d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  return 0;
303d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}
304d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
305d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
306d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgvoid DelayManager::Reset() {
307d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  packet_len_ms_ = 0;  // Packet size unknown.
308d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  streaming_mode_ = false;
309d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  peak_detector_.Reset();
310d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  ResetHistogram();  // Resets target levels too.
311d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  iat_factor_ = 0;  // Adapt the histogram faster for the first few packets.
312d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  packet_iat_count_ms_ = 0;
313d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  max_timer_ms_ = 0;
314d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  iat_cumulative_sum_ = 0;
315d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  max_iat_cumulative_sum_ = 0;
316d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  last_pack_cng_or_dtmf_ = 1;
317d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}
318d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
319d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgint DelayManager::AverageIAT() const {
320d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  int32_t sum_q24 = 0;
321362a55e7b0852a7be95f0d627321503258152551turaj@webrtc.org  // Using an int for the upper limit of the following for-loop so the
322362a55e7b0852a7be95f0d627321503258152551turaj@webrtc.org  // loop-counter can be int. Otherwise we need a cast where |sum_q24| is
323362a55e7b0852a7be95f0d627321503258152551turaj@webrtc.org  // updated.
324362a55e7b0852a7be95f0d627321503258152551turaj@webrtc.org  const int iat_vec_size = static_cast<int>(iat_vector_.size());
325d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  assert(iat_vector_.size() == 65);  // Algorithm is hard-coded for this size.
326362a55e7b0852a7be95f0d627321503258152551turaj@webrtc.org  for (int i = 0; i < iat_vec_size; ++i) {
327d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // Shift 6 to fit worst case: 2^30 * 64.
328d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    sum_q24 += (iat_vector_[i] >> 6) * i;
329d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  }
330d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Subtract the nominal inter-arrival time 1 = 2^24 in Q24.
331d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  sum_q24 -= (1 << 24);
332d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Multiply with 1000000 / 2^24 = 15625 / 2^18 to get in parts-per-million.
333d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Shift 7 to Q17 first, then multiply with 15625 and shift another 11.
334d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  return ((sum_q24 >> 7) * 15625) >> 11;
335d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}
336d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
337d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgbool DelayManager::PeakFound() const {
338d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  return peak_detector_.peak_found();
339d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}
340d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
341d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgvoid DelayManager::UpdateCounters(int elapsed_time_ms) {
342d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  packet_iat_count_ms_ += elapsed_time_ms;
343d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  peak_detector_.IncrementCounter(elapsed_time_ms);
344d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  max_timer_ms_ += elapsed_time_ms;
345d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}
346d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
3472d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.orgvoid DelayManager::ResetPacketIatCount() { packet_iat_count_ms_ = 0; }
3482d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.org
349f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org// Note that |low_limit| and |higher_limit| are not assigned to
350f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org// |minimum_delay_ms_| and |maximum_delay_ms_| defined by the client of this
351f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org// class. They are computed from |target_level_| and used for decision making.
352d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgvoid DelayManager::BufferLimits(int* lower_limit, int* higher_limit) const {
353d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  if (!lower_limit || !higher_limit) {
354d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    LOG_F(LS_ERROR) << "NULL pointers supplied as input";
355d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    assert(false);
356d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    return;
357d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  }
358d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
359d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  int window_20ms = 0x7FFF;  // Default large value for legacy bit-exactness.
360d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  if (packet_len_ms_ > 0) {
361d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    window_20ms = (20 << 8) / packet_len_ms_;
362d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  }
363f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org
364d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // |target_level_| is in Q8 already.
365f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  *lower_limit = (target_level_ * 3) / 4;
366f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  // |higher_limit| is equal to |target_level_|, but should at
367d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // least be 20 ms higher than |lower_limit_|.
368f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  *higher_limit = std::max(target_level_, *lower_limit + window_20ms);
369d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}
370d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
371d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgint DelayManager::TargetLevel() const {
372f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  return target_level_;
373d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}
374d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
375d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgvoid DelayManager::LastDecoderType(NetEqDecoder decoder_type) {
376ee1879ca40ffe4af9bb9613e03eacc5c2c4881fckwiberg  if (decoder_type == NetEqDecoder::kDecoderAVT ||
377ee1879ca40ffe4af9bb9613e03eacc5c2c4881fckwiberg      decoder_type == NetEqDecoder::kDecoderCNGnb ||
378ee1879ca40ffe4af9bb9613e03eacc5c2c4881fckwiberg      decoder_type == NetEqDecoder::kDecoderCNGwb ||
379ee1879ca40ffe4af9bb9613e03eacc5c2c4881fckwiberg      decoder_type == NetEqDecoder::kDecoderCNGswb32kHz ||
380ee1879ca40ffe4af9bb9613e03eacc5c2c4881fckwiberg      decoder_type == NetEqDecoder::kDecoderCNGswb48kHz) {
381d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    last_pack_cng_or_dtmf_ = 1;
382d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  } else if (last_pack_cng_or_dtmf_ != 0) {
383d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    last_pack_cng_or_dtmf_ = -1;
384d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  }
385d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}
3862d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.org
387f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.orgbool DelayManager::SetMinimumDelay(int delay_ms) {
388f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  // Minimum delay shouldn't be more than maximum delay, if any maximum is set.
389f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  // Also, if possible check |delay| to less than 75% of
390f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  // |max_packets_in_buffer_|.
391f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  if ((maximum_delay_ms_ > 0 && delay_ms > maximum_delay_ms_) ||
392f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org      (packet_len_ms_ > 0 &&
393dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting       delay_ms >
394dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting           static_cast<int>(3 * max_packets_in_buffer_ * packet_len_ms_ / 4))) {
395f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org    return false;
396f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  }
397f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  minimum_delay_ms_ = delay_ms;
398f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  return true;
399f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org}
400f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org
401f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.orgbool DelayManager::SetMaximumDelay(int delay_ms) {
402f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  if (delay_ms == 0) {
403f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org    // Zero input unsets the maximum delay.
404f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org    maximum_delay_ms_ = 0;
405f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org    return true;
406f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  } else if (delay_ms < minimum_delay_ms_ || delay_ms < packet_len_ms_) {
407f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org    // Maximum delay shouldn't be less than minimum delay or less than a packet.
408f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org    return false;
409f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  }
410f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  maximum_delay_ms_ = delay_ms;
411f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  return true;
412f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org}
413f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org
414f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.orgint DelayManager::least_required_delay_ms() const {
415f1efc5713907e019207fbf7522706321a8c4647fturaj@webrtc.org  return least_required_delay_ms_;
4162d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.org}
4172d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.org
4182d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.orgint DelayManager::base_target_level() const { return base_target_level_; }
4192d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.orgvoid DelayManager::set_streaming_mode(bool value) { streaming_mode_ = value; }
4202d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.orgint DelayManager::last_pack_cng_or_dtmf() const {
4212d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.org  return last_pack_cng_or_dtmf_;
4222d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.org}
4232d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.org
4242d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.orgvoid DelayManager::set_last_pack_cng_or_dtmf(int value) {
4252d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.org  last_pack_cng_or_dtmf_ = value;
4262d1a55caed6f8203b2198fe261c0eeaf7177262fpbos@webrtc.org}
427d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}  // namespace webrtc
428