• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2020 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_configurer.h"
12 
13 #include <set>
14 
15 #include "absl/strings/string_view.h"
16 #include "rtc_base/arraysize.h"
17 #include "test/testsupport/file_utils.h"
18 
19 namespace webrtc {
20 namespace webrtc_pc_e2e {
21 namespace {
22 
23 using AudioConfig = PeerConnectionE2EQualityTestFixture::AudioConfig;
24 using VideoConfig = PeerConnectionE2EQualityTestFixture::VideoConfig;
25 using RunParams = PeerConnectionE2EQualityTestFixture::RunParams;
26 using VideoCodecConfig = PeerConnectionE2EQualityTestFixture::VideoCodecConfig;
27 
28 // List of default names of generic participants according to
29 // https://en.wikipedia.org/wiki/Alice_and_Bob
30 constexpr absl::string_view kDefaultNames[] = {"alice", "bob",  "charlie",
31                                                "david", "erin", "frank"};
32 
33 class DefaultNamesProvider {
34  public:
35   // Caller have to ensure that default names array will outlive names provider
36   // instance.
DefaultNamesProvider(absl::string_view prefix,rtc::ArrayView<const absl::string_view> default_names={})37   explicit DefaultNamesProvider(
38       absl::string_view prefix,
39       rtc::ArrayView<const absl::string_view> default_names = {})
40       : prefix_(prefix), default_names_(default_names) {}
41 
MaybeSetName(absl::optional<std::string> * name)42   void MaybeSetName(absl::optional<std::string>* name) {
43     if (name->has_value()) {
44       known_names_.insert(name->value());
45     } else {
46       *name = GenerateName();
47     }
48   }
49 
50  private:
GenerateName()51   std::string GenerateName() {
52     std::string name;
53     do {
54       name = GenerateNameInternal();
55     } while (!known_names_.insert(name).second);
56     return name;
57   }
58 
GenerateNameInternal()59   std::string GenerateNameInternal() {
60     if (counter_ < default_names_.size()) {
61       return std::string(default_names_[counter_++]);
62     }
63     return prefix_ + std::to_string(counter_++);
64   }
65 
66   const std::string prefix_;
67   const rtc::ArrayView<const absl::string_view> default_names_;
68 
69   std::set<std::string> known_names_;
70   size_t counter_ = 0;
71 };
72 
73 }  // namespace
74 
SetDefaultValuesForMissingParams(RunParams * run_params,std::vector<std::unique_ptr<PeerConfigurerImpl>> * peers)75 void SetDefaultValuesForMissingParams(
76     RunParams* run_params,
77     std::vector<std::unique_ptr<PeerConfigurerImpl>>* peers) {
78   DefaultNamesProvider peer_names_provider("peer_", kDefaultNames);
79   for (size_t i = 0; i < peers->size(); ++i) {
80     auto* peer = peers->at(i).get();
81     auto* p = peer->params();
82     peer_names_provider.MaybeSetName(&p->name);
83     DefaultNamesProvider video_stream_names_provider(
84         *p->name + "_auto_video_stream_label_");
85     for (VideoConfig& video_config : p->video_configs) {
86       video_stream_names_provider.MaybeSetName(&video_config.stream_label);
87     }
88     if (p->audio_config) {
89       DefaultNamesProvider audio_stream_names_provider(
90           *p->name + "_auto_audio_stream_label_");
91       audio_stream_names_provider.MaybeSetName(&p->audio_config->stream_label);
92     }
93   }
94 
95   if (run_params->video_codecs.empty()) {
96     run_params->video_codecs.push_back(
97         VideoCodecConfig(cricket::kVp8CodecName));
98   }
99 }
100 
ValidateParams(const RunParams & run_params,const std::vector<std::unique_ptr<PeerConfigurerImpl>> & peers)101 void ValidateParams(
102     const RunParams& run_params,
103     const std::vector<std::unique_ptr<PeerConfigurerImpl>>& peers) {
104   RTC_CHECK_GT(run_params.video_encoder_bitrate_multiplier, 0.0);
105   RTC_CHECK_GE(run_params.video_codecs.size(), 1);
106 
107   std::set<std::string> peer_names;
108   std::set<std::string> video_labels;
109   std::set<std::string> audio_labels;
110   std::set<std::string> video_sync_groups;
111   std::set<std::string> audio_sync_groups;
112   int media_streams_count = 0;
113 
114   for (size_t i = 0; i < peers.size(); ++i) {
115     Params* p = peers[i]->params();
116 
117     {
118       RTC_CHECK(p->name);
119       bool inserted = peer_names.insert(p->name.value()).second;
120       RTC_CHECK(inserted) << "Duplicate name=" << p->name.value();
121     }
122 
123     if (p->audio_config) {
124       media_streams_count++;
125     }
126     media_streams_count += p->video_configs.size();
127 
128     // Validate that all video stream labels are unique and sync groups are
129     // valid.
130     for (const VideoConfig& video_config : p->video_configs) {
131       RTC_CHECK(video_config.stream_label);
132       bool inserted =
133           video_labels.insert(video_config.stream_label.value()).second;
134       RTC_CHECK(inserted) << "Duplicate video_config.stream_label="
135                           << video_config.stream_label.value();
136 
137       // TODO(bugs.webrtc.org/4762): remove this check after synchronization of
138       // more than two streams is supported.
139       if (video_config.sync_group.has_value()) {
140         bool sync_group_inserted =
141             video_sync_groups.insert(video_config.sync_group.value()).second;
142         RTC_CHECK(sync_group_inserted)
143             << "Sync group shouldn't consist of more than two streams (one "
144                "video and one audio). Duplicate video_config.sync_group="
145             << video_config.sync_group.value();
146       }
147 
148       if (video_config.simulcast_config) {
149         if (video_config.simulcast_config->target_spatial_index) {
150           RTC_CHECK_GE(*video_config.simulcast_config->target_spatial_index, 0);
151           RTC_CHECK_LT(*video_config.simulcast_config->target_spatial_index,
152                        video_config.simulcast_config->simulcast_streams_count);
153         }
154         RTC_CHECK_EQ(run_params.video_codecs.size(), 1)
155             << "Only 1 video codec is supported when simulcast is enabled in "
156             << "at least 1 video config";
157         RTC_CHECK(!video_config.max_encode_bitrate_bps)
158             << "Setting max encode bitrate is not implemented for simulcast.";
159         RTC_CHECK(!video_config.min_encode_bitrate_bps)
160             << "Setting min encode bitrate is not implemented for simulcast.";
161         if (run_params.video_codecs[0].name == cricket::kVp8CodecName &&
162             !video_config.simulcast_config->encoding_params.empty()) {
163           RTC_CHECK_EQ(video_config.simulcast_config->simulcast_streams_count,
164                        video_config.simulcast_config->encoding_params.size())
165               << "|encoding_params| have to be specified for each simulcast "
166               << "stream in |simulcast_config|.";
167         }
168       }
169     }
170     if (p->audio_config) {
171       bool inserted =
172           audio_labels.insert(p->audio_config->stream_label.value()).second;
173       RTC_CHECK(inserted) << "Duplicate audio_config.stream_label="
174                           << p->audio_config->stream_label.value();
175       // TODO(bugs.webrtc.org/4762): remove this check after synchronization of
176       // more than two streams is supported.
177       if (p->audio_config->sync_group.has_value()) {
178         bool sync_group_inserted =
179             audio_sync_groups.insert(p->audio_config->sync_group.value())
180                 .second;
181         RTC_CHECK(sync_group_inserted)
182             << "Sync group shouldn't consist of more than two streams (one "
183                "video and one audio). Duplicate audio_config.sync_group="
184             << p->audio_config->sync_group.value();
185       }
186       // Check that if mode input file name specified only if mode is kFile.
187       if (p->audio_config.value().mode == AudioConfig::Mode::kGenerated) {
188         RTC_CHECK(!p->audio_config.value().input_file_name);
189       }
190       if (p->audio_config.value().mode == AudioConfig::Mode::kFile) {
191         RTC_CHECK(p->audio_config.value().input_file_name);
192         RTC_CHECK(
193             test::FileExists(p->audio_config.value().input_file_name.value()))
194             << p->audio_config.value().input_file_name.value()
195             << " doesn't exist";
196       }
197     }
198   }
199 
200   RTC_CHECK_GT(media_streams_count, 0) << "No media in the call.";
201 }
202 
203 }  // namespace webrtc_pc_e2e
204 }  // namespace webrtc
205