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 | 14 |
| 15 #include "webrtc/base/arraysize.h" | |
| 15 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
| 16 #include "webrtc/base/exp_filter.h" | 17 #include "webrtc/base/exp_filter.h" |
| 17 #include "webrtc/base/safe_conversions.h" | 18 #include "webrtc/base/safe_conversions.h" |
| 18 #include "webrtc/common_types.h" | 19 #include "webrtc/common_types.h" |
| 19 #include "webrtc/modules/audio_coding/audio_network_adaptor/audio_network_adapto r_impl.h" | 20 #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 " | 21 #include "webrtc/modules/audio_coding/audio_network_adaptor/controller_manager.h " |
| 21 #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h" | 22 #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h" |
| 22 #include "webrtc/system_wrappers/include/clock.h" | 23 #include "webrtc/system_wrappers/include/clock.h" |
| 23 | 24 |
| 24 namespace webrtc { | 25 namespace webrtc { |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 AudioEncoderOpus::AudioEncoderOpus( | 146 AudioEncoderOpus::AudioEncoderOpus( |
| 146 const Config& config, | 147 const Config& config, |
| 147 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator) | 148 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator) |
| 148 : packet_loss_rate_(0.0), | 149 : packet_loss_rate_(0.0), |
| 149 inst_(nullptr), | 150 inst_(nullptr), |
| 150 packet_loss_fraction_smoother_(new PacketLossFractionSmoother( | 151 packet_loss_fraction_smoother_(new PacketLossFractionSmoother( |
| 151 config.clock ? config.clock : Clock::GetRealTimeClock())), | 152 config.clock ? config.clock : Clock::GetRealTimeClock())), |
| 152 audio_network_adaptor_creator_( | 153 audio_network_adaptor_creator_( |
| 153 audio_network_adaptor_creator | 154 audio_network_adaptor_creator |
| 154 ? std::move(audio_network_adaptor_creator) | 155 ? std::move(audio_network_adaptor_creator) |
| 155 : [this](const std::string& config_string, const Clock* clock) { | 156 : [this](const std::string& config_string, |
| 156 return DefaultAudioNetworkAdaptorCreator(config_string, | 157 int min_receiver_frame_length_ms, |
| 157 clock); | 158 int max_receiver_frame_length_ms, |
| 159 const Clock* clock) { | |
| 160 return DefaultAudioNetworkAdaptorCreator( | |
| 161 config_string, min_receiver_frame_length_ms, | |
| 162 max_receiver_frame_length_ms, clock); | |
| 158 }) { | 163 }) { |
| 159 RTC_CHECK(RecreateEncoderInstance(config)); | 164 RTC_CHECK(RecreateEncoderInstance(config)); |
| 160 } | 165 } |
| 161 | 166 |
| 162 AudioEncoderOpus::AudioEncoderOpus(const CodecInst& codec_inst) | 167 AudioEncoderOpus::AudioEncoderOpus(const CodecInst& codec_inst) |
| 163 : AudioEncoderOpus(CreateConfig(codec_inst), nullptr) {} | 168 : AudioEncoderOpus(CreateConfig(codec_inst), nullptr) {} |
| 164 | 169 |
| 165 AudioEncoderOpus::~AudioEncoderOpus() { | 170 AudioEncoderOpus::~AudioEncoderOpus() { |
| 166 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); | 171 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); |
| 167 } | 172 } |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 245 | 250 |
| 246 void AudioEncoderOpus::SetTargetBitrate(int bits_per_second) { | 251 void AudioEncoderOpus::SetTargetBitrate(int bits_per_second) { |
| 247 config_.bitrate_bps = rtc::Optional<int>( | 252 config_.bitrate_bps = rtc::Optional<int>( |
| 248 std::max(std::min(bits_per_second, kMaxBitrateBps), kMinBitrateBps)); | 253 std::max(std::min(bits_per_second, kMaxBitrateBps), kMinBitrateBps)); |
| 249 RTC_DCHECK(config_.IsOk()); | 254 RTC_DCHECK(config_.IsOk()); |
| 250 RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, config_.GetBitrateBps())); | 255 RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, config_.GetBitrateBps())); |
| 251 } | 256 } |
| 252 | 257 |
| 253 bool AudioEncoderOpus::EnableAudioNetworkAdaptor( | 258 bool AudioEncoderOpus::EnableAudioNetworkAdaptor( |
| 254 const std::string& config_string, | 259 const std::string& config_string, |
| 260 int min_receiver_frame_length_ms, | |
| 261 int max_receiver_frame_length_ms, | |
| 255 const Clock* clock) { | 262 const Clock* clock) { |
| 256 audio_network_adaptor_ = audio_network_adaptor_creator_(config_string, clock); | 263 audio_network_adaptor_ = audio_network_adaptor_creator_( |
| 264 config_string, min_receiver_frame_length_ms, max_receiver_frame_length_ms, | |
| 265 clock); | |
| 257 return audio_network_adaptor_.get() != nullptr; | 266 return audio_network_adaptor_.get() != nullptr; |
| 258 } | 267 } |
| 259 | 268 |
| 260 void AudioEncoderOpus::DisableAudioNetworkAdaptor() { | 269 void AudioEncoderOpus::DisableAudioNetworkAdaptor() { |
| 261 audio_network_adaptor_.reset(nullptr); | 270 audio_network_adaptor_.reset(nullptr); |
| 262 } | 271 } |
| 263 | 272 |
| 264 void AudioEncoderOpus::OnReceivedUplinkBandwidth(int uplink_bandwidth_bps) { | 273 void AudioEncoderOpus::OnReceivedUplinkBandwidth(int uplink_bandwidth_bps) { |
| 265 if (!audio_network_adaptor_) | 274 if (!audio_network_adaptor_) |
| 266 return; | 275 return; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 288 ApplyAudioNetworkAdaptor(); | 297 ApplyAudioNetworkAdaptor(); |
| 289 } | 298 } |
| 290 | 299 |
| 291 void AudioEncoderOpus::OnReceivedRtt(int rtt_ms) { | 300 void AudioEncoderOpus::OnReceivedRtt(int rtt_ms) { |
| 292 if (!audio_network_adaptor_) | 301 if (!audio_network_adaptor_) |
| 293 return; | 302 return; |
| 294 audio_network_adaptor_->SetRtt(rtt_ms); | 303 audio_network_adaptor_->SetRtt(rtt_ms); |
| 295 ApplyAudioNetworkAdaptor(); | 304 ApplyAudioNetworkAdaptor(); |
| 296 } | 305 } |
| 297 | 306 |
| 298 void AudioEncoderOpus::SetReceiverFrameLengthRange(int min_frame_length_ms, | |
| 299 int max_frame_length_ms) { | |
| 300 if (!audio_network_adaptor_) | |
| 301 return; | |
| 302 audio_network_adaptor_->SetReceiverFrameLengthRange(min_frame_length_ms, | |
| 303 max_frame_length_ms); | |
| 304 ApplyAudioNetworkAdaptor(); | |
| 305 } | |
| 306 | |
| 307 AudioEncoder::EncodedInfo AudioEncoderOpus::EncodeImpl( | 307 AudioEncoder::EncodedInfo AudioEncoderOpus::EncodeImpl( |
| 308 uint32_t rtp_timestamp, | 308 uint32_t rtp_timestamp, |
| 309 rtc::ArrayView<const int16_t> audio, | 309 rtc::ArrayView<const int16_t> audio, |
| 310 rtc::Buffer* encoded) { | 310 rtc::Buffer* encoded) { |
| 311 | 311 |
| 312 if (input_buffer_.empty()) | 312 if (input_buffer_.empty()) |
| 313 first_timestamp_in_buffer_ = rtp_timestamp; | 313 first_timestamp_in_buffer_ = rtp_timestamp; |
| 314 | 314 |
| 315 input_buffer_.insert(input_buffer_.end(), audio.cbegin(), audio.cend()); | 315 input_buffer_.insert(input_buffer_.end(), audio.cbegin(), audio.cend()); |
| 316 if (input_buffer_.size() < | 316 if (input_buffer_.size() < |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 435 SetFrameLength(*config.frame_length_ms); | 435 SetFrameLength(*config.frame_length_ms); |
| 436 SetFec(*config.enable_fec); | 436 SetFec(*config.enable_fec); |
| 437 SetProjectedPacketLossRate(*config.uplink_packet_loss_fraction); | 437 SetProjectedPacketLossRate(*config.uplink_packet_loss_fraction); |
| 438 SetDtx(*config.enable_dtx); | 438 SetDtx(*config.enable_dtx); |
| 439 SetNumChannelsToEncode(*config.num_channels); | 439 SetNumChannelsToEncode(*config.num_channels); |
| 440 } | 440 } |
| 441 | 441 |
| 442 std::unique_ptr<AudioNetworkAdaptor> | 442 std::unique_ptr<AudioNetworkAdaptor> |
| 443 AudioEncoderOpus::DefaultAudioNetworkAdaptorCreator( | 443 AudioEncoderOpus::DefaultAudioNetworkAdaptorCreator( |
| 444 const std::string& config_string, | 444 const std::string& config_string, |
| 445 int min_receiver_frame_length_ms, | |
| 446 int max_receiver_frame_length_ms, | |
| 445 const Clock* clock) const { | 447 const Clock* clock) const { |
| 446 AudioNetworkAdaptorImpl::Config config; | 448 AudioNetworkAdaptorImpl::Config config; |
| 447 config.clock = clock; | 449 config.clock = clock; |
| 450 | |
| 451 std::vector<int> frame_lengths; | |
|
michaelt
2016/10/18 08:25:23
Would be nice to have a unit-test for this functio
minyue-webrtc
2016/10/18 13:25:13
I agree. I thought about it but did not find a goo
| |
| 452 std::copy_if(kSupportedFrameLengths, | |
| 453 kSupportedFrameLengths + arraysize(kSupportedFrameLengths), | |
| 454 std::back_inserter(frame_lengths), [&](int frame_length_ms) { | |
| 455 return frame_length_ms >= min_receiver_frame_length_ms && | |
| 456 frame_length_ms <= max_receiver_frame_length_ms; | |
| 457 }); | |
| 458 RTC_DCHECK(std::is_sorted(frame_lengths.begin(), frame_lengths.end())); | |
| 459 | |
| 448 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( | 460 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( |
| 449 config, ControllerManagerImpl::Create( | 461 config, ControllerManagerImpl::Create( |
| 450 config_string, NumChannels(), kSupportedFrameLengths, | 462 config_string, NumChannels(), frame_lengths, |
| 451 num_channels_to_encode_, next_frame_length_ms_, | 463 num_channels_to_encode_, next_frame_length_ms_, |
| 452 GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); | 464 GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); |
| 453 } | 465 } |
| 454 | 466 |
| 455 } // namespace webrtc | 467 } // namespace webrtc |
| OLD | NEW |