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