blob: d5a644d5c82341be4557aac073ffa5df849fd452 [file] [log] [blame]
turaj@webrtc.org7959e162013-09-12 18:30:26 +00001/*
2 * Copyright (c) 2013 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
kjellander3e6db232015-11-26 04:44:54 -080011#ifndef WEBRTC_MODULES_AUDIO_CODING_ACM2_ACM_RECEIVER_H_
12#define WEBRTC_MODULES_AUDIO_CODING_ACM2_ACM_RECEIVER_H_
turaj@webrtc.org7959e162013-09-12 18:30:26 +000013
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +000014#include <map>
turaj@webrtc.org7959e162013-09-12 18:30:26 +000015#include <vector>
16
kwibergee2bac22015-11-11 10:34:00 -080017#include "webrtc/base/array_view.h"
henrik.lundin057fb892015-11-23 08:19:52 -080018#include "webrtc/base/optional.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000019#include "webrtc/base/scoped_ptr.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000020#include "webrtc/base/thread_annotations.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000021#include "webrtc/common_audio/vad/include/webrtc_vad.h"
22#include "webrtc/engine_configurations.h"
kjellander3e6db232015-11-26 04:44:54 -080023#include "webrtc/modules/audio_coding/include/audio_coding_module.h"
24#include "webrtc/modules/audio_coding/acm2/acm_resampler.h"
25#include "webrtc/modules/audio_coding/acm2/call_statistics.h"
26#include "webrtc/modules/audio_coding/acm2/initial_delay_manager.h"
Henrik Kjellander74640892015-10-29 11:31:02 +010027#include "webrtc/modules/audio_coding/neteq/include/neteq.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010028#include "webrtc/modules/include/module_common_types.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000029#include "webrtc/typedefs.h"
30
31namespace webrtc {
32
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +000033struct CodecInst;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000034class CriticalSectionWrapper;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000035class NetEq;
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +000036
37namespace acm2 {
38
turaj@webrtc.org7959e162013-09-12 18:30:26 +000039class AcmReceiver {
40 public:
41 struct Decoder {
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +000042 int acm_codec_id;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000043 uint8_t payload_type;
44 // This field is meaningful for codecs where both mono and
45 // stereo versions are registered under the same ID.
46 int channels;
Karl Wibergd8399e62015-05-25 14:39:56 +020047 int sample_rate_hz;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000048 };
49
50 // Constructor of the class
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +000051 explicit AcmReceiver(const AudioCodingModule::Config& config);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000052
53 // Destructor of the class.
54 ~AcmReceiver();
55
56 //
57 // Inserts a payload with its associated RTP-header into NetEq.
58 //
59 // Input:
60 // - rtp_header : RTP header for the incoming payload containing
61 // information about payload type, sequence number,
62 // timestamp, SSRC and marker bit.
63 // - incoming_payload : Incoming audio payload.
64 // - length_payload : Length of incoming audio payload in bytes.
65 //
66 // Return value : 0 if OK.
67 // <0 if NetEq returned an error.
68 //
69 int InsertPacket(const WebRtcRTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -080070 rtc::ArrayView<const uint8_t> incoming_payload);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000071
72 //
73 // Asks NetEq for 10 milliseconds of decoded audio.
74 //
75 // Input:
76 // -desired_freq_hz : specifies the sampling rate [Hz] of the output
77 // audio. If set -1 indicates to resampling is
78 // is required and the audio returned at the
79 // sampling rate of the decoder.
80 //
81 // Output:
82 // -audio_frame : an audio frame were output data and
83 // associated parameters are written to.
84 //
85 // Return value : 0 if OK.
86 // -1 if NetEq returned an error.
87 //
88 int GetAudio(int desired_freq_hz, AudioFrame* audio_frame);
89
90 //
91 // Adds a new codec to the NetEq codec database.
92 //
93 // Input:
kwiberg4e14f092015-08-24 05:27:22 -070094 // - acm_codec_id : ACM codec ID; -1 means external decoder.
turaj@webrtc.org7959e162013-09-12 18:30:26 +000095 // - payload_type : payload type.
Karl Wibergd8399e62015-05-25 14:39:56 +020096 // - sample_rate_hz : sample rate.
kwiberg4e14f092015-08-24 05:27:22 -070097 // - audio_decoder : pointer to a decoder object. If it's null, then
98 // NetEq will internally create a decoder object
99 // based on the value of |acm_codec_id| (which
100 // mustn't be -1). Otherwise, NetEq will use the
101 // given decoder for the given payload type. NetEq
102 // won't take ownership of the decoder; it's up to
103 // the caller to delete it when it's no longer
104 // needed.
105 //
106 // Providing an existing decoder object here is
107 // necessary for external decoders, but may also be
108 // used for built-in decoders if NetEq doesn't have
109 // all the info it needs to construct them properly
110 // (e.g. iSAC, where the decoder needs to be paired
111 // with an encoder).
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000112 //
113 // Return value : 0 if OK.
114 // <0 if NetEq returned an error.
115 //
116 int AddCodec(int acm_codec_id,
117 uint8_t payload_type,
118 int channels,
Karl Wibergd8399e62015-05-25 14:39:56 +0200119 int sample_rate_hz,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000120 AudioDecoder* audio_decoder);
121
122 //
123 // Sets a minimum delay for packet buffer. The given delay is maintained,
124 // unless channel condition dictates a higher delay.
125 //
126 // Input:
127 // - delay_ms : minimum delay in milliseconds.
128 //
129 // Return value : 0 if OK.
130 // <0 if NetEq returned an error.
131 //
132 int SetMinimumDelay(int delay_ms);
133
134 //
135 // Sets a maximum delay [ms] for the packet buffer. The target delay does not
136 // exceed the given value, even if channel condition requires so.
137 //
138 // Input:
139 // - delay_ms : maximum delay in milliseconds.
140 //
141 // Return value : 0 if OK.
142 // <0 if NetEq returned an error.
143 //
144 int SetMaximumDelay(int delay_ms);
145
146 //
147 // Get least required delay computed based on channel conditions. Note that
148 // this is before applying any user-defined limits (specified by calling
149 // (SetMinimumDelay() and/or SetMaximumDelay()).
150 //
151 int LeastRequiredDelayMs() const;
152
153 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000154 // Resets the initial delay to zero.
155 //
156 void ResetInitialDelay();
157
henrik.lundin057fb892015-11-23 08:19:52 -0800158 // Returns the sample rate of the decoder associated with the last incoming
159 // packet. If no packet of a registered non-CNG codec has been received, the
160 // return value is empty. Also, if the decoder was unregistered since the last
161 // packet was inserted, the return value is empty.
162 rtc::Optional<int> last_packet_sample_rate_hz() const;
163
henrik.lundind89814b2015-11-23 06:49:25 -0800164 // Returns last_output_sample_rate_hz from the NetEq instance.
165 int last_output_sample_rate_hz() const;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000166
167 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000168 // Get the current network statistics from NetEq.
169 //
170 // Output:
171 // - statistics : The current network statistics.
172 //
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000173 void GetNetworkStatistics(NetworkStatistics* statistics);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000174
175 //
176 // Enable post-decoding VAD.
177 //
178 void EnableVad();
179
180 //
181 // Disable post-decoding VAD.
182 //
183 void DisableVad();
184
185 //
186 // Returns whether post-decoding VAD is enabled (true) or disabled (false).
187 //
188 bool vad_enabled() const { return vad_enabled_; }
189
190 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000191 // Flushes the NetEq packet and speech buffers.
192 //
193 void FlushBuffers();
194
195 //
196 // Removes a payload-type from the NetEq codec database.
197 //
198 // Input:
199 // - payload_type : the payload-type to be removed.
200 //
201 // Return value : 0 if OK.
202 // -1 if an error occurred.
203 //
204 int RemoveCodec(uint8_t payload_type);
205
206 //
207 // Remove all registered codecs.
208 //
209 int RemoveAllCodecs();
210
211 //
212 // Set ID.
213 //
214 void set_id(int id); // TODO(turajs): can be inline.
215
216 //
wu@webrtc.org94454b72014-06-05 20:34:08 +0000217 // Gets the RTP timestamp of the last sample delivered by GetAudio().
218 // Returns true if the RTP timestamp is valid, otherwise false.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000219 //
wu@webrtc.org94454b72014-06-05 20:34:08 +0000220 bool GetPlayoutTimestamp(uint32_t* timestamp);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000221
222 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000223 // Get the audio codec associated with the last non-CNG/non-DTMF received
224 // payload. If no non-CNG/non-DTMF packet is received -1 is returned,
225 // otherwise return 0.
226 //
227 int LastAudioCodec(CodecInst* codec) const;
228
229 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000230 // Get a decoder given its registered payload-type.
231 //
232 // Input:
233 // -payload_type : the payload-type of the codec to be retrieved.
234 //
235 // Output:
236 // -codec : codec associated with the given payload-type.
237 //
238 // Return value : 0 if succeeded.
239 // -1 if failed, e.g. given payload-type is not
240 // registered.
241 //
242 int DecoderByPayloadType(uint8_t payload_type,
243 CodecInst* codec) const;
244
245 //
246 // Enable NACK and set the maximum size of the NACK list. If NACK is already
247 // enabled then the maximum NACK list size is modified accordingly.
248 //
249 // Input:
250 // -max_nack_list_size : maximum NACK list size
251 // should be positive (none zero) and less than or
252 // equal to |Nack::kNackListSizeLimit|
253 // Return value
254 // : 0 if succeeded.
255 // -1 if failed
256 //
257 int EnableNack(size_t max_nack_list_size);
258
259 // Disable NACK.
260 void DisableNack();
261
262 //
263 // Get a list of packets to be retransmitted.
264 //
265 // Input:
266 // -round_trip_time_ms : estimate of the round-trip-time (in milliseconds).
267 // Return value : list of packets to be retransmitted.
268 //
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000269 std::vector<uint16_t> GetNackList(int64_t round_trip_time_ms) const;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000270
271 //
wu@webrtc.org24301a62013-12-13 19:17:43 +0000272 // Get statistics of calls to GetAudio().
273 void GetDecodingCallStatistics(AudioDecodingCallStats* stats) const;
274
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000275 private:
Jelena Marusica9907842015-03-26 14:01:30 +0100276 const Decoder* RtpHeaderToDecoder(const RTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -0800277 uint8_t payload_type) const
Jelena Marusica9907842015-03-26 14:01:30 +0100278 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000279
280 uint32_t NowInTimestamp(int decoder_sampling_rate) const;
281
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000282 rtc::scoped_ptr<CriticalSectionWrapper> crit_sect_;
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000283 int id_; // TODO(henrik.lundin) Make const.
Jelena Marusica9907842015-03-26 14:01:30 +0100284 const Decoder* last_audio_decoder_ GUARDED_BY(crit_sect_);
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000285 AudioFrame::VADActivity previous_audio_activity_ GUARDED_BY(crit_sect_);
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000286 ACMResampler resampler_ GUARDED_BY(crit_sect_);
287 // Used in GetAudio, declared as member to avoid allocating every 10ms.
288 // TODO(henrik.lundin) Stack-allocate in GetAudio instead?
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000289 rtc::scoped_ptr<int16_t[]> audio_buffer_ GUARDED_BY(crit_sect_);
290 rtc::scoped_ptr<int16_t[]> last_audio_buffer_ GUARDED_BY(crit_sect_);
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000291 CallStatistics call_stats_ GUARDED_BY(crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000292 NetEq* neteq_;
Jelena Marusica9907842015-03-26 14:01:30 +0100293 // Decoders map is keyed by payload type
294 std::map<uint8_t, Decoder> decoders_ GUARDED_BY(crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000295 bool vad_enabled_;
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000296 Clock* clock_; // TODO(henrik.lundin) Make const if possible.
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000297 bool resampled_last_output_frame_ GUARDED_BY(crit_sect_);
henrik.lundin057fb892015-11-23 08:19:52 -0800298 rtc::Optional<int> last_packet_sample_rate_hz_ GUARDED_BY(crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000299};
300
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +0000301} // namespace acm2
302
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000303} // namespace webrtc
304
kjellander3e6db232015-11-26 04:44:54 -0800305#endif // WEBRTC_MODULES_AUDIO_CODING_ACM2_ACM_RECEIVER_H_