xref: /aosp_15_r20/external/webrtc/modules/audio_coding/audio_network_adaptor/channel_controller.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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/channel_controller.h"
12 
13 #include <algorithm>
14 
15 #include "rtc_base/checks.h"
16 
17 namespace webrtc {
18 
Config(size_t num_encoder_channels,size_t intial_channels_to_encode,int channel_1_to_2_bandwidth_bps,int channel_2_to_1_bandwidth_bps)19 ChannelController::Config::Config(size_t num_encoder_channels,
20                                   size_t intial_channels_to_encode,
21                                   int channel_1_to_2_bandwidth_bps,
22                                   int channel_2_to_1_bandwidth_bps)
23     : num_encoder_channels(num_encoder_channels),
24       intial_channels_to_encode(intial_channels_to_encode),
25       channel_1_to_2_bandwidth_bps(channel_1_to_2_bandwidth_bps),
26       channel_2_to_1_bandwidth_bps(channel_2_to_1_bandwidth_bps) {}
27 
ChannelController(const Config & config)28 ChannelController::ChannelController(const Config& config)
29     : config_(config), channels_to_encode_(config_.intial_channels_to_encode) {
30   RTC_DCHECK_GT(config_.intial_channels_to_encode, 0lu);
31   // Currently, we require `intial_channels_to_encode` to be <= 2.
32   RTC_DCHECK_LE(config_.intial_channels_to_encode, 2lu);
33   RTC_DCHECK_GE(config_.num_encoder_channels,
34                 config_.intial_channels_to_encode);
35 }
36 
37 ChannelController::~ChannelController() = default;
38 
UpdateNetworkMetrics(const NetworkMetrics & network_metrics)39 void ChannelController::UpdateNetworkMetrics(
40     const NetworkMetrics& network_metrics) {
41   if (network_metrics.uplink_bandwidth_bps)
42     uplink_bandwidth_bps_ = network_metrics.uplink_bandwidth_bps;
43 }
44 
MakeDecision(AudioEncoderRuntimeConfig * config)45 void ChannelController::MakeDecision(AudioEncoderRuntimeConfig* config) {
46   // Decision on `num_channels` should not have been made.
47   RTC_DCHECK(!config->num_channels);
48 
49   if (uplink_bandwidth_bps_) {
50     if (channels_to_encode_ == 2 &&
51         *uplink_bandwidth_bps_ <= config_.channel_2_to_1_bandwidth_bps) {
52       channels_to_encode_ = 1;
53     } else if (channels_to_encode_ == 1 &&
54                *uplink_bandwidth_bps_ >= config_.channel_1_to_2_bandwidth_bps) {
55       channels_to_encode_ =
56           std::min(static_cast<size_t>(2), config_.num_encoder_channels);
57     }
58   }
59   config->num_channels = channels_to_encode_;
60 }
61 
62 }  // namespace webrtc
63