blob: 31da3d46ab350f5627953e4f043c12916372046d [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"
Ivo Creusen3ce44a32019-10-31 14:38:11 +010023#include "api/neteq/neteq.h"
Ivo Creusenc3d1f9b2019-11-01 11:47:51 +010024#include "api/neteq/neteq_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "system_wrappers/include/clock.h"
kjellander3e6db232015-11-26 04:44:54 -080027
28namespace webrtc {
29
30// forward declarations
kjellander3e6db232015-11-26 04:44:54 -080031class AudioDecoder;
32class AudioEncoder;
33class AudioFrame;
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,
Minyue Li48655cf2020-01-23 13:45:50 +010047 size_t payload_len_bytes,
48 int64_t absolute_capture_timestamp_ms) {
49 // TODO(bugs.webrtc.org/10739): Deprecate the old SendData and make this one
50 // pure virtual.
51 RTC_NOTREACHED() << "This method must be overridden, or not used.";
52 return -1;
53 }
54 virtual int32_t SendData(AudioFrameType frame_type,
55 uint8_t payload_type,
56 uint32_t timestamp,
57 const uint8_t* payload_data,
58 size_t payload_len_bytes) {
59 return SendData(frame_type, payload_type, timestamp, payload_data,
60 payload_len_bytes, 0);
61 }
kjellander3e6db232015-11-26 04:44:54 -080062};
63
64// Callback class used for reporting VAD decision
65class ACMVADCallback {
66 public:
67 virtual ~ACMVADCallback() {}
68
Niels Möller87e2d782019-03-07 10:18:23 +010069 virtual int32_t InFrameType(AudioFrameType frame_type) = 0;
kjellander3e6db232015-11-26 04:44:54 -080070};
71
72class AudioCodingModule {
73 protected:
74 AudioCodingModule() {}
75
76 public:
77 struct Config {
Karl Wiberg5817d3d2018-04-06 10:06:42 +020078 explicit Config(
79 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory = nullptr);
kwiberg36a43882016-08-29 05:33:32 -070080 Config(const Config&);
81 ~Config();
kjellander3e6db232015-11-26 04:44:54 -080082
kjellander3e6db232015-11-26 04:44:54 -080083 NetEq::Config neteq_config;
84 Clock* clock;
ossue3525782016-05-25 07:37:43 -070085 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory;
Ivo Creusenc3d1f9b2019-11-01 11:47:51 +010086 NetEqFactory* neteq_factory = nullptr;
kjellander3e6db232015-11-26 04:44:54 -080087 };
88
kjellander3e6db232015-11-26 04:44:54 -080089 static AudioCodingModule* Create(const Config& config);
90 virtual ~AudioCodingModule() = default;
91
92 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -080093 // Sender
94 //
95
kwiberg4cdbd572016-03-30 03:10:05 -070096 // |modifier| is called exactly once with one argument: a pointer to the
97 // unique_ptr that holds the current encoder (which is null if there is no
98 // current encoder). For the duration of the call, |modifier| has exclusive
99 // access to the unique_ptr; it may call the encoder, steal the encoder and
100 // replace it with another encoder or with nullptr, etc.
101 virtual void ModifyEncoder(
kwiberg24c7c122016-09-28 11:57:10 -0700102 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) = 0;
kwiberg4cdbd572016-03-30 03:10:05 -0700103
104 // Utility method for simply replacing the existing encoder with a new one.
105 void SetEncoder(std::unique_ptr<AudioEncoder> new_encoder) {
106 ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
107 *encoder = std::move(new_encoder);
108 });
109 }
110
kjellander3e6db232015-11-26 04:44:54 -0800111 // int32_t RegisterTransportCallback()
112 // Register a transport callback which will be called to deliver
113 // the encoded buffers whenever Process() is called and a
114 // bit-stream is ready.
115 //
116 // Input:
117 // -transport : pointer to the callback class
118 // transport->SendData() is called whenever
119 // Process() is called and bit-stream is ready
120 // to deliver.
121 //
122 // Return value:
123 // -1 if the transport callback could not be registered
124 // 0 if registration is successful.
125 //
126 virtual int32_t RegisterTransportCallback(
127 AudioPacketizationCallback* transport) = 0;
128
129 ///////////////////////////////////////////////////////////////////////////
130 // int32_t Add10MsData()
131 // Add 10MS of raw (PCM) audio data and encode it. If the sampling
132 // frequency of the audio does not match the sampling frequency of the
133 // current encoder ACM will resample the audio. If an encoded packet was
134 // produced, it will be delivered via the callback object registered using
135 // RegisterTransportCallback, and the return value from this function will
136 // be the number of bytes encoded.
137 //
138 // Input:
139 // -audio_frame : the input audio frame, containing raw audio
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +0200140 // sampling frequency etc.
kjellander3e6db232015-11-26 04:44:54 -0800141 //
142 // Return value:
143 // >= 0 number of bytes encoded.
144 // -1 some error occurred.
145 //
146 virtual int32_t Add10MsData(const AudioFrame& audio_frame) = 0;
147
148 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -0800149 // int SetPacketLossRate()
150 // Sets expected packet loss rate for encoding. Some encoders provide packet
151 // loss gnostic encoding to make stream less sensitive to packet losses,
152 // through e.g., FEC. No effects on codecs that do not provide such encoding.
153 //
154 // Input:
155 // -packet_loss_rate : expected packet loss rate (0 -- 100 inclusive).
156 //
157 // Return value
158 // -1 if failed to set packet loss rate,
159 // 0 if succeeded.
160 //
minyue7e304322016-10-12 05:00:55 -0700161 // This is only used in test code that rely on old ACM APIs.
162 // TODO(minyue): Remove it when possible.
kjellander3e6db232015-11-26 04:44:54 -0800163 virtual int SetPacketLossRate(int packet_loss_rate) = 0;
164
165 ///////////////////////////////////////////////////////////////////////////
166 // (VAD) Voice Activity Detection
167 //
168
169 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -0800170 // int32_t RegisterVADCallback()
171 // Call this method to register a callback function which is called
172 // any time that ACM encounters an empty frame. That is a frame which is
173 // recognized inactive. Depending on the codec WebRtc VAD or internal codec
174 // VAD is employed to identify a frame as active/inactive.
175 //
176 // Input:
177 // -vad_callback : pointer to a callback function.
178 //
179 // Return value:
180 // -1 if failed to register the callback function.
181 // 0 if the callback function is registered successfully.
182 //
183 virtual int32_t RegisterVADCallback(ACMVADCallback* vad_callback) = 0;
184
185 ///////////////////////////////////////////////////////////////////////////
186 // Receiver
187 //
188
189 ///////////////////////////////////////////////////////////////////////////
190 // int32_t InitializeReceiver()
191 // Any decoder-related state of ACM will be initialized to the
192 // same state when ACM is created. This will not interrupt or
193 // effect encoding functionality of ACM. ACM would lose all the
194 // decoding-related settings by calling this function.
195 // For instance, all registered codecs are deleted and have to be
196 // registered again.
197 //
198 // Return value:
199 // -1 if failed to initialize,
200 // 0 if succeeded.
201 //
202 virtual int32_t InitializeReceiver() = 0;
203
kwiberg1c07c702017-03-27 07:15:49 -0700204 // Replace any existing decoders with the given payload type -> decoder map.
205 virtual void SetReceiveCodecs(
206 const std::map<int, SdpAudioFormat>& codecs) = 0;
207
kjellander3e6db232015-11-26 04:44:54 -0800208 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -0800209 // int32_t IncomingPacket()
210 // Call this function to insert a parsed RTP packet into ACM.
211 //
212 // Inputs:
213 // -incoming_payload : received payload.
214 // -payload_len_bytes : the length of payload in bytes.
215 // -rtp_info : the relevant information retrieved from RTP
216 // header.
217 //
218 // Return value:
219 // -1 if failed to push in the payload
220 // 0 if payload is successfully pushed in.
221 //
222 virtual int32_t IncomingPacket(const uint8_t* incoming_payload,
223 const size_t payload_len_bytes,
Niels Möllerafb5dbb2019-02-15 15:21:47 +0100224 const RTPHeader& rtp_header) = 0;
kjellander3e6db232015-11-26 04:44:54 -0800225
226 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -0800227 // int32_t PlayoutData10Ms(
228 // Get 10 milliseconds of raw audio data for playout, at the given sampling
229 // frequency. ACM will perform a resampling if required.
230 //
231 // Input:
232 // -desired_freq_hz : the desired sampling frequency, in Hertz, of the
233 // output audio. If set to -1, the function returns
234 // the audio at the current sampling frequency.
235 //
236 // Output:
237 // -audio_frame : output audio frame which contains raw audio data
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +0200238 // and other relevant parameters.
henrik.lundin834a6ea2016-05-13 03:45:24 -0700239 // -muted : if true, the sample data in audio_frame is not
240 // populated, and must be interpreted as all zero.
kjellander3e6db232015-11-26 04:44:54 -0800241 //
242 // Return value:
243 // -1 if the function fails,
244 // 0 if the function succeeds.
245 //
246 virtual int32_t PlayoutData10Ms(int32_t desired_freq_hz,
henrik.lundin834a6ea2016-05-13 03:45:24 -0700247 AudioFrame* audio_frame,
248 bool* muted) = 0;
249
kjellander3e6db232015-11-26 04:44:54 -0800250 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -0800251 // statistics
252 //
253
254 ///////////////////////////////////////////////////////////////////////////
255 // int32_t GetNetworkStatistics()
256 // Get network statistics. Note that the internal statistics of NetEq are
257 // reset by this call.
258 //
259 // Input:
260 // -network_statistics : a structure that contains network statistics.
261 //
262 // Return value:
263 // -1 if failed to set the network statistics,
264 // 0 if statistics are set successfully.
265 //
266 virtual int32_t GetNetworkStatistics(
267 NetworkStatistics* network_statistics) = 0;
268
ivoce1198e02017-09-08 08:13:19 -0700269 virtual ANAStats GetANAStats() const = 0;
kjellander3e6db232015-11-26 04:44:54 -0800270};
271
272} // namespace webrtc
273
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200274#endif // MODULES_AUDIO_CODING_INCLUDE_AUDIO_CODING_MODULE_H_