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