blob: 85084c873d5a5879d636ede03a25d2de5d5ced04 [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
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <stdint.h>
minyue25f6a392016-09-22 22:23:20 -070014#include <utility>
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <vector>
minyue25f6a392016-09-22 22:23:20 -070016
Yves Gerey988cc082018-10-23 12:03:01 +020017#include "modules/audio_coding/audio_network_adaptor/controller_manager.h"
18#include "modules/audio_coding/audio_network_adaptor/debug_dump_writer.h"
19#include "modules/audio_coding/audio_network_adaptor/event_log_writer.h"
20#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/timeutils.h"
22#include "system_wrappers/include/field_trial.h"
minyue4b7c9522017-01-24 04:54:59 -080023
minyuecaa9cb22016-09-13 13:34:15 -070024namespace webrtc {
25
minyue4b7c9522017-01-24 04:54:59 -080026namespace {
27constexpr int kEventLogMinBitrateChangeBps = 5000;
28constexpr float kEventLogMinBitrateChangeFraction = 0.25;
29constexpr float kEventLogMinPacketLossChangeFraction = 0.5;
30} // namespace
31
michaelt92aef172017-04-18 00:11:48 -070032AudioNetworkAdaptorImpl::Config::Config() : event_log(nullptr){};
minyuecaa9cb22016-09-13 13:34:15 -070033
34AudioNetworkAdaptorImpl::Config::~Config() = default;
35
36AudioNetworkAdaptorImpl::AudioNetworkAdaptorImpl(
37 const Config& config,
minyue25f6a392016-09-22 22:23:20 -070038 std::unique_ptr<ControllerManager> controller_manager,
39 std::unique_ptr<DebugDumpWriter> debug_dump_writer)
40 : config_(config),
41 controller_manager_(std::move(controller_manager)),
minyue4b7c9522017-01-24 04:54:59 -080042 debug_dump_writer_(std::move(debug_dump_writer)),
43 event_log_writer_(
44 config.event_log
45 ? new EventLogWriter(config.event_log,
46 kEventLogMinBitrateChangeBps,
47 kEventLogMinBitrateChangeFraction,
48 kEventLogMinPacketLossChangeFraction)
ivoc17289092017-09-09 08:45:40 -070049 : nullptr),
50 enable_bitrate_adaptation_(
51 webrtc::field_trial::IsEnabled("WebRTC-Audio-BitrateAdaptation")),
52 enable_dtx_adaptation_(
53 webrtc::field_trial::IsEnabled("WebRTC-Audio-DtxAdaptation")),
54 enable_fec_adaptation_(
55 webrtc::field_trial::IsEnabled("WebRTC-Audio-FecAdaptation")),
56 enable_channel_adaptation_(
57 webrtc::field_trial::IsEnabled("WebRTC-Audio-ChannelAdaptation")),
58 enable_frame_length_adaptation_(webrtc::field_trial::IsEnabled(
59 "WebRTC-Audio-FrameLengthAdaptation")) {
minyuecaa9cb22016-09-13 13:34:15 -070060 RTC_DCHECK(controller_manager_);
61}
62
63AudioNetworkAdaptorImpl::~AudioNetworkAdaptorImpl() = default;
64
65void AudioNetworkAdaptorImpl::SetUplinkBandwidth(int uplink_bandwidth_bps) {
Oskar Sundbom12ab00b2017-11-16 15:31:38 +010066 last_metrics_.uplink_bandwidth_bps = uplink_bandwidth_bps;
minyue25f6a392016-09-22 22:23:20 -070067 DumpNetworkMetrics();
minyuea6a6d652017-01-30 10:50:00 -080068
69 Controller::NetworkMetrics network_metrics;
Oskar Sundbom12ab00b2017-11-16 15:31:38 +010070 network_metrics.uplink_bandwidth_bps = uplink_bandwidth_bps;
minyuea6a6d652017-01-30 10:50:00 -080071 UpdateNetworkMetrics(network_metrics);
minyuecaa9cb22016-09-13 13:34:15 -070072}
73
74void AudioNetworkAdaptorImpl::SetUplinkPacketLossFraction(
75 float uplink_packet_loss_fraction) {
Oskar Sundbom12ab00b2017-11-16 15:31:38 +010076 last_metrics_.uplink_packet_loss_fraction = uplink_packet_loss_fraction;
minyue25f6a392016-09-22 22:23:20 -070077 DumpNetworkMetrics();
minyuea6a6d652017-01-30 10:50:00 -080078
79 Controller::NetworkMetrics network_metrics;
Oskar Sundbom12ab00b2017-11-16 15:31:38 +010080 network_metrics.uplink_packet_loss_fraction = uplink_packet_loss_fraction;
minyuea6a6d652017-01-30 10:50:00 -080081 UpdateNetworkMetrics(network_metrics);
minyue25f6a392016-09-22 22:23:20 -070082}
minyuecaa9cb22016-09-13 13:34:15 -070083
elad.alondadb4dc2017-03-23 15:29:50 -070084void AudioNetworkAdaptorImpl::SetUplinkRecoverablePacketLossFraction(
85 float uplink_recoverable_packet_loss_fraction) {
86 last_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 DumpNetworkMetrics();
89
90 Controller::NetworkMetrics network_metrics;
91 network_metrics.uplink_recoverable_packet_loss_fraction =
Oskar Sundbom12ab00b2017-11-16 15:31:38 +010092 uplink_recoverable_packet_loss_fraction;
elad.alondadb4dc2017-03-23 15:29:50 -070093 UpdateNetworkMetrics(network_metrics);
94}
95
minyuec9e80ee2016-11-29 13:00:28 -080096void AudioNetworkAdaptorImpl::SetRtt(int rtt_ms) {
Oskar Sundbom12ab00b2017-11-16 15:31:38 +010097 last_metrics_.rtt_ms = rtt_ms;
minyuec9e80ee2016-11-29 13:00:28 -080098 DumpNetworkMetrics();
minyuea6a6d652017-01-30 10:50:00 -080099
100 Controller::NetworkMetrics network_metrics;
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100101 network_metrics.rtt_ms = rtt_ms;
minyuea6a6d652017-01-30 10:50:00 -0800102 UpdateNetworkMetrics(network_metrics);
minyuec9e80ee2016-11-29 13:00:28 -0800103}
104
minyuee5e632f2016-09-27 12:54:19 -0700105void AudioNetworkAdaptorImpl::SetTargetAudioBitrate(
106 int target_audio_bitrate_bps) {
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100107 last_metrics_.target_audio_bitrate_bps = target_audio_bitrate_bps;
minyuee5e632f2016-09-27 12:54:19 -0700108 DumpNetworkMetrics();
minyuea6a6d652017-01-30 10:50:00 -0800109
110 Controller::NetworkMetrics network_metrics;
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100111 network_metrics.target_audio_bitrate_bps = target_audio_bitrate_bps;
minyuea6a6d652017-01-30 10:50:00 -0800112 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) {
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100116 last_metrics_.overhead_bytes_per_packet = overhead_bytes_per_packet;
minyue25f6a392016-09-22 22:23:20 -0700117 DumpNetworkMetrics();
minyuea6a6d652017-01-30 10:50:00 -0800118
119 Controller::NetworkMetrics network_metrics;
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100120 network_metrics.overhead_bytes_per_packet = overhead_bytes_per_packet;
minyuea6a6d652017-01-30 10:50:00 -0800121 UpdateNetworkMetrics(network_metrics);
minyuecaa9cb22016-09-13 13:34:15 -0700122}
123
michaeltcde46b72017-04-06 05:59:10 -0700124AudioEncoderRuntimeConfig AudioNetworkAdaptorImpl::GetEncoderRuntimeConfig() {
125 AudioEncoderRuntimeConfig config;
minyuecaa9cb22016-09-13 13:34:15 -0700126 for (auto& controller :
127 controller_manager_->GetSortedControllers(last_metrics_))
minyuea6a6d652017-01-30 10:50:00 -0800128 controller->MakeDecision(&config);
minyuecaa9cb22016-09-13 13:34:15 -0700129
ivoc17289092017-09-09 08:45:40 -0700130 // Update ANA stats.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200131 auto increment_opt = [](absl::optional<uint32_t>& a) {
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100132 a = a.value_or(0) + 1;
ivoc17289092017-09-09 08:45:40 -0700133 };
134 if (prev_config_) {
135 if (config.bitrate_bps != prev_config_->bitrate_bps) {
136 increment_opt(stats_.bitrate_action_counter);
137 }
138 if (config.enable_dtx != prev_config_->enable_dtx) {
139 increment_opt(stats_.dtx_action_counter);
140 }
141 if (config.enable_fec != prev_config_->enable_fec) {
142 increment_opt(stats_.fec_action_counter);
143 }
144 if (config.frame_length_ms && prev_config_->frame_length_ms) {
145 if (*config.frame_length_ms > *prev_config_->frame_length_ms) {
146 increment_opt(stats_.frame_length_increase_counter);
147 } else if (*config.frame_length_ms < *prev_config_->frame_length_ms) {
148 increment_opt(stats_.frame_length_decrease_counter);
149 }
150 }
151 if (config.num_channels != prev_config_->num_channels) {
152 increment_opt(stats_.channel_action_counter);
153 }
154 if (config.uplink_packet_loss_fraction) {
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100155 stats_.uplink_packet_loss_fraction = *config.uplink_packet_loss_fraction;
ivoc17289092017-09-09 08:45:40 -0700156 }
157 }
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100158 prev_config_ = config;
ivoc17289092017-09-09 08:45:40 -0700159
160 // Prevent certain controllers from taking action (determined by field trials)
161 if (!enable_bitrate_adaptation_ && config.bitrate_bps) {
162 config.bitrate_bps.reset();
163 }
164 if (!enable_dtx_adaptation_ && config.enable_dtx) {
165 config.enable_dtx.reset();
166 }
167 if (!enable_fec_adaptation_ && config.enable_fec) {
168 config.enable_fec.reset();
169 config.uplink_packet_loss_fraction.reset();
170 }
171 if (!enable_frame_length_adaptation_ && config.frame_length_ms) {
172 config.frame_length_ms.reset();
173 }
174 if (!enable_channel_adaptation_ && config.num_channels) {
175 config.num_channels.reset();
176 }
177
minyue25f6a392016-09-22 22:23:20 -0700178 if (debug_dump_writer_)
michaelt92aef172017-04-18 00:11:48 -0700179 debug_dump_writer_->DumpEncoderRuntimeConfig(config, rtc::TimeMillis());
minyuecaa9cb22016-09-13 13:34:15 -0700180
minyue4b7c9522017-01-24 04:54:59 -0800181 if (event_log_writer_)
182 event_log_writer_->MaybeLogEncoderConfig(config);
183
minyuecaa9cb22016-09-13 13:34:15 -0700184 return config;
185}
186
minyuecaa9cb22016-09-13 13:34:15 -0700187void AudioNetworkAdaptorImpl::StartDebugDump(FILE* file_handle) {
minyue25f6a392016-09-22 22:23:20 -0700188 debug_dump_writer_ = DebugDumpWriter::Create(file_handle);
189}
190
191void AudioNetworkAdaptorImpl::StopDebugDump() {
192 debug_dump_writer_.reset(nullptr);
193}
194
ivoce1198e02017-09-08 08:13:19 -0700195ANAStats AudioNetworkAdaptorImpl::GetStats() const {
ivoc17289092017-09-09 08:45:40 -0700196 return stats_;
ivoce1198e02017-09-08 08:13:19 -0700197}
198
minyue25f6a392016-09-22 22:23:20 -0700199void AudioNetworkAdaptorImpl::DumpNetworkMetrics() {
200 if (debug_dump_writer_)
michaelt92aef172017-04-18 00:11:48 -0700201 debug_dump_writer_->DumpNetworkMetrics(last_metrics_, rtc::TimeMillis());
minyuecaa9cb22016-09-13 13:34:15 -0700202}
203
minyuea6a6d652017-01-30 10:50:00 -0800204void AudioNetworkAdaptorImpl::UpdateNetworkMetrics(
205 const Controller::NetworkMetrics& network_metrics) {
206 for (auto& controller : controller_manager_->GetControllers())
207 controller->UpdateNetworkMetrics(network_metrics);
208}
209
minyuecaa9cb22016-09-13 13:34:15 -0700210} // namespace webrtc