blob: 7fa389741fe69703a4172531148333d8060efc6b [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
36 virtual ~DelayManager() {}
37
38 // Read the inter-arrival time histogram. Mainly for testing purposes.
39 virtual const IATVector& iat_vector() const { return iat_vector_; }
40
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.
82 virtual void ResetPacketIatCount() { packet_iat_count_ms_ = 0; }
83
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.
96 virtual void set_extra_delay_ms(int16_t delay) { extra_delay_ms_ = delay; }
97 virtual int base_target_level() const { return base_target_level_; }
98 virtual void set_streaming_mode(bool value) { streaming_mode_ = value; }
99 virtual int last_pack_cng_or_dtmf() const { return last_pack_cng_or_dtmf_; }
100 virtual void set_last_pack_cng_or_dtmf(int value) {
101 last_pack_cng_or_dtmf_ = value;
102 }
103
104 private:
105 static const int kLimitProbability = 53687091; // 1/20 in Q30.
106 static const int kLimitProbabilityStreaming = 536871; // 1/2000 in Q30.
107 static const int kMaxStreamingPeakPeriodMs = 600000; // 10 minutes in ms.
108 static const int kCumulativeSumDrift = 2; // Drift term for cumulative sum
109 // |iat_cumulative_sum_|.
110 // Steady-state forgetting factor for |iat_vector_|, 0.9993 in Q15.
111 static const int kIatFactor_ = 32745;
112 static const int kMaxIat = 64; // Max inter-arrival time to register.
113
114 // Sets |iat_vector_| to the default start distribution and sets the
115 // |base_target_level_| and |target_level_| to the corresponding values.
116 void ResetHistogram();
117
118 // Updates |iat_cumulative_sum_| and |max_iat_cumulative_sum_|. (These are
119 // used by the streaming mode.) This method is called by Update().
120 void UpdateCumulativeSums(int packet_len_ms, uint16_t sequence_number);
121
122 // Updates the histogram |iat_vector_|. The probability for inter-arrival time
123 // equal to |iat_packets| (in integer packets) is increased slightly, while
124 // all other entries are decreased. This method is called by Update().
125 void UpdateHistogram(size_t iat_packets);
126
127 // Makes sure that |target_level_| is not too large, taking
128 // |max_packets_in_buffer_| and |extra_delay_ms_| into account. This method is
129 // called by Update().
130 void LimitTargetLevel();
131
132 bool first_packet_received_;
133 const int max_packets_in_buffer_; // Capacity of the packet buffer.
134 IATVector iat_vector_; // Histogram of inter-arrival times.
135 int iat_factor_; // Forgetting factor for updating the IAT histogram (Q15).
136 int packet_iat_count_ms_; // Milliseconds elapsed since last packet.
137 int base_target_level_; // Currently preferred buffer level before peak
138 // detection and streaming mode (Q0).
139 int target_level_; // Currently preferred buffer level in (fractions)
140 // of packets (Q8), before adding any extra delay.
141 int packet_len_ms_; // Length of audio in each incoming packet [ms].
142 bool streaming_mode_;
143 uint16_t last_seq_no_; // Sequence number for last received packet.
144 uint32_t last_timestamp_; // Timestamp for the last received packet.
145 int extra_delay_ms_; // Externally set extra delay.
146 int iat_cumulative_sum_; // Cumulative sum of delta inter-arrival times.
147 int max_iat_cumulative_sum_; // Max of |iat_cumulative_sum_|.
148 int max_timer_ms_; // Time elapsed since maximum was observed.
149 DelayPeakDetector& peak_detector_;
150 int last_pack_cng_or_dtmf_;
151
152 DISALLOW_COPY_AND_ASSIGN(DelayManager);
153};
154
155} // namespace webrtc
156#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ4_DELAY_MANAGER_H_