ossu | eb1fde4 | 2017-05-02 13:46:30 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef API_AUDIO_CODECS_AUDIO_ENCODER_H_ |
| 12 | #define API_AUDIO_CODECS_AUDIO_ENCODER_H_ |
ossu | eb1fde4 | 2017-05-02 13:46:30 | [diff] [blame] | 13 | |
Dor Hen | aefed55 | 2024-06-18 13:20:35 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
| 16 | |
ossu | eb1fde4 | 2017-05-02 13:46:30 | [diff] [blame] | 17 | #include <memory> |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 18 | #include <optional> |
ossu | eb1fde4 | 2017-05-02 13:46:30 | [diff] [blame] | 19 | #include <string> |
Sebastian Jansson | 62aee93 | 2019-10-02 10:27:06 | [diff] [blame] | 20 | #include <utility> |
ossu | eb1fde4 | 2017-05-02 13:46:30 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
Danil Chapovalov | e904161 | 2021-02-22 11:43:39 | [diff] [blame] | 23 | #include "absl/base/attributes.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 24 | #include "api/array_view.h" |
Sebastian Jansson | 540ef28 | 2018-11-21 18:18:51 | [diff] [blame] | 25 | #include "api/call/bitrate_allocation.h" |
Jakob Ivarsson | 9d9b3a3 | 2024-02-07 08:44:31 | [diff] [blame] | 26 | #include "api/units/data_rate.h" |
Sebastian Jansson | 62aee93 | 2019-10-02 10:27:06 | [diff] [blame] | 27 | #include "api/units/time_delta.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 28 | #include "rtc_base/buffer.h" |
ossu | eb1fde4 | 2017-05-02 13:46:30 | [diff] [blame] | 29 | |
| 30 | namespace webrtc { |
| 31 | |
ossu | eb1fde4 | 2017-05-02 13:46:30 | [diff] [blame] | 32 | class RtcEventLog; |
| 33 | |
ivoc | e1198e0 | 2017-09-08 15:13:19 | [diff] [blame] | 34 | // Statistics related to Audio Network Adaptation. |
| 35 | struct 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 Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 42 | std::optional<uint32_t> bitrate_action_counter; |
ivoc | e1198e0 | 2017-09-08 15:13:19 | [diff] [blame] | 43 | // 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 Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 46 | std::optional<uint32_t> channel_action_counter; |
ivoc | e1198e0 | 2017-09-08 15:13:19 | [diff] [blame] | 47 | // 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 Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 50 | std::optional<uint32_t> dtx_action_counter; |
ivoc | e1198e0 | 2017-09-08 15:13:19 | [diff] [blame] | 51 | // 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 Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 54 | std::optional<uint32_t> fec_action_counter; |
ivoc | 0d0b912 | 2017-09-08 20:24:21 | [diff] [blame] | 55 | // 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 Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 58 | std::optional<uint32_t> frame_length_increase_counter; |
ivoc | 0d0b912 | 2017-09-08 20:24:21 | [diff] [blame] | 59 | // 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 Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 62 | std::optional<uint32_t> frame_length_decrease_counter; |
ivoc | 0d0b912 | 2017-09-08 20:24:21 | [diff] [blame] | 63 | // 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 Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 65 | std::optional<float> uplink_packet_loss_fraction; |
ivoc | e1198e0 | 2017-09-08 15:13:19 | [diff] [blame] | 66 | }; |
| 67 | |
ossu | eb1fde4 | 2017-05-02 13:46:30 | [diff] [blame] | 68 | // This is the interface class for encoders in AudioCoding module. Each codec |
| 69 | // type must have an implementation of this class. |
| 70 | class 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 Titov | 0e61fdd | 2021-07-25 19:50:14 | [diff] [blame] | 102 | // total number of `encoded_bytes`, the `encoded_timestamp` and the |
| 103 | // `payload_type`. If the packet contains redundant encodings, the `redundant` |
ossu | eb1fde4 | 2017-05-02 13:46:30 | [diff] [blame] | 104 | // 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 Titov | 0e61fdd | 2021-07-25 19:50:14 | [diff] [blame] | 108 | // struct's `encoded_bytes` will be the sum of all the `encoded_bytes` in the |
ossu | eb1fde4 | 2017-05-02 13:46:30 | [diff] [blame] | 109 | // 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 Titov | 0e61fdd | 2021-07-25 19:50:14 | [diff] [blame] | 150 | // The encoder appends zero or more bytes of output to `encoded` and returns |
ossu | eb1fde4 | 2017-05-02 13:46:30 | [diff] [blame] | 151 | // 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 | |
ossu | eb1fde4 | 2017-05-02 13:46:30 | [diff] [blame] | 189 | // 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 Chapovalov | e904161 | 2021-02-22 11:43:39 | [diff] [blame] | 192 | ABSL_DEPRECATED("Use OnReceivedTargetAudioBitrate instead") |
| 193 | virtual void SetTargetBitrate(int target_bps); |
ossu | eb1fde4 | 2017-05-02 13:46:30 | [diff] [blame] | 194 | |
| 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 Titov | 0e61fdd | 2021-07-25 19:50:14 | [diff] [blame] | 212 | // `uplink_packet_loss_fraction` is in the range [0.0, 1.0]. |
ossu | eb1fde4 | 2017-05-02 13:46:30 | [diff] [blame] | 213 | virtual void OnReceivedUplinkPacketLossFraction( |
| 214 | float uplink_packet_loss_fraction); |
| 215 | |
Danil Chapovalov | e904161 | 2021-02-22 11:43:39 | [diff] [blame] | 216 | ABSL_DEPRECATED("") |
| 217 | virtual void OnReceivedUplinkRecoverablePacketLossFraction( |
ossu | eb1fde4 | 2017-05-02 13:46:30 | [diff] [blame] | 218 | 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 Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 225 | virtual void OnReceivedUplinkBandwidth(int target_audio_bitrate_bps, |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 226 | std::optional<int64_t> bwe_period_ms); |
ossu | eb1fde4 | 2017-05-02 13:46:30 | [diff] [blame] | 227 | |
Sebastian Jansson | 540ef28 | 2018-11-21 18:18:51 | [diff] [blame] | 228 | // 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 | |
ossu | eb1fde4 | 2017-05-02 13:46:30 | [diff] [blame] | 232 | // 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 | |
ivoc | e1198e0 | 2017-09-08 15:13:19 | [diff] [blame] | 244 | // Get statistics related to audio network adaptation. |
| 245 | virtual ANAStats GetANAStats() const; |
| 246 | |
Jakob Ivarsson | 9d9b3a3 | 2024-02-07 08:44:31 | [diff] [blame] | 247 | // 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 Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 250 | virtual std::optional<std::pair<TimeDelta, TimeDelta>> GetFrameLengthRange() |
Ali Tofigh | 90ecee1 | 2020-03-24 14:35:25 | [diff] [blame] | 251 | const = 0; |
Sebastian Jansson | 62aee93 | 2019-10-02 10:27:06 | [diff] [blame] | 252 | |
Jakob Ivarsson | 9d9b3a3 | 2024-02-07 08:44:31 | [diff] [blame] | 253 | // 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 Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 256 | virtual std::optional<std::pair<DataRate, DataRate>> GetBitrateRange() const { |
| 257 | return std::nullopt; |
Jakob Ivarsson | 9d9b3a3 | 2024-02-07 08:44:31 | [diff] [blame] | 258 | } |
| 259 | |
Ivo Creusen | d823259 | 2021-11-16 15:11:28 | [diff] [blame] | 260 | // The maximum number of audio channels supported by WebRTC encoders. |
| 261 | static constexpr int kMaxNumberOfChannels = 24; |
| 262 | |
ossu | eb1fde4 | 2017-05-02 13:46:30 | [diff] [blame] | 263 | 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 Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 271 | #endif // API_AUDIO_CODECS_AUDIO_ENCODER_H_ |