OLD | NEW |
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 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 const int max_frame_length_ms = | 277 const int max_frame_length_ms = |
278 GetFormatParameter<int>(format, "maxptime").value_or(kMaxANAFrameLength); | 278 GetFormatParameter<int>(format, "maxptime").value_or(kMaxANAFrameLength); |
279 | 279 |
280 FindSupportedFrameLengths(min_frame_length_ms, max_frame_length_ms, | 280 FindSupportedFrameLengths(min_frame_length_ms, max_frame_length_ms, |
281 &config.supported_frame_lengths_ms); | 281 &config.supported_frame_lengths_ms); |
282 return config; | 282 return config; |
283 } | 283 } |
284 | 284 |
285 class AudioEncoderOpus::PacketLossFractionSmoother { | 285 class AudioEncoderOpus::PacketLossFractionSmoother { |
286 public: | 286 public: |
287 explicit PacketLossFractionSmoother(const Clock* clock) | 287 explicit PacketLossFractionSmoother() |
288 : clock_(clock), | 288 : last_sample_time_ms_(rtc::TimeMillis()), |
289 last_sample_time_ms_(clock_->TimeInMilliseconds()), | |
290 smoother_(kAlphaForPacketLossFractionSmoother) {} | 289 smoother_(kAlphaForPacketLossFractionSmoother) {} |
291 | 290 |
292 // Gets the smoothed packet loss fraction. | 291 // Gets the smoothed packet loss fraction. |
293 float GetAverage() const { | 292 float GetAverage() const { |
294 float value = smoother_.filtered(); | 293 float value = smoother_.filtered(); |
295 return (value == rtc::ExpFilter::kValueUndefined) ? 0.0f : value; | 294 return (value == rtc::ExpFilter::kValueUndefined) ? 0.0f : value; |
296 } | 295 } |
297 | 296 |
298 // Add new observation to the packet loss fraction smoother. | 297 // Add new observation to the packet loss fraction smoother. |
299 void AddSample(float packet_loss_fraction) { | 298 void AddSample(float packet_loss_fraction) { |
300 int64_t now_ms = clock_->TimeInMilliseconds(); | 299 int64_t now_ms = rtc::TimeMillis(); |
301 smoother_.Apply(static_cast<float>(now_ms - last_sample_time_ms_), | 300 smoother_.Apply(static_cast<float>(now_ms - last_sample_time_ms_), |
302 packet_loss_fraction); | 301 packet_loss_fraction); |
303 last_sample_time_ms_ = now_ms; | 302 last_sample_time_ms_ = now_ms; |
304 } | 303 } |
305 | 304 |
306 private: | 305 private: |
307 const Clock* const clock_; | |
308 int64_t last_sample_time_ms_; | 306 int64_t last_sample_time_ms_; |
309 | 307 |
310 // An exponential filter is used to smooth the packet loss fraction. | 308 // An exponential filter is used to smooth the packet loss fraction. |
311 rtc::ExpFilter smoother_; | 309 rtc::ExpFilter smoother_; |
312 }; | 310 }; |
313 | 311 |
314 AudioEncoderOpus::Config::Config() { | 312 AudioEncoderOpus::Config::Config() { |
315 #if WEBRTC_OPUS_VARIABLE_COMPLEXITY | 313 #if WEBRTC_OPUS_VARIABLE_COMPLEXITY |
316 low_rate_complexity = 9; | 314 low_rate_complexity = 9; |
317 #endif | 315 #endif |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 } | 357 } |
360 | 358 |
361 AudioEncoderOpus::AudioEncoderOpus( | 359 AudioEncoderOpus::AudioEncoderOpus( |
362 const Config& config, | 360 const Config& config, |
363 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator, | 361 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator, |
364 std::unique_ptr<SmoothingFilter> bitrate_smoother) | 362 std::unique_ptr<SmoothingFilter> bitrate_smoother) |
365 : send_side_bwe_with_overhead_(webrtc::field_trial::IsEnabled( | 363 : send_side_bwe_with_overhead_(webrtc::field_trial::IsEnabled( |
366 "WebRTC-SendSideBwe-WithOverhead")), | 364 "WebRTC-SendSideBwe-WithOverhead")), |
367 packet_loss_rate_(0.0), | 365 packet_loss_rate_(0.0), |
368 inst_(nullptr), | 366 inst_(nullptr), |
369 packet_loss_fraction_smoother_(new PacketLossFractionSmoother( | 367 packet_loss_fraction_smoother_(new PacketLossFractionSmoother()), |
370 config.clock)), | |
371 audio_network_adaptor_creator_( | 368 audio_network_adaptor_creator_( |
372 audio_network_adaptor_creator | 369 audio_network_adaptor_creator |
373 ? std::move(audio_network_adaptor_creator) | 370 ? std::move(audio_network_adaptor_creator) |
374 : [this](const ProtoString& config_string, | 371 : [this](const ProtoString& config_string, |
375 RtcEventLog* event_log, | 372 RtcEventLog* event_log) { |
376 const Clock* clock) { | |
377 return DefaultAudioNetworkAdaptorCreator(config_string, | 373 return DefaultAudioNetworkAdaptorCreator(config_string, |
378 event_log, clock); | 374 event_log); |
379 }), | 375 }), |
380 bitrate_smoother_(bitrate_smoother | 376 bitrate_smoother_(bitrate_smoother |
381 ? std::move(bitrate_smoother) : std::unique_ptr<SmoothingFilter>( | 377 ? std::move(bitrate_smoother) : std::unique_ptr<SmoothingFilter>( |
382 // We choose 5sec as initial time constant due to empirical data. | 378 // We choose 5sec as initial time constant due to empirical data. |
383 new SmoothingFilterImpl(5000, config.clock))) { | 379 new SmoothingFilterImpl(5000))) { |
384 RTC_CHECK(RecreateEncoderInstance(config)); | 380 RTC_CHECK(RecreateEncoderInstance(config)); |
385 } | 381 } |
386 | 382 |
387 AudioEncoderOpus::AudioEncoderOpus(const CodecInst& codec_inst) | 383 AudioEncoderOpus::AudioEncoderOpus(const CodecInst& codec_inst) |
388 : AudioEncoderOpus(CreateConfig(codec_inst), nullptr) {} | 384 : AudioEncoderOpus(CreateConfig(codec_inst), nullptr) {} |
389 | 385 |
390 AudioEncoderOpus::AudioEncoderOpus(int payload_type, | 386 AudioEncoderOpus::AudioEncoderOpus(int payload_type, |
391 const SdpAudioFormat& format) | 387 const SdpAudioFormat& format) |
392 : AudioEncoderOpus(CreateConfig(payload_type, format), nullptr) {} | 388 : AudioEncoderOpus(CreateConfig(payload_type, format), nullptr) {} |
393 | 389 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 } | 453 } |
458 | 454 |
459 void AudioEncoderOpus::SetMaxPlaybackRate(int frequency_hz) { | 455 void AudioEncoderOpus::SetMaxPlaybackRate(int frequency_hz) { |
460 auto conf = config_; | 456 auto conf = config_; |
461 conf.max_playback_rate_hz = frequency_hz; | 457 conf.max_playback_rate_hz = frequency_hz; |
462 RTC_CHECK(RecreateEncoderInstance(conf)); | 458 RTC_CHECK(RecreateEncoderInstance(conf)); |
463 } | 459 } |
464 | 460 |
465 bool AudioEncoderOpus::EnableAudioNetworkAdaptor( | 461 bool AudioEncoderOpus::EnableAudioNetworkAdaptor( |
466 const std::string& config_string, | 462 const std::string& config_string, |
467 RtcEventLog* event_log, | 463 RtcEventLog* event_log) { |
468 const Clock* clock) { | |
469 audio_network_adaptor_ = | 464 audio_network_adaptor_ = |
470 audio_network_adaptor_creator_(config_string, event_log, clock); | 465 audio_network_adaptor_creator_(config_string, event_log); |
471 return audio_network_adaptor_.get() != nullptr; | 466 return audio_network_adaptor_.get() != nullptr; |
472 } | 467 } |
473 | 468 |
474 void AudioEncoderOpus::DisableAudioNetworkAdaptor() { | 469 void AudioEncoderOpus::DisableAudioNetworkAdaptor() { |
475 audio_network_adaptor_.reset(nullptr); | 470 audio_network_adaptor_.reset(nullptr); |
476 } | 471 } |
477 | 472 |
478 void AudioEncoderOpus::OnReceivedUplinkPacketLossFraction( | 473 void AudioEncoderOpus::OnReceivedUplinkPacketLossFraction( |
479 float uplink_packet_loss_fraction) { | 474 float uplink_packet_loss_fraction) { |
480 if (!audio_network_adaptor_) { | 475 if (!audio_network_adaptor_) { |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
716 SetProjectedPacketLossRate(*config.uplink_packet_loss_fraction); | 711 SetProjectedPacketLossRate(*config.uplink_packet_loss_fraction); |
717 if (config.enable_dtx) | 712 if (config.enable_dtx) |
718 SetDtx(*config.enable_dtx); | 713 SetDtx(*config.enable_dtx); |
719 if (config.num_channels) | 714 if (config.num_channels) |
720 SetNumChannelsToEncode(*config.num_channels); | 715 SetNumChannelsToEncode(*config.num_channels); |
721 } | 716 } |
722 | 717 |
723 std::unique_ptr<AudioNetworkAdaptor> | 718 std::unique_ptr<AudioNetworkAdaptor> |
724 AudioEncoderOpus::DefaultAudioNetworkAdaptorCreator( | 719 AudioEncoderOpus::DefaultAudioNetworkAdaptorCreator( |
725 const ProtoString& config_string, | 720 const ProtoString& config_string, |
726 RtcEventLog* event_log, | 721 RtcEventLog* event_log) const { |
727 const Clock* clock) const { | |
728 AudioNetworkAdaptorImpl::Config config; | 722 AudioNetworkAdaptorImpl::Config config; |
729 config.clock = clock; | |
730 config.event_log = event_log; | 723 config.event_log = event_log; |
731 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( | 724 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( |
732 config, | 725 config, |
733 ControllerManagerImpl::Create( | 726 ControllerManagerImpl::Create( |
734 config_string, NumChannels(), supported_frame_lengths_ms(), | 727 config_string, NumChannels(), supported_frame_lengths_ms(), |
735 kOpusMinBitrateBps, num_channels_to_encode_, next_frame_length_ms_, | 728 kOpusMinBitrateBps, num_channels_to_encode_, next_frame_length_ms_, |
736 GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); | 729 GetTargetBitrate(), config_.fec_enabled, GetDtx()))); |
737 } | 730 } |
738 | 731 |
739 void AudioEncoderOpus::MaybeUpdateUplinkBandwidth() { | 732 void AudioEncoderOpus::MaybeUpdateUplinkBandwidth() { |
740 if (audio_network_adaptor_) { | 733 if (audio_network_adaptor_) { |
741 int64_t now_ms = rtc::TimeMillis(); | 734 int64_t now_ms = rtc::TimeMillis(); |
742 if (!bitrate_smoother_last_update_time_ || | 735 if (!bitrate_smoother_last_update_time_ || |
743 now_ms - *bitrate_smoother_last_update_time_ >= | 736 now_ms - *bitrate_smoother_last_update_time_ >= |
744 config_.uplink_bandwidth_update_interval_ms) { | 737 config_.uplink_bandwidth_update_interval_ms) { |
745 rtc::Optional<float> smoothed_bitrate = bitrate_smoother_->GetAverage(); | 738 rtc::Optional<float> smoothed_bitrate = bitrate_smoother_->GetAverage(); |
746 if (smoothed_bitrate) | 739 if (smoothed_bitrate) |
747 audio_network_adaptor_->SetUplinkBandwidth(*smoothed_bitrate); | 740 audio_network_adaptor_->SetUplinkBandwidth(*smoothed_bitrate); |
748 bitrate_smoother_last_update_time_ = rtc::Optional<int64_t>(now_ms); | 741 bitrate_smoother_last_update_time_ = rtc::Optional<int64_t>(now_ms); |
749 } | 742 } |
750 } | 743 } |
751 } | 744 } |
752 | 745 |
753 } // namespace webrtc | 746 } // namespace webrtc |
OLD | NEW |