Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(516)

Side by Side Diff: webrtc/modules/audio_coding/acm2/rent_a_codec.cc

Issue 1702943002: Pass ownership of external encoders to the ACM (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 return std::unique_ptr<AudioEncoder>(new AudioEncoderIlbc(speech_inst)); 172 return std::unique_ptr<AudioEncoder>(new AudioEncoderIlbc(speech_inst));
173 #endif 173 #endif
174 #ifdef WEBRTC_CODEC_G722 174 #ifdef WEBRTC_CODEC_G722
175 if (STR_CASE_CMP(speech_inst.plname, "g722") == 0) 175 if (STR_CASE_CMP(speech_inst.plname, "g722") == 0)
176 return std::unique_ptr<AudioEncoder>(new AudioEncoderG722(speech_inst)); 176 return std::unique_ptr<AudioEncoder>(new AudioEncoderG722(speech_inst));
177 #endif 177 #endif
178 LOG_F(LS_ERROR) << "Could not create encoder of type " << speech_inst.plname; 178 LOG_F(LS_ERROR) << "Could not create encoder of type " << speech_inst.plname;
179 return std::unique_ptr<AudioEncoder>(); 179 return std::unique_ptr<AudioEncoder>();
180 } 180 }
181 181
182 std::unique_ptr<AudioEncoder> CreateRedEncoder(AudioEncoder* encoder, 182 std::unique_ptr<AudioEncoder> CreateRedEncoder(
183 int red_payload_type) { 183 std::unique_ptr<AudioEncoder> encoder,
184 int red_payload_type) {
184 #ifdef WEBRTC_CODEC_RED 185 #ifdef WEBRTC_CODEC_RED
185 AudioEncoderCopyRed::Config config; 186 AudioEncoderCopyRed::Config config;
186 config.payload_type = red_payload_type; 187 config.payload_type = red_payload_type;
187 config.speech_encoder = encoder; 188 config.speech_encoder = std::move(encoder);
188 return std::unique_ptr<AudioEncoder>(new AudioEncoderCopyRed(config)); 189 return std::unique_ptr<AudioEncoder>(
190 new AudioEncoderCopyRed(std::move(config)));
189 #else 191 #else
190 return std::unique_ptr<AudioEncoder>(); 192 return std::unique_ptr<AudioEncoder>();
191 #endif 193 #endif
192 } 194 }
193 195
194 std::unique_ptr<AudioEncoder> CreateCngEncoder(AudioEncoder* encoder, 196 std::unique_ptr<AudioEncoder> CreateCngEncoder(
195 int payload_type, 197 std::unique_ptr<AudioEncoder> encoder,
196 ACMVADMode vad_mode) { 198 int payload_type,
199 ACMVADMode vad_mode) {
197 AudioEncoderCng::Config config; 200 AudioEncoderCng::Config config;
198 config.num_channels = encoder->NumChannels(); 201 config.num_channels = encoder->NumChannels();
199 config.payload_type = payload_type; 202 config.payload_type = payload_type;
200 config.speech_encoder = encoder; 203 config.speech_encoder = std::move(encoder);
201 switch (vad_mode) { 204 switch (vad_mode) {
202 case VADNormal: 205 case VADNormal:
203 config.vad_mode = Vad::kVadNormal; 206 config.vad_mode = Vad::kVadNormal;
204 break; 207 break;
205 case VADLowBitrate: 208 case VADLowBitrate:
206 config.vad_mode = Vad::kVadLowBitrate; 209 config.vad_mode = Vad::kVadLowBitrate;
207 break; 210 break;
208 case VADAggr: 211 case VADAggr:
209 config.vad_mode = Vad::kVadAggressive; 212 config.vad_mode = Vad::kVadAggressive;
210 break; 213 break;
211 case VADVeryAggr: 214 case VADVeryAggr:
212 config.vad_mode = Vad::kVadVeryAggressive; 215 config.vad_mode = Vad::kVadVeryAggressive;
213 break; 216 break;
214 default: 217 default:
215 FATAL(); 218 FATAL();
216 } 219 }
217 return std::unique_ptr<AudioEncoder>(new AudioEncoderCng(config)); 220 return std::unique_ptr<AudioEncoder>(new AudioEncoderCng(std::move(config)));
218 } 221 }
219 222
220 std::unique_ptr<AudioDecoder> CreateIsacDecoder( 223 std::unique_ptr<AudioDecoder> CreateIsacDecoder(
221 LockedIsacBandwidthInfo* bwinfo) { 224 LockedIsacBandwidthInfo* bwinfo) {
222 #if defined(WEBRTC_CODEC_ISACFX) 225 #if defined(WEBRTC_CODEC_ISACFX)
223 return std::unique_ptr<AudioDecoder>(new AudioDecoderIsacFix(bwinfo)); 226 return std::unique_ptr<AudioDecoder>(new AudioDecoderIsacFix(bwinfo));
224 #elif defined(WEBRTC_CODEC_ISAC) 227 #elif defined(WEBRTC_CODEC_ISAC)
225 return std::unique_ptr<AudioDecoder>(new AudioDecoderIsac(bwinfo)); 228 return std::unique_ptr<AudioDecoder>(new AudioDecoderIsac(bwinfo));
226 #else 229 #else
227 FATAL() << "iSAC is not supported."; 230 FATAL() << "iSAC is not supported.";
228 return std::unique_ptr<AudioDecoder>(); 231 return std::unique_ptr<AudioDecoder>();
229 #endif 232 #endif
230 } 233 }
231 234
232 } // namespace 235 } // namespace
233 236
234 RentACodec::RentACodec() = default; 237 RentACodec::RentACodec() = default;
235 RentACodec::~RentACodec() = default; 238 RentACodec::~RentACodec() = default;
236 239
237 AudioEncoder* RentACodec::RentEncoder(const CodecInst& codec_inst) { 240 std::unique_ptr<AudioEncoder> RentACodec::RentEncoder(
238 std::unique_ptr<AudioEncoder> enc = 241 const CodecInst& codec_inst) {
239 CreateEncoder(codec_inst, &isac_bandwidth_info_); 242 return CreateEncoder(codec_inst, &isac_bandwidth_info_);
240 if (!enc)
241 return nullptr;
242 speech_encoder_ = std::move(enc);
243 return speech_encoder_.get();
244 } 243 }
245 244
246 RentACodec::StackParameters::StackParameters() { 245 RentACodec::StackParameters::StackParameters() {
247 // Register the default payload types for RED and CNG. 246 // Register the default payload types for RED and CNG.
248 for (const CodecInst& ci : RentACodec::Database()) { 247 for (const CodecInst& ci : RentACodec::Database()) {
249 RentACodec::RegisterCngPayloadType(&cng_payload_types, ci); 248 RentACodec::RegisterCngPayloadType(&cng_payload_types, ci);
250 RentACodec::RegisterRedPayloadType(&red_payload_types, ci); 249 RentACodec::RegisterRedPayloadType(&red_payload_types, ci);
251 } 250 }
252 } 251 }
253 252
254 RentACodec::StackParameters::~StackParameters() = default; 253 RentACodec::StackParameters::~StackParameters() = default;
255 254
256 AudioEncoder* RentACodec::RentEncoderStack(StackParameters* param) { 255 std::unique_ptr<AudioEncoder> RentACodec::RentEncoderStack(
256 StackParameters* param) {
257 RTC_DCHECK(param->speech_encoder); 257 RTC_DCHECK(param->speech_encoder);
258 258
259 if (param->use_codec_fec) { 259 if (param->use_codec_fec) {
260 // Switch FEC on. On failure, remember that FEC is off. 260 // Switch FEC on. On failure, remember that FEC is off.
261 if (!param->speech_encoder->SetFec(true)) 261 if (!param->speech_encoder->SetFec(true))
262 param->use_codec_fec = false; 262 param->use_codec_fec = false;
263 } else { 263 } else {
264 // Switch FEC off. This shouldn't fail. 264 // Switch FEC off. This shouldn't fail.
265 const bool success = param->speech_encoder->SetFec(false); 265 const bool success = param->speech_encoder->SetFec(false);
266 RTC_DCHECK(success); 266 RTC_DCHECK(success);
267 } 267 }
268 268
269 auto pt = [&param](const std::map<int, int>& m) { 269 auto pt = [&param](const std::map<int, int>& m) {
270 auto it = m.find(param->speech_encoder->SampleRateHz()); 270 auto it = m.find(param->speech_encoder->SampleRateHz());
271 return it == m.end() ? rtc::Optional<int>() 271 return it == m.end() ? rtc::Optional<int>()
272 : rtc::Optional<int>(it->second); 272 : rtc::Optional<int>(it->second);
273 }; 273 };
274 auto cng_pt = pt(param->cng_payload_types); 274 auto cng_pt = pt(param->cng_payload_types);
275 param->use_cng = 275 param->use_cng =
276 param->use_cng && cng_pt && param->speech_encoder->NumChannels() == 1; 276 param->use_cng && cng_pt && param->speech_encoder->NumChannels() == 1;
277 auto red_pt = pt(param->red_payload_types); 277 auto red_pt = pt(param->red_payload_types);
278 param->use_red = param->use_red && red_pt; 278 param->use_red = param->use_red && red_pt;
279 279
280 if (param->use_cng || param->use_red) { 280 if (param->use_cng || param->use_red) {
281 // The RED and CNG encoders need to be in sync with the speech encoder, so 281 // The RED and CNG encoders need to be in sync with the speech encoder, so
282 // reset the latter to ensure its buffer is empty. 282 // reset the latter to ensure its buffer is empty.
283 param->speech_encoder->Reset(); 283 param->speech_encoder->Reset();
284 } 284 }
285 AudioEncoder* encoder_stack = param->speech_encoder; 285 std::unique_ptr<AudioEncoder> encoder_stack =
286 std::move(param->speech_encoder);
286 if (param->use_red) { 287 if (param->use_red) {
287 red_encoder_ = CreateRedEncoder(encoder_stack, *red_pt); 288 encoder_stack = CreateRedEncoder(std::move(encoder_stack), *red_pt);
288 if (red_encoder_)
289 encoder_stack = red_encoder_.get();
290 } else {
291 red_encoder_.reset();
292 } 289 }
293 if (param->use_cng) { 290 if (param->use_cng) {
294 cng_encoder_ = CreateCngEncoder(encoder_stack, *cng_pt, param->vad_mode); 291 encoder_stack =
295 encoder_stack = cng_encoder_.get(); 292 CreateCngEncoder(std::move(encoder_stack), *cng_pt, param->vad_mode);
296 } else {
297 cng_encoder_.reset();
298 } 293 }
299 return encoder_stack; 294 return encoder_stack;
300 } 295 }
301 296
302 AudioDecoder* RentACodec::RentIsacDecoder() { 297 AudioDecoder* RentACodec::RentIsacDecoder() {
303 if (!isac_decoder_) 298 if (!isac_decoder_)
304 isac_decoder_ = CreateIsacDecoder(&isac_bandwidth_info_); 299 isac_decoder_ = CreateIsacDecoder(&isac_bandwidth_info_);
305 return isac_decoder_.get(); 300 return isac_decoder_.get();
306 } 301 }
307 302
308 } // namespace acm2 303 } // namespace acm2
309 } // namespace webrtc 304 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698