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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 config.supported_frame_lengths_ms.push_back(config.frame_size_ms); | 46 config.supported_frame_lengths_ms.push_back(config.frame_size_ms); |
47 return config; | 47 return config; |
48 } | 48 } |
49 | 49 |
50 // Optimize the loss rate to configure Opus. Basically, optimized loss rate is | 50 // Optimize the loss rate to configure Opus. Basically, optimized loss rate is |
51 // the input loss rate rounded down to various levels, because a robustly good | 51 // the input loss rate rounded down to various levels, because a robustly good |
52 // audio quality is achieved by lowering the packet loss down. | 52 // audio quality is achieved by lowering the packet loss down. |
53 // Additionally, to prevent toggling, margins are used, i.e., when jumping to | 53 // Additionally, to prevent toggling, margins are used, i.e., when jumping to |
54 // a loss rate from below, a higher threshold is used than jumping to the same | 54 // a loss rate from below, a higher threshold is used than jumping to the same |
55 // level from above. | 55 // level from above. |
56 double OptimizePacketLossRate(double new_loss_rate, double old_loss_rate) { | 56 float OptimizePacketLossRate(float new_loss_rate, float old_loss_rate) { |
57 RTC_DCHECK_GE(new_loss_rate, 0.0); | 57 RTC_DCHECK_GE(new_loss_rate, 0.0f); |
58 RTC_DCHECK_LE(new_loss_rate, 1.0); | 58 RTC_DCHECK_LE(new_loss_rate, 1.0f); |
59 RTC_DCHECK_GE(old_loss_rate, 0.0); | 59 RTC_DCHECK_GE(old_loss_rate, 0.0f); |
60 RTC_DCHECK_LE(old_loss_rate, 1.0); | 60 RTC_DCHECK_LE(old_loss_rate, 1.0f); |
61 const double kPacketLossRate20 = 0.20; | 61 constexpr float kPacketLossRate20 = 0.20f; |
62 const double kPacketLossRate10 = 0.10; | 62 constexpr float kPacketLossRate10 = 0.10f; |
63 const double kPacketLossRate5 = 0.05; | 63 constexpr float kPacketLossRate5 = 0.05f; |
64 const double kPacketLossRate1 = 0.01; | 64 constexpr float kPacketLossRate1 = 0.01f; |
65 const double kLossRate20Margin = 0.02; | 65 constexpr float kLossRate20Margin = 0.02f; |
66 const double kLossRate10Margin = 0.01; | 66 constexpr float kLossRate10Margin = 0.01f; |
67 const double kLossRate5Margin = 0.01; | 67 constexpr float kLossRate5Margin = 0.01f; |
68 if (new_loss_rate >= | 68 if (new_loss_rate >= |
69 kPacketLossRate20 + | 69 kPacketLossRate20 + |
70 kLossRate20Margin * | 70 kLossRate20Margin * |
71 (kPacketLossRate20 - old_loss_rate > 0 ? 1 : -1)) { | 71 (kPacketLossRate20 - old_loss_rate > 0 ? 1 : -1)) { |
72 return kPacketLossRate20; | 72 return kPacketLossRate20; |
73 } else if (new_loss_rate >= | 73 } else if (new_loss_rate >= |
74 kPacketLossRate10 + | 74 kPacketLossRate10 + |
75 kLossRate10Margin * | 75 kLossRate10Margin * |
76 (kPacketLossRate10 - old_loss_rate > 0 ? 1 : -1)) { | 76 (kPacketLossRate10 - old_loss_rate > 0 ? 1 : -1)) { |
77 return kPacketLossRate10; | 77 return kPacketLossRate10; |
78 } else if (new_loss_rate >= | 78 } else if (new_loss_rate >= |
79 kPacketLossRate5 + | 79 kPacketLossRate5 + |
80 kLossRate5Margin * | 80 kLossRate5Margin * |
81 (kPacketLossRate5 - old_loss_rate > 0 ? 1 : -1)) { | 81 (kPacketLossRate5 - old_loss_rate > 0 ? 1 : -1)) { |
82 return kPacketLossRate5; | 82 return kPacketLossRate5; |
83 } else if (new_loss_rate >= kPacketLossRate1) { | 83 } else if (new_loss_rate >= kPacketLossRate1) { |
84 return kPacketLossRate1; | 84 return kPacketLossRate1; |
85 } else { | 85 } else { |
86 return 0.0; | 86 return 0.0f; |
87 } | 87 } |
88 } | 88 } |
89 | 89 |
90 } // namespace | 90 } // namespace |
91 | 91 |
92 class AudioEncoderOpus::PacketLossFractionSmoother { | 92 class AudioEncoderOpus::PacketLossFractionSmoother { |
93 public: | 93 public: |
94 explicit PacketLossFractionSmoother(const Clock* clock) | 94 explicit PacketLossFractionSmoother(const Clock* clock) |
95 : clock_(clock), | 95 : clock_(clock), |
96 last_sample_time_ms_(clock_->TimeInMilliseconds()), | 96 last_sample_time_ms_(clock_->TimeInMilliseconds()), |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 } | 228 } |
229 return RecreateEncoderInstance(conf); | 229 return RecreateEncoderInstance(conf); |
230 } | 230 } |
231 | 231 |
232 void AudioEncoderOpus::SetMaxPlaybackRate(int frequency_hz) { | 232 void AudioEncoderOpus::SetMaxPlaybackRate(int frequency_hz) { |
233 auto conf = config_; | 233 auto conf = config_; |
234 conf.max_playback_rate_hz = frequency_hz; | 234 conf.max_playback_rate_hz = frequency_hz; |
235 RTC_CHECK(RecreateEncoderInstance(conf)); | 235 RTC_CHECK(RecreateEncoderInstance(conf)); |
236 } | 236 } |
237 | 237 |
238 void AudioEncoderOpus::SetProjectedPacketLossRate(double fraction) { | |
239 double opt_loss_rate = OptimizePacketLossRate(fraction, packet_loss_rate_); | |
240 if (packet_loss_rate_ != opt_loss_rate) { | |
241 packet_loss_rate_ = opt_loss_rate; | |
242 RTC_CHECK_EQ( | |
243 0, WebRtcOpus_SetPacketLossRate( | |
244 inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5))); | |
245 } | |
246 } | |
247 | |
248 void AudioEncoderOpus::SetTargetBitrate(int bits_per_second) { | |
249 config_.bitrate_bps = rtc::Optional<int>( | |
250 std::max(std::min(bits_per_second, kMaxBitrateBps), kMinBitrateBps)); | |
251 RTC_DCHECK(config_.IsOk()); | |
252 RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, config_.GetBitrateBps())); | |
253 } | |
254 | |
255 bool AudioEncoderOpus::EnableAudioNetworkAdaptor( | 238 bool AudioEncoderOpus::EnableAudioNetworkAdaptor( |
256 const std::string& config_string, | 239 const std::string& config_string, |
257 const Clock* clock) { | 240 const Clock* clock) { |
258 audio_network_adaptor_ = audio_network_adaptor_creator_(config_string, clock); | 241 audio_network_adaptor_ = audio_network_adaptor_creator_(config_string, clock); |
259 return audio_network_adaptor_.get() != nullptr; | 242 return audio_network_adaptor_.get() != nullptr; |
260 } | 243 } |
261 | 244 |
262 void AudioEncoderOpus::DisableAudioNetworkAdaptor() { | 245 void AudioEncoderOpus::DisableAudioNetworkAdaptor() { |
263 audio_network_adaptor_.reset(nullptr); | 246 audio_network_adaptor_.reset(nullptr); |
264 } | 247 } |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 RTC_DCHECK_GT(num_channels_to_encode, 0u); | 406 RTC_DCHECK_GT(num_channels_to_encode, 0u); |
424 RTC_DCHECK_LE(num_channels_to_encode, config_.num_channels); | 407 RTC_DCHECK_LE(num_channels_to_encode, config_.num_channels); |
425 | 408 |
426 if (num_channels_to_encode_ == num_channels_to_encode) | 409 if (num_channels_to_encode_ == num_channels_to_encode) |
427 return; | 410 return; |
428 | 411 |
429 RTC_CHECK_EQ(0, WebRtcOpus_SetForceChannels(inst_, num_channels_to_encode)); | 412 RTC_CHECK_EQ(0, WebRtcOpus_SetForceChannels(inst_, num_channels_to_encode)); |
430 num_channels_to_encode_ = num_channels_to_encode; | 413 num_channels_to_encode_ = num_channels_to_encode; |
431 } | 414 } |
432 | 415 |
| 416 void AudioEncoderOpus::SetProjectedPacketLossRate(float fraction) { |
| 417 float opt_loss_rate = OptimizePacketLossRate(fraction, packet_loss_rate_); |
| 418 if (packet_loss_rate_ != opt_loss_rate) { |
| 419 packet_loss_rate_ = opt_loss_rate; |
| 420 RTC_CHECK_EQ( |
| 421 0, WebRtcOpus_SetPacketLossRate( |
| 422 inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5))); |
| 423 } |
| 424 } |
| 425 |
| 426 void AudioEncoderOpus::SetTargetBitrate(int bits_per_second) { |
| 427 config_.bitrate_bps = rtc::Optional<int>( |
| 428 std::max(std::min(bits_per_second, kMaxBitrateBps), kMinBitrateBps)); |
| 429 RTC_DCHECK(config_.IsOk()); |
| 430 RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, config_.GetBitrateBps())); |
| 431 } |
| 432 |
433 void AudioEncoderOpus::ApplyAudioNetworkAdaptor() { | 433 void AudioEncoderOpus::ApplyAudioNetworkAdaptor() { |
434 auto config = audio_network_adaptor_->GetEncoderRuntimeConfig(); | 434 auto config = audio_network_adaptor_->GetEncoderRuntimeConfig(); |
435 // |audio_network_adaptor_| is supposed to be configured to output all | 435 // |audio_network_adaptor_| is supposed to be configured to output all |
436 // following parameters. | 436 // following parameters. |
437 RTC_DCHECK(config.bitrate_bps); | 437 RTC_DCHECK(config.bitrate_bps); |
438 RTC_DCHECK(config.frame_length_ms); | 438 RTC_DCHECK(config.frame_length_ms); |
439 RTC_DCHECK(config.uplink_packet_loss_fraction); | 439 RTC_DCHECK(config.uplink_packet_loss_fraction); |
440 RTC_DCHECK(config.enable_fec); | 440 RTC_DCHECK(config.enable_fec); |
441 RTC_DCHECK(config.enable_dtx); | 441 RTC_DCHECK(config.enable_dtx); |
442 RTC_DCHECK(config.num_channels); | 442 RTC_DCHECK(config.num_channels); |
(...skipping 15 matching lines...) Expand all Loading... |
458 AudioNetworkAdaptorImpl::Config config; | 458 AudioNetworkAdaptorImpl::Config config; |
459 config.clock = clock; | 459 config.clock = clock; |
460 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( | 460 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( |
461 config, ControllerManagerImpl::Create( | 461 config, ControllerManagerImpl::Create( |
462 config_string, NumChannels(), supported_frame_lengths_ms(), | 462 config_string, NumChannels(), supported_frame_lengths_ms(), |
463 num_channels_to_encode_, next_frame_length_ms_, | 463 num_channels_to_encode_, next_frame_length_ms_, |
464 GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); | 464 GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); |
465 } | 465 } |
466 | 466 |
467 } // namespace webrtc | 467 } // namespace webrtc |
OLD | NEW |