blob: 54423e67b99b8ca81829448a4e565a35af7ece08 [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) {
61 last_metrics_.uplink_bandwidth_bps = rtc::Optional<int>(uplink_bandwidth_bps);
minyue25f6a392016-09-22 22:23:20 -070062 DumpNetworkMetrics();
minyuea6a6d652017-01-30 10:50:00 -080063
64 Controller::NetworkMetrics network_metrics;
65 network_metrics.uplink_bandwidth_bps =
66 rtc::Optional<int>(uplink_bandwidth_bps);
67 UpdateNetworkMetrics(network_metrics);
minyuecaa9cb22016-09-13 13:34:15 -070068}
69
70void AudioNetworkAdaptorImpl::SetUplinkPacketLossFraction(
71 float uplink_packet_loss_fraction) {
72 last_metrics_.uplink_packet_loss_fraction =
73 rtc::Optional<float>(uplink_packet_loss_fraction);
minyue25f6a392016-09-22 22:23:20 -070074 DumpNetworkMetrics();
minyuea6a6d652017-01-30 10:50:00 -080075
76 Controller::NetworkMetrics network_metrics;
77 network_metrics.uplink_packet_loss_fraction =
78 rtc::Optional<float>(uplink_packet_loss_fraction);
79 UpdateNetworkMetrics(network_metrics);
minyue25f6a392016-09-22 22:23:20 -070080}
minyuecaa9cb22016-09-13 13:34:15 -070081
elad.alondadb4dc2017-03-23 15:29:50 -070082void 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
minyuec9e80ee2016-11-29 13:00:28 -080094void AudioNetworkAdaptorImpl::SetRtt(int rtt_ms) {
95 last_metrics_.rtt_ms = rtc::Optional<int>(rtt_ms);
96 DumpNetworkMetrics();
minyuea6a6d652017-01-30 10:50:00 -080097
98 Controller::NetworkMetrics network_metrics;
99 network_metrics.rtt_ms = rtc::Optional<int>(rtt_ms);
100 UpdateNetworkMetrics(network_metrics);
minyuec9e80ee2016-11-29 13:00:28 -0800101}
102
minyuee5e632f2016-09-27 12:54:19 -0700103void 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();
minyuea6a6d652017-01-30 10:50:00 -0800108
109 Controller::NetworkMetrics network_metrics;
110 network_metrics.target_audio_bitrate_bps =
111 rtc::Optional<int>(target_audio_bitrate_bps);
112 UpdateNetworkMetrics(network_metrics);
minyuee5e632f2016-09-27 12:54:19 -0700113}
114
minyuec9e80ee2016-11-29 13:00:28 -0800115void AudioNetworkAdaptorImpl::SetOverhead(size_t overhead_bytes_per_packet) {
116 last_metrics_.overhead_bytes_per_packet =
117 rtc::Optional<size_t>(overhead_bytes_per_packet);
minyue25f6a392016-09-22 22:23:20 -0700118 DumpNetworkMetrics();
minyuea6a6d652017-01-30 10:50:00 -0800119
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);
minyuecaa9cb22016-09-13 13:34:15 -0700124}
125
michaeltcde46b72017-04-06 05:59:10 -0700126AudioEncoderRuntimeConfig AudioNetworkAdaptorImpl::GetEncoderRuntimeConfig() {
127 AudioEncoderRuntimeConfig config;
minyuecaa9cb22016-09-13 13:34:15 -0700128 for (auto& controller :
129 controller_manager_->GetSortedControllers(last_metrics_))
minyuea6a6d652017-01-30 10:50:00 -0800130 controller->MakeDecision(&config);
minyuecaa9cb22016-09-13 13:34:15 -0700131
ivoc17289092017-09-09 08:45:40 -0700132 // 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
minyue25f6a392016-09-22 22:23:20 -0700181 if (debug_dump_writer_)
michaelt92aef172017-04-18 00:11:48 -0700182 debug_dump_writer_->DumpEncoderRuntimeConfig(config, rtc::TimeMillis());
minyuecaa9cb22016-09-13 13:34:15 -0700183
minyue4b7c9522017-01-24 04:54:59 -0800184 if (event_log_writer_)
185 event_log_writer_->MaybeLogEncoderConfig(config);
186
minyuecaa9cb22016-09-13 13:34:15 -0700187 return config;
188}
189
minyuecaa9cb22016-09-13 13:34:15 -0700190void AudioNetworkAdaptorImpl::StartDebugDump(FILE* file_handle) {
minyue25f6a392016-09-22 22:23:20 -0700191 debug_dump_writer_ = DebugDumpWriter::Create(file_handle);
192}
193
194void AudioNetworkAdaptorImpl::StopDebugDump() {
195 debug_dump_writer_.reset(nullptr);
196}
197
ivoce1198e02017-09-08 08:13:19 -0700198ANAStats AudioNetworkAdaptorImpl::GetStats() const {
ivoc17289092017-09-09 08:45:40 -0700199 return stats_;
ivoce1198e02017-09-08 08:13:19 -0700200}
201
minyue25f6a392016-09-22 22:23:20 -0700202void AudioNetworkAdaptorImpl::DumpNetworkMetrics() {
203 if (debug_dump_writer_)
michaelt92aef172017-04-18 00:11:48 -0700204 debug_dump_writer_->DumpNetworkMetrics(last_metrics_, rtc::TimeMillis());
minyuecaa9cb22016-09-13 13:34:15 -0700205}
206
minyuea6a6d652017-01-30 10:50:00 -0800207void AudioNetworkAdaptorImpl::UpdateNetworkMetrics(
208 const Controller::NetworkMetrics& network_metrics) {
209 for (auto& controller : controller_manager_->GetControllers())
210 controller->UpdateNetworkMetrics(network_metrics);
211}
212
minyuecaa9cb22016-09-13 13:34:15 -0700213} // namespace webrtc