blob: 9d385e7260eacdbb7b91d155137e55bbd94b7485 [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
11#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ4_DELAY_MANAGER_H_
12#define WEBRTC_MODULES_AUDIO_CODING_NETEQ4_DELAY_MANAGER_H_
13
14#include <cstring> // Provide access to size_t.
15#include <vector>
16
17#include "webrtc/modules/audio_coding/neteq4/interface/audio_decoder.h"
18#include "webrtc/system_wrappers/interface/constructor_magic.h"
19#include "webrtc/typedefs.h"
20
21namespace webrtc {
22
23// Forward declaration.
24class DelayPeakDetector;
25
26class DelayManager {
27 public:
28 typedef std::vector<int> IATVector;
29
30 // Create a DelayManager object. Notify the delay manager that the packet
31 // buffer can hold no more than |max_packets_in_buffer| packets (i.e., this
32 // is the number of packet slots in the buffer). Supply a PeakDetector
33 // object to the DelayManager.
34 DelayManager(int max_packets_in_buffer, DelayPeakDetector* peak_detector);
35
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000036 virtual ~DelayManager();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000037
38 // Read the inter-arrival time histogram. Mainly for testing purposes.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000039 virtual const IATVector& iat_vector() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000040
41 // Updates the delay manager with a new incoming packet, with
42 // |sequence_number| and |timestamp| from the RTP header. This updates the
43 // inter-arrival time histogram and other statistics, as well as the
44 // associated DelayPeakDetector. A new target buffer level is calculated.
45 // Returns 0 on success, -1 on failure (invalid sample rate).
46 virtual int Update(uint16_t sequence_number,
47 uint32_t timestamp,
48 int sample_rate_hz);
49
50 // Calculates a new target buffer level. Called from the Update() method.
51 // Sets target_level_ (in Q8) and returns the same value. Also calculates
52 // and updates base_target_level_, which is the target buffer level before
53 // taking delay peaks into account.
54 virtual int CalculateTargetLevel(int iat_packets);
55
56 // Notifies the DelayManager of how much audio data is carried in each packet.
57 // The method updates the DelayPeakDetector too, and resets the inter-arrival
58 // time counter. Returns 0 on success, -1 on failure.
59 virtual int SetPacketAudioLength(int length_ms);
60
61 // Resets the DelayManager and the associated DelayPeakDetector.
62 virtual void Reset();
63
64 // Calculates the average inter-arrival time deviation from the histogram.
65 // The result is returned as parts-per-million deviation from the nominal
66 // inter-arrival time. That is, if the average inter-arrival time is equal to
67 // the nominal frame time, the return value is zero. A positive value
68 // corresponds to packet spacing being too large, while a negative value means
69 // that the packets arrive with less spacing than expected.
70 virtual int AverageIAT() const;
71
72 // Returns true if peak-mode is active. That is, delay peaks were observed
73 // recently. This method simply asks for the same information from the
74 // DelayPeakDetector object.
75 virtual bool PeakFound() const;
76
77 // Notifies the counters in DelayManager and DelayPeakDetector that
78 // |elapsed_time_ms| have elapsed.
79 virtual void UpdateCounters(int elapsed_time_ms);
80
81 // Reset the inter-arrival time counter to 0.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000082 virtual void ResetPacketIatCount();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000083
84 // Writes the lower and higher limits which the buffer level should stay
85 // within to the corresponding pointers. The values are in (fractions of)
86 // packets in Q8.
87 virtual void BufferLimits(int* lower_limit, int* higher_limit) const;
88
89 // Gets the target buffer level, in (fractions of) packets in Q8. This value
90 // includes any extra delay set through the set_extra_delay_ms() method.
91 virtual int TargetLevel() const;
92
93 virtual void LastDecoderType(NetEqDecoder decoder_type);
94
95 // Accessors and mutators.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000096 virtual void set_extra_delay_ms(int16_t delay);
97 virtual int base_target_level() const;
98 virtual void set_streaming_mode(bool value);
99 virtual int last_pack_cng_or_dtmf() const;
100 virtual void set_last_pack_cng_or_dtmf(int value);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000101
102 private:
103 static const int kLimitProbability = 53687091; // 1/20 in Q30.
104 static const int kLimitProbabilityStreaming = 536871; // 1/2000 in Q30.
105 static const int kMaxStreamingPeakPeriodMs = 600000; // 10 minutes in ms.
106 static const int kCumulativeSumDrift = 2; // Drift term for cumulative sum
107 // |iat_cumulative_sum_|.
108 // Steady-state forgetting factor for |iat_vector_|, 0.9993 in Q15.
109 static const int kIatFactor_ = 32745;
110 static const int kMaxIat = 64; // Max inter-arrival time to register.
111
112 // Sets |iat_vector_| to the default start distribution and sets the
113 // |base_target_level_| and |target_level_| to the corresponding values.
114 void ResetHistogram();
115
116 // Updates |iat_cumulative_sum_| and |max_iat_cumulative_sum_|. (These are
117 // used by the streaming mode.) This method is called by Update().
118 void UpdateCumulativeSums(int packet_len_ms, uint16_t sequence_number);
119
120 // Updates the histogram |iat_vector_|. The probability for inter-arrival time
121 // equal to |iat_packets| (in integer packets) is increased slightly, while
122 // all other entries are decreased. This method is called by Update().
123 void UpdateHistogram(size_t iat_packets);
124
125 // Makes sure that |target_level_| is not too large, taking
126 // |max_packets_in_buffer_| and |extra_delay_ms_| into account. This method is
127 // called by Update().
128 void LimitTargetLevel();
129
130 bool first_packet_received_;
131 const int max_packets_in_buffer_; // Capacity of the packet buffer.
132 IATVector iat_vector_; // Histogram of inter-arrival times.
133 int iat_factor_; // Forgetting factor for updating the IAT histogram (Q15).
134 int packet_iat_count_ms_; // Milliseconds elapsed since last packet.
135 int base_target_level_; // Currently preferred buffer level before peak
136 // detection and streaming mode (Q0).
137 int target_level_; // Currently preferred buffer level in (fractions)
138 // of packets (Q8), before adding any extra delay.
139 int packet_len_ms_; // Length of audio in each incoming packet [ms].
140 bool streaming_mode_;
141 uint16_t last_seq_no_; // Sequence number for last received packet.
142 uint32_t last_timestamp_; // Timestamp for the last received packet.
143 int extra_delay_ms_; // Externally set extra delay.
144 int iat_cumulative_sum_; // Cumulative sum of delta inter-arrival times.
145 int max_iat_cumulative_sum_; // Max of |iat_cumulative_sum_|.
146 int max_timer_ms_; // Time elapsed since maximum was observed.
147 DelayPeakDetector& peak_detector_;
148 int last_pack_cng_or_dtmf_;
149
150 DISALLOW_COPY_AND_ASSIGN(DelayManager);
151};
152
153} // namespace webrtc
154#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ4_DELAY_MANAGER_H_