blob: 8e0c4d55ed94831b91fe087a1a3f0156a3f8469f [file] [log] [blame]
kjellander3e6db232015-11-26 04:44:54 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_INCLUDE_AUDIO_CODING_MODULE_H_
12#define MODULES_AUDIO_CODING_INCLUDE_AUDIO_CODING_MODULE_H_
kjellander3e6db232015-11-26 04:44:54 -080013
kwiberg84be5112016-04-27 01:19:58 -070014#include <memory>
henrik.lundin4cf61dd2015-12-09 06:20:58 -080015#include <string>
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +010016#include <utility>
kjellander3e6db232015-11-26 04:44:54 -080017#include <vector>
18
Danil Chapovalovb6021232018-06-19 13:26:36 +020019#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/audio_codecs/audio_decoder_factory.h"
21#include "api/audio_codecs/audio_encoder.h"
Artem Titov741daaf2019-03-21 14:37:36 +010022#include "api/function_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
24#include "modules/audio_coding/neteq/include/neteq.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "system_wrappers/include/clock.h"
kjellander3e6db232015-11-26 04:44:54 -080026
27namespace webrtc {
28
29// forward declarations
kjellander3e6db232015-11-26 04:44:54 -080030class AudioDecoder;
31class AudioEncoder;
32class AudioFrame;
33class RTPFragmentationHeader;
Niels Möllerafb5dbb2019-02-15 15:21:47 +010034struct RTPHeader;
kjellander3e6db232015-11-26 04:44:54 -080035
36#define WEBRTC_10MS_PCM_AUDIO 960 // 16 bits super wideband 48 kHz
37
38// Callback class used for sending data ready to be packetized
39class AudioPacketizationCallback {
40 public:
41 virtual ~AudioPacketizationCallback() {}
42
Niels Möller87e2d782019-03-07 10:18:23 +010043 virtual int32_t SendData(AudioFrameType frame_type,
kjellander3e6db232015-11-26 04:44:54 -080044 uint8_t payload_type,
45 uint32_t timestamp,
46 const uint8_t* payload_data,
47 size_t payload_len_bytes,
48 const RTPFragmentationHeader* fragmentation) = 0;
49};
50
51// Callback class used for reporting VAD decision
52class ACMVADCallback {
53 public:
54 virtual ~ACMVADCallback() {}
55
Niels Möller87e2d782019-03-07 10:18:23 +010056 virtual int32_t InFrameType(AudioFrameType frame_type) = 0;
kjellander3e6db232015-11-26 04:44:54 -080057};
58
59class AudioCodingModule {
60 protected:
61 AudioCodingModule() {}
62
63 public:
64 struct Config {
Karl Wiberg5817d3d2018-04-06 10:06:42 +020065 explicit Config(
66 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory = nullptr);
kwiberg36a43882016-08-29 05:33:32 -070067 Config(const Config&);
68 ~Config();
kjellander3e6db232015-11-26 04:44:54 -080069
kjellander3e6db232015-11-26 04:44:54 -080070 NetEq::Config neteq_config;
71 Clock* clock;
ossue3525782016-05-25 07:37:43 -070072 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory;
kjellander3e6db232015-11-26 04:44:54 -080073 };
74
kjellander3e6db232015-11-26 04:44:54 -080075 static AudioCodingModule* Create(const Config& config);
76 virtual ~AudioCodingModule() = default;
77
78 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -080079 // Sender
80 //
81
kwiberg4cdbd572016-03-30 03:10:05 -070082 // |modifier| is called exactly once with one argument: a pointer to the
83 // unique_ptr that holds the current encoder (which is null if there is no
84 // current encoder). For the duration of the call, |modifier| has exclusive
85 // access to the unique_ptr; it may call the encoder, steal the encoder and
86 // replace it with another encoder or with nullptr, etc.
87 virtual void ModifyEncoder(
kwiberg24c7c122016-09-28 11:57:10 -070088 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) = 0;
kwiberg4cdbd572016-03-30 03:10:05 -070089
90 // Utility method for simply replacing the existing encoder with a new one.
91 void SetEncoder(std::unique_ptr<AudioEncoder> new_encoder) {
92 ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
93 *encoder = std::move(new_encoder);
94 });
95 }
96
kjellander3e6db232015-11-26 04:44:54 -080097 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -080098 // Sets the bitrate to the specified value in bits/sec. If the value is not
99 // supported by the codec, it will choose another appropriate value.
minyue7e304322016-10-12 05:00:55 -0700100 //
101 // This is only used in test code that rely on old ACM APIs.
102 // TODO(minyue): Remove it when possible.
kjellander3e6db232015-11-26 04:44:54 -0800103 virtual void SetBitRate(int bitrate_bps) = 0;
104
105 // int32_t RegisterTransportCallback()
106 // Register a transport callback which will be called to deliver
107 // the encoded buffers whenever Process() is called and a
108 // bit-stream is ready.
109 //
110 // Input:
111 // -transport : pointer to the callback class
112 // transport->SendData() is called whenever
113 // Process() is called and bit-stream is ready
114 // to deliver.
115 //
116 // Return value:
117 // -1 if the transport callback could not be registered
118 // 0 if registration is successful.
119 //
120 virtual int32_t RegisterTransportCallback(
121 AudioPacketizationCallback* transport) = 0;
122
123 ///////////////////////////////////////////////////////////////////////////
124 // int32_t Add10MsData()
125 // Add 10MS of raw (PCM) audio data and encode it. If the sampling
126 // frequency of the audio does not match the sampling frequency of the
127 // current encoder ACM will resample the audio. If an encoded packet was
128 // produced, it will be delivered via the callback object registered using
129 // RegisterTransportCallback, and the return value from this function will
130 // be the number of bytes encoded.
131 //
132 // Input:
133 // -audio_frame : the input audio frame, containing raw audio
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +0200134 // sampling frequency etc.
kjellander3e6db232015-11-26 04:44:54 -0800135 //
136 // Return value:
137 // >= 0 number of bytes encoded.
138 // -1 some error occurred.
139 //
140 virtual int32_t Add10MsData(const AudioFrame& audio_frame) = 0;
141
142 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -0800143 // int SetPacketLossRate()
144 // Sets expected packet loss rate for encoding. Some encoders provide packet
145 // loss gnostic encoding to make stream less sensitive to packet losses,
146 // through e.g., FEC. No effects on codecs that do not provide such encoding.
147 //
148 // Input:
149 // -packet_loss_rate : expected packet loss rate (0 -- 100 inclusive).
150 //
151 // Return value
152 // -1 if failed to set packet loss rate,
153 // 0 if succeeded.
154 //
minyue7e304322016-10-12 05:00:55 -0700155 // This is only used in test code that rely on old ACM APIs.
156 // TODO(minyue): Remove it when possible.
kjellander3e6db232015-11-26 04:44:54 -0800157 virtual int SetPacketLossRate(int packet_loss_rate) = 0;
158
159 ///////////////////////////////////////////////////////////////////////////
160 // (VAD) Voice Activity Detection
161 //
162
163 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -0800164 // int32_t RegisterVADCallback()
165 // Call this method to register a callback function which is called
166 // any time that ACM encounters an empty frame. That is a frame which is
167 // recognized inactive. Depending on the codec WebRtc VAD or internal codec
168 // VAD is employed to identify a frame as active/inactive.
169 //
170 // Input:
171 // -vad_callback : pointer to a callback function.
172 //
173 // Return value:
174 // -1 if failed to register the callback function.
175 // 0 if the callback function is registered successfully.
176 //
177 virtual int32_t RegisterVADCallback(ACMVADCallback* vad_callback) = 0;
178
179 ///////////////////////////////////////////////////////////////////////////
180 // Receiver
181 //
182
183 ///////////////////////////////////////////////////////////////////////////
184 // int32_t InitializeReceiver()
185 // Any decoder-related state of ACM will be initialized to the
186 // same state when ACM is created. This will not interrupt or
187 // effect encoding functionality of ACM. ACM would lose all the
188 // decoding-related settings by calling this function.
189 // For instance, all registered codecs are deleted and have to be
190 // registered again.
191 //
192 // Return value:
193 // -1 if failed to initialize,
194 // 0 if succeeded.
195 //
196 virtual int32_t InitializeReceiver() = 0;
197
198 ///////////////////////////////////////////////////////////////////////////
199 // int32_t ReceiveFrequency()
200 // Get sampling frequency of the last received payload.
201 //
202 // Return value:
203 // non-negative the sampling frequency in Hertz.
204 // -1 if an error has occurred.
205 //
206 virtual int32_t ReceiveFrequency() const = 0;
207
208 ///////////////////////////////////////////////////////////////////////////
209 // int32_t PlayoutFrequency()
210 // Get sampling frequency of audio played out.
211 //
212 // Return value:
213 // the sampling frequency in Hertz.
214 //
215 virtual int32_t PlayoutFrequency() const = 0;
216
kwiberg1c07c702017-03-27 07:15:49 -0700217 // Replace any existing decoders with the given payload type -> decoder map.
218 virtual void SetReceiveCodecs(
219 const std::map<int, SdpAudioFormat>& codecs) = 0;
220
kjellander3e6db232015-11-26 04:44:54 -0800221 ///////////////////////////////////////////////////////////////////////////
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100222 // absl::optional<std::pair<int, SdpAudioFormat>> ReceiveCodec()
223 // Get the codec info associated with last received payload.
kjellander3e6db232015-11-26 04:44:54 -0800224 //
225 // Return value:
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100226 // A payload type and SdpAudioFormat describing the format associated with
227 // the last received payload.
ossue280cde2016-10-12 11:04:10 -0700228 // An empty Optional if no payload has yet been received.
229 //
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100230 virtual absl::optional<std::pair<int, SdpAudioFormat>>
231 ReceiveCodec() const = 0;
ossue280cde2016-10-12 11:04:10 -0700232
233 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -0800234 // int32_t IncomingPacket()
235 // Call this function to insert a parsed RTP packet into ACM.
236 //
237 // Inputs:
238 // -incoming_payload : received payload.
239 // -payload_len_bytes : the length of payload in bytes.
240 // -rtp_info : the relevant information retrieved from RTP
241 // header.
242 //
243 // Return value:
244 // -1 if failed to push in the payload
245 // 0 if payload is successfully pushed in.
246 //
247 virtual int32_t IncomingPacket(const uint8_t* incoming_payload,
248 const size_t payload_len_bytes,
Niels Möllerafb5dbb2019-02-15 15:21:47 +0100249 const RTPHeader& rtp_header) = 0;
kjellander3e6db232015-11-26 04:44:54 -0800250
251 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -0800252 // int SetMinimumPlayoutDelay()
253 // Set a minimum for the playout delay, used for lip-sync. NetEq maintains
254 // such a delay unless channel condition yields to a higher delay.
255 //
256 // Input:
257 // -time_ms : minimum delay in milliseconds.
258 //
259 // Return value:
260 // -1 if failed to set the delay,
261 // 0 if the minimum delay is set.
262 //
263 virtual int SetMinimumPlayoutDelay(int time_ms) = 0;
264
265 ///////////////////////////////////////////////////////////////////////////
266 // int SetMaximumPlayoutDelay()
267 // Set a maximum for the playout delay
268 //
269 // Input:
270 // -time_ms : maximum delay in milliseconds.
271 //
272 // Return value:
273 // -1 if failed to set the delay,
274 // 0 if the maximum delay is set.
275 //
276 virtual int SetMaximumPlayoutDelay(int time_ms) = 0;
277
Ruslan Burakov3b50f9f2019-02-06 09:45:56 +0100278 // Sets a base minimum for the playout delay. Base minimum delay sets lower
279 // bound minimum delay value which is set via SetMinimumPlayoutDelay.
280 //
281 // Returns true if value was successfully set, false overwise.
282 virtual bool SetBaseMinimumPlayoutDelayMs(int delay_ms) = 0;
283
284 // Returns current value of base minimum delay in milliseconds.
285 virtual int GetBaseMinimumPlayoutDelayMs() const = 0;
286
henrik.lundin9a410dd2016-04-06 01:39:22 -0700287 ///////////////////////////////////////////////////////////////////////////
288 // int32_t PlayoutTimestamp()
289 // The send timestamp of an RTP packet is associated with the decoded
290 // audio of the packet in question. This function returns the timestamp of
291 // the latest audio obtained by calling PlayoutData10ms(), or empty if no
292 // valid timestamp is available.
293 //
Danil Chapovalovb6021232018-06-19 13:26:36 +0200294 virtual absl::optional<uint32_t> PlayoutTimestamp() = 0;
kjellander3e6db232015-11-26 04:44:54 -0800295
296 ///////////////////////////////////////////////////////////////////////////
henrik.lundinb3f1c5d2016-08-22 15:39:53 -0700297 // int FilteredCurrentDelayMs()
298 // Returns the current total delay from NetEq (packet buffer and sync buffer)
299 // in ms, with smoothing applied to even out short-time fluctuations due to
300 // jitter. The packet buffer part of the delay is not updated during DTX/CNG
301 // periods.
302 //
303 virtual int FilteredCurrentDelayMs() const = 0;
304
305 ///////////////////////////////////////////////////////////////////////////
Henrik Lundinabbff892017-11-29 09:14:04 +0100306 // int FilteredCurrentDelayMs()
307 // Returns the current target delay for NetEq in ms.
308 //
309 virtual int TargetDelayMs() const = 0;
310
311 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -0800312 // int32_t PlayoutData10Ms(
313 // Get 10 milliseconds of raw audio data for playout, at the given sampling
314 // frequency. ACM will perform a resampling if required.
315 //
316 // Input:
317 // -desired_freq_hz : the desired sampling frequency, in Hertz, of the
318 // output audio. If set to -1, the function returns
319 // the audio at the current sampling frequency.
320 //
321 // Output:
322 // -audio_frame : output audio frame which contains raw audio data
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +0200323 // and other relevant parameters.
henrik.lundin834a6ea2016-05-13 03:45:24 -0700324 // -muted : if true, the sample data in audio_frame is not
325 // populated, and must be interpreted as all zero.
kjellander3e6db232015-11-26 04:44:54 -0800326 //
327 // Return value:
328 // -1 if the function fails,
329 // 0 if the function succeeds.
330 //
331 virtual int32_t PlayoutData10Ms(int32_t desired_freq_hz,
henrik.lundin834a6ea2016-05-13 03:45:24 -0700332 AudioFrame* audio_frame,
333 bool* muted) = 0;
334
kjellander3e6db232015-11-26 04:44:54 -0800335 ///////////////////////////////////////////////////////////////////////////
336 // Codec specific
337 //
338
339 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -0800340 // int SetOpusMaxPlaybackRate()
341 // If current send codec is Opus, informs it about maximum playback rate the
342 // receiver will render. Opus can use this information to optimize the bit
343 // rate and increase the computation efficiency.
344 //
345 // Input:
346 // -frequency_hz : maximum playback rate in Hz.
347 //
348 // Return value:
349 // -1 if current send codec is not Opus or
350 // error occurred in setting the maximum playback rate,
351 // 0 if maximum bandwidth is set successfully.
352 //
353 virtual int SetOpusMaxPlaybackRate(int frequency_hz) = 0;
354
355 ///////////////////////////////////////////////////////////////////////////
356 // EnableOpusDtx()
357 // Enable the DTX, if current send codec is Opus.
358 //
359 // Return value:
360 // -1 if current send codec is not Opus or error occurred in enabling the
361 // Opus DTX.
362 // 0 if Opus DTX is enabled successfully.
363 //
364 virtual int EnableOpusDtx() = 0;
365
366 ///////////////////////////////////////////////////////////////////////////
367 // int DisableOpusDtx()
368 // If current send codec is Opus, disables its internal DTX.
369 //
370 // Return value:
371 // -1 if current send codec is not Opus or error occurred in disabling DTX.
372 // 0 if Opus DTX is disabled successfully.
373 //
374 virtual int DisableOpusDtx() = 0;
375
376 ///////////////////////////////////////////////////////////////////////////
377 // statistics
378 //
379
380 ///////////////////////////////////////////////////////////////////////////
381 // int32_t GetNetworkStatistics()
382 // Get network statistics. Note that the internal statistics of NetEq are
383 // reset by this call.
384 //
385 // Input:
386 // -network_statistics : a structure that contains network statistics.
387 //
388 // Return value:
389 // -1 if failed to set the network statistics,
390 // 0 if statistics are set successfully.
391 //
392 virtual int32_t GetNetworkStatistics(
393 NetworkStatistics* network_statistics) = 0;
394
395 //
396 // Enable NACK and set the maximum size of the NACK list. If NACK is already
397 // enable then the maximum NACK list size is modified accordingly.
398 //
399 // If the sequence number of last received packet is N, the sequence numbers
400 // of NACK list are in the range of [N - |max_nack_list_size|, N).
401 //
402 // |max_nack_list_size| should be positive (none zero) and less than or
403 // equal to |Nack::kNackListSizeLimit|. Otherwise, No change is applied and -1
404 // is returned. 0 is returned at success.
405 //
406 virtual int EnableNack(size_t max_nack_list_size) = 0;
407
408 // Disable NACK.
409 virtual void DisableNack() = 0;
410
411 //
412 // Get a list of packets to be retransmitted. |round_trip_time_ms| is an
413 // estimate of the round-trip-time (in milliseconds). Missing packets which
414 // will be playout in a shorter time than the round-trip-time (with respect
415 // to the time this API is called) will not be included in the list.
416 //
417 // Negative |round_trip_time_ms| results is an error message and empty list
418 // is returned.
419 //
420 virtual std::vector<uint16_t> GetNackList(
421 int64_t round_trip_time_ms) const = 0;
422
423 virtual void GetDecodingCallStatistics(
424 AudioDecodingCallStats* call_stats) const = 0;
ivoce1198e02017-09-08 08:13:19 -0700425
426 virtual ANAStats GetANAStats() const = 0;
kjellander3e6db232015-11-26 04:44:54 -0800427};
428
429} // namespace webrtc
430
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200431#endif // MODULES_AUDIO_CODING_INCLUDE_AUDIO_CODING_MODULE_H_