henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [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_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_ |
| 13 | |
| 14 | #include <algorithm> |
henrik.lundin@webrtc.org | c1c9291 | 2014-12-16 13:41:36 +0000 | [diff] [blame] | 15 | #include <vector> |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 16 | |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 17 | #include "webrtc/base/array_view.h" |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 18 | #include "webrtc/base/buffer.h" |
| 19 | #include "webrtc/base/deprecation.h" |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 20 | #include "webrtc/typedefs.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 24 | class Clock; |
| 25 | |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 26 | // This is the interface class for encoders in AudioCoding module. Each codec |
henrik.lundin@webrtc.org | c1c9291 | 2014-12-16 13:41:36 +0000 | [diff] [blame] | 27 | // type must have an implementation of this class. |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 28 | class AudioEncoder { |
| 29 | public: |
aleloi | 8bce67b | 2016-05-16 07:34:24 -0700 | [diff] [blame] | 30 | // Used for UMA logging of codec usage. The same codecs, with the |
| 31 | // same values, must be listed in |
| 32 | // src/tools/metrics/histograms/histograms.xml in chromium to log |
| 33 | // correct values. |
| 34 | enum class CodecType { |
| 35 | kOther = 0, // Codec not specified, and/or not listed in this enum |
| 36 | kOpus = 1, |
| 37 | kIsac = 2, |
| 38 | kPcmA = 3, |
| 39 | kPcmU = 4, |
| 40 | kG722 = 5, |
| 41 | kIlbc = 6, |
| 42 | |
| 43 | // Number of histogram bins in the UMA logging of codec types. The |
| 44 | // total number of different codecs that are logged cannot exceed this |
| 45 | // number. |
| 46 | kMaxLoggedAudioCodecTypes |
| 47 | }; |
| 48 | |
henrik.lundin@webrtc.org | c1c9291 | 2014-12-16 13:41:36 +0000 | [diff] [blame] | 49 | struct EncodedInfoLeaf { |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 50 | size_t encoded_bytes = 0; |
| 51 | uint32_t encoded_timestamp = 0; |
| 52 | int payload_type = 0; |
| 53 | bool send_even_if_empty = false; |
| 54 | bool speech = true; |
aleloi | 8bce67b | 2016-05-16 07:34:24 -0700 | [diff] [blame] | 55 | CodecType encoder_type = CodecType::kOther; |
henrik.lundin@webrtc.org | 1db20a4 | 2014-12-01 14:44:50 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
henrik.lundin@webrtc.org | c1c9291 | 2014-12-16 13:41:36 +0000 | [diff] [blame] | 58 | // This is the main struct for auxiliary encoding information. Each encoded |
| 59 | // packet should be accompanied by one EncodedInfo struct, containing the |
| 60 | // total number of |encoded_bytes|, the |encoded_timestamp| and the |
| 61 | // |payload_type|. If the packet contains redundant encodings, the |redundant| |
| 62 | // vector will be populated with EncodedInfoLeaf structs. Each struct in the |
| 63 | // vector represents one encoding; the order of structs in the vector is the |
| 64 | // same as the order in which the actual payloads are written to the byte |
| 65 | // stream. When EncoderInfoLeaf structs are present in the vector, the main |
| 66 | // struct's |encoded_bytes| will be the sum of all the |encoded_bytes| in the |
| 67 | // vector. |
| 68 | struct EncodedInfo : public EncodedInfoLeaf { |
| 69 | EncodedInfo(); |
kjellander | 470dd37 | 2016-04-19 03:03:23 -0700 | [diff] [blame] | 70 | EncodedInfo(const EncodedInfo&); |
kwiberg | 4fb3d2b | 2016-04-22 04:59:31 -0700 | [diff] [blame] | 71 | EncodedInfo(EncodedInfo&&); |
henrik.lundin@webrtc.org | c1c9291 | 2014-12-16 13:41:36 +0000 | [diff] [blame] | 72 | ~EncodedInfo(); |
kwiberg | 4fb3d2b | 2016-04-22 04:59:31 -0700 | [diff] [blame] | 73 | EncodedInfo& operator=(const EncodedInfo&); |
| 74 | EncodedInfo& operator=(EncodedInfo&&); |
henrik.lundin@webrtc.org | c1c9291 | 2014-12-16 13:41:36 +0000 | [diff] [blame] | 75 | |
| 76 | std::vector<EncodedInfoLeaf> redundant; |
| 77 | }; |
| 78 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 79 | virtual ~AudioEncoder() = default; |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 80 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 81 | // Returns the input sample rate in Hz and the number of input channels. |
| 82 | // These are constants set at instantiation time. |
| 83 | virtual int SampleRateHz() const = 0; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 84 | virtual size_t NumChannels() const = 0; |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 85 | |
| 86 | // Returns the rate at which the RTP timestamps are updated. The default |
| 87 | // implementation returns SampleRateHz(). |
kwiberg@webrtc.org | 0521127 | 2015-02-18 12:00:32 +0000 | [diff] [blame] | 88 | virtual int RtpTimestampRateHz() const; |
henrik.lundin@webrtc.org | 478cedc | 2015-01-27 18:24:45 +0000 | [diff] [blame] | 89 | |
kwiberg@webrtc.org | decd930 | 2014-10-29 08:38:50 +0000 | [diff] [blame] | 90 | // Returns the number of 10 ms frames the encoder will put in the next |
| 91 | // packet. This value may only change when Encode() outputs a packet; i.e., |
| 92 | // the encoder may vary the number of 10 ms frames from packet to packet, but |
| 93 | // it must decide the length of the next packet no later than when outputting |
| 94 | // the preceding packet. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 95 | virtual size_t Num10MsFramesInNextPacket() const = 0; |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 96 | |
henrik.lundin@webrtc.org | 8911bc5 | 2014-12-08 21:15:55 +0000 | [diff] [blame] | 97 | // Returns the maximum value that can be returned by |
| 98 | // Num10MsFramesInNextPacket(). |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 99 | virtual size_t Max10MsFramesInAPacket() const = 0; |
henrik.lundin@webrtc.org | 8911bc5 | 2014-12-08 21:15:55 +0000 | [diff] [blame] | 100 | |
Henrik Lundin | 3e89dbf | 2015-06-18 14:58:34 +0200 | [diff] [blame] | 101 | // Returns the current target bitrate in bits/s. The value -1 means that the |
| 102 | // codec adapts the target automatically, and a current target cannot be |
| 103 | // provided. |
| 104 | virtual int GetTargetBitrate() const = 0; |
| 105 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 106 | // Accepts one 10 ms block of input audio (i.e., SampleRateHz() / 100 * |
| 107 | // NumChannels() samples). Multi-channel audio must be sample-interleaved. |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 108 | // The encoder appends zero or more bytes of output to |encoded| and returns |
| 109 | // additional encoding information. Encode() checks some preconditions, calls |
ossu | 4f43fcf | 2016-03-04 00:54:32 -0800 | [diff] [blame] | 110 | // EncodeImpl() which does the actual work, and then checks some |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 111 | // postconditions. |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 112 | EncodedInfo Encode(uint32_t rtp_timestamp, |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 113 | rtc::ArrayView<const int16_t> audio, |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 114 | rtc::Buffer* encoded); |
henrik.lundin@webrtc.org | 478cedc | 2015-01-27 18:24:45 +0000 | [diff] [blame] | 115 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 116 | // Resets the encoder to its starting state, discarding any input that has |
| 117 | // been fed to the encoder but not yet emitted in a packet. |
Karl Wiberg | dcccab3 | 2015-05-07 12:35:12 +0200 | [diff] [blame] | 118 | virtual void Reset() = 0; |
| 119 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 120 | // Enables or disables codec-internal FEC (forward error correction). Returns |
| 121 | // true if the codec was able to comply. The default implementation returns |
| 122 | // true when asked to disable FEC and false when asked to enable it (meaning |
| 123 | // that FEC isn't supported). |
| 124 | virtual bool SetFec(bool enable); |
Karl Wiberg | dcccab3 | 2015-05-07 12:35:12 +0200 | [diff] [blame] | 125 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 126 | // Enables or disables codec-internal VAD/DTX. Returns true if the codec was |
| 127 | // able to comply. The default implementation returns true when asked to |
| 128 | // disable DTX and false when asked to enable it (meaning that DTX isn't |
| 129 | // supported). |
| 130 | virtual bool SetDtx(bool enable); |
Karl Wiberg | dcccab3 | 2015-05-07 12:35:12 +0200 | [diff] [blame] | 131 | |
ivoc | 85228d6 | 2016-07-27 04:53:47 -0700 | [diff] [blame] | 132 | // Returns the status of codec-internal DTX. The default implementation always |
| 133 | // returns false. |
| 134 | virtual bool GetDtx() const; |
| 135 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 136 | // Sets the application mode. Returns true if the codec was able to comply. |
| 137 | // The default implementation just returns false. |
| 138 | enum class Application { kSpeech, kAudio }; |
| 139 | virtual bool SetApplication(Application application); |
Karl Wiberg | dcccab3 | 2015-05-07 12:35:12 +0200 | [diff] [blame] | 140 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 141 | // Tells the encoder about the highest sample rate the decoder is expected to |
| 142 | // use when decoding the bitstream. The encoder would typically use this |
| 143 | // information to adjust the quality of the encoding. The default |
kwiberg | 7eb914d | 2015-12-15 14:20:24 -0800 | [diff] [blame] | 144 | // implementation does nothing. |
kwiberg | 3f5f1c2 | 2015-09-08 23:15:33 -0700 | [diff] [blame] | 145 | virtual void SetMaxPlaybackRate(int frequency_hz); |
Karl Wiberg | dcccab3 | 2015-05-07 12:35:12 +0200 | [diff] [blame] | 146 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 147 | // Tells the encoder what the projected packet loss rate is. The rate is in |
| 148 | // the range [0.0, 1.0]. The encoder would typically use this information to |
| 149 | // adjust channel coding efforts, such as FEC. The default implementation |
| 150 | // does nothing. |
| 151 | virtual void SetProjectedPacketLossRate(double fraction); |
Karl Wiberg | dcccab3 | 2015-05-07 12:35:12 +0200 | [diff] [blame] | 152 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 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 | virtual void SetTargetBitrate(int target_bps); |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 157 | |
kwiberg | 3f81fcd | 2016-06-23 03:58:36 -0700 | [diff] [blame] | 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 | |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 167 | // Enables audio network adaptor. Returns true if successful. |
| 168 | virtual bool EnableAudioNetworkAdaptor(const std::string& config_string, |
| 169 | const Clock* clock); |
| 170 | |
| 171 | // Disables audio network adaptor. |
| 172 | virtual void DisableAudioNetworkAdaptor(); |
| 173 | |
| 174 | // Provides uplink bandwidth to this encoder to allow it to adapt. |
| 175 | virtual void OnReceivedUplinkBandwidth(int uplink_bandwidth_bps); |
| 176 | |
| 177 | // Provides uplink packet loss fraction to this encoder to allow it to adapt. |
| 178 | virtual void OnReceivedUplinkPacketLossFraction( |
| 179 | float uplink_packet_loss_fraction); |
| 180 | |
| 181 | // Provides target audio bitrate to this encoder to allow it to adapt. |
| 182 | virtual void OnReceivedTargetAudioBitrate(int target_audio_bitrate_bps); |
| 183 | |
| 184 | // Provides RTT to this encoder to allow it to adapt. |
| 185 | virtual void OnReceivedRtt(int rtt_ms); |
| 186 | |
| 187 | // To allow encoder to adapt its frame length, it must be provided the frame |
minyue | 6b825df | 2016-10-31 04:08:32 -0700 | [diff] [blame^] | 188 | // length range that receivers can accept. |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 189 | virtual void SetReceiverFrameLengthRange(int min_frame_length_ms, |
| 190 | int max_frame_length_ms); |
| 191 | |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 192 | protected: |
| 193 | // Subclasses implement this to perform the actual encoding. Called by |
ossu | 2903ba5 | 2016-04-18 06:14:33 -0700 | [diff] [blame] | 194 | // Encode(). |
ossu | 4f43fcf | 2016-03-04 00:54:32 -0800 | [diff] [blame] | 195 | virtual EncodedInfo EncodeImpl(uint32_t rtp_timestamp, |
| 196 | rtc::ArrayView<const int16_t> audio, |
ossu | 2903ba5 | 2016-04-18 06:14:33 -0700 | [diff] [blame] | 197 | rtc::Buffer* encoded) = 0; |
Karl Wiberg | dcccab3 | 2015-05-07 12:35:12 +0200 | [diff] [blame] | 198 | }; |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 199 | } // namespace webrtc |
| 200 | #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_ |