blob: f1de72d2cbc59c7564be037de4be9f0dc902cbbc [file] [log] [blame]
kwibergfce4a942015-10-27 11:40:24 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_ACM2_RENT_A_CODEC_H_
12#define MODULES_AUDIO_CODING_ACM2_RENT_A_CODEC_H_
kwibergfce4a942015-10-27 11:40:24 -070013
14#include <stddef.h>
kwiberge1a27d42015-11-18 07:32:49 -080015#include <map>
kwiberg16c5a962016-02-15 02:27:22 -080016#include <memory>
kwibergfce4a942015-10-27 11:40:24 -070017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/array_view.h"
19#include "api/audio_codecs/audio_decoder.h"
20#include "api/audio_codecs/audio_encoder.h"
21#include "api/optional.h"
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"
Mirko Bonadei71207422017-09-15 13:58:09 +020026#include "typedefs.h" // NOLINT(build/include)
kwiberg805fc712015-11-10 04:05:16 -080027
kwibergfce4a942015-10-27 11:40:24 -070028namespace webrtc {
29
30struct CodecInst;
kwiberg0d05da72016-03-30 04:10:11 -070031class LockedIsacBandwidthInfo;
kwibergfce4a942015-10-27 11:40:24 -070032
33namespace acm2 {
34
35class RentACodec {
36 public:
37 enum class CodecId {
38#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
39 kISAC,
40#endif
41#ifdef WEBRTC_CODEC_ISAC
42 kISACSWB,
43#endif
44 // Mono
45 kPCM16B,
46 kPCM16Bwb,
47 kPCM16Bswb32kHz,
48 // Stereo
49 kPCM16B_2ch,
50 kPCM16Bwb_2ch,
51 kPCM16Bswb32kHz_2ch,
52 // Mono
53 kPCMU,
54 kPCMA,
55 // Stereo
56 kPCMU_2ch,
57 kPCMA_2ch,
58#ifdef WEBRTC_CODEC_ILBC
59 kILBC,
60#endif
61#ifdef WEBRTC_CODEC_G722
62 kG722, // Mono
63 kG722_2ch, // Stereo
64#endif
65#ifdef WEBRTC_CODEC_OPUS
66 kOpus, // Mono and stereo
67#endif
68 kCNNB,
69 kCNWB,
70 kCNSWB,
71#ifdef ENABLE_48000_HZ
72 kCNFB,
73#endif
74 kAVT,
solenberg2779bab2016-11-17 04:45:19 -080075 kAVT16kHz,
76 kAVT32kHz,
77 kAVT48kHz,
kwibergfce4a942015-10-27 11:40:24 -070078#ifdef WEBRTC_CODEC_RED
79 kRED,
80#endif
81 kNumCodecs, // Implementation detail. Don't use.
82
83// Set unsupported codecs to -1.
84#if !defined(WEBRTC_CODEC_ISAC) && !defined(WEBRTC_CODEC_ISACFX)
85 kISAC = -1,
86#endif
87#ifndef WEBRTC_CODEC_ISAC
88 kISACSWB = -1,
89#endif
90 // 48 kHz not supported, always set to -1.
91 kPCM16Bswb48kHz = -1,
92#ifndef WEBRTC_CODEC_ILBC
93 kILBC = -1,
94#endif
95#ifndef WEBRTC_CODEC_G722
96 kG722 = -1, // Mono
97 kG722_2ch = -1, // Stereo
98#endif
99#ifndef WEBRTC_CODEC_OPUS
100 kOpus = -1, // Mono and stereo
101#endif
102#ifndef WEBRTC_CODEC_RED
103 kRED = -1,
104#endif
105#ifndef ENABLE_48000_HZ
106 kCNFB = -1,
107#endif
108
109 kNone = -1
110 };
111
112 static inline size_t NumberOfCodecs() {
113 return static_cast<size_t>(CodecId::kNumCodecs);
114 }
115
Karl Wibergbe579832015-11-10 22:34:18 +0100116 static inline rtc::Optional<int> CodecIndexFromId(CodecId codec_id) {
kwibergfce4a942015-10-27 11:40:24 -0700117 const int i = static_cast<int>(codec_id);
Karl Wibergbe579832015-11-10 22:34:18 +0100118 return i >= 0 && i < static_cast<int>(NumberOfCodecs())
119 ? rtc::Optional<int>(i)
120 : rtc::Optional<int>();
kwibergfce4a942015-10-27 11:40:24 -0700121 }
122
Karl Wibergbe579832015-11-10 22:34:18 +0100123 static inline rtc::Optional<CodecId> CodecIdFromIndex(int codec_index) {
kwibergfce4a942015-10-27 11:40:24 -0700124 return static_cast<size_t>(codec_index) < NumberOfCodecs()
Karl Wibergbe579832015-11-10 22:34:18 +0100125 ? rtc::Optional<RentACodec::CodecId>(
kwiberg102c6a62015-10-30 02:47:38 -0700126 static_cast<RentACodec::CodecId>(codec_index))
Karl Wibergbe579832015-11-10 22:34:18 +0100127 : rtc::Optional<RentACodec::CodecId>();
kwibergfce4a942015-10-27 11:40:24 -0700128 }
129
Karl Wibergbe579832015-11-10 22:34:18 +0100130 static rtc::Optional<CodecId> CodecIdByParams(const char* payload_name,
131 int sampling_freq_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800132 size_t channels);
Karl Wibergbe579832015-11-10 22:34:18 +0100133 static rtc::Optional<CodecInst> CodecInstById(CodecId codec_id);
134 static rtc::Optional<CodecId> CodecIdByInst(const CodecInst& codec_inst);
135 static rtc::Optional<CodecInst> CodecInstByParams(const char* payload_name,
136 int sampling_freq_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800137 size_t channels);
kwibergfce4a942015-10-27 11:40:24 -0700138 static bool IsCodecValid(const CodecInst& codec_inst);
kwiberg93a2feb2015-11-05 07:39:37 -0800139
140 static inline bool IsPayloadTypeValid(int payload_type) {
141 return payload_type >= 0 && payload_type <= 127;
142 }
143
kwibergfce4a942015-10-27 11:40:24 -0700144 static rtc::ArrayView<const CodecInst> Database();
kwibergee1879c2015-10-29 06:20:28 -0700145
Karl Wibergbe579832015-11-10 22:34:18 +0100146 static rtc::Optional<bool> IsSupportedNumChannels(CodecId codec_id,
Peter Kasting69558702016-01-12 16:26:35 -0800147 size_t num_channels);
kwibergde94d082015-11-03 05:46:09 -0800148
Peter Kasting69558702016-01-12 16:26:35 -0800149 static rtc::Optional<NetEqDecoder> NetEqDecoderFromCodecId(
150 CodecId codec_id,
151 size_t num_channels);
kwiberg805fc712015-11-10 04:05:16 -0800152
kwiberge1a27d42015-11-18 07:32:49 -0800153 // Parse codec_inst and extract payload types. If the given CodecInst was for
154 // the wrong sort of codec, return kSkip; otherwise, if the rate was illegal,
155 // return kBadFreq; otherwise, update the given RTP timestamp rate (Hz) ->
156 // payload type map and return kOk.
157 enum class RegistrationResult { kOk, kSkip, kBadFreq };
158 static RegistrationResult RegisterCngPayloadType(std::map<int, int>* pt_map,
159 const CodecInst& codec_inst);
160 static RegistrationResult RegisterRedPayloadType(std::map<int, int>* pt_map,
161 const CodecInst& codec_inst);
162
kwiberg805fc712015-11-10 04:05:16 -0800163 RentACodec();
164 ~RentACodec();
165
166 // Creates and returns an audio encoder built to the given specification.
kwiberg6030a122016-03-08 06:01:31 -0800167 // Returns null in case of error.
168 std::unique_ptr<AudioEncoder> RentEncoder(const CodecInst& codec_inst);
kwiberg805fc712015-11-10 04:05:16 -0800169
kwiberg1379f1f2015-11-23 04:30:52 -0800170 struct StackParameters {
171 StackParameters();
172 ~StackParameters();
173
kwiberg6030a122016-03-08 06:01:31 -0800174 std::unique_ptr<AudioEncoder> speech_encoder;
175
kwiberg1379f1f2015-11-23 04:30:52 -0800176 bool use_codec_fec = false;
177 bool use_red = false;
178 bool use_cng = false;
179 ACMVADMode vad_mode = VADNormal;
180
181 // Maps from RTP timestamp rate (in Hz) to payload type.
182 std::map<int, int> cng_payload_types;
183 std::map<int, int> red_payload_types;
kwiberge155ae62015-11-16 04:49:54 -0800184 };
kwiberg1379f1f2015-11-23 04:30:52 -0800185
186 // Creates and returns an audio encoder stack constructed to the given
187 // specification. If the specification isn't compatible with the encoder, it
kwiberg6030a122016-03-08 06:01:31 -0800188 // will be changed to match (things will be switched off). The speech encoder
kwibergc8d071e2016-04-06 12:22:38 -0700189 // will be stolen. If the specification isn't complete, returns nullptr.
kwiberg6030a122016-03-08 06:01:31 -0800190 std::unique_ptr<AudioEncoder> RentEncoderStack(StackParameters* param);
kwiberge155ae62015-11-16 04:49:54 -0800191
kwiberg4cdbd572016-03-30 03:10:05 -0700192 // Creates and returns an iSAC decoder.
kwibergabe95ba2016-06-02 02:58:59 -0700193 std::unique_ptr<AudioDecoder> RentIsacDecoder(int sample_rate_hz);
kwiberg805fc712015-11-10 04:05:16 -0800194
195 private:
kwiberg16c5a962016-02-15 02:27:22 -0800196 std::unique_ptr<AudioEncoder> speech_encoder_;
197 std::unique_ptr<AudioEncoder> cng_encoder_;
198 std::unique_ptr<AudioEncoder> red_encoder_;
kwiberg0d05da72016-03-30 04:10:11 -0700199 rtc::scoped_refptr<LockedIsacBandwidthInfo> isac_bandwidth_info_;
kwiberg805fc712015-11-10 04:05:16 -0800200
201 RTC_DISALLOW_COPY_AND_ASSIGN(RentACodec);
kwibergfce4a942015-10-27 11:40:24 -0700202};
203
204} // namespace acm2
205} // namespace webrtc
206
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200207#endif // MODULES_AUDIO_CODING_ACM2_RENT_A_CODEC_H_