blob: d8c926007704fbe9bae65bcf6babeee51512dac2 [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,
Niels Möller4babc682019-04-26 15:46:12 +020047 size_t payload_len_bytes) = 0;
kjellander3e6db232015-11-26 04:44:54 -080048};
49
50// Callback class used for reporting VAD decision
51class ACMVADCallback {
52 public:
53 virtual ~ACMVADCallback() {}
54
Niels Möller87e2d782019-03-07 10:18:23 +010055 virtual int32_t InFrameType(AudioFrameType frame_type) = 0;
kjellander3e6db232015-11-26 04:44:54 -080056};
57
58class AudioCodingModule {
59 protected:
60 AudioCodingModule() {}
61
62 public:
63 struct Config {
Karl Wiberg5817d3d2018-04-06 10:06:42 +020064 explicit Config(
65 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory = nullptr);
kwiberg36a43882016-08-29 05:33:32 -070066 Config(const Config&);
67 ~Config();
kjellander3e6db232015-11-26 04:44:54 -080068
kjellander3e6db232015-11-26 04:44:54 -080069 NetEq::Config neteq_config;
70 Clock* clock;
ossue3525782016-05-25 07:37:43 -070071 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory;
Ivo Creusenc3d1f9b2019-11-01 11:47:51 +010072 NetEqFactory* neteq_factory = nullptr;
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 // int32_t RegisterTransportCallback()
98 // Register a transport callback which will be called to deliver
99 // the encoded buffers whenever Process() is called and a
100 // bit-stream is ready.
101 //
102 // Input:
103 // -transport : pointer to the callback class
104 // transport->SendData() is called whenever
105 // Process() is called and bit-stream is ready
106 // to deliver.
107 //
108 // Return value:
109 // -1 if the transport callback could not be registered
110 // 0 if registration is successful.
111 //
112 virtual int32_t RegisterTransportCallback(
113 AudioPacketizationCallback* transport) = 0;
114
115 ///////////////////////////////////////////////////////////////////////////
116 // int32_t Add10MsData()
117 // Add 10MS of raw (PCM) audio data and encode it. If the sampling
118 // frequency of the audio does not match the sampling frequency of the
119 // current encoder ACM will resample the audio. If an encoded packet was
120 // produced, it will be delivered via the callback object registered using
121 // RegisterTransportCallback, and the return value from this function will
122 // be the number of bytes encoded.
123 //
124 // Input:
125 // -audio_frame : the input audio frame, containing raw audio
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +0200126 // sampling frequency etc.
kjellander3e6db232015-11-26 04:44:54 -0800127 //
128 // Return value:
129 // >= 0 number of bytes encoded.
130 // -1 some error occurred.
131 //
132 virtual int32_t Add10MsData(const AudioFrame& audio_frame) = 0;
133
134 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -0800135 // int SetPacketLossRate()
136 // Sets expected packet loss rate for encoding. Some encoders provide packet
137 // loss gnostic encoding to make stream less sensitive to packet losses,
138 // through e.g., FEC. No effects on codecs that do not provide such encoding.
139 //
140 // Input:
141 // -packet_loss_rate : expected packet loss rate (0 -- 100 inclusive).
142 //
143 // Return value
144 // -1 if failed to set packet loss rate,
145 // 0 if succeeded.
146 //
minyue7e304322016-10-12 05:00:55 -0700147 // This is only used in test code that rely on old ACM APIs.
148 // TODO(minyue): Remove it when possible.
kjellander3e6db232015-11-26 04:44:54 -0800149 virtual int SetPacketLossRate(int packet_loss_rate) = 0;
150
151 ///////////////////////////////////////////////////////////////////////////
152 // (VAD) Voice Activity Detection
153 //
154
155 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -0800156 // int32_t RegisterVADCallback()
157 // Call this method to register a callback function which is called
158 // any time that ACM encounters an empty frame. That is a frame which is
159 // recognized inactive. Depending on the codec WebRtc VAD or internal codec
160 // VAD is employed to identify a frame as active/inactive.
161 //
162 // Input:
163 // -vad_callback : pointer to a callback function.
164 //
165 // Return value:
166 // -1 if failed to register the callback function.
167 // 0 if the callback function is registered successfully.
168 //
169 virtual int32_t RegisterVADCallback(ACMVADCallback* vad_callback) = 0;
170
171 ///////////////////////////////////////////////////////////////////////////
172 // Receiver
173 //
174
175 ///////////////////////////////////////////////////////////////////////////
176 // int32_t InitializeReceiver()
177 // Any decoder-related state of ACM will be initialized to the
178 // same state when ACM is created. This will not interrupt or
179 // effect encoding functionality of ACM. ACM would lose all the
180 // decoding-related settings by calling this function.
181 // For instance, all registered codecs are deleted and have to be
182 // registered again.
183 //
184 // Return value:
185 // -1 if failed to initialize,
186 // 0 if succeeded.
187 //
188 virtual int32_t InitializeReceiver() = 0;
189
kwiberg1c07c702017-03-27 07:15:49 -0700190 // Replace any existing decoders with the given payload type -> decoder map.
191 virtual void SetReceiveCodecs(
192 const std::map<int, SdpAudioFormat>& codecs) = 0;
193
kjellander3e6db232015-11-26 04:44:54 -0800194 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -0800195 // int32_t IncomingPacket()
196 // Call this function to insert a parsed RTP packet into ACM.
197 //
198 // Inputs:
199 // -incoming_payload : received payload.
200 // -payload_len_bytes : the length of payload in bytes.
201 // -rtp_info : the relevant information retrieved from RTP
202 // header.
203 //
204 // Return value:
205 // -1 if failed to push in the payload
206 // 0 if payload is successfully pushed in.
207 //
208 virtual int32_t IncomingPacket(const uint8_t* incoming_payload,
209 const size_t payload_len_bytes,
Niels Möllerafb5dbb2019-02-15 15:21:47 +0100210 const RTPHeader& rtp_header) = 0;
kjellander3e6db232015-11-26 04:44:54 -0800211
212 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -0800213 // int32_t PlayoutData10Ms(
214 // Get 10 milliseconds of raw audio data for playout, at the given sampling
215 // frequency. ACM will perform a resampling if required.
216 //
217 // Input:
218 // -desired_freq_hz : the desired sampling frequency, in Hertz, of the
219 // output audio. If set to -1, the function returns
220 // the audio at the current sampling frequency.
221 //
222 // Output:
223 // -audio_frame : output audio frame which contains raw audio data
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +0200224 // and other relevant parameters.
henrik.lundin834a6ea2016-05-13 03:45:24 -0700225 // -muted : if true, the sample data in audio_frame is not
226 // populated, and must be interpreted as all zero.
kjellander3e6db232015-11-26 04:44:54 -0800227 //
228 // Return value:
229 // -1 if the function fails,
230 // 0 if the function succeeds.
231 //
232 virtual int32_t PlayoutData10Ms(int32_t desired_freq_hz,
henrik.lundin834a6ea2016-05-13 03:45:24 -0700233 AudioFrame* audio_frame,
234 bool* muted) = 0;
235
kjellander3e6db232015-11-26 04:44:54 -0800236 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -0800237 // statistics
238 //
239
240 ///////////////////////////////////////////////////////////////////////////
241 // int32_t GetNetworkStatistics()
242 // Get network statistics. Note that the internal statistics of NetEq are
243 // reset by this call.
244 //
245 // Input:
246 // -network_statistics : a structure that contains network statistics.
247 //
248 // Return value:
249 // -1 if failed to set the network statistics,
250 // 0 if statistics are set successfully.
251 //
252 virtual int32_t GetNetworkStatistics(
253 NetworkStatistics* network_statistics) = 0;
254
ivoce1198e02017-09-08 08:13:19 -0700255 virtual ANAStats GetANAStats() const = 0;
kjellander3e6db232015-11-26 04:44:54 -0800256};
257
258} // namespace webrtc
259
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200260#endif // MODULES_AUDIO_CODING_INCLUDE_AUDIO_CODING_MODULE_H_