Chromium Code Reviews| Index: webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc |
| diff --git a/webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc b/webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc |
| index d03f2d3cc2440c3d5fa029e91c4dd037fcf54c96..a6696ce528d2bf01a2f4bf8fc73d84fb1c4e5a0e 100644 |
| --- a/webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc |
| +++ b/webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc |
| @@ -16,6 +16,7 @@ |
| #include "webrtc/base/safe_conversions.h" |
| #include "webrtc/common_types.h" |
| #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h" |
| +#include "webrtc/modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor.h" |
| namespace webrtc { |
| @@ -141,15 +142,23 @@ void AudioEncoderOpus::Reset() { |
| } |
| bool AudioEncoderOpus::SetFec(bool enable) { |
| - auto conf = config_; |
| - conf.fec_enabled = enable; |
| - return RecreateEncoderInstance(conf); |
| + if (enable) { |
| + RTC_CHECK_EQ(0, WebRtcOpus_EnableFec(inst_)); |
| + } else { |
| + RTC_CHECK_EQ(0, WebRtcOpus_DisableFec(inst_)); |
| + } |
| + config_.fec_enabled = enable; |
| + return true; |
| } |
| bool AudioEncoderOpus::SetDtx(bool enable) { |
| - auto conf = config_; |
| - conf.dtx_enabled = enable; |
| - return RecreateEncoderInstance(conf); |
| + if (enable) { |
| + RTC_CHECK_EQ(0, WebRtcOpus_EnableDtx(inst_)); |
| + } else { |
| + RTC_CHECK_EQ(0, WebRtcOpus_DisableDtx(inst_)); |
| + } |
| + config_.dtx_enabled = enable; |
| + return true; |
| } |
| bool AudioEncoderOpus::GetDtx() const { |
| @@ -192,6 +201,38 @@ void AudioEncoderOpus::SetTargetBitrate(int bits_per_second) { |
| RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, config_.GetBitrateBps())); |
| } |
| +void AudioEncoderOpus::OnReceivedUplinkBandwidth(int uplink_bandwidth_bps) { |
| + if (!config_.audio_network_adaptor_enabled) |
| + return; |
| + RTC_DCHECK(audio_network_adaptor_); |
| + audio_network_adaptor_->SetUplinkBandwidth(uplink_bandwidth_bps); |
| + ApplyAudioNetworkAdaptor(); |
| +} |
| + |
| +void AudioEncoderOpus::OnReceivedUplinkPacketLossFraction(float uplink_packet_loss_fraction) { |
| + if (!config_.audio_network_adaptor_enabled) |
| + return; |
| + RTC_DCHECK(audio_network_adaptor_); |
| + audio_network_adaptor_->SetUplinkPacketLossFraction(uplink_packet_loss_fraction); |
| + ApplyAudioNetworkAdaptor(); |
| +} |
| + |
| +void AudioEncoderOpus::OnReceivedTargetAudioBitrate(int target_audio_bitrate_bps) { |
| + if (!config_.audio_network_adaptor_enabled) |
| + return; |
| + RTC_DCHECK(audio_network_adaptor_); |
| +// audio_network_adaptor_->SetReceivedTargetAudioBitrate(uplink_bandwidth_bps); |
| + ApplyAudioNetworkAdaptor(); |
| +} |
| + |
| +void AudioEncoderOpus::OnReceivedRtt(int rtt_ms) { |
| + if (!config_.audio_network_adaptor_enabled) |
| + return; |
| + RTC_DCHECK(audio_network_adaptor_); |
| +// audio_network_adaptor_->SetRtt(rtt_ms); |
| + ApplyAudioNetworkAdaptor(); |
| +} |
| + |
| AudioEncoder::EncodedInfo AudioEncoderOpus::EncodeImpl( |
| uint32_t rtp_timestamp, |
| rtc::ArrayView<const int16_t> audio, |
| @@ -226,6 +267,9 @@ AudioEncoder::EncodedInfo AudioEncoderOpus::EncodeImpl( |
| }); |
| input_buffer_.clear(); |
| + // Will use new packet size for next encoding. |
| + config_.frame_size_ms = next_frame_size_ms_; |
| + |
| info.encoded_timestamp = first_timestamp_in_buffer_; |
| info.payload_type = config_.payload_type; |
| info.send_even_if_empty = true; // Allows Opus to send empty packets. |
| @@ -282,7 +326,44 @@ bool AudioEncoderOpus::RecreateEncoderInstance(const Config& config) { |
| WebRtcOpus_SetPacketLossRate( |
| inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5))); |
| config_ = config; |
| + |
| + num_channels_to_encode_ = 0; // Opus automatic mode. |
|
michaelt
2016/09/22 12:13:14
I think we should create a enum class for num_chan
minyue-webrtc
2016/09/27 08:30:36
I think we should not use "0", since audio network
|
| + next_frame_size_ms_ = config_.frame_size_ms; |
| + if (config_.audio_network_adaptor_enabled) { |
| + // TODO(minyue): Create AudioNetworkAdaptorImpl. |
| + } |
| return true; |
| } |
| +void AudioEncoderOpus::SetFrameLength(int frame_length_ms) { |
| + next_frame_size_ms_ = frame_length_ms; |
| +} |
| + |
| +void AudioEncoderOpus::SetNumChannelsToEncode(size_t num_channels_to_encode) { |
| + RTC_DCHECK_GT(num_channels_to_encode, 0u); |
| + RTC_DCHECK_LE(num_channels_to_encode, config_.num_channels); |
| + |
| + if (num_channels_to_encode_ == num_channels_to_encode) |
| + return; |
| + |
| +// RTC_CHECK_EQ(0, WebRtcOpus_SetForceChannels(inst_, num_channels_to_encode)); |
| + num_channels_to_encode_ = num_channels_to_encode; |
| +} |
| + |
| +void AudioEncoderOpus::ApplyAudioNetworkAdaptor() { |
| + auto config = audio_network_adaptor_->GetEncoderRuntimeConfig(); |
| + |
| + RTC_DCHECK(config.bitrate_bps && config.frame_length_ms && |
| + config.uplink_packet_loss_fraction && config.enable_fec && |
| + config.enable_dtx); |
| + RTC_DCHECK(*config.frame_length_ms == 20 || *config.frame_length_ms == 60); |
| + |
| + SetTargetBitrate(*config.bitrate_bps); |
| + SetFrameLength(*config.frame_length_ms); |
| + SetFec(*config.enable_fec); |
| + SetProjectedPacketLossRate(*config.uplink_packet_loss_fraction); |
| + SetDtx(*config.enable_dtx); |
| + SetNumChannelsToEncode(*config.num_channels); |
| +} |
| + |
| } // namespace webrtc |