| 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 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 // 16-20 kb/s for WB speech, | 41 // 16-20 kb/s for WB speech, |
| 42 // 28-40 kb/s for FB speech, | 42 // 28-40 kb/s for FB speech, |
| 43 // 48-64 kb/s for FB mono music, and | 43 // 48-64 kb/s for FB mono music, and |
| 44 // 64-128 kb/s for FB stereo music. | 44 // 64-128 kb/s for FB stereo music. |
| 45 // The current implementation applies the following values to mono signals, | 45 // The current implementation applies the following values to mono signals, |
| 46 // and multiplies them by 2 for stereo. | 46 // and multiplies them by 2 for stereo. |
| 47 constexpr int kOpusBitrateNbBps = 12000; | 47 constexpr int kOpusBitrateNbBps = 12000; |
| 48 constexpr int kOpusBitrateWbBps = 20000; | 48 constexpr int kOpusBitrateWbBps = 20000; |
| 49 constexpr int kOpusBitrateFbBps = 32000; | 49 constexpr int kOpusBitrateFbBps = 32000; |
| 50 | 50 |
| 51 // Opus API allows a min bitrate of 500bps, but Opus documentation suggests |
| 52 // bitrate should be in the range of 6000 to 510000, inclusive. |
| 53 constexpr int kOpusMinBitrateBps = 6000; |
| 54 constexpr int kOpusMaxBitrateBps = 510000; |
| 55 |
| 51 constexpr int kSampleRateHz = 48000; | 56 constexpr int kSampleRateHz = 48000; |
| 52 constexpr int kDefaultMaxPlaybackRate = 48000; | 57 constexpr int kDefaultMaxPlaybackRate = 48000; |
| 53 | 58 |
| 54 // These two lists must be sorted from low to high | 59 // These two lists must be sorted from low to high |
| 55 #if WEBRTC_OPUS_SUPPORT_120MS_PTIME | 60 #if WEBRTC_OPUS_SUPPORT_120MS_PTIME |
| 56 constexpr int kANASupportedFrameLengths[] = {20, 60, 120}; | 61 constexpr int kANASupportedFrameLengths[] = {20, 60, 120}; |
| 57 constexpr int kOpusSupportedFrameLengths[] = {10, 20, 40, 60, 120}; | 62 constexpr int kOpusSupportedFrameLengths[] = {10, 20, 40, 60, 120}; |
| 58 #else | 63 #else |
| 59 constexpr int kANASupportedFrameLengths[] = {20, 60}; | 64 constexpr int kANASupportedFrameLengths[] = {20, 60}; |
| 60 constexpr int kOpusSupportedFrameLengths[] = {10, 20, 40, 60}; | 65 constexpr int kOpusSupportedFrameLengths[] = {10, 20, 40, 60}; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 int CalculateDefaultBitrate(int max_playback_rate, size_t num_channels) { | 126 int CalculateDefaultBitrate(int max_playback_rate, size_t num_channels) { |
| 122 const int bitrate = [&] { | 127 const int bitrate = [&] { |
| 123 if (max_playback_rate <= 8000) { | 128 if (max_playback_rate <= 8000) { |
| 124 return kOpusBitrateNbBps * rtc::dchecked_cast<int>(num_channels); | 129 return kOpusBitrateNbBps * rtc::dchecked_cast<int>(num_channels); |
| 125 } else if (max_playback_rate <= 16000) { | 130 } else if (max_playback_rate <= 16000) { |
| 126 return kOpusBitrateWbBps * rtc::dchecked_cast<int>(num_channels); | 131 return kOpusBitrateWbBps * rtc::dchecked_cast<int>(num_channels); |
| 127 } else { | 132 } else { |
| 128 return kOpusBitrateFbBps * rtc::dchecked_cast<int>(num_channels); | 133 return kOpusBitrateFbBps * rtc::dchecked_cast<int>(num_channels); |
| 129 } | 134 } |
| 130 }(); | 135 }(); |
| 131 RTC_DCHECK_GE(bitrate, AudioEncoderOpusConfig::kMinBitrateBps); | 136 RTC_DCHECK_GE(bitrate, kOpusMinBitrateBps); |
| 132 RTC_DCHECK_LE(bitrate, AudioEncoderOpusConfig::kMaxBitrateBps); | 137 RTC_DCHECK_LE(bitrate, kOpusMaxBitrateBps); |
| 133 return bitrate; | 138 return bitrate; |
| 134 } | 139 } |
| 135 | 140 |
| 136 // Get the maxaveragebitrate parameter in string-form, so we can properly figure | 141 // Get the maxaveragebitrate parameter in string-form, so we can properly figure |
| 137 // out how invalid it is and accurately log invalid values. | 142 // out how invalid it is and accurately log invalid values. |
| 138 int CalculateBitrate(int max_playback_rate_hz, | 143 int CalculateBitrate(int max_playback_rate_hz, |
| 139 size_t num_channels, | 144 size_t num_channels, |
| 140 rtc::Optional<std::string> bitrate_param) { | 145 rtc::Optional<std::string> bitrate_param) { |
| 141 const int default_bitrate = | 146 const int default_bitrate = |
| 142 CalculateDefaultBitrate(max_playback_rate_hz, num_channels); | 147 CalculateDefaultBitrate(max_playback_rate_hz, num_channels); |
| 143 | 148 |
| 144 if (bitrate_param) { | 149 if (bitrate_param) { |
| 145 const auto bitrate = rtc::StringToNumber<int>(*bitrate_param); | 150 const auto bitrate = rtc::StringToNumber<int>(*bitrate_param); |
| 146 if (bitrate) { | 151 if (bitrate) { |
| 147 const int chosen_bitrate = | 152 const int chosen_bitrate = |
| 148 std::max(AudioEncoderOpusConfig::kMinBitrateBps, | 153 std::max(kOpusMinBitrateBps, std::min(*bitrate, kOpusMaxBitrateBps)); |
| 149 std::min(*bitrate, AudioEncoderOpusConfig::kMaxBitrateBps)); | |
| 150 if (bitrate != chosen_bitrate) { | 154 if (bitrate != chosen_bitrate) { |
| 151 LOG(LS_WARNING) << "Invalid maxaveragebitrate " << *bitrate | 155 LOG(LS_WARNING) << "Invalid maxaveragebitrate " << *bitrate |
| 152 << " clamped to " << chosen_bitrate; | 156 << " clamped to " << chosen_bitrate; |
| 153 } | 157 } |
| 154 return chosen_bitrate; | 158 return chosen_bitrate; |
| 155 } | 159 } |
| 156 LOG(LS_WARNING) << "Invalid maxaveragebitrate \"" << *bitrate_param | 160 LOG(LS_WARNING) << "Invalid maxaveragebitrate \"" << *bitrate_param |
| 157 << "\" replaced by default bitrate " << default_bitrate; | 161 << "\" replaced by default bitrate " << default_bitrate; |
| 158 } | 162 } |
| 159 | 163 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 184 // kOpusSupportedFrameLengths. | 188 // kOpusSupportedFrameLengths. |
| 185 for (const int supported_frame_length : kOpusSupportedFrameLengths) { | 189 for (const int supported_frame_length : kOpusSupportedFrameLengths) { |
| 186 if (supported_frame_length >= *ptime) { | 190 if (supported_frame_length >= *ptime) { |
| 187 return supported_frame_length; | 191 return supported_frame_length; |
| 188 } | 192 } |
| 189 } | 193 } |
| 190 // If none was found, return the largest supported frame length. | 194 // If none was found, return the largest supported frame length. |
| 191 return *(std::end(kOpusSupportedFrameLengths) - 1); | 195 return *(std::end(kOpusSupportedFrameLengths) - 1); |
| 192 } | 196 } |
| 193 | 197 |
| 194 return AudioEncoderOpusConfig::kDefaultFrameSizeMs; | 198 return AudioEncoderOpus::Config::kDefaultFrameSizeMs; |
| 195 } | 199 } |
| 196 | 200 |
| 197 void FindSupportedFrameLengths(int min_frame_length_ms, | 201 void FindSupportedFrameLengths(int min_frame_length_ms, |
| 198 int max_frame_length_ms, | 202 int max_frame_length_ms, |
| 199 std::vector<int>* out) { | 203 std::vector<int>* out) { |
| 200 out->clear(); | 204 out->clear(); |
| 201 std::copy_if(std::begin(kANASupportedFrameLengths), | 205 std::copy_if(std::begin(kANASupportedFrameLengths), |
| 202 std::end(kANASupportedFrameLengths), std::back_inserter(*out), | 206 std::end(kANASupportedFrameLengths), std::back_inserter(*out), |
| 203 [&](int frame_length_ms) { | 207 [&](int frame_length_ms) { |
| 204 return frame_length_ms >= min_frame_length_ms && | 208 return frame_length_ms >= min_frame_length_ms && |
| 205 frame_length_ms <= max_frame_length_ms; | 209 frame_length_ms <= max_frame_length_ms; |
| 206 }); | 210 }); |
| 207 RTC_DCHECK(std::is_sorted(out->begin(), out->end())); | 211 RTC_DCHECK(std::is_sorted(out->begin(), out->end())); |
| 208 } | 212 } |
| 209 | 213 |
| 210 int GetBitrateBps(const AudioEncoderOpusConfig& config) { | |
| 211 RTC_DCHECK(config.IsOk()); | |
| 212 return config.bitrate_bps; | |
| 213 } | |
| 214 | |
| 215 } // namespace | 214 } // namespace |
| 216 | 215 |
| 217 rtc::Optional<AudioCodecInfo> AudioEncoderOpusImpl::QueryAudioEncoder( | 216 rtc::Optional<AudioCodecInfo> AudioEncoderOpus::QueryAudioEncoder( |
| 218 const SdpAudioFormat& format) { | 217 const SdpAudioFormat& format) { |
| 219 if (STR_CASE_CMP(format.name.c_str(), GetPayloadName()) == 0 && | 218 if (STR_CASE_CMP(format.name.c_str(), GetPayloadName()) == 0 && |
| 220 format.clockrate_hz == 48000 && format.num_channels == 2) { | 219 format.clockrate_hz == 48000 && format.num_channels == 2) { |
| 221 const size_t num_channels = GetChannelCount(format); | 220 const size_t num_channels = GetChannelCount(format); |
| 222 const int bitrate = | 221 const int bitrate = |
| 223 CalculateBitrate(GetMaxPlaybackRate(format), num_channels, | 222 CalculateBitrate(GetMaxPlaybackRate(format), num_channels, |
| 224 GetFormatParameter(format, "maxaveragebitrate")); | 223 GetFormatParameter(format, "maxaveragebitrate")); |
| 225 AudioCodecInfo info(48000, num_channels, bitrate, | 224 AudioCodecInfo info(48000, num_channels, bitrate, kOpusMinBitrateBps, |
| 226 AudioEncoderOpusConfig::kMinBitrateBps, | 225 kOpusMaxBitrateBps); |
| 227 AudioEncoderOpusConfig::kMaxBitrateBps); | |
| 228 info.allow_comfort_noise = false; | 226 info.allow_comfort_noise = false; |
| 229 info.supports_network_adaption = true; | 227 info.supports_network_adaption = true; |
| 230 | 228 |
| 231 return rtc::Optional<AudioCodecInfo>(info); | 229 return rtc::Optional<AudioCodecInfo>(info); |
| 232 } | 230 } |
| 233 return rtc::Optional<AudioCodecInfo>(); | 231 return rtc::Optional<AudioCodecInfo>(); |
| 234 } | 232 } |
| 235 | 233 |
| 236 AudioEncoderOpusConfig AudioEncoderOpusImpl::CreateConfig( | 234 AudioEncoderOpus::Config AudioEncoderOpus::CreateConfig( |
| 237 const CodecInst& codec_inst) { | 235 const CodecInst& codec_inst) { |
| 238 AudioEncoderOpusConfig config; | 236 AudioEncoderOpus::Config config; |
| 239 config.frame_size_ms = rtc::CheckedDivExact(codec_inst.pacsize, 48); | 237 config.frame_size_ms = rtc::CheckedDivExact(codec_inst.pacsize, 48); |
| 240 config.num_channels = codec_inst.channels; | 238 config.num_channels = codec_inst.channels; |
| 241 config.bitrate_bps = codec_inst.rate; | 239 config.bitrate_bps = rtc::Optional<int>(codec_inst.rate); |
| 242 config.application = config.num_channels == 1 | 240 config.payload_type = codec_inst.pltype; |
| 243 ? AudioEncoderOpusConfig::ApplicationMode::kVoip | 241 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip |
| 244 : AudioEncoderOpusConfig::ApplicationMode::kAudio; | 242 : AudioEncoderOpus::kAudio; |
| 245 config.supported_frame_lengths_ms.push_back(config.frame_size_ms); | 243 config.supported_frame_lengths_ms.push_back(config.frame_size_ms); |
| 244 #if WEBRTC_OPUS_VARIABLE_COMPLEXITY |
| 245 config.low_rate_complexity = 9; |
| 246 #endif |
| 246 return config; | 247 return config; |
| 247 } | 248 } |
| 248 | 249 |
| 249 rtc::Optional<AudioEncoderOpusConfig> AudioEncoderOpusImpl::SdpToConfig( | 250 AudioEncoderOpus::Config AudioEncoderOpus::CreateConfig( |
| 251 int payload_type, |
| 250 const SdpAudioFormat& format) { | 252 const SdpAudioFormat& format) { |
| 251 if (STR_CASE_CMP(format.name.c_str(), "opus") != 0 || | 253 AudioEncoderOpus::Config config; |
| 252 format.clockrate_hz != 48000 || format.num_channels != 2) { | |
| 253 return rtc::Optional<AudioEncoderOpusConfig>(); | |
| 254 } | |
| 255 | 254 |
| 256 AudioEncoderOpusConfig config; | |
| 257 config.num_channels = GetChannelCount(format); | 255 config.num_channels = GetChannelCount(format); |
| 258 config.frame_size_ms = GetFrameSizeMs(format); | 256 config.frame_size_ms = GetFrameSizeMs(format); |
| 259 config.max_playback_rate_hz = GetMaxPlaybackRate(format); | 257 config.max_playback_rate_hz = GetMaxPlaybackRate(format); |
| 260 config.fec_enabled = (GetFormatParameter(format, "useinbandfec") == "1"); | 258 config.fec_enabled = (GetFormatParameter(format, "useinbandfec") == "1"); |
| 261 config.dtx_enabled = (GetFormatParameter(format, "usedtx") == "1"); | 259 config.dtx_enabled = (GetFormatParameter(format, "usedtx") == "1"); |
| 262 config.cbr_enabled = (GetFormatParameter(format, "cbr") == "1"); | 260 config.cbr_enabled = (GetFormatParameter(format, "cbr") == "1"); |
| 263 config.bitrate_bps = | 261 config.bitrate_bps = rtc::Optional<int>( |
| 264 CalculateBitrate(config.max_playback_rate_hz, config.num_channels, | 262 CalculateBitrate(config.max_playback_rate_hz, config.num_channels, |
| 265 GetFormatParameter(format, "maxaveragebitrate")); | 263 GetFormatParameter(format, "maxaveragebitrate"))); |
| 266 config.application = config.num_channels == 1 | 264 config.payload_type = payload_type; |
| 267 ? AudioEncoderOpusConfig::ApplicationMode::kVoip | 265 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip |
| 268 : AudioEncoderOpusConfig::ApplicationMode::kAudio; | 266 : AudioEncoderOpus::kAudio; |
| 267 #if WEBRTC_OPUS_VARIABLE_COMPLEXITY |
| 268 config.low_rate_complexity = 9; |
| 269 #endif |
| 269 | 270 |
| 270 constexpr int kMinANAFrameLength = kANASupportedFrameLengths[0]; | 271 constexpr int kMinANAFrameLength = kANASupportedFrameLengths[0]; |
| 271 constexpr int kMaxANAFrameLength = | 272 constexpr int kMaxANAFrameLength = |
| 272 kANASupportedFrameLengths[arraysize(kANASupportedFrameLengths) - 1]; | 273 kANASupportedFrameLengths[arraysize(kANASupportedFrameLengths) - 1]; |
| 273 | |
| 274 // For now, minptime and maxptime are only used with ANA. If ptime is outside | 274 // For now, minptime and maxptime are only used with ANA. If ptime is outside |
| 275 // of this range, it will get adjusted once ANA takes hold. Ideally, we'd know | 275 // of this range, it will get adjusted once ANA takes hold. Ideally, we'd know |
| 276 // if ANA was to be used when setting up the config, and adjust accordingly. | 276 // if ANA was to be used when setting up the config, and adjust accordingly. |
| 277 const int min_frame_length_ms = | 277 const int min_frame_length_ms = |
| 278 GetFormatParameter<int>(format, "minptime").value_or(kMinANAFrameLength); | 278 GetFormatParameter<int>(format, "minptime").value_or(kMinANAFrameLength); |
| 279 const int max_frame_length_ms = | 279 const int max_frame_length_ms = |
| 280 GetFormatParameter<int>(format, "maxptime").value_or(kMaxANAFrameLength); | 280 GetFormatParameter<int>(format, "maxptime").value_or(kMaxANAFrameLength); |
| 281 | 281 |
| 282 FindSupportedFrameLengths(min_frame_length_ms, max_frame_length_ms, | 282 FindSupportedFrameLengths(min_frame_length_ms, max_frame_length_ms, |
| 283 &config.supported_frame_lengths_ms); | 283 &config.supported_frame_lengths_ms); |
| 284 RTC_DCHECK(config.IsOk()); | 284 return config; |
| 285 return rtc::Optional<AudioEncoderOpusConfig>(config); | |
| 286 } | 285 } |
| 287 | 286 |
| 288 rtc::Optional<int> AudioEncoderOpusImpl::GetNewComplexity( | 287 class AudioEncoderOpus::PacketLossFractionSmoother { |
| 289 const AudioEncoderOpusConfig& config) { | |
| 290 RTC_DCHECK(config.IsOk()); | |
| 291 const int bitrate_bps = GetBitrateBps(config); | |
| 292 if (bitrate_bps >= config.complexity_threshold_bps - | |
| 293 config.complexity_threshold_window_bps && | |
| 294 bitrate_bps <= config.complexity_threshold_bps + | |
| 295 config.complexity_threshold_window_bps) { | |
| 296 // Within the hysteresis window; make no change. | |
| 297 return rtc::Optional<int>(); | |
| 298 } else { | |
| 299 return rtc::Optional<int>(bitrate_bps <= config.complexity_threshold_bps | |
| 300 ? config.low_rate_complexity | |
| 301 : config.complexity); | |
| 302 } | |
| 303 } | |
| 304 | |
| 305 class AudioEncoderOpusImpl::PacketLossFractionSmoother { | |
| 306 public: | 288 public: |
| 307 explicit PacketLossFractionSmoother() | 289 explicit PacketLossFractionSmoother() |
| 308 : last_sample_time_ms_(rtc::TimeMillis()), | 290 : last_sample_time_ms_(rtc::TimeMillis()), |
| 309 smoother_(kAlphaForPacketLossFractionSmoother) {} | 291 smoother_(kAlphaForPacketLossFractionSmoother) {} |
| 310 | 292 |
| 311 // Gets the smoothed packet loss fraction. | 293 // Gets the smoothed packet loss fraction. |
| 312 float GetAverage() const { | 294 float GetAverage() const { |
| 313 float value = smoother_.filtered(); | 295 float value = smoother_.filtered(); |
| 314 return (value == rtc::ExpFilter::kValueUndefined) ? 0.0f : value; | 296 return (value == rtc::ExpFilter::kValueUndefined) ? 0.0f : value; |
| 315 } | 297 } |
| 316 | 298 |
| 317 // Add new observation to the packet loss fraction smoother. | 299 // Add new observation to the packet loss fraction smoother. |
| 318 void AddSample(float packet_loss_fraction) { | 300 void AddSample(float packet_loss_fraction) { |
| 319 int64_t now_ms = rtc::TimeMillis(); | 301 int64_t now_ms = rtc::TimeMillis(); |
| 320 smoother_.Apply(static_cast<float>(now_ms - last_sample_time_ms_), | 302 smoother_.Apply(static_cast<float>(now_ms - last_sample_time_ms_), |
| 321 packet_loss_fraction); | 303 packet_loss_fraction); |
| 322 last_sample_time_ms_ = now_ms; | 304 last_sample_time_ms_ = now_ms; |
| 323 } | 305 } |
| 324 | 306 |
| 325 private: | 307 private: |
| 326 int64_t last_sample_time_ms_; | 308 int64_t last_sample_time_ms_; |
| 327 | 309 |
| 328 // An exponential filter is used to smooth the packet loss fraction. | 310 // An exponential filter is used to smooth the packet loss fraction. |
| 329 rtc::ExpFilter smoother_; | 311 rtc::ExpFilter smoother_; |
| 330 }; | 312 }; |
| 331 | 313 |
| 332 AudioEncoderOpusImpl::AudioEncoderOpusImpl( | 314 AudioEncoderOpus::Config::Config() { |
| 333 const AudioEncoderOpusConfig& config, | 315 #if WEBRTC_OPUS_VARIABLE_COMPLEXITY |
| 334 int payload_type, | 316 low_rate_complexity = 9; |
| 317 #endif |
| 318 } |
| 319 AudioEncoderOpus::Config::Config(const Config&) = default; |
| 320 AudioEncoderOpus::Config::~Config() = default; |
| 321 auto AudioEncoderOpus::Config::operator=(const Config&) -> Config& = default; |
| 322 |
| 323 bool AudioEncoderOpus::Config::IsOk() const { |
| 324 if (frame_size_ms <= 0 || frame_size_ms % 10 != 0) |
| 325 return false; |
| 326 if (num_channels != 1 && num_channels != 2) |
| 327 return false; |
| 328 if (bitrate_bps && |
| 329 (*bitrate_bps < kOpusMinBitrateBps || *bitrate_bps > kOpusMaxBitrateBps)) |
| 330 return false; |
| 331 if (complexity < 0 || complexity > 10) |
| 332 return false; |
| 333 if (low_rate_complexity < 0 || low_rate_complexity > 10) |
| 334 return false; |
| 335 return true; |
| 336 } |
| 337 |
| 338 int AudioEncoderOpus::Config::GetBitrateBps() const { |
| 339 RTC_DCHECK(IsOk()); |
| 340 if (bitrate_bps) |
| 341 return *bitrate_bps; // Explicitly set value. |
| 342 else |
| 343 return num_channels == 1 ? 32000 : 64000; // Default value. |
| 344 } |
| 345 |
| 346 rtc::Optional<int> AudioEncoderOpus::Config::GetNewComplexity() const { |
| 347 RTC_DCHECK(IsOk()); |
| 348 const int bitrate_bps = GetBitrateBps(); |
| 349 if (bitrate_bps >= |
| 350 complexity_threshold_bps - complexity_threshold_window_bps && |
| 351 bitrate_bps <= |
| 352 complexity_threshold_bps + complexity_threshold_window_bps) { |
| 353 // Within the hysteresis window; make no change. |
| 354 return rtc::Optional<int>(); |
| 355 } |
| 356 return bitrate_bps <= complexity_threshold_bps |
| 357 ? rtc::Optional<int>(low_rate_complexity) |
| 358 : rtc::Optional<int>(complexity); |
| 359 } |
| 360 |
| 361 AudioEncoderOpus::AudioEncoderOpus( |
| 362 const Config& config, |
| 335 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator, | 363 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator, |
| 336 std::unique_ptr<SmoothingFilter> bitrate_smoother) | 364 std::unique_ptr<SmoothingFilter> bitrate_smoother) |
| 337 : payload_type_(payload_type), | 365 : send_side_bwe_with_overhead_(webrtc::field_trial::IsEnabled( |
| 338 send_side_bwe_with_overhead_(webrtc::field_trial::IsEnabled( | |
| 339 "WebRTC-SendSideBwe-WithOverhead")), | 366 "WebRTC-SendSideBwe-WithOverhead")), |
| 340 packet_loss_rate_(0.0), | 367 packet_loss_rate_(0.0), |
| 341 inst_(nullptr), | 368 inst_(nullptr), |
| 342 packet_loss_fraction_smoother_(new PacketLossFractionSmoother()), | 369 packet_loss_fraction_smoother_(new PacketLossFractionSmoother()), |
| 343 audio_network_adaptor_creator_( | 370 audio_network_adaptor_creator_( |
| 344 audio_network_adaptor_creator | 371 audio_network_adaptor_creator |
| 345 ? std::move(audio_network_adaptor_creator) | 372 ? std::move(audio_network_adaptor_creator) |
| 346 : [this](const ProtoString& config_string, | 373 : [this](const ProtoString& config_string, |
| 347 RtcEventLog* event_log) { | 374 RtcEventLog* event_log) { |
| 348 return DefaultAudioNetworkAdaptorCreator(config_string, | 375 return DefaultAudioNetworkAdaptorCreator(config_string, |
| 349 event_log); | 376 event_log); |
| 350 }), | 377 }), |
| 351 bitrate_smoother_(bitrate_smoother | 378 bitrate_smoother_(bitrate_smoother |
| 352 ? std::move(bitrate_smoother) : std::unique_ptr<SmoothingFilter>( | 379 ? std::move(bitrate_smoother) : std::unique_ptr<SmoothingFilter>( |
| 353 // We choose 5sec as initial time constant due to empirical data. | 380 // We choose 5sec as initial time constant due to empirical data. |
| 354 new SmoothingFilterImpl(5000))) { | 381 new SmoothingFilterImpl(5000))) { |
| 355 RTC_CHECK(RecreateEncoderInstance(config)); | 382 RTC_CHECK(RecreateEncoderInstance(config)); |
| 356 } | 383 } |
| 357 | 384 |
| 358 AudioEncoderOpusImpl::AudioEncoderOpusImpl(const CodecInst& codec_inst) | 385 AudioEncoderOpus::AudioEncoderOpus(const CodecInst& codec_inst) |
| 359 : AudioEncoderOpusImpl(CreateConfig(codec_inst), codec_inst.pltype) {} | 386 : AudioEncoderOpus(CreateConfig(codec_inst), nullptr) {} |
| 360 | 387 |
| 361 AudioEncoderOpusImpl::AudioEncoderOpusImpl(int payload_type, | 388 AudioEncoderOpus::AudioEncoderOpus(int payload_type, |
| 362 const SdpAudioFormat& format) | 389 const SdpAudioFormat& format) |
| 363 : AudioEncoderOpusImpl(*SdpToConfig(format), payload_type) {} | 390 : AudioEncoderOpus(CreateConfig(payload_type, format), nullptr) {} |
| 364 | 391 |
| 365 AudioEncoderOpusImpl::~AudioEncoderOpusImpl() { | 392 AudioEncoderOpus::~AudioEncoderOpus() { |
| 366 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); | 393 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); |
| 367 } | 394 } |
| 368 | 395 |
| 369 int AudioEncoderOpusImpl::SampleRateHz() const { | 396 int AudioEncoderOpus::SampleRateHz() const { |
| 370 return kSampleRateHz; | 397 return kSampleRateHz; |
| 371 } | 398 } |
| 372 | 399 |
| 373 size_t AudioEncoderOpusImpl::NumChannels() const { | 400 size_t AudioEncoderOpus::NumChannels() const { |
| 374 return config_.num_channels; | 401 return config_.num_channels; |
| 375 } | 402 } |
| 376 | 403 |
| 377 size_t AudioEncoderOpusImpl::Num10MsFramesInNextPacket() const { | 404 size_t AudioEncoderOpus::Num10MsFramesInNextPacket() const { |
| 378 return Num10msFramesPerPacket(); | 405 return Num10msFramesPerPacket(); |
| 379 } | 406 } |
| 380 | 407 |
| 381 size_t AudioEncoderOpusImpl::Max10MsFramesInAPacket() const { | 408 size_t AudioEncoderOpus::Max10MsFramesInAPacket() const { |
| 382 return Num10msFramesPerPacket(); | 409 return Num10msFramesPerPacket(); |
| 383 } | 410 } |
| 384 | 411 |
| 385 int AudioEncoderOpusImpl::GetTargetBitrate() const { | 412 int AudioEncoderOpus::GetTargetBitrate() const { |
| 386 return GetBitrateBps(config_); | 413 return config_.GetBitrateBps(); |
| 387 } | 414 } |
| 388 | 415 |
| 389 void AudioEncoderOpusImpl::Reset() { | 416 void AudioEncoderOpus::Reset() { |
| 390 RTC_CHECK(RecreateEncoderInstance(config_)); | 417 RTC_CHECK(RecreateEncoderInstance(config_)); |
| 391 } | 418 } |
| 392 | 419 |
| 393 bool AudioEncoderOpusImpl::SetFec(bool enable) { | 420 bool AudioEncoderOpus::SetFec(bool enable) { |
| 394 if (enable) { | 421 if (enable) { |
| 395 RTC_CHECK_EQ(0, WebRtcOpus_EnableFec(inst_)); | 422 RTC_CHECK_EQ(0, WebRtcOpus_EnableFec(inst_)); |
| 396 } else { | 423 } else { |
| 397 RTC_CHECK_EQ(0, WebRtcOpus_DisableFec(inst_)); | 424 RTC_CHECK_EQ(0, WebRtcOpus_DisableFec(inst_)); |
| 398 } | 425 } |
| 399 config_.fec_enabled = enable; | 426 config_.fec_enabled = enable; |
| 400 return true; | 427 return true; |
| 401 } | 428 } |
| 402 | 429 |
| 403 bool AudioEncoderOpusImpl::SetDtx(bool enable) { | 430 bool AudioEncoderOpus::SetDtx(bool enable) { |
| 404 if (enable) { | 431 if (enable) { |
| 405 RTC_CHECK_EQ(0, WebRtcOpus_EnableDtx(inst_)); | 432 RTC_CHECK_EQ(0, WebRtcOpus_EnableDtx(inst_)); |
| 406 } else { | 433 } else { |
| 407 RTC_CHECK_EQ(0, WebRtcOpus_DisableDtx(inst_)); | 434 RTC_CHECK_EQ(0, WebRtcOpus_DisableDtx(inst_)); |
| 408 } | 435 } |
| 409 config_.dtx_enabled = enable; | 436 config_.dtx_enabled = enable; |
| 410 return true; | 437 return true; |
| 411 } | 438 } |
| 412 | 439 |
| 413 bool AudioEncoderOpusImpl::GetDtx() const { | 440 bool AudioEncoderOpus::GetDtx() const { |
| 414 return config_.dtx_enabled; | 441 return config_.dtx_enabled; |
| 415 } | 442 } |
| 416 | 443 |
| 417 bool AudioEncoderOpusImpl::SetApplication(Application application) { | 444 bool AudioEncoderOpus::SetApplication(Application application) { |
| 418 auto conf = config_; | 445 auto conf = config_; |
| 419 switch (application) { | 446 switch (application) { |
| 420 case Application::kSpeech: | 447 case Application::kSpeech: |
| 421 conf.application = AudioEncoderOpusConfig::ApplicationMode::kVoip; | 448 conf.application = AudioEncoderOpus::kVoip; |
| 422 break; | 449 break; |
| 423 case Application::kAudio: | 450 case Application::kAudio: |
| 424 conf.application = AudioEncoderOpusConfig::ApplicationMode::kAudio; | 451 conf.application = AudioEncoderOpus::kAudio; |
| 425 break; | 452 break; |
| 426 } | 453 } |
| 427 return RecreateEncoderInstance(conf); | 454 return RecreateEncoderInstance(conf); |
| 428 } | 455 } |
| 429 | 456 |
| 430 void AudioEncoderOpusImpl::SetMaxPlaybackRate(int frequency_hz) { | 457 void AudioEncoderOpus::SetMaxPlaybackRate(int frequency_hz) { |
| 431 auto conf = config_; | 458 auto conf = config_; |
| 432 conf.max_playback_rate_hz = frequency_hz; | 459 conf.max_playback_rate_hz = frequency_hz; |
| 433 RTC_CHECK(RecreateEncoderInstance(conf)); | 460 RTC_CHECK(RecreateEncoderInstance(conf)); |
| 434 } | 461 } |
| 435 | 462 |
| 436 bool AudioEncoderOpusImpl::EnableAudioNetworkAdaptor( | 463 bool AudioEncoderOpus::EnableAudioNetworkAdaptor( |
| 437 const std::string& config_string, | 464 const std::string& config_string, |
| 438 RtcEventLog* event_log) { | 465 RtcEventLog* event_log) { |
| 439 audio_network_adaptor_ = | 466 audio_network_adaptor_ = |
| 440 audio_network_adaptor_creator_(config_string, event_log); | 467 audio_network_adaptor_creator_(config_string, event_log); |
| 441 return audio_network_adaptor_.get() != nullptr; | 468 return audio_network_adaptor_.get() != nullptr; |
| 442 } | 469 } |
| 443 | 470 |
| 444 void AudioEncoderOpusImpl::DisableAudioNetworkAdaptor() { | 471 void AudioEncoderOpus::DisableAudioNetworkAdaptor() { |
| 445 audio_network_adaptor_.reset(nullptr); | 472 audio_network_adaptor_.reset(nullptr); |
| 446 } | 473 } |
| 447 | 474 |
| 448 void AudioEncoderOpusImpl::OnReceivedUplinkPacketLossFraction( | 475 void AudioEncoderOpus::OnReceivedUplinkPacketLossFraction( |
| 449 float uplink_packet_loss_fraction) { | 476 float uplink_packet_loss_fraction) { |
| 450 if (!audio_network_adaptor_) { | 477 if (!audio_network_adaptor_) { |
| 451 packet_loss_fraction_smoother_->AddSample(uplink_packet_loss_fraction); | 478 packet_loss_fraction_smoother_->AddSample(uplink_packet_loss_fraction); |
| 452 float average_fraction_loss = packet_loss_fraction_smoother_->GetAverage(); | 479 float average_fraction_loss = packet_loss_fraction_smoother_->GetAverage(); |
| 453 return SetProjectedPacketLossRate(average_fraction_loss); | 480 return SetProjectedPacketLossRate(average_fraction_loss); |
| 454 } | 481 } |
| 455 audio_network_adaptor_->SetUplinkPacketLossFraction( | 482 audio_network_adaptor_->SetUplinkPacketLossFraction( |
| 456 uplink_packet_loss_fraction); | 483 uplink_packet_loss_fraction); |
| 457 ApplyAudioNetworkAdaptor(); | 484 ApplyAudioNetworkAdaptor(); |
| 458 } | 485 } |
| 459 | 486 |
| 460 void AudioEncoderOpusImpl::OnReceivedUplinkRecoverablePacketLossFraction( | 487 void AudioEncoderOpus::OnReceivedUplinkRecoverablePacketLossFraction( |
| 461 float uplink_recoverable_packet_loss_fraction) { | 488 float uplink_recoverable_packet_loss_fraction) { |
| 462 if (!audio_network_adaptor_) | 489 if (!audio_network_adaptor_) |
| 463 return; | 490 return; |
| 464 audio_network_adaptor_->SetUplinkRecoverablePacketLossFraction( | 491 audio_network_adaptor_->SetUplinkRecoverablePacketLossFraction( |
| 465 uplink_recoverable_packet_loss_fraction); | 492 uplink_recoverable_packet_loss_fraction); |
| 466 ApplyAudioNetworkAdaptor(); | 493 ApplyAudioNetworkAdaptor(); |
| 467 } | 494 } |
| 468 | 495 |
| 469 void AudioEncoderOpusImpl::OnReceivedUplinkBandwidth( | 496 void AudioEncoderOpus::OnReceivedUplinkBandwidth( |
| 470 int target_audio_bitrate_bps, | 497 int target_audio_bitrate_bps, |
| 471 rtc::Optional<int64_t> probing_interval_ms) { | 498 rtc::Optional<int64_t> probing_interval_ms) { |
| 472 if (audio_network_adaptor_) { | 499 if (audio_network_adaptor_) { |
| 473 audio_network_adaptor_->SetTargetAudioBitrate(target_audio_bitrate_bps); | 500 audio_network_adaptor_->SetTargetAudioBitrate(target_audio_bitrate_bps); |
| 474 // We give smoothed bitrate allocation to audio network adaptor as | 501 // We give smoothed bitrate allocation to audio network adaptor as |
| 475 // the uplink bandwidth. | 502 // the uplink bandwidth. |
| 476 // The probing spikes should not affect the bitrate smoother more than 25%. | 503 // The probing spikes should not affect the bitrate smoother more than 25%. |
| 477 // To simplify the calculations we use a step response as input signal. | 504 // To simplify the calculations we use a step response as input signal. |
| 478 // The step response of an exponential filter is | 505 // The step response of an exponential filter is |
| 479 // u(t) = 1 - e^(-t / time_constant). | 506 // u(t) = 1 - e^(-t / time_constant). |
| 480 // In order to limit the affect of a BWE spike within 25% of its value | 507 // In order to limit the affect of a BWE spike within 25% of its value |
| 481 // before | 508 // before |
| 482 // the next probing, we would choose a time constant that fulfills | 509 // the next probing, we would choose a time constant that fulfills |
| 483 // 1 - e^(-probing_interval_ms / time_constant) < 0.25 | 510 // 1 - e^(-probing_interval_ms / time_constant) < 0.25 |
| 484 // Then 4 * probing_interval_ms is a good choice. | 511 // Then 4 * probing_interval_ms is a good choice. |
| 485 if (probing_interval_ms) | 512 if (probing_interval_ms) |
| 486 bitrate_smoother_->SetTimeConstantMs(*probing_interval_ms * 4); | 513 bitrate_smoother_->SetTimeConstantMs(*probing_interval_ms * 4); |
| 487 bitrate_smoother_->AddSample(target_audio_bitrate_bps); | 514 bitrate_smoother_->AddSample(target_audio_bitrate_bps); |
| 488 | 515 |
| 489 ApplyAudioNetworkAdaptor(); | 516 ApplyAudioNetworkAdaptor(); |
| 490 } else if (send_side_bwe_with_overhead_) { | 517 } else if (send_side_bwe_with_overhead_) { |
| 491 if (!overhead_bytes_per_packet_) { | 518 if (!overhead_bytes_per_packet_) { |
| 492 LOG(LS_INFO) | 519 LOG(LS_INFO) |
| 493 << "AudioEncoderOpusImpl: Overhead unknown, target audio bitrate " | 520 << "AudioEncoderOpus: Overhead unknown, target audio bitrate " |
| 494 << target_audio_bitrate_bps << " bps is ignored."; | 521 << target_audio_bitrate_bps << " bps is ignored."; |
| 495 return; | 522 return; |
| 496 } | 523 } |
| 497 const int overhead_bps = static_cast<int>( | 524 const int overhead_bps = static_cast<int>( |
| 498 *overhead_bytes_per_packet_ * 8 * 100 / Num10MsFramesInNextPacket()); | 525 *overhead_bytes_per_packet_ * 8 * 100 / Num10MsFramesInNextPacket()); |
| 499 SetTargetBitrate( | 526 SetTargetBitrate(std::min( |
| 500 std::min(AudioEncoderOpusConfig::kMaxBitrateBps, | 527 kOpusMaxBitrateBps, |
| 501 std::max(AudioEncoderOpusConfig::kMinBitrateBps, | 528 std::max(kOpusMinBitrateBps, target_audio_bitrate_bps - overhead_bps))); |
| 502 target_audio_bitrate_bps - overhead_bps))); | |
| 503 } else { | 529 } else { |
| 504 SetTargetBitrate(target_audio_bitrate_bps); | 530 SetTargetBitrate(target_audio_bitrate_bps); |
| 505 } | 531 } |
| 506 } | 532 } |
| 507 | 533 |
| 508 void AudioEncoderOpusImpl::OnReceivedRtt(int rtt_ms) { | 534 void AudioEncoderOpus::OnReceivedRtt(int rtt_ms) { |
| 509 if (!audio_network_adaptor_) | 535 if (!audio_network_adaptor_) |
| 510 return; | 536 return; |
| 511 audio_network_adaptor_->SetRtt(rtt_ms); | 537 audio_network_adaptor_->SetRtt(rtt_ms); |
| 512 ApplyAudioNetworkAdaptor(); | 538 ApplyAudioNetworkAdaptor(); |
| 513 } | 539 } |
| 514 | 540 |
| 515 void AudioEncoderOpusImpl::OnReceivedOverhead( | 541 void AudioEncoderOpus::OnReceivedOverhead(size_t overhead_bytes_per_packet) { |
| 516 size_t overhead_bytes_per_packet) { | |
| 517 if (audio_network_adaptor_) { | 542 if (audio_network_adaptor_) { |
| 518 audio_network_adaptor_->SetOverhead(overhead_bytes_per_packet); | 543 audio_network_adaptor_->SetOverhead(overhead_bytes_per_packet); |
| 519 ApplyAudioNetworkAdaptor(); | 544 ApplyAudioNetworkAdaptor(); |
| 520 } else { | 545 } else { |
| 521 overhead_bytes_per_packet_ = | 546 overhead_bytes_per_packet_ = |
| 522 rtc::Optional<size_t>(overhead_bytes_per_packet); | 547 rtc::Optional<size_t>(overhead_bytes_per_packet); |
| 523 } | 548 } |
| 524 } | 549 } |
| 525 | 550 |
| 526 void AudioEncoderOpusImpl::SetReceiverFrameLengthRange( | 551 void AudioEncoderOpus::SetReceiverFrameLengthRange(int min_frame_length_ms, |
| 527 int min_frame_length_ms, | 552 int max_frame_length_ms) { |
| 528 int max_frame_length_ms) { | |
| 529 // Ensure that |SetReceiverFrameLengthRange| is called before | 553 // Ensure that |SetReceiverFrameLengthRange| is called before |
| 530 // |EnableAudioNetworkAdaptor|, otherwise we need to recreate | 554 // |EnableAudioNetworkAdaptor|, otherwise we need to recreate |
| 531 // |audio_network_adaptor_|, which is not a needed use case. | 555 // |audio_network_adaptor_|, which is not a needed use case. |
| 532 RTC_DCHECK(!audio_network_adaptor_); | 556 RTC_DCHECK(!audio_network_adaptor_); |
| 533 FindSupportedFrameLengths(min_frame_length_ms, max_frame_length_ms, | 557 FindSupportedFrameLengths(min_frame_length_ms, max_frame_length_ms, |
| 534 &config_.supported_frame_lengths_ms); | 558 &config_.supported_frame_lengths_ms); |
| 535 } | 559 } |
| 536 | 560 |
| 537 AudioEncoder::EncodedInfo AudioEncoderOpusImpl::EncodeImpl( | 561 AudioEncoder::EncodedInfo AudioEncoderOpus::EncodeImpl( |
| 538 uint32_t rtp_timestamp, | 562 uint32_t rtp_timestamp, |
| 539 rtc::ArrayView<const int16_t> audio, | 563 rtc::ArrayView<const int16_t> audio, |
| 540 rtc::Buffer* encoded) { | 564 rtc::Buffer* encoded) { |
| 541 MaybeUpdateUplinkBandwidth(); | 565 MaybeUpdateUplinkBandwidth(); |
| 542 | 566 |
| 543 if (input_buffer_.empty()) | 567 if (input_buffer_.empty()) |
| 544 first_timestamp_in_buffer_ = rtp_timestamp; | 568 first_timestamp_in_buffer_ = rtp_timestamp; |
| 545 | 569 |
| 546 input_buffer_.insert(input_buffer_.end(), audio.cbegin(), audio.cend()); | 570 input_buffer_.insert(input_buffer_.end(), audio.cbegin(), audio.cend()); |
| 547 if (input_buffer_.size() < | 571 if (input_buffer_.size() < |
| (...skipping 18 matching lines...) Expand all Loading... |
| 566 RTC_CHECK_GE(status, 0); // Fails only if fed invalid data. | 590 RTC_CHECK_GE(status, 0); // Fails only if fed invalid data. |
| 567 | 591 |
| 568 return static_cast<size_t>(status); | 592 return static_cast<size_t>(status); |
| 569 }); | 593 }); |
| 570 input_buffer_.clear(); | 594 input_buffer_.clear(); |
| 571 | 595 |
| 572 // Will use new packet size for next encoding. | 596 // Will use new packet size for next encoding. |
| 573 config_.frame_size_ms = next_frame_length_ms_; | 597 config_.frame_size_ms = next_frame_length_ms_; |
| 574 | 598 |
| 575 info.encoded_timestamp = first_timestamp_in_buffer_; | 599 info.encoded_timestamp = first_timestamp_in_buffer_; |
| 576 info.payload_type = payload_type_; | 600 info.payload_type = config_.payload_type; |
| 577 info.send_even_if_empty = true; // Allows Opus to send empty packets. | 601 info.send_even_if_empty = true; // Allows Opus to send empty packets. |
| 578 info.speech = (info.encoded_bytes > 0); | 602 info.speech = (info.encoded_bytes > 0); |
| 579 info.encoder_type = CodecType::kOpus; | 603 info.encoder_type = CodecType::kOpus; |
| 580 return info; | 604 return info; |
| 581 } | 605 } |
| 582 | 606 |
| 583 size_t AudioEncoderOpusImpl::Num10msFramesPerPacket() const { | 607 size_t AudioEncoderOpus::Num10msFramesPerPacket() const { |
| 584 return static_cast<size_t>(rtc::CheckedDivExact(config_.frame_size_ms, 10)); | 608 return static_cast<size_t>(rtc::CheckedDivExact(config_.frame_size_ms, 10)); |
| 585 } | 609 } |
| 586 | 610 |
| 587 size_t AudioEncoderOpusImpl::SamplesPer10msFrame() const { | 611 size_t AudioEncoderOpus::SamplesPer10msFrame() const { |
| 588 return rtc::CheckedDivExact(kSampleRateHz, 100) * config_.num_channels; | 612 return rtc::CheckedDivExact(kSampleRateHz, 100) * config_.num_channels; |
| 589 } | 613 } |
| 590 | 614 |
| 591 size_t AudioEncoderOpusImpl::SufficientOutputBufferSize() const { | 615 size_t AudioEncoderOpus::SufficientOutputBufferSize() const { |
| 592 // Calculate the number of bytes we expect the encoder to produce, | 616 // Calculate the number of bytes we expect the encoder to produce, |
| 593 // then multiply by two to give a wide margin for error. | 617 // then multiply by two to give a wide margin for error. |
| 594 const size_t bytes_per_millisecond = | 618 const size_t bytes_per_millisecond = |
| 595 static_cast<size_t>(GetBitrateBps(config_) / (1000 * 8) + 1); | 619 static_cast<size_t>(config_.GetBitrateBps() / (1000 * 8) + 1); |
| 596 const size_t approx_encoded_bytes = | 620 const size_t approx_encoded_bytes = |
| 597 Num10msFramesPerPacket() * 10 * bytes_per_millisecond; | 621 Num10msFramesPerPacket() * 10 * bytes_per_millisecond; |
| 598 return 2 * approx_encoded_bytes; | 622 return 2 * approx_encoded_bytes; |
| 599 } | 623 } |
| 600 | 624 |
| 601 // If the given config is OK, recreate the Opus encoder instance with those | 625 // If the given config is OK, recreate the Opus encoder instance with those |
| 602 // settings, save the config, and return true. Otherwise, do nothing and return | 626 // settings, save the config, and return true. Otherwise, do nothing and return |
| 603 // false. | 627 // false. |
| 604 bool AudioEncoderOpusImpl::RecreateEncoderInstance( | 628 bool AudioEncoderOpus::RecreateEncoderInstance(const Config& config) { |
| 605 const AudioEncoderOpusConfig& config) { | |
| 606 if (!config.IsOk()) | 629 if (!config.IsOk()) |
| 607 return false; | 630 return false; |
| 608 config_ = config; | 631 config_ = config; |
| 609 if (inst_) | 632 if (inst_) |
| 610 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); | 633 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); |
| 611 input_buffer_.clear(); | 634 input_buffer_.clear(); |
| 612 input_buffer_.reserve(Num10msFramesPerPacket() * SamplesPer10msFrame()); | 635 input_buffer_.reserve(Num10msFramesPerPacket() * SamplesPer10msFrame()); |
| 613 RTC_CHECK_EQ(0, WebRtcOpus_EncoderCreate( | 636 RTC_CHECK_EQ(0, WebRtcOpus_EncoderCreate(&inst_, config.num_channels, |
| 614 &inst_, config.num_channels, | 637 config.application)); |
| 615 config.application == | 638 RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, config.GetBitrateBps())); |
| 616 AudioEncoderOpusConfig::ApplicationMode::kVoip | |
| 617 ? 0 | |
| 618 : 1)); | |
| 619 RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, GetBitrateBps(config))); | |
| 620 if (config.fec_enabled) { | 639 if (config.fec_enabled) { |
| 621 RTC_CHECK_EQ(0, WebRtcOpus_EnableFec(inst_)); | 640 RTC_CHECK_EQ(0, WebRtcOpus_EnableFec(inst_)); |
| 622 } else { | 641 } else { |
| 623 RTC_CHECK_EQ(0, WebRtcOpus_DisableFec(inst_)); | 642 RTC_CHECK_EQ(0, WebRtcOpus_DisableFec(inst_)); |
| 624 } | 643 } |
| 625 RTC_CHECK_EQ( | 644 RTC_CHECK_EQ( |
| 626 0, WebRtcOpus_SetMaxPlaybackRate(inst_, config.max_playback_rate_hz)); | 645 0, WebRtcOpus_SetMaxPlaybackRate(inst_, config.max_playback_rate_hz)); |
| 627 // Use the default complexity if the start bitrate is within the hysteresis | 646 // Use the default complexity if the start bitrate is within the hysteresis |
| 628 // window. | 647 // window. |
| 629 complexity_ = GetNewComplexity(config).value_or(config.complexity); | 648 complexity_ = config.GetNewComplexity().value_or(config.complexity); |
| 630 RTC_CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, complexity_)); | 649 RTC_CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, complexity_)); |
| 631 if (config.dtx_enabled) { | 650 if (config.dtx_enabled) { |
| 632 RTC_CHECK_EQ(0, WebRtcOpus_EnableDtx(inst_)); | 651 RTC_CHECK_EQ(0, WebRtcOpus_EnableDtx(inst_)); |
| 633 } else { | 652 } else { |
| 634 RTC_CHECK_EQ(0, WebRtcOpus_DisableDtx(inst_)); | 653 RTC_CHECK_EQ(0, WebRtcOpus_DisableDtx(inst_)); |
| 635 } | 654 } |
| 636 RTC_CHECK_EQ(0, | 655 RTC_CHECK_EQ(0, |
| 637 WebRtcOpus_SetPacketLossRate( | 656 WebRtcOpus_SetPacketLossRate( |
| 638 inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5))); | 657 inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5))); |
| 639 if (config.cbr_enabled) { | 658 if (config.cbr_enabled) { |
| 640 RTC_CHECK_EQ(0, WebRtcOpus_EnableCbr(inst_)); | 659 RTC_CHECK_EQ(0, WebRtcOpus_EnableCbr(inst_)); |
| 641 } else { | 660 } else { |
| 642 RTC_CHECK_EQ(0, WebRtcOpus_DisableCbr(inst_)); | 661 RTC_CHECK_EQ(0, WebRtcOpus_DisableCbr(inst_)); |
| 643 } | 662 } |
| 644 num_channels_to_encode_ = NumChannels(); | 663 num_channels_to_encode_ = NumChannels(); |
| 645 next_frame_length_ms_ = config_.frame_size_ms; | 664 next_frame_length_ms_ = config_.frame_size_ms; |
| 646 return true; | 665 return true; |
| 647 } | 666 } |
| 648 | 667 |
| 649 void AudioEncoderOpusImpl::SetFrameLength(int frame_length_ms) { | 668 void AudioEncoderOpus::SetFrameLength(int frame_length_ms) { |
| 650 next_frame_length_ms_ = frame_length_ms; | 669 next_frame_length_ms_ = frame_length_ms; |
| 651 } | 670 } |
| 652 | 671 |
| 653 void AudioEncoderOpusImpl::SetNumChannelsToEncode( | 672 void AudioEncoderOpus::SetNumChannelsToEncode(size_t num_channels_to_encode) { |
| 654 size_t num_channels_to_encode) { | |
| 655 RTC_DCHECK_GT(num_channels_to_encode, 0); | 673 RTC_DCHECK_GT(num_channels_to_encode, 0); |
| 656 RTC_DCHECK_LE(num_channels_to_encode, config_.num_channels); | 674 RTC_DCHECK_LE(num_channels_to_encode, config_.num_channels); |
| 657 | 675 |
| 658 if (num_channels_to_encode_ == num_channels_to_encode) | 676 if (num_channels_to_encode_ == num_channels_to_encode) |
| 659 return; | 677 return; |
| 660 | 678 |
| 661 RTC_CHECK_EQ(0, WebRtcOpus_SetForceChannels(inst_, num_channels_to_encode)); | 679 RTC_CHECK_EQ(0, WebRtcOpus_SetForceChannels(inst_, num_channels_to_encode)); |
| 662 num_channels_to_encode_ = num_channels_to_encode; | 680 num_channels_to_encode_ = num_channels_to_encode; |
| 663 } | 681 } |
| 664 | 682 |
| 665 void AudioEncoderOpusImpl::SetProjectedPacketLossRate(float fraction) { | 683 void AudioEncoderOpus::SetProjectedPacketLossRate(float fraction) { |
| 666 float opt_loss_rate = OptimizePacketLossRate(fraction, packet_loss_rate_); | 684 float opt_loss_rate = OptimizePacketLossRate(fraction, packet_loss_rate_); |
| 667 if (packet_loss_rate_ != opt_loss_rate) { | 685 if (packet_loss_rate_ != opt_loss_rate) { |
| 668 packet_loss_rate_ = opt_loss_rate; | 686 packet_loss_rate_ = opt_loss_rate; |
| 669 RTC_CHECK_EQ( | 687 RTC_CHECK_EQ( |
| 670 0, WebRtcOpus_SetPacketLossRate( | 688 0, WebRtcOpus_SetPacketLossRate( |
| 671 inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5))); | 689 inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5))); |
| 672 } | 690 } |
| 673 } | 691 } |
| 674 | 692 |
| 675 void AudioEncoderOpusImpl::SetTargetBitrate(int bits_per_second) { | 693 void AudioEncoderOpus::SetTargetBitrate(int bits_per_second) { |
| 676 config_.bitrate_bps = rtc::SafeClamp<int>( | 694 config_.bitrate_bps = rtc::Optional<int>(rtc::SafeClamp<int>( |
| 677 bits_per_second, AudioEncoderOpusConfig::kMinBitrateBps, | 695 bits_per_second, kOpusMinBitrateBps, kOpusMaxBitrateBps)); |
| 678 AudioEncoderOpusConfig::kMaxBitrateBps); | |
| 679 RTC_DCHECK(config_.IsOk()); | 696 RTC_DCHECK(config_.IsOk()); |
| 680 RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, GetBitrateBps(config_))); | 697 RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, config_.GetBitrateBps())); |
| 681 const auto new_complexity = GetNewComplexity(config_); | 698 const auto new_complexity = config_.GetNewComplexity(); |
| 682 if (new_complexity && complexity_ != *new_complexity) { | 699 if (new_complexity && complexity_ != *new_complexity) { |
| 683 complexity_ = *new_complexity; | 700 complexity_ = *new_complexity; |
| 684 RTC_CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, complexity_)); | 701 RTC_CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, complexity_)); |
| 685 } | 702 } |
| 686 } | 703 } |
| 687 | 704 |
| 688 void AudioEncoderOpusImpl::ApplyAudioNetworkAdaptor() { | 705 void AudioEncoderOpus::ApplyAudioNetworkAdaptor() { |
| 689 auto config = audio_network_adaptor_->GetEncoderRuntimeConfig(); | 706 auto config = audio_network_adaptor_->GetEncoderRuntimeConfig(); |
| 690 RTC_DCHECK(!config.frame_length_ms || *config.frame_length_ms == 20 || | 707 RTC_DCHECK(!config.frame_length_ms || *config.frame_length_ms == 20 || |
| 691 *config.frame_length_ms == 60); | 708 *config.frame_length_ms == 60); |
| 692 | 709 |
| 693 if (config.bitrate_bps) | 710 if (config.bitrate_bps) |
| 694 SetTargetBitrate(*config.bitrate_bps); | 711 SetTargetBitrate(*config.bitrate_bps); |
| 695 if (config.frame_length_ms) | 712 if (config.frame_length_ms) |
| 696 SetFrameLength(*config.frame_length_ms); | 713 SetFrameLength(*config.frame_length_ms); |
| 697 if (config.enable_fec) | 714 if (config.enable_fec) |
| 698 SetFec(*config.enable_fec); | 715 SetFec(*config.enable_fec); |
| 699 if (config.uplink_packet_loss_fraction) | 716 if (config.uplink_packet_loss_fraction) |
| 700 SetProjectedPacketLossRate(*config.uplink_packet_loss_fraction); | 717 SetProjectedPacketLossRate(*config.uplink_packet_loss_fraction); |
| 701 if (config.enable_dtx) | 718 if (config.enable_dtx) |
| 702 SetDtx(*config.enable_dtx); | 719 SetDtx(*config.enable_dtx); |
| 703 if (config.num_channels) | 720 if (config.num_channels) |
| 704 SetNumChannelsToEncode(*config.num_channels); | 721 SetNumChannelsToEncode(*config.num_channels); |
| 705 } | 722 } |
| 706 | 723 |
| 707 std::unique_ptr<AudioNetworkAdaptor> | 724 std::unique_ptr<AudioNetworkAdaptor> |
| 708 AudioEncoderOpusImpl::DefaultAudioNetworkAdaptorCreator( | 725 AudioEncoderOpus::DefaultAudioNetworkAdaptorCreator( |
| 709 const ProtoString& config_string, | 726 const ProtoString& config_string, |
| 710 RtcEventLog* event_log) const { | 727 RtcEventLog* event_log) const { |
| 711 AudioNetworkAdaptorImpl::Config config; | 728 AudioNetworkAdaptorImpl::Config config; |
| 712 config.event_log = event_log; | 729 config.event_log = event_log; |
| 713 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( | 730 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( |
| 714 config, ControllerManagerImpl::Create( | 731 config, |
| 715 config_string, NumChannels(), supported_frame_lengths_ms(), | 732 ControllerManagerImpl::Create( |
| 716 AudioEncoderOpusConfig::kMinBitrateBps, | 733 config_string, NumChannels(), supported_frame_lengths_ms(), |
| 717 num_channels_to_encode_, next_frame_length_ms_, | 734 kOpusMinBitrateBps, num_channels_to_encode_, next_frame_length_ms_, |
| 718 GetTargetBitrate(), config_.fec_enabled, GetDtx()))); | 735 GetTargetBitrate(), config_.fec_enabled, GetDtx()))); |
| 719 } | 736 } |
| 720 | 737 |
| 721 void AudioEncoderOpusImpl::MaybeUpdateUplinkBandwidth() { | 738 void AudioEncoderOpus::MaybeUpdateUplinkBandwidth() { |
| 722 if (audio_network_adaptor_) { | 739 if (audio_network_adaptor_) { |
| 723 int64_t now_ms = rtc::TimeMillis(); | 740 int64_t now_ms = rtc::TimeMillis(); |
| 724 if (!bitrate_smoother_last_update_time_ || | 741 if (!bitrate_smoother_last_update_time_ || |
| 725 now_ms - *bitrate_smoother_last_update_time_ >= | 742 now_ms - *bitrate_smoother_last_update_time_ >= |
| 726 config_.uplink_bandwidth_update_interval_ms) { | 743 config_.uplink_bandwidth_update_interval_ms) { |
| 727 rtc::Optional<float> smoothed_bitrate = bitrate_smoother_->GetAverage(); | 744 rtc::Optional<float> smoothed_bitrate = bitrate_smoother_->GetAverage(); |
| 728 if (smoothed_bitrate) | 745 if (smoothed_bitrate) |
| 729 audio_network_adaptor_->SetUplinkBandwidth(*smoothed_bitrate); | 746 audio_network_adaptor_->SetUplinkBandwidth(*smoothed_bitrate); |
| 730 bitrate_smoother_last_update_time_ = rtc::Optional<int64_t>(now_ms); | 747 bitrate_smoother_last_update_time_ = rtc::Optional<int64_t>(now_ms); |
| 731 } | 748 } |
| 732 } | 749 } |
| 733 } | 750 } |
| 734 | 751 |
| 735 } // namespace webrtc | 752 } // namespace webrtc |
| OLD | NEW |