blob: 6f3c14aea9da0eda02ca4608283453c6bdf1358b [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000011#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_
12#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000014#include <string.h> // Provide access to size_t.
15
henrik.lundin8f8c96d2016-04-28 23:19:20 -070016#include <memory>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000017#include <vector>
18
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000019#include "webrtc/base/constructormagic.h"
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000020#include "webrtc/modules/audio_coding/neteq/audio_decoder_impl.h"
henrik.lundin8f8c96d2016-04-28 23:19:20 -070021#include "webrtc/modules/audio_coding/neteq/tick_timer.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000022#include "webrtc/typedefs.h"
23
24namespace webrtc {
25
26// Forward declaration.
27class DelayPeakDetector;
28
29class DelayManager {
30 public:
31 typedef std::vector<int> IATVector;
32
33 // Create a DelayManager object. Notify the delay manager that the packet
34 // buffer can hold no more than |max_packets_in_buffer| packets (i.e., this
35 // is the number of packet slots in the buffer). Supply a PeakDetector
36 // object to the DelayManager.
henrik.lundin8f8c96d2016-04-28 23:19:20 -070037 DelayManager(size_t max_packets_in_buffer,
38 DelayPeakDetector* peak_detector,
39 const TickTimer* tick_timer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000040
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000041 virtual ~DelayManager();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000042
43 // Read the inter-arrival time histogram. Mainly for testing purposes.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000044 virtual const IATVector& iat_vector() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000045
46 // Updates the delay manager with a new incoming packet, with
47 // |sequence_number| and |timestamp| from the RTP header. This updates the
48 // inter-arrival time histogram and other statistics, as well as the
49 // associated DelayPeakDetector. A new target buffer level is calculated.
50 // Returns 0 on success, -1 on failure (invalid sample rate).
51 virtual int Update(uint16_t sequence_number,
52 uint32_t timestamp,
53 int sample_rate_hz);
54
55 // Calculates a new target buffer level. Called from the Update() method.
56 // Sets target_level_ (in Q8) and returns the same value. Also calculates
57 // and updates base_target_level_, which is the target buffer level before
58 // taking delay peaks into account.
59 virtual int CalculateTargetLevel(int iat_packets);
60
61 // Notifies the DelayManager of how much audio data is carried in each packet.
62 // The method updates the DelayPeakDetector too, and resets the inter-arrival
63 // time counter. Returns 0 on success, -1 on failure.
64 virtual int SetPacketAudioLength(int length_ms);
65
66 // Resets the DelayManager and the associated DelayPeakDetector.
67 virtual void Reset();
68
69 // Calculates the average inter-arrival time deviation from the histogram.
70 // The result is returned as parts-per-million deviation from the nominal
71 // inter-arrival time. That is, if the average inter-arrival time is equal to
72 // the nominal frame time, the return value is zero. A positive value
73 // corresponds to packet spacing being too large, while a negative value means
74 // that the packets arrive with less spacing than expected.
75 virtual int AverageIAT() const;
76
77 // Returns true if peak-mode is active. That is, delay peaks were observed
78 // recently. This method simply asks for the same information from the
79 // DelayPeakDetector object.
80 virtual bool PeakFound() const;
81
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000082 // Reset the inter-arrival time counter to 0.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000083 virtual void ResetPacketIatCount();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000084
85 // Writes the lower and higher limits which the buffer level should stay
86 // within to the corresponding pointers. The values are in (fractions of)
87 // packets in Q8.
88 virtual void BufferLimits(int* lower_limit, int* higher_limit) const;
89
90 // Gets the target buffer level, in (fractions of) packets in Q8. This value
91 // includes any extra delay set through the set_extra_delay_ms() method.
92 virtual int TargetLevel() const;
93
94 virtual void LastDecoderType(NetEqDecoder decoder_type);
95
96 // Accessors and mutators.
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +000097 // Assuming |delay| is in valid range.
98 virtual bool SetMinimumDelay(int delay_ms);
99 virtual bool SetMaximumDelay(int delay_ms);
100 virtual int least_required_delay_ms() const;
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000101 virtual int base_target_level() const;
102 virtual void set_streaming_mode(bool value);
103 virtual int last_pack_cng_or_dtmf() const;
104 virtual void set_last_pack_cng_or_dtmf(int value);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000105
106 private:
107 static const int kLimitProbability = 53687091; // 1/20 in Q30.
108 static const int kLimitProbabilityStreaming = 536871; // 1/2000 in Q30.
109 static const int kMaxStreamingPeakPeriodMs = 600000; // 10 minutes in ms.
110 static const int kCumulativeSumDrift = 2; // Drift term for cumulative sum
111 // |iat_cumulative_sum_|.
112 // Steady-state forgetting factor for |iat_vector_|, 0.9993 in Q15.
113 static const int kIatFactor_ = 32745;
114 static const int kMaxIat = 64; // Max inter-arrival time to register.
115
116 // Sets |iat_vector_| to the default start distribution and sets the
117 // |base_target_level_| and |target_level_| to the corresponding values.
118 void ResetHistogram();
119
120 // Updates |iat_cumulative_sum_| and |max_iat_cumulative_sum_|. (These are
121 // used by the streaming mode.) This method is called by Update().
122 void UpdateCumulativeSums(int packet_len_ms, uint16_t sequence_number);
123
124 // Updates the histogram |iat_vector_|. The probability for inter-arrival time
125 // equal to |iat_packets| (in integer packets) is increased slightly, while
126 // all other entries are decreased. This method is called by Update().
127 void UpdateHistogram(size_t iat_packets);
128
129 // Makes sure that |target_level_| is not too large, taking
130 // |max_packets_in_buffer_| and |extra_delay_ms_| into account. This method is
131 // called by Update().
132 void LimitTargetLevel();
133
134 bool first_packet_received_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700135 const size_t max_packets_in_buffer_; // Capacity of the packet buffer.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000136 IATVector iat_vector_; // Histogram of inter-arrival times.
137 int iat_factor_; // Forgetting factor for updating the IAT histogram (Q15).
henrik.lundin8f8c96d2016-04-28 23:19:20 -0700138 const TickTimer* tick_timer_;
139 // Time elapsed since last packet.
140 std::unique_ptr<TickTimer::Stopwatch> packet_iat_stopwatch_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000141 int base_target_level_; // Currently preferred buffer level before peak
142 // detection and streaming mode (Q0).
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000143 // TODO(turajs) change the comment according to the implementation of
144 // minimum-delay.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000145 int target_level_; // Currently preferred buffer level in (fractions)
146 // of packets (Q8), before adding any extra delay.
147 int packet_len_ms_; // Length of audio in each incoming packet [ms].
148 bool streaming_mode_;
149 uint16_t last_seq_no_; // Sequence number for last received packet.
150 uint32_t last_timestamp_; // Timestamp for the last received packet.
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000151 int minimum_delay_ms_; // Externally set minimum delay.
152 int least_required_delay_ms_; // Smallest preferred buffer level (same unit
153 // as |target_level_|), before applying
154 // |minimum_delay_ms_| and/or |maximum_delay_ms_|.
155 int maximum_delay_ms_; // Externally set maximum allowed delay.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000156 int iat_cumulative_sum_; // Cumulative sum of delta inter-arrival times.
157 int max_iat_cumulative_sum_; // Max of |iat_cumulative_sum_|.
henrik.lundin8f8c96d2016-04-28 23:19:20 -0700158 // Time elapsed since maximum was observed.
159 std::unique_ptr<TickTimer::Stopwatch> max_iat_stopwatch_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000160 DelayPeakDetector& peak_detector_;
161 int last_pack_cng_or_dtmf_;
162
henrikg3c089d72015-09-16 05:37:44 -0700163 RTC_DISALLOW_COPY_AND_ASSIGN(DelayManager);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000164};
165
166} // namespace webrtc
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +0000167#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_