Chromium Code Reviews

Unified Diff: webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc

Issue 2390883004: Hooking up audio network adaptor to VoE. (Closed)
Patch Set: go back to old API for some tests. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
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 ae9dae2fbea4bc9e66d6a8b6826236258cc2ecab..60a98f6c6023070f764c9f53d66c8f15fae55967 100644
--- a/webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc
+++ b/webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc
@@ -13,6 +13,7 @@
#include <algorithm>
#include "webrtc/base/checks.h"
+#include "webrtc/base/exp_filter.h"
#include "webrtc/base/safe_conversions.h"
#include "webrtc/common_types.h"
#include "webrtc/modules/audio_coding/audio_network_adaptor/audio_network_adaptor_impl.h"
@@ -82,6 +83,35 @@ double OptimizePacketLossRate(double new_loss_rate, double old_loss_rate) {
} // namespace
+class AudioEncoderOpus::PacketLossFractionSmoother {
+ public:
+ explicit PacketLossFractionSmoother(const Clock* clock)
+ : clock_(clock),
+ last_sample_time_ms_(clock_->TimeInMilliseconds()),
+ smoother_(0.9999f) {}
the sun 2016/10/11 17:52:42 Would be nice with a comment about what the filter
minyue-webrtc 2016/10/12 11:27:35 I do not quite recall what that number means :P, t
+
+ // Gets the smoothed packet loss fraction.
+ float GetAverage() const {
+ float value = smoother_.filtered();
+ return (value == rtc::ExpFilter::kValueUndefined) ? 0.0f : value;
+ }
+
+ // Add new observation to the packet loss fraction smoother.
+ void AddSample(float packet_loss_fraction) {
+ int64_t now_ms = clock_->TimeInMilliseconds();
+ smoother_.Apply(static_cast<float>(now_ms - last_sample_time_ms_),
+ packet_loss_fraction);
+ last_sample_time_ms_ = now_ms;
+ }
+
+ private:
+ const Clock* const clock_;
+ int64_t last_sample_time_ms_;
+
+ // An exponential filter is used to smooth the packet loss fraction.
+ rtc::ExpFilter smoother_;
+};
+
AudioEncoderOpus::Config::Config() = default;
AudioEncoderOpus::Config::Config(const Config&) = default;
AudioEncoderOpus::Config::~Config() = default;
@@ -113,9 +143,11 @@ AudioEncoderOpus::AudioEncoderOpus(
AudioNetworkAdaptorCreator&& audio_network_adaptor_creator)
: packet_loss_rate_(0.0),
inst_(nullptr),
+ packet_loss_fraction_smoother_(new PacketLossFractionSmoother(
+ config.clock ? config.clock : Clock::GetRealTimeClock())),
audio_network_adaptor_creator_(
audio_network_adaptor_creator
- ? audio_network_adaptor_creator
+ ? std::move(audio_network_adaptor_creator)
: [this](const std::string& config_string, const Clock* clock) {
return DefaultAudioNetworkAdaptorCreator(config_string,
clock);
@@ -234,8 +266,11 @@ void AudioEncoderOpus::OnReceivedUplinkBandwidth(int uplink_bandwidth_bps) {
void AudioEncoderOpus::OnReceivedUplinkPacketLossFraction(
float uplink_packet_loss_fraction) {
- if (!audio_network_adaptor_)
- return;
+ if (!audio_network_adaptor_) {
+ packet_loss_fraction_smoother_->AddSample(uplink_packet_loss_fraction);
+ float average_fraction_loss = packet_loss_fraction_smoother_->GetAverage();
+ return SetProjectedPacketLossRate(average_fraction_loss);
+ }
audio_network_adaptor_->SetUplinkPacketLossFraction(
uplink_packet_loss_fraction);
ApplyAudioNetworkAdaptor();
@@ -244,7 +279,7 @@ void AudioEncoderOpus::OnReceivedUplinkPacketLossFraction(
void AudioEncoderOpus::OnReceivedTargetAudioBitrate(
int target_audio_bitrate_bps) {
if (!audio_network_adaptor_)
- return;
+ return SetTargetBitrate(target_audio_bitrate_bps);
audio_network_adaptor_->SetTargetAudioBitrate(target_audio_bitrate_bps);
ApplyAudioNetworkAdaptor();
}

Powered by Google App Engine