minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 11 | #include "modules/audio_coding/audio_network_adaptor/audio_network_adaptor_impl.h" |
minyue | 161b390 | 2016-09-22 14:16:55 -0700 | [diff] [blame] | 12 | |
minyue | 25f6a39 | 2016-09-22 22:23:20 -0700 | [diff] [blame] | 13 | #include <utility> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 15 | #include "rtc_base/logging.h" |
| 16 | #include "rtc_base/timeutils.h" |
| 17 | #include "system_wrappers/include/field_trial.h" |
minyue | 4b7c952 | 2017-01-24 04:54:59 -0800 | [diff] [blame] | 18 | |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 19 | namespace webrtc { |
| 20 | |
minyue | 4b7c952 | 2017-01-24 04:54:59 -0800 | [diff] [blame] | 21 | namespace { |
| 22 | constexpr int kEventLogMinBitrateChangeBps = 5000; |
| 23 | constexpr float kEventLogMinBitrateChangeFraction = 0.25; |
| 24 | constexpr float kEventLogMinPacketLossChangeFraction = 0.5; |
| 25 | } // namespace |
| 26 | |
michaelt | 92aef17 | 2017-04-18 00:11:48 -0700 | [diff] [blame] | 27 | AudioNetworkAdaptorImpl::Config::Config() : event_log(nullptr){}; |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 28 | |
| 29 | AudioNetworkAdaptorImpl::Config::~Config() = default; |
| 30 | |
| 31 | AudioNetworkAdaptorImpl::AudioNetworkAdaptorImpl( |
| 32 | const Config& config, |
minyue | 25f6a39 | 2016-09-22 22:23:20 -0700 | [diff] [blame] | 33 | std::unique_ptr<ControllerManager> controller_manager, |
| 34 | std::unique_ptr<DebugDumpWriter> debug_dump_writer) |
| 35 | : config_(config), |
| 36 | controller_manager_(std::move(controller_manager)), |
minyue | 4b7c952 | 2017-01-24 04:54:59 -0800 | [diff] [blame] | 37 | debug_dump_writer_(std::move(debug_dump_writer)), |
| 38 | event_log_writer_( |
| 39 | config.event_log |
| 40 | ? new EventLogWriter(config.event_log, |
| 41 | kEventLogMinBitrateChangeBps, |
| 42 | kEventLogMinBitrateChangeFraction, |
| 43 | kEventLogMinPacketLossChangeFraction) |
ivoc | 1728909 | 2017-09-09 08:45:40 -0700 | [diff] [blame] | 44 | : nullptr), |
| 45 | enable_bitrate_adaptation_( |
| 46 | webrtc::field_trial::IsEnabled("WebRTC-Audio-BitrateAdaptation")), |
| 47 | enable_dtx_adaptation_( |
| 48 | webrtc::field_trial::IsEnabled("WebRTC-Audio-DtxAdaptation")), |
| 49 | enable_fec_adaptation_( |
| 50 | webrtc::field_trial::IsEnabled("WebRTC-Audio-FecAdaptation")), |
| 51 | enable_channel_adaptation_( |
| 52 | webrtc::field_trial::IsEnabled("WebRTC-Audio-ChannelAdaptation")), |
| 53 | enable_frame_length_adaptation_(webrtc::field_trial::IsEnabled( |
| 54 | "WebRTC-Audio-FrameLengthAdaptation")) { |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 55 | RTC_DCHECK(controller_manager_); |
| 56 | } |
| 57 | |
| 58 | AudioNetworkAdaptorImpl::~AudioNetworkAdaptorImpl() = default; |
| 59 | |
| 60 | void AudioNetworkAdaptorImpl::SetUplinkBandwidth(int uplink_bandwidth_bps) { |
| 61 | last_metrics_.uplink_bandwidth_bps = rtc::Optional<int>(uplink_bandwidth_bps); |
minyue | 25f6a39 | 2016-09-22 22:23:20 -0700 | [diff] [blame] | 62 | DumpNetworkMetrics(); |
minyue | a6a6d65 | 2017-01-30 10:50:00 -0800 | [diff] [blame] | 63 | |
| 64 | Controller::NetworkMetrics network_metrics; |
| 65 | network_metrics.uplink_bandwidth_bps = |
| 66 | rtc::Optional<int>(uplink_bandwidth_bps); |
| 67 | UpdateNetworkMetrics(network_metrics); |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void AudioNetworkAdaptorImpl::SetUplinkPacketLossFraction( |
| 71 | float uplink_packet_loss_fraction) { |
| 72 | last_metrics_.uplink_packet_loss_fraction = |
| 73 | rtc::Optional<float>(uplink_packet_loss_fraction); |
minyue | 25f6a39 | 2016-09-22 22:23:20 -0700 | [diff] [blame] | 74 | DumpNetworkMetrics(); |
minyue | a6a6d65 | 2017-01-30 10:50:00 -0800 | [diff] [blame] | 75 | |
| 76 | Controller::NetworkMetrics network_metrics; |
| 77 | network_metrics.uplink_packet_loss_fraction = |
| 78 | rtc::Optional<float>(uplink_packet_loss_fraction); |
| 79 | UpdateNetworkMetrics(network_metrics); |
minyue | 25f6a39 | 2016-09-22 22:23:20 -0700 | [diff] [blame] | 80 | } |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 81 | |
elad.alon | dadb4dc | 2017-03-23 15:29:50 -0700 | [diff] [blame] | 82 | void AudioNetworkAdaptorImpl::SetUplinkRecoverablePacketLossFraction( |
| 83 | float uplink_recoverable_packet_loss_fraction) { |
| 84 | last_metrics_.uplink_recoverable_packet_loss_fraction = |
| 85 | rtc::Optional<float>(uplink_recoverable_packet_loss_fraction); |
| 86 | DumpNetworkMetrics(); |
| 87 | |
| 88 | Controller::NetworkMetrics network_metrics; |
| 89 | network_metrics.uplink_recoverable_packet_loss_fraction = |
| 90 | rtc::Optional<float>(uplink_recoverable_packet_loss_fraction); |
| 91 | UpdateNetworkMetrics(network_metrics); |
| 92 | } |
| 93 | |
minyue | c9e80ee | 2016-11-29 13:00:28 -0800 | [diff] [blame] | 94 | void AudioNetworkAdaptorImpl::SetRtt(int rtt_ms) { |
| 95 | last_metrics_.rtt_ms = rtc::Optional<int>(rtt_ms); |
| 96 | DumpNetworkMetrics(); |
minyue | a6a6d65 | 2017-01-30 10:50:00 -0800 | [diff] [blame] | 97 | |
| 98 | Controller::NetworkMetrics network_metrics; |
| 99 | network_metrics.rtt_ms = rtc::Optional<int>(rtt_ms); |
| 100 | UpdateNetworkMetrics(network_metrics); |
minyue | c9e80ee | 2016-11-29 13:00:28 -0800 | [diff] [blame] | 101 | } |
| 102 | |
minyue | e5e632f | 2016-09-27 12:54:19 -0700 | [diff] [blame] | 103 | void AudioNetworkAdaptorImpl::SetTargetAudioBitrate( |
| 104 | int target_audio_bitrate_bps) { |
| 105 | last_metrics_.target_audio_bitrate_bps = |
| 106 | rtc::Optional<int>(target_audio_bitrate_bps); |
| 107 | DumpNetworkMetrics(); |
minyue | a6a6d65 | 2017-01-30 10:50:00 -0800 | [diff] [blame] | 108 | |
| 109 | Controller::NetworkMetrics network_metrics; |
| 110 | network_metrics.target_audio_bitrate_bps = |
| 111 | rtc::Optional<int>(target_audio_bitrate_bps); |
| 112 | UpdateNetworkMetrics(network_metrics); |
minyue | e5e632f | 2016-09-27 12:54:19 -0700 | [diff] [blame] | 113 | } |
| 114 | |
minyue | c9e80ee | 2016-11-29 13:00:28 -0800 | [diff] [blame] | 115 | void AudioNetworkAdaptorImpl::SetOverhead(size_t overhead_bytes_per_packet) { |
| 116 | last_metrics_.overhead_bytes_per_packet = |
| 117 | rtc::Optional<size_t>(overhead_bytes_per_packet); |
minyue | 25f6a39 | 2016-09-22 22:23:20 -0700 | [diff] [blame] | 118 | DumpNetworkMetrics(); |
minyue | a6a6d65 | 2017-01-30 10:50:00 -0800 | [diff] [blame] | 119 | |
| 120 | Controller::NetworkMetrics network_metrics; |
| 121 | network_metrics.overhead_bytes_per_packet = |
| 122 | rtc::Optional<size_t>(overhead_bytes_per_packet); |
| 123 | UpdateNetworkMetrics(network_metrics); |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 124 | } |
| 125 | |
michaelt | cde46b7 | 2017-04-06 05:59:10 -0700 | [diff] [blame] | 126 | AudioEncoderRuntimeConfig AudioNetworkAdaptorImpl::GetEncoderRuntimeConfig() { |
| 127 | AudioEncoderRuntimeConfig config; |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 128 | for (auto& controller : |
| 129 | controller_manager_->GetSortedControllers(last_metrics_)) |
minyue | a6a6d65 | 2017-01-30 10:50:00 -0800 | [diff] [blame] | 130 | controller->MakeDecision(&config); |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 131 | |
ivoc | 1728909 | 2017-09-09 08:45:40 -0700 | [diff] [blame] | 132 | // Update ANA stats. |
| 133 | auto increment_opt = [](rtc::Optional<uint32_t>& a) { |
| 134 | a = rtc::Optional<uint32_t>(a.value_or(0) + 1); |
| 135 | }; |
| 136 | if (prev_config_) { |
| 137 | if (config.bitrate_bps != prev_config_->bitrate_bps) { |
| 138 | increment_opt(stats_.bitrate_action_counter); |
| 139 | } |
| 140 | if (config.enable_dtx != prev_config_->enable_dtx) { |
| 141 | increment_opt(stats_.dtx_action_counter); |
| 142 | } |
| 143 | if (config.enable_fec != prev_config_->enable_fec) { |
| 144 | increment_opt(stats_.fec_action_counter); |
| 145 | } |
| 146 | if (config.frame_length_ms && prev_config_->frame_length_ms) { |
| 147 | if (*config.frame_length_ms > *prev_config_->frame_length_ms) { |
| 148 | increment_opt(stats_.frame_length_increase_counter); |
| 149 | } else if (*config.frame_length_ms < *prev_config_->frame_length_ms) { |
| 150 | increment_opt(stats_.frame_length_decrease_counter); |
| 151 | } |
| 152 | } |
| 153 | if (config.num_channels != prev_config_->num_channels) { |
| 154 | increment_opt(stats_.channel_action_counter); |
| 155 | } |
| 156 | if (config.uplink_packet_loss_fraction) { |
| 157 | stats_.uplink_packet_loss_fraction = |
| 158 | rtc::Optional<float>(*config.uplink_packet_loss_fraction); |
| 159 | } |
| 160 | } |
| 161 | prev_config_ = rtc::Optional<AudioEncoderRuntimeConfig>(config); |
| 162 | |
| 163 | // Prevent certain controllers from taking action (determined by field trials) |
| 164 | if (!enable_bitrate_adaptation_ && config.bitrate_bps) { |
| 165 | config.bitrate_bps.reset(); |
| 166 | } |
| 167 | if (!enable_dtx_adaptation_ && config.enable_dtx) { |
| 168 | config.enable_dtx.reset(); |
| 169 | } |
| 170 | if (!enable_fec_adaptation_ && config.enable_fec) { |
| 171 | config.enable_fec.reset(); |
| 172 | config.uplink_packet_loss_fraction.reset(); |
| 173 | } |
| 174 | if (!enable_frame_length_adaptation_ && config.frame_length_ms) { |
| 175 | config.frame_length_ms.reset(); |
| 176 | } |
| 177 | if (!enable_channel_adaptation_ && config.num_channels) { |
| 178 | config.num_channels.reset(); |
| 179 | } |
| 180 | |
minyue | 25f6a39 | 2016-09-22 22:23:20 -0700 | [diff] [blame] | 181 | if (debug_dump_writer_) |
michaelt | 92aef17 | 2017-04-18 00:11:48 -0700 | [diff] [blame] | 182 | debug_dump_writer_->DumpEncoderRuntimeConfig(config, rtc::TimeMillis()); |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 183 | |
minyue | 4b7c952 | 2017-01-24 04:54:59 -0800 | [diff] [blame] | 184 | if (event_log_writer_) |
| 185 | event_log_writer_->MaybeLogEncoderConfig(config); |
| 186 | |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 187 | return config; |
| 188 | } |
| 189 | |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 190 | void AudioNetworkAdaptorImpl::StartDebugDump(FILE* file_handle) { |
minyue | 25f6a39 | 2016-09-22 22:23:20 -0700 | [diff] [blame] | 191 | debug_dump_writer_ = DebugDumpWriter::Create(file_handle); |
| 192 | } |
| 193 | |
| 194 | void AudioNetworkAdaptorImpl::StopDebugDump() { |
| 195 | debug_dump_writer_.reset(nullptr); |
| 196 | } |
| 197 | |
ivoc | e1198e0 | 2017-09-08 08:13:19 -0700 | [diff] [blame] | 198 | ANAStats AudioNetworkAdaptorImpl::GetStats() const { |
ivoc | 1728909 | 2017-09-09 08:45:40 -0700 | [diff] [blame] | 199 | return stats_; |
ivoc | e1198e0 | 2017-09-08 08:13:19 -0700 | [diff] [blame] | 200 | } |
| 201 | |
minyue | 25f6a39 | 2016-09-22 22:23:20 -0700 | [diff] [blame] | 202 | void AudioNetworkAdaptorImpl::DumpNetworkMetrics() { |
| 203 | if (debug_dump_writer_) |
michaelt | 92aef17 | 2017-04-18 00:11:48 -0700 | [diff] [blame] | 204 | debug_dump_writer_->DumpNetworkMetrics(last_metrics_, rtc::TimeMillis()); |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 205 | } |
| 206 | |
minyue | a6a6d65 | 2017-01-30 10:50:00 -0800 | [diff] [blame] | 207 | void AudioNetworkAdaptorImpl::UpdateNetworkMetrics( |
| 208 | const Controller::NetworkMetrics& network_metrics) { |
| 209 | for (auto& controller : controller_manager_->GetControllers()) |
| 210 | controller->UpdateNetworkMetrics(network_metrics); |
| 211 | } |
| 212 | |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 213 | } // namespace webrtc |