kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_CODING_ACM2_RENT_A_CODEC_H_ |
| 12 | #define MODULES_AUDIO_CODING_ACM2_RENT_A_CODEC_H_ |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 13 | |
| 14 | #include <stddef.h> |
kwiberg | e1a27d4 | 2015-11-18 07:32:49 -0800 | [diff] [blame] | 15 | #include <map> |
kwiberg | 16c5a96 | 2016-02-15 02:27:22 -0800 | [diff] [blame] | 16 | #include <memory> |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 17 | |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 18 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "api/array_view.h" |
| 20 | #include "api/audio_codecs/audio_decoder.h" |
| 21 | #include "api/audio_codecs/audio_encoder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "modules/audio_coding/include/audio_coding_module_typedefs.h" |
| 23 | #include "modules/audio_coding/neteq/neteq_decoder_enum.h" |
| 24 | #include "rtc_base/constructormagic.h" |
| 25 | #include "rtc_base/scoped_ref_ptr.h" |
kwiberg | 805fc71 | 2015-11-10 04:05:16 -0800 | [diff] [blame] | 26 | |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 27 | namespace webrtc { |
| 28 | |
| 29 | struct CodecInst; |
kwiberg | 0d05da7 | 2016-03-30 04:10:11 -0700 | [diff] [blame] | 30 | class LockedIsacBandwidthInfo; |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 31 | |
| 32 | namespace acm2 { |
| 33 | |
| 34 | class RentACodec { |
| 35 | public: |
| 36 | enum class CodecId { |
| 37 | #if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX) |
| 38 | kISAC, |
| 39 | #endif |
| 40 | #ifdef WEBRTC_CODEC_ISAC |
| 41 | kISACSWB, |
| 42 | #endif |
| 43 | // Mono |
| 44 | kPCM16B, |
| 45 | kPCM16Bwb, |
| 46 | kPCM16Bswb32kHz, |
| 47 | // Stereo |
| 48 | kPCM16B_2ch, |
| 49 | kPCM16Bwb_2ch, |
| 50 | kPCM16Bswb32kHz_2ch, |
| 51 | // Mono |
| 52 | kPCMU, |
| 53 | kPCMA, |
| 54 | // Stereo |
| 55 | kPCMU_2ch, |
| 56 | kPCMA_2ch, |
| 57 | #ifdef WEBRTC_CODEC_ILBC |
| 58 | kILBC, |
| 59 | #endif |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 60 | kG722, // Mono |
| 61 | kG722_2ch, // Stereo |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 62 | #ifdef WEBRTC_CODEC_OPUS |
| 63 | kOpus, // Mono and stereo |
| 64 | #endif |
| 65 | kCNNB, |
| 66 | kCNWB, |
| 67 | kCNSWB, |
| 68 | #ifdef ENABLE_48000_HZ |
| 69 | kCNFB, |
| 70 | #endif |
| 71 | kAVT, |
solenberg | 2779bab | 2016-11-17 04:45:19 -0800 | [diff] [blame] | 72 | kAVT16kHz, |
| 73 | kAVT32kHz, |
| 74 | kAVT48kHz, |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 75 | #ifdef WEBRTC_CODEC_RED |
| 76 | kRED, |
| 77 | #endif |
| 78 | kNumCodecs, // Implementation detail. Don't use. |
| 79 | |
| 80 | // Set unsupported codecs to -1. |
| 81 | #if !defined(WEBRTC_CODEC_ISAC) && !defined(WEBRTC_CODEC_ISACFX) |
| 82 | kISAC = -1, |
| 83 | #endif |
| 84 | #ifndef WEBRTC_CODEC_ISAC |
| 85 | kISACSWB = -1, |
| 86 | #endif |
| 87 | // 48 kHz not supported, always set to -1. |
| 88 | kPCM16Bswb48kHz = -1, |
| 89 | #ifndef WEBRTC_CODEC_ILBC |
| 90 | kILBC = -1, |
| 91 | #endif |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 92 | #ifndef WEBRTC_CODEC_OPUS |
| 93 | kOpus = -1, // Mono and stereo |
| 94 | #endif |
| 95 | #ifndef WEBRTC_CODEC_RED |
| 96 | kRED = -1, |
| 97 | #endif |
| 98 | #ifndef ENABLE_48000_HZ |
| 99 | kCNFB = -1, |
| 100 | #endif |
| 101 | |
| 102 | kNone = -1 |
| 103 | }; |
| 104 | |
| 105 | static inline size_t NumberOfCodecs() { |
| 106 | return static_cast<size_t>(CodecId::kNumCodecs); |
| 107 | } |
| 108 | |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 109 | static inline absl::optional<int> CodecIndexFromId(CodecId codec_id) { |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 110 | const int i = static_cast<int>(codec_id); |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 111 | return i >= 0 && i < static_cast<int>(NumberOfCodecs()) |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 112 | ? absl::optional<int>(i) |
| 113 | : absl::nullopt; |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 116 | static inline absl::optional<CodecId> CodecIdFromIndex(int codec_index) { |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 117 | return static_cast<size_t>(codec_index) < NumberOfCodecs() |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 118 | ? absl::optional<RentACodec::CodecId>( |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 119 | static_cast<RentACodec::CodecId>(codec_index)) |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 120 | : absl::nullopt; |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 123 | static absl::optional<CodecId> CodecIdByParams(const char* payload_name, |
| 124 | int sampling_freq_hz, |
| 125 | size_t channels); |
| 126 | static absl::optional<CodecInst> CodecInstById(CodecId codec_id); |
| 127 | static absl::optional<CodecId> CodecIdByInst(const CodecInst& codec_inst); |
| 128 | static absl::optional<CodecInst> CodecInstByParams(const char* payload_name, |
| 129 | int sampling_freq_hz, |
| 130 | size_t channels); |
kwiberg | 93a2feb | 2015-11-05 07:39:37 -0800 | [diff] [blame] | 131 | |
| 132 | static inline bool IsPayloadTypeValid(int payload_type) { |
| 133 | return payload_type >= 0 && payload_type <= 127; |
| 134 | } |
| 135 | |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 136 | static rtc::ArrayView<const CodecInst> Database(); |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 137 | |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 138 | static absl::optional<bool> IsSupportedNumChannels(CodecId codec_id, |
| 139 | size_t num_channels); |
kwiberg | de94d08 | 2015-11-03 05:46:09 -0800 | [diff] [blame] | 140 | |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 141 | static absl::optional<NetEqDecoder> NetEqDecoderFromCodecId( |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 142 | CodecId codec_id, |
| 143 | size_t num_channels); |
kwiberg | 805fc71 | 2015-11-10 04:05:16 -0800 | [diff] [blame] | 144 | |
kwiberg | e1a27d4 | 2015-11-18 07:32:49 -0800 | [diff] [blame] | 145 | // Parse codec_inst and extract payload types. If the given CodecInst was for |
| 146 | // the wrong sort of codec, return kSkip; otherwise, if the rate was illegal, |
| 147 | // return kBadFreq; otherwise, update the given RTP timestamp rate (Hz) -> |
| 148 | // payload type map and return kOk. |
| 149 | enum class RegistrationResult { kOk, kSkip, kBadFreq }; |
| 150 | static RegistrationResult RegisterCngPayloadType(std::map<int, int>* pt_map, |
| 151 | const CodecInst& codec_inst); |
| 152 | static RegistrationResult RegisterRedPayloadType(std::map<int, int>* pt_map, |
| 153 | const CodecInst& codec_inst); |
| 154 | |
kwiberg | 805fc71 | 2015-11-10 04:05:16 -0800 | [diff] [blame] | 155 | RentACodec(); |
| 156 | ~RentACodec(); |
| 157 | |
| 158 | // Creates and returns an audio encoder built to the given specification. |
kwiberg | 6030a12 | 2016-03-08 06:01:31 -0800 | [diff] [blame] | 159 | // Returns null in case of error. |
| 160 | std::unique_ptr<AudioEncoder> RentEncoder(const CodecInst& codec_inst); |
kwiberg | 805fc71 | 2015-11-10 04:05:16 -0800 | [diff] [blame] | 161 | |
kwiberg | 1379f1f | 2015-11-23 04:30:52 -0800 | [diff] [blame] | 162 | struct StackParameters { |
| 163 | StackParameters(); |
| 164 | ~StackParameters(); |
| 165 | |
kwiberg | 6030a12 | 2016-03-08 06:01:31 -0800 | [diff] [blame] | 166 | std::unique_ptr<AudioEncoder> speech_encoder; |
| 167 | |
kwiberg | 1379f1f | 2015-11-23 04:30:52 -0800 | [diff] [blame] | 168 | bool use_codec_fec = false; |
| 169 | bool use_red = false; |
| 170 | bool use_cng = false; |
| 171 | ACMVADMode vad_mode = VADNormal; |
| 172 | |
| 173 | // Maps from RTP timestamp rate (in Hz) to payload type. |
| 174 | std::map<int, int> cng_payload_types; |
| 175 | std::map<int, int> red_payload_types; |
kwiberg | e155ae6 | 2015-11-16 04:49:54 -0800 | [diff] [blame] | 176 | }; |
kwiberg | 1379f1f | 2015-11-23 04:30:52 -0800 | [diff] [blame] | 177 | |
| 178 | // Creates and returns an audio encoder stack constructed to the given |
| 179 | // specification. If the specification isn't compatible with the encoder, it |
kwiberg | 6030a12 | 2016-03-08 06:01:31 -0800 | [diff] [blame] | 180 | // will be changed to match (things will be switched off). The speech encoder |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 181 | // will be stolen. If the specification isn't complete, returns nullptr. |
kwiberg | 6030a12 | 2016-03-08 06:01:31 -0800 | [diff] [blame] | 182 | std::unique_ptr<AudioEncoder> RentEncoderStack(StackParameters* param); |
kwiberg | e155ae6 | 2015-11-16 04:49:54 -0800 | [diff] [blame] | 183 | |
kwiberg | 4cdbd57 | 2016-03-30 03:10:05 -0700 | [diff] [blame] | 184 | // Creates and returns an iSAC decoder. |
kwiberg | abe95ba | 2016-06-02 02:58:59 -0700 | [diff] [blame] | 185 | std::unique_ptr<AudioDecoder> RentIsacDecoder(int sample_rate_hz); |
kwiberg | 805fc71 | 2015-11-10 04:05:16 -0800 | [diff] [blame] | 186 | |
| 187 | private: |
kwiberg | 16c5a96 | 2016-02-15 02:27:22 -0800 | [diff] [blame] | 188 | std::unique_ptr<AudioEncoder> speech_encoder_; |
| 189 | std::unique_ptr<AudioEncoder> cng_encoder_; |
| 190 | std::unique_ptr<AudioEncoder> red_encoder_; |
kwiberg | 0d05da7 | 2016-03-30 04:10:11 -0700 | [diff] [blame] | 191 | rtc::scoped_refptr<LockedIsacBandwidthInfo> isac_bandwidth_info_; |
kwiberg | 805fc71 | 2015-11-10 04:05:16 -0800 | [diff] [blame] | 192 | |
| 193 | RTC_DISALLOW_COPY_AND_ASSIGN(RentACodec); |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | } // namespace acm2 |
| 197 | } // namespace webrtc |
| 198 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 199 | #endif // MODULES_AUDIO_CODING_ACM2_RENT_A_CODEC_H_ |