blob: 00e39af6a56459e8969718fb7991b0556f389c81 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_
12#define 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_coding/neteq/tick_timer.h"
20#include "rtc_base/constructormagic.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000021
22namespace webrtc {
23
24// Forward declaration.
25class DelayPeakDetector;
26
27class DelayManager {
28 public:
29 typedef std::vector<int> IATVector;
30
31 // Create a DelayManager object. Notify the delay manager that the packet
32 // buffer can hold no more than |max_packets_in_buffer| packets (i.e., this
33 // is the number of packet slots in the buffer). Supply a PeakDetector
34 // object to the DelayManager.
henrik.lundin8f8c96d2016-04-28 23:19:20 -070035 DelayManager(size_t max_packets_in_buffer,
36 DelayPeakDetector* peak_detector,
37 const TickTimer* tick_timer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000038
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000039 virtual ~DelayManager();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000040
41 // Read the inter-arrival time histogram. Mainly for testing purposes.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000042 virtual const IATVector& iat_vector() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043
44 // Updates the delay manager with a new incoming packet, with
45 // |sequence_number| and |timestamp| from the RTP header. This updates the
46 // inter-arrival time histogram and other statistics, as well as the
47 // associated DelayPeakDetector. A new target buffer level is calculated.
48 // Returns 0 on success, -1 on failure (invalid sample rate).
49 virtual int Update(uint16_t sequence_number,
50 uint32_t timestamp,
51 int sample_rate_hz);
52
53 // Calculates a new target buffer level. Called from the Update() method.
54 // Sets target_level_ (in Q8) and returns the same value. Also calculates
55 // and updates base_target_level_, which is the target buffer level before
56 // taking delay peaks into account.
57 virtual int CalculateTargetLevel(int iat_packets);
58
59 // Notifies the DelayManager of how much audio data is carried in each packet.
60 // The method updates the DelayPeakDetector too, and resets the inter-arrival
61 // time counter. Returns 0 on success, -1 on failure.
62 virtual int SetPacketAudioLength(int length_ms);
63
64 // Resets the DelayManager and the associated DelayPeakDetector.
65 virtual void Reset();
66
67 // Calculates the average inter-arrival time deviation from the histogram.
68 // The result is returned as parts-per-million deviation from the nominal
69 // inter-arrival time. That is, if the average inter-arrival time is equal to
70 // the nominal frame time, the return value is zero. A positive value
71 // corresponds to packet spacing being too large, while a negative value means
72 // that the packets arrive with less spacing than expected.
henrik.lundin0d838572016-10-13 03:35:55 -070073 virtual double EstimatedClockDriftPpm() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000074
75 // Returns true if peak-mode is active. That is, delay peaks were observed
76 // recently. This method simply asks for the same information from the
77 // DelayPeakDetector object.
78 virtual bool PeakFound() const;
79
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080 // Reset the inter-arrival time counter to 0.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000081 virtual void ResetPacketIatCount();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000082
83 // Writes the lower and higher limits which the buffer level should stay
84 // within to the corresponding pointers. The values are in (fractions of)
85 // packets in Q8.
86 virtual void BufferLimits(int* lower_limit, int* higher_limit) const;
87
88 // Gets the target buffer level, in (fractions of) packets in Q8. This value
89 // includes any extra delay set through the set_extra_delay_ms() method.
90 virtual int TargetLevel() const;
91
ossuf1b08da2016-09-23 02:19:43 -070092 // Informs the delay manager whether or not the last decoded packet contained
93 // speech.
94 virtual void LastDecodedWasCngOrDtmf(bool it_was);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000095
henrik.lundinb8c55b12017-05-10 07:38:01 -070096 // Notify the delay manager that empty packets have been received. These are
97 // packets that are part of the sequence number series, so that an empty
98 // packet will shift the sequence numbers for the following packets.
99 virtual void RegisterEmptyPacket();
100
Ivo Creusen385b10b2017-10-13 12:37:27 +0200101 // Apply compression or stretching to the IAT histogram, for a change in frame
102 // size. This returns an updated histogram. This function is public for
103 // testability.
104 static IATVector ScaleHistogram(const IATVector& histogram,
105 int old_packet_length,
106 int new_packet_length);
107
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000108 // Accessors and mutators.
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000109 // Assuming |delay| is in valid range.
110 virtual bool SetMinimumDelay(int delay_ms);
111 virtual bool SetMaximumDelay(int delay_ms);
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000112 virtual int base_target_level() const;
113 virtual void set_streaming_mode(bool value);
114 virtual int last_pack_cng_or_dtmf() const;
115 virtual void set_last_pack_cng_or_dtmf(int value);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000116
117 private:
Yves Gerey665174f2018-06-19 15:03:05 +0200118 static const int kLimitProbability = 53687091; // 1/20 in Q30.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000119 static const int kLimitProbabilityStreaming = 536871; // 1/2000 in Q30.
Yves Gerey665174f2018-06-19 15:03:05 +0200120 static const int kMaxStreamingPeakPeriodMs = 600000; // 10 minutes in ms.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000121 static const int kCumulativeSumDrift = 2; // Drift term for cumulative sum
122 // |iat_cumulative_sum_|.
123 // Steady-state forgetting factor for |iat_vector_|, 0.9993 in Q15.
124 static const int kIatFactor_ = 32745;
125 static const int kMaxIat = 64; // Max inter-arrival time to register.
126
127 // Sets |iat_vector_| to the default start distribution and sets the
128 // |base_target_level_| and |target_level_| to the corresponding values.
129 void ResetHistogram();
130
131 // Updates |iat_cumulative_sum_| and |max_iat_cumulative_sum_|. (These are
132 // used by the streaming mode.) This method is called by Update().
133 void UpdateCumulativeSums(int packet_len_ms, uint16_t sequence_number);
134
135 // Updates the histogram |iat_vector_|. The probability for inter-arrival time
136 // equal to |iat_packets| (in integer packets) is increased slightly, while
137 // all other entries are decreased. This method is called by Update().
138 void UpdateHistogram(size_t iat_packets);
139
140 // Makes sure that |target_level_| is not too large, taking
141 // |max_packets_in_buffer_| and |extra_delay_ms_| into account. This method is
142 // called by Update().
143 void LimitTargetLevel();
144
145 bool first_packet_received_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700146 const size_t max_packets_in_buffer_; // Capacity of the packet buffer.
Yves Gerey665174f2018-06-19 15:03:05 +0200147 IATVector iat_vector_; // Histogram of inter-arrival times.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000148 int iat_factor_; // Forgetting factor for updating the IAT histogram (Q15).
henrik.lundin8f8c96d2016-04-28 23:19:20 -0700149 const TickTimer* tick_timer_;
150 // Time elapsed since last packet.
151 std::unique_ptr<TickTimer::Stopwatch> packet_iat_stopwatch_;
Yves Gerey665174f2018-06-19 15:03:05 +0200152 int base_target_level_; // Currently preferred buffer level before peak
153 // detection and streaming mode (Q0).
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000154 // TODO(turajs) change the comment according to the implementation of
155 // minimum-delay.
Yves Gerey665174f2018-06-19 15:03:05 +0200156 int target_level_; // Currently preferred buffer level in (fractions)
157 // of packets (Q8), before adding any extra delay.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000158 int packet_len_ms_; // Length of audio in each incoming packet [ms].
159 bool streaming_mode_;
Yves Gerey665174f2018-06-19 15:03:05 +0200160 uint16_t last_seq_no_; // Sequence number for last received packet.
161 uint32_t last_timestamp_; // Timestamp for the last received packet.
162 int minimum_delay_ms_; // Externally set minimum delay.
Yves Gerey665174f2018-06-19 15:03:05 +0200163 int maximum_delay_ms_; // Externally set maximum allowed delay.
164 int iat_cumulative_sum_; // Cumulative sum of delta inter-arrival times.
165 int max_iat_cumulative_sum_; // Max of |iat_cumulative_sum_|.
henrik.lundin8f8c96d2016-04-28 23:19:20 -0700166 // Time elapsed since maximum was observed.
167 std::unique_ptr<TickTimer::Stopwatch> max_iat_stopwatch_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000168 DelayPeakDetector& peak_detector_;
169 int last_pack_cng_or_dtmf_;
Ivo Creusen385b10b2017-10-13 12:37:27 +0200170 const bool frame_length_change_experiment_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000171
henrikg3c089d72015-09-16 05:37:44 -0700172 RTC_DISALLOW_COPY_AND_ASSIGN(DelayManager);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000173};
174
175} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200176#endif // MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_