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

Side by Side Diff: webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc

Issue 2930243003: Opus implementation of the AudioEncoderFactoryTemplate API (Closed)
Patch Set: rebase Created 3 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) 2014 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2014 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 30 matching lines...) Expand all
41 // 16-20 kb/s for WB speech, 41 // 16-20 kb/s for WB speech,
42 // 28-40 kb/s for FB speech, 42 // 28-40 kb/s for FB speech,
43 // 48-64 kb/s for FB mono music, and 43 // 48-64 kb/s for FB mono music, and
44 // 64-128 kb/s for FB stereo music. 44 // 64-128 kb/s for FB stereo music.
45 // The current implementation applies the following values to mono signals, 45 // The current implementation applies the following values to mono signals,
46 // and multiplies them by 2 for stereo. 46 // and multiplies them by 2 for stereo.
47 constexpr int kOpusBitrateNbBps = 12000; 47 constexpr int kOpusBitrateNbBps = 12000;
48 constexpr int kOpusBitrateWbBps = 20000; 48 constexpr int kOpusBitrateWbBps = 20000;
49 constexpr int kOpusBitrateFbBps = 32000; 49 constexpr int kOpusBitrateFbBps = 32000;
50 50
51 // Opus API allows a min bitrate of 500bps, but Opus documentation suggests
52 // bitrate should be in the range of 6000 to 510000, inclusive.
53 constexpr int kOpusMinBitrateBps = 6000;
54 constexpr int kOpusMaxBitrateBps = 510000;
55
56 constexpr int kSampleRateHz = 48000; 51 constexpr int kSampleRateHz = 48000;
57 constexpr int kDefaultMaxPlaybackRate = 48000; 52 constexpr int kDefaultMaxPlaybackRate = 48000;
58 53
59 // These two lists must be sorted from low to high 54 // These two lists must be sorted from low to high
60 #if WEBRTC_OPUS_SUPPORT_120MS_PTIME 55 #if WEBRTC_OPUS_SUPPORT_120MS_PTIME
61 constexpr int kANASupportedFrameLengths[] = {20, 60, 120}; 56 constexpr int kANASupportedFrameLengths[] = {20, 60, 120};
62 constexpr int kOpusSupportedFrameLengths[] = {10, 20, 40, 60, 120}; 57 constexpr int kOpusSupportedFrameLengths[] = {10, 20, 40, 60, 120};
63 #else 58 #else
64 constexpr int kANASupportedFrameLengths[] = {20, 60}; 59 constexpr int kANASupportedFrameLengths[] = {20, 60};
65 constexpr int kOpusSupportedFrameLengths[] = {10, 20, 40, 60}; 60 constexpr int kOpusSupportedFrameLengths[] = {10, 20, 40, 60};
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 int CalculateDefaultBitrate(int max_playback_rate, size_t num_channels) { 121 int CalculateDefaultBitrate(int max_playback_rate, size_t num_channels) {
127 const int bitrate = [&] { 122 const int bitrate = [&] {
128 if (max_playback_rate <= 8000) { 123 if (max_playback_rate <= 8000) {
129 return kOpusBitrateNbBps * rtc::dchecked_cast<int>(num_channels); 124 return kOpusBitrateNbBps * rtc::dchecked_cast<int>(num_channels);
130 } else if (max_playback_rate <= 16000) { 125 } else if (max_playback_rate <= 16000) {
131 return kOpusBitrateWbBps * rtc::dchecked_cast<int>(num_channels); 126 return kOpusBitrateWbBps * rtc::dchecked_cast<int>(num_channels);
132 } else { 127 } else {
133 return kOpusBitrateFbBps * rtc::dchecked_cast<int>(num_channels); 128 return kOpusBitrateFbBps * rtc::dchecked_cast<int>(num_channels);
134 } 129 }
135 }(); 130 }();
136 RTC_DCHECK_GE(bitrate, kOpusMinBitrateBps); 131 RTC_DCHECK_GE(bitrate, AudioEncoderOpusConfig::kMinBitrateBps);
137 RTC_DCHECK_LE(bitrate, kOpusMaxBitrateBps); 132 RTC_DCHECK_LE(bitrate, AudioEncoderOpusConfig::kMaxBitrateBps);
138 return bitrate; 133 return bitrate;
139 } 134 }
140 135
141 // Get the maxaveragebitrate parameter in string-form, so we can properly figure 136 // Get the maxaveragebitrate parameter in string-form, so we can properly figure
142 // out how invalid it is and accurately log invalid values. 137 // out how invalid it is and accurately log invalid values.
143 int CalculateBitrate(int max_playback_rate_hz, 138 int CalculateBitrate(int max_playback_rate_hz,
144 size_t num_channels, 139 size_t num_channels,
145 rtc::Optional<std::string> bitrate_param) { 140 rtc::Optional<std::string> bitrate_param) {
146 const int default_bitrate = 141 const int default_bitrate =
147 CalculateDefaultBitrate(max_playback_rate_hz, num_channels); 142 CalculateDefaultBitrate(max_playback_rate_hz, num_channels);
148 143
149 if (bitrate_param) { 144 if (bitrate_param) {
150 const auto bitrate = rtc::StringToNumber<int>(*bitrate_param); 145 const auto bitrate = rtc::StringToNumber<int>(*bitrate_param);
151 if (bitrate) { 146 if (bitrate) {
152 const int chosen_bitrate = 147 const int chosen_bitrate =
153 std::max(kOpusMinBitrateBps, std::min(*bitrate, kOpusMaxBitrateBps)); 148 std::max(AudioEncoderOpusConfig::kMinBitrateBps,
149 std::min(*bitrate, AudioEncoderOpusConfig::kMaxBitrateBps));
154 if (bitrate != chosen_bitrate) { 150 if (bitrate != chosen_bitrate) {
155 LOG(LS_WARNING) << "Invalid maxaveragebitrate " << *bitrate 151 LOG(LS_WARNING) << "Invalid maxaveragebitrate " << *bitrate
156 << " clamped to " << chosen_bitrate; 152 << " clamped to " << chosen_bitrate;
157 } 153 }
158 return chosen_bitrate; 154 return chosen_bitrate;
159 } 155 }
160 LOG(LS_WARNING) << "Invalid maxaveragebitrate \"" << *bitrate_param 156 LOG(LS_WARNING) << "Invalid maxaveragebitrate \"" << *bitrate_param
161 << "\" replaced by default bitrate " << default_bitrate; 157 << "\" replaced by default bitrate " << default_bitrate;
162 } 158 }
163 159
(...skipping 24 matching lines...) Expand all
188 // kOpusSupportedFrameLengths. 184 // kOpusSupportedFrameLengths.
189 for (const int supported_frame_length : kOpusSupportedFrameLengths) { 185 for (const int supported_frame_length : kOpusSupportedFrameLengths) {
190 if (supported_frame_length >= *ptime) { 186 if (supported_frame_length >= *ptime) {
191 return supported_frame_length; 187 return supported_frame_length;
192 } 188 }
193 } 189 }
194 // If none was found, return the largest supported frame length. 190 // If none was found, return the largest supported frame length.
195 return *(std::end(kOpusSupportedFrameLengths) - 1); 191 return *(std::end(kOpusSupportedFrameLengths) - 1);
196 } 192 }
197 193
198 return AudioEncoderOpus::Config::kDefaultFrameSizeMs; 194 return AudioEncoderOpusConfig::kDefaultFrameSizeMs;
199 } 195 }
200 196
201 void FindSupportedFrameLengths(int min_frame_length_ms, 197 void FindSupportedFrameLengths(int min_frame_length_ms,
202 int max_frame_length_ms, 198 int max_frame_length_ms,
203 std::vector<int>* out) { 199 std::vector<int>* out) {
204 out->clear(); 200 out->clear();
205 std::copy_if(std::begin(kANASupportedFrameLengths), 201 std::copy_if(std::begin(kANASupportedFrameLengths),
206 std::end(kANASupportedFrameLengths), std::back_inserter(*out), 202 std::end(kANASupportedFrameLengths), std::back_inserter(*out),
207 [&](int frame_length_ms) { 203 [&](int frame_length_ms) {
208 return frame_length_ms >= min_frame_length_ms && 204 return frame_length_ms >= min_frame_length_ms &&
209 frame_length_ms <= max_frame_length_ms; 205 frame_length_ms <= max_frame_length_ms;
210 }); 206 });
211 RTC_DCHECK(std::is_sorted(out->begin(), out->end())); 207 RTC_DCHECK(std::is_sorted(out->begin(), out->end()));
212 } 208 }
213 209
210 int GetBitrateBps(const AudioEncoderOpusConfig& config) {
211 RTC_DCHECK(config.IsOk());
212 return config.bitrate_bps;
213 }
214
214 } // namespace 215 } // namespace
215 216
216 rtc::Optional<AudioCodecInfo> AudioEncoderOpus::QueryAudioEncoder( 217 rtc::Optional<AudioCodecInfo> AudioEncoderOpusImpl::QueryAudioEncoder(
217 const SdpAudioFormat& format) { 218 const SdpAudioFormat& format) {
218 if (STR_CASE_CMP(format.name.c_str(), GetPayloadName()) == 0 && 219 if (STR_CASE_CMP(format.name.c_str(), GetPayloadName()) == 0 &&
219 format.clockrate_hz == 48000 && format.num_channels == 2) { 220 format.clockrate_hz == 48000 && format.num_channels == 2) {
220 const size_t num_channels = GetChannelCount(format); 221 const size_t num_channels = GetChannelCount(format);
221 const int bitrate = 222 const int bitrate =
222 CalculateBitrate(GetMaxPlaybackRate(format), num_channels, 223 CalculateBitrate(GetMaxPlaybackRate(format), num_channels,
223 GetFormatParameter(format, "maxaveragebitrate")); 224 GetFormatParameter(format, "maxaveragebitrate"));
224 AudioCodecInfo info(48000, num_channels, bitrate, kOpusMinBitrateBps, 225 AudioCodecInfo info(48000, num_channels, bitrate,
225 kOpusMaxBitrateBps); 226 AudioEncoderOpusConfig::kMinBitrateBps,
227 AudioEncoderOpusConfig::kMaxBitrateBps);
226 info.allow_comfort_noise = false; 228 info.allow_comfort_noise = false;
227 info.supports_network_adaption = true; 229 info.supports_network_adaption = true;
228 230
229 return rtc::Optional<AudioCodecInfo>(info); 231 return rtc::Optional<AudioCodecInfo>(info);
230 } 232 }
231 return rtc::Optional<AudioCodecInfo>(); 233 return rtc::Optional<AudioCodecInfo>();
232 } 234 }
233 235
234 AudioEncoderOpus::Config AudioEncoderOpus::CreateConfig( 236 AudioEncoderOpusConfig AudioEncoderOpusImpl::CreateConfig(
235 const CodecInst& codec_inst) { 237 const CodecInst& codec_inst) {
236 AudioEncoderOpus::Config config; 238 AudioEncoderOpusConfig config;
237 config.frame_size_ms = rtc::CheckedDivExact(codec_inst.pacsize, 48); 239 config.frame_size_ms = rtc::CheckedDivExact(codec_inst.pacsize, 48);
238 config.num_channels = codec_inst.channels; 240 config.num_channels = codec_inst.channels;
239 config.bitrate_bps = rtc::Optional<int>(codec_inst.rate); 241 config.bitrate_bps = codec_inst.rate;
240 config.payload_type = codec_inst.pltype; 242 config.application = config.num_channels == 1
241 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip 243 ? AudioEncoderOpusConfig::ApplicationMode::kVoip
242 : AudioEncoderOpus::kAudio; 244 : AudioEncoderOpusConfig::ApplicationMode::kAudio;
243 config.supported_frame_lengths_ms.push_back(config.frame_size_ms); 245 config.supported_frame_lengths_ms.push_back(config.frame_size_ms);
244 #if WEBRTC_OPUS_VARIABLE_COMPLEXITY
245 config.low_rate_complexity = 9;
246 #endif
247 return config; 246 return config;
248 } 247 }
249 248
250 AudioEncoderOpus::Config AudioEncoderOpus::CreateConfig( 249 rtc::Optional<AudioEncoderOpusConfig> AudioEncoderOpusImpl::SdpToConfig(
251 int payload_type,
252 const SdpAudioFormat& format) { 250 const SdpAudioFormat& format) {
253 AudioEncoderOpus::Config config; 251 if (STR_CASE_CMP(format.name.c_str(), "opus") != 0 ||
252 format.clockrate_hz != 48000 || format.num_channels != 2) {
253 return rtc::Optional<AudioEncoderOpusConfig>();
254 }
254 255
256 AudioEncoderOpusConfig config;
255 config.num_channels = GetChannelCount(format); 257 config.num_channels = GetChannelCount(format);
256 config.frame_size_ms = GetFrameSizeMs(format); 258 config.frame_size_ms = GetFrameSizeMs(format);
257 config.max_playback_rate_hz = GetMaxPlaybackRate(format); 259 config.max_playback_rate_hz = GetMaxPlaybackRate(format);
258 config.fec_enabled = (GetFormatParameter(format, "useinbandfec") == "1"); 260 config.fec_enabled = (GetFormatParameter(format, "useinbandfec") == "1");
259 config.dtx_enabled = (GetFormatParameter(format, "usedtx") == "1"); 261 config.dtx_enabled = (GetFormatParameter(format, "usedtx") == "1");
260 config.cbr_enabled = (GetFormatParameter(format, "cbr") == "1"); 262 config.cbr_enabled = (GetFormatParameter(format, "cbr") == "1");
261 config.bitrate_bps = rtc::Optional<int>( 263 config.bitrate_bps =
262 CalculateBitrate(config.max_playback_rate_hz, config.num_channels, 264 CalculateBitrate(config.max_playback_rate_hz, config.num_channels,
263 GetFormatParameter(format, "maxaveragebitrate"))); 265 GetFormatParameter(format, "maxaveragebitrate"));
264 config.payload_type = payload_type; 266 config.application = config.num_channels == 1
265 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip 267 ? AudioEncoderOpusConfig::ApplicationMode::kVoip
266 : AudioEncoderOpus::kAudio; 268 : AudioEncoderOpusConfig::ApplicationMode::kAudio;
267 #if WEBRTC_OPUS_VARIABLE_COMPLEXITY
268 config.low_rate_complexity = 9;
269 #endif
270 269
271 constexpr int kMinANAFrameLength = kANASupportedFrameLengths[0]; 270 constexpr int kMinANAFrameLength = kANASupportedFrameLengths[0];
272 constexpr int kMaxANAFrameLength = 271 constexpr int kMaxANAFrameLength =
273 kANASupportedFrameLengths[arraysize(kANASupportedFrameLengths) - 1]; 272 kANASupportedFrameLengths[arraysize(kANASupportedFrameLengths) - 1];
273
274 // For now, minptime and maxptime are only used with ANA. If ptime is outside 274 // For now, minptime and maxptime are only used with ANA. If ptime is outside
275 // of this range, it will get adjusted once ANA takes hold. Ideally, we'd know 275 // of this range, it will get adjusted once ANA takes hold. Ideally, we'd know
276 // if ANA was to be used when setting up the config, and adjust accordingly. 276 // if ANA was to be used when setting up the config, and adjust accordingly.
277 const int min_frame_length_ms = 277 const int min_frame_length_ms =
278 GetFormatParameter<int>(format, "minptime").value_or(kMinANAFrameLength); 278 GetFormatParameter<int>(format, "minptime").value_or(kMinANAFrameLength);
279 const int max_frame_length_ms = 279 const int max_frame_length_ms =
280 GetFormatParameter<int>(format, "maxptime").value_or(kMaxANAFrameLength); 280 GetFormatParameter<int>(format, "maxptime").value_or(kMaxANAFrameLength);
281 281
282 FindSupportedFrameLengths(min_frame_length_ms, max_frame_length_ms, 282 FindSupportedFrameLengths(min_frame_length_ms, max_frame_length_ms,
283 &config.supported_frame_lengths_ms); 283 &config.supported_frame_lengths_ms);
284 return config; 284 RTC_DCHECK(config.IsOk());
285 return rtc::Optional<AudioEncoderOpusConfig>(config);
285 } 286 }
286 287
287 class AudioEncoderOpus::PacketLossFractionSmoother { 288 rtc::Optional<int> AudioEncoderOpusImpl::GetNewComplexity(
289 const AudioEncoderOpusConfig& config) {
290 RTC_DCHECK(config.IsOk());
291 const int bitrate_bps = GetBitrateBps(config);
292 if (bitrate_bps >= config.complexity_threshold_bps -
293 config.complexity_threshold_window_bps &&
294 bitrate_bps <= config.complexity_threshold_bps +
295 config.complexity_threshold_window_bps) {
296 // Within the hysteresis window; make no change.
297 return rtc::Optional<int>();
298 } else {
299 return rtc::Optional<int>(bitrate_bps <= config.complexity_threshold_bps
300 ? config.low_rate_complexity
301 : config.complexity);
302 }
303 }
304
305 class AudioEncoderOpusImpl::PacketLossFractionSmoother {
288 public: 306 public:
289 explicit PacketLossFractionSmoother() 307 explicit PacketLossFractionSmoother()
290 : last_sample_time_ms_(rtc::TimeMillis()), 308 : last_sample_time_ms_(rtc::TimeMillis()),
291 smoother_(kAlphaForPacketLossFractionSmoother) {} 309 smoother_(kAlphaForPacketLossFractionSmoother) {}
292 310
293 // Gets the smoothed packet loss fraction. 311 // Gets the smoothed packet loss fraction.
294 float GetAverage() const { 312 float GetAverage() const {
295 float value = smoother_.filtered(); 313 float value = smoother_.filtered();
296 return (value == rtc::ExpFilter::kValueUndefined) ? 0.0f : value; 314 return (value == rtc::ExpFilter::kValueUndefined) ? 0.0f : value;
297 } 315 }
298 316
299 // Add new observation to the packet loss fraction smoother. 317 // Add new observation to the packet loss fraction smoother.
300 void AddSample(float packet_loss_fraction) { 318 void AddSample(float packet_loss_fraction) {
301 int64_t now_ms = rtc::TimeMillis(); 319 int64_t now_ms = rtc::TimeMillis();
302 smoother_.Apply(static_cast<float>(now_ms - last_sample_time_ms_), 320 smoother_.Apply(static_cast<float>(now_ms - last_sample_time_ms_),
303 packet_loss_fraction); 321 packet_loss_fraction);
304 last_sample_time_ms_ = now_ms; 322 last_sample_time_ms_ = now_ms;
305 } 323 }
306 324
307 private: 325 private:
308 int64_t last_sample_time_ms_; 326 int64_t last_sample_time_ms_;
309 327
310 // An exponential filter is used to smooth the packet loss fraction. 328 // An exponential filter is used to smooth the packet loss fraction.
311 rtc::ExpFilter smoother_; 329 rtc::ExpFilter smoother_;
312 }; 330 };
313 331
314 AudioEncoderOpus::Config::Config() { 332 AudioEncoderOpusImpl::AudioEncoderOpusImpl(
315 #if WEBRTC_OPUS_VARIABLE_COMPLEXITY 333 const AudioEncoderOpusConfig& config,
316 low_rate_complexity = 9; 334 int payload_type,
317 #endif
318 }
319 AudioEncoderOpus::Config::Config(const Config&) = default;
320 AudioEncoderOpus::Config::~Config() = default;
321 auto AudioEncoderOpus::Config::operator=(const Config&) -> Config& = default;
322
323 bool AudioEncoderOpus::Config::IsOk() const {
324 if (frame_size_ms <= 0 || frame_size_ms % 10 != 0)
325 return false;
326 if (num_channels != 1 && num_channels != 2)
327 return false;
328 if (bitrate_bps &&
329 (*bitrate_bps < kOpusMinBitrateBps || *bitrate_bps > kOpusMaxBitrateBps))
330 return false;
331 if (complexity < 0 || complexity > 10)
332 return false;
333 if (low_rate_complexity < 0 || low_rate_complexity > 10)
334 return false;
335 return true;
336 }
337
338 int AudioEncoderOpus::Config::GetBitrateBps() const {
339 RTC_DCHECK(IsOk());
340 if (bitrate_bps)
341 return *bitrate_bps; // Explicitly set value.
342 else
343 return num_channels == 1 ? 32000 : 64000; // Default value.
344 }
345
346 rtc::Optional<int> AudioEncoderOpus::Config::GetNewComplexity() const {
347 RTC_DCHECK(IsOk());
348 const int bitrate_bps = GetBitrateBps();
349 if (bitrate_bps >=
350 complexity_threshold_bps - complexity_threshold_window_bps &&
351 bitrate_bps <=
352 complexity_threshold_bps + complexity_threshold_window_bps) {
353 // Within the hysteresis window; make no change.
354 return rtc::Optional<int>();
355 }
356 return bitrate_bps <= complexity_threshold_bps
357 ? rtc::Optional<int>(low_rate_complexity)
358 : rtc::Optional<int>(complexity);
359 }
360
361 AudioEncoderOpus::AudioEncoderOpus(
362 const Config& config,
363 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator, 335 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator,
364 std::unique_ptr<SmoothingFilter> bitrate_smoother) 336 std::unique_ptr<SmoothingFilter> bitrate_smoother)
365 : send_side_bwe_with_overhead_(webrtc::field_trial::IsEnabled( 337 : payload_type_(payload_type),
338 send_side_bwe_with_overhead_(webrtc::field_trial::IsEnabled(
366 "WebRTC-SendSideBwe-WithOverhead")), 339 "WebRTC-SendSideBwe-WithOverhead")),
367 packet_loss_rate_(0.0), 340 packet_loss_rate_(0.0),
368 inst_(nullptr), 341 inst_(nullptr),
369 packet_loss_fraction_smoother_(new PacketLossFractionSmoother()), 342 packet_loss_fraction_smoother_(new PacketLossFractionSmoother()),
370 audio_network_adaptor_creator_( 343 audio_network_adaptor_creator_(
371 audio_network_adaptor_creator 344 audio_network_adaptor_creator
372 ? std::move(audio_network_adaptor_creator) 345 ? std::move(audio_network_adaptor_creator)
373 : [this](const ProtoString& config_string, 346 : [this](const ProtoString& config_string,
374 RtcEventLog* event_log) { 347 RtcEventLog* event_log) {
375 return DefaultAudioNetworkAdaptorCreator(config_string, 348 return DefaultAudioNetworkAdaptorCreator(config_string,
376 event_log); 349 event_log);
377 }), 350 }),
378 bitrate_smoother_(bitrate_smoother 351 bitrate_smoother_(bitrate_smoother
379 ? std::move(bitrate_smoother) : std::unique_ptr<SmoothingFilter>( 352 ? std::move(bitrate_smoother) : std::unique_ptr<SmoothingFilter>(
380 // We choose 5sec as initial time constant due to empirical data. 353 // We choose 5sec as initial time constant due to empirical data.
381 new SmoothingFilterImpl(5000))) { 354 new SmoothingFilterImpl(5000))) {
382 RTC_CHECK(RecreateEncoderInstance(config)); 355 RTC_CHECK(RecreateEncoderInstance(config));
383 } 356 }
384 357
385 AudioEncoderOpus::AudioEncoderOpus(const CodecInst& codec_inst) 358 AudioEncoderOpusImpl::AudioEncoderOpusImpl(const CodecInst& codec_inst)
386 : AudioEncoderOpus(CreateConfig(codec_inst), nullptr) {} 359 : AudioEncoderOpusImpl(CreateConfig(codec_inst), codec_inst.pltype) {}
387 360
388 AudioEncoderOpus::AudioEncoderOpus(int payload_type, 361 AudioEncoderOpusImpl::AudioEncoderOpusImpl(int payload_type,
389 const SdpAudioFormat& format) 362 const SdpAudioFormat& format)
390 : AudioEncoderOpus(CreateConfig(payload_type, format), nullptr) {} 363 : AudioEncoderOpusImpl(*SdpToConfig(format), payload_type) {}
391 364
392 AudioEncoderOpus::~AudioEncoderOpus() { 365 AudioEncoderOpusImpl::~AudioEncoderOpusImpl() {
393 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); 366 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_));
394 } 367 }
395 368
396 int AudioEncoderOpus::SampleRateHz() const { 369 int AudioEncoderOpusImpl::SampleRateHz() const {
397 return kSampleRateHz; 370 return kSampleRateHz;
398 } 371 }
399 372
400 size_t AudioEncoderOpus::NumChannels() const { 373 size_t AudioEncoderOpusImpl::NumChannels() const {
401 return config_.num_channels; 374 return config_.num_channels;
402 } 375 }
403 376
404 size_t AudioEncoderOpus::Num10MsFramesInNextPacket() const { 377 size_t AudioEncoderOpusImpl::Num10MsFramesInNextPacket() const {
405 return Num10msFramesPerPacket(); 378 return Num10msFramesPerPacket();
406 } 379 }
407 380
408 size_t AudioEncoderOpus::Max10MsFramesInAPacket() const { 381 size_t AudioEncoderOpusImpl::Max10MsFramesInAPacket() const {
409 return Num10msFramesPerPacket(); 382 return Num10msFramesPerPacket();
410 } 383 }
411 384
412 int AudioEncoderOpus::GetTargetBitrate() const { 385 int AudioEncoderOpusImpl::GetTargetBitrate() const {
413 return config_.GetBitrateBps(); 386 return GetBitrateBps(config_);
414 } 387 }
415 388
416 void AudioEncoderOpus::Reset() { 389 void AudioEncoderOpusImpl::Reset() {
417 RTC_CHECK(RecreateEncoderInstance(config_)); 390 RTC_CHECK(RecreateEncoderInstance(config_));
418 } 391 }
419 392
420 bool AudioEncoderOpus::SetFec(bool enable) { 393 bool AudioEncoderOpusImpl::SetFec(bool enable) {
421 if (enable) { 394 if (enable) {
422 RTC_CHECK_EQ(0, WebRtcOpus_EnableFec(inst_)); 395 RTC_CHECK_EQ(0, WebRtcOpus_EnableFec(inst_));
423 } else { 396 } else {
424 RTC_CHECK_EQ(0, WebRtcOpus_DisableFec(inst_)); 397 RTC_CHECK_EQ(0, WebRtcOpus_DisableFec(inst_));
425 } 398 }
426 config_.fec_enabled = enable; 399 config_.fec_enabled = enable;
427 return true; 400 return true;
428 } 401 }
429 402
430 bool AudioEncoderOpus::SetDtx(bool enable) { 403 bool AudioEncoderOpusImpl::SetDtx(bool enable) {
431 if (enable) { 404 if (enable) {
432 RTC_CHECK_EQ(0, WebRtcOpus_EnableDtx(inst_)); 405 RTC_CHECK_EQ(0, WebRtcOpus_EnableDtx(inst_));
433 } else { 406 } else {
434 RTC_CHECK_EQ(0, WebRtcOpus_DisableDtx(inst_)); 407 RTC_CHECK_EQ(0, WebRtcOpus_DisableDtx(inst_));
435 } 408 }
436 config_.dtx_enabled = enable; 409 config_.dtx_enabled = enable;
437 return true; 410 return true;
438 } 411 }
439 412
440 bool AudioEncoderOpus::GetDtx() const { 413 bool AudioEncoderOpusImpl::GetDtx() const {
441 return config_.dtx_enabled; 414 return config_.dtx_enabled;
442 } 415 }
443 416
444 bool AudioEncoderOpus::SetApplication(Application application) { 417 bool AudioEncoderOpusImpl::SetApplication(Application application) {
445 auto conf = config_; 418 auto conf = config_;
446 switch (application) { 419 switch (application) {
447 case Application::kSpeech: 420 case Application::kSpeech:
448 conf.application = AudioEncoderOpus::kVoip; 421 conf.application = AudioEncoderOpusConfig::ApplicationMode::kVoip;
449 break; 422 break;
450 case Application::kAudio: 423 case Application::kAudio:
451 conf.application = AudioEncoderOpus::kAudio; 424 conf.application = AudioEncoderOpusConfig::ApplicationMode::kAudio;
452 break; 425 break;
453 } 426 }
454 return RecreateEncoderInstance(conf); 427 return RecreateEncoderInstance(conf);
455 } 428 }
456 429
457 void AudioEncoderOpus::SetMaxPlaybackRate(int frequency_hz) { 430 void AudioEncoderOpusImpl::SetMaxPlaybackRate(int frequency_hz) {
458 auto conf = config_; 431 auto conf = config_;
459 conf.max_playback_rate_hz = frequency_hz; 432 conf.max_playback_rate_hz = frequency_hz;
460 RTC_CHECK(RecreateEncoderInstance(conf)); 433 RTC_CHECK(RecreateEncoderInstance(conf));
461 } 434 }
462 435
463 bool AudioEncoderOpus::EnableAudioNetworkAdaptor( 436 bool AudioEncoderOpusImpl::EnableAudioNetworkAdaptor(
464 const std::string& config_string, 437 const std::string& config_string,
465 RtcEventLog* event_log) { 438 RtcEventLog* event_log) {
466 audio_network_adaptor_ = 439 audio_network_adaptor_ =
467 audio_network_adaptor_creator_(config_string, event_log); 440 audio_network_adaptor_creator_(config_string, event_log);
468 return audio_network_adaptor_.get() != nullptr; 441 return audio_network_adaptor_.get() != nullptr;
469 } 442 }
470 443
471 void AudioEncoderOpus::DisableAudioNetworkAdaptor() { 444 void AudioEncoderOpusImpl::DisableAudioNetworkAdaptor() {
472 audio_network_adaptor_.reset(nullptr); 445 audio_network_adaptor_.reset(nullptr);
473 } 446 }
474 447
475 void AudioEncoderOpus::OnReceivedUplinkPacketLossFraction( 448 void AudioEncoderOpusImpl::OnReceivedUplinkPacketLossFraction(
476 float uplink_packet_loss_fraction) { 449 float uplink_packet_loss_fraction) {
477 if (!audio_network_adaptor_) { 450 if (!audio_network_adaptor_) {
478 packet_loss_fraction_smoother_->AddSample(uplink_packet_loss_fraction); 451 packet_loss_fraction_smoother_->AddSample(uplink_packet_loss_fraction);
479 float average_fraction_loss = packet_loss_fraction_smoother_->GetAverage(); 452 float average_fraction_loss = packet_loss_fraction_smoother_->GetAverage();
480 return SetProjectedPacketLossRate(average_fraction_loss); 453 return SetProjectedPacketLossRate(average_fraction_loss);
481 } 454 }
482 audio_network_adaptor_->SetUplinkPacketLossFraction( 455 audio_network_adaptor_->SetUplinkPacketLossFraction(
483 uplink_packet_loss_fraction); 456 uplink_packet_loss_fraction);
484 ApplyAudioNetworkAdaptor(); 457 ApplyAudioNetworkAdaptor();
485 } 458 }
486 459
487 void AudioEncoderOpus::OnReceivedUplinkRecoverablePacketLossFraction( 460 void AudioEncoderOpusImpl::OnReceivedUplinkRecoverablePacketLossFraction(
488 float uplink_recoverable_packet_loss_fraction) { 461 float uplink_recoverable_packet_loss_fraction) {
489 if (!audio_network_adaptor_) 462 if (!audio_network_adaptor_)
490 return; 463 return;
491 audio_network_adaptor_->SetUplinkRecoverablePacketLossFraction( 464 audio_network_adaptor_->SetUplinkRecoverablePacketLossFraction(
492 uplink_recoverable_packet_loss_fraction); 465 uplink_recoverable_packet_loss_fraction);
493 ApplyAudioNetworkAdaptor(); 466 ApplyAudioNetworkAdaptor();
494 } 467 }
495 468
496 void AudioEncoderOpus::OnReceivedUplinkBandwidth( 469 void AudioEncoderOpusImpl::OnReceivedUplinkBandwidth(
497 int target_audio_bitrate_bps, 470 int target_audio_bitrate_bps,
498 rtc::Optional<int64_t> probing_interval_ms) { 471 rtc::Optional<int64_t> probing_interval_ms) {
499 if (audio_network_adaptor_) { 472 if (audio_network_adaptor_) {
500 audio_network_adaptor_->SetTargetAudioBitrate(target_audio_bitrate_bps); 473 audio_network_adaptor_->SetTargetAudioBitrate(target_audio_bitrate_bps);
501 // We give smoothed bitrate allocation to audio network adaptor as 474 // We give smoothed bitrate allocation to audio network adaptor as
502 // the uplink bandwidth. 475 // the uplink bandwidth.
503 // The probing spikes should not affect the bitrate smoother more than 25%. 476 // The probing spikes should not affect the bitrate smoother more than 25%.
504 // To simplify the calculations we use a step response as input signal. 477 // To simplify the calculations we use a step response as input signal.
505 // The step response of an exponential filter is 478 // The step response of an exponential filter is
506 // u(t) = 1 - e^(-t / time_constant). 479 // u(t) = 1 - e^(-t / time_constant).
507 // In order to limit the affect of a BWE spike within 25% of its value 480 // In order to limit the affect of a BWE spike within 25% of its value
508 // before 481 // before
509 // the next probing, we would choose a time constant that fulfills 482 // the next probing, we would choose a time constant that fulfills
510 // 1 - e^(-probing_interval_ms / time_constant) < 0.25 483 // 1 - e^(-probing_interval_ms / time_constant) < 0.25
511 // Then 4 * probing_interval_ms is a good choice. 484 // Then 4 * probing_interval_ms is a good choice.
512 if (probing_interval_ms) 485 if (probing_interval_ms)
513 bitrate_smoother_->SetTimeConstantMs(*probing_interval_ms * 4); 486 bitrate_smoother_->SetTimeConstantMs(*probing_interval_ms * 4);
514 bitrate_smoother_->AddSample(target_audio_bitrate_bps); 487 bitrate_smoother_->AddSample(target_audio_bitrate_bps);
515 488
516 ApplyAudioNetworkAdaptor(); 489 ApplyAudioNetworkAdaptor();
517 } else if (send_side_bwe_with_overhead_) { 490 } else if (send_side_bwe_with_overhead_) {
518 if (!overhead_bytes_per_packet_) { 491 if (!overhead_bytes_per_packet_) {
519 LOG(LS_INFO) 492 LOG(LS_INFO)
520 << "AudioEncoderOpus: Overhead unknown, target audio bitrate " 493 << "AudioEncoderOpusImpl: Overhead unknown, target audio bitrate "
521 << target_audio_bitrate_bps << " bps is ignored."; 494 << target_audio_bitrate_bps << " bps is ignored.";
522 return; 495 return;
523 } 496 }
524 const int overhead_bps = static_cast<int>( 497 const int overhead_bps = static_cast<int>(
525 *overhead_bytes_per_packet_ * 8 * 100 / Num10MsFramesInNextPacket()); 498 *overhead_bytes_per_packet_ * 8 * 100 / Num10MsFramesInNextPacket());
526 SetTargetBitrate(std::min( 499 SetTargetBitrate(
527 kOpusMaxBitrateBps, 500 std::min(AudioEncoderOpusConfig::kMaxBitrateBps,
ossu 2017/06/16 09:53:53 A candidate for SafeClamp here. Perhaps not in thi
kwiberg-webrtc 2017/06/16 12:44:14 Yes, this part of the CL is just search+replace fo
528 std::max(kOpusMinBitrateBps, target_audio_bitrate_bps - overhead_bps))); 501 std::max(AudioEncoderOpusConfig::kMinBitrateBps,
502 target_audio_bitrate_bps - overhead_bps)));
529 } else { 503 } else {
530 SetTargetBitrate(target_audio_bitrate_bps); 504 SetTargetBitrate(target_audio_bitrate_bps);
531 } 505 }
532 } 506 }
533 507
534 void AudioEncoderOpus::OnReceivedRtt(int rtt_ms) { 508 void AudioEncoderOpusImpl::OnReceivedRtt(int rtt_ms) {
535 if (!audio_network_adaptor_) 509 if (!audio_network_adaptor_)
536 return; 510 return;
537 audio_network_adaptor_->SetRtt(rtt_ms); 511 audio_network_adaptor_->SetRtt(rtt_ms);
538 ApplyAudioNetworkAdaptor(); 512 ApplyAudioNetworkAdaptor();
539 } 513 }
540 514
541 void AudioEncoderOpus::OnReceivedOverhead(size_t overhead_bytes_per_packet) { 515 void AudioEncoderOpusImpl::OnReceivedOverhead(
516 size_t overhead_bytes_per_packet) {
542 if (audio_network_adaptor_) { 517 if (audio_network_adaptor_) {
543 audio_network_adaptor_->SetOverhead(overhead_bytes_per_packet); 518 audio_network_adaptor_->SetOverhead(overhead_bytes_per_packet);
544 ApplyAudioNetworkAdaptor(); 519 ApplyAudioNetworkAdaptor();
545 } else { 520 } else {
546 overhead_bytes_per_packet_ = 521 overhead_bytes_per_packet_ =
547 rtc::Optional<size_t>(overhead_bytes_per_packet); 522 rtc::Optional<size_t>(overhead_bytes_per_packet);
548 } 523 }
549 } 524 }
550 525
551 void AudioEncoderOpus::SetReceiverFrameLengthRange(int min_frame_length_ms, 526 void AudioEncoderOpusImpl::SetReceiverFrameLengthRange(
552 int max_frame_length_ms) { 527 int min_frame_length_ms,
528 int max_frame_length_ms) {
553 // Ensure that |SetReceiverFrameLengthRange| is called before 529 // Ensure that |SetReceiverFrameLengthRange| is called before
554 // |EnableAudioNetworkAdaptor|, otherwise we need to recreate 530 // |EnableAudioNetworkAdaptor|, otherwise we need to recreate
555 // |audio_network_adaptor_|, which is not a needed use case. 531 // |audio_network_adaptor_|, which is not a needed use case.
556 RTC_DCHECK(!audio_network_adaptor_); 532 RTC_DCHECK(!audio_network_adaptor_);
557 FindSupportedFrameLengths(min_frame_length_ms, max_frame_length_ms, 533 FindSupportedFrameLengths(min_frame_length_ms, max_frame_length_ms,
558 &config_.supported_frame_lengths_ms); 534 &config_.supported_frame_lengths_ms);
559 } 535 }
560 536
561 AudioEncoder::EncodedInfo AudioEncoderOpus::EncodeImpl( 537 AudioEncoder::EncodedInfo AudioEncoderOpusImpl::EncodeImpl(
562 uint32_t rtp_timestamp, 538 uint32_t rtp_timestamp,
563 rtc::ArrayView<const int16_t> audio, 539 rtc::ArrayView<const int16_t> audio,
564 rtc::Buffer* encoded) { 540 rtc::Buffer* encoded) {
565 MaybeUpdateUplinkBandwidth(); 541 MaybeUpdateUplinkBandwidth();
566 542
567 if (input_buffer_.empty()) 543 if (input_buffer_.empty())
568 first_timestamp_in_buffer_ = rtp_timestamp; 544 first_timestamp_in_buffer_ = rtp_timestamp;
569 545
570 input_buffer_.insert(input_buffer_.end(), audio.cbegin(), audio.cend()); 546 input_buffer_.insert(input_buffer_.end(), audio.cbegin(), audio.cend());
571 if (input_buffer_.size() < 547 if (input_buffer_.size() <
(...skipping 18 matching lines...) Expand all
590 RTC_CHECK_GE(status, 0); // Fails only if fed invalid data. 566 RTC_CHECK_GE(status, 0); // Fails only if fed invalid data.
591 567
592 return static_cast<size_t>(status); 568 return static_cast<size_t>(status);
593 }); 569 });
594 input_buffer_.clear(); 570 input_buffer_.clear();
595 571
596 // Will use new packet size for next encoding. 572 // Will use new packet size for next encoding.
597 config_.frame_size_ms = next_frame_length_ms_; 573 config_.frame_size_ms = next_frame_length_ms_;
598 574
599 info.encoded_timestamp = first_timestamp_in_buffer_; 575 info.encoded_timestamp = first_timestamp_in_buffer_;
600 info.payload_type = config_.payload_type; 576 info.payload_type = payload_type_;
601 info.send_even_if_empty = true; // Allows Opus to send empty packets. 577 info.send_even_if_empty = true; // Allows Opus to send empty packets.
602 info.speech = (info.encoded_bytes > 0); 578 info.speech = (info.encoded_bytes > 0);
603 info.encoder_type = CodecType::kOpus; 579 info.encoder_type = CodecType::kOpus;
604 return info; 580 return info;
605 } 581 }
606 582
607 size_t AudioEncoderOpus::Num10msFramesPerPacket() const { 583 size_t AudioEncoderOpusImpl::Num10msFramesPerPacket() const {
608 return static_cast<size_t>(rtc::CheckedDivExact(config_.frame_size_ms, 10)); 584 return static_cast<size_t>(rtc::CheckedDivExact(config_.frame_size_ms, 10));
609 } 585 }
610 586
611 size_t AudioEncoderOpus::SamplesPer10msFrame() const { 587 size_t AudioEncoderOpusImpl::SamplesPer10msFrame() const {
612 return rtc::CheckedDivExact(kSampleRateHz, 100) * config_.num_channels; 588 return rtc::CheckedDivExact(kSampleRateHz, 100) * config_.num_channels;
613 } 589 }
614 590
615 size_t AudioEncoderOpus::SufficientOutputBufferSize() const { 591 size_t AudioEncoderOpusImpl::SufficientOutputBufferSize() const {
616 // Calculate the number of bytes we expect the encoder to produce, 592 // Calculate the number of bytes we expect the encoder to produce,
617 // then multiply by two to give a wide margin for error. 593 // then multiply by two to give a wide margin for error.
618 const size_t bytes_per_millisecond = 594 const size_t bytes_per_millisecond =
619 static_cast<size_t>(config_.GetBitrateBps() / (1000 * 8) + 1); 595 static_cast<size_t>(GetBitrateBps(config_) / (1000 * 8) + 1);
620 const size_t approx_encoded_bytes = 596 const size_t approx_encoded_bytes =
621 Num10msFramesPerPacket() * 10 * bytes_per_millisecond; 597 Num10msFramesPerPacket() * 10 * bytes_per_millisecond;
622 return 2 * approx_encoded_bytes; 598 return 2 * approx_encoded_bytes;
623 } 599 }
624 600
625 // If the given config is OK, recreate the Opus encoder instance with those 601 // If the given config is OK, recreate the Opus encoder instance with those
626 // settings, save the config, and return true. Otherwise, do nothing and return 602 // settings, save the config, and return true. Otherwise, do nothing and return
627 // false. 603 // false.
628 bool AudioEncoderOpus::RecreateEncoderInstance(const Config& config) { 604 bool AudioEncoderOpusImpl::RecreateEncoderInstance(
605 const AudioEncoderOpusConfig& config) {
629 if (!config.IsOk()) 606 if (!config.IsOk())
630 return false; 607 return false;
631 config_ = config; 608 config_ = config;
632 if (inst_) 609 if (inst_)
633 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); 610 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_));
634 input_buffer_.clear(); 611 input_buffer_.clear();
635 input_buffer_.reserve(Num10msFramesPerPacket() * SamplesPer10msFrame()); 612 input_buffer_.reserve(Num10msFramesPerPacket() * SamplesPer10msFrame());
636 RTC_CHECK_EQ(0, WebRtcOpus_EncoderCreate(&inst_, config.num_channels, 613 RTC_CHECK_EQ(0, WebRtcOpus_EncoderCreate(
637 config.application)); 614 &inst_, config.num_channels,
638 RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, config.GetBitrateBps())); 615 config.application ==
616 AudioEncoderOpusConfig::ApplicationMode::kVoip
617 ? 0
618 : 1));
619 RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, GetBitrateBps(config)));
639 if (config.fec_enabled) { 620 if (config.fec_enabled) {
640 RTC_CHECK_EQ(0, WebRtcOpus_EnableFec(inst_)); 621 RTC_CHECK_EQ(0, WebRtcOpus_EnableFec(inst_));
641 } else { 622 } else {
642 RTC_CHECK_EQ(0, WebRtcOpus_DisableFec(inst_)); 623 RTC_CHECK_EQ(0, WebRtcOpus_DisableFec(inst_));
643 } 624 }
644 RTC_CHECK_EQ( 625 RTC_CHECK_EQ(
645 0, WebRtcOpus_SetMaxPlaybackRate(inst_, config.max_playback_rate_hz)); 626 0, WebRtcOpus_SetMaxPlaybackRate(inst_, config.max_playback_rate_hz));
646 // Use the default complexity if the start bitrate is within the hysteresis 627 // Use the default complexity if the start bitrate is within the hysteresis
647 // window. 628 // window.
648 complexity_ = config.GetNewComplexity().value_or(config.complexity); 629 complexity_ = GetNewComplexity(config).value_or(config.complexity);
649 RTC_CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, complexity_)); 630 RTC_CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, complexity_));
650 if (config.dtx_enabled) { 631 if (config.dtx_enabled) {
651 RTC_CHECK_EQ(0, WebRtcOpus_EnableDtx(inst_)); 632 RTC_CHECK_EQ(0, WebRtcOpus_EnableDtx(inst_));
652 } else { 633 } else {
653 RTC_CHECK_EQ(0, WebRtcOpus_DisableDtx(inst_)); 634 RTC_CHECK_EQ(0, WebRtcOpus_DisableDtx(inst_));
654 } 635 }
655 RTC_CHECK_EQ(0, 636 RTC_CHECK_EQ(0,
656 WebRtcOpus_SetPacketLossRate( 637 WebRtcOpus_SetPacketLossRate(
657 inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5))); 638 inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5)));
658 if (config.cbr_enabled) { 639 if (config.cbr_enabled) {
659 RTC_CHECK_EQ(0, WebRtcOpus_EnableCbr(inst_)); 640 RTC_CHECK_EQ(0, WebRtcOpus_EnableCbr(inst_));
660 } else { 641 } else {
661 RTC_CHECK_EQ(0, WebRtcOpus_DisableCbr(inst_)); 642 RTC_CHECK_EQ(0, WebRtcOpus_DisableCbr(inst_));
662 } 643 }
663 num_channels_to_encode_ = NumChannels(); 644 num_channels_to_encode_ = NumChannels();
664 next_frame_length_ms_ = config_.frame_size_ms; 645 next_frame_length_ms_ = config_.frame_size_ms;
665 return true; 646 return true;
666 } 647 }
667 648
668 void AudioEncoderOpus::SetFrameLength(int frame_length_ms) { 649 void AudioEncoderOpusImpl::SetFrameLength(int frame_length_ms) {
669 next_frame_length_ms_ = frame_length_ms; 650 next_frame_length_ms_ = frame_length_ms;
670 } 651 }
671 652
672 void AudioEncoderOpus::SetNumChannelsToEncode(size_t num_channels_to_encode) { 653 void AudioEncoderOpusImpl::SetNumChannelsToEncode(
654 size_t num_channels_to_encode) {
673 RTC_DCHECK_GT(num_channels_to_encode, 0); 655 RTC_DCHECK_GT(num_channels_to_encode, 0);
674 RTC_DCHECK_LE(num_channels_to_encode, config_.num_channels); 656 RTC_DCHECK_LE(num_channels_to_encode, config_.num_channels);
675 657
676 if (num_channels_to_encode_ == num_channels_to_encode) 658 if (num_channels_to_encode_ == num_channels_to_encode)
677 return; 659 return;
678 660
679 RTC_CHECK_EQ(0, WebRtcOpus_SetForceChannels(inst_, num_channels_to_encode)); 661 RTC_CHECK_EQ(0, WebRtcOpus_SetForceChannels(inst_, num_channels_to_encode));
680 num_channels_to_encode_ = num_channels_to_encode; 662 num_channels_to_encode_ = num_channels_to_encode;
681 } 663 }
682 664
683 void AudioEncoderOpus::SetProjectedPacketLossRate(float fraction) { 665 void AudioEncoderOpusImpl::SetProjectedPacketLossRate(float fraction) {
684 float opt_loss_rate = OptimizePacketLossRate(fraction, packet_loss_rate_); 666 float opt_loss_rate = OptimizePacketLossRate(fraction, packet_loss_rate_);
685 if (packet_loss_rate_ != opt_loss_rate) { 667 if (packet_loss_rate_ != opt_loss_rate) {
686 packet_loss_rate_ = opt_loss_rate; 668 packet_loss_rate_ = opt_loss_rate;
687 RTC_CHECK_EQ( 669 RTC_CHECK_EQ(
688 0, WebRtcOpus_SetPacketLossRate( 670 0, WebRtcOpus_SetPacketLossRate(
689 inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5))); 671 inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5)));
690 } 672 }
691 } 673 }
692 674
693 void AudioEncoderOpus::SetTargetBitrate(int bits_per_second) { 675 void AudioEncoderOpusImpl::SetTargetBitrate(int bits_per_second) {
694 config_.bitrate_bps = rtc::Optional<int>(rtc::SafeClamp<int>( 676 config_.bitrate_bps = rtc::SafeClamp<int>(
695 bits_per_second, kOpusMinBitrateBps, kOpusMaxBitrateBps)); 677 bits_per_second, AudioEncoderOpusConfig::kMinBitrateBps,
678 AudioEncoderOpusConfig::kMaxBitrateBps);
696 RTC_DCHECK(config_.IsOk()); 679 RTC_DCHECK(config_.IsOk());
697 RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, config_.GetBitrateBps())); 680 RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, GetBitrateBps(config_)));
698 const auto new_complexity = config_.GetNewComplexity(); 681 const auto new_complexity = GetNewComplexity(config_);
699 if (new_complexity && complexity_ != *new_complexity) { 682 if (new_complexity && complexity_ != *new_complexity) {
700 complexity_ = *new_complexity; 683 complexity_ = *new_complexity;
701 RTC_CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, complexity_)); 684 RTC_CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, complexity_));
702 } 685 }
703 } 686 }
704 687
705 void AudioEncoderOpus::ApplyAudioNetworkAdaptor() { 688 void AudioEncoderOpusImpl::ApplyAudioNetworkAdaptor() {
706 auto config = audio_network_adaptor_->GetEncoderRuntimeConfig(); 689 auto config = audio_network_adaptor_->GetEncoderRuntimeConfig();
707 RTC_DCHECK(!config.frame_length_ms || *config.frame_length_ms == 20 || 690 RTC_DCHECK(!config.frame_length_ms || *config.frame_length_ms == 20 ||
708 *config.frame_length_ms == 60); 691 *config.frame_length_ms == 60);
709 692
710 if (config.bitrate_bps) 693 if (config.bitrate_bps)
711 SetTargetBitrate(*config.bitrate_bps); 694 SetTargetBitrate(*config.bitrate_bps);
712 if (config.frame_length_ms) 695 if (config.frame_length_ms)
713 SetFrameLength(*config.frame_length_ms); 696 SetFrameLength(*config.frame_length_ms);
714 if (config.enable_fec) 697 if (config.enable_fec)
715 SetFec(*config.enable_fec); 698 SetFec(*config.enable_fec);
716 if (config.uplink_packet_loss_fraction) 699 if (config.uplink_packet_loss_fraction)
717 SetProjectedPacketLossRate(*config.uplink_packet_loss_fraction); 700 SetProjectedPacketLossRate(*config.uplink_packet_loss_fraction);
718 if (config.enable_dtx) 701 if (config.enable_dtx)
719 SetDtx(*config.enable_dtx); 702 SetDtx(*config.enable_dtx);
720 if (config.num_channels) 703 if (config.num_channels)
721 SetNumChannelsToEncode(*config.num_channels); 704 SetNumChannelsToEncode(*config.num_channels);
722 } 705 }
723 706
724 std::unique_ptr<AudioNetworkAdaptor> 707 std::unique_ptr<AudioNetworkAdaptor>
725 AudioEncoderOpus::DefaultAudioNetworkAdaptorCreator( 708 AudioEncoderOpusImpl::DefaultAudioNetworkAdaptorCreator(
726 const ProtoString& config_string, 709 const ProtoString& config_string,
727 RtcEventLog* event_log) const { 710 RtcEventLog* event_log) const {
728 AudioNetworkAdaptorImpl::Config config; 711 AudioNetworkAdaptorImpl::Config config;
729 config.event_log = event_log; 712 config.event_log = event_log;
730 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( 713 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl(
731 config, 714 config, ControllerManagerImpl::Create(
732 ControllerManagerImpl::Create( 715 config_string, NumChannels(), supported_frame_lengths_ms(),
733 config_string, NumChannels(), supported_frame_lengths_ms(), 716 AudioEncoderOpusConfig::kMinBitrateBps,
734 kOpusMinBitrateBps, num_channels_to_encode_, next_frame_length_ms_, 717 num_channels_to_encode_, next_frame_length_ms_,
735 GetTargetBitrate(), config_.fec_enabled, GetDtx()))); 718 GetTargetBitrate(), config_.fec_enabled, GetDtx())));
736 } 719 }
737 720
738 void AudioEncoderOpus::MaybeUpdateUplinkBandwidth() { 721 void AudioEncoderOpusImpl::MaybeUpdateUplinkBandwidth() {
739 if (audio_network_adaptor_) { 722 if (audio_network_adaptor_) {
740 int64_t now_ms = rtc::TimeMillis(); 723 int64_t now_ms = rtc::TimeMillis();
741 if (!bitrate_smoother_last_update_time_ || 724 if (!bitrate_smoother_last_update_time_ ||
742 now_ms - *bitrate_smoother_last_update_time_ >= 725 now_ms - *bitrate_smoother_last_update_time_ >=
743 config_.uplink_bandwidth_update_interval_ms) { 726 config_.uplink_bandwidth_update_interval_ms) {
744 rtc::Optional<float> smoothed_bitrate = bitrate_smoother_->GetAverage(); 727 rtc::Optional<float> smoothed_bitrate = bitrate_smoother_->GetAverage();
745 if (smoothed_bitrate) 728 if (smoothed_bitrate)
746 audio_network_adaptor_->SetUplinkBandwidth(*smoothed_bitrate); 729 audio_network_adaptor_->SetUplinkBandwidth(*smoothed_bitrate);
747 bitrate_smoother_last_update_time_ = rtc::Optional<int64_t>(now_ms); 730 bitrate_smoother_last_update_time_ = rtc::Optional<int64_t>(now_ms);
748 } 731 }
749 } 732 }
750 } 733 }
751 734
752 } // namespace webrtc 735 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698