Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.h" | 11 #include "webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.h" |
| 12 | 12 |
| 13 #include <stdlib.h> | |
|
the sun
2017/03/17 09:07:31
nit: what for?
ossu
2017/03/17 09:55:39
Not sure. Will look into it. May be a hold-over fr
| |
| 14 | |
| 13 #include <algorithm> | 15 #include <algorithm> |
| 14 #include <iterator> | 16 #include <iterator> |
| 15 | 17 |
| 18 #include "webrtc/base/arraysize.h" | |
| 16 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
| 17 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
| 18 #include "webrtc/base/numerics/exp_filter.h" | 21 #include "webrtc/base/numerics/exp_filter.h" |
| 19 #include "webrtc/base/safe_conversions.h" | 22 #include "webrtc/base/safe_conversions.h" |
| 23 #include "webrtc/base/string_to_number.h" | |
| 20 #include "webrtc/base/timeutils.h" | 24 #include "webrtc/base/timeutils.h" |
| 21 #include "webrtc/common_types.h" | 25 #include "webrtc/common_types.h" |
| 22 #include "webrtc/modules/audio_coding/audio_network_adaptor/audio_network_adapto r_impl.h" | 26 #include "webrtc/modules/audio_coding/audio_network_adaptor/audio_network_adapto r_impl.h" |
| 23 #include "webrtc/modules/audio_coding/audio_network_adaptor/controller_manager.h " | 27 #include "webrtc/modules/audio_coding/audio_network_adaptor/controller_manager.h " |
| 24 #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h" | 28 #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h" |
| 25 #include "webrtc/system_wrappers/include/field_trial.h" | 29 #include "webrtc/system_wrappers/include/field_trial.h" |
| 26 | 30 |
| 27 namespace webrtc { | 31 namespace webrtc { |
| 28 | 32 |
| 29 namespace { | 33 namespace { |
| 30 | 34 |
| 35 // Codec parameters for Opus. | |
| 36 // draft-spittka-payload-rtp-opus-03 | |
| 37 | |
| 38 // Recommended bitrates: | |
| 39 // 8-12 kb/s for NB speech, | |
| 40 // 16-20 kb/s for WB speech, | |
| 41 // 28-40 kb/s for FB speech, | |
| 42 // 48-64 kb/s for FB mono music, and | |
| 43 // 64-128 kb/s for FB stereo music. | |
| 44 // The current implementation applies the following values to mono signals, | |
| 45 // and multiplies them by 2 for stereo. | |
| 46 constexpr int kOpusBitrateNbBps = 12000; | |
| 47 constexpr int kOpusBitrateWbBps = 20000; | |
| 48 constexpr int kOpusBitrateFbBps = 32000; | |
| 49 | |
| 50 // Opus API allows a min bitrate of 500bps, but Opus documentation suggests | |
| 51 // bitrate should be in the range of 6000 to 510000, inclusive. | |
| 52 constexpr int kOpusMinBitrateBps = 6000; | |
| 53 constexpr int kOpusMaxBitrateBps = 510000; | |
| 54 | |
| 31 constexpr int kSampleRateHz = 48000; | 55 constexpr int kSampleRateHz = 48000; |
| 32 | 56 |
| 33 // Opus API allows a min bitrate of 500bps, but Opus documentation suggests | 57 // These two lists must be sorted from low to high |
| 34 // a minimum bitrate of 6kbps. | |
| 35 constexpr int kMinBitrateBps = 6000; | |
| 36 | |
| 37 constexpr int kMaxBitrateBps = 512000; | |
| 38 | |
| 39 #if WEBRTC_OPUS_SUPPORT_120MS_PTIME | 58 #if WEBRTC_OPUS_SUPPORT_120MS_PTIME |
| 40 constexpr int kSupportedFrameLengths[] = {20, 60, 120}; | 59 constexpr int kANASupportedFrameLengths[] = {20, 60, 120}; |
| 60 constexpr int kOpusSupportedFrameLengths[] = {10, 20, 40, 60, 120}; | |
| 41 #else | 61 #else |
| 42 constexpr int kSupportedFrameLengths[] = {20, 60}; | 62 constexpr int kANASupportedFrameLengths[] = {20, 60}; |
| 63 constexpr int kOpusSupportedFrameLengths[] = {10, 20, 40, 60}; | |
| 43 #endif | 64 #endif |
| 44 | 65 |
| 45 // PacketLossFractionSmoother uses an exponential filter with a time constant | 66 // PacketLossFractionSmoother uses an exponential filter with a time constant |
| 46 // of -1.0 / ln(0.9999) = 10000 ms. | 67 // of -1.0 / ln(0.9999) = 10000 ms. |
| 47 constexpr float kAlphaForPacketLossFractionSmoother = 0.9999f; | 68 constexpr float kAlphaForPacketLossFractionSmoother = 0.9999f; |
| 48 | 69 |
| 49 AudioEncoderOpus::Config CreateConfig(const CodecInst& codec_inst) { | |
| 50 AudioEncoderOpus::Config config; | |
| 51 config.frame_size_ms = rtc::CheckedDivExact(codec_inst.pacsize, 48); | |
| 52 config.num_channels = codec_inst.channels; | |
| 53 config.bitrate_bps = rtc::Optional<int>(codec_inst.rate); | |
| 54 config.payload_type = codec_inst.pltype; | |
| 55 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip | |
| 56 : AudioEncoderOpus::kAudio; | |
| 57 config.supported_frame_lengths_ms.push_back(config.frame_size_ms); | |
| 58 #if WEBRTC_OPUS_VARIABLE_COMPLEXITY | |
| 59 config.low_rate_complexity = 9; | |
| 60 #endif | |
| 61 return config; | |
| 62 } | |
| 63 | |
| 64 // Optimize the loss rate to configure Opus. Basically, optimized loss rate is | 70 // Optimize the loss rate to configure Opus. Basically, optimized loss rate is |
| 65 // the input loss rate rounded down to various levels, because a robustly good | 71 // the input loss rate rounded down to various levels, because a robustly good |
| 66 // audio quality is achieved by lowering the packet loss down. | 72 // audio quality is achieved by lowering the packet loss down. |
| 67 // Additionally, to prevent toggling, margins are used, i.e., when jumping to | 73 // Additionally, to prevent toggling, margins are used, i.e., when jumping to |
| 68 // a loss rate from below, a higher threshold is used than jumping to the same | 74 // a loss rate from below, a higher threshold is used than jumping to the same |
| 69 // level from above. | 75 // level from above. |
| 70 float OptimizePacketLossRate(float new_loss_rate, float old_loss_rate) { | 76 float OptimizePacketLossRate(float new_loss_rate, float old_loss_rate) { |
| 71 RTC_DCHECK_GE(new_loss_rate, 0.0f); | 77 RTC_DCHECK_GE(new_loss_rate, 0.0f); |
| 72 RTC_DCHECK_LE(new_loss_rate, 1.0f); | 78 RTC_DCHECK_LE(new_loss_rate, 1.0f); |
| 73 RTC_DCHECK_GE(old_loss_rate, 0.0f); | 79 RTC_DCHECK_GE(old_loss_rate, 0.0f); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 94 kLossRate5Margin * | 100 kLossRate5Margin * |
| 95 (kPacketLossRate5 - old_loss_rate > 0 ? 1 : -1)) { | 101 (kPacketLossRate5 - old_loss_rate > 0 ? 1 : -1)) { |
| 96 return kPacketLossRate5; | 102 return kPacketLossRate5; |
| 97 } else if (new_loss_rate >= kPacketLossRate1) { | 103 } else if (new_loss_rate >= kPacketLossRate1) { |
| 98 return kPacketLossRate1; | 104 return kPacketLossRate1; |
| 99 } else { | 105 } else { |
| 100 return 0.0f; | 106 return 0.0f; |
| 101 } | 107 } |
| 102 } | 108 } |
| 103 | 109 |
| 110 rtc::Optional<std::string> GetFormatParameter(const SdpAudioFormat& format, | |
| 111 const std::string& param) { | |
| 112 auto it = format.parameters.find(param); | |
| 113 return (it == format.parameters.end()) | |
| 114 ? rtc::Optional<std::string>() | |
| 115 : rtc::Optional<std::string>(it->second); | |
| 116 } | |
| 117 | |
| 118 template <typename T> | |
| 119 rtc::Optional<T> GetFormatParameter(const SdpAudioFormat& format, | |
| 120 const std::string& param) { | |
| 121 return rtc::StringToNumber<T>(GetFormatParameter(format, param).value_or("")); | |
| 122 }; | |
| 123 | |
| 124 int CalculateDefaultBitrate(int max_playback_rate, int num_channels) { | |
| 125 const int bitrate = [&] { | |
| 126 if (max_playback_rate <= 8000) { | |
| 127 return kOpusBitrateNbBps * num_channels; | |
| 128 } else if (max_playback_rate <= 16000) { | |
| 129 return kOpusBitrateWbBps * num_channels; | |
| 130 } else { | |
| 131 return kOpusBitrateFbBps * num_channels; | |
| 132 } | |
| 133 }(); | |
| 134 RTC_DCHECK_GE(bitrate, kOpusMinBitrateBps); | |
| 135 RTC_DCHECK_LE(bitrate, kOpusMaxBitrateBps); | |
| 136 return bitrate; | |
| 137 } | |
| 138 | |
| 139 // Get the maxaveragebitrate parameter in string-form, so we can properly figure | |
| 140 // out how invalid it is and accurately log invalid values. | |
| 141 int CalculateBitrate(int max_playback_rate_hz, | |
| 142 int num_channels, | |
| 143 rtc::Optional<std::string> bitrate_param) { | |
| 144 const int default_bitrate = | |
| 145 CalculateDefaultBitrate(max_playback_rate_hz, num_channels); | |
| 146 | |
| 147 if (bitrate_param) { | |
| 148 const auto bitrate = rtc::StringToNumber<int>(*bitrate_param); | |
| 149 if (bitrate) { | |
| 150 if (*bitrate >= kOpusMinBitrateBps && *bitrate <= kOpusMaxBitrateBps) { | |
| 151 return *bitrate; | |
| 152 } | |
| 153 | |
| 154 const int new_bitrate = (*bitrate < kOpusMinBitrateBps) | |
| 155 ? kOpusMinBitrateBps | |
| 156 : kOpusMaxBitrateBps; | |
| 157 LOG(LS_WARNING) << "Invalid maxaveragebitrate " << *bitrate | |
| 158 << " clamped to " << new_bitrate; | |
| 159 return new_bitrate; | |
| 160 } | |
| 161 | |
| 162 LOG(LS_WARNING) << "Invalid maxaveragebitrate \"" << *bitrate_param | |
| 163 << "\" replaced by default bitrate " << default_bitrate; | |
| 164 } | |
| 165 | |
| 166 return default_bitrate; | |
| 167 } | |
| 168 | |
| 169 rtc::Optional<int> GetChannelCount(const SdpAudioFormat& format) { | |
| 170 const auto param = GetFormatParameter(format, "stereo"); | |
| 171 if (!param || param == "0") { | |
| 172 return rtc::Optional<int>(1); | |
| 173 } else if (param == "1") { | |
| 174 return rtc::Optional<int>(2); | |
| 175 } | |
| 176 return rtc::Optional<int>(); | |
| 177 } | |
| 178 | |
| 179 rtc::Optional<int> GetMaxPlaybackRate(const SdpAudioFormat& format) { | |
| 180 const auto param = GetFormatParameter(format, "maxplaybackrate"); | |
| 181 if (!param) { | |
| 182 return rtc::Optional<int>(48000); | |
| 183 } | |
| 184 const auto parsed = rtc::StringToNumber<int>(*param); | |
| 185 if (parsed && *parsed >= 8000) { | |
| 186 return rtc::Optional<int>(*parsed); | |
| 187 } | |
| 188 return rtc::Optional<int>(); | |
| 189 } | |
| 190 | |
| 104 } // namespace | 191 } // namespace |
| 105 | 192 |
| 193 rtc::Optional<AudioCodecInfo> AudioEncoderOpus::QueryAudioEncoder( | |
| 194 const SdpAudioFormat& format) { | |
| 195 if (STR_CASE_CMP(format.name.c_str(), GetPayloadName()) == 0 && | |
| 196 format.clockrate_hz == 48000 && format.num_channels == 2) { | |
| 197 | |
| 198 const rtc::Optional<int> num_channels = GetChannelCount(format); | |
| 199 const rtc::Optional<int> max_playback_rate = GetMaxPlaybackRate(format); | |
| 200 if (num_channels && max_playback_rate) { | |
| 201 const int bitrate = | |
| 202 CalculateBitrate(*max_playback_rate, *num_channels, | |
| 203 GetFormatParameter(format, "maxaveragebitrate")); | |
| 204 AudioCodecInfo info(48000, *num_channels, bitrate, kOpusMinBitrateBps, | |
| 205 kOpusMaxBitrateBps); | |
| 206 info.allow_comfort_noise = false; | |
| 207 info.supports_network_adaption = true; | |
| 208 | |
| 209 return rtc::Optional<AudioCodecInfo>(info); | |
| 210 } | |
| 211 } | |
| 212 return rtc::Optional<AudioCodecInfo>(); | |
| 213 } | |
| 214 | |
| 215 AudioEncoderOpus::Config AudioEncoderOpus::CreateConfig( | |
| 216 const CodecInst& codec_inst) { | |
| 217 AudioEncoderOpus::Config config; | |
| 218 config.frame_size_ms = rtc::CheckedDivExact(codec_inst.pacsize, 48); | |
| 219 config.num_channels = codec_inst.channels; | |
| 220 config.bitrate_bps = rtc::Optional<int>(codec_inst.rate); | |
| 221 config.payload_type = codec_inst.pltype; | |
| 222 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip | |
| 223 : AudioEncoderOpus::kAudio; | |
| 224 config.supported_frame_lengths_ms.push_back(config.frame_size_ms); | |
| 225 #if WEBRTC_OPUS_VARIABLE_COMPLEXITY | |
| 226 config.low_rate_complexity = 9; | |
| 227 #endif | |
| 228 return config; | |
| 229 } | |
| 230 | |
| 231 AudioEncoderOpus::Config AudioEncoderOpus::CreateConfig( | |
| 232 int payload_type, | |
| 233 const SdpAudioFormat& format) { | |
| 234 AudioEncoderOpus::Config config; | |
| 235 | |
| 236 // Normally, the first two parameters should already have been checked by | |
| 237 // QueryAudioEncoder. At this point, we might as well fall back to something | |
| 238 // reasonable. | |
| 239 config.num_channels = GetChannelCount(format).value_or(1); | |
| 240 config.max_playback_rate_hz = GetMaxPlaybackRate(format).value_or(48000); | |
| 241 config.fec_enabled = (GetFormatParameter(format, "useinbandfec") == "1"); | |
| 242 config.dtx_enabled = (GetFormatParameter(format, "usedtx") == "1"); | |
| 243 config.bitrate_bps = rtc::Optional<int>( | |
| 244 CalculateBitrate(config.max_playback_rate_hz, config.num_channels, | |
| 245 GetFormatParameter(format, "maxaveragebitrate"))); | |
| 246 config.payload_type = payload_type; | |
| 247 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip | |
| 248 : AudioEncoderOpus::kAudio; | |
| 249 #if WEBRTC_OPUS_VARIABLE_COMPLEXITY | |
| 250 config.low_rate_complexity = 9; | |
| 251 #endif | |
| 252 | |
| 253 constexpr int kMinSupportedFrameLength = kOpusSupportedFrameLengths[0]; | |
| 254 constexpr int kMaxSupportedFrameLength = | |
| 255 kOpusSupportedFrameLengths[arraysize(kOpusSupportedFrameLengths) - 1]; | |
| 256 | |
| 257 const auto ptime = GetFormatParameter<int>(format, "ptime"); | |
| 258 if (ptime) { | |
| 259 // Pick the next highest supported frame length from | |
| 260 // kOpusSupportedFrameLengths. Default to the largest, if we find none. | |
| 261 config.frame_size_ms = kMaxSupportedFrameLength; | |
| 262 for (const int supported_frame_length : kOpusSupportedFrameLengths) { | |
| 263 if (supported_frame_length >= *ptime) { | |
| 264 config.frame_size_ms = supported_frame_length; | |
| 265 break; | |
| 266 } | |
| 267 } | |
| 268 } | |
| 269 | |
| 270 // For now, minptime and maxptime are only used with ANA. If ptime is outside | |
| 271 // of this range, it will get adjusted once ANA takes hold. Ideally, we'd know | |
| 272 // if ANA was to be used when setting up the config, and adjust accordingly. | |
| 273 const int min_frame_length_ms = | |
| 274 std::min(std::max(GetFormatParameter<int>(format, "minptime") | |
| 275 .value_or(kMinSupportedFrameLength), | |
| 276 kMinSupportedFrameLength), | |
| 277 kMaxSupportedFrameLength); | |
| 278 const int max_frame_length_ms = | |
| 279 std::min(std::max(GetFormatParameter<int>(format, "maxptime") | |
| 280 .value_or(kMaxSupportedFrameLength), | |
| 281 kMinSupportedFrameLength), | |
| 282 kMaxSupportedFrameLength); | |
| 283 | |
| 284 for (const int frame_length_ms : kANASupportedFrameLengths) { | |
| 285 if (frame_length_ms >= min_frame_length_ms && | |
| 286 frame_length_ms <= max_frame_length_ms) { | |
| 287 config.supported_frame_lengths_ms.push_back(frame_length_ms); | |
| 288 } | |
| 289 } | |
| 290 | |
| 291 // As a fallback, just pick the whole set of supported frame lengths. | |
| 292 if (config.supported_frame_lengths_ms.empty()) { | |
| 293 config.supported_frame_lengths_ms.assign( | |
| 294 std::begin(kANASupportedFrameLengths), | |
| 295 std::end(kANASupportedFrameLengths)); | |
| 296 } | |
| 297 | |
| 298 RTC_DCHECK(std::is_sorted(config.supported_frame_lengths_ms.begin(), | |
| 299 config.supported_frame_lengths_ms.end())); | |
| 300 | |
| 301 return config; | |
| 302 } | |
| 303 | |
| 106 class AudioEncoderOpus::PacketLossFractionSmoother { | 304 class AudioEncoderOpus::PacketLossFractionSmoother { |
| 107 public: | 305 public: |
| 108 explicit PacketLossFractionSmoother(const Clock* clock) | 306 explicit PacketLossFractionSmoother(const Clock* clock) |
| 109 : clock_(clock), | 307 : clock_(clock), |
| 110 last_sample_time_ms_(clock_->TimeInMilliseconds()), | 308 last_sample_time_ms_(clock_->TimeInMilliseconds()), |
| 111 smoother_(kAlphaForPacketLossFractionSmoother) {} | 309 smoother_(kAlphaForPacketLossFractionSmoother) {} |
| 112 | 310 |
| 113 // Gets the smoothed packet loss fraction. | 311 // Gets the smoothed packet loss fraction. |
| 114 float GetAverage() const { | 312 float GetAverage() const { |
| 115 float value = smoother_.filtered(); | 313 float value = smoother_.filtered(); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 140 AudioEncoderOpus::Config::Config(const Config&) = default; | 338 AudioEncoderOpus::Config::Config(const Config&) = default; |
| 141 AudioEncoderOpus::Config::~Config() = default; | 339 AudioEncoderOpus::Config::~Config() = default; |
| 142 auto AudioEncoderOpus::Config::operator=(const Config&) -> Config& = default; | 340 auto AudioEncoderOpus::Config::operator=(const Config&) -> Config& = default; |
| 143 | 341 |
| 144 bool AudioEncoderOpus::Config::IsOk() const { | 342 bool AudioEncoderOpus::Config::IsOk() const { |
| 145 if (frame_size_ms <= 0 || frame_size_ms % 10 != 0) | 343 if (frame_size_ms <= 0 || frame_size_ms % 10 != 0) |
| 146 return false; | 344 return false; |
| 147 if (num_channels != 1 && num_channels != 2) | 345 if (num_channels != 1 && num_channels != 2) |
| 148 return false; | 346 return false; |
| 149 if (bitrate_bps && | 347 if (bitrate_bps && |
| 150 (*bitrate_bps < kMinBitrateBps || *bitrate_bps > kMaxBitrateBps)) | 348 (*bitrate_bps < kOpusMinBitrateBps || *bitrate_bps > kOpusMaxBitrateBps)) |
| 151 return false; | 349 return false; |
| 152 if (complexity < 0 || complexity > 10) | 350 if (complexity < 0 || complexity > 10) |
| 153 return false; | 351 return false; |
| 154 if (low_rate_complexity < 0 || low_rate_complexity > 10) | 352 if (low_rate_complexity < 0 || low_rate_complexity > 10) |
| 155 return false; | 353 return false; |
| 156 return true; | 354 return true; |
| 157 } | 355 } |
| 158 | 356 |
| 159 int AudioEncoderOpus::Config::GetBitrateBps() const { | 357 int AudioEncoderOpus::Config::GetBitrateBps() const { |
| 160 RTC_DCHECK(IsOk()); | 358 RTC_DCHECK(IsOk()); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 201 bitrate_smoother_(bitrate_smoother | 399 bitrate_smoother_(bitrate_smoother |
| 202 ? std::move(bitrate_smoother) : std::unique_ptr<SmoothingFilter>( | 400 ? std::move(bitrate_smoother) : std::unique_ptr<SmoothingFilter>( |
| 203 // We choose 5sec as initial time constant due to empirical data. | 401 // We choose 5sec as initial time constant due to empirical data. |
| 204 new SmoothingFilterImpl(5000, config.clock))) { | 402 new SmoothingFilterImpl(5000, config.clock))) { |
| 205 RTC_CHECK(RecreateEncoderInstance(config)); | 403 RTC_CHECK(RecreateEncoderInstance(config)); |
| 206 } | 404 } |
| 207 | 405 |
| 208 AudioEncoderOpus::AudioEncoderOpus(const CodecInst& codec_inst) | 406 AudioEncoderOpus::AudioEncoderOpus(const CodecInst& codec_inst) |
| 209 : AudioEncoderOpus(CreateConfig(codec_inst), nullptr) {} | 407 : AudioEncoderOpus(CreateConfig(codec_inst), nullptr) {} |
| 210 | 408 |
| 409 AudioEncoderOpus::AudioEncoderOpus(int payload_type, | |
| 410 const SdpAudioFormat& format) | |
| 411 : AudioEncoderOpus(CreateConfig(payload_type, format), nullptr) {} | |
| 412 | |
| 211 AudioEncoderOpus::~AudioEncoderOpus() { | 413 AudioEncoderOpus::~AudioEncoderOpus() { |
| 212 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); | 414 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); |
| 213 } | 415 } |
| 214 | 416 |
| 215 int AudioEncoderOpus::SampleRateHz() const { | 417 int AudioEncoderOpus::SampleRateHz() const { |
| 216 return kSampleRateHz; | 418 return kSampleRateHz; |
| 217 } | 419 } |
| 218 | 420 |
| 219 size_t AudioEncoderOpus::NumChannels() const { | 421 size_t AudioEncoderOpus::NumChannels() const { |
| 220 return config_.num_channels; | 422 return config_.num_channels; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 328 } else if (send_side_bwe_with_overhead_) { | 530 } else if (send_side_bwe_with_overhead_) { |
| 329 if (!overhead_bytes_per_packet_) { | 531 if (!overhead_bytes_per_packet_) { |
| 330 LOG(LS_INFO) | 532 LOG(LS_INFO) |
| 331 << "AudioEncoderOpus: Overhead unknown, target audio bitrate " | 533 << "AudioEncoderOpus: Overhead unknown, target audio bitrate " |
| 332 << target_audio_bitrate_bps << " bps is ignored."; | 534 << target_audio_bitrate_bps << " bps is ignored."; |
| 333 return; | 535 return; |
| 334 } | 536 } |
| 335 const int overhead_bps = static_cast<int>( | 537 const int overhead_bps = static_cast<int>( |
| 336 *overhead_bytes_per_packet_ * 8 * 100 / Num10MsFramesInNextPacket()); | 538 *overhead_bytes_per_packet_ * 8 * 100 / Num10MsFramesInNextPacket()); |
| 337 SetTargetBitrate(std::min( | 539 SetTargetBitrate(std::min( |
| 338 kMaxBitrateBps, | 540 kOpusMaxBitrateBps, |
| 339 std::max(kMinBitrateBps, target_audio_bitrate_bps - overhead_bps))); | 541 std::max(kOpusMinBitrateBps, target_audio_bitrate_bps - overhead_bps))); |
| 340 } else { | 542 } else { |
| 341 SetTargetBitrate(target_audio_bitrate_bps); | 543 SetTargetBitrate(target_audio_bitrate_bps); |
| 342 } | 544 } |
| 343 } | 545 } |
| 344 | 546 |
| 345 void AudioEncoderOpus::OnReceivedRtt(int rtt_ms) { | 547 void AudioEncoderOpus::OnReceivedRtt(int rtt_ms) { |
| 346 if (!audio_network_adaptor_) | 548 if (!audio_network_adaptor_) |
| 347 return; | 549 return; |
| 348 audio_network_adaptor_->SetRtt(rtt_ms); | 550 audio_network_adaptor_->SetRtt(rtt_ms); |
| 349 ApplyAudioNetworkAdaptor(); | 551 ApplyAudioNetworkAdaptor(); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 360 } | 562 } |
| 361 | 563 |
| 362 void AudioEncoderOpus::SetReceiverFrameLengthRange(int min_frame_length_ms, | 564 void AudioEncoderOpus::SetReceiverFrameLengthRange(int min_frame_length_ms, |
| 363 int max_frame_length_ms) { | 565 int max_frame_length_ms) { |
| 364 // Ensure that |SetReceiverFrameLengthRange| is called before | 566 // Ensure that |SetReceiverFrameLengthRange| is called before |
| 365 // |EnableAudioNetworkAdaptor|, otherwise we need to recreate | 567 // |EnableAudioNetworkAdaptor|, otherwise we need to recreate |
| 366 // |audio_network_adaptor_|, which is not a needed use case. | 568 // |audio_network_adaptor_|, which is not a needed use case. |
| 367 RTC_DCHECK(!audio_network_adaptor_); | 569 RTC_DCHECK(!audio_network_adaptor_); |
| 368 | 570 |
| 369 config_.supported_frame_lengths_ms.clear(); | 571 config_.supported_frame_lengths_ms.clear(); |
| 370 std::copy_if(std::begin(kSupportedFrameLengths), | 572 std::copy_if(std::begin(kANASupportedFrameLengths), |
| 371 std::end(kSupportedFrameLengths), | 573 std::end(kANASupportedFrameLengths), |
| 372 std::back_inserter(config_.supported_frame_lengths_ms), | 574 std::back_inserter(config_.supported_frame_lengths_ms), |
| 373 [&](int frame_length_ms) { | 575 [&](int frame_length_ms) { |
| 374 return frame_length_ms >= min_frame_length_ms && | 576 return frame_length_ms >= min_frame_length_ms && |
| 375 frame_length_ms <= max_frame_length_ms; | 577 frame_length_ms <= max_frame_length_ms; |
| 376 }); | 578 }); |
| 377 RTC_DCHECK(std::is_sorted(config_.supported_frame_lengths_ms.begin(), | 579 RTC_DCHECK(std::is_sorted(config_.supported_frame_lengths_ms.begin(), |
| 378 config_.supported_frame_lengths_ms.end())); | 580 config_.supported_frame_lengths_ms.end())); |
| 379 } | 581 } |
| 380 | 582 |
| 381 AudioEncoder::EncodedInfo AudioEncoderOpus::EncodeImpl( | 583 AudioEncoder::EncodedInfo AudioEncoderOpus::EncodeImpl( |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 500 float opt_loss_rate = OptimizePacketLossRate(fraction, packet_loss_rate_); | 702 float opt_loss_rate = OptimizePacketLossRate(fraction, packet_loss_rate_); |
| 501 if (packet_loss_rate_ != opt_loss_rate) { | 703 if (packet_loss_rate_ != opt_loss_rate) { |
| 502 packet_loss_rate_ = opt_loss_rate; | 704 packet_loss_rate_ = opt_loss_rate; |
| 503 RTC_CHECK_EQ( | 705 RTC_CHECK_EQ( |
| 504 0, WebRtcOpus_SetPacketLossRate( | 706 0, WebRtcOpus_SetPacketLossRate( |
| 505 inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5))); | 707 inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5))); |
| 506 } | 708 } |
| 507 } | 709 } |
| 508 | 710 |
| 509 void AudioEncoderOpus::SetTargetBitrate(int bits_per_second) { | 711 void AudioEncoderOpus::SetTargetBitrate(int bits_per_second) { |
| 510 config_.bitrate_bps = rtc::Optional<int>( | 712 config_.bitrate_bps = rtc::Optional<int>(std::max( |
| 511 std::max(std::min(bits_per_second, kMaxBitrateBps), kMinBitrateBps)); | 713 std::min(bits_per_second, kOpusMaxBitrateBps), kOpusMinBitrateBps)); |
| 512 RTC_DCHECK(config_.IsOk()); | 714 RTC_DCHECK(config_.IsOk()); |
| 513 RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, config_.GetBitrateBps())); | 715 RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, config_.GetBitrateBps())); |
| 514 const auto new_complexity = config_.GetNewComplexity(); | 716 const auto new_complexity = config_.GetNewComplexity(); |
| 515 if (new_complexity && complexity_ != *new_complexity) { | 717 if (new_complexity && complexity_ != *new_complexity) { |
| 516 complexity_ = *new_complexity; | 718 complexity_ = *new_complexity; |
| 517 RTC_CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, complexity_)); | 719 RTC_CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, complexity_)); |
| 518 } | 720 } |
| 519 } | 721 } |
| 520 | 722 |
| 521 void AudioEncoderOpus::ApplyAudioNetworkAdaptor() { | 723 void AudioEncoderOpus::ApplyAudioNetworkAdaptor() { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 542 const std::string& config_string, | 744 const std::string& config_string, |
| 543 RtcEventLog* event_log, | 745 RtcEventLog* event_log, |
| 544 const Clock* clock) const { | 746 const Clock* clock) const { |
| 545 AudioNetworkAdaptorImpl::Config config; | 747 AudioNetworkAdaptorImpl::Config config; |
| 546 config.clock = clock; | 748 config.clock = clock; |
| 547 config.event_log = event_log; | 749 config.event_log = event_log; |
| 548 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( | 750 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( |
| 549 config, | 751 config, |
| 550 ControllerManagerImpl::Create( | 752 ControllerManagerImpl::Create( |
| 551 config_string, NumChannels(), supported_frame_lengths_ms(), | 753 config_string, NumChannels(), supported_frame_lengths_ms(), |
| 552 kMinBitrateBps, num_channels_to_encode_, next_frame_length_ms_, | 754 kOpusMinBitrateBps, num_channels_to_encode_, next_frame_length_ms_, |
| 553 GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); | 755 GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); |
| 554 } | 756 } |
| 555 | 757 |
| 556 void AudioEncoderOpus::MaybeUpdateUplinkBandwidth() { | 758 void AudioEncoderOpus::MaybeUpdateUplinkBandwidth() { |
| 557 if (audio_network_adaptor_) { | 759 if (audio_network_adaptor_) { |
| 558 int64_t now_ms = rtc::TimeMillis(); | 760 int64_t now_ms = rtc::TimeMillis(); |
| 559 if (!bitrate_smoother_last_update_time_ || | 761 if (!bitrate_smoother_last_update_time_ || |
| 560 now_ms - *bitrate_smoother_last_update_time_ >= | 762 now_ms - *bitrate_smoother_last_update_time_ >= |
| 561 config_.uplink_bandwidth_update_interval_ms) { | 763 config_.uplink_bandwidth_update_interval_ms) { |
| 562 rtc::Optional<float> smoothed_bitrate = bitrate_smoother_->GetAverage(); | 764 rtc::Optional<float> smoothed_bitrate = bitrate_smoother_->GetAverage(); |
| 563 if (smoothed_bitrate) | 765 if (smoothed_bitrate) |
| 564 audio_network_adaptor_->SetUplinkBandwidth(*smoothed_bitrate); | 766 audio_network_adaptor_->SetUplinkBandwidth(*smoothed_bitrate); |
| 565 bitrate_smoother_last_update_time_ = rtc::Optional<int64_t>(now_ms); | 767 bitrate_smoother_last_update_time_ = rtc::Optional<int64_t>(now_ms); |
| 566 } | 768 } |
| 567 } | 769 } |
| 568 } | 770 } |
| 569 | 771 |
| 570 } // namespace webrtc | 772 } // namespace webrtc |
| OLD | NEW |