xref: /aosp_15_r20/external/webrtc/test/pc/e2e/peer_params_preprocessor.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2022 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 "test/pc/e2e/peer_params_preprocessor.h"
12 
13 #include <set>
14 #include <string>
15 
16 #include "absl/strings/string_view.h"
17 #include "api/test/pclf/media_configuration.h"
18 #include "api/test/pclf/media_quality_test_params.h"
19 #include "api/test/pclf/peer_configurer.h"
20 #include "api/test/peer_network_dependencies.h"
21 #include "modules/video_coding/svc/create_scalability_structure.h"
22 #include "modules/video_coding/svc/scalability_mode_util.h"
23 #include "rtc_base/arraysize.h"
24 #include "test/testsupport/file_utils.h"
25 
26 namespace webrtc {
27 namespace webrtc_pc_e2e {
28 namespace {
29 
30 // List of default names of generic participants according to
31 // https://en.wikipedia.org/wiki/Alice_and_Bob
32 constexpr absl::string_view kDefaultNames[] = {"alice", "bob",  "charlie",
33                                                "david", "erin", "frank"};
34 
35 }  // namespace
36 
37 class PeerParamsPreprocessor::DefaultNamesProvider {
38  public:
39   // Caller have to ensure that default names array will outlive names provider
40   // instance.
DefaultNamesProvider(absl::string_view prefix,rtc::ArrayView<const absl::string_view> default_names={})41   explicit DefaultNamesProvider(
42       absl::string_view prefix,
43       rtc::ArrayView<const absl::string_view> default_names = {})
44       : prefix_(prefix), default_names_(default_names) {}
45 
MaybeSetName(absl::optional<std::string> & name)46   void MaybeSetName(absl::optional<std::string>& name) {
47     if (name.has_value()) {
48       known_names_.insert(name.value());
49     } else {
50       name = GenerateName();
51     }
52   }
53 
54  private:
GenerateName()55   std::string GenerateName() {
56     std::string name;
57     do {
58       name = GenerateNameInternal();
59     } while (!known_names_.insert(name).second);
60     return name;
61   }
62 
GenerateNameInternal()63   std::string GenerateNameInternal() {
64     if (counter_ < default_names_.size()) {
65       return std::string(default_names_[counter_++]);
66     }
67     return prefix_ + std::to_string(counter_++);
68   }
69 
70   const std::string prefix_;
71   const rtc::ArrayView<const absl::string_view> default_names_;
72 
73   std::set<std::string> known_names_;
74   size_t counter_ = 0;
75 };
76 
PeerParamsPreprocessor()77 PeerParamsPreprocessor::PeerParamsPreprocessor()
78     : peer_names_provider_(
79           std::make_unique<DefaultNamesProvider>("peer_", kDefaultNames)) {}
80 PeerParamsPreprocessor::~PeerParamsPreprocessor() = default;
81 
SetDefaultValuesForMissingParams(PeerConfigurer & peer)82 void PeerParamsPreprocessor::SetDefaultValuesForMissingParams(
83     PeerConfigurer& peer) {
84   Params* params = peer.params();
85   ConfigurableParams* configurable_params = peer.configurable_params();
86   peer_names_provider_->MaybeSetName(params->name);
87   DefaultNamesProvider video_stream_names_provider(*params->name +
88                                                    "_auto_video_stream_label_");
89   for (VideoConfig& config : configurable_params->video_configs) {
90     video_stream_names_provider.MaybeSetName(config.stream_label);
91   }
92   if (params->audio_config) {
93     DefaultNamesProvider audio_stream_names_provider(
94         *params->name + "_auto_audio_stream_label_");
95     audio_stream_names_provider.MaybeSetName(
96         params->audio_config->stream_label);
97   }
98 
99   if (params->video_codecs.empty()) {
100     params->video_codecs.push_back(VideoCodecConfig(cricket::kVp8CodecName));
101   }
102 }
103 
ValidateParams(const PeerConfigurer & peer)104 void PeerParamsPreprocessor::ValidateParams(const PeerConfigurer& peer) {
105   const Params& p = peer.params();
106   RTC_CHECK_GT(p.video_encoder_bitrate_multiplier, 0.0);
107   // Each peer should at least support 1 video codec.
108   RTC_CHECK_GE(p.video_codecs.size(), 1);
109 
110   {
111     RTC_CHECK(p.name);
112     bool inserted = peer_names_.insert(p.name.value()).second;
113     RTC_CHECK(inserted) << "Duplicate name=" << p.name.value();
114   }
115 
116   // Validate that all video stream labels are unique and sync groups are
117   // valid.
118   for (const VideoConfig& video_config :
119        peer.configurable_params().video_configs) {
120     RTC_CHECK(video_config.stream_label);
121     bool inserted =
122         video_labels_.insert(video_config.stream_label.value()).second;
123     RTC_CHECK(inserted) << "Duplicate video_config.stream_label="
124                         << video_config.stream_label.value();
125 
126     // TODO(bugs.webrtc.org/4762): remove this check after synchronization of
127     // more than two streams is supported.
128     if (video_config.sync_group.has_value()) {
129       bool sync_group_inserted =
130           video_sync_groups_.insert(video_config.sync_group.value()).second;
131       RTC_CHECK(sync_group_inserted)
132           << "Sync group shouldn't consist of more than two streams (one "
133              "video and one audio). Duplicate video_config.sync_group="
134           << video_config.sync_group.value();
135     }
136 
137     if (video_config.simulcast_config) {
138       if (!video_config.encoding_params.empty()) {
139         RTC_CHECK_EQ(video_config.simulcast_config->simulcast_streams_count,
140                      video_config.encoding_params.size())
141             << "|encoding_params| have to be specified for each simulcast "
142             << "stream in |video_config|.";
143       }
144     } else {
145       RTC_CHECK_LE(video_config.encoding_params.size(), 1)
146           << "|encoding_params| has multiple values but simulcast is not "
147              "enabled.";
148     }
149 
150     if (video_config.emulated_sfu_config) {
151       if (video_config.simulcast_config &&
152           video_config.emulated_sfu_config->target_layer_index) {
153         RTC_CHECK_LT(*video_config.emulated_sfu_config->target_layer_index,
154                      video_config.simulcast_config->simulcast_streams_count);
155       }
156       if (!video_config.encoding_params.empty()) {
157         bool is_svc = false;
158         for (const auto& encoding_param : video_config.encoding_params) {
159           if (!encoding_param.scalability_mode)
160             continue;
161 
162           absl::optional<ScalabilityMode> scalability_mode =
163               ScalabilityModeFromString(*encoding_param.scalability_mode);
164           RTC_CHECK(scalability_mode) << "Unknown scalability_mode requested";
165 
166           absl::optional<ScalableVideoController::StreamLayersConfig>
167               stream_layers_config =
168                   ScalabilityStructureConfig(*scalability_mode);
169           is_svc |= stream_layers_config->num_spatial_layers > 1;
170           RTC_CHECK(stream_layers_config->num_spatial_layers == 1 ||
171                     video_config.encoding_params.size() == 1)
172               << "Can't enable SVC modes with multiple spatial layers ("
173               << stream_layers_config->num_spatial_layers
174               << " layers) or simulcast ("
175               << video_config.encoding_params.size() << " layers)";
176           if (video_config.emulated_sfu_config->target_layer_index) {
177             RTC_CHECK_LT(*video_config.emulated_sfu_config->target_layer_index,
178                          stream_layers_config->num_spatial_layers);
179           }
180         }
181         if (!is_svc && video_config.emulated_sfu_config->target_layer_index) {
182           RTC_CHECK_LT(*video_config.emulated_sfu_config->target_layer_index,
183                        video_config.encoding_params.size());
184         }
185       }
186     }
187   }
188   if (p.audio_config) {
189     bool inserted =
190         audio_labels_.insert(p.audio_config->stream_label.value()).second;
191     RTC_CHECK(inserted) << "Duplicate audio_config.stream_label="
192                         << p.audio_config->stream_label.value();
193     // TODO(bugs.webrtc.org/4762): remove this check after synchronization of
194     // more than two streams is supported.
195     if (p.audio_config->sync_group.has_value()) {
196       bool sync_group_inserted =
197           audio_sync_groups_.insert(p.audio_config->sync_group.value()).second;
198       RTC_CHECK(sync_group_inserted)
199           << "Sync group shouldn't consist of more than two streams (one "
200              "video and one audio). Duplicate audio_config.sync_group="
201           << p.audio_config->sync_group.value();
202     }
203     // Check that if mode input file name specified only if mode is kFile.
204     if (p.audio_config.value().mode == AudioConfig::Mode::kGenerated) {
205       RTC_CHECK(!p.audio_config.value().input_file_name);
206     }
207     if (p.audio_config.value().mode == AudioConfig::Mode::kFile) {
208       RTC_CHECK(p.audio_config.value().input_file_name);
209       RTC_CHECK(
210           test::FileExists(p.audio_config.value().input_file_name.value()))
211           << p.audio_config.value().input_file_name.value() << " doesn't exist";
212     }
213   }
214 }
215 
216 }  // namespace webrtc_pc_e2e
217 }  // namespace webrtc
218