blob: e1b3d82a5b111d23dd6d81ec5b63b6dccbdf7829 [file] [log] [blame]
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +00001/*
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.orgc1c92912014-12-16 13:41:36 +000015#include <vector>
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000016
kwiberg288886b2015-11-06 01:21:35 -080017#include "webrtc/base/array_view.h"
ossu10a029e2016-03-01 00:41:31 -080018#include "webrtc/base/buffer.h"
19#include "webrtc/base/deprecation.h"
michaelt566d8202017-01-12 10:17:38 -080020#include "webrtc/base/optional.h"
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000021#include "webrtc/typedefs.h"
22
23namespace webrtc {
24
minyue41b9c802016-10-06 07:13:54 -070025class Clock;
26
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000027// This is the interface class for encoders in AudioCoding module. Each codec
henrik.lundin@webrtc.orgc1c92912014-12-16 13:41:36 +000028// type must have an implementation of this class.
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000029class AudioEncoder {
30 public:
aleloi8bce67b2016-05-16 07:34:24 -070031 // Used for UMA logging of codec usage. The same codecs, with the
32 // same values, must be listed in
33 // src/tools/metrics/histograms/histograms.xml in chromium to log
34 // correct values.
35 enum class CodecType {
36 kOther = 0, // Codec not specified, and/or not listed in this enum
37 kOpus = 1,
38 kIsac = 2,
39 kPcmA = 3,
40 kPcmU = 4,
41 kG722 = 5,
42 kIlbc = 6,
43
44 // Number of histogram bins in the UMA logging of codec types. The
45 // total number of different codecs that are logged cannot exceed this
46 // number.
47 kMaxLoggedAudioCodecTypes
48 };
49
henrik.lundin@webrtc.orgc1c92912014-12-16 13:41:36 +000050 struct EncodedInfoLeaf {
kwiberg12cfc9b2015-09-08 05:57:53 -070051 size_t encoded_bytes = 0;
52 uint32_t encoded_timestamp = 0;
53 int payload_type = 0;
54 bool send_even_if_empty = false;
55 bool speech = true;
aleloi8bce67b2016-05-16 07:34:24 -070056 CodecType encoder_type = CodecType::kOther;
henrik.lundin@webrtc.org1db20a42014-12-01 14:44:50 +000057 };
58
henrik.lundin@webrtc.orgc1c92912014-12-16 13:41:36 +000059 // This is the main struct for auxiliary encoding information. Each encoded
60 // packet should be accompanied by one EncodedInfo struct, containing the
61 // total number of |encoded_bytes|, the |encoded_timestamp| and the
62 // |payload_type|. If the packet contains redundant encodings, the |redundant|
63 // vector will be populated with EncodedInfoLeaf structs. Each struct in the
64 // vector represents one encoding; the order of structs in the vector is the
65 // same as the order in which the actual payloads are written to the byte
66 // stream. When EncoderInfoLeaf structs are present in the vector, the main
67 // struct's |encoded_bytes| will be the sum of all the |encoded_bytes| in the
68 // vector.
69 struct EncodedInfo : public EncodedInfoLeaf {
70 EncodedInfo();
kjellander470dd372016-04-19 03:03:23 -070071 EncodedInfo(const EncodedInfo&);
kwiberg4fb3d2b2016-04-22 04:59:31 -070072 EncodedInfo(EncodedInfo&&);
henrik.lundin@webrtc.orgc1c92912014-12-16 13:41:36 +000073 ~EncodedInfo();
kwiberg4fb3d2b2016-04-22 04:59:31 -070074 EncodedInfo& operator=(const EncodedInfo&);
75 EncodedInfo& operator=(EncodedInfo&&);
henrik.lundin@webrtc.orgc1c92912014-12-16 13:41:36 +000076
77 std::vector<EncodedInfoLeaf> redundant;
78 };
79
kwiberg12cfc9b2015-09-08 05:57:53 -070080 virtual ~AudioEncoder() = default;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000081
kwiberg12cfc9b2015-09-08 05:57:53 -070082 // Returns the input sample rate in Hz and the number of input channels.
83 // These are constants set at instantiation time.
84 virtual int SampleRateHz() const = 0;
Peter Kasting69558702016-01-12 16:26:35 -080085 virtual size_t NumChannels() const = 0;
kwiberg12cfc9b2015-09-08 05:57:53 -070086
87 // Returns the rate at which the RTP timestamps are updated. The default
88 // implementation returns SampleRateHz().
kwiberg@webrtc.org05211272015-02-18 12:00:32 +000089 virtual int RtpTimestampRateHz() const;
henrik.lundin@webrtc.org478cedc2015-01-27 18:24:45 +000090
kwiberg@webrtc.orgdecd9302014-10-29 08:38:50 +000091 // Returns the number of 10 ms frames the encoder will put in the next
92 // packet. This value may only change when Encode() outputs a packet; i.e.,
93 // the encoder may vary the number of 10 ms frames from packet to packet, but
94 // it must decide the length of the next packet no later than when outputting
95 // the preceding packet.
Peter Kastingdce40cf2015-08-24 14:52:23 -070096 virtual size_t Num10MsFramesInNextPacket() const = 0;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000097
henrik.lundin@webrtc.org8911bc52014-12-08 21:15:55 +000098 // Returns the maximum value that can be returned by
99 // Num10MsFramesInNextPacket().
Peter Kastingdce40cf2015-08-24 14:52:23 -0700100 virtual size_t Max10MsFramesInAPacket() const = 0;
henrik.lundin@webrtc.org8911bc52014-12-08 21:15:55 +0000101
Henrik Lundin3e89dbf2015-06-18 14:58:34 +0200102 // Returns the current target bitrate in bits/s. The value -1 means that the
103 // codec adapts the target automatically, and a current target cannot be
104 // provided.
105 virtual int GetTargetBitrate() const = 0;
106
kwiberg12cfc9b2015-09-08 05:57:53 -0700107 // Accepts one 10 ms block of input audio (i.e., SampleRateHz() / 100 *
108 // NumChannels() samples). Multi-channel audio must be sample-interleaved.
ossu10a029e2016-03-01 00:41:31 -0800109 // The encoder appends zero or more bytes of output to |encoded| and returns
110 // additional encoding information. Encode() checks some preconditions, calls
ossu4f43fcf2016-03-04 00:54:32 -0800111 // EncodeImpl() which does the actual work, and then checks some
ossu10a029e2016-03-01 00:41:31 -0800112 // postconditions.
kwiberg12cfc9b2015-09-08 05:57:53 -0700113 EncodedInfo Encode(uint32_t rtp_timestamp,
kwiberg288886b2015-11-06 01:21:35 -0800114 rtc::ArrayView<const int16_t> audio,
ossu10a029e2016-03-01 00:41:31 -0800115 rtc::Buffer* encoded);
henrik.lundin@webrtc.org478cedc2015-01-27 18:24:45 +0000116
kwiberg12cfc9b2015-09-08 05:57:53 -0700117 // Resets the encoder to its starting state, discarding any input that has
118 // been fed to the encoder but not yet emitted in a packet.
Karl Wibergdcccab32015-05-07 12:35:12 +0200119 virtual void Reset() = 0;
120
kwiberg12cfc9b2015-09-08 05:57:53 -0700121 // Enables or disables codec-internal FEC (forward error correction). Returns
122 // true if the codec was able to comply. The default implementation returns
123 // true when asked to disable FEC and false when asked to enable it (meaning
124 // that FEC isn't supported).
125 virtual bool SetFec(bool enable);
Karl Wibergdcccab32015-05-07 12:35:12 +0200126
kwiberg12cfc9b2015-09-08 05:57:53 -0700127 // Enables or disables codec-internal VAD/DTX. Returns true if the codec was
128 // able to comply. The default implementation returns true when asked to
129 // disable DTX and false when asked to enable it (meaning that DTX isn't
130 // supported).
131 virtual bool SetDtx(bool enable);
Karl Wibergdcccab32015-05-07 12:35:12 +0200132
ivoc85228d62016-07-27 04:53:47 -0700133 // Returns the status of codec-internal DTX. The default implementation always
134 // returns false.
135 virtual bool GetDtx() const;
136
kwiberg12cfc9b2015-09-08 05:57:53 -0700137 // Sets the application mode. Returns true if the codec was able to comply.
138 // The default implementation just returns false.
139 enum class Application { kSpeech, kAudio };
140 virtual bool SetApplication(Application application);
Karl Wibergdcccab32015-05-07 12:35:12 +0200141
kwiberg12cfc9b2015-09-08 05:57:53 -0700142 // Tells the encoder about the highest sample rate the decoder is expected to
143 // use when decoding the bitstream. The encoder would typically use this
144 // information to adjust the quality of the encoding. The default
kwiberg7eb914d2015-12-15 14:20:24 -0800145 // implementation does nothing.
kwiberg3f5f1c22015-09-08 23:15:33 -0700146 virtual void SetMaxPlaybackRate(int frequency_hz);
Karl Wibergdcccab32015-05-07 12:35:12 +0200147
minyue4b9a2cb2016-11-30 06:49:59 -0800148 // This is to be deprecated. Please use |OnReceivedTargetAudioBitrate|
149 // instead.
minyuee69b4682016-11-30 01:18:58 -0800150 // Tells the encoder what average bitrate we'd like it to produce. The
151 // encoder is free to adjust or disregard the given bitrate (the default
152 // implementation does the latter).
minyue4b9a2cb2016-11-30 06:49:59 -0800153 RTC_DEPRECATED virtual void SetTargetBitrate(int target_bps);
minyuee69b4682016-11-30 01:18:58 -0800154
kwiberg3f81fcd2016-06-23 03:58:36 -0700155 // Causes this encoder to let go of any other encoders it contains, and
156 // returns a pointer to an array where they are stored (which is required to
157 // live as long as this encoder). Unless the returned array is empty, you may
158 // not call any methods on this encoder afterwards, except for the
159 // destructor. The default implementation just returns an empty array.
160 // NOTE: This method is subject to change. Do not call or override it.
161 virtual rtc::ArrayView<std::unique_ptr<AudioEncoder>>
162 ReclaimContainedEncoders();
163
minyue41b9c802016-10-06 07:13:54 -0700164 // Enables audio network adaptor. Returns true if successful.
165 virtual bool EnableAudioNetworkAdaptor(const std::string& config_string,
166 const Clock* clock);
167
168 // Disables audio network adaptor.
169 virtual void DisableAudioNetworkAdaptor();
170
minyue41b9c802016-10-06 07:13:54 -0700171 // Provides uplink packet loss fraction to this encoder to allow it to adapt.
minyue4b9a2cb2016-11-30 06:49:59 -0800172 // |uplink_packet_loss_fraction| is in the range [0.0, 1.0].
minyue41b9c802016-10-06 07:13:54 -0700173 virtual void OnReceivedUplinkPacketLossFraction(
174 float uplink_packet_loss_fraction);
175
176 // Provides target audio bitrate to this encoder to allow it to adapt.
michaelt566d8202017-01-12 10:17:38 -0800177 virtual void OnReceivedTargetAudioBitrate(int target_bps);
178
179 // Provides target audio bitrate and corresponding probing interval of
180 // the bandwidth estimator to this encoder to allow it to adapt.
181 virtual void OnReceivedUplinkBandwidth(
182 int target_audio_bitrate_bps,
183 rtc::Optional<int64_t> probing_interval_ms);
minyue41b9c802016-10-06 07:13:54 -0700184
185 // Provides RTT to this encoder to allow it to adapt.
186 virtual void OnReceivedRtt(int rtt_ms);
187
minyueeca373f2016-12-07 01:40:34 -0800188 // Provides overhead to this encoder to adapt. The overhead is the number of
189 // bytes that will be added to each packet the encoder generates.
190 virtual void OnReceivedOverhead(size_t overhead_bytes_per_packet);
191
minyue41b9c802016-10-06 07:13:54 -0700192 // To allow encoder to adapt its frame length, it must be provided the frame
minyue6b825df2016-10-31 04:08:32 -0700193 // length range that receivers can accept.
minyue41b9c802016-10-06 07:13:54 -0700194 virtual void SetReceiverFrameLengthRange(int min_frame_length_ms,
195 int max_frame_length_ms);
196
ossu10a029e2016-03-01 00:41:31 -0800197 protected:
198 // Subclasses implement this to perform the actual encoding. Called by
ossu2903ba52016-04-18 06:14:33 -0700199 // Encode().
ossu4f43fcf2016-03-04 00:54:32 -0800200 virtual EncodedInfo EncodeImpl(uint32_t rtp_timestamp,
201 rtc::ArrayView<const int16_t> audio,
ossu2903ba52016-04-18 06:14:33 -0700202 rtc::Buffer* encoded) = 0;
Karl Wibergdcccab32015-05-07 12:35:12 +0200203};
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +0000204} // namespace webrtc
205#endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_