Index: webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc |
diff --git a/webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc b/webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc |
index af66cd3634ee1cdabcfab57f18c80c953a0a9391..507753da109e5881acdd8b2e94cf799729de08fe 100644 |
--- a/webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc |
+++ b/webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc |
@@ -172,7 +172,8 @@ rtc::Optional<int> AudioEncoderOpus::Config::GetNewComplexity() const { |
AudioEncoderOpus::AudioEncoderOpus( |
const Config& config, |
- AudioNetworkAdaptorCreator&& audio_network_adaptor_creator) |
+ AudioNetworkAdaptorCreator&& audio_network_adaptor_creator, |
+ std::unique_ptr<SmoothingFilter> bitrate_smoother) |
: packet_loss_rate_(0.0), |
inst_(nullptr), |
packet_loss_fraction_smoother_(new PacketLossFractionSmoother( |
@@ -183,7 +184,10 @@ AudioEncoderOpus::AudioEncoderOpus( |
: [this](const std::string& config_string, const Clock* clock) { |
return DefaultAudioNetworkAdaptorCreator(config_string, |
clock); |
- }) { |
+ }), |
+ bitrate_smoother_(bitrate_smoother |
+ ? std::move(bitrate_smoother) : std::unique_ptr<SmoothingFilter>( |
+ new SmoothingFilterImpl(500, config.clock))) { |
minyue-webrtc
2016/12/28 08:46:07
add a comment on the choice of 500
michaelt
2017/01/09 09:24:05
Done.
|
RTC_CHECK(RecreateEncoderInstance(config)); |
} |
@@ -272,13 +276,6 @@ void AudioEncoderOpus::DisableAudioNetworkAdaptor() { |
audio_network_adaptor_.reset(nullptr); |
} |
-void AudioEncoderOpus::OnReceivedUplinkBandwidth(int uplink_bandwidth_bps) { |
- if (!audio_network_adaptor_) |
- return; |
- audio_network_adaptor_->SetUplinkBandwidth(uplink_bandwidth_bps); |
- ApplyAudioNetworkAdaptor(); |
-} |
- |
void AudioEncoderOpus::OnReceivedUplinkPacketLossFraction( |
float uplink_packet_loss_fraction) { |
if (!audio_network_adaptor_) { |
@@ -292,9 +289,25 @@ void AudioEncoderOpus::OnReceivedUplinkPacketLossFraction( |
} |
void AudioEncoderOpus::OnReceivedTargetAudioBitrate( |
- int target_audio_bitrate_bps) { |
+ int target_audio_bitrate_bps, |
+ rtc::Optional<int64_t> probing_interval_ms) { |
if (audio_network_adaptor_) { |
audio_network_adaptor_->SetTargetAudioBitrate(target_audio_bitrate_bps); |
+ // We give smoothed bitrate allocation to audio network adaptor as |
+ // the uplink bandwidth. |
+ // The probing spikes should not affect the bitrate smoother more than 25%. |
+ // To simplify the calculations we use a step response as input signal. |
+ // The step response of an exponential filter is |
+ // u(t) = 1 - e^(-t / time_constant). |
+ // In order to limit the affect of a BWE spike within 25% of its value |
+ // before |
+ // the next probing, we would choose a time constant that fulfills |
+ // 1 - e^(-probing_interval_ms / time_constant) < 0.25 |
+ // Then 4 * probing_interval_ms is a good choice. |
+ if (probing_interval_ms) |
+ bitrate_smoother_->SetTimeConstantMs(*probing_interval_ms * 4); |
+ bitrate_smoother_->AddSample(target_audio_bitrate_bps); |
+ |
ApplyAudioNetworkAdaptor(); |
} else if (webrtc::field_trial::FindFullName( |
"WebRTC-SendSideBwe-WithOverhead") == "Enabled") { |
@@ -354,6 +367,7 @@ AudioEncoder::EncodedInfo AudioEncoderOpus::EncodeImpl( |
uint32_t rtp_timestamp, |
rtc::ArrayView<const int16_t> audio, |
rtc::Buffer* encoded) { |
+ MaybeUpdateUplinkBandwidth(); |
if (input_buffer_.empty()) |
first_timestamp_in_buffer_ = rtp_timestamp; |
@@ -523,4 +537,18 @@ AudioEncoderOpus::DefaultAudioNetworkAdaptorCreator( |
GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); |
} |
+void AudioEncoderOpus::MaybeUpdateUplinkBandwidth() { |
+ if (audio_network_adaptor_) { |
+ int64_t now = config_.clock->TimeInMilliseconds(); |
+ if (!bitrate_smoother_last_update_time_ || |
+ now - *bitrate_smoother_last_update_time_ >= |
+ config_.update_uplink_bandwidth_interval_ms) { |
+ rtc::Optional<float> smoothed_bitrate = bitrate_smoother_->GetAverage(); |
+ if (smoothed_bitrate) |
+ audio_network_adaptor_->SetUplinkBandwidth(*smoothed_bitrate); |
+ bitrate_smoother_last_update_time_ = rtc::Optional<int64_t>(now); |
+ } |
+ } |
+} |
+ |
} // namespace webrtc |