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 | 15 |
| 16 #include "webrtc/base/arraysize.h" | |
| 15 #include "webrtc/base/checks.h" | 17 #include "webrtc/base/checks.h" |
| 16 #include "webrtc/base/exp_filter.h" | 18 #include "webrtc/base/exp_filter.h" |
| 17 #include "webrtc/base/safe_conversions.h" | 19 #include "webrtc/base/safe_conversions.h" |
| 18 #include "webrtc/common_types.h" | 20 #include "webrtc/common_types.h" |
| 19 #include "webrtc/modules/audio_coding/audio_network_adaptor/audio_network_adapto r_impl.h" | 21 #include "webrtc/modules/audio_coding/audio_network_adaptor/audio_network_adapto r_impl.h" |
| 20 #include "webrtc/modules/audio_coding/audio_network_adaptor/controller_manager.h " | 22 #include "webrtc/modules/audio_coding/audio_network_adaptor/controller_manager.h " |
| 21 #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h" | 23 #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h" |
| 22 #include "webrtc/system_wrappers/include/clock.h" | 24 #include "webrtc/system_wrappers/include/clock.h" |
| 23 | 25 |
| 24 namespace webrtc { | 26 namespace webrtc { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 35 constexpr float kAlphaForPacketLossFractionSmoother = 0.9999f; | 37 constexpr float kAlphaForPacketLossFractionSmoother = 0.9999f; |
| 36 | 38 |
| 37 AudioEncoderOpus::Config CreateConfig(const CodecInst& codec_inst) { | 39 AudioEncoderOpus::Config CreateConfig(const CodecInst& codec_inst) { |
| 38 AudioEncoderOpus::Config config; | 40 AudioEncoderOpus::Config config; |
| 39 config.frame_size_ms = rtc::CheckedDivExact(codec_inst.pacsize, 48); | 41 config.frame_size_ms = rtc::CheckedDivExact(codec_inst.pacsize, 48); |
| 40 config.num_channels = codec_inst.channels; | 42 config.num_channels = codec_inst.channels; |
| 41 config.bitrate_bps = rtc::Optional<int>(codec_inst.rate); | 43 config.bitrate_bps = rtc::Optional<int>(codec_inst.rate); |
| 42 config.payload_type = codec_inst.pltype; | 44 config.payload_type = codec_inst.pltype; |
| 43 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip | 45 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip |
| 44 : AudioEncoderOpus::kAudio; | 46 : AudioEncoderOpus::kAudio; |
| 47 config.supported_frame_lengths_ms.push_back(config.frame_size_ms); | |
| 45 return config; | 48 return config; |
| 46 } | 49 } |
| 47 | 50 |
| 48 // Optimize the loss rate to configure Opus. Basically, optimized loss rate is | 51 // Optimize the loss rate to configure Opus. Basically, optimized loss rate is |
| 49 // the input loss rate rounded down to various levels, because a robustly good | 52 // the input loss rate rounded down to various levels, because a robustly good |
| 50 // audio quality is achieved by lowering the packet loss down. | 53 // audio quality is achieved by lowering the packet loss down. |
| 51 // Additionally, to prevent toggling, margins are used, i.e., when jumping to | 54 // Additionally, to prevent toggling, margins are used, i.e., when jumping to |
| 52 // a loss rate from below, a higher threshold is used than jumping to the same | 55 // a loss rate from below, a higher threshold is used than jumping to the same |
| 53 // level from above. | 56 // level from above. |
| 54 double OptimizePacketLossRate(double new_loss_rate, double old_loss_rate) { | 57 double OptimizePacketLossRate(double new_loss_rate, double old_loss_rate) { |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 290 | 293 |
| 291 void AudioEncoderOpus::OnReceivedRtt(int rtt_ms) { | 294 void AudioEncoderOpus::OnReceivedRtt(int rtt_ms) { |
| 292 if (!audio_network_adaptor_) | 295 if (!audio_network_adaptor_) |
| 293 return; | 296 return; |
| 294 audio_network_adaptor_->SetRtt(rtt_ms); | 297 audio_network_adaptor_->SetRtt(rtt_ms); |
| 295 ApplyAudioNetworkAdaptor(); | 298 ApplyAudioNetworkAdaptor(); |
| 296 } | 299 } |
| 297 | 300 |
| 298 void AudioEncoderOpus::SetReceiverFrameLengthRange(int min_frame_length_ms, | 301 void AudioEncoderOpus::SetReceiverFrameLengthRange(int min_frame_length_ms, |
| 299 int max_frame_length_ms) { | 302 int max_frame_length_ms) { |
| 300 if (!audio_network_adaptor_) | 303 // Ensure that |SetReceiverFrameLengthRange| is called before |
| 301 return; | 304 // |EnableAudioNetworkAdaptor|, otherwise we need to recreate |
| 302 audio_network_adaptor_->SetReceiverFrameLengthRange(min_frame_length_ms, | 305 // |audio_network_adaptor_|, which is not a needed use case. |
| 303 max_frame_length_ms); | 306 RTC_DCHECK(!audio_network_adaptor_); |
| 304 ApplyAudioNetworkAdaptor(); | 307 |
| 308 config_.supported_frame_lengths_ms.clear(); | |
| 309 std::copy_if(kSupportedFrameLengths, | |
| 310 kSupportedFrameLengths + arraysize(kSupportedFrameLengths), | |
|
kwiberg-webrtc
2016/10/19 09:12:01
You should be able to use std::begin(kSupportedFra
minyue-webrtc
2016/10/19 09:25:54
nice! thanks!
| |
| 311 std::back_inserter(config_.supported_frame_lengths_ms), | |
| 312 [&](int frame_length_ms) { | |
| 313 return frame_length_ms >= min_frame_length_ms && | |
| 314 frame_length_ms <= max_frame_length_ms; | |
| 315 }); | |
| 316 RTC_DCHECK(std::is_sorted(config_.supported_frame_lengths_ms.begin(), | |
| 317 config_.supported_frame_lengths_ms.end())); | |
| 305 } | 318 } |
| 306 | 319 |
| 307 AudioEncoder::EncodedInfo AudioEncoderOpus::EncodeImpl( | 320 AudioEncoder::EncodedInfo AudioEncoderOpus::EncodeImpl( |
| 308 uint32_t rtp_timestamp, | 321 uint32_t rtp_timestamp, |
| 309 rtc::ArrayView<const int16_t> audio, | 322 rtc::ArrayView<const int16_t> audio, |
| 310 rtc::Buffer* encoded) { | 323 rtc::Buffer* encoded) { |
| 311 | 324 |
| 312 if (input_buffer_.empty()) | 325 if (input_buffer_.empty()) |
| 313 first_timestamp_in_buffer_ = rtp_timestamp; | 326 first_timestamp_in_buffer_ = rtp_timestamp; |
| 314 | 327 |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 440 } | 453 } |
| 441 | 454 |
| 442 std::unique_ptr<AudioNetworkAdaptor> | 455 std::unique_ptr<AudioNetworkAdaptor> |
| 443 AudioEncoderOpus::DefaultAudioNetworkAdaptorCreator( | 456 AudioEncoderOpus::DefaultAudioNetworkAdaptorCreator( |
| 444 const std::string& config_string, | 457 const std::string& config_string, |
| 445 const Clock* clock) const { | 458 const Clock* clock) const { |
| 446 AudioNetworkAdaptorImpl::Config config; | 459 AudioNetworkAdaptorImpl::Config config; |
| 447 config.clock = clock; | 460 config.clock = clock; |
| 448 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( | 461 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( |
| 449 config, ControllerManagerImpl::Create( | 462 config, ControllerManagerImpl::Create( |
| 450 config_string, NumChannels(), kSupportedFrameLengths, | 463 config_string, NumChannels(), supported_frame_lengths_ms(), |
| 451 num_channels_to_encode_, next_frame_length_ms_, | 464 num_channels_to_encode_, next_frame_length_ms_, |
| 452 GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); | 465 GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); |
| 453 } | 466 } |
| 454 | 467 |
| 455 } // namespace webrtc | 468 } // namespace webrtc |
| OLD | NEW |