blob: c4832a36de4a557e577e6e5d73476060598288ec [file] [log] [blame]
minyuecaa9cb22016-09-13 13:34:15 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/audio_network_adaptor/audio_network_adaptor_impl.h"
minyue161b3902016-09-22 14:16:55 -070012
minyue25f6a392016-09-22 22:23:20 -070013#include <utility>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/logging.h"
16#include "rtc_base/timeutils.h"
17#include "system_wrappers/include/field_trial.h"
minyue4b7c9522017-01-24 04:54:59 -080018
minyuecaa9cb22016-09-13 13:34:15 -070019namespace webrtc {
20
minyue4b7c9522017-01-24 04:54:59 -080021namespace {
22constexpr int kEventLogMinBitrateChangeBps = 5000;
23constexpr float kEventLogMinBitrateChangeFraction = 0.25;
24constexpr float kEventLogMinPacketLossChangeFraction = 0.5;
25} // namespace
26
michaelt92aef172017-04-18 00:11:48 -070027AudioNetworkAdaptorImpl::Config::Config() : event_log(nullptr){};
minyuecaa9cb22016-09-13 13:34:15 -070028
29AudioNetworkAdaptorImpl::Config::~Config() = default;
30
31AudioNetworkAdaptorImpl::AudioNetworkAdaptorImpl(
32 const Config& config,
minyue25f6a392016-09-22 22:23:20 -070033 std::unique_ptr<ControllerManager> controller_manager,
34 std::unique_ptr<DebugDumpWriter> debug_dump_writer)
35 : config_(config),
36 controller_manager_(std::move(controller_manager)),
minyue4b7c9522017-01-24 04:54:59 -080037 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)
ivoc17289092017-09-09 08:45:40 -070044 : 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")) {
minyuecaa9cb22016-09-13 13:34:15 -070055 RTC_DCHECK(controller_manager_);
56}
57
58AudioNetworkAdaptorImpl::~AudioNetworkAdaptorImpl() = default;
59
60void AudioNetworkAdaptorImpl::SetUplinkBandwidth(int uplink_bandwidth_bps) {
Oskar Sundbom12ab00b2017-11-16 15:31:38 +010061 last_metrics_.uplink_bandwidth_bps = uplink_bandwidth_bps;
minyue25f6a392016-09-22 22:23:20 -070062 DumpNetworkMetrics();
minyuea6a6d652017-01-30 10:50:00 -080063
64 Controller::NetworkMetrics network_metrics;
Oskar Sundbom12ab00b2017-11-16 15:31:38 +010065 network_metrics.uplink_bandwidth_bps = uplink_bandwidth_bps;
minyuea6a6d652017-01-30 10:50:00 -080066 UpdateNetworkMetrics(network_metrics);
minyuecaa9cb22016-09-13 13:34:15 -070067}
68
69void AudioNetworkAdaptorImpl::SetUplinkPacketLossFraction(
70 float uplink_packet_loss_fraction) {
Oskar Sundbom12ab00b2017-11-16 15:31:38 +010071 last_metrics_.uplink_packet_loss_fraction = uplink_packet_loss_fraction;
minyue25f6a392016-09-22 22:23:20 -070072 DumpNetworkMetrics();
minyuea6a6d652017-01-30 10:50:00 -080073
74 Controller::NetworkMetrics network_metrics;
Oskar Sundbom12ab00b2017-11-16 15:31:38 +010075 network_metrics.uplink_packet_loss_fraction = uplink_packet_loss_fraction;
minyuea6a6d652017-01-30 10:50:00 -080076 UpdateNetworkMetrics(network_metrics);
minyue25f6a392016-09-22 22:23:20 -070077}
minyuecaa9cb22016-09-13 13:34:15 -070078
elad.alondadb4dc2017-03-23 15:29:50 -070079void AudioNetworkAdaptorImpl::SetUplinkRecoverablePacketLossFraction(
80 float uplink_recoverable_packet_loss_fraction) {
81 last_metrics_.uplink_recoverable_packet_loss_fraction =
Oskar Sundbom12ab00b2017-11-16 15:31:38 +010082 uplink_recoverable_packet_loss_fraction;
elad.alondadb4dc2017-03-23 15:29:50 -070083 DumpNetworkMetrics();
84
85 Controller::NetworkMetrics network_metrics;
86 network_metrics.uplink_recoverable_packet_loss_fraction =
Oskar Sundbom12ab00b2017-11-16 15:31:38 +010087 uplink_recoverable_packet_loss_fraction;
elad.alondadb4dc2017-03-23 15:29:50 -070088 UpdateNetworkMetrics(network_metrics);
89}
90
minyuec9e80ee2016-11-29 13:00:28 -080091void AudioNetworkAdaptorImpl::SetRtt(int rtt_ms) {
Oskar Sundbom12ab00b2017-11-16 15:31:38 +010092 last_metrics_.rtt_ms = rtt_ms;
minyuec9e80ee2016-11-29 13:00:28 -080093 DumpNetworkMetrics();
minyuea6a6d652017-01-30 10:50:00 -080094
95 Controller::NetworkMetrics network_metrics;
Oskar Sundbom12ab00b2017-11-16 15:31:38 +010096 network_metrics.rtt_ms = rtt_ms;
minyuea6a6d652017-01-30 10:50:00 -080097 UpdateNetworkMetrics(network_metrics);
minyuec9e80ee2016-11-29 13:00:28 -080098}
99
minyuee5e632f2016-09-27 12:54:19 -0700100void AudioNetworkAdaptorImpl::SetTargetAudioBitrate(
101 int target_audio_bitrate_bps) {
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100102 last_metrics_.target_audio_bitrate_bps = target_audio_bitrate_bps;
minyuee5e632f2016-09-27 12:54:19 -0700103 DumpNetworkMetrics();
minyuea6a6d652017-01-30 10:50:00 -0800104
105 Controller::NetworkMetrics network_metrics;
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100106 network_metrics.target_audio_bitrate_bps = target_audio_bitrate_bps;
minyuea6a6d652017-01-30 10:50:00 -0800107 UpdateNetworkMetrics(network_metrics);
minyuee5e632f2016-09-27 12:54:19 -0700108}
109
minyuec9e80ee2016-11-29 13:00:28 -0800110void AudioNetworkAdaptorImpl::SetOverhead(size_t overhead_bytes_per_packet) {
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100111 last_metrics_.overhead_bytes_per_packet = overhead_bytes_per_packet;
minyue25f6a392016-09-22 22:23:20 -0700112 DumpNetworkMetrics();
minyuea6a6d652017-01-30 10:50:00 -0800113
114 Controller::NetworkMetrics network_metrics;
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100115 network_metrics.overhead_bytes_per_packet = overhead_bytes_per_packet;
minyuea6a6d652017-01-30 10:50:00 -0800116 UpdateNetworkMetrics(network_metrics);
minyuecaa9cb22016-09-13 13:34:15 -0700117}
118
michaeltcde46b72017-04-06 05:59:10 -0700119AudioEncoderRuntimeConfig AudioNetworkAdaptorImpl::GetEncoderRuntimeConfig() {
120 AudioEncoderRuntimeConfig config;
minyuecaa9cb22016-09-13 13:34:15 -0700121 for (auto& controller :
122 controller_manager_->GetSortedControllers(last_metrics_))
minyuea6a6d652017-01-30 10:50:00 -0800123 controller->MakeDecision(&config);
minyuecaa9cb22016-09-13 13:34:15 -0700124
ivoc17289092017-09-09 08:45:40 -0700125 // Update ANA stats.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200126 auto increment_opt = [](absl::optional<uint32_t>& a) {
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100127 a = a.value_or(0) + 1;
ivoc17289092017-09-09 08:45:40 -0700128 };
129 if (prev_config_) {
130 if (config.bitrate_bps != prev_config_->bitrate_bps) {
131 increment_opt(stats_.bitrate_action_counter);
132 }
133 if (config.enable_dtx != prev_config_->enable_dtx) {
134 increment_opt(stats_.dtx_action_counter);
135 }
136 if (config.enable_fec != prev_config_->enable_fec) {
137 increment_opt(stats_.fec_action_counter);
138 }
139 if (config.frame_length_ms && prev_config_->frame_length_ms) {
140 if (*config.frame_length_ms > *prev_config_->frame_length_ms) {
141 increment_opt(stats_.frame_length_increase_counter);
142 } else if (*config.frame_length_ms < *prev_config_->frame_length_ms) {
143 increment_opt(stats_.frame_length_decrease_counter);
144 }
145 }
146 if (config.num_channels != prev_config_->num_channels) {
147 increment_opt(stats_.channel_action_counter);
148 }
149 if (config.uplink_packet_loss_fraction) {
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100150 stats_.uplink_packet_loss_fraction = *config.uplink_packet_loss_fraction;
ivoc17289092017-09-09 08:45:40 -0700151 }
152 }
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100153 prev_config_ = config;
ivoc17289092017-09-09 08:45:40 -0700154
155 // Prevent certain controllers from taking action (determined by field trials)
156 if (!enable_bitrate_adaptation_ && config.bitrate_bps) {
157 config.bitrate_bps.reset();
158 }
159 if (!enable_dtx_adaptation_ && config.enable_dtx) {
160 config.enable_dtx.reset();
161 }
162 if (!enable_fec_adaptation_ && config.enable_fec) {
163 config.enable_fec.reset();
164 config.uplink_packet_loss_fraction.reset();
165 }
166 if (!enable_frame_length_adaptation_ && config.frame_length_ms) {
167 config.frame_length_ms.reset();
168 }
169 if (!enable_channel_adaptation_ && config.num_channels) {
170 config.num_channels.reset();
171 }
172
minyue25f6a392016-09-22 22:23:20 -0700173 if (debug_dump_writer_)
michaelt92aef172017-04-18 00:11:48 -0700174 debug_dump_writer_->DumpEncoderRuntimeConfig(config, rtc::TimeMillis());
minyuecaa9cb22016-09-13 13:34:15 -0700175
minyue4b7c9522017-01-24 04:54:59 -0800176 if (event_log_writer_)
177 event_log_writer_->MaybeLogEncoderConfig(config);
178
minyuecaa9cb22016-09-13 13:34:15 -0700179 return config;
180}
181
minyuecaa9cb22016-09-13 13:34:15 -0700182void AudioNetworkAdaptorImpl::StartDebugDump(FILE* file_handle) {
minyue25f6a392016-09-22 22:23:20 -0700183 debug_dump_writer_ = DebugDumpWriter::Create(file_handle);
184}
185
186void AudioNetworkAdaptorImpl::StopDebugDump() {
187 debug_dump_writer_.reset(nullptr);
188}
189
ivoce1198e02017-09-08 08:13:19 -0700190ANAStats AudioNetworkAdaptorImpl::GetStats() const {
ivoc17289092017-09-09 08:45:40 -0700191 return stats_;
ivoce1198e02017-09-08 08:13:19 -0700192}
193
minyue25f6a392016-09-22 22:23:20 -0700194void AudioNetworkAdaptorImpl::DumpNetworkMetrics() {
195 if (debug_dump_writer_)
michaelt92aef172017-04-18 00:11:48 -0700196 debug_dump_writer_->DumpNetworkMetrics(last_metrics_, rtc::TimeMillis());
minyuecaa9cb22016-09-13 13:34:15 -0700197}
198
minyuea6a6d652017-01-30 10:50:00 -0800199void AudioNetworkAdaptorImpl::UpdateNetworkMetrics(
200 const Controller::NetworkMetrics& network_metrics) {
201 for (auto& controller : controller_manager_->GetControllers())
202 controller->UpdateNetworkMetrics(network_metrics);
203}
204
minyuecaa9cb22016-09-13 13:34:15 -0700205} // namespace webrtc