Adding ChannelController to audio network adaptor.

BUG=webrtc:6303

Review-Url: https://codereview.webrtc.org/2319873002
Cr-Commit-Position: refs/heads/master@{#14216}
This commit is contained in:
minyue
2016-09-14 06:47:36 -07:00
committed by Commit bot
parent fdafab84bc
commit 2e164c6b53
7 changed files with 224 additions and 0 deletions

View File

@ -703,6 +703,8 @@ source_set("audio_network_adaptor") {
"audio_network_adaptor/audio_network_adaptor.cc",
"audio_network_adaptor/audio_network_adaptor_impl.cc",
"audio_network_adaptor/audio_network_adaptor_impl.h",
"audio_network_adaptor/channel_controller.cc",
"audio_network_adaptor/channel_controller.h",
"audio_network_adaptor/controller.cc",
"audio_network_adaptor/controller.h",
"audio_network_adaptor/controller_manager.cc",

View File

@ -14,6 +14,8 @@
'audio_network_adaptor.cc',
'audio_network_adaptor_impl.cc',
'audio_network_adaptor_impl.h',
'channel_controller.cc',
'channel_controller.h',
'controller.h',
'controller.cc',
'controller_manager.cc',

View File

@ -0,0 +1,56 @@
/*
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <algorithm>
#include "webrtc/modules/audio_coding/audio_network_adaptor/channel_controller.h"
#include "webrtc/base/checks.h"
namespace webrtc {
ChannelController::Config::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)
: num_encoder_channels(num_encoder_channels),
intial_channels_to_encode(intial_channels_to_encode),
channel_1_to_2_bandwidth_bps(channel_1_to_2_bandwidth_bps),
channel_2_to_1_bandwidth_bps(channel_2_to_1_bandwidth_bps) {}
ChannelController::ChannelController(const Config& config)
: config_(config), channels_to_encode_(config_.intial_channels_to_encode) {
RTC_DCHECK_GT(config_.intial_channels_to_encode, 0lu);
// Currently, we require |intial_channels_to_encode| to be <= 2.
RTC_DCHECK_LE(config_.intial_channels_to_encode, 2lu);
RTC_DCHECK_GE(config_.num_encoder_channels,
config_.intial_channels_to_encode);
}
void ChannelController::MakeDecision(
const NetworkMetrics& metrics,
AudioNetworkAdaptor::EncoderRuntimeConfig* config) {
// Decision on |num_channels| should not have been made.
RTC_DCHECK(!config->num_channels);
if (metrics.uplink_bandwidth_bps) {
if (channels_to_encode_ == 2 &&
*metrics.uplink_bandwidth_bps <= config_.channel_2_to_1_bandwidth_bps) {
channels_to_encode_ = 1;
} else if (channels_to_encode_ == 1 &&
*metrics.uplink_bandwidth_bps >=
config_.channel_1_to_2_bandwidth_bps) {
channels_to_encode_ =
std::min(static_cast<size_t>(2), config_.num_encoder_channels);
}
}
config->num_channels = rtc::Optional<size_t>(channels_to_encode_);
}
} // namespace webrtc

View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef WEBRTC_MODULES_AUDIO_CODING_AUDIO_NETWORK_ADAPTOR_CHANNEL_CONTROLLER_H_
#define WEBRTC_MODULES_AUDIO_CODING_AUDIO_NETWORK_ADAPTOR_CHANNEL_CONTROLLER_H_
#include "webrtc/base/constructormagic.h"
#include "webrtc/modules/audio_coding/audio_network_adaptor/controller.h"
namespace webrtc {
class ChannelController final : public Controller {
public:
struct Config {
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);
size_t num_encoder_channels;
size_t intial_channels_to_encode;
// Uplink bandwidth above which the number of encoded channels should switch
// from 1 to 2.
int channel_1_to_2_bandwidth_bps;
// Uplink bandwidth below which the number of encoded channels should switch
// from 2 to 1.
int channel_2_to_1_bandwidth_bps;
};
explicit ChannelController(const Config& config);
void MakeDecision(const NetworkMetrics& metrics,
AudioNetworkAdaptor::EncoderRuntimeConfig* config) override;
private:
const Config config_;
size_t channels_to_encode_;
RTC_DISALLOW_COPY_AND_ASSIGN(ChannelController);
};
} // namespace webrtc
#endif // WEBRTC_MODULES_AUDIO_CODING_AUDIO_NETWORK_ADAPTOR_CHANNEL_CONTROLLER_H_

View File

@ -0,0 +1,113 @@
/*
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <memory>
#include "webrtc/modules/audio_coding/audio_network_adaptor/channel_controller.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace webrtc {
namespace {
constexpr int kNumChannels = 2;
constexpr int kChannel1To2BandwidthBps = 31000;
constexpr int kChannel2To1BandwidthBps = 29000;
constexpr int kMediumBandwidthBps =
(kChannel1To2BandwidthBps + kChannel2To1BandwidthBps) / 2;
std::unique_ptr<ChannelController> CreateChannelController(int init_channels) {
std::unique_ptr<ChannelController> controller(
new ChannelController(ChannelController::Config(
kNumChannels, init_channels, kChannel1To2BandwidthBps,
kChannel2To1BandwidthBps)));
return controller;
}
void CheckDecision(ChannelController* controller,
const Controller::NetworkMetrics& metrics,
size_t expected_num_channels) {
AudioNetworkAdaptor::EncoderRuntimeConfig config;
controller->MakeDecision(metrics, &config);
EXPECT_EQ(rtc::Optional<size_t>(expected_num_channels), config.num_channels);
}
} // namespace
TEST(ChannelControllerTest, OutputInitValueWhenUplinkBandwidthUnknown) {
constexpr int kInitChannels = 2;
auto controller = CreateChannelController(kInitChannels);
Controller::NetworkMetrics metrics;
CheckDecision(controller.get(), metrics, kInitChannels);
}
TEST(ChannelControllerTest, SwitchTo2ChannelsOnHighUplinkBandwidth) {
constexpr int kInitChannels = 1;
auto controller = CreateChannelController(kInitChannels);
Controller::NetworkMetrics metrics;
// Use high bandwidth to check output switch to 2.
metrics.uplink_bandwidth_bps = rtc::Optional<int>(kChannel1To2BandwidthBps);
CheckDecision(controller.get(), metrics, 2);
}
TEST(ChannelControllerTest, SwitchTo1ChannelOnLowUplinkBandwidth) {
constexpr int kInitChannels = 2;
auto controller = CreateChannelController(kInitChannels);
Controller::NetworkMetrics metrics;
// Use low bandwidth to check output switch to 1.
metrics.uplink_bandwidth_bps = rtc::Optional<int>(kChannel2To1BandwidthBps);
CheckDecision(controller.get(), metrics, 1);
}
TEST(ChannelControllerTest, Maintain1ChannelOnMediumUplinkBandwidth) {
constexpr int kInitChannels = 1;
auto controller = CreateChannelController(kInitChannels);
Controller::NetworkMetrics metrics;
// Use between-thresholds bandwidth to check output remains at 1.
metrics.uplink_bandwidth_bps = rtc::Optional<int>(kMediumBandwidthBps);
CheckDecision(controller.get(), metrics, 1);
}
TEST(ChannelControllerTest, Maintain2ChannelsOnMediumUplinkBandwidth) {
constexpr int kInitChannels = 2;
auto controller = CreateChannelController(kInitChannels);
Controller::NetworkMetrics metrics;
// Use between-thresholds bandwidth to check output remains at 2.
metrics.uplink_bandwidth_bps = rtc::Optional<int>(kMediumBandwidthBps);
CheckDecision(controller.get(), metrics, 2);
}
TEST(ChannelControllerTest, CheckBehaviorOnChangingUplinkBandwidth) {
constexpr int kInitChannels = 1;
auto controller = CreateChannelController(kInitChannels);
Controller::NetworkMetrics metrics;
// Use between-thresholds bandwidth to check output remains at 1.
metrics.uplink_bandwidth_bps = rtc::Optional<int>(kMediumBandwidthBps);
CheckDecision(controller.get(), metrics, 1);
// Use high bandwidth to check output switch to 2.
metrics.uplink_bandwidth_bps = rtc::Optional<int>(kChannel1To2BandwidthBps);
CheckDecision(controller.get(), metrics, 2);
// Use between-thresholds bandwidth to check output remains at 2.
metrics.uplink_bandwidth_bps = rtc::Optional<int>(kMediumBandwidthBps);
CheckDecision(controller.get(), metrics, 2);
// Use low bandwidth to check output switch to 1.
metrics.uplink_bandwidth_bps = rtc::Optional<int>(kChannel2To1BandwidthBps);
CheckDecision(controller.get(), metrics, 1);
}
} // namespace webrtc