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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 #endif | 49 #endif |
50 return config; | 50 return config; |
51 } | 51 } |
52 | 52 |
53 // Optimize the loss rate to configure Opus. Basically, optimized loss rate is | 53 // Optimize the loss rate to configure Opus. Basically, optimized loss rate is |
54 // the input loss rate rounded down to various levels, because a robustly good | 54 // the input loss rate rounded down to various levels, because a robustly good |
55 // audio quality is achieved by lowering the packet loss down. | 55 // audio quality is achieved by lowering the packet loss down. |
56 // Additionally, to prevent toggling, margins are used, i.e., when jumping to | 56 // Additionally, to prevent toggling, margins are used, i.e., when jumping to |
57 // a loss rate from below, a higher threshold is used than jumping to the same | 57 // a loss rate from below, a higher threshold is used than jumping to the same |
58 // level from above. | 58 // level from above. |
59 float OptimizePacketLossRate(float new_loss_rate, float old_loss_rate) { | 59 double OptimizePacketLossRate(double new_loss_rate, double old_loss_rate) { |
60 RTC_DCHECK_GE(new_loss_rate, 0.0f); | 60 RTC_DCHECK_GE(new_loss_rate, 0.0); |
61 RTC_DCHECK_LE(new_loss_rate, 1.0f); | 61 RTC_DCHECK_LE(new_loss_rate, 1.0); |
62 RTC_DCHECK_GE(old_loss_rate, 0.0f); | 62 RTC_DCHECK_GE(old_loss_rate, 0.0); |
63 RTC_DCHECK_LE(old_loss_rate, 1.0f); | 63 RTC_DCHECK_LE(old_loss_rate, 1.0); |
64 constexpr float kPacketLossRate20 = 0.20f; | 64 const double kPacketLossRate20 = 0.20; |
65 constexpr float kPacketLossRate10 = 0.10f; | 65 const double kPacketLossRate10 = 0.10; |
66 constexpr float kPacketLossRate5 = 0.05f; | 66 const double kPacketLossRate5 = 0.05; |
67 constexpr float kPacketLossRate1 = 0.01f; | 67 const double kPacketLossRate1 = 0.01; |
68 constexpr float kLossRate20Margin = 0.02f; | 68 const double kLossRate20Margin = 0.02; |
69 constexpr float kLossRate10Margin = 0.01f; | 69 const double kLossRate10Margin = 0.01; |
70 constexpr float kLossRate5Margin = 0.01f; | 70 const double kLossRate5Margin = 0.01; |
71 if (new_loss_rate >= | 71 if (new_loss_rate >= |
72 kPacketLossRate20 + | 72 kPacketLossRate20 + |
73 kLossRate20Margin * | 73 kLossRate20Margin * |
74 (kPacketLossRate20 - old_loss_rate > 0 ? 1 : -1)) { | 74 (kPacketLossRate20 - old_loss_rate > 0 ? 1 : -1)) { |
75 return kPacketLossRate20; | 75 return kPacketLossRate20; |
76 } else if (new_loss_rate >= | 76 } else if (new_loss_rate >= |
77 kPacketLossRate10 + | 77 kPacketLossRate10 + |
78 kLossRate10Margin * | 78 kLossRate10Margin * |
79 (kPacketLossRate10 - old_loss_rate > 0 ? 1 : -1)) { | 79 (kPacketLossRate10 - old_loss_rate > 0 ? 1 : -1)) { |
80 return kPacketLossRate10; | 80 return kPacketLossRate10; |
81 } else if (new_loss_rate >= | 81 } else if (new_loss_rate >= |
82 kPacketLossRate5 + | 82 kPacketLossRate5 + |
83 kLossRate5Margin * | 83 kLossRate5Margin * |
84 (kPacketLossRate5 - old_loss_rate > 0 ? 1 : -1)) { | 84 (kPacketLossRate5 - old_loss_rate > 0 ? 1 : -1)) { |
85 return kPacketLossRate5; | 85 return kPacketLossRate5; |
86 } else if (new_loss_rate >= kPacketLossRate1) { | 86 } else if (new_loss_rate >= kPacketLossRate1) { |
87 return kPacketLossRate1; | 87 return kPacketLossRate1; |
88 } else { | 88 } else { |
89 return 0.0f; | 89 return 0.0; |
90 } | 90 } |
91 } | 91 } |
92 | 92 |
93 } // namespace | 93 } // namespace |
94 | 94 |
95 class AudioEncoderOpus::PacketLossFractionSmoother { | 95 class AudioEncoderOpus::PacketLossFractionSmoother { |
96 public: | 96 public: |
97 explicit PacketLossFractionSmoother(const Clock* clock) | 97 explicit PacketLossFractionSmoother(const Clock* clock) |
98 : clock_(clock), | 98 : clock_(clock), |
99 last_sample_time_ms_(clock_->TimeInMilliseconds()), | 99 last_sample_time_ms_(clock_->TimeInMilliseconds()), |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 } | 252 } |
253 return RecreateEncoderInstance(conf); | 253 return RecreateEncoderInstance(conf); |
254 } | 254 } |
255 | 255 |
256 void AudioEncoderOpus::SetMaxPlaybackRate(int frequency_hz) { | 256 void AudioEncoderOpus::SetMaxPlaybackRate(int frequency_hz) { |
257 auto conf = config_; | 257 auto conf = config_; |
258 conf.max_playback_rate_hz = frequency_hz; | 258 conf.max_playback_rate_hz = frequency_hz; |
259 RTC_CHECK(RecreateEncoderInstance(conf)); | 259 RTC_CHECK(RecreateEncoderInstance(conf)); |
260 } | 260 } |
261 | 261 |
| 262 void AudioEncoderOpus::SetProjectedPacketLossRate(double fraction) { |
| 263 double opt_loss_rate = OptimizePacketLossRate(fraction, packet_loss_rate_); |
| 264 if (packet_loss_rate_ != opt_loss_rate) { |
| 265 packet_loss_rate_ = opt_loss_rate; |
| 266 RTC_CHECK_EQ( |
| 267 0, WebRtcOpus_SetPacketLossRate( |
| 268 inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5))); |
| 269 } |
| 270 } |
| 271 |
| 272 void AudioEncoderOpus::SetTargetBitrate(int bits_per_second) { |
| 273 config_.bitrate_bps = rtc::Optional<int>( |
| 274 std::max(std::min(bits_per_second, kMaxBitrateBps), kMinBitrateBps)); |
| 275 RTC_DCHECK(config_.IsOk()); |
| 276 RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, config_.GetBitrateBps())); |
| 277 const auto new_complexity = config_.GetNewComplexity(); |
| 278 if (new_complexity && complexity_ != *new_complexity) { |
| 279 complexity_ = *new_complexity; |
| 280 RTC_CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, complexity_)); |
| 281 } |
| 282 } |
| 283 |
262 bool AudioEncoderOpus::EnableAudioNetworkAdaptor( | 284 bool AudioEncoderOpus::EnableAudioNetworkAdaptor( |
263 const std::string& config_string, | 285 const std::string& config_string, |
264 const Clock* clock) { | 286 const Clock* clock) { |
265 audio_network_adaptor_ = audio_network_adaptor_creator_(config_string, clock); | 287 audio_network_adaptor_ = audio_network_adaptor_creator_(config_string, clock); |
266 return audio_network_adaptor_.get() != nullptr; | 288 return audio_network_adaptor_.get() != nullptr; |
267 } | 289 } |
268 | 290 |
269 void AudioEncoderOpus::DisableAudioNetworkAdaptor() { | 291 void AudioEncoderOpus::DisableAudioNetworkAdaptor() { |
270 audio_network_adaptor_.reset(nullptr); | 292 audio_network_adaptor_.reset(nullptr); |
271 } | 293 } |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 RTC_DCHECK_GT(num_channels_to_encode, 0); | 455 RTC_DCHECK_GT(num_channels_to_encode, 0); |
434 RTC_DCHECK_LE(num_channels_to_encode, config_.num_channels); | 456 RTC_DCHECK_LE(num_channels_to_encode, config_.num_channels); |
435 | 457 |
436 if (num_channels_to_encode_ == num_channels_to_encode) | 458 if (num_channels_to_encode_ == num_channels_to_encode) |
437 return; | 459 return; |
438 | 460 |
439 RTC_CHECK_EQ(0, WebRtcOpus_SetForceChannels(inst_, num_channels_to_encode)); | 461 RTC_CHECK_EQ(0, WebRtcOpus_SetForceChannels(inst_, num_channels_to_encode)); |
440 num_channels_to_encode_ = num_channels_to_encode; | 462 num_channels_to_encode_ = num_channels_to_encode; |
441 } | 463 } |
442 | 464 |
443 void AudioEncoderOpus::SetProjectedPacketLossRate(float fraction) { | |
444 float opt_loss_rate = OptimizePacketLossRate(fraction, packet_loss_rate_); | |
445 if (packet_loss_rate_ != opt_loss_rate) { | |
446 packet_loss_rate_ = opt_loss_rate; | |
447 RTC_CHECK_EQ( | |
448 0, WebRtcOpus_SetPacketLossRate( | |
449 inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5))); | |
450 } | |
451 } | |
452 | |
453 void AudioEncoderOpus::SetTargetBitrate(int bits_per_second) { | |
454 config_.bitrate_bps = rtc::Optional<int>( | |
455 std::max(std::min(bits_per_second, kMaxBitrateBps), kMinBitrateBps)); | |
456 RTC_DCHECK(config_.IsOk()); | |
457 RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, config_.GetBitrateBps())); | |
458 const auto new_complexity = config_.GetNewComplexity(); | |
459 if (new_complexity && complexity_ != *new_complexity) { | |
460 complexity_ = *new_complexity; | |
461 RTC_CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, complexity_)); | |
462 } | |
463 } | |
464 | |
465 void AudioEncoderOpus::ApplyAudioNetworkAdaptor() { | 465 void AudioEncoderOpus::ApplyAudioNetworkAdaptor() { |
466 auto config = audio_network_adaptor_->GetEncoderRuntimeConfig(); | 466 auto config = audio_network_adaptor_->GetEncoderRuntimeConfig(); |
467 // |audio_network_adaptor_| is supposed to be configured to output all | 467 // |audio_network_adaptor_| is supposed to be configured to output all |
468 // following parameters. | 468 // following parameters. |
469 RTC_DCHECK(config.bitrate_bps); | 469 RTC_DCHECK(config.bitrate_bps); |
470 RTC_DCHECK(config.frame_length_ms); | 470 RTC_DCHECK(config.frame_length_ms); |
471 RTC_DCHECK(config.uplink_packet_loss_fraction); | 471 RTC_DCHECK(config.uplink_packet_loss_fraction); |
472 RTC_DCHECK(config.enable_fec); | 472 RTC_DCHECK(config.enable_fec); |
473 RTC_DCHECK(config.enable_dtx); | 473 RTC_DCHECK(config.enable_dtx); |
474 RTC_DCHECK(config.num_channels); | 474 RTC_DCHECK(config.num_channels); |
(...skipping 15 matching lines...) Expand all Loading... |
490 AudioNetworkAdaptorImpl::Config config; | 490 AudioNetworkAdaptorImpl::Config config; |
491 config.clock = clock; | 491 config.clock = clock; |
492 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( | 492 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( |
493 config, ControllerManagerImpl::Create( | 493 config, ControllerManagerImpl::Create( |
494 config_string, NumChannels(), supported_frame_lengths_ms(), | 494 config_string, NumChannels(), supported_frame_lengths_ms(), |
495 num_channels_to_encode_, next_frame_length_ms_, | 495 num_channels_to_encode_, next_frame_length_ms_, |
496 GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); | 496 GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); |
497 } | 497 } |
498 | 498 |
499 } // namespace webrtc | 499 } // namespace webrtc |
OLD | NEW |