blob: 6a1f6534b21a8a3ffb6d209d95571c89ae208a16 [file] [log] [blame]
ossueb1fde42017-05-02 06:46:30 -07001/*
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 06:47:31 +020011#ifndef API_AUDIO_CODECS_AUDIO_ENCODER_H_
12#define API_AUDIO_CODECS_AUDIO_ENCODER_H_
ossueb1fde42017-05-02 06:46:30 -070013
14#include <algorithm>
15#include <memory>
16#include <string>
17#include <vector>
18
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020019#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/buffer.h"
22#include "rtc_base/deprecation.h"
ossueb1fde42017-05-02 06:46:30 -070023
24namespace webrtc {
25
ossueb1fde42017-05-02 06:46:30 -070026class RtcEventLog;
27
ivoce1198e02017-09-08 08:13:19 -070028// Statistics related to Audio Network Adaptation.
29struct ANAStats {
30 ANAStats();
31 ANAStats(const ANAStats&);
32 ~ANAStats();
33 // Number of actions taken by the ANA bitrate controller since the start of
34 // the call. If this value is not set, it indicates that the bitrate
35 // controller is disabled.
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020036 absl::optional<uint32_t> bitrate_action_counter;
ivoce1198e02017-09-08 08:13:19 -070037 // Number of actions taken by the ANA channel controller since the start of
38 // the call. If this value is not set, it indicates that the channel
39 // controller is disabled.
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020040 absl::optional<uint32_t> channel_action_counter;
ivoce1198e02017-09-08 08:13:19 -070041 // Number of actions taken by the ANA DTX controller since the start of the
42 // call. If this value is not set, it indicates that the DTX controller is
43 // disabled.
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020044 absl::optional<uint32_t> dtx_action_counter;
ivoce1198e02017-09-08 08:13:19 -070045 // Number of actions taken by the ANA FEC controller since the start of the
46 // call. If this value is not set, it indicates that the FEC controller is
47 // disabled.
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020048 absl::optional<uint32_t> fec_action_counter;
ivoc0d0b9122017-09-08 13:24:21 -070049 // Number of times the ANA frame length controller decided to increase the
50 // frame length since the start of the call. If this value is not set, it
51 // indicates that the frame length controller is disabled.
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020052 absl::optional<uint32_t> frame_length_increase_counter;
ivoc0d0b9122017-09-08 13:24:21 -070053 // Number of times the ANA frame length controller decided to decrease the
54 // frame length since the start of the call. If this value is not set, it
55 // indicates that the frame length controller is disabled.
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020056 absl::optional<uint32_t> frame_length_decrease_counter;
ivoc0d0b9122017-09-08 13:24:21 -070057 // The uplink packet loss fractions as set by the ANA FEC controller. If this
58 // value is not set, it indicates that the ANA FEC controller is not active.
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020059 absl::optional<float> uplink_packet_loss_fraction;
ivoce1198e02017-09-08 08:13:19 -070060};
61
ossueb1fde42017-05-02 06:46:30 -070062// This is the interface class for encoders in AudioCoding module. Each codec
63// type must have an implementation of this class.
64class AudioEncoder {
65 public:
66 // Used for UMA logging of codec usage. The same codecs, with the
67 // same values, must be listed in
68 // src/tools/metrics/histograms/histograms.xml in chromium to log
69 // correct values.
70 enum class CodecType {
71 kOther = 0, // Codec not specified, and/or not listed in this enum
72 kOpus = 1,
73 kIsac = 2,
74 kPcmA = 3,
75 kPcmU = 4,
76 kG722 = 5,
77 kIlbc = 6,
78
79 // Number of histogram bins in the UMA logging of codec types. The
80 // total number of different codecs that are logged cannot exceed this
81 // number.
82 kMaxLoggedAudioCodecTypes
83 };
84
85 struct EncodedInfoLeaf {
86 size_t encoded_bytes = 0;
87 uint32_t encoded_timestamp = 0;
88 int payload_type = 0;
89 bool send_even_if_empty = false;
90 bool speech = true;
91 CodecType encoder_type = CodecType::kOther;
92 };
93
94 // This is the main struct for auxiliary encoding information. Each encoded
95 // packet should be accompanied by one EncodedInfo struct, containing the
96 // total number of |encoded_bytes|, the |encoded_timestamp| and the
97 // |payload_type|. If the packet contains redundant encodings, the |redundant|
98 // vector will be populated with EncodedInfoLeaf structs. Each struct in the
99 // vector represents one encoding; the order of structs in the vector is the
100 // same as the order in which the actual payloads are written to the byte
101 // stream. When EncoderInfoLeaf structs are present in the vector, the main
102 // struct's |encoded_bytes| will be the sum of all the |encoded_bytes| in the
103 // vector.
104 struct EncodedInfo : public EncodedInfoLeaf {
105 EncodedInfo();
106 EncodedInfo(const EncodedInfo&);
107 EncodedInfo(EncodedInfo&&);
108 ~EncodedInfo();
109 EncodedInfo& operator=(const EncodedInfo&);
110 EncodedInfo& operator=(EncodedInfo&&);
111
112 std::vector<EncodedInfoLeaf> redundant;
113 };
114
115 virtual ~AudioEncoder() = default;
116
117 // Returns the input sample rate in Hz and the number of input channels.
118 // These are constants set at instantiation time.
119 virtual int SampleRateHz() const = 0;
120 virtual size_t NumChannels() const = 0;
121
122 // Returns the rate at which the RTP timestamps are updated. The default
123 // implementation returns SampleRateHz().
124 virtual int RtpTimestampRateHz() const;
125
126 // Returns the number of 10 ms frames the encoder will put in the next
127 // packet. This value may only change when Encode() outputs a packet; i.e.,
128 // the encoder may vary the number of 10 ms frames from packet to packet, but
129 // it must decide the length of the next packet no later than when outputting
130 // the preceding packet.
131 virtual size_t Num10MsFramesInNextPacket() const = 0;
132
133 // Returns the maximum value that can be returned by
134 // Num10MsFramesInNextPacket().
135 virtual size_t Max10MsFramesInAPacket() const = 0;
136
137 // Returns the current target bitrate in bits/s. The value -1 means that the
138 // codec adapts the target automatically, and a current target cannot be
139 // provided.
140 virtual int GetTargetBitrate() const = 0;
141
142 // Accepts one 10 ms block of input audio (i.e., SampleRateHz() / 100 *
143 // NumChannels() samples). Multi-channel audio must be sample-interleaved.
144 // The encoder appends zero or more bytes of output to |encoded| and returns
145 // additional encoding information. Encode() checks some preconditions, calls
146 // EncodeImpl() which does the actual work, and then checks some
147 // postconditions.
148 EncodedInfo Encode(uint32_t rtp_timestamp,
149 rtc::ArrayView<const int16_t> audio,
150 rtc::Buffer* encoded);
151
152 // Resets the encoder to its starting state, discarding any input that has
153 // been fed to the encoder but not yet emitted in a packet.
154 virtual void Reset() = 0;
155
156 // Enables or disables codec-internal FEC (forward error correction). Returns
157 // true if the codec was able to comply. The default implementation returns
158 // true when asked to disable FEC and false when asked to enable it (meaning
159 // that FEC isn't supported).
160 virtual bool SetFec(bool enable);
161
162 // Enables or disables codec-internal VAD/DTX. Returns true if the codec was
163 // able to comply. The default implementation returns true when asked to
164 // disable DTX and false when asked to enable it (meaning that DTX isn't
165 // supported).
166 virtual bool SetDtx(bool enable);
167
168 // Returns the status of codec-internal DTX. The default implementation always
169 // returns false.
170 virtual bool GetDtx() const;
171
172 // Sets the application mode. Returns true if the codec was able to comply.
173 // The default implementation just returns false.
174 enum class Application { kSpeech, kAudio };
175 virtual bool SetApplication(Application application);
176
177 // Tells the encoder about the highest sample rate the decoder is expected to
178 // use when decoding the bitstream. The encoder would typically use this
179 // information to adjust the quality of the encoding. The default
180 // implementation does nothing.
181 virtual void SetMaxPlaybackRate(int frequency_hz);
182
183 // This is to be deprecated. Please use |OnReceivedTargetAudioBitrate|
184 // instead.
185 // Tells the encoder what average bitrate we'd like it to produce. The
186 // encoder is free to adjust or disregard the given bitrate (the default
187 // implementation does the latter).
188 RTC_DEPRECATED virtual void SetTargetBitrate(int target_bps);
189
190 // Causes this encoder to let go of any other encoders it contains, and
191 // returns a pointer to an array where they are stored (which is required to
192 // live as long as this encoder). Unless the returned array is empty, you may
193 // not call any methods on this encoder afterwards, except for the
194 // destructor. The default implementation just returns an empty array.
195 // NOTE: This method is subject to change. Do not call or override it.
196 virtual rtc::ArrayView<std::unique_ptr<AudioEncoder>>
197 ReclaimContainedEncoders();
198
199 // Enables audio network adaptor. Returns true if successful.
200 virtual bool EnableAudioNetworkAdaptor(const std::string& config_string,
201 RtcEventLog* event_log);
202
203 // Disables audio network adaptor.
204 virtual void DisableAudioNetworkAdaptor();
205
206 // Provides uplink packet loss fraction to this encoder to allow it to adapt.
207 // |uplink_packet_loss_fraction| is in the range [0.0, 1.0].
208 virtual void OnReceivedUplinkPacketLossFraction(
209 float uplink_packet_loss_fraction);
210
211 // Provides 1st-order-FEC-recoverable uplink packet loss rate to this encoder
212 // to allow it to adapt.
213 // |uplink_recoverable_packet_loss_fraction| is in the range [0.0, 1.0].
214 virtual void OnReceivedUplinkRecoverablePacketLossFraction(
215 float uplink_recoverable_packet_loss_fraction);
216
217 // Provides target audio bitrate to this encoder to allow it to adapt.
218 virtual void OnReceivedTargetAudioBitrate(int target_bps);
219
220 // Provides target audio bitrate and corresponding probing interval of
221 // the bandwidth estimator to this encoder to allow it to adapt.
Yves Gerey665174f2018-06-19 15:03:05 +0200222 virtual void OnReceivedUplinkBandwidth(int target_audio_bitrate_bps,
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +0200223 absl::optional<int64_t> bwe_period_ms);
ossueb1fde42017-05-02 06:46:30 -0700224
225 // Provides RTT to this encoder to allow it to adapt.
226 virtual void OnReceivedRtt(int rtt_ms);
227
228 // Provides overhead to this encoder to adapt. The overhead is the number of
229 // bytes that will be added to each packet the encoder generates.
230 virtual void OnReceivedOverhead(size_t overhead_bytes_per_packet);
231
232 // To allow encoder to adapt its frame length, it must be provided the frame
233 // length range that receivers can accept.
234 virtual void SetReceiverFrameLengthRange(int min_frame_length_ms,
235 int max_frame_length_ms);
236
ivoce1198e02017-09-08 08:13:19 -0700237 // Get statistics related to audio network adaptation.
238 virtual ANAStats GetANAStats() const;
239
ossueb1fde42017-05-02 06:46:30 -0700240 protected:
241 // Subclasses implement this to perform the actual encoding. Called by
242 // Encode().
243 virtual EncodedInfo EncodeImpl(uint32_t rtp_timestamp,
244 rtc::ArrayView<const int16_t> audio,
245 rtc::Buffer* encoded) = 0;
246};
247} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200248#endif // API_AUDIO_CODECS_AUDIO_ENCODER_H_