Implement Opus bandwidth adjustment behind a FieldTrial

Bug: webrtc:8522
Change-Id: I3a32ebfecd27ff74b507c2cee9e16aab17153442
Reviewed-on: https://webrtc-review.googlesource.com/22210
Commit-Queue: Alejandro Luebs <aluebs@webrtc.org>
Reviewed-by: Henrik Lundin <henrik.lundin@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#20799}
This commit is contained in:
Alex Luebs
2017-11-20 11:13:56 -08:00
committed by Commit Bot
parent 64eaa99cfc
commit eeb2765f6c
9 changed files with 368 additions and 3 deletions

View File

@ -330,6 +330,28 @@ rtc::Optional<int> AudioEncoderOpusImpl::GetNewComplexity(
}
}
rtc::Optional<int> AudioEncoderOpusImpl::GetNewBandwidth(
const AudioEncoderOpusConfig& config,
OpusEncInst* inst) {
constexpr int kMinWidebandBitrate = 8000;
constexpr int kMaxNarrowbandBitrate = 9000;
constexpr int kAutomaticThreshold = 11000;
RTC_DCHECK(config.IsOk());
const int bitrate = GetBitrateBps(config);
if (bitrate > kAutomaticThreshold) {
return rtc::Optional<int>(OPUS_AUTO);
}
const int bandwidth = WebRtcOpus_GetBandwidth(inst);
RTC_DCHECK_GE(bandwidth, 0);
if (bitrate > kMaxNarrowbandBitrate && bandwidth < OPUS_BANDWIDTH_WIDEBAND) {
return rtc::Optional<int>(OPUS_BANDWIDTH_WIDEBAND);
} else if (bitrate < kMinWidebandBitrate &&
bandwidth > OPUS_BANDWIDTH_NARROWBAND) {
return rtc::Optional<int>(OPUS_BANDWIDTH_NARROWBAND);
}
return rtc::Optional<int>();
}
class AudioEncoderOpusImpl::PacketLossFractionSmoother {
public:
explicit PacketLossFractionSmoother()
@ -376,6 +398,9 @@ AudioEncoderOpusImpl::AudioEncoderOpusImpl(
: payload_type_(payload_type),
send_side_bwe_with_overhead_(
webrtc::field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")),
adjust_bandwidth_(
webrtc::field_trial::IsEnabled("WebRTC-AdjustOpusBandwidth")),
bitrate_changed_(true),
packet_loss_rate_(0.0),
inst_(nullptr),
packet_loss_fraction_smoother_(new PacketLossFractionSmoother()),
@ -609,6 +634,14 @@ AudioEncoder::EncodedInfo AudioEncoderOpusImpl::EncodeImpl(
// Will use new packet size for next encoding.
config_.frame_size_ms = next_frame_length_ms_;
if (adjust_bandwidth_ && bitrate_changed_) {
const auto bandwidth = GetNewBandwidth(config_, inst_);
if (bandwidth) {
RTC_CHECK_EQ(0, WebRtcOpus_SetBandwidth(inst_, *bandwidth));
}
bitrate_changed_ = false;
}
info.encoded_timestamp = first_timestamp_in_buffer_;
info.payload_type = payload_type_;
info.send_even_if_empty = true; // Allows Opus to send empty packets.
@ -672,6 +705,7 @@ bool AudioEncoderOpusImpl::RecreateEncoderInstance(
// window.
complexity_ = GetNewComplexity(config).value_or(config.complexity);
RTC_CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, complexity_));
bitrate_changed_ = true;
if (config.dtx_enabled) {
RTC_CHECK_EQ(0, WebRtcOpus_EnableDtx(inst_));
} else {
@ -727,6 +761,7 @@ void AudioEncoderOpusImpl::SetTargetBitrate(int bits_per_second) {
complexity_ = *new_complexity;
RTC_CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, complexity_));
}
bitrate_changed_ = true;
}
void AudioEncoderOpusImpl::ApplyAudioNetworkAdaptor() {