blob: 63ed43d8fa8d5751a5be7a28b77c5df11f059c6f [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>
kwiberg16c5a962016-02-15 02:27:22 -080015#include <memory>
henrik.lundin4cf61dd2015-12-09 06:20:58 -080016#include <string>
turaj@webrtc.org7959e162013-09-12 18:30:26 +000017#include <vector>
18
kwibergee2bac22015-11-11 10:34:00 -080019#include "webrtc/base/array_view.h"
Tommi9090e0b2016-01-20 13:39:36 +010020#include "webrtc/base/criticalsection.h"
henrik.lundin057fb892015-11-23 08:19:52 -080021#include "webrtc/base/optional.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000022#include "webrtc/base/thread_annotations.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000023#include "webrtc/common_audio/vad/include/webrtc_vad.h"
kjellander3e6db232015-11-26 04:44:54 -080024#include "webrtc/modules/audio_coding/acm2/acm_resampler.h"
25#include "webrtc/modules/audio_coding/acm2/call_statistics.h"
Tommi9090e0b2016-01-20 13:39:36 +010026#include "webrtc/modules/audio_coding/include/audio_coding_module.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 NetEq;
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +000035
36namespace acm2 {
37
turaj@webrtc.org7959e162013-09-12 18:30:26 +000038class AcmReceiver {
39 public:
turaj@webrtc.org7959e162013-09-12 18:30:26 +000040 // Constructor of the class
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +000041 explicit AcmReceiver(const AudioCodingModule::Config& config);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000042
43 // Destructor of the class.
44 ~AcmReceiver();
45
46 //
47 // Inserts a payload with its associated RTP-header into NetEq.
48 //
49 // Input:
50 // - rtp_header : RTP header for the incoming payload containing
51 // information about payload type, sequence number,
52 // timestamp, SSRC and marker bit.
53 // - incoming_payload : Incoming audio payload.
54 // - length_payload : Length of incoming audio payload in bytes.
55 //
56 // Return value : 0 if OK.
57 // <0 if NetEq returned an error.
58 //
59 int InsertPacket(const WebRtcRTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -080060 rtc::ArrayView<const uint8_t> incoming_payload);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000061
62 //
63 // Asks NetEq for 10 milliseconds of decoded audio.
64 //
65 // Input:
66 // -desired_freq_hz : specifies the sampling rate [Hz] of the output
67 // audio. If set -1 indicates to resampling is
68 // is required and the audio returned at the
69 // sampling rate of the decoder.
70 //
71 // Output:
72 // -audio_frame : an audio frame were output data and
73 // associated parameters are written to.
henrik.lundin834a6ea2016-05-13 03:45:24 -070074 // -muted : if true, the sample data in audio_frame is not
75 // populated, and must be interpreted as all zero.
turaj@webrtc.org7959e162013-09-12 18:30:26 +000076 //
77 // Return value : 0 if OK.
78 // -1 if NetEq returned an error.
79 //
henrik.lundin834a6ea2016-05-13 03:45:24 -070080 int GetAudio(int desired_freq_hz, AudioFrame* audio_frame, bool* muted);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000081
82 //
83 // Adds a new codec to the NetEq codec database.
84 //
85 // Input:
kwiberg4e14f092015-08-24 05:27:22 -070086 // - acm_codec_id : ACM codec ID; -1 means external decoder.
turaj@webrtc.org7959e162013-09-12 18:30:26 +000087 // - payload_type : payload type.
Karl Wibergd8399e62015-05-25 14:39:56 +020088 // - sample_rate_hz : sample rate.
kwiberg4e14f092015-08-24 05:27:22 -070089 // - audio_decoder : pointer to a decoder object. If it's null, then
90 // NetEq will internally create a decoder object
91 // based on the value of |acm_codec_id| (which
92 // mustn't be -1). Otherwise, NetEq will use the
93 // given decoder for the given payload type. NetEq
94 // won't take ownership of the decoder; it's up to
95 // the caller to delete it when it's no longer
96 // needed.
97 //
98 // Providing an existing decoder object here is
99 // necessary for external decoders, but may also be
100 // used for built-in decoders if NetEq doesn't have
101 // all the info it needs to construct them properly
102 // (e.g. iSAC, where the decoder needs to be paired
103 // with an encoder).
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000104 //
105 // Return value : 0 if OK.
106 // <0 if NetEq returned an error.
107 //
108 int AddCodec(int acm_codec_id,
109 uint8_t payload_type,
Peter Kasting69558702016-01-12 16:26:35 -0800110 size_t channels,
Karl Wibergd8399e62015-05-25 14:39:56 +0200111 int sample_rate_hz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800112 AudioDecoder* audio_decoder,
113 const std::string& name);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000114
kwiberg5adaf732016-10-04 09:33:27 -0700115 // Adds a new decoder to the NetEq codec database. Returns true iff
116 // successful.
117 bool AddCodec(int rtp_payload_type, const SdpAudioFormat& audio_format);
118
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000119 //
120 // Sets a minimum delay for packet buffer. The given delay is maintained,
121 // unless channel condition dictates a higher delay.
122 //
123 // Input:
124 // - delay_ms : minimum delay in milliseconds.
125 //
126 // Return value : 0 if OK.
127 // <0 if NetEq returned an error.
128 //
129 int SetMinimumDelay(int delay_ms);
130
131 //
132 // Sets a maximum delay [ms] for the packet buffer. The target delay does not
133 // exceed the given value, even if channel condition requires so.
134 //
135 // Input:
136 // - delay_ms : maximum delay in milliseconds.
137 //
138 // Return value : 0 if OK.
139 // <0 if NetEq returned an error.
140 //
141 int SetMaximumDelay(int delay_ms);
142
143 //
144 // Get least required delay computed based on channel conditions. Note that
145 // this is before applying any user-defined limits (specified by calling
146 // (SetMinimumDelay() and/or SetMaximumDelay()).
147 //
148 int LeastRequiredDelayMs() const;
149
150 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000151 // Resets the initial delay to zero.
152 //
153 void ResetInitialDelay();
154
henrik.lundin057fb892015-11-23 08:19:52 -0800155 // Returns the sample rate of the decoder associated with the last incoming
156 // packet. If no packet of a registered non-CNG codec has been received, the
157 // return value is empty. Also, if the decoder was unregistered since the last
158 // packet was inserted, the return value is empty.
159 rtc::Optional<int> last_packet_sample_rate_hz() const;
160
henrik.lundind89814b2015-11-23 06:49:25 -0800161 // Returns last_output_sample_rate_hz from the NetEq instance.
162 int last_output_sample_rate_hz() const;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000163
164 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000165 // Get the current network statistics from NetEq.
166 //
167 // Output:
168 // - statistics : The current network statistics.
169 //
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000170 void GetNetworkStatistics(NetworkStatistics* statistics);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000171
172 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000173 // Flushes the NetEq packet and speech buffers.
174 //
175 void FlushBuffers();
176
177 //
178 // Removes a payload-type from the NetEq codec database.
179 //
180 // Input:
181 // - payload_type : the payload-type to be removed.
182 //
183 // Return value : 0 if OK.
184 // -1 if an error occurred.
185 //
186 int RemoveCodec(uint8_t payload_type);
187
188 //
189 // Remove all registered codecs.
190 //
kwiberg6b19b562016-09-20 04:02:25 -0700191 void RemoveAllCodecs();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000192
henrik.lundin9a410dd2016-04-06 01:39:22 -0700193 // Returns the RTP timestamp for the last sample delivered by GetAudio().
194 // The return value will be empty if no valid timestamp is available.
195 rtc::Optional<uint32_t> GetPlayoutTimestamp();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000196
henrik.lundinb3f1c5d2016-08-22 15:39:53 -0700197 // Returns the current total delay from NetEq (packet buffer and sync buffer)
198 // in ms, with smoothing applied to even out short-time fluctuations due to
199 // jitter. The packet buffer part of the delay is not updated during DTX/CNG
200 // periods.
201 //
202 int FilteredCurrentDelayMs() const;
203
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000204 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000205 // Get the audio codec associated with the last non-CNG/non-DTMF received
206 // payload. If no non-CNG/non-DTMF packet is received -1 is returned,
207 // otherwise return 0.
208 //
209 int LastAudioCodec(CodecInst* codec) const;
210
ossue280cde2016-10-12 11:04:10 -0700211 rtc::Optional<SdpAudioFormat> LastAudioFormat() const;
212
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000213 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000214 // Get a decoder given its registered payload-type.
215 //
216 // Input:
217 // -payload_type : the payload-type of the codec to be retrieved.
218 //
219 // Output:
220 // -codec : codec associated with the given payload-type.
221 //
222 // Return value : 0 if succeeded.
223 // -1 if failed, e.g. given payload-type is not
224 // registered.
225 //
226 int DecoderByPayloadType(uint8_t payload_type,
227 CodecInst* codec) const;
228
229 //
230 // Enable NACK and set the maximum size of the NACK list. If NACK is already
231 // enabled then the maximum NACK list size is modified accordingly.
232 //
233 // Input:
234 // -max_nack_list_size : maximum NACK list size
235 // should be positive (none zero) and less than or
236 // equal to |Nack::kNackListSizeLimit|
237 // Return value
238 // : 0 if succeeded.
239 // -1 if failed
240 //
241 int EnableNack(size_t max_nack_list_size);
242
243 // Disable NACK.
244 void DisableNack();
245
246 //
247 // Get a list of packets to be retransmitted.
248 //
249 // Input:
250 // -round_trip_time_ms : estimate of the round-trip-time (in milliseconds).
251 // Return value : list of packets to be retransmitted.
252 //
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000253 std::vector<uint16_t> GetNackList(int64_t round_trip_time_ms) const;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000254
255 //
wu@webrtc.org24301a62013-12-13 19:17:43 +0000256 // Get statistics of calls to GetAudio().
257 void GetDecodingCallStatistics(AudioDecodingCallStats* stats) const;
258
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000259 private:
kwiberg6f0f6162016-09-20 03:07:46 -0700260 struct Decoder {
261 int acm_codec_id;
262 uint8_t payload_type;
263 // This field is meaningful for codecs where both mono and
264 // stereo versions are registered under the same ID.
265 size_t channels;
266 int sample_rate_hz;
267 };
268
269 const rtc::Optional<CodecInst> RtpHeaderToDecoder(
270 const RTPHeader& rtp_header,
271 uint8_t first_payload_byte) const EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000272
273 uint32_t NowInTimestamp(int decoder_sampling_rate) const;
274
pbos5ad935c2016-01-25 03:52:44 -0800275 rtc::CriticalSection crit_sect_;
kwiberg6f0f6162016-09-20 03:07:46 -0700276 rtc::Optional<CodecInst> last_audio_decoder_ GUARDED_BY(crit_sect_);
ossue280cde2016-10-12 11:04:10 -0700277 rtc::Optional<SdpAudioFormat> last_audio_format_ GUARDED_BY(crit_sect_);
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000278 ACMResampler resampler_ GUARDED_BY(crit_sect_);
kwiberg16c5a962016-02-15 02:27:22 -0800279 std::unique_ptr<int16_t[]> last_audio_buffer_ GUARDED_BY(crit_sect_);
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000280 CallStatistics call_stats_ GUARDED_BY(crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000281 NetEq* neteq_;
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000282 Clock* clock_; // TODO(henrik.lundin) Make const if possible.
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000283 bool resampled_last_output_frame_ GUARDED_BY(crit_sect_);
henrik.lundin057fb892015-11-23 08:19:52 -0800284 rtc::Optional<int> last_packet_sample_rate_hz_ GUARDED_BY(crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000285};
286
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +0000287} // namespace acm2
288
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000289} // namespace webrtc
290
kjellander3e6db232015-11-26 04:44:54 -0800291#endif // WEBRTC_MODULES_AUDIO_CODING_ACM2_ACM_RECEIVER_H_