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