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

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

Issue 2089183002: Don't recreate the speech encoder if we don't have to (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 6 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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 case RentACodec::RegistrationResult::kSkip: 106 case RentACodec::RegistrationResult::kSkip:
107 break; 107 break;
108 } 108 }
109 109
110 if (IsOpus(send_codec)) { 110 if (IsOpus(send_codec)) {
111 // VAD/DTX not supported. 111 // VAD/DTX not supported.
112 codec_stack_params_.use_cng = false; 112 codec_stack_params_.use_cng = false;
113 } 113 }
114 114
115 send_codec_inst_ = rtc::Optional<CodecInst>(send_codec); 115 send_codec_inst_ = rtc::Optional<CodecInst>(send_codec);
116 codec_stack_params_.speech_encoder.reset(); // Caller must recreate it. 116 recreate_encoder_ = true; // Caller must recreate it.
117 return true; 117 return true;
118 } 118 }
119 119
120 CodecInst CodecManager::ForgeCodecInst( 120 CodecInst CodecManager::ForgeCodecInst(
121 const AudioEncoder* external_speech_encoder) { 121 const AudioEncoder* external_speech_encoder) {
122 CodecInst ci; 122 CodecInst ci;
123 ci.channels = external_speech_encoder->NumChannels(); 123 ci.channels = external_speech_encoder->NumChannels();
124 ci.plfreq = external_speech_encoder->SampleRateHz(); 124 ci.plfreq = external_speech_encoder->SampleRateHz();
125 ci.pacsize = rtc::CheckedDivExact( 125 ci.pacsize = rtc::CheckedDivExact(
126 static_cast<int>(external_speech_encoder->Max10MsFramesInAPacket() * 126 static_cast<int>(external_speech_encoder->Max10MsFramesInAPacket() *
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 if (enable_codec_fec && codec_stack_params_.use_red) { 183 if (enable_codec_fec && codec_stack_params_.use_red) {
184 WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceAudioCoding, 0, 184 WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceAudioCoding, 0,
185 "Codec internal FEC and RED cannot be co-enabled."); 185 "Codec internal FEC and RED cannot be co-enabled.");
186 return false; 186 return false;
187 } 187 }
188 188
189 codec_stack_params_.use_codec_fec = enable_codec_fec; 189 codec_stack_params_.use_codec_fec = enable_codec_fec;
190 return true; 190 return true;
191 } 191 }
192 192
193 bool CodecManager::MakeEncoder(RentACodec* rac, AudioCodingModule* acm) {
194 RTC_DCHECK(rac);
195 RTC_DCHECK(acm);
196
197 if (!recreate_encoder_) {
ossu 2016/06/23 08:32:07 The flow of this whole function is pretty difficul
kwiberg-webrtc 2016/06/23 10:31:51 Hmm. Yes, that's possible. We change the cng and r
hlundin-webrtc 2016/06/23 10:38:46 Speed is of importance here.
198 bool error = false;
199 // Try to re-use the speech encoder we've given to the ACM.
200 acm->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
201 if (!*encoder) {
202 // There is no existing encoder.
203 recreate_encoder_ = true;
204 return;
205 }
206
207 // Extract the speech encoder from the ACM.
208 std::unique_ptr<AudioEncoder> enc = std::move(*encoder);
209 while (true) {
210 auto sub_enc = enc->ReclaimContainedEncoders();
211 if (sub_enc.empty()) {
212 break;
213 }
214 RTC_CHECK_EQ(1u, sub_enc.size());
215
216 // Replace enc with its sub encoder. We need to put the sub encoder in
217 // a temporary first, since otherwise the old value of enc would be
218 // destroyed before the new value got assigned, which would be bad
219 // since the new value is a part of the old value.
220 auto tmp_enc = std::move(sub_enc[0]);
221 enc = std::move(tmp_enc);
222 }
223
224 // Wrap it in a new encoder stack and put it back.
225 codec_stack_params_.speech_encoder = std::move(enc);
226 *encoder = rac->RentEncoderStack(&codec_stack_params_);
227 if (!*encoder) {
228 error = true;
229 }
230 });
231 if (error) {
232 return false;
233 }
234 if (!recreate_encoder_) {
235 return true;
236 }
237 }
238
239 if (!send_codec_inst_) {
240 // We don't have the information we need to create a new speech encoder.
241 // (This is not an error.)
242 return true;
hlundin-webrtc 2016/06/23 07:46:36 Should recreate_encoder_ be modified here?
kwiberg-webrtc 2016/06/23 10:31:51 No. It's currently true, and should be true becaus
hlundin-webrtc 2016/06/23 10:38:46 Acknowledged.
243 }
244
245 codec_stack_params_.speech_encoder = rac->RentEncoder(*send_codec_inst_);
246 auto stack = rac->RentEncoderStack(&codec_stack_params_);
247 if (!stack) {
248 return false;
hlundin-webrtc 2016/06/23 07:46:36 .. and here?
kwiberg-webrtc 2016/06/23 10:31:51 Same reasoning as above. We should only set it on
hlundin-webrtc 2016/06/23 10:38:47 Acknowledged.
249 }
250 acm->SetEncoder(std::move(stack));
251 recreate_encoder_ = false;
252 return true;
253 }
254
193 } // namespace acm2 255 } // namespace acm2
194 } // namespace webrtc 256 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698