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/checks.h" | 15 #include "webrtc/base/checks.h" |
16 #include "webrtc/base/exp_filter.h" | |
16 #include "webrtc/base/safe_conversions.h" | 17 #include "webrtc/base/safe_conversions.h" |
17 #include "webrtc/common_types.h" | 18 #include "webrtc/common_types.h" |
18 #include "webrtc/modules/audio_coding/audio_network_adaptor/audio_network_adapto r_impl.h" | 19 #include "webrtc/modules/audio_coding/audio_network_adaptor/audio_network_adapto r_impl.h" |
19 #include "webrtc/modules/audio_coding/audio_network_adaptor/controller_manager.h " | 20 #include "webrtc/modules/audio_coding/audio_network_adaptor/controller_manager.h " |
20 #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h" | 21 #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h" |
21 #include "webrtc/system_wrappers/include/clock.h" | 22 #include "webrtc/system_wrappers/include/clock.h" |
22 | 23 |
23 namespace webrtc { | 24 namespace webrtc { |
24 | 25 |
25 namespace { | 26 namespace { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
75 return kPacketLossRate5; | 76 return kPacketLossRate5; |
76 } else if (new_loss_rate >= kPacketLossRate1) { | 77 } else if (new_loss_rate >= kPacketLossRate1) { |
77 return kPacketLossRate1; | 78 return kPacketLossRate1; |
78 } else { | 79 } else { |
79 return 0.0; | 80 return 0.0; |
80 } | 81 } |
81 } | 82 } |
82 | 83 |
83 } // namespace | 84 } // namespace |
84 | 85 |
86 class AudioEncoderOpus::PacketLossFractionSmoother { | |
87 public: | |
88 explicit PacketLossFractionSmoother(const Clock* clock) | |
89 : clock_(clock), | |
90 last_sample_time_ms_(clock_->TimeInMilliseconds()), | |
91 smoother_(0.9999f) {} | |
the sun
2016/10/11 17:52:42
Would be nice with a comment about what the filter
minyue-webrtc
2016/10/12 11:27:35
I do not quite recall what that number means :P, t
| |
92 | |
93 // Gets the smoothed packet loss fraction. | |
94 float GetAverage() const { | |
95 float value = smoother_.filtered(); | |
96 return (value == rtc::ExpFilter::kValueUndefined) ? 0.0f : value; | |
97 } | |
98 | |
99 // Add new observation to the packet loss fraction smoother. | |
100 void AddSample(float packet_loss_fraction) { | |
101 int64_t now_ms = clock_->TimeInMilliseconds(); | |
102 smoother_.Apply(static_cast<float>(now_ms - last_sample_time_ms_), | |
103 packet_loss_fraction); | |
104 last_sample_time_ms_ = now_ms; | |
105 } | |
106 | |
107 private: | |
108 const Clock* const clock_; | |
109 int64_t last_sample_time_ms_; | |
110 | |
111 // An exponential filter is used to smooth the packet loss fraction. | |
112 rtc::ExpFilter smoother_; | |
113 }; | |
114 | |
85 AudioEncoderOpus::Config::Config() = default; | 115 AudioEncoderOpus::Config::Config() = default; |
86 AudioEncoderOpus::Config::Config(const Config&) = default; | 116 AudioEncoderOpus::Config::Config(const Config&) = default; |
87 AudioEncoderOpus::Config::~Config() = default; | 117 AudioEncoderOpus::Config::~Config() = default; |
88 auto AudioEncoderOpus::Config::operator=(const Config&) -> Config& = default; | 118 auto AudioEncoderOpus::Config::operator=(const Config&) -> Config& = default; |
89 | 119 |
90 bool AudioEncoderOpus::Config::IsOk() const { | 120 bool AudioEncoderOpus::Config::IsOk() const { |
91 if (frame_size_ms <= 0 || frame_size_ms % 10 != 0) | 121 if (frame_size_ms <= 0 || frame_size_ms % 10 != 0) |
92 return false; | 122 return false; |
93 if (num_channels != 1 && num_channels != 2) | 123 if (num_channels != 1 && num_channels != 2) |
94 return false; | 124 return false; |
(...skipping 11 matching lines...) Expand all Loading... | |
106 return *bitrate_bps; // Explicitly set value. | 136 return *bitrate_bps; // Explicitly set value. |
107 else | 137 else |
108 return num_channels == 1 ? 32000 : 64000; // Default value. | 138 return num_channels == 1 ? 32000 : 64000; // Default value. |
109 } | 139 } |
110 | 140 |
111 AudioEncoderOpus::AudioEncoderOpus( | 141 AudioEncoderOpus::AudioEncoderOpus( |
112 const Config& config, | 142 const Config& config, |
113 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator) | 143 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator) |
114 : packet_loss_rate_(0.0), | 144 : packet_loss_rate_(0.0), |
115 inst_(nullptr), | 145 inst_(nullptr), |
146 packet_loss_fraction_smoother_(new PacketLossFractionSmoother( | |
147 config.clock ? config.clock : Clock::GetRealTimeClock())), | |
116 audio_network_adaptor_creator_( | 148 audio_network_adaptor_creator_( |
117 audio_network_adaptor_creator | 149 audio_network_adaptor_creator |
118 ? audio_network_adaptor_creator | 150 ? std::move(audio_network_adaptor_creator) |
119 : [this](const std::string& config_string, const Clock* clock) { | 151 : [this](const std::string& config_string, const Clock* clock) { |
120 return DefaultAudioNetworkAdaptorCreator(config_string, | 152 return DefaultAudioNetworkAdaptorCreator(config_string, |
121 clock); | 153 clock); |
122 }) { | 154 }) { |
123 RTC_CHECK(RecreateEncoderInstance(config)); | 155 RTC_CHECK(RecreateEncoderInstance(config)); |
124 } | 156 } |
125 | 157 |
126 AudioEncoderOpus::AudioEncoderOpus(const CodecInst& codec_inst) | 158 AudioEncoderOpus::AudioEncoderOpus(const CodecInst& codec_inst) |
127 : AudioEncoderOpus(CreateConfig(codec_inst), nullptr) {} | 159 : AudioEncoderOpus(CreateConfig(codec_inst), nullptr) {} |
128 | 160 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
227 | 259 |
228 void AudioEncoderOpus::OnReceivedUplinkBandwidth(int uplink_bandwidth_bps) { | 260 void AudioEncoderOpus::OnReceivedUplinkBandwidth(int uplink_bandwidth_bps) { |
229 if (!audio_network_adaptor_) | 261 if (!audio_network_adaptor_) |
230 return; | 262 return; |
231 audio_network_adaptor_->SetUplinkBandwidth(uplink_bandwidth_bps); | 263 audio_network_adaptor_->SetUplinkBandwidth(uplink_bandwidth_bps); |
232 ApplyAudioNetworkAdaptor(); | 264 ApplyAudioNetworkAdaptor(); |
233 } | 265 } |
234 | 266 |
235 void AudioEncoderOpus::OnReceivedUplinkPacketLossFraction( | 267 void AudioEncoderOpus::OnReceivedUplinkPacketLossFraction( |
236 float uplink_packet_loss_fraction) { | 268 float uplink_packet_loss_fraction) { |
237 if (!audio_network_adaptor_) | 269 if (!audio_network_adaptor_) { |
238 return; | 270 packet_loss_fraction_smoother_->AddSample(uplink_packet_loss_fraction); |
271 float average_fraction_loss = packet_loss_fraction_smoother_->GetAverage(); | |
272 return SetProjectedPacketLossRate(average_fraction_loss); | |
273 } | |
239 audio_network_adaptor_->SetUplinkPacketLossFraction( | 274 audio_network_adaptor_->SetUplinkPacketLossFraction( |
240 uplink_packet_loss_fraction); | 275 uplink_packet_loss_fraction); |
241 ApplyAudioNetworkAdaptor(); | 276 ApplyAudioNetworkAdaptor(); |
242 } | 277 } |
243 | 278 |
244 void AudioEncoderOpus::OnReceivedTargetAudioBitrate( | 279 void AudioEncoderOpus::OnReceivedTargetAudioBitrate( |
245 int target_audio_bitrate_bps) { | 280 int target_audio_bitrate_bps) { |
246 if (!audio_network_adaptor_) | 281 if (!audio_network_adaptor_) |
247 return; | 282 return SetTargetBitrate(target_audio_bitrate_bps); |
248 audio_network_adaptor_->SetTargetAudioBitrate(target_audio_bitrate_bps); | 283 audio_network_adaptor_->SetTargetAudioBitrate(target_audio_bitrate_bps); |
249 ApplyAudioNetworkAdaptor(); | 284 ApplyAudioNetworkAdaptor(); |
250 } | 285 } |
251 | 286 |
252 void AudioEncoderOpus::OnReceivedRtt(int rtt_ms) { | 287 void AudioEncoderOpus::OnReceivedRtt(int rtt_ms) { |
253 if (!audio_network_adaptor_) | 288 if (!audio_network_adaptor_) |
254 return; | 289 return; |
255 audio_network_adaptor_->SetRtt(rtt_ms); | 290 audio_network_adaptor_->SetRtt(rtt_ms); |
256 ApplyAudioNetworkAdaptor(); | 291 ApplyAudioNetworkAdaptor(); |
257 } | 292 } |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
407 AudioNetworkAdaptorImpl::Config config; | 442 AudioNetworkAdaptorImpl::Config config; |
408 config.clock = clock; | 443 config.clock = clock; |
409 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( | 444 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( |
410 config, ControllerManagerImpl::Create( | 445 config, ControllerManagerImpl::Create( |
411 config_string, NumChannels(), kSupportedFrameLengths, | 446 config_string, NumChannels(), kSupportedFrameLengths, |
412 num_channels_to_encode_, next_frame_length_ms_, | 447 num_channels_to_encode_, next_frame_length_ms_, |
413 GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); | 448 GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); |
414 } | 449 } |
415 | 450 |
416 } // namespace webrtc | 451 } // namespace webrtc |
OLD | NEW |