Chromium Code Reviews| 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 |
| 11 #include "webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.h" | 11 #include "webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.h" |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 | 14 |
| 15 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
| 16 #include "webrtc/base/exp_filter.h" | |
| 16 #include "webrtc/base/safe_conversions.h" | 17 #include "webrtc/base/safe_conversions.h" |
| 17 #include "webrtc/common_types.h" | 18 #include "webrtc/common_types.h" |
| 18 #include "webrtc/modules/audio_coding/audio_network_adaptor/audio_network_adapto r_impl.h" | 19 #include "webrtc/modules/audio_coding/audio_network_adaptor/audio_network_adapto r_impl.h" |
| 19 #include "webrtc/modules/audio_coding/audio_network_adaptor/controller_manager.h " | 20 #include "webrtc/modules/audio_coding/audio_network_adaptor/controller_manager.h " |
| 20 #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h" | 21 #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h" |
| 21 #include "webrtc/system_wrappers/include/clock.h" | 22 #include "webrtc/system_wrappers/include/clock.h" |
| 22 | 23 |
| 23 namespace webrtc { | 24 namespace webrtc { |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 kLossRate5Margin * | 74 kLossRate5Margin * |
| 74 (kPacketLossRate5 - old_loss_rate > 0 ? 1 : -1)) { | 75 (kPacketLossRate5 - old_loss_rate > 0 ? 1 : -1)) { |
| 75 return kPacketLossRate5; | 76 return kPacketLossRate5; |
| 76 } else if (new_loss_rate >= kPacketLossRate1) { | 77 } else if (new_loss_rate >= kPacketLossRate1) { |
| 77 return kPacketLossRate1; | 78 return kPacketLossRate1; |
| 78 } else { | 79 } else { |
| 79 return 0.0; | 80 return 0.0; |
| 80 } | 81 } |
| 81 } | 82 } |
| 82 | 83 |
| 84 class PacketLossFractionSmoother { | |
| 85 public: | |
| 86 explicit PacketLossFractionSmoother(const Clock* clock) | |
| 87 : clock_(clock), | |
| 88 last_sample_time_ms_(clock_->TimeInMilliseconds()), | |
| 89 smoother_(new rtc::ExpFilter(0.9999f)) {} | |
| 90 | |
| 91 ~PacketLossFractionSmoother() = default; | |
|
kwiberg-webrtc
2016/10/11 14:56:19
I don't think this line does anything. This is wha
minyue-webrtc
2016/10/11 15:31:09
Done.
| |
| 92 | |
| 93 // Gets the smoothed packet loss fraction. | |
| 94 float GetAverage() const { | |
| 95 float value = smoother_->filtered(); | |
| 96 return (value == rtc::ExpFilter::kValueUndefined) ? 0.0f : value; | |
| 97 } | |
| 98 | |
| 99 // Add new observation to the packet loss fraction smoother. | |
| 100 void AddSample(float packet_loss_fraction) { | |
| 101 int64_t now_ms = clock_->TimeInMilliseconds(); | |
| 102 smoother_->Apply(static_cast<float>(now_ms - last_sample_time_ms_), | |
| 103 packet_loss_fraction); | |
| 104 last_sample_time_ms_ = now_ms; | |
| 105 } | |
| 106 | |
| 107 private: | |
| 108 const Clock* const clock_; | |
| 109 int64_t last_sample_time_ms_; | |
| 110 | |
| 111 // An exponential filter is used to smooth the packet loss fraction. | |
| 112 std::unique_ptr<rtc::ExpFilter> smoother_; | |
|
kwiberg-webrtc
2016/10/11 14:56:19
Do you need to point to the ExpFilter? Can't you e
minyue-webrtc
2016/10/11 15:31:09
no. I simply copied from the old implementation. B
| |
| 113 }; | |
| 114 | |
| 83 } // namespace | 115 } // namespace |
| 84 | 116 |
| 85 AudioEncoderOpus::Config::Config() = default; | 117 AudioEncoderOpus::Config::Config() = default; |
| 86 AudioEncoderOpus::Config::Config(const Config&) = default; | 118 AudioEncoderOpus::Config::Config(const Config&) = default; |
| 87 AudioEncoderOpus::Config::~Config() = default; | 119 AudioEncoderOpus::Config::~Config() = default; |
| 88 auto AudioEncoderOpus::Config::operator=(const Config&) -> Config& = default; | 120 auto AudioEncoderOpus::Config::operator=(const Config&) -> Config& = default; |
| 89 | 121 |
| 90 bool AudioEncoderOpus::Config::IsOk() const { | 122 bool AudioEncoderOpus::Config::IsOk() const { |
| 91 if (frame_size_ms <= 0 || frame_size_ms % 10 != 0) | 123 if (frame_size_ms <= 0 || frame_size_ms % 10 != 0) |
| 92 return false; | 124 return false; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 106 return *bitrate_bps; // Explicitly set value. | 138 return *bitrate_bps; // Explicitly set value. |
| 107 else | 139 else |
| 108 return num_channels == 1 ? 32000 : 64000; // Default value. | 140 return num_channels == 1 ? 32000 : 64000; // Default value. |
| 109 } | 141 } |
| 110 | 142 |
| 111 AudioEncoderOpus::AudioEncoderOpus( | 143 AudioEncoderOpus::AudioEncoderOpus( |
| 112 const Config& config, | 144 const Config& config, |
| 113 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator) | 145 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator) |
| 114 : packet_loss_rate_(0.0), | 146 : packet_loss_rate_(0.0), |
| 115 inst_(nullptr), | 147 inst_(nullptr), |
| 148 packet_loss_fraction_smoother_(new PacketLossFractionSmoother( | |
| 149 config.clock ? config.clock : Clock::GetRealTimeClock())), | |
| 116 audio_network_adaptor_creator_( | 150 audio_network_adaptor_creator_( |
| 117 audio_network_adaptor_creator | 151 audio_network_adaptor_creator |
| 118 ? audio_network_adaptor_creator | 152 ? std::move(audio_network_adaptor_creator) |
| 119 : [this](const std::string& config_string, const Clock* clock) { | 153 : [this](const std::string& config_string, const Clock* clock) { |
| 120 return DefaultAudioNetworkAdaptorCreator(config_string, | 154 return DefaultAudioNetworkAdaptorCreator(config_string, |
| 121 clock); | 155 clock); |
| 122 }) { | 156 }) { |
| 123 RTC_CHECK(RecreateEncoderInstance(config)); | 157 RTC_CHECK(RecreateEncoderInstance(config)); |
| 124 } | 158 } |
| 125 | 159 |
| 126 AudioEncoderOpus::AudioEncoderOpus(const CodecInst& codec_inst) | 160 AudioEncoderOpus::AudioEncoderOpus(const CodecInst& codec_inst) |
| 127 : AudioEncoderOpus(CreateConfig(codec_inst), nullptr) {} | 161 : AudioEncoderOpus(CreateConfig(codec_inst), nullptr) {} |
| 128 | 162 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 | 261 |
| 228 void AudioEncoderOpus::OnReceivedUplinkBandwidth(int uplink_bandwidth_bps) { | 262 void AudioEncoderOpus::OnReceivedUplinkBandwidth(int uplink_bandwidth_bps) { |
| 229 if (!audio_network_adaptor_) | 263 if (!audio_network_adaptor_) |
| 230 return; | 264 return; |
| 231 audio_network_adaptor_->SetUplinkBandwidth(uplink_bandwidth_bps); | 265 audio_network_adaptor_->SetUplinkBandwidth(uplink_bandwidth_bps); |
| 232 ApplyAudioNetworkAdaptor(); | 266 ApplyAudioNetworkAdaptor(); |
| 233 } | 267 } |
| 234 | 268 |
| 235 void AudioEncoderOpus::OnReceivedUplinkPacketLossFraction( | 269 void AudioEncoderOpus::OnReceivedUplinkPacketLossFraction( |
| 236 float uplink_packet_loss_fraction) { | 270 float uplink_packet_loss_fraction) { |
| 237 if (!audio_network_adaptor_) | 271 if (!audio_network_adaptor_) { |
| 238 return; | 272 packet_loss_fraction_smoother_->AddSample(uplink_packet_loss_fraction); |
| 273 float average_fraction_loss = packet_loss_fraction_smoother_->GetAverage(); | |
| 274 return SetProjectedPacketLossRate(average_fraction_loss); | |
| 275 } | |
| 239 audio_network_adaptor_->SetUplinkPacketLossFraction( | 276 audio_network_adaptor_->SetUplinkPacketLossFraction( |
| 240 uplink_packet_loss_fraction); | 277 uplink_packet_loss_fraction); |
| 241 ApplyAudioNetworkAdaptor(); | 278 ApplyAudioNetworkAdaptor(); |
| 242 } | 279 } |
| 243 | 280 |
| 244 void AudioEncoderOpus::OnReceivedTargetAudioBitrate( | 281 void AudioEncoderOpus::OnReceivedTargetAudioBitrate( |
| 245 int target_audio_bitrate_bps) { | 282 int target_audio_bitrate_bps) { |
| 246 if (!audio_network_adaptor_) | 283 if (!audio_network_adaptor_) |
| 247 return; | 284 return SetTargetBitrate(target_audio_bitrate_bps); |
| 248 audio_network_adaptor_->SetTargetAudioBitrate(target_audio_bitrate_bps); | 285 audio_network_adaptor_->SetTargetAudioBitrate(target_audio_bitrate_bps); |
| 249 ApplyAudioNetworkAdaptor(); | 286 ApplyAudioNetworkAdaptor(); |
| 250 } | 287 } |
| 251 | 288 |
| 252 void AudioEncoderOpus::OnReceivedRtt(int rtt_ms) { | 289 void AudioEncoderOpus::OnReceivedRtt(int rtt_ms) { |
| 253 if (!audio_network_adaptor_) | 290 if (!audio_network_adaptor_) |
| 254 return; | 291 return; |
| 255 audio_network_adaptor_->SetRtt(rtt_ms); | 292 audio_network_adaptor_->SetRtt(rtt_ms); |
| 256 ApplyAudioNetworkAdaptor(); | 293 ApplyAudioNetworkAdaptor(); |
| 257 } | 294 } |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 407 AudioNetworkAdaptorImpl::Config config; | 444 AudioNetworkAdaptorImpl::Config config; |
| 408 config.clock = clock; | 445 config.clock = clock; |
| 409 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( | 446 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( |
| 410 config, ControllerManagerImpl::Create( | 447 config, ControllerManagerImpl::Create( |
| 411 config_string, NumChannels(), kSupportedFrameLengths, | 448 config_string, NumChannels(), kSupportedFrameLengths, |
| 412 num_channels_to_encode_, next_frame_length_ms_, | 449 num_channels_to_encode_, next_frame_length_ms_, |
| 413 GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); | 450 GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); |
| 414 } | 451 } |
| 415 | 452 |
| 416 } // namespace webrtc | 453 } // namespace webrtc |
| OLD | NEW |