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 
11 #ifndef MODULES_AUDIO_CODING_ACM2_RENT_A_CODEC_H_
12 #define MODULES_AUDIO_CODING_ACM2_RENT_A_CODEC_H_
13 
14 #include <stddef.h>
15 #include <map>
16 #include <memory>
17 
18 #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"
26 #include "typedefs.h"  // NOLINT(build/include)
27 
28 namespace webrtc {
29 
30 struct CodecInst;
31 class LockedIsacBandwidthInfo;
32 
33 namespace acm2 {
34 
35 class 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     kG722,      // Mono
62     kG722_2ch,  // Stereo
63 #ifdef WEBRTC_CODEC_OPUS
64     kOpus,  // Mono and stereo
65 #endif
66     kCNNB,
67     kCNWB,
68     kCNSWB,
69 #ifdef ENABLE_48000_HZ
70     kCNFB,
71 #endif
72     kAVT,
73     kAVT16kHz,
74     kAVT32kHz,
75     kAVT48kHz,
76 #ifdef WEBRTC_CODEC_RED
77     kRED,
78 #endif
79     kNumCodecs,  // Implementation detail. Don't use.
80 
81 // Set unsupported codecs to -1.
82 #if !defined(WEBRTC_CODEC_ISAC) && !defined(WEBRTC_CODEC_ISACFX)
83     kISAC = -1,
84 #endif
85 #ifndef WEBRTC_CODEC_ISAC
86     kISACSWB = -1,
87 #endif
88     // 48 kHz not supported, always set to -1.
89     kPCM16Bswb48kHz = -1,
90 #ifndef WEBRTC_CODEC_ILBC
91     kILBC = -1,
92 #endif
93 #ifndef WEBRTC_CODEC_OPUS
94     kOpus = -1,  // Mono and stereo
95 #endif
96 #ifndef WEBRTC_CODEC_RED
97     kRED = -1,
98 #endif
99 #ifndef ENABLE_48000_HZ
100     kCNFB = -1,
101 #endif
102 
103     kNone = -1
104   };
105 
NumberOfCodecs()106   static inline size_t NumberOfCodecs() {
107     return static_cast<size_t>(CodecId::kNumCodecs);
108   }
109 
CodecIndexFromId(CodecId codec_id)110   static inline rtc::Optional<int> CodecIndexFromId(CodecId codec_id) {
111     const int i = static_cast<int>(codec_id);
112     return i >= 0 && i < static_cast<int>(NumberOfCodecs())
113                ? rtc::Optional<int>(i)
114                : rtc::nullopt;
115   }
116 
CodecIdFromIndex(int codec_index)117   static inline rtc::Optional<CodecId> CodecIdFromIndex(int codec_index) {
118     return static_cast<size_t>(codec_index) < NumberOfCodecs()
119                ? rtc::Optional<RentACodec::CodecId>(
120                      static_cast<RentACodec::CodecId>(codec_index))
121                : rtc::nullopt;
122   }
123 
124   static rtc::Optional<CodecId> CodecIdByParams(const char* payload_name,
125                                                 int sampling_freq_hz,
126                                                 size_t channels);
127   static rtc::Optional<CodecInst> CodecInstById(CodecId codec_id);
128   static rtc::Optional<CodecId> CodecIdByInst(const CodecInst& codec_inst);
129   static rtc::Optional<CodecInst> CodecInstByParams(const char* payload_name,
130                                                     int sampling_freq_hz,
131                                                     size_t channels);
132   static bool IsCodecValid(const CodecInst& codec_inst);
133 
IsPayloadTypeValid(int payload_type)134   static inline bool IsPayloadTypeValid(int payload_type) {
135     return payload_type >= 0 && payload_type <= 127;
136   }
137 
138   static rtc::ArrayView<const CodecInst> Database();
139 
140   static rtc::Optional<bool> IsSupportedNumChannels(CodecId codec_id,
141                                                     size_t num_channels);
142 
143   static rtc::Optional<NetEqDecoder> NetEqDecoderFromCodecId(
144       CodecId codec_id,
145       size_t num_channels);
146 
147   // Parse codec_inst and extract payload types. If the given CodecInst was for
148   // the wrong sort of codec, return kSkip; otherwise, if the rate was illegal,
149   // return kBadFreq; otherwise, update the given RTP timestamp rate (Hz) ->
150   // payload type map and return kOk.
151   enum class RegistrationResult { kOk, kSkip, kBadFreq };
152   static RegistrationResult RegisterCngPayloadType(std::map<int, int>* pt_map,
153                                                    const CodecInst& codec_inst);
154   static RegistrationResult RegisterRedPayloadType(std::map<int, int>* pt_map,
155                                                    const CodecInst& codec_inst);
156 
157   RentACodec();
158   ~RentACodec();
159 
160   // Creates and returns an audio encoder built to the given specification.
161   // Returns null in case of error.
162   std::unique_ptr<AudioEncoder> RentEncoder(const CodecInst& codec_inst);
163 
164   struct StackParameters {
165     StackParameters();
166     ~StackParameters();
167 
168     std::unique_ptr<AudioEncoder> speech_encoder;
169 
170     bool use_codec_fec = false;
171     bool use_red = false;
172     bool use_cng = false;
173     ACMVADMode vad_mode = VADNormal;
174 
175     // Maps from RTP timestamp rate (in Hz) to payload type.
176     std::map<int, int> cng_payload_types;
177     std::map<int, int> red_payload_types;
178   };
179 
180   // Creates and returns an audio encoder stack constructed to the given
181   // specification. If the specification isn't compatible with the encoder, it
182   // will be changed to match (things will be switched off). The speech encoder
183   // will be stolen. If the specification isn't complete, returns nullptr.
184   std::unique_ptr<AudioEncoder> RentEncoderStack(StackParameters* param);
185 
186   // Creates and returns an iSAC decoder.
187   std::unique_ptr<AudioDecoder> RentIsacDecoder(int sample_rate_hz);
188 
189  private:
190   std::unique_ptr<AudioEncoder> speech_encoder_;
191   std::unique_ptr<AudioEncoder> cng_encoder_;
192   std::unique_ptr<AudioEncoder> red_encoder_;
193   rtc::scoped_refptr<LockedIsacBandwidthInfo> isac_bandwidth_info_;
194 
195   RTC_DISALLOW_COPY_AND_ASSIGN(RentACodec);
196 };
197 
198 }  // namespace acm2
199 }  // namespace webrtc
200 
201 #endif  // MODULES_AUDIO_CODING_ACM2_RENT_A_CODEC_H_
202