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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 // Within the hysteresis window; make no change. | 165 // Within the hysteresis window; make no change. |
166 return rtc::Optional<int>(); | 166 return rtc::Optional<int>(); |
167 } | 167 } |
168 return bitrate_bps <= complexity_threshold_bps | 168 return bitrate_bps <= complexity_threshold_bps |
169 ? rtc::Optional<int>(low_rate_complexity) | 169 ? rtc::Optional<int>(low_rate_complexity) |
170 : rtc::Optional<int>(complexity); | 170 : rtc::Optional<int>(complexity); |
171 } | 171 } |
172 | 172 |
173 AudioEncoderOpus::AudioEncoderOpus( | 173 AudioEncoderOpus::AudioEncoderOpus( |
174 const Config& config, | 174 const Config& config, |
175 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator) | 175 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator, |
176 std::unique_ptr<SmoothingFilter> bitrate_smoother) | |
176 : packet_loss_rate_(0.0), | 177 : packet_loss_rate_(0.0), |
177 inst_(nullptr), | 178 inst_(nullptr), |
178 packet_loss_fraction_smoother_(new PacketLossFractionSmoother( | 179 packet_loss_fraction_smoother_(new PacketLossFractionSmoother( |
179 config.clock ? config.clock : Clock::GetRealTimeClock())), | 180 config.clock ? config.clock : Clock::GetRealTimeClock())), |
180 audio_network_adaptor_creator_( | 181 audio_network_adaptor_creator_( |
181 audio_network_adaptor_creator | 182 audio_network_adaptor_creator |
182 ? std::move(audio_network_adaptor_creator) | 183 ? std::move(audio_network_adaptor_creator) |
183 : [this](const std::string& config_string, const Clock* clock) { | 184 : [this](const std::string& config_string, const Clock* clock) { |
184 return DefaultAudioNetworkAdaptorCreator(config_string, | 185 return DefaultAudioNetworkAdaptorCreator(config_string, |
185 clock); | 186 clock); |
186 }) { | 187 }), |
188 bitrate_smoother_(bitrate_smoother | |
189 ? std::move(bitrate_smoother) : std::unique_ptr<SmoothingFilter>( | |
190 new SmoothingFilterImpl(500, config.clock))) { | |
minyue-webrtc
2016/12/22 14:51:26
need to explain 500
| |
187 RTC_CHECK(RecreateEncoderInstance(config)); | 191 RTC_CHECK(RecreateEncoderInstance(config)); |
188 } | 192 } |
189 | 193 |
190 AudioEncoderOpus::AudioEncoderOpus(const CodecInst& codec_inst) | 194 AudioEncoderOpus::AudioEncoderOpus(const CodecInst& codec_inst) |
191 : AudioEncoderOpus(CreateConfig(codec_inst), nullptr) {} | 195 : AudioEncoderOpus(CreateConfig(codec_inst), nullptr) {} |
192 | 196 |
193 AudioEncoderOpus::~AudioEncoderOpus() { | 197 AudioEncoderOpus::~AudioEncoderOpus() { |
194 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); | 198 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); |
195 } | 199 } |
196 | 200 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 const std::string& config_string, | 269 const std::string& config_string, |
266 const Clock* clock) { | 270 const Clock* clock) { |
267 audio_network_adaptor_ = audio_network_adaptor_creator_(config_string, clock); | 271 audio_network_adaptor_ = audio_network_adaptor_creator_(config_string, clock); |
268 return audio_network_adaptor_.get() != nullptr; | 272 return audio_network_adaptor_.get() != nullptr; |
269 } | 273 } |
270 | 274 |
271 void AudioEncoderOpus::DisableAudioNetworkAdaptor() { | 275 void AudioEncoderOpus::DisableAudioNetworkAdaptor() { |
272 audio_network_adaptor_.reset(nullptr); | 276 audio_network_adaptor_.reset(nullptr); |
273 } | 277 } |
274 | 278 |
275 void AudioEncoderOpus::OnReceivedUplinkBandwidth(int uplink_bandwidth_bps) { | |
276 if (!audio_network_adaptor_) | |
277 return; | |
278 audio_network_adaptor_->SetUplinkBandwidth(uplink_bandwidth_bps); | |
279 ApplyAudioNetworkAdaptor(); | |
280 } | |
281 | |
282 void AudioEncoderOpus::OnReceivedUplinkPacketLossFraction( | 279 void AudioEncoderOpus::OnReceivedUplinkPacketLossFraction( |
283 float uplink_packet_loss_fraction) { | 280 float uplink_packet_loss_fraction) { |
284 if (!audio_network_adaptor_) { | 281 if (!audio_network_adaptor_) { |
285 packet_loss_fraction_smoother_->AddSample(uplink_packet_loss_fraction); | 282 packet_loss_fraction_smoother_->AddSample(uplink_packet_loss_fraction); |
286 float average_fraction_loss = packet_loss_fraction_smoother_->GetAverage(); | 283 float average_fraction_loss = packet_loss_fraction_smoother_->GetAverage(); |
287 return SetProjectedPacketLossRate(average_fraction_loss); | 284 return SetProjectedPacketLossRate(average_fraction_loss); |
288 } | 285 } |
289 audio_network_adaptor_->SetUplinkPacketLossFraction( | 286 audio_network_adaptor_->SetUplinkPacketLossFraction( |
290 uplink_packet_loss_fraction); | 287 uplink_packet_loss_fraction); |
291 ApplyAudioNetworkAdaptor(); | 288 ApplyAudioNetworkAdaptor(); |
292 } | 289 } |
293 | 290 |
294 void AudioEncoderOpus::OnReceivedTargetAudioBitrate( | 291 void AudioEncoderOpus::OnReceivedTargetAudioBitrate( |
295 int target_audio_bitrate_bps) { | 292 int target_audio_bitrate_bps, |
293 rtc::Optional<int64_t> probing_interval_ms) { | |
296 if (audio_network_adaptor_) { | 294 if (audio_network_adaptor_) { |
297 audio_network_adaptor_->SetTargetAudioBitrate(target_audio_bitrate_bps); | 295 audio_network_adaptor_->SetTargetAudioBitrate(target_audio_bitrate_bps); |
296 // We give smoothed bitrate allocation to audio network adaptor as | |
297 // the uplink bandwidth. | |
298 // The probing spikes should not affect the bitrate smoother more than 25%. | |
299 // To simplify the calculations we use a step response as input signal. | |
300 // The step response of an exponential filter is | |
301 // u(t) = 1 - e^(-t / time_constant). | |
302 // In order to limit the affect of a BWE spike within 25% of its value | |
303 // before | |
304 // the next probing, we would choose a time constant that fulfills | |
305 // 1 - e^(-probing_interval_ms / time_constant) < 0.25 | |
306 // Then 4 * probing_interval_ms is a good choice. | |
307 if (probing_interval_ms) | |
308 bitrate_smoother_->SetTimeConstantMs(*probing_interval_ms * 4); | |
309 bitrate_smoother_->AddSample(target_audio_bitrate_bps); | |
310 | |
298 ApplyAudioNetworkAdaptor(); | 311 ApplyAudioNetworkAdaptor(); |
299 } else if (webrtc::field_trial::FindFullName( | 312 } else if (webrtc::field_trial::FindFullName( |
300 "WebRTC-SendSideBwe-WithOverhead") == "Enabled") { | 313 "WebRTC-SendSideBwe-WithOverhead") == "Enabled") { |
301 if (!overhead_bytes_per_packet_) { | 314 if (!overhead_bytes_per_packet_) { |
302 LOG(LS_INFO) | 315 LOG(LS_INFO) |
303 << "AudioEncoderOpus: Overhead unknown, target audio bitrate " | 316 << "AudioEncoderOpus: Overhead unknown, target audio bitrate " |
304 << target_audio_bitrate_bps << " bps is ignored."; | 317 << target_audio_bitrate_bps << " bps is ignored."; |
305 return; | 318 return; |
306 } | 319 } |
307 const int overhead_bps = static_cast<int>( | 320 const int overhead_bps = static_cast<int>( |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
347 frame_length_ms <= max_frame_length_ms; | 360 frame_length_ms <= max_frame_length_ms; |
348 }); | 361 }); |
349 RTC_DCHECK(std::is_sorted(config_.supported_frame_lengths_ms.begin(), | 362 RTC_DCHECK(std::is_sorted(config_.supported_frame_lengths_ms.begin(), |
350 config_.supported_frame_lengths_ms.end())); | 363 config_.supported_frame_lengths_ms.end())); |
351 } | 364 } |
352 | 365 |
353 AudioEncoder::EncodedInfo AudioEncoderOpus::EncodeImpl( | 366 AudioEncoder::EncodedInfo AudioEncoderOpus::EncodeImpl( |
354 uint32_t rtp_timestamp, | 367 uint32_t rtp_timestamp, |
355 rtc::ArrayView<const int16_t> audio, | 368 rtc::ArrayView<const int16_t> audio, |
356 rtc::Buffer* encoded) { | 369 rtc::Buffer* encoded) { |
370 MayUpdateUplinkBandwidth(); | |
357 | 371 |
358 if (input_buffer_.empty()) | 372 if (input_buffer_.empty()) |
359 first_timestamp_in_buffer_ = rtp_timestamp; | 373 first_timestamp_in_buffer_ = rtp_timestamp; |
360 | 374 |
361 input_buffer_.insert(input_buffer_.end(), audio.cbegin(), audio.cend()); | 375 input_buffer_.insert(input_buffer_.end(), audio.cbegin(), audio.cend()); |
362 if (input_buffer_.size() < | 376 if (input_buffer_.size() < |
363 (Num10msFramesPerPacket() * SamplesPer10msFrame())) { | 377 (Num10msFramesPerPacket() * SamplesPer10msFrame())) { |
364 return EncodedInfo(); | 378 return EncodedInfo(); |
365 } | 379 } |
366 RTC_CHECK_EQ(input_buffer_.size(), | 380 RTC_CHECK_EQ(input_buffer_.size(), |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
516 const Clock* clock) const { | 530 const Clock* clock) const { |
517 AudioNetworkAdaptorImpl::Config config; | 531 AudioNetworkAdaptorImpl::Config config; |
518 config.clock = clock; | 532 config.clock = clock; |
519 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( | 533 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( |
520 config, ControllerManagerImpl::Create( | 534 config, ControllerManagerImpl::Create( |
521 config_string, NumChannels(), supported_frame_lengths_ms(), | 535 config_string, NumChannels(), supported_frame_lengths_ms(), |
522 num_channels_to_encode_, next_frame_length_ms_, | 536 num_channels_to_encode_, next_frame_length_ms_, |
523 GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); | 537 GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); |
524 } | 538 } |
525 | 539 |
540 void AudioEncoderOpus::MayUpdateUplinkBandwidth() { | |
ossu
2016/12/22 14:01:15
nit: May -> Maybe
There are several Maybe functio
michaelt
2016/12/22 14:56:10
Done.
| |
541 if (audio_network_adaptor_) { | |
542 int64_t now = config_.clock->TimeInMilliseconds(); | |
543 if (!last_smoothed_bandwith_update_ || | |
544 now - *last_smoothed_bandwith_update_ >= | |
545 config_.update_uplink_bandwidth_interval_ms) { | |
546 audio_network_adaptor_->SetUplinkBandwidth( | |
547 *bitrate_smoother_->GetAverage()); | |
minyue-webrtc
2016/12/22 14:51:26
shall Dcheck bitrate_smoother_.GetAverage() is not
michaelt
2016/12/22 15:10:10
Added a if since this could relay happens.
| |
548 last_smoothed_bandwith_update_ = rtc::Optional<int64_t>(now); | |
549 } | |
550 } | |
551 } | |
552 | |
526 } // namespace webrtc | 553 } // namespace webrtc |
OLD | NEW |