xref: /aosp_15_r20/external/webrtc/api/audio_codecs/audio_encoder.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef API_AUDIO_CODECS_AUDIO_ENCODER_H_
12*d9f75844SAndroid Build Coastguard Worker #define API_AUDIO_CODECS_AUDIO_ENCODER_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <memory>
15*d9f75844SAndroid Build Coastguard Worker #include <string>
16*d9f75844SAndroid Build Coastguard Worker #include <utility>
17*d9f75844SAndroid Build Coastguard Worker #include <vector>
18*d9f75844SAndroid Build Coastguard Worker 
19*d9f75844SAndroid Build Coastguard Worker #include "absl/base/attributes.h"
20*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h"
21*d9f75844SAndroid Build Coastguard Worker #include "api/array_view.h"
22*d9f75844SAndroid Build Coastguard Worker #include "api/call/bitrate_allocation.h"
23*d9f75844SAndroid Build Coastguard Worker #include "api/units/time_delta.h"
24*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/buffer.h"
25*d9f75844SAndroid Build Coastguard Worker 
26*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
27*d9f75844SAndroid Build Coastguard Worker 
28*d9f75844SAndroid Build Coastguard Worker class RtcEventLog;
29*d9f75844SAndroid Build Coastguard Worker 
30*d9f75844SAndroid Build Coastguard Worker // Statistics related to Audio Network Adaptation.
31*d9f75844SAndroid Build Coastguard Worker struct ANAStats {
32*d9f75844SAndroid Build Coastguard Worker   ANAStats();
33*d9f75844SAndroid Build Coastguard Worker   ANAStats(const ANAStats&);
34*d9f75844SAndroid Build Coastguard Worker   ~ANAStats();
35*d9f75844SAndroid Build Coastguard Worker   // Number of actions taken by the ANA bitrate controller since the start of
36*d9f75844SAndroid Build Coastguard Worker   // the call. If this value is not set, it indicates that the bitrate
37*d9f75844SAndroid Build Coastguard Worker   // controller is disabled.
38*d9f75844SAndroid Build Coastguard Worker   absl::optional<uint32_t> bitrate_action_counter;
39*d9f75844SAndroid Build Coastguard Worker   // Number of actions taken by the ANA channel controller since the start of
40*d9f75844SAndroid Build Coastguard Worker   // the call. If this value is not set, it indicates that the channel
41*d9f75844SAndroid Build Coastguard Worker   // controller is disabled.
42*d9f75844SAndroid Build Coastguard Worker   absl::optional<uint32_t> channel_action_counter;
43*d9f75844SAndroid Build Coastguard Worker   // Number of actions taken by the ANA DTX controller since the start of the
44*d9f75844SAndroid Build Coastguard Worker   // call. If this value is not set, it indicates that the DTX controller is
45*d9f75844SAndroid Build Coastguard Worker   // disabled.
46*d9f75844SAndroid Build Coastguard Worker   absl::optional<uint32_t> dtx_action_counter;
47*d9f75844SAndroid Build Coastguard Worker   // Number of actions taken by the ANA FEC controller since the start of the
48*d9f75844SAndroid Build Coastguard Worker   // call. If this value is not set, it indicates that the FEC controller is
49*d9f75844SAndroid Build Coastguard Worker   // disabled.
50*d9f75844SAndroid Build Coastguard Worker   absl::optional<uint32_t> fec_action_counter;
51*d9f75844SAndroid Build Coastguard Worker   // Number of times the ANA frame length controller decided to increase the
52*d9f75844SAndroid Build Coastguard Worker   // frame length since the start of the call. If this value is not set, it
53*d9f75844SAndroid Build Coastguard Worker   // indicates that the frame length controller is disabled.
54*d9f75844SAndroid Build Coastguard Worker   absl::optional<uint32_t> frame_length_increase_counter;
55*d9f75844SAndroid Build Coastguard Worker   // Number of times the ANA frame length controller decided to decrease the
56*d9f75844SAndroid Build Coastguard Worker   // frame length since the start of the call. If this value is not set, it
57*d9f75844SAndroid Build Coastguard Worker   // indicates that the frame length controller is disabled.
58*d9f75844SAndroid Build Coastguard Worker   absl::optional<uint32_t> frame_length_decrease_counter;
59*d9f75844SAndroid Build Coastguard Worker   // The uplink packet loss fractions as set by the ANA FEC controller. If this
60*d9f75844SAndroid Build Coastguard Worker   // value is not set, it indicates that the ANA FEC controller is not active.
61*d9f75844SAndroid Build Coastguard Worker   absl::optional<float> uplink_packet_loss_fraction;
62*d9f75844SAndroid Build Coastguard Worker };
63*d9f75844SAndroid Build Coastguard Worker 
64*d9f75844SAndroid Build Coastguard Worker // This is the interface class for encoders in AudioCoding module. Each codec
65*d9f75844SAndroid Build Coastguard Worker // type must have an implementation of this class.
66*d9f75844SAndroid Build Coastguard Worker class AudioEncoder {
67*d9f75844SAndroid Build Coastguard Worker  public:
68*d9f75844SAndroid Build Coastguard Worker   // Used for UMA logging of codec usage. The same codecs, with the
69*d9f75844SAndroid Build Coastguard Worker   // same values, must be listed in
70*d9f75844SAndroid Build Coastguard Worker   // src/tools/metrics/histograms/histograms.xml in chromium to log
71*d9f75844SAndroid Build Coastguard Worker   // correct values.
72*d9f75844SAndroid Build Coastguard Worker   enum class CodecType {
73*d9f75844SAndroid Build Coastguard Worker     kOther = 0,  // Codec not specified, and/or not listed in this enum
74*d9f75844SAndroid Build Coastguard Worker     kOpus = 1,
75*d9f75844SAndroid Build Coastguard Worker     kIsac = 2,
76*d9f75844SAndroid Build Coastguard Worker     kPcmA = 3,
77*d9f75844SAndroid Build Coastguard Worker     kPcmU = 4,
78*d9f75844SAndroid Build Coastguard Worker     kG722 = 5,
79*d9f75844SAndroid Build Coastguard Worker     kIlbc = 6,
80*d9f75844SAndroid Build Coastguard Worker 
81*d9f75844SAndroid Build Coastguard Worker     // Number of histogram bins in the UMA logging of codec types. The
82*d9f75844SAndroid Build Coastguard Worker     // total number of different codecs that are logged cannot exceed this
83*d9f75844SAndroid Build Coastguard Worker     // number.
84*d9f75844SAndroid Build Coastguard Worker     kMaxLoggedAudioCodecTypes
85*d9f75844SAndroid Build Coastguard Worker   };
86*d9f75844SAndroid Build Coastguard Worker 
87*d9f75844SAndroid Build Coastguard Worker   struct EncodedInfoLeaf {
88*d9f75844SAndroid Build Coastguard Worker     size_t encoded_bytes = 0;
89*d9f75844SAndroid Build Coastguard Worker     uint32_t encoded_timestamp = 0;
90*d9f75844SAndroid Build Coastguard Worker     int payload_type = 0;
91*d9f75844SAndroid Build Coastguard Worker     bool send_even_if_empty = false;
92*d9f75844SAndroid Build Coastguard Worker     bool speech = true;
93*d9f75844SAndroid Build Coastguard Worker     CodecType encoder_type = CodecType::kOther;
94*d9f75844SAndroid Build Coastguard Worker   };
95*d9f75844SAndroid Build Coastguard Worker 
96*d9f75844SAndroid Build Coastguard Worker   // This is the main struct for auxiliary encoding information. Each encoded
97*d9f75844SAndroid Build Coastguard Worker   // packet should be accompanied by one EncodedInfo struct, containing the
98*d9f75844SAndroid Build Coastguard Worker   // total number of `encoded_bytes`, the `encoded_timestamp` and the
99*d9f75844SAndroid Build Coastguard Worker   // `payload_type`. If the packet contains redundant encodings, the `redundant`
100*d9f75844SAndroid Build Coastguard Worker   // vector will be populated with EncodedInfoLeaf structs. Each struct in the
101*d9f75844SAndroid Build Coastguard Worker   // vector represents one encoding; the order of structs in the vector is the
102*d9f75844SAndroid Build Coastguard Worker   // same as the order in which the actual payloads are written to the byte
103*d9f75844SAndroid Build Coastguard Worker   // stream. When EncoderInfoLeaf structs are present in the vector, the main
104*d9f75844SAndroid Build Coastguard Worker   // struct's `encoded_bytes` will be the sum of all the `encoded_bytes` in the
105*d9f75844SAndroid Build Coastguard Worker   // vector.
106*d9f75844SAndroid Build Coastguard Worker   struct EncodedInfo : public EncodedInfoLeaf {
107*d9f75844SAndroid Build Coastguard Worker     EncodedInfo();
108*d9f75844SAndroid Build Coastguard Worker     EncodedInfo(const EncodedInfo&);
109*d9f75844SAndroid Build Coastguard Worker     EncodedInfo(EncodedInfo&&);
110*d9f75844SAndroid Build Coastguard Worker     ~EncodedInfo();
111*d9f75844SAndroid Build Coastguard Worker     EncodedInfo& operator=(const EncodedInfo&);
112*d9f75844SAndroid Build Coastguard Worker     EncodedInfo& operator=(EncodedInfo&&);
113*d9f75844SAndroid Build Coastguard Worker 
114*d9f75844SAndroid Build Coastguard Worker     std::vector<EncodedInfoLeaf> redundant;
115*d9f75844SAndroid Build Coastguard Worker   };
116*d9f75844SAndroid Build Coastguard Worker 
117*d9f75844SAndroid Build Coastguard Worker   virtual ~AudioEncoder() = default;
118*d9f75844SAndroid Build Coastguard Worker 
119*d9f75844SAndroid Build Coastguard Worker   // Returns the input sample rate in Hz and the number of input channels.
120*d9f75844SAndroid Build Coastguard Worker   // These are constants set at instantiation time.
121*d9f75844SAndroid Build Coastguard Worker   virtual int SampleRateHz() const = 0;
122*d9f75844SAndroid Build Coastguard Worker   virtual size_t NumChannels() const = 0;
123*d9f75844SAndroid Build Coastguard Worker 
124*d9f75844SAndroid Build Coastguard Worker   // Returns the rate at which the RTP timestamps are updated. The default
125*d9f75844SAndroid Build Coastguard Worker   // implementation returns SampleRateHz().
126*d9f75844SAndroid Build Coastguard Worker   virtual int RtpTimestampRateHz() const;
127*d9f75844SAndroid Build Coastguard Worker 
128*d9f75844SAndroid Build Coastguard Worker   // Returns the number of 10 ms frames the encoder will put in the next
129*d9f75844SAndroid Build Coastguard Worker   // packet. This value may only change when Encode() outputs a packet; i.e.,
130*d9f75844SAndroid Build Coastguard Worker   // the encoder may vary the number of 10 ms frames from packet to packet, but
131*d9f75844SAndroid Build Coastguard Worker   // it must decide the length of the next packet no later than when outputting
132*d9f75844SAndroid Build Coastguard Worker   // the preceding packet.
133*d9f75844SAndroid Build Coastguard Worker   virtual size_t Num10MsFramesInNextPacket() const = 0;
134*d9f75844SAndroid Build Coastguard Worker 
135*d9f75844SAndroid Build Coastguard Worker   // Returns the maximum value that can be returned by
136*d9f75844SAndroid Build Coastguard Worker   // Num10MsFramesInNextPacket().
137*d9f75844SAndroid Build Coastguard Worker   virtual size_t Max10MsFramesInAPacket() const = 0;
138*d9f75844SAndroid Build Coastguard Worker 
139*d9f75844SAndroid Build Coastguard Worker   // Returns the current target bitrate in bits/s. The value -1 means that the
140*d9f75844SAndroid Build Coastguard Worker   // codec adapts the target automatically, and a current target cannot be
141*d9f75844SAndroid Build Coastguard Worker   // provided.
142*d9f75844SAndroid Build Coastguard Worker   virtual int GetTargetBitrate() const = 0;
143*d9f75844SAndroid Build Coastguard Worker 
144*d9f75844SAndroid Build Coastguard Worker   // Accepts one 10 ms block of input audio (i.e., SampleRateHz() / 100 *
145*d9f75844SAndroid Build Coastguard Worker   // NumChannels() samples). Multi-channel audio must be sample-interleaved.
146*d9f75844SAndroid Build Coastguard Worker   // The encoder appends zero or more bytes of output to `encoded` and returns
147*d9f75844SAndroid Build Coastguard Worker   // additional encoding information.  Encode() checks some preconditions, calls
148*d9f75844SAndroid Build Coastguard Worker   // EncodeImpl() which does the actual work, and then checks some
149*d9f75844SAndroid Build Coastguard Worker   // postconditions.
150*d9f75844SAndroid Build Coastguard Worker   EncodedInfo Encode(uint32_t rtp_timestamp,
151*d9f75844SAndroid Build Coastguard Worker                      rtc::ArrayView<const int16_t> audio,
152*d9f75844SAndroid Build Coastguard Worker                      rtc::Buffer* encoded);
153*d9f75844SAndroid Build Coastguard Worker 
154*d9f75844SAndroid Build Coastguard Worker   // Resets the encoder to its starting state, discarding any input that has
155*d9f75844SAndroid Build Coastguard Worker   // been fed to the encoder but not yet emitted in a packet.
156*d9f75844SAndroid Build Coastguard Worker   virtual void Reset() = 0;
157*d9f75844SAndroid Build Coastguard Worker 
158*d9f75844SAndroid Build Coastguard Worker   // Enables or disables codec-internal FEC (forward error correction). Returns
159*d9f75844SAndroid Build Coastguard Worker   // true if the codec was able to comply. The default implementation returns
160*d9f75844SAndroid Build Coastguard Worker   // true when asked to disable FEC and false when asked to enable it (meaning
161*d9f75844SAndroid Build Coastguard Worker   // that FEC isn't supported).
162*d9f75844SAndroid Build Coastguard Worker   virtual bool SetFec(bool enable);
163*d9f75844SAndroid Build Coastguard Worker 
164*d9f75844SAndroid Build Coastguard Worker   // Enables or disables codec-internal VAD/DTX. Returns true if the codec was
165*d9f75844SAndroid Build Coastguard Worker   // able to comply. The default implementation returns true when asked to
166*d9f75844SAndroid Build Coastguard Worker   // disable DTX and false when asked to enable it (meaning that DTX isn't
167*d9f75844SAndroid Build Coastguard Worker   // supported).
168*d9f75844SAndroid Build Coastguard Worker   virtual bool SetDtx(bool enable);
169*d9f75844SAndroid Build Coastguard Worker 
170*d9f75844SAndroid Build Coastguard Worker   // Returns the status of codec-internal DTX. The default implementation always
171*d9f75844SAndroid Build Coastguard Worker   // returns false.
172*d9f75844SAndroid Build Coastguard Worker   virtual bool GetDtx() const;
173*d9f75844SAndroid Build Coastguard Worker 
174*d9f75844SAndroid Build Coastguard Worker   // Sets the application mode. Returns true if the codec was able to comply.
175*d9f75844SAndroid Build Coastguard Worker   // The default implementation just returns false.
176*d9f75844SAndroid Build Coastguard Worker   enum class Application { kSpeech, kAudio };
177*d9f75844SAndroid Build Coastguard Worker   virtual bool SetApplication(Application application);
178*d9f75844SAndroid Build Coastguard Worker 
179*d9f75844SAndroid Build Coastguard Worker   // Tells the encoder about the highest sample rate the decoder is expected to
180*d9f75844SAndroid Build Coastguard Worker   // use when decoding the bitstream. The encoder would typically use this
181*d9f75844SAndroid Build Coastguard Worker   // information to adjust the quality of the encoding. The default
182*d9f75844SAndroid Build Coastguard Worker   // implementation does nothing.
183*d9f75844SAndroid Build Coastguard Worker   virtual void SetMaxPlaybackRate(int frequency_hz);
184*d9f75844SAndroid Build Coastguard Worker 
185*d9f75844SAndroid Build Coastguard Worker   // Tells the encoder what average bitrate we'd like it to produce. The
186*d9f75844SAndroid Build Coastguard Worker   // encoder is free to adjust or disregard the given bitrate (the default
187*d9f75844SAndroid Build Coastguard Worker   // implementation does the latter).
188*d9f75844SAndroid Build Coastguard Worker   ABSL_DEPRECATED("Use OnReceivedTargetAudioBitrate instead")
189*d9f75844SAndroid Build Coastguard Worker   virtual void SetTargetBitrate(int target_bps);
190*d9f75844SAndroid Build Coastguard Worker 
191*d9f75844SAndroid Build Coastguard Worker   // Causes this encoder to let go of any other encoders it contains, and
192*d9f75844SAndroid Build Coastguard Worker   // returns a pointer to an array where they are stored (which is required to
193*d9f75844SAndroid Build Coastguard Worker   // live as long as this encoder). Unless the returned array is empty, you may
194*d9f75844SAndroid Build Coastguard Worker   // not call any methods on this encoder afterwards, except for the
195*d9f75844SAndroid Build Coastguard Worker   // destructor. The default implementation just returns an empty array.
196*d9f75844SAndroid Build Coastguard Worker   // NOTE: This method is subject to change. Do not call or override it.
197*d9f75844SAndroid Build Coastguard Worker   virtual rtc::ArrayView<std::unique_ptr<AudioEncoder>>
198*d9f75844SAndroid Build Coastguard Worker   ReclaimContainedEncoders();
199*d9f75844SAndroid Build Coastguard Worker 
200*d9f75844SAndroid Build Coastguard Worker   // Enables audio network adaptor. Returns true if successful.
201*d9f75844SAndroid Build Coastguard Worker   virtual bool EnableAudioNetworkAdaptor(const std::string& config_string,
202*d9f75844SAndroid Build Coastguard Worker                                          RtcEventLog* event_log);
203*d9f75844SAndroid Build Coastguard Worker 
204*d9f75844SAndroid Build Coastguard Worker   // Disables audio network adaptor.
205*d9f75844SAndroid Build Coastguard Worker   virtual void DisableAudioNetworkAdaptor();
206*d9f75844SAndroid Build Coastguard Worker 
207*d9f75844SAndroid Build Coastguard Worker   // Provides uplink packet loss fraction to this encoder to allow it to adapt.
208*d9f75844SAndroid Build Coastguard Worker   // `uplink_packet_loss_fraction` is in the range [0.0, 1.0].
209*d9f75844SAndroid Build Coastguard Worker   virtual void OnReceivedUplinkPacketLossFraction(
210*d9f75844SAndroid Build Coastguard Worker       float uplink_packet_loss_fraction);
211*d9f75844SAndroid Build Coastguard Worker 
212*d9f75844SAndroid Build Coastguard Worker   ABSL_DEPRECATED("")
213*d9f75844SAndroid Build Coastguard Worker   virtual void OnReceivedUplinkRecoverablePacketLossFraction(
214*d9f75844SAndroid Build Coastguard Worker       float uplink_recoverable_packet_loss_fraction);
215*d9f75844SAndroid Build Coastguard Worker 
216*d9f75844SAndroid Build Coastguard Worker   // Provides target audio bitrate to this encoder to allow it to adapt.
217*d9f75844SAndroid Build Coastguard Worker   virtual void OnReceivedTargetAudioBitrate(int target_bps);
218*d9f75844SAndroid Build Coastguard Worker 
219*d9f75844SAndroid Build Coastguard Worker   // Provides target audio bitrate and corresponding probing interval of
220*d9f75844SAndroid Build Coastguard Worker   // the bandwidth estimator to this encoder to allow it to adapt.
221*d9f75844SAndroid Build Coastguard Worker   virtual void OnReceivedUplinkBandwidth(int target_audio_bitrate_bps,
222*d9f75844SAndroid Build Coastguard Worker                                          absl::optional<int64_t> bwe_period_ms);
223*d9f75844SAndroid Build Coastguard Worker 
224*d9f75844SAndroid Build Coastguard Worker   // Provides target audio bitrate and corresponding probing interval of
225*d9f75844SAndroid Build Coastguard Worker   // the bandwidth estimator to this encoder to allow it to adapt.
226*d9f75844SAndroid Build Coastguard Worker   virtual void OnReceivedUplinkAllocation(BitrateAllocationUpdate update);
227*d9f75844SAndroid Build Coastguard Worker 
228*d9f75844SAndroid Build Coastguard Worker   // Provides RTT to this encoder to allow it to adapt.
229*d9f75844SAndroid Build Coastguard Worker   virtual void OnReceivedRtt(int rtt_ms);
230*d9f75844SAndroid Build Coastguard Worker 
231*d9f75844SAndroid Build Coastguard Worker   // Provides overhead to this encoder to adapt. The overhead is the number of
232*d9f75844SAndroid Build Coastguard Worker   // bytes that will be added to each packet the encoder generates.
233*d9f75844SAndroid Build Coastguard Worker   virtual void OnReceivedOverhead(size_t overhead_bytes_per_packet);
234*d9f75844SAndroid Build Coastguard Worker 
235*d9f75844SAndroid Build Coastguard Worker   // To allow encoder to adapt its frame length, it must be provided the frame
236*d9f75844SAndroid Build Coastguard Worker   // length range that receivers can accept.
237*d9f75844SAndroid Build Coastguard Worker   virtual void SetReceiverFrameLengthRange(int min_frame_length_ms,
238*d9f75844SAndroid Build Coastguard Worker                                            int max_frame_length_ms);
239*d9f75844SAndroid Build Coastguard Worker 
240*d9f75844SAndroid Build Coastguard Worker   // Get statistics related to audio network adaptation.
241*d9f75844SAndroid Build Coastguard Worker   virtual ANAStats GetANAStats() const;
242*d9f75844SAndroid Build Coastguard Worker 
243*d9f75844SAndroid Build Coastguard Worker   // The range of frame lengths that are supported or nullopt if there's no sch
244*d9f75844SAndroid Build Coastguard Worker   // information. This is used to calculated the full bitrate range, including
245*d9f75844SAndroid Build Coastguard Worker   // overhead.
246*d9f75844SAndroid Build Coastguard Worker   virtual absl::optional<std::pair<TimeDelta, TimeDelta>> GetFrameLengthRange()
247*d9f75844SAndroid Build Coastguard Worker       const = 0;
248*d9f75844SAndroid Build Coastguard Worker 
249*d9f75844SAndroid Build Coastguard Worker   // The maximum number of audio channels supported by WebRTC encoders.
250*d9f75844SAndroid Build Coastguard Worker   static constexpr int kMaxNumberOfChannels = 24;
251*d9f75844SAndroid Build Coastguard Worker 
252*d9f75844SAndroid Build Coastguard Worker  protected:
253*d9f75844SAndroid Build Coastguard Worker   // Subclasses implement this to perform the actual encoding. Called by
254*d9f75844SAndroid Build Coastguard Worker   // Encode().
255*d9f75844SAndroid Build Coastguard Worker   virtual EncodedInfo EncodeImpl(uint32_t rtp_timestamp,
256*d9f75844SAndroid Build Coastguard Worker                                  rtc::ArrayView<const int16_t> audio,
257*d9f75844SAndroid Build Coastguard Worker                                  rtc::Buffer* encoded) = 0;
258*d9f75844SAndroid Build Coastguard Worker };
259*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
260*d9f75844SAndroid Build Coastguard Worker #endif  // API_AUDIO_CODECS_AUDIO_ENCODER_H_
261