blob: d84d2b5d50e97ac2c94d91f6bcfa41dae1636bf4 [file] [log] [blame]
ossueb1fde42017-05-02 13:46:301/*
2 * Copyright (c) 2014 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 04:47:3111#ifndef API_AUDIO_CODECS_AUDIO_ENCODER_H_
12#define API_AUDIO_CODECS_AUDIO_ENCODER_H_
ossueb1fde42017-05-02 13:46:3013
Dor Henaefed552024-06-18 13:20:3514#include <stddef.h>
15#include <stdint.h>
16
ossueb1fde42017-05-02 13:46:3017#include <memory>
Florent Castelli8037fc62024-08-29 13:00:4018#include <optional>
ossueb1fde42017-05-02 13:46:3019#include <string>
Sebastian Jansson62aee932019-10-02 10:27:0620#include <utility>
ossueb1fde42017-05-02 13:46:3021#include <vector>
22
Danil Chapovalove9041612021-02-22 11:43:3923#include "absl/base/attributes.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3124#include "api/array_view.h"
Sebastian Jansson540ef282018-11-21 18:18:5125#include "api/call/bitrate_allocation.h"
Jakob Ivarsson9d9b3a32024-02-07 08:44:3126#include "api/units/data_rate.h"
Sebastian Jansson62aee932019-10-02 10:27:0627#include "api/units/time_delta.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3128#include "rtc_base/buffer.h"
ossueb1fde42017-05-02 13:46:3029
30namespace webrtc {
31
ossueb1fde42017-05-02 13:46:3032class RtcEventLog;
33
ivoce1198e02017-09-08 15:13:1934// Statistics related to Audio Network Adaptation.
35struct ANAStats {
36 ANAStats();
37 ANAStats(const ANAStats&);
38 ~ANAStats();
39 // Number of actions taken by the ANA bitrate controller since the start of
40 // the call. If this value is not set, it indicates that the bitrate
41 // controller is disabled.
Florent Castelli8037fc62024-08-29 13:00:4042 std::optional<uint32_t> bitrate_action_counter;
ivoce1198e02017-09-08 15:13:1943 // Number of actions taken by the ANA channel controller since the start of
44 // the call. If this value is not set, it indicates that the channel
45 // controller is disabled.
Florent Castelli8037fc62024-08-29 13:00:4046 std::optional<uint32_t> channel_action_counter;
ivoce1198e02017-09-08 15:13:1947 // Number of actions taken by the ANA DTX controller since the start of the
48 // call. If this value is not set, it indicates that the DTX controller is
49 // disabled.
Florent Castelli8037fc62024-08-29 13:00:4050 std::optional<uint32_t> dtx_action_counter;
ivoce1198e02017-09-08 15:13:1951 // Number of actions taken by the ANA FEC controller since the start of the
52 // call. If this value is not set, it indicates that the FEC controller is
53 // disabled.
Florent Castelli8037fc62024-08-29 13:00:4054 std::optional<uint32_t> fec_action_counter;
ivoc0d0b9122017-09-08 20:24:2155 // Number of times the ANA frame length controller decided to increase the
56 // frame length since the start of the call. If this value is not set, it
57 // indicates that the frame length controller is disabled.
Florent Castelli8037fc62024-08-29 13:00:4058 std::optional<uint32_t> frame_length_increase_counter;
ivoc0d0b9122017-09-08 20:24:2159 // Number of times the ANA frame length controller decided to decrease the
60 // frame length since the start of the call. If this value is not set, it
61 // indicates that the frame length controller is disabled.
Florent Castelli8037fc62024-08-29 13:00:4062 std::optional<uint32_t> frame_length_decrease_counter;
ivoc0d0b9122017-09-08 20:24:2163 // The uplink packet loss fractions as set by the ANA FEC controller. If this
64 // value is not set, it indicates that the ANA FEC controller is not active.
Florent Castelli8037fc62024-08-29 13:00:4065 std::optional<float> uplink_packet_loss_fraction;
ivoce1198e02017-09-08 15:13:1966};
67
ossueb1fde42017-05-02 13:46:3068// This is the interface class for encoders in AudioCoding module. Each codec
69// type must have an implementation of this class.
70class AudioEncoder {
71 public:
72 // Used for UMA logging of codec usage. The same codecs, with the
73 // same values, must be listed in
74 // src/tools/metrics/histograms/histograms.xml in chromium to log
75 // correct values.
76 enum class CodecType {
77 kOther = 0, // Codec not specified, and/or not listed in this enum
78 kOpus = 1,
79 kIsac = 2,
80 kPcmA = 3,
81 kPcmU = 4,
82 kG722 = 5,
83 kIlbc = 6,
84
85 // Number of histogram bins in the UMA logging of codec types. The
86 // total number of different codecs that are logged cannot exceed this
87 // number.
88 kMaxLoggedAudioCodecTypes
89 };
90
91 struct EncodedInfoLeaf {
92 size_t encoded_bytes = 0;
93 uint32_t encoded_timestamp = 0;
94 int payload_type = 0;
95 bool send_even_if_empty = false;
96 bool speech = true;
97 CodecType encoder_type = CodecType::kOther;
98 };
99
100 // This is the main struct for auxiliary encoding information. Each encoded
101 // packet should be accompanied by one EncodedInfo struct, containing the
Artem Titov0e61fdd2021-07-25 19:50:14102 // total number of `encoded_bytes`, the `encoded_timestamp` and the
103 // `payload_type`. If the packet contains redundant encodings, the `redundant`
ossueb1fde42017-05-02 13:46:30104 // vector will be populated with EncodedInfoLeaf structs. Each struct in the
105 // vector represents one encoding; the order of structs in the vector is the
106 // same as the order in which the actual payloads are written to the byte
107 // stream. When EncoderInfoLeaf structs are present in the vector, the main
Artem Titov0e61fdd2021-07-25 19:50:14108 // struct's `encoded_bytes` will be the sum of all the `encoded_bytes` in the
ossueb1fde42017-05-02 13:46:30109 // vector.
110 struct EncodedInfo : public EncodedInfoLeaf {
111 EncodedInfo();
112 EncodedInfo(const EncodedInfo&);
113 EncodedInfo(EncodedInfo&&);
114 ~EncodedInfo();
115 EncodedInfo& operator=(const EncodedInfo&);
116 EncodedInfo& operator=(EncodedInfo&&);
117
118 std::vector<EncodedInfoLeaf> redundant;
119 };
120
121 virtual ~AudioEncoder() = default;
122
123 // Returns the input sample rate in Hz and the number of input channels.
124 // These are constants set at instantiation time.
125 virtual int SampleRateHz() const = 0;
126 virtual size_t NumChannels() const = 0;
127
128 // Returns the rate at which the RTP timestamps are updated. The default
129 // implementation returns SampleRateHz().
130 virtual int RtpTimestampRateHz() const;
131
132 // Returns the number of 10 ms frames the encoder will put in the next
133 // packet. This value may only change when Encode() outputs a packet; i.e.,
134 // the encoder may vary the number of 10 ms frames from packet to packet, but
135 // it must decide the length of the next packet no later than when outputting
136 // the preceding packet.
137 virtual size_t Num10MsFramesInNextPacket() const = 0;
138
139 // Returns the maximum value that can be returned by
140 // Num10MsFramesInNextPacket().
141 virtual size_t Max10MsFramesInAPacket() const = 0;
142
143 // Returns the current target bitrate in bits/s. The value -1 means that the
144 // codec adapts the target automatically, and a current target cannot be
145 // provided.
146 virtual int GetTargetBitrate() const = 0;
147
148 // Accepts one 10 ms block of input audio (i.e., SampleRateHz() / 100 *
149 // NumChannels() samples). Multi-channel audio must be sample-interleaved.
Artem Titov0e61fdd2021-07-25 19:50:14150 // The encoder appends zero or more bytes of output to `encoded` and returns
ossueb1fde42017-05-02 13:46:30151 // additional encoding information. Encode() checks some preconditions, calls
152 // EncodeImpl() which does the actual work, and then checks some
153 // postconditions.
154 EncodedInfo Encode(uint32_t rtp_timestamp,
155 rtc::ArrayView<const int16_t> audio,
156 rtc::Buffer* encoded);
157
158 // Resets the encoder to its starting state, discarding any input that has
159 // been fed to the encoder but not yet emitted in a packet.
160 virtual void Reset() = 0;
161
162 // Enables or disables codec-internal FEC (forward error correction). Returns
163 // true if the codec was able to comply. The default implementation returns
164 // true when asked to disable FEC and false when asked to enable it (meaning
165 // that FEC isn't supported).
166 virtual bool SetFec(bool enable);
167
168 // Enables or disables codec-internal VAD/DTX. Returns true if the codec was
169 // able to comply. The default implementation returns true when asked to
170 // disable DTX and false when asked to enable it (meaning that DTX isn't
171 // supported).
172 virtual bool SetDtx(bool enable);
173
174 // Returns the status of codec-internal DTX. The default implementation always
175 // returns false.
176 virtual bool GetDtx() const;
177
178 // Sets the application mode. Returns true if the codec was able to comply.
179 // The default implementation just returns false.
180 enum class Application { kSpeech, kAudio };
181 virtual bool SetApplication(Application application);
182
183 // Tells the encoder about the highest sample rate the decoder is expected to
184 // use when decoding the bitstream. The encoder would typically use this
185 // information to adjust the quality of the encoding. The default
186 // implementation does nothing.
187 virtual void SetMaxPlaybackRate(int frequency_hz);
188
ossueb1fde42017-05-02 13:46:30189 // Tells the encoder what average bitrate we'd like it to produce. The
190 // encoder is free to adjust or disregard the given bitrate (the default
191 // implementation does the latter).
Danil Chapovalove9041612021-02-22 11:43:39192 ABSL_DEPRECATED("Use OnReceivedTargetAudioBitrate instead")
193 virtual void SetTargetBitrate(int target_bps);
ossueb1fde42017-05-02 13:46:30194
195 // Causes this encoder to let go of any other encoders it contains, and
196 // returns a pointer to an array where they are stored (which is required to
197 // live as long as this encoder). Unless the returned array is empty, you may
198 // not call any methods on this encoder afterwards, except for the
199 // destructor. The default implementation just returns an empty array.
200 // NOTE: This method is subject to change. Do not call or override it.
201 virtual rtc::ArrayView<std::unique_ptr<AudioEncoder>>
202 ReclaimContainedEncoders();
203
204 // Enables audio network adaptor. Returns true if successful.
205 virtual bool EnableAudioNetworkAdaptor(const std::string& config_string,
206 RtcEventLog* event_log);
207
208 // Disables audio network adaptor.
209 virtual void DisableAudioNetworkAdaptor();
210
211 // Provides uplink packet loss fraction to this encoder to allow it to adapt.
Artem Titov0e61fdd2021-07-25 19:50:14212 // `uplink_packet_loss_fraction` is in the range [0.0, 1.0].
ossueb1fde42017-05-02 13:46:30213 virtual void OnReceivedUplinkPacketLossFraction(
214 float uplink_packet_loss_fraction);
215
Danil Chapovalove9041612021-02-22 11:43:39216 ABSL_DEPRECATED("")
217 virtual void OnReceivedUplinkRecoverablePacketLossFraction(
ossueb1fde42017-05-02 13:46:30218 float uplink_recoverable_packet_loss_fraction);
219
220 // Provides target audio bitrate to this encoder to allow it to adapt.
221 virtual void OnReceivedTargetAudioBitrate(int target_bps);
222
223 // Provides target audio bitrate and corresponding probing interval of
224 // the bandwidth estimator to this encoder to allow it to adapt.
Yves Gerey665174f2018-06-19 13:03:05225 virtual void OnReceivedUplinkBandwidth(int target_audio_bitrate_bps,
Florent Castelli8037fc62024-08-29 13:00:40226 std::optional<int64_t> bwe_period_ms);
ossueb1fde42017-05-02 13:46:30227
Sebastian Jansson540ef282018-11-21 18:18:51228 // Provides target audio bitrate and corresponding probing interval of
229 // the bandwidth estimator to this encoder to allow it to adapt.
230 virtual void OnReceivedUplinkAllocation(BitrateAllocationUpdate update);
231
ossueb1fde42017-05-02 13:46:30232 // Provides RTT to this encoder to allow it to adapt.
233 virtual void OnReceivedRtt(int rtt_ms);
234
235 // Provides overhead to this encoder to adapt. The overhead is the number of
236 // bytes that will be added to each packet the encoder generates.
237 virtual void OnReceivedOverhead(size_t overhead_bytes_per_packet);
238
239 // To allow encoder to adapt its frame length, it must be provided the frame
240 // length range that receivers can accept.
241 virtual void SetReceiverFrameLengthRange(int min_frame_length_ms,
242 int max_frame_length_ms);
243
ivoce1198e02017-09-08 15:13:19244 // Get statistics related to audio network adaptation.
245 virtual ANAStats GetANAStats() const;
246
Jakob Ivarsson9d9b3a32024-02-07 08:44:31247 // The range of frame lengths that are supported or nullopt if there's no such
248 // information. This is used together with the bitrate range to calculate the
249 // full bitrate range, including overhead.
Florent Castelli8037fc62024-08-29 13:00:40250 virtual std::optional<std::pair<TimeDelta, TimeDelta>> GetFrameLengthRange()
Ali Tofigh90ecee12020-03-24 14:35:25251 const = 0;
Sebastian Jansson62aee932019-10-02 10:27:06252
Jakob Ivarsson9d9b3a32024-02-07 08:44:31253 // The range of payload bitrates that are supported. This is used together
254 // with the frame length range to calculate the full bitrate range, including
255 // overhead.
Florent Castelli8037fc62024-08-29 13:00:40256 virtual std::optional<std::pair<DataRate, DataRate>> GetBitrateRange() const {
257 return std::nullopt;
Jakob Ivarsson9d9b3a32024-02-07 08:44:31258 }
259
Ivo Creusend8232592021-11-16 15:11:28260 // The maximum number of audio channels supported by WebRTC encoders.
261 static constexpr int kMaxNumberOfChannels = 24;
262
ossueb1fde42017-05-02 13:46:30263 protected:
264 // Subclasses implement this to perform the actual encoding. Called by
265 // Encode().
266 virtual EncodedInfo EncodeImpl(uint32_t rtp_timestamp,
267 rtc::ArrayView<const int16_t> audio,
268 rtc::Buffer* encoded) = 0;
269};
270} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 04:47:31271#endif // API_AUDIO_CODECS_AUDIO_ENCODER_H_