kwiberg@webrtc.org | 663fdd0 | 2014-10-29 07:28:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/codecs/opus/audio_encoder_opus.h" |
kwiberg@webrtc.org | 663fdd0 | 2014-10-29 07:28:36 +0000 | [diff] [blame] | 12 | |
ossu | 264087f | 2016-04-18 08:07:24 -0700 | [diff] [blame] | 13 | #include <algorithm> |
minyue | a6f495c | 2016-10-24 09:19:14 -0700 | [diff] [blame] | 14 | #include <iterator> |
Mirko Bonadei | e45c688 | 2019-02-16 09:59:29 +0100 | [diff] [blame] | 15 | #include <string> |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 16 | #include <utility> |
ossu | 264087f | 2016-04-18 08:07:24 -0700 | [diff] [blame] | 17 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 18 | #include "absl/memory/memory.h" |
Niels Möller | 2edab4c | 2018-10-22 09:48:08 +0200 | [diff] [blame] | 19 | #include "absl/strings/match.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "modules/audio_coding/audio_network_adaptor/audio_network_adaptor_impl.h" |
| 21 | #include "modules/audio_coding/audio_network_adaptor/controller_manager.h" |
Alex Loiko | e5b9416 | 2019-04-08 17:19:41 +0200 | [diff] [blame] | 22 | #include "modules/audio_coding/codecs/opus/audio_coder_opus_common.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "modules/audio_coding/codecs/opus/opus_interface.h" |
| 24 | #include "rtc_base/arraysize.h" |
| 25 | #include "rtc_base/checks.h" |
| 26 | #include "rtc_base/logging.h" |
| 27 | #include "rtc_base/numerics/exp_filter.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 28 | #include "rtc_base/numerics/safe_conversions.h" |
| 29 | #include "rtc_base/numerics/safe_minmax.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #include "rtc_base/string_to_number.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 31 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 32 | #include "system_wrappers/include/field_trial.h" |
kwiberg@webrtc.org | 663fdd0 | 2014-10-29 07:28:36 +0000 | [diff] [blame] | 33 | |
| 34 | namespace webrtc { |
| 35 | |
| 36 | namespace { |
| 37 | |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 38 | // Codec parameters for Opus. |
| 39 | // draft-spittka-payload-rtp-opus-03 |
| 40 | |
| 41 | // Recommended bitrates: |
| 42 | // 8-12 kb/s for NB speech, |
| 43 | // 16-20 kb/s for WB speech, |
| 44 | // 28-40 kb/s for FB speech, |
| 45 | // 48-64 kb/s for FB mono music, and |
| 46 | // 64-128 kb/s for FB stereo music. |
| 47 | // The current implementation applies the following values to mono signals, |
| 48 | // and multiplies them by 2 for stereo. |
| 49 | constexpr int kOpusBitrateNbBps = 12000; |
| 50 | constexpr int kOpusBitrateWbBps = 20000; |
| 51 | constexpr int kOpusBitrateFbBps = 32000; |
michaelt | 54340d8 | 2017-01-31 09:06:53 -0800 | [diff] [blame] | 52 | |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 53 | constexpr int kSampleRateHz = 48000; |
Karl Wiberg | eb16697 | 2019-05-16 15:14:01 +0200 | [diff] [blame^] | 54 | constexpr int kRtpTimestampRateHz = 48000; |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 55 | constexpr int kDefaultMaxPlaybackRate = 48000; |
michaelt | a55f021 | 2017-02-02 07:47:19 -0800 | [diff] [blame] | 56 | |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 57 | // These two lists must be sorted from low to high |
michaelt | a55f021 | 2017-02-02 07:47:19 -0800 | [diff] [blame] | 58 | #if WEBRTC_OPUS_SUPPORT_120MS_PTIME |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 59 | constexpr int kANASupportedFrameLengths[] = {20, 60, 120}; |
| 60 | constexpr int kOpusSupportedFrameLengths[] = {10, 20, 40, 60, 120}; |
michaelt | a55f021 | 2017-02-02 07:47:19 -0800 | [diff] [blame] | 61 | #else |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 62 | constexpr int kANASupportedFrameLengths[] = {20, 60}; |
| 63 | constexpr int kOpusSupportedFrameLengths[] = {10, 20, 40, 60}; |
michaelt | a55f021 | 2017-02-02 07:47:19 -0800 | [diff] [blame] | 64 | #endif |
henrik.lundin@webrtc.org | 478cedc | 2015-01-27 18:24:45 +0000 | [diff] [blame] | 65 | |
minyue | 7e30432 | 2016-10-12 05:00:55 -0700 | [diff] [blame] | 66 | // PacketLossFractionSmoother uses an exponential filter with a time constant |
| 67 | // of -1.0 / ln(0.9999) = 10000 ms. |
| 68 | constexpr float kAlphaForPacketLossFractionSmoother = 0.9999f; |
| 69 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 70 | // Optimize the loss rate to configure Opus. Basically, optimized loss rate is |
| 71 | // the input loss rate rounded down to various levels, because a robustly good |
| 72 | // audio quality is achieved by lowering the packet loss down. |
| 73 | // Additionally, to prevent toggling, margins are used, i.e., when jumping to |
| 74 | // a loss rate from below, a higher threshold is used than jumping to the same |
| 75 | // level from above. |
minyue | 4b9a2cb | 2016-11-30 06:49:59 -0800 | [diff] [blame] | 76 | float OptimizePacketLossRate(float new_loss_rate, float old_loss_rate) { |
| 77 | RTC_DCHECK_GE(new_loss_rate, 0.0f); |
| 78 | RTC_DCHECK_LE(new_loss_rate, 1.0f); |
| 79 | RTC_DCHECK_GE(old_loss_rate, 0.0f); |
| 80 | RTC_DCHECK_LE(old_loss_rate, 1.0f); |
| 81 | constexpr float kPacketLossRate20 = 0.20f; |
| 82 | constexpr float kPacketLossRate10 = 0.10f; |
| 83 | constexpr float kPacketLossRate5 = 0.05f; |
| 84 | constexpr float kPacketLossRate1 = 0.01f; |
| 85 | constexpr float kLossRate20Margin = 0.02f; |
| 86 | constexpr float kLossRate10Margin = 0.01f; |
| 87 | constexpr float kLossRate5Margin = 0.01f; |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 88 | if (new_loss_rate >= |
| 89 | kPacketLossRate20 + |
| 90 | kLossRate20Margin * |
| 91 | (kPacketLossRate20 - old_loss_rate > 0 ? 1 : -1)) { |
| 92 | return kPacketLossRate20; |
| 93 | } else if (new_loss_rate >= |
| 94 | kPacketLossRate10 + |
| 95 | kLossRate10Margin * |
| 96 | (kPacketLossRate10 - old_loss_rate > 0 ? 1 : -1)) { |
| 97 | return kPacketLossRate10; |
| 98 | } else if (new_loss_rate >= |
| 99 | kPacketLossRate5 + |
| 100 | kLossRate5Margin * |
| 101 | (kPacketLossRate5 - old_loss_rate > 0 ? 1 : -1)) { |
| 102 | return kPacketLossRate5; |
| 103 | } else if (new_loss_rate >= kPacketLossRate1) { |
| 104 | return kPacketLossRate1; |
| 105 | } else { |
minyue | 4b9a2cb | 2016-11-30 06:49:59 -0800 | [diff] [blame] | 106 | return 0.0f; |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
kwiberg@webrtc.org | 663fdd0 | 2014-10-29 07:28:36 +0000 | [diff] [blame] | 109 | |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 110 | int CalculateDefaultBitrate(int max_playback_rate, size_t num_channels) { |
| 111 | const int bitrate = [&] { |
| 112 | if (max_playback_rate <= 8000) { |
| 113 | return kOpusBitrateNbBps * rtc::dchecked_cast<int>(num_channels); |
| 114 | } else if (max_playback_rate <= 16000) { |
| 115 | return kOpusBitrateWbBps * rtc::dchecked_cast<int>(num_channels); |
| 116 | } else { |
| 117 | return kOpusBitrateFbBps * rtc::dchecked_cast<int>(num_channels); |
| 118 | } |
| 119 | }(); |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 120 | RTC_DCHECK_GE(bitrate, AudioEncoderOpusConfig::kMinBitrateBps); |
| 121 | RTC_DCHECK_LE(bitrate, AudioEncoderOpusConfig::kMaxBitrateBps); |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 122 | return bitrate; |
| 123 | } |
| 124 | |
| 125 | // Get the maxaveragebitrate parameter in string-form, so we can properly figure |
| 126 | // out how invalid it is and accurately log invalid values. |
| 127 | int CalculateBitrate(int max_playback_rate_hz, |
| 128 | size_t num_channels, |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 129 | absl::optional<std::string> bitrate_param) { |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 130 | const int default_bitrate = |
| 131 | CalculateDefaultBitrate(max_playback_rate_hz, num_channels); |
| 132 | |
| 133 | if (bitrate_param) { |
| 134 | const auto bitrate = rtc::StringToNumber<int>(*bitrate_param); |
| 135 | if (bitrate) { |
| 136 | const int chosen_bitrate = |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 137 | std::max(AudioEncoderOpusConfig::kMinBitrateBps, |
| 138 | std::min(*bitrate, AudioEncoderOpusConfig::kMaxBitrateBps)); |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 139 | if (bitrate != chosen_bitrate) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 140 | RTC_LOG(LS_WARNING) << "Invalid maxaveragebitrate " << *bitrate |
| 141 | << " clamped to " << chosen_bitrate; |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 142 | } |
| 143 | return chosen_bitrate; |
| 144 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 145 | RTC_LOG(LS_WARNING) << "Invalid maxaveragebitrate \"" << *bitrate_param |
| 146 | << "\" replaced by default bitrate " << default_bitrate; |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | return default_bitrate; |
| 150 | } |
| 151 | |
| 152 | int GetChannelCount(const SdpAudioFormat& format) { |
| 153 | const auto param = GetFormatParameter(format, "stereo"); |
| 154 | if (param == "1") { |
| 155 | return 2; |
| 156 | } else { |
| 157 | return 1; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | int GetMaxPlaybackRate(const SdpAudioFormat& format) { |
| 162 | const auto param = GetFormatParameter<int>(format, "maxplaybackrate"); |
| 163 | if (param && *param >= 8000) { |
| 164 | return std::min(*param, kDefaultMaxPlaybackRate); |
| 165 | } |
| 166 | return kDefaultMaxPlaybackRate; |
| 167 | } |
| 168 | |
| 169 | int GetFrameSizeMs(const SdpAudioFormat& format) { |
| 170 | const auto ptime = GetFormatParameter<int>(format, "ptime"); |
| 171 | if (ptime) { |
| 172 | // Pick the next highest supported frame length from |
| 173 | // kOpusSupportedFrameLengths. |
| 174 | for (const int supported_frame_length : kOpusSupportedFrameLengths) { |
| 175 | if (supported_frame_length >= *ptime) { |
| 176 | return supported_frame_length; |
| 177 | } |
| 178 | } |
| 179 | // If none was found, return the largest supported frame length. |
| 180 | return *(std::end(kOpusSupportedFrameLengths) - 1); |
| 181 | } |
| 182 | |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 183 | return AudioEncoderOpusConfig::kDefaultFrameSizeMs; |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void FindSupportedFrameLengths(int min_frame_length_ms, |
| 187 | int max_frame_length_ms, |
| 188 | std::vector<int>* out) { |
| 189 | out->clear(); |
| 190 | std::copy_if(std::begin(kANASupportedFrameLengths), |
| 191 | std::end(kANASupportedFrameLengths), std::back_inserter(*out), |
| 192 | [&](int frame_length_ms) { |
| 193 | return frame_length_ms >= min_frame_length_ms && |
| 194 | frame_length_ms <= max_frame_length_ms; |
| 195 | }); |
| 196 | RTC_DCHECK(std::is_sorted(out->begin(), out->end())); |
| 197 | } |
| 198 | |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 199 | int GetBitrateBps(const AudioEncoderOpusConfig& config) { |
| 200 | RTC_DCHECK(config.IsOk()); |
| 201 | return *config.bitrate_bps; |
| 202 | } |
| 203 | |
Jakob Ivarsson | 83bd37c | 2018-10-15 09:54:46 +0200 | [diff] [blame] | 204 | bool IsValidPacketLossRate(int value) { |
| 205 | return value >= 0 && value <= 100; |
| 206 | } |
| 207 | |
| 208 | float ToFraction(int percent) { |
| 209 | return static_cast<float>(percent) / 100; |
| 210 | } |
| 211 | |
Jakob Ivarsson | 88b68ac | 2018-10-10 10:15:06 +0200 | [diff] [blame] | 212 | float GetMinPacketLossRate() { |
| 213 | constexpr char kPacketLossFieldTrial[] = "WebRTC-Audio-OpusMinPacketLossRate"; |
| 214 | const bool use_opus_min_packet_loss_rate = |
| 215 | webrtc::field_trial::IsEnabled(kPacketLossFieldTrial); |
| 216 | if (use_opus_min_packet_loss_rate) { |
| 217 | const std::string field_trial_string = |
| 218 | webrtc::field_trial::FindFullName(kPacketLossFieldTrial); |
| 219 | constexpr int kDefaultMinPacketLossRate = 1; |
| 220 | int value = kDefaultMinPacketLossRate; |
| 221 | if (sscanf(field_trial_string.c_str(), "Enabled-%d", &value) == 1 && |
Jakob Ivarsson | 83bd37c | 2018-10-15 09:54:46 +0200 | [diff] [blame] | 222 | !IsValidPacketLossRate(value)) { |
Jakob Ivarsson | 88b68ac | 2018-10-10 10:15:06 +0200 | [diff] [blame] | 223 | RTC_LOG(LS_WARNING) << "Invalid parameter for " << kPacketLossFieldTrial |
| 224 | << ", using default value: " |
| 225 | << kDefaultMinPacketLossRate; |
| 226 | value = kDefaultMinPacketLossRate; |
| 227 | } |
Jakob Ivarsson | 83bd37c | 2018-10-15 09:54:46 +0200 | [diff] [blame] | 228 | return ToFraction(value); |
Jakob Ivarsson | 88b68ac | 2018-10-10 10:15:06 +0200 | [diff] [blame] | 229 | } |
| 230 | return 0.0; |
| 231 | } |
| 232 | |
Jakob Ivarsson | 83bd37c | 2018-10-15 09:54:46 +0200 | [diff] [blame] | 233 | std::unique_ptr<AudioEncoderOpusImpl::NewPacketLossRateOptimizer> |
| 234 | GetNewPacketLossRateOptimizer() { |
| 235 | constexpr char kPacketLossOptimizationName[] = |
| 236 | "WebRTC-Audio-NewOpusPacketLossRateOptimization"; |
| 237 | const bool use_new_packet_loss_optimization = |
| 238 | webrtc::field_trial::IsEnabled(kPacketLossOptimizationName); |
| 239 | if (use_new_packet_loss_optimization) { |
| 240 | const std::string field_trial_string = |
| 241 | webrtc::field_trial::FindFullName(kPacketLossOptimizationName); |
| 242 | int min_rate; |
| 243 | int max_rate; |
| 244 | float slope; |
| 245 | if (sscanf(field_trial_string.c_str(), "Enabled-%d-%d-%f", &min_rate, |
| 246 | &max_rate, &slope) == 3 && |
| 247 | IsValidPacketLossRate(min_rate) && IsValidPacketLossRate(max_rate)) { |
| 248 | return absl::make_unique< |
| 249 | AudioEncoderOpusImpl::NewPacketLossRateOptimizer>( |
| 250 | ToFraction(min_rate), ToFraction(max_rate), slope); |
| 251 | } |
| 252 | RTC_LOG(LS_WARNING) << "Invalid parameters for " |
| 253 | << kPacketLossOptimizationName |
| 254 | << ", using default values."; |
| 255 | return absl::make_unique< |
| 256 | AudioEncoderOpusImpl::NewPacketLossRateOptimizer>(); |
| 257 | } |
| 258 | return nullptr; |
| 259 | } |
| 260 | |
kwiberg@webrtc.org | 663fdd0 | 2014-10-29 07:28:36 +0000 | [diff] [blame] | 261 | } // namespace |
| 262 | |
Jakob Ivarsson | 83bd37c | 2018-10-15 09:54:46 +0200 | [diff] [blame] | 263 | AudioEncoderOpusImpl::NewPacketLossRateOptimizer::NewPacketLossRateOptimizer( |
| 264 | float min_packet_loss_rate, |
| 265 | float max_packet_loss_rate, |
| 266 | float slope) |
| 267 | : min_packet_loss_rate_(min_packet_loss_rate), |
| 268 | max_packet_loss_rate_(max_packet_loss_rate), |
| 269 | slope_(slope) {} |
| 270 | |
| 271 | float AudioEncoderOpusImpl::NewPacketLossRateOptimizer::OptimizePacketLossRate( |
| 272 | float packet_loss_rate) const { |
| 273 | packet_loss_rate = slope_ * packet_loss_rate; |
| 274 | return std::min(std::max(packet_loss_rate, min_packet_loss_rate_), |
| 275 | max_packet_loss_rate_); |
| 276 | } |
| 277 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 278 | void AudioEncoderOpusImpl::AppendSupportedEncoders( |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 279 | std::vector<AudioCodecSpec>* specs) { |
Karl Wiberg | eb16697 | 2019-05-16 15:14:01 +0200 | [diff] [blame^] | 280 | const SdpAudioFormat fmt = {"opus", |
| 281 | kRtpTimestampRateHz, |
| 282 | 2, |
| 283 | {{"minptime", "10"}, {"useinbandfec", "1"}}}; |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 284 | const AudioCodecInfo info = QueryAudioEncoder(*SdpToConfig(fmt)); |
| 285 | specs->push_back({fmt, info}); |
| 286 | } |
| 287 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 288 | AudioCodecInfo AudioEncoderOpusImpl::QueryAudioEncoder( |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 289 | const AudioEncoderOpusConfig& config) { |
| 290 | RTC_DCHECK(config.IsOk()); |
Karl Wiberg | eb16697 | 2019-05-16 15:14:01 +0200 | [diff] [blame^] | 291 | AudioCodecInfo info(kRtpTimestampRateHz, config.num_channels, |
| 292 | *config.bitrate_bps, |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 293 | AudioEncoderOpusConfig::kMinBitrateBps, |
| 294 | AudioEncoderOpusConfig::kMaxBitrateBps); |
| 295 | info.allow_comfort_noise = false; |
| 296 | info.supports_network_adaption = true; |
| 297 | return info; |
| 298 | } |
| 299 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 300 | std::unique_ptr<AudioEncoder> AudioEncoderOpusImpl::MakeAudioEncoder( |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 301 | const AudioEncoderOpusConfig& config, |
| 302 | int payload_type) { |
| 303 | RTC_DCHECK(config.IsOk()); |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 304 | return absl::make_unique<AudioEncoderOpusImpl>(config, payload_type); |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 307 | absl::optional<AudioCodecInfo> AudioEncoderOpusImpl::QueryAudioEncoder( |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 308 | const SdpAudioFormat& format) { |
Niels Möller | 2edab4c | 2018-10-22 09:48:08 +0200 | [diff] [blame] | 309 | if (absl::EqualsIgnoreCase(format.name, GetPayloadName()) && |
Karl Wiberg | eb16697 | 2019-05-16 15:14:01 +0200 | [diff] [blame^] | 310 | format.clockrate_hz == kRtpTimestampRateHz && format.num_channels == 2) { |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 311 | const size_t num_channels = GetChannelCount(format); |
| 312 | const int bitrate = |
| 313 | CalculateBitrate(GetMaxPlaybackRate(format), num_channels, |
| 314 | GetFormatParameter(format, "maxaveragebitrate")); |
Karl Wiberg | eb16697 | 2019-05-16 15:14:01 +0200 | [diff] [blame^] | 315 | AudioCodecInfo info(kRtpTimestampRateHz, num_channels, bitrate, |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 316 | AudioEncoderOpusConfig::kMinBitrateBps, |
| 317 | AudioEncoderOpusConfig::kMaxBitrateBps); |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 318 | info.allow_comfort_noise = false; |
| 319 | info.supports_network_adaption = true; |
| 320 | |
Oskar Sundbom | 12ab00b | 2017-11-16 15:31:38 +0100 | [diff] [blame] | 321 | return info; |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 322 | } |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 323 | return absl::nullopt; |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 326 | absl::optional<AudioEncoderOpusConfig> AudioEncoderOpusImpl::SdpToConfig( |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 327 | const SdpAudioFormat& format) { |
Niels Möller | 2edab4c | 2018-10-22 09:48:08 +0200 | [diff] [blame] | 328 | if (!absl::EqualsIgnoreCase(format.name, "opus") || |
Karl Wiberg | eb16697 | 2019-05-16 15:14:01 +0200 | [diff] [blame^] | 329 | format.clockrate_hz != kRtpTimestampRateHz || format.num_channels != 2) { |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 330 | return absl::nullopt; |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 331 | } |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 332 | |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 333 | AudioEncoderOpusConfig config; |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 334 | config.num_channels = GetChannelCount(format); |
| 335 | config.frame_size_ms = GetFrameSizeMs(format); |
| 336 | config.max_playback_rate_hz = GetMaxPlaybackRate(format); |
| 337 | config.fec_enabled = (GetFormatParameter(format, "useinbandfec") == "1"); |
| 338 | config.dtx_enabled = (GetFormatParameter(format, "usedtx") == "1"); |
ossu | 0d6195d | 2017-04-28 07:33:18 -0700 | [diff] [blame] | 339 | config.cbr_enabled = (GetFormatParameter(format, "cbr") == "1"); |
Oskar Sundbom | 12ab00b | 2017-11-16 15:31:38 +0100 | [diff] [blame] | 340 | config.bitrate_bps = |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 341 | CalculateBitrate(config.max_playback_rate_hz, config.num_channels, |
Oskar Sundbom | 12ab00b | 2017-11-16 15:31:38 +0100 | [diff] [blame] | 342 | GetFormatParameter(format, "maxaveragebitrate")); |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 343 | config.application = config.num_channels == 1 |
| 344 | ? AudioEncoderOpusConfig::ApplicationMode::kVoip |
| 345 | : AudioEncoderOpusConfig::ApplicationMode::kAudio; |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 346 | |
| 347 | constexpr int kMinANAFrameLength = kANASupportedFrameLengths[0]; |
| 348 | constexpr int kMaxANAFrameLength = |
| 349 | kANASupportedFrameLengths[arraysize(kANASupportedFrameLengths) - 1]; |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 350 | |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 351 | // For now, minptime and maxptime are only used with ANA. If ptime is outside |
| 352 | // of this range, it will get adjusted once ANA takes hold. Ideally, we'd know |
| 353 | // if ANA was to be used when setting up the config, and adjust accordingly. |
| 354 | const int min_frame_length_ms = |
| 355 | GetFormatParameter<int>(format, "minptime").value_or(kMinANAFrameLength); |
| 356 | const int max_frame_length_ms = |
| 357 | GetFormatParameter<int>(format, "maxptime").value_or(kMaxANAFrameLength); |
| 358 | |
| 359 | FindSupportedFrameLengths(min_frame_length_ms, max_frame_length_ms, |
| 360 | &config.supported_frame_lengths_ms); |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 361 | RTC_DCHECK(config.IsOk()); |
Oskar Sundbom | 12ab00b | 2017-11-16 15:31:38 +0100 | [diff] [blame] | 362 | return config; |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 365 | absl::optional<int> AudioEncoderOpusImpl::GetNewComplexity( |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 366 | const AudioEncoderOpusConfig& config) { |
| 367 | RTC_DCHECK(config.IsOk()); |
| 368 | const int bitrate_bps = GetBitrateBps(config); |
| 369 | if (bitrate_bps >= config.complexity_threshold_bps - |
| 370 | config.complexity_threshold_window_bps && |
| 371 | bitrate_bps <= config.complexity_threshold_bps + |
| 372 | config.complexity_threshold_window_bps) { |
| 373 | // Within the hysteresis window; make no change. |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 374 | return absl::nullopt; |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 375 | } else { |
Oskar Sundbom | 12ab00b | 2017-11-16 15:31:38 +0100 | [diff] [blame] | 376 | return bitrate_bps <= config.complexity_threshold_bps |
| 377 | ? config.low_rate_complexity |
| 378 | : config.complexity; |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 379 | } |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 382 | absl::optional<int> AudioEncoderOpusImpl::GetNewBandwidth( |
Alex Luebs | eeb2765 | 2017-11-20 11:13:56 -0800 | [diff] [blame] | 383 | const AudioEncoderOpusConfig& config, |
| 384 | OpusEncInst* inst) { |
| 385 | constexpr int kMinWidebandBitrate = 8000; |
| 386 | constexpr int kMaxNarrowbandBitrate = 9000; |
| 387 | constexpr int kAutomaticThreshold = 11000; |
| 388 | RTC_DCHECK(config.IsOk()); |
| 389 | const int bitrate = GetBitrateBps(config); |
| 390 | if (bitrate > kAutomaticThreshold) { |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 391 | return absl::optional<int>(OPUS_AUTO); |
Alex Luebs | eeb2765 | 2017-11-20 11:13:56 -0800 | [diff] [blame] | 392 | } |
| 393 | const int bandwidth = WebRtcOpus_GetBandwidth(inst); |
| 394 | RTC_DCHECK_GE(bandwidth, 0); |
| 395 | if (bitrate > kMaxNarrowbandBitrate && bandwidth < OPUS_BANDWIDTH_WIDEBAND) { |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 396 | return absl::optional<int>(OPUS_BANDWIDTH_WIDEBAND); |
Alex Luebs | eeb2765 | 2017-11-20 11:13:56 -0800 | [diff] [blame] | 397 | } else if (bitrate < kMinWidebandBitrate && |
| 398 | bandwidth > OPUS_BANDWIDTH_NARROWBAND) { |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 399 | return absl::optional<int>(OPUS_BANDWIDTH_NARROWBAND); |
Alex Luebs | eeb2765 | 2017-11-20 11:13:56 -0800 | [diff] [blame] | 400 | } |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 401 | return absl::optional<int>(); |
Alex Luebs | eeb2765 | 2017-11-20 11:13:56 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 404 | class AudioEncoderOpusImpl::PacketLossFractionSmoother { |
minyue | 7e30432 | 2016-10-12 05:00:55 -0700 | [diff] [blame] | 405 | public: |
michaelt | 92aef17 | 2017-04-18 00:11:48 -0700 | [diff] [blame] | 406 | explicit PacketLossFractionSmoother() |
| 407 | : last_sample_time_ms_(rtc::TimeMillis()), |
minyue | 7e30432 | 2016-10-12 05:00:55 -0700 | [diff] [blame] | 408 | smoother_(kAlphaForPacketLossFractionSmoother) {} |
| 409 | |
| 410 | // Gets the smoothed packet loss fraction. |
| 411 | float GetAverage() const { |
| 412 | float value = smoother_.filtered(); |
| 413 | return (value == rtc::ExpFilter::kValueUndefined) ? 0.0f : value; |
| 414 | } |
| 415 | |
| 416 | // Add new observation to the packet loss fraction smoother. |
| 417 | void AddSample(float packet_loss_fraction) { |
michaelt | 92aef17 | 2017-04-18 00:11:48 -0700 | [diff] [blame] | 418 | int64_t now_ms = rtc::TimeMillis(); |
minyue | 7e30432 | 2016-10-12 05:00:55 -0700 | [diff] [blame] | 419 | smoother_.Apply(static_cast<float>(now_ms - last_sample_time_ms_), |
| 420 | packet_loss_fraction); |
| 421 | last_sample_time_ms_ = now_ms; |
| 422 | } |
| 423 | |
| 424 | private: |
minyue | 7e30432 | 2016-10-12 05:00:55 -0700 | [diff] [blame] | 425 | int64_t last_sample_time_ms_; |
| 426 | |
| 427 | // An exponential filter is used to smooth the packet loss fraction. |
| 428 | rtc::ExpFilter smoother_; |
| 429 | }; |
| 430 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 431 | AudioEncoderOpusImpl::AudioEncoderOpusImpl(const AudioEncoderOpusConfig& config, |
| 432 | int payload_type) |
| 433 | : AudioEncoderOpusImpl( |
minyue-webrtc | 5d68910 | 2017-08-14 14:33:32 +0200 | [diff] [blame] | 434 | config, |
| 435 | payload_type, |
Mirko Bonadei | e45c688 | 2019-02-16 09:59:29 +0100 | [diff] [blame] | 436 | [this](const std::string& config_string, RtcEventLog* event_log) { |
minyue-webrtc | 5d68910 | 2017-08-14 14:33:32 +0200 | [diff] [blame] | 437 | return DefaultAudioNetworkAdaptorCreator(config_string, event_log); |
| 438 | }, |
| 439 | // We choose 5sec as initial time constant due to empirical data. |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 440 | absl::make_unique<SmoothingFilterImpl>(5000)) {} |
minyue-webrtc | 5d68910 | 2017-08-14 14:33:32 +0200 | [diff] [blame] | 441 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 442 | AudioEncoderOpusImpl::AudioEncoderOpusImpl( |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 443 | const AudioEncoderOpusConfig& config, |
| 444 | int payload_type, |
minyue-webrtc | 5d68910 | 2017-08-14 14:33:32 +0200 | [diff] [blame] | 445 | const AudioNetworkAdaptorCreator& audio_network_adaptor_creator, |
michaelt | 566d820 | 2017-01-12 10:17:38 -0800 | [diff] [blame] | 446 | std::unique_ptr<SmoothingFilter> bitrate_smoother) |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 447 | : payload_type_(payload_type), |
minyue-webrtc | 5d68910 | 2017-08-14 14:33:32 +0200 | [diff] [blame] | 448 | send_side_bwe_with_overhead_( |
| 449 | webrtc::field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")), |
Sebastian Jansson | 8ac05cc | 2018-11-21 19:26:12 +0100 | [diff] [blame] | 450 | use_link_capacity_for_adaptation_(webrtc::field_trial::IsEnabled( |
| 451 | "WebRTC-Audio-LinkCapacityAdaptation")), |
Alex Luebs | eeb2765 | 2017-11-20 11:13:56 -0800 | [diff] [blame] | 452 | adjust_bandwidth_( |
| 453 | webrtc::field_trial::IsEnabled("WebRTC-AdjustOpusBandwidth")), |
| 454 | bitrate_changed_(true), |
elad.alon | 0fe1216 | 2017-01-31 05:48:37 -0800 | [diff] [blame] | 455 | packet_loss_rate_(0.0), |
Jakob Ivarsson | 88b68ac | 2018-10-10 10:15:06 +0200 | [diff] [blame] | 456 | min_packet_loss_rate_(GetMinPacketLossRate()), |
Jakob Ivarsson | 83bd37c | 2018-10-15 09:54:46 +0200 | [diff] [blame] | 457 | new_packet_loss_optimizer_(GetNewPacketLossRateOptimizer()), |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 458 | inst_(nullptr), |
michaelt | 92aef17 | 2017-04-18 00:11:48 -0700 | [diff] [blame] | 459 | packet_loss_fraction_smoother_(new PacketLossFractionSmoother()), |
minyue-webrtc | 5d68910 | 2017-08-14 14:33:32 +0200 | [diff] [blame] | 460 | audio_network_adaptor_creator_(audio_network_adaptor_creator), |
Gustaf Ullberg | 36de62e | 2017-11-20 14:55:41 +0100 | [diff] [blame] | 461 | bitrate_smoother_(std::move(bitrate_smoother)), |
| 462 | consecutive_dtx_frames_(0) { |
minyue-webrtc | 5d68910 | 2017-08-14 14:33:32 +0200 | [diff] [blame] | 463 | RTC_DCHECK(0 <= payload_type && payload_type <= 127); |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 464 | |
| 465 | // Sanity check of the redundant payload type field that we want to get rid |
| 466 | // of. See https://bugs.chromium.org/p/webrtc/issues/detail?id=7847 |
| 467 | RTC_CHECK(config.payload_type == -1 || config.payload_type == payload_type); |
| 468 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 469 | RTC_CHECK(RecreateEncoderInstance(config)); |
Jakob Ivarsson | 88b68ac | 2018-10-10 10:15:06 +0200 | [diff] [blame] | 470 | SetProjectedPacketLossRate(packet_loss_rate_); |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 473 | AudioEncoderOpusImpl::AudioEncoderOpusImpl(int payload_type, |
| 474 | const SdpAudioFormat& format) |
| 475 | : AudioEncoderOpusImpl(*SdpToConfig(format), payload_type) {} |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 476 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 477 | AudioEncoderOpusImpl::~AudioEncoderOpusImpl() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 478 | RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 481 | int AudioEncoderOpusImpl::SampleRateHz() const { |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 482 | return kSampleRateHz; |
| 483 | } |
| 484 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 485 | size_t AudioEncoderOpusImpl::NumChannels() const { |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 486 | return config_.num_channels; |
| 487 | } |
| 488 | |
Karl Wiberg | eb16697 | 2019-05-16 15:14:01 +0200 | [diff] [blame^] | 489 | int AudioEncoderOpusImpl::RtpTimestampRateHz() const { |
| 490 | return kRtpTimestampRateHz; |
| 491 | } |
| 492 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 493 | size_t AudioEncoderOpusImpl::Num10MsFramesInNextPacket() const { |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 494 | return Num10msFramesPerPacket(); |
| 495 | } |
| 496 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 497 | size_t AudioEncoderOpusImpl::Max10MsFramesInAPacket() const { |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 498 | return Num10msFramesPerPacket(); |
| 499 | } |
| 500 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 501 | int AudioEncoderOpusImpl::GetTargetBitrate() const { |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 502 | return GetBitrateBps(config_); |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 505 | void AudioEncoderOpusImpl::Reset() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 506 | RTC_CHECK(RecreateEncoderInstance(config_)); |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 509 | bool AudioEncoderOpusImpl::SetFec(bool enable) { |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 510 | if (enable) { |
| 511 | RTC_CHECK_EQ(0, WebRtcOpus_EnableFec(inst_)); |
| 512 | } else { |
| 513 | RTC_CHECK_EQ(0, WebRtcOpus_DisableFec(inst_)); |
| 514 | } |
| 515 | config_.fec_enabled = enable; |
| 516 | return true; |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 519 | bool AudioEncoderOpusImpl::SetDtx(bool enable) { |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 520 | if (enable) { |
| 521 | RTC_CHECK_EQ(0, WebRtcOpus_EnableDtx(inst_)); |
| 522 | } else { |
| 523 | RTC_CHECK_EQ(0, WebRtcOpus_DisableDtx(inst_)); |
| 524 | } |
| 525 | config_.dtx_enabled = enable; |
| 526 | return true; |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 529 | bool AudioEncoderOpusImpl::GetDtx() const { |
ivoc | 85228d6 | 2016-07-27 04:53:47 -0700 | [diff] [blame] | 530 | return config_.dtx_enabled; |
| 531 | } |
| 532 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 533 | bool AudioEncoderOpusImpl::SetApplication(Application application) { |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 534 | auto conf = config_; |
| 535 | switch (application) { |
| 536 | case Application::kSpeech: |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 537 | conf.application = AudioEncoderOpusConfig::ApplicationMode::kVoip; |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 538 | break; |
| 539 | case Application::kAudio: |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 540 | conf.application = AudioEncoderOpusConfig::ApplicationMode::kAudio; |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 541 | break; |
| 542 | } |
| 543 | return RecreateEncoderInstance(conf); |
| 544 | } |
| 545 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 546 | void AudioEncoderOpusImpl::SetMaxPlaybackRate(int frequency_hz) { |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 547 | auto conf = config_; |
| 548 | conf.max_playback_rate_hz = frequency_hz; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 549 | RTC_CHECK(RecreateEncoderInstance(conf)); |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 552 | bool AudioEncoderOpusImpl::EnableAudioNetworkAdaptor( |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 553 | const std::string& config_string, |
michaelt | 92aef17 | 2017-04-18 00:11:48 -0700 | [diff] [blame] | 554 | RtcEventLog* event_log) { |
michaelt | bf279fc | 2017-01-13 06:02:29 -0800 | [diff] [blame] | 555 | audio_network_adaptor_ = |
michaelt | 92aef17 | 2017-04-18 00:11:48 -0700 | [diff] [blame] | 556 | audio_network_adaptor_creator_(config_string, event_log); |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 557 | return audio_network_adaptor_.get() != nullptr; |
| 558 | } |
| 559 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 560 | void AudioEncoderOpusImpl::DisableAudioNetworkAdaptor() { |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 561 | audio_network_adaptor_.reset(nullptr); |
| 562 | } |
| 563 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 564 | void AudioEncoderOpusImpl::OnReceivedUplinkPacketLossFraction( |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 565 | float uplink_packet_loss_fraction) { |
minyue | 7e30432 | 2016-10-12 05:00:55 -0700 | [diff] [blame] | 566 | if (!audio_network_adaptor_) { |
| 567 | packet_loss_fraction_smoother_->AddSample(uplink_packet_loss_fraction); |
| 568 | float average_fraction_loss = packet_loss_fraction_smoother_->GetAverage(); |
| 569 | return SetProjectedPacketLossRate(average_fraction_loss); |
| 570 | } |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 571 | audio_network_adaptor_->SetUplinkPacketLossFraction( |
| 572 | uplink_packet_loss_fraction); |
| 573 | ApplyAudioNetworkAdaptor(); |
| 574 | } |
| 575 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 576 | void AudioEncoderOpusImpl::OnReceivedUplinkRecoverablePacketLossFraction( |
elad.alon | dadb4dc | 2017-03-23 15:29:50 -0700 | [diff] [blame] | 577 | float uplink_recoverable_packet_loss_fraction) { |
| 578 | if (!audio_network_adaptor_) |
| 579 | return; |
| 580 | audio_network_adaptor_->SetUplinkRecoverablePacketLossFraction( |
| 581 | uplink_recoverable_packet_loss_fraction); |
| 582 | ApplyAudioNetworkAdaptor(); |
| 583 | } |
| 584 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 585 | void AudioEncoderOpusImpl::OnReceivedUplinkBandwidth( |
michaelt | 566d820 | 2017-01-12 10:17:38 -0800 | [diff] [blame] | 586 | int target_audio_bitrate_bps, |
Sebastian Jansson | 8ac05cc | 2018-11-21 19:26:12 +0100 | [diff] [blame] | 587 | absl::optional<int64_t> bwe_period_ms, |
| 588 | absl::optional<int64_t> link_capacity_allocation_bps) { |
minyue | eca373f | 2016-12-07 01:40:34 -0800 | [diff] [blame] | 589 | if (audio_network_adaptor_) { |
| 590 | audio_network_adaptor_->SetTargetAudioBitrate(target_audio_bitrate_bps); |
michaelt | 566d820 | 2017-01-12 10:17:38 -0800 | [diff] [blame] | 591 | // We give smoothed bitrate allocation to audio network adaptor as |
| 592 | // the uplink bandwidth. |
minyue-webrtc | bf94fda | 2017-09-01 10:44:37 +0200 | [diff] [blame] | 593 | // The BWE spikes should not affect the bitrate smoother more than 25%. |
michaelt | 566d820 | 2017-01-12 10:17:38 -0800 | [diff] [blame] | 594 | // To simplify the calculations we use a step response as input signal. |
| 595 | // The step response of an exponential filter is |
| 596 | // u(t) = 1 - e^(-t / time_constant). |
| 597 | // In order to limit the affect of a BWE spike within 25% of its value |
| 598 | // before |
minyue-webrtc | bf94fda | 2017-09-01 10:44:37 +0200 | [diff] [blame] | 599 | // the next BWE update, we would choose a time constant that fulfills |
| 600 | // 1 - e^(-bwe_period_ms / time_constant) < 0.25 |
| 601 | // Then 4 * bwe_period_ms is a good choice. |
| 602 | if (bwe_period_ms) |
| 603 | bitrate_smoother_->SetTimeConstantMs(*bwe_period_ms * 4); |
michaelt | 566d820 | 2017-01-12 10:17:38 -0800 | [diff] [blame] | 604 | bitrate_smoother_->AddSample(target_audio_bitrate_bps); |
| 605 | |
Sebastian Jansson | 8ac05cc | 2018-11-21 19:26:12 +0100 | [diff] [blame] | 606 | if (link_capacity_allocation_bps) |
| 607 | link_capacity_allocation_bps_ = link_capacity_allocation_bps; |
| 608 | |
minyue | eca373f | 2016-12-07 01:40:34 -0800 | [diff] [blame] | 609 | ApplyAudioNetworkAdaptor(); |
elad.alon | 0fe1216 | 2017-01-31 05:48:37 -0800 | [diff] [blame] | 610 | } else if (send_side_bwe_with_overhead_) { |
minyue | eca373f | 2016-12-07 01:40:34 -0800 | [diff] [blame] | 611 | if (!overhead_bytes_per_packet_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 612 | RTC_LOG(LS_INFO) |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 613 | << "AudioEncoderOpusImpl: Overhead unknown, target audio bitrate " |
minyue | eca373f | 2016-12-07 01:40:34 -0800 | [diff] [blame] | 614 | << target_audio_bitrate_bps << " bps is ignored."; |
| 615 | return; |
| 616 | } |
| 617 | const int overhead_bps = static_cast<int>( |
| 618 | *overhead_bytes_per_packet_ * 8 * 100 / Num10MsFramesInNextPacket()); |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 619 | SetTargetBitrate( |
| 620 | std::min(AudioEncoderOpusConfig::kMaxBitrateBps, |
| 621 | std::max(AudioEncoderOpusConfig::kMinBitrateBps, |
| 622 | target_audio_bitrate_bps - overhead_bps))); |
minyue | eca373f | 2016-12-07 01:40:34 -0800 | [diff] [blame] | 623 | } else { |
| 624 | SetTargetBitrate(target_audio_bitrate_bps); |
| 625 | } |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 626 | } |
Sebastian Jansson | 8ac05cc | 2018-11-21 19:26:12 +0100 | [diff] [blame] | 627 | void AudioEncoderOpusImpl::OnReceivedUplinkBandwidth( |
| 628 | int target_audio_bitrate_bps, |
| 629 | absl::optional<int64_t> bwe_period_ms) { |
| 630 | OnReceivedUplinkBandwidth(target_audio_bitrate_bps, bwe_period_ms, |
| 631 | absl::nullopt); |
| 632 | } |
| 633 | |
| 634 | void AudioEncoderOpusImpl::OnReceivedUplinkAllocation( |
| 635 | BitrateAllocationUpdate update) { |
| 636 | OnReceivedUplinkBandwidth(update.target_bitrate.bps(), update.bwe_period.ms(), |
| 637 | update.link_capacity.bps()); |
| 638 | } |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 639 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 640 | void AudioEncoderOpusImpl::OnReceivedRtt(int rtt_ms) { |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 641 | if (!audio_network_adaptor_) |
| 642 | return; |
| 643 | audio_network_adaptor_->SetRtt(rtt_ms); |
| 644 | ApplyAudioNetworkAdaptor(); |
| 645 | } |
| 646 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 647 | void AudioEncoderOpusImpl::OnReceivedOverhead( |
| 648 | size_t overhead_bytes_per_packet) { |
minyue | eca373f | 2016-12-07 01:40:34 -0800 | [diff] [blame] | 649 | if (audio_network_adaptor_) { |
| 650 | audio_network_adaptor_->SetOverhead(overhead_bytes_per_packet); |
| 651 | ApplyAudioNetworkAdaptor(); |
| 652 | } else { |
Oskar Sundbom | 12ab00b | 2017-11-16 15:31:38 +0100 | [diff] [blame] | 653 | overhead_bytes_per_packet_ = overhead_bytes_per_packet; |
minyue | eca373f | 2016-12-07 01:40:34 -0800 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 657 | void AudioEncoderOpusImpl::SetReceiverFrameLengthRange( |
| 658 | int min_frame_length_ms, |
| 659 | int max_frame_length_ms) { |
minyue | a6f495c | 2016-10-24 09:19:14 -0700 | [diff] [blame] | 660 | // Ensure that |SetReceiverFrameLengthRange| is called before |
| 661 | // |EnableAudioNetworkAdaptor|, otherwise we need to recreate |
| 662 | // |audio_network_adaptor_|, which is not a needed use case. |
| 663 | RTC_DCHECK(!audio_network_adaptor_); |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 664 | FindSupportedFrameLengths(min_frame_length_ms, max_frame_length_ms, |
| 665 | &config_.supported_frame_lengths_ms); |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 668 | AudioEncoder::EncodedInfo AudioEncoderOpusImpl::EncodeImpl( |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 669 | uint32_t rtp_timestamp, |
| 670 | rtc::ArrayView<const int16_t> audio, |
| 671 | rtc::Buffer* encoded) { |
michaelt | 566d820 | 2017-01-12 10:17:38 -0800 | [diff] [blame] | 672 | MaybeUpdateUplinkBandwidth(); |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 673 | |
| 674 | if (input_buffer_.empty()) |
| 675 | first_timestamp_in_buffer_ = rtp_timestamp; |
| 676 | |
| 677 | input_buffer_.insert(input_buffer_.end(), audio.cbegin(), audio.cend()); |
| 678 | if (input_buffer_.size() < |
| 679 | (Num10msFramesPerPacket() * SamplesPer10msFrame())) { |
| 680 | return EncodedInfo(); |
| 681 | } |
| 682 | RTC_CHECK_EQ(input_buffer_.size(), |
| 683 | Num10msFramesPerPacket() * SamplesPer10msFrame()); |
| 684 | |
ossu | 2903ba5 | 2016-04-18 06:14:33 -0700 | [diff] [blame] | 685 | const size_t max_encoded_bytes = SufficientOutputBufferSize(); |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 686 | EncodedInfo info; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 687 | info.encoded_bytes = encoded->AppendData( |
| 688 | max_encoded_bytes, [&](rtc::ArrayView<uint8_t> encoded) { |
| 689 | int status = WebRtcOpus_Encode( |
| 690 | inst_, &input_buffer_[0], |
| 691 | rtc::CheckedDivExact(input_buffer_.size(), config_.num_channels), |
| 692 | rtc::saturated_cast<int16_t>(max_encoded_bytes), encoded.data()); |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 693 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 694 | RTC_CHECK_GE(status, 0); // Fails only if fed invalid data. |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 695 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 696 | return static_cast<size_t>(status); |
| 697 | }); |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 698 | input_buffer_.clear(); |
| 699 | |
Gustaf Ullberg | 36de62e | 2017-11-20 14:55:41 +0100 | [diff] [blame] | 700 | bool dtx_frame = (info.encoded_bytes <= 2); |
| 701 | |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 702 | // Will use new packet size for next encoding. |
| 703 | config_.frame_size_ms = next_frame_length_ms_; |
| 704 | |
Alex Luebs | eeb2765 | 2017-11-20 11:13:56 -0800 | [diff] [blame] | 705 | if (adjust_bandwidth_ && bitrate_changed_) { |
| 706 | const auto bandwidth = GetNewBandwidth(config_, inst_); |
| 707 | if (bandwidth) { |
| 708 | RTC_CHECK_EQ(0, WebRtcOpus_SetBandwidth(inst_, *bandwidth)); |
| 709 | } |
| 710 | bitrate_changed_ = false; |
| 711 | } |
| 712 | |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 713 | info.encoded_timestamp = first_timestamp_in_buffer_; |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 714 | info.payload_type = payload_type_; |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 715 | info.send_even_if_empty = true; // Allows Opus to send empty packets. |
Gustaf Ullberg | 36de62e | 2017-11-20 14:55:41 +0100 | [diff] [blame] | 716 | // After 20 DTX frames (MAX_CONSECUTIVE_DTX) Opus will send a frame |
| 717 | // coding the background noise. Avoid flagging this frame as speech |
| 718 | // (even though there is a probability of the frame being speech). |
| 719 | info.speech = !dtx_frame && (consecutive_dtx_frames_ != 20); |
aleloi | 8bce67b | 2016-05-16 07:34:24 -0700 | [diff] [blame] | 720 | info.encoder_type = CodecType::kOpus; |
Gustaf Ullberg | 36de62e | 2017-11-20 14:55:41 +0100 | [diff] [blame] | 721 | |
| 722 | // Increase or reset DTX counter. |
| 723 | consecutive_dtx_frames_ = (dtx_frame) ? (consecutive_dtx_frames_ + 1) : (0); |
| 724 | |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 725 | return info; |
| 726 | } |
| 727 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 728 | size_t AudioEncoderOpusImpl::Num10msFramesPerPacket() const { |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 729 | return static_cast<size_t>(rtc::CheckedDivExact(config_.frame_size_ms, 10)); |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 732 | size_t AudioEncoderOpusImpl::SamplesPer10msFrame() const { |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 733 | return rtc::CheckedDivExact(kSampleRateHz, 100) * config_.num_channels; |
| 734 | } |
| 735 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 736 | size_t AudioEncoderOpusImpl::SufficientOutputBufferSize() const { |
ossu | 2903ba5 | 2016-04-18 06:14:33 -0700 | [diff] [blame] | 737 | // Calculate the number of bytes we expect the encoder to produce, |
| 738 | // then multiply by two to give a wide margin for error. |
| 739 | const size_t bytes_per_millisecond = |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 740 | static_cast<size_t>(GetBitrateBps(config_) / (1000 * 8) + 1); |
ossu | 2903ba5 | 2016-04-18 06:14:33 -0700 | [diff] [blame] | 741 | const size_t approx_encoded_bytes = |
| 742 | Num10msFramesPerPacket() * 10 * bytes_per_millisecond; |
| 743 | return 2 * approx_encoded_bytes; |
| 744 | } |
| 745 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 746 | // If the given config is OK, recreate the Opus encoder instance with those |
| 747 | // settings, save the config, and return true. Otherwise, do nothing and return |
| 748 | // false. |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 749 | bool AudioEncoderOpusImpl::RecreateEncoderInstance( |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 750 | const AudioEncoderOpusConfig& config) { |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 751 | if (!config.IsOk()) |
| 752 | return false; |
elad.alon | cfd88bb | 2017-03-14 02:50:46 -0700 | [diff] [blame] | 753 | config_ = config; |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 754 | if (inst_) |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 755 | RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 756 | input_buffer_.clear(); |
| 757 | input_buffer_.reserve(Num10msFramesPerPacket() * SamplesPer10msFrame()); |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 758 | RTC_CHECK_EQ(0, WebRtcOpus_EncoderCreate( |
| 759 | &inst_, config.num_channels, |
| 760 | config.application == |
| 761 | AudioEncoderOpusConfig::ApplicationMode::kVoip |
| 762 | ? 0 |
| 763 | : 1)); |
Minyue Li | 9dac02d | 2019-01-18 11:09:47 +0100 | [diff] [blame] | 764 | const int bitrate = GetBitrateBps(config); |
| 765 | RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, bitrate)); |
| 766 | RTC_LOG(LS_INFO) << "Set Opus bitrate to " << bitrate << " bps."; |
henrik.lundin@webrtc.org | 1398025 | 2015-01-29 16:08:40 +0000 | [diff] [blame] | 767 | if (config.fec_enabled) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 768 | RTC_CHECK_EQ(0, WebRtcOpus_EnableFec(inst_)); |
henrik.lundin@webrtc.org | 1398025 | 2015-01-29 16:08:40 +0000 | [diff] [blame] | 769 | } else { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 770 | RTC_CHECK_EQ(0, WebRtcOpus_DisableFec(inst_)); |
henrik.lundin@webrtc.org | 1398025 | 2015-01-29 16:08:40 +0000 | [diff] [blame] | 771 | } |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 772 | RTC_CHECK_EQ( |
| 773 | 0, WebRtcOpus_SetMaxPlaybackRate(inst_, config.max_playback_rate_hz)); |
henrik.lundin | 875862c | 2016-11-22 02:07:54 -0800 | [diff] [blame] | 774 | // Use the default complexity if the start bitrate is within the hysteresis |
| 775 | // window. |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 776 | complexity_ = GetNewComplexity(config).value_or(config.complexity); |
henrik.lundin | 875862c | 2016-11-22 02:07:54 -0800 | [diff] [blame] | 777 | RTC_CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, complexity_)); |
Alex Luebs | eeb2765 | 2017-11-20 11:13:56 -0800 | [diff] [blame] | 778 | bitrate_changed_ = true; |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 779 | if (config.dtx_enabled) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 780 | RTC_CHECK_EQ(0, WebRtcOpus_EnableDtx(inst_)); |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 781 | } else { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 782 | RTC_CHECK_EQ(0, WebRtcOpus_DisableDtx(inst_)); |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 783 | } |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 784 | RTC_CHECK_EQ(0, |
| 785 | WebRtcOpus_SetPacketLossRate( |
| 786 | inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5))); |
ossu | 0d6195d | 2017-04-28 07:33:18 -0700 | [diff] [blame] | 787 | if (config.cbr_enabled) { |
| 788 | RTC_CHECK_EQ(0, WebRtcOpus_EnableCbr(inst_)); |
| 789 | } else { |
| 790 | RTC_CHECK_EQ(0, WebRtcOpus_DisableCbr(inst_)); |
| 791 | } |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 792 | num_channels_to_encode_ = NumChannels(); |
| 793 | next_frame_length_ms_ = config_.frame_size_ms; |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 794 | return true; |
Karl Wiberg | dcccab3 | 2015-05-07 12:35:12 +0200 | [diff] [blame] | 795 | } |
| 796 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 797 | void AudioEncoderOpusImpl::SetFrameLength(int frame_length_ms) { |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 798 | next_frame_length_ms_ = frame_length_ms; |
| 799 | } |
| 800 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 801 | void AudioEncoderOpusImpl::SetNumChannelsToEncode( |
| 802 | size_t num_channels_to_encode) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 803 | RTC_DCHECK_GT(num_channels_to_encode, 0); |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 804 | RTC_DCHECK_LE(num_channels_to_encode, config_.num_channels); |
| 805 | |
| 806 | if (num_channels_to_encode_ == num_channels_to_encode) |
| 807 | return; |
| 808 | |
| 809 | RTC_CHECK_EQ(0, WebRtcOpus_SetForceChannels(inst_, num_channels_to_encode)); |
| 810 | num_channels_to_encode_ = num_channels_to_encode; |
| 811 | } |
| 812 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 813 | void AudioEncoderOpusImpl::SetProjectedPacketLossRate(float fraction) { |
Jakob Ivarsson | 83bd37c | 2018-10-15 09:54:46 +0200 | [diff] [blame] | 814 | if (new_packet_loss_optimizer_) { |
| 815 | fraction = new_packet_loss_optimizer_->OptimizePacketLossRate(fraction); |
| 816 | } else { |
| 817 | fraction = OptimizePacketLossRate(fraction, packet_loss_rate_); |
| 818 | fraction = std::max(fraction, min_packet_loss_rate_); |
| 819 | } |
| 820 | if (packet_loss_rate_ != fraction) { |
| 821 | packet_loss_rate_ = fraction; |
minyue | 4b9a2cb | 2016-11-30 06:49:59 -0800 | [diff] [blame] | 822 | RTC_CHECK_EQ( |
| 823 | 0, WebRtcOpus_SetPacketLossRate( |
| 824 | inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5))); |
| 825 | } |
| 826 | } |
| 827 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 828 | void AudioEncoderOpusImpl::SetTargetBitrate(int bits_per_second) { |
Minyue Li | 9dac02d | 2019-01-18 11:09:47 +0100 | [diff] [blame] | 829 | const int new_bitrate = rtc::SafeClamp<int>( |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 830 | bits_per_second, AudioEncoderOpusConfig::kMinBitrateBps, |
Oskar Sundbom | 12ab00b | 2017-11-16 15:31:38 +0100 | [diff] [blame] | 831 | AudioEncoderOpusConfig::kMaxBitrateBps); |
Minyue Li | 9dac02d | 2019-01-18 11:09:47 +0100 | [diff] [blame] | 832 | if (config_.bitrate_bps && *config_.bitrate_bps != new_bitrate) { |
| 833 | config_.bitrate_bps = new_bitrate; |
| 834 | RTC_DCHECK(config_.IsOk()); |
| 835 | const int bitrate = GetBitrateBps(config_); |
| 836 | RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, bitrate)); |
| 837 | RTC_LOG(LS_INFO) << "Set Opus bitrate to " << bitrate << " bps."; |
| 838 | bitrate_changed_ = true; |
| 839 | } |
| 840 | |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 841 | const auto new_complexity = GetNewComplexity(config_); |
minyue | 4b9a2cb | 2016-11-30 06:49:59 -0800 | [diff] [blame] | 842 | if (new_complexity && complexity_ != *new_complexity) { |
| 843 | complexity_ = *new_complexity; |
| 844 | RTC_CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, complexity_)); |
| 845 | } |
| 846 | } |
| 847 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 848 | void AudioEncoderOpusImpl::ApplyAudioNetworkAdaptor() { |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 849 | auto config = audio_network_adaptor_->GetEncoderRuntimeConfig(); |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 850 | |
michaelt | 584c35a | 2016-12-27 08:21:29 -0800 | [diff] [blame] | 851 | if (config.bitrate_bps) |
| 852 | SetTargetBitrate(*config.bitrate_bps); |
| 853 | if (config.frame_length_ms) |
| 854 | SetFrameLength(*config.frame_length_ms); |
| 855 | if (config.enable_fec) |
| 856 | SetFec(*config.enable_fec); |
| 857 | if (config.uplink_packet_loss_fraction) |
| 858 | SetProjectedPacketLossRate(*config.uplink_packet_loss_fraction); |
| 859 | if (config.enable_dtx) |
| 860 | SetDtx(*config.enable_dtx); |
| 861 | if (config.num_channels) |
| 862 | SetNumChannelsToEncode(*config.num_channels); |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | std::unique_ptr<AudioNetworkAdaptor> |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 866 | AudioEncoderOpusImpl::DefaultAudioNetworkAdaptorCreator( |
Mirko Bonadei | e45c688 | 2019-02-16 09:59:29 +0100 | [diff] [blame] | 867 | const std::string& config_string, |
michaelt | 92aef17 | 2017-04-18 00:11:48 -0700 | [diff] [blame] | 868 | RtcEventLog* event_log) const { |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 869 | AudioNetworkAdaptorImpl::Config config; |
michaelt | bf279fc | 2017-01-13 06:02:29 -0800 | [diff] [blame] | 870 | config.event_log = event_log; |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 871 | return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( |
kwiberg | 96da011 | 2017-06-30 04:23:22 -0700 | [diff] [blame] | 872 | config, ControllerManagerImpl::Create( |
| 873 | config_string, NumChannels(), supported_frame_lengths_ms(), |
| 874 | AudioEncoderOpusConfig::kMinBitrateBps, |
| 875 | num_channels_to_encode_, next_frame_length_ms_, |
| 876 | GetTargetBitrate(), config_.fec_enabled, GetDtx()))); |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 879 | void AudioEncoderOpusImpl::MaybeUpdateUplinkBandwidth() { |
michaelt | 566d820 | 2017-01-12 10:17:38 -0800 | [diff] [blame] | 880 | if (audio_network_adaptor_) { |
Sebastian Jansson | 8ac05cc | 2018-11-21 19:26:12 +0100 | [diff] [blame] | 881 | if (use_link_capacity_for_adaptation_ && link_capacity_allocation_bps_) { |
| 882 | audio_network_adaptor_->SetUplinkBandwidth( |
| 883 | *link_capacity_allocation_bps_); |
| 884 | } else { |
| 885 | int64_t now_ms = rtc::TimeMillis(); |
| 886 | if (!bitrate_smoother_last_update_time_ || |
| 887 | now_ms - *bitrate_smoother_last_update_time_ >= |
| 888 | config_.uplink_bandwidth_update_interval_ms) { |
| 889 | absl::optional<float> smoothed_bitrate = |
| 890 | bitrate_smoother_->GetAverage(); |
| 891 | if (smoothed_bitrate) |
| 892 | audio_network_adaptor_->SetUplinkBandwidth(*smoothed_bitrate); |
| 893 | bitrate_smoother_last_update_time_ = now_ms; |
| 894 | } |
michaelt | 566d820 | 2017-01-12 10:17:38 -0800 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | } |
| 898 | |
Karl Wiberg | 7275e18 | 2017-10-25 09:57:40 +0200 | [diff] [blame] | 899 | ANAStats AudioEncoderOpusImpl::GetANAStats() const { |
ivoc | e1198e0 | 2017-09-08 08:13:19 -0700 | [diff] [blame] | 900 | if (audio_network_adaptor_) { |
| 901 | return audio_network_adaptor_->GetStats(); |
| 902 | } |
| 903 | return ANAStats(); |
| 904 | } |
| 905 | |
kwiberg@webrtc.org | 663fdd0 | 2014-10-29 07:28:36 +0000 | [diff] [blame] | 906 | } // namespace webrtc |