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 #ifndef MODULES_AUDIO_CODING_AUDIO_NETWORK_ADAPTOR_CONTROLLER_MANAGER_H_ 12 #define MODULES_AUDIO_CODING_AUDIO_NETWORK_ADAPTOR_CONTROLLER_MANAGER_H_ 13 14 #include <map> 15 #include <memory> 16 #include <string> 17 #include <vector> 18 19 #include "modules/audio_coding/audio_network_adaptor/controller.h" 20 #include "rtc_base/constructor_magic.h" 21 22 namespace webrtc { 23 24 class DebugDumpWriter; 25 26 class ControllerManager { 27 public: 28 virtual ~ControllerManager() = default; 29 30 // Sort controllers based on their significance. 31 virtual std::vector<Controller*> GetSortedControllers( 32 const Controller::NetworkMetrics& metrics) = 0; 33 34 virtual std::vector<Controller*> GetControllers() const = 0; 35 }; 36 37 class ControllerManagerImpl final : public ControllerManager { 38 public: 39 struct Config { 40 Config(int min_reordering_time_ms, float min_reordering_squared_distance); 41 ~Config(); 42 // Least time since last reordering for a new reordering to be made. 43 int min_reordering_time_ms; 44 // Least squared distance from last scoring point for a new reordering to be 45 // made. 46 float min_reordering_squared_distance; 47 }; 48 49 static std::unique_ptr<ControllerManager> Create( 50 const std::string& config_string, 51 size_t num_encoder_channels, 52 rtc::ArrayView<const int> encoder_frame_lengths_ms, 53 int min_encoder_bitrate_bps, 54 size_t intial_channels_to_encode, 55 int initial_frame_length_ms, 56 int initial_bitrate_bps, 57 bool initial_fec_enabled, 58 bool initial_dtx_enabled); 59 60 static std::unique_ptr<ControllerManager> Create( 61 const std::string& config_string, 62 size_t num_encoder_channels, 63 rtc::ArrayView<const int> encoder_frame_lengths_ms, 64 int min_encoder_bitrate_bps, 65 size_t intial_channels_to_encode, 66 int initial_frame_length_ms, 67 int initial_bitrate_bps, 68 bool initial_fec_enabled, 69 bool initial_dtx_enabled, 70 DebugDumpWriter* debug_dump_writer); 71 72 explicit ControllerManagerImpl(const Config& config); 73 74 // Dependency injection for testing. 75 ControllerManagerImpl( 76 const Config& config, 77 std::vector<std::unique_ptr<Controller>> controllers, 78 const std::map<const Controller*, std::pair<int, float>>& 79 chracteristic_points); 80 81 ~ControllerManagerImpl() override; 82 83 // Sort controllers based on their significance. 84 std::vector<Controller*> GetSortedControllers( 85 const Controller::NetworkMetrics& metrics) override; 86 87 std::vector<Controller*> GetControllers() const override; 88 89 private: 90 // Scoring point is a subset of NetworkMetrics that is used for comparing the 91 // significance of controllers. 92 struct ScoringPoint { 93 // TODO(eladalon): Do we want to experiment with RPLR-based scoring? 94 ScoringPoint(int uplink_bandwidth_bps, float uplink_packet_loss_fraction); 95 96 // Calculate the normalized [0,1] distance between two scoring points. 97 float SquaredDistanceTo(const ScoringPoint& scoring_point) const; 98 99 int uplink_bandwidth_bps; 100 float uplink_packet_loss_fraction; 101 }; 102 103 const Config config_; 104 105 std::vector<std::unique_ptr<Controller>> controllers_; 106 107 absl::optional<int64_t> last_reordering_time_ms_; 108 ScoringPoint last_scoring_point_; 109 110 std::vector<Controller*> default_sorted_controllers_; 111 112 std::vector<Controller*> sorted_controllers_; 113 114 // |scoring_points_| saves the scoring points of various 115 // controllers. 116 std::map<const Controller*, ScoringPoint> controller_scoring_points_; 117 118 RTC_DISALLOW_COPY_AND_ASSIGN(ControllerManagerImpl); 119 }; 120 121 } // namespace webrtc 122 123 #endif // MODULES_AUDIO_CODING_AUDIO_NETWORK_ADAPTOR_CONTROLLER_MANAGER_H_ 124