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