• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include "modules/audio_coding/audio_network_adaptor/audio_network_adaptor_impl.h"
12 
13 #include <stdint.h>
14 
15 #include <utility>
16 #include <vector>
17 
18 #include "modules/audio_coding/audio_network_adaptor/controller_manager.h"
19 #include "modules/audio_coding/audio_network_adaptor/debug_dump_writer.h"
20 #include "modules/audio_coding/audio_network_adaptor/event_log_writer.h"
21 #include "rtc_base/checks.h"
22 #include "rtc_base/time_utils.h"
23 #include "system_wrappers/include/field_trial.h"
24 
25 namespace webrtc {
26 
27 namespace {
28 constexpr int kEventLogMinBitrateChangeBps = 5000;
29 constexpr float kEventLogMinBitrateChangeFraction = 0.25;
30 constexpr float kEventLogMinPacketLossChangeFraction = 0.5;
31 }  // namespace
32 
Config()33 AudioNetworkAdaptorImpl::Config::Config() : event_log(nullptr) {}
34 
35 AudioNetworkAdaptorImpl::Config::~Config() = default;
36 
AudioNetworkAdaptorImpl(const Config & config,std::unique_ptr<ControllerManager> controller_manager,std::unique_ptr<DebugDumpWriter> debug_dump_writer)37 AudioNetworkAdaptorImpl::AudioNetworkAdaptorImpl(
38     const Config& config,
39     std::unique_ptr<ControllerManager> controller_manager,
40     std::unique_ptr<DebugDumpWriter> debug_dump_writer)
41     : config_(config),
42       controller_manager_(std::move(controller_manager)),
43       debug_dump_writer_(std::move(debug_dump_writer)),
44       event_log_writer_(
45           config.event_log
46               ? new EventLogWriter(config.event_log,
47                                    kEventLogMinBitrateChangeBps,
48                                    kEventLogMinBitrateChangeFraction,
49                                    kEventLogMinPacketLossChangeFraction)
50               : nullptr) {
51   RTC_DCHECK(controller_manager_);
52 }
53 
54 AudioNetworkAdaptorImpl::~AudioNetworkAdaptorImpl() = default;
55 
SetUplinkBandwidth(int uplink_bandwidth_bps)56 void AudioNetworkAdaptorImpl::SetUplinkBandwidth(int uplink_bandwidth_bps) {
57   last_metrics_.uplink_bandwidth_bps = uplink_bandwidth_bps;
58   DumpNetworkMetrics();
59 
60   Controller::NetworkMetrics network_metrics;
61   network_metrics.uplink_bandwidth_bps = uplink_bandwidth_bps;
62   UpdateNetworkMetrics(network_metrics);
63 }
64 
SetUplinkPacketLossFraction(float uplink_packet_loss_fraction)65 void AudioNetworkAdaptorImpl::SetUplinkPacketLossFraction(
66     float uplink_packet_loss_fraction) {
67   last_metrics_.uplink_packet_loss_fraction = uplink_packet_loss_fraction;
68   DumpNetworkMetrics();
69 
70   Controller::NetworkMetrics network_metrics;
71   network_metrics.uplink_packet_loss_fraction = uplink_packet_loss_fraction;
72   UpdateNetworkMetrics(network_metrics);
73 }
74 
SetRtt(int rtt_ms)75 void AudioNetworkAdaptorImpl::SetRtt(int rtt_ms) {
76   last_metrics_.rtt_ms = rtt_ms;
77   DumpNetworkMetrics();
78 
79   Controller::NetworkMetrics network_metrics;
80   network_metrics.rtt_ms = rtt_ms;
81   UpdateNetworkMetrics(network_metrics);
82 }
83 
SetTargetAudioBitrate(int target_audio_bitrate_bps)84 void AudioNetworkAdaptorImpl::SetTargetAudioBitrate(
85     int target_audio_bitrate_bps) {
86   last_metrics_.target_audio_bitrate_bps = target_audio_bitrate_bps;
87   DumpNetworkMetrics();
88 
89   Controller::NetworkMetrics network_metrics;
90   network_metrics.target_audio_bitrate_bps = target_audio_bitrate_bps;
91   UpdateNetworkMetrics(network_metrics);
92 }
93 
SetOverhead(size_t overhead_bytes_per_packet)94 void AudioNetworkAdaptorImpl::SetOverhead(size_t overhead_bytes_per_packet) {
95   last_metrics_.overhead_bytes_per_packet = overhead_bytes_per_packet;
96   DumpNetworkMetrics();
97 
98   Controller::NetworkMetrics network_metrics;
99   network_metrics.overhead_bytes_per_packet = overhead_bytes_per_packet;
100   UpdateNetworkMetrics(network_metrics);
101 }
102 
GetEncoderRuntimeConfig()103 AudioEncoderRuntimeConfig AudioNetworkAdaptorImpl::GetEncoderRuntimeConfig() {
104   AudioEncoderRuntimeConfig config;
105   for (auto& controller :
106        controller_manager_->GetSortedControllers(last_metrics_))
107     controller->MakeDecision(&config);
108 
109   // Update ANA stats.
110   auto increment_opt = [](absl::optional<uint32_t>& a) {
111     a = a.value_or(0) + 1;
112   };
113   if (prev_config_) {
114     if (config.bitrate_bps != prev_config_->bitrate_bps) {
115       increment_opt(stats_.bitrate_action_counter);
116     }
117     if (config.enable_dtx != prev_config_->enable_dtx) {
118       increment_opt(stats_.dtx_action_counter);
119     }
120     if (config.enable_fec != prev_config_->enable_fec) {
121       increment_opt(stats_.fec_action_counter);
122     }
123     if (config.frame_length_ms && prev_config_->frame_length_ms) {
124       if (*config.frame_length_ms > *prev_config_->frame_length_ms) {
125         increment_opt(stats_.frame_length_increase_counter);
126       } else if (*config.frame_length_ms < *prev_config_->frame_length_ms) {
127         increment_opt(stats_.frame_length_decrease_counter);
128       }
129     }
130     if (config.num_channels != prev_config_->num_channels) {
131       increment_opt(stats_.channel_action_counter);
132     }
133     if (config.uplink_packet_loss_fraction) {
134       stats_.uplink_packet_loss_fraction = *config.uplink_packet_loss_fraction;
135     }
136   }
137   prev_config_ = config;
138 
139   if (debug_dump_writer_)
140     debug_dump_writer_->DumpEncoderRuntimeConfig(config, rtc::TimeMillis());
141 
142   if (event_log_writer_)
143     event_log_writer_->MaybeLogEncoderConfig(config);
144 
145   return config;
146 }
147 
StartDebugDump(FILE * file_handle)148 void AudioNetworkAdaptorImpl::StartDebugDump(FILE* file_handle) {
149   debug_dump_writer_ = DebugDumpWriter::Create(file_handle);
150 }
151 
StopDebugDump()152 void AudioNetworkAdaptorImpl::StopDebugDump() {
153   debug_dump_writer_.reset(nullptr);
154 }
155 
GetStats() const156 ANAStats AudioNetworkAdaptorImpl::GetStats() const {
157   return stats_;
158 }
159 
DumpNetworkMetrics()160 void AudioNetworkAdaptorImpl::DumpNetworkMetrics() {
161   if (debug_dump_writer_)
162     debug_dump_writer_->DumpNetworkMetrics(last_metrics_, rtc::TimeMillis());
163 }
164 
UpdateNetworkMetrics(const Controller::NetworkMetrics & network_metrics)165 void AudioNetworkAdaptorImpl::UpdateNetworkMetrics(
166     const Controller::NetworkMetrics& network_metrics) {
167   for (auto& controller : controller_manager_->GetControllers())
168     controller->UpdateNetworkMetrics(network_metrics);
169 }
170 
171 }  // namespace webrtc
172