blob: ecc28d96a16239de05e39f04b3f41676f2d3f380 [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"
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000020#include "webrtc/typedefs.h"
21
22namespace webrtc {
23
24// This is the interface class for encoders in AudioCoding module. Each codec
henrik.lundin@webrtc.orgc1c92912014-12-16 13:41:36 +000025// type must have an implementation of this class.
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000026class AudioEncoder {
27 public:
aleloi8bce67b2016-05-16 07:34:24 -070028 // Used for UMA logging of codec usage. The same codecs, with the
29 // same values, must be listed in
30 // src/tools/metrics/histograms/histograms.xml in chromium to log
31 // correct values.
32 enum class CodecType {
33 kOther = 0, // Codec not specified, and/or not listed in this enum
34 kOpus = 1,
35 kIsac = 2,
36 kPcmA = 3,
37 kPcmU = 4,
38 kG722 = 5,
39 kIlbc = 6,
40
41 // Number of histogram bins in the UMA logging of codec types. The
42 // total number of different codecs that are logged cannot exceed this
43 // number.
44 kMaxLoggedAudioCodecTypes
45 };
46
henrik.lundin@webrtc.orgc1c92912014-12-16 13:41:36 +000047 struct EncodedInfoLeaf {
kwiberg12cfc9b2015-09-08 05:57:53 -070048 size_t encoded_bytes = 0;
49 uint32_t encoded_timestamp = 0;
50 int payload_type = 0;
51 bool send_even_if_empty = false;
52 bool speech = true;
aleloi8bce67b2016-05-16 07:34:24 -070053 CodecType encoder_type = CodecType::kOther;
henrik.lundin@webrtc.org1db20a42014-12-01 14:44:50 +000054 };
55
henrik.lundin@webrtc.orgc1c92912014-12-16 13:41:36 +000056 // This is the main struct for auxiliary encoding information. Each encoded
57 // packet should be accompanied by one EncodedInfo struct, containing the
58 // total number of |encoded_bytes|, the |encoded_timestamp| and the
59 // |payload_type|. If the packet contains redundant encodings, the |redundant|
60 // vector will be populated with EncodedInfoLeaf structs. Each struct in the
61 // vector represents one encoding; the order of structs in the vector is the
62 // same as the order in which the actual payloads are written to the byte
63 // stream. When EncoderInfoLeaf structs are present in the vector, the main
64 // struct's |encoded_bytes| will be the sum of all the |encoded_bytes| in the
65 // vector.
66 struct EncodedInfo : public EncodedInfoLeaf {
67 EncodedInfo();
kjellander470dd372016-04-19 03:03:23 -070068 EncodedInfo(const EncodedInfo&);
kwiberg4fb3d2b2016-04-22 04:59:31 -070069 EncodedInfo(EncodedInfo&&);
henrik.lundin@webrtc.orgc1c92912014-12-16 13:41:36 +000070 ~EncodedInfo();
kwiberg4fb3d2b2016-04-22 04:59:31 -070071 EncodedInfo& operator=(const EncodedInfo&);
72 EncodedInfo& operator=(EncodedInfo&&);
henrik.lundin@webrtc.orgc1c92912014-12-16 13:41:36 +000073
74 std::vector<EncodedInfoLeaf> redundant;
75 };
76
kwiberg12cfc9b2015-09-08 05:57:53 -070077 virtual ~AudioEncoder() = default;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000078
kwiberg12cfc9b2015-09-08 05:57:53 -070079 // Returns the input sample rate in Hz and the number of input channels.
80 // These are constants set at instantiation time.
81 virtual int SampleRateHz() const = 0;
Peter Kasting69558702016-01-12 16:26:35 -080082 virtual size_t NumChannels() const = 0;
kwiberg12cfc9b2015-09-08 05:57:53 -070083
84 // Returns the rate at which the RTP timestamps are updated. The default
85 // implementation returns SampleRateHz().
kwiberg@webrtc.org05211272015-02-18 12:00:32 +000086 virtual int RtpTimestampRateHz() const;
henrik.lundin@webrtc.org478cedc2015-01-27 18:24:45 +000087
kwiberg@webrtc.orgdecd9302014-10-29 08:38:50 +000088 // Returns the number of 10 ms frames the encoder will put in the next
89 // packet. This value may only change when Encode() outputs a packet; i.e.,
90 // the encoder may vary the number of 10 ms frames from packet to packet, but
91 // it must decide the length of the next packet no later than when outputting
92 // the preceding packet.
Peter Kastingdce40cf2015-08-24 14:52:23 -070093 virtual size_t Num10MsFramesInNextPacket() const = 0;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000094
henrik.lundin@webrtc.org8911bc52014-12-08 21:15:55 +000095 // Returns the maximum value that can be returned by
96 // Num10MsFramesInNextPacket().
Peter Kastingdce40cf2015-08-24 14:52:23 -070097 virtual size_t Max10MsFramesInAPacket() const = 0;
henrik.lundin@webrtc.org8911bc52014-12-08 21:15:55 +000098
Henrik Lundin3e89dbf2015-06-18 14:58:34 +020099 // Returns the current target bitrate in bits/s. The value -1 means that the
100 // codec adapts the target automatically, and a current target cannot be
101 // provided.
102 virtual int GetTargetBitrate() const = 0;
103
kwiberg12cfc9b2015-09-08 05:57:53 -0700104 // Accepts one 10 ms block of input audio (i.e., SampleRateHz() / 100 *
105 // NumChannels() samples). Multi-channel audio must be sample-interleaved.
ossu10a029e2016-03-01 00:41:31 -0800106 // The encoder appends zero or more bytes of output to |encoded| and returns
107 // additional encoding information. Encode() checks some preconditions, calls
ossu4f43fcf2016-03-04 00:54:32 -0800108 // EncodeImpl() which does the actual work, and then checks some
ossu10a029e2016-03-01 00:41:31 -0800109 // postconditions.
kwiberg12cfc9b2015-09-08 05:57:53 -0700110 EncodedInfo Encode(uint32_t rtp_timestamp,
kwiberg288886b2015-11-06 01:21:35 -0800111 rtc::ArrayView<const int16_t> audio,
ossu10a029e2016-03-01 00:41:31 -0800112 rtc::Buffer* encoded);
henrik.lundin@webrtc.org478cedc2015-01-27 18:24:45 +0000113
kwiberg12cfc9b2015-09-08 05:57:53 -0700114 // Resets the encoder to its starting state, discarding any input that has
115 // been fed to the encoder but not yet emitted in a packet.
Karl Wibergdcccab32015-05-07 12:35:12 +0200116 virtual void Reset() = 0;
117
kwiberg12cfc9b2015-09-08 05:57:53 -0700118 // Enables or disables codec-internal FEC (forward error correction). Returns
119 // true if the codec was able to comply. The default implementation returns
120 // true when asked to disable FEC and false when asked to enable it (meaning
121 // that FEC isn't supported).
122 virtual bool SetFec(bool enable);
Karl Wibergdcccab32015-05-07 12:35:12 +0200123
kwiberg12cfc9b2015-09-08 05:57:53 -0700124 // Enables or disables codec-internal VAD/DTX. Returns true if the codec was
125 // able to comply. The default implementation returns true when asked to
126 // disable DTX and false when asked to enable it (meaning that DTX isn't
127 // supported).
128 virtual bool SetDtx(bool enable);
Karl Wibergdcccab32015-05-07 12:35:12 +0200129
kwiberg12cfc9b2015-09-08 05:57:53 -0700130 // Sets the application mode. Returns true if the codec was able to comply.
131 // The default implementation just returns false.
132 enum class Application { kSpeech, kAudio };
133 virtual bool SetApplication(Application application);
Karl Wibergdcccab32015-05-07 12:35:12 +0200134
kwiberg12cfc9b2015-09-08 05:57:53 -0700135 // Tells the encoder about the highest sample rate the decoder is expected to
136 // use when decoding the bitstream. The encoder would typically use this
137 // information to adjust the quality of the encoding. The default
kwiberg7eb914d2015-12-15 14:20:24 -0800138 // implementation does nothing.
kwiberg3f5f1c22015-09-08 23:15:33 -0700139 virtual void SetMaxPlaybackRate(int frequency_hz);
Karl Wibergdcccab32015-05-07 12:35:12 +0200140
kwiberg12cfc9b2015-09-08 05:57:53 -0700141 // Tells the encoder what the projected packet loss rate is. The rate is in
142 // the range [0.0, 1.0]. The encoder would typically use this information to
143 // adjust channel coding efforts, such as FEC. The default implementation
144 // does nothing.
145 virtual void SetProjectedPacketLossRate(double fraction);
Karl Wibergdcccab32015-05-07 12:35:12 +0200146
kwiberg12cfc9b2015-09-08 05:57:53 -0700147 // Tells the encoder what average bitrate we'd like it to produce. The
148 // encoder is free to adjust or disregard the given bitrate (the default
149 // implementation does the latter).
150 virtual void SetTargetBitrate(int target_bps);
ossu10a029e2016-03-01 00:41:31 -0800151
kwiberg3f81fcd2016-06-23 03:58:36 -0700152 // Causes this encoder to let go of any other encoders it contains, and
153 // returns a pointer to an array where they are stored (which is required to
154 // live as long as this encoder). Unless the returned array is empty, you may
155 // not call any methods on this encoder afterwards, except for the
156 // destructor. The default implementation just returns an empty array.
157 // NOTE: This method is subject to change. Do not call or override it.
158 virtual rtc::ArrayView<std::unique_ptr<AudioEncoder>>
159 ReclaimContainedEncoders();
160
ossu10a029e2016-03-01 00:41:31 -0800161 protected:
162 // Subclasses implement this to perform the actual encoding. Called by
ossu2903ba52016-04-18 06:14:33 -0700163 // Encode().
ossu4f43fcf2016-03-04 00:54:32 -0800164 virtual EncodedInfo EncodeImpl(uint32_t rtp_timestamp,
165 rtc::ArrayView<const int16_t> audio,
ossu2903ba52016-04-18 06:14:33 -0700166 rtc::Buffer* encoded) = 0;
Karl Wibergdcccab32015-05-07 12:35:12 +0200167};
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +0000168} // namespace webrtc
169#endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_