From 2fedf9c69aa13752312c346e2f99e9df88aa6467 Mon Sep 17 00:00:00 2001 From: michaelt Date: Mon, 28 Nov 2016 02:34:18 -0800 Subject: [PATCH] Smooth BWE and pass it to Audio Network Adaptor. BUG=webrtc:6443, webrtc:6303 Review-Url: https://codereview.webrtc.org/2503713003 Cr-Commit-Position: refs/heads/master@{#15257} --- .../mocks/mock_smoothing_filter.h | 1 + webrtc/common_audio/smoothing_filter.cc | 5 +++++ webrtc/common_audio/smoothing_filter.h | 4 +++- webrtc/voice_engine/channel.cc | 19 ++++++++++++++++++- webrtc/voice_engine/channel.h | 3 +++ 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/webrtc/common_audio/mocks/mock_smoothing_filter.h b/webrtc/common_audio/mocks/mock_smoothing_filter.h index 6b2991b22f..171a8ca574 100644 --- a/webrtc/common_audio/mocks/mock_smoothing_filter.h +++ b/webrtc/common_audio/mocks/mock_smoothing_filter.h @@ -20,6 +20,7 @@ class MockSmoothingFilter : public SmoothingFilter { public: MOCK_METHOD1(AddSample, void(float)); MOCK_CONST_METHOD0(GetAverage, rtc::Optional()); + MOCK_METHOD1(SetTimeConstantMs, void(int)); }; } // namespace webrtc diff --git a/webrtc/common_audio/smoothing_filter.cc b/webrtc/common_audio/smoothing_filter.cc index 1cf9580d2a..ff79ab8799 100644 --- a/webrtc/common_audio/smoothing_filter.cc +++ b/webrtc/common_audio/smoothing_filter.cc @@ -59,4 +59,9 @@ rtc::Optional SmoothingFilterImpl::GetAverage() const { : rtc::Optional(value); } +void SmoothingFilterImpl::SetTimeConstantMs(int time_constant_ms) { + time_constant_ms_ = time_constant_ms; + filter_.UpdateBase(exp(1.0f / time_constant_ms_)); +} + } // namespace webrtc diff --git a/webrtc/common_audio/smoothing_filter.h b/webrtc/common_audio/smoothing_filter.h index 64a83d1484..03eb718d54 100644 --- a/webrtc/common_audio/smoothing_filter.h +++ b/webrtc/common_audio/smoothing_filter.h @@ -23,6 +23,7 @@ class SmoothingFilter { virtual ~SmoothingFilter() = default; virtual void AddSample(float sample) = 0; virtual rtc::Optional GetAverage() const = 0; + virtual void SetTimeConstantMs(int time_constant_ms) = 0; }; // SmoothingFilterImpl applies an exponential filter @@ -35,9 +36,10 @@ class SmoothingFilterImpl final : public SmoothingFilter { void AddSample(float sample) override; rtc::Optional GetAverage() const override; + void SetTimeConstantMs(int time_constant_ms) override; private: - const int time_constant_ms_; + int time_constant_ms_; const Clock* const clock_; bool first_sample_received_; diff --git a/webrtc/voice_engine/channel.cc b/webrtc/voice_engine/channel.cc index b2fade8743..a7df1f2176 100644 --- a/webrtc/voice_engine/channel.cc +++ b/webrtc/voice_engine/channel.cc @@ -894,7 +894,10 @@ Channel::Channel(int32_t channelId, rtp_packet_sender_proxy_(new RtpPacketSenderProxy()), retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(), kMaxRetransmissionWindowMs)), - decoder_factory_(config.acm_config.decoder_factory) { + decoder_factory_(config.acm_config.decoder_factory), + // Bitrate smoother can be initialized with arbitrary time constant + // (0 used here). The actual time constant will be set in SetBitRate. + bitrate_smoother_(0, Clock::GetRealTimeClock()) { WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId), "Channel::Channel() - ctor"); AudioCodingModule::Config acm_config(config.acm_config); @@ -1306,6 +1309,20 @@ void Channel::SetBitRate(int bitrate_bps) { (*encoder)->OnReceivedTargetAudioBitrate(bitrate_bps); }); retransmission_rate_limiter_->SetMaxRate(bitrate_bps); + + // We give smoothed bitrate allocation to audio network adaptor as + // the uplink bandwidth. + // TODO(michaelt) : Remove kDefaultBitrateSmoothingTimeConstantMs as soon as + // we pass the probing interval to this function. + constexpr int64_t kDefaultBitrateSmoothingTimeConstantMs = 20000; + bitrate_smoother_.SetTimeConstantMs(kDefaultBitrateSmoothingTimeConstantMs); + bitrate_smoother_.AddSample(bitrate_bps); + audio_coding_->ModifyEncoder([&](std::unique_ptr* encoder) { + if (*encoder) { + (*encoder)->OnReceivedUplinkBandwidth( + static_cast(*bitrate_smoother_.GetAverage())); + } + }); } void Channel::OnIncomingFractionLoss(int fraction_lost) { diff --git a/webrtc/voice_engine/channel.h b/webrtc/voice_engine/channel.h index 31c44efccd..84f9691ef2 100644 --- a/webrtc/voice_engine/channel.h +++ b/webrtc/voice_engine/channel.h @@ -17,6 +17,7 @@ #include "webrtc/api/call/audio_sink.h" #include "webrtc/base/criticalsection.h" #include "webrtc/base/optional.h" +#include "webrtc/common_audio/smoothing_filter.h" #include "webrtc/common_audio/resampler/include/push_resampler.h" #include "webrtc/common_types.h" #include "webrtc/modules/audio_coding/acm2/codec_manager.h" @@ -543,6 +544,8 @@ class Channel // TODO(ossu): Remove once GetAudioDecoderFactory() is no longer needed. rtc::scoped_refptr decoder_factory_; + + SmoothingFilterImpl bitrate_smoother_; }; } // namespace voe