1 /*
2 * Copyright 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 "api/test/pclf/media_configuration.h"
12
13 #include <string>
14 #include <utility>
15
16 #include "absl/strings/string_view.h"
17 #include "absl/types/optional.h"
18 #include "api/array_view.h"
19 #include "api/test/video/video_frame_writer.h"
20 #include "rtc_base/checks.h"
21 #include "rtc_base/strings/string_builder.h"
22 #include "test/pc/e2e/analyzer/video/video_dumping.h"
23 #include "test/testsupport/file_utils.h"
24 #include "test/testsupport/video_frame_writer.h"
25
26 namespace webrtc {
27 namespace webrtc_pc_e2e {
28 namespace {
29
SpecToString(VideoResolution::Spec spec)30 std::string SpecToString(VideoResolution::Spec spec) {
31 switch (spec) {
32 case VideoResolution::Spec::kNone:
33 return "None";
34 case VideoResolution::Spec::kMaxFromSender:
35 return "MaxFromSender";
36 }
37 }
38
AppendResolution(const VideoResolution & resolution,rtc::StringBuilder & builder)39 void AppendResolution(const VideoResolution& resolution,
40 rtc::StringBuilder& builder) {
41 builder << "_" << resolution.width() << "x" << resolution.height() << "_"
42 << resolution.fps();
43 }
44
45 } // namespace
46
ScreenShareConfig(TimeDelta slide_change_interval)47 ScreenShareConfig::ScreenShareConfig(TimeDelta slide_change_interval)
48 : slide_change_interval(slide_change_interval) {
49 RTC_CHECK_GT(slide_change_interval.ms(), 0);
50 }
VideoSimulcastConfig(int simulcast_streams_count)51 VideoSimulcastConfig::VideoSimulcastConfig(int simulcast_streams_count)
52 : simulcast_streams_count(simulcast_streams_count) {
53 RTC_CHECK_GT(simulcast_streams_count, 1);
54 }
EmulatedSFUConfig(int target_layer_index)55 EmulatedSFUConfig::EmulatedSFUConfig(int target_layer_index)
56 : target_layer_index(target_layer_index) {
57 RTC_CHECK_GE(target_layer_index, 0);
58 }
59
EmulatedSFUConfig(absl::optional<int> target_layer_index,absl::optional<int> target_temporal_index)60 EmulatedSFUConfig::EmulatedSFUConfig(absl::optional<int> target_layer_index,
61 absl::optional<int> target_temporal_index)
62 : target_layer_index(target_layer_index),
63 target_temporal_index(target_temporal_index) {
64 RTC_CHECK_GE(target_temporal_index.value_or(0), 0);
65 if (target_temporal_index)
66 RTC_CHECK_GE(*target_temporal_index, 0);
67 }
68
VideoResolution(size_t width,size_t height,int32_t fps)69 VideoResolution::VideoResolution(size_t width, size_t height, int32_t fps)
70 : width_(width), height_(height), fps_(fps), spec_(Spec::kNone) {}
VideoResolution(Spec spec)71 VideoResolution::VideoResolution(Spec spec)
72 : width_(0), height_(0), fps_(0), spec_(spec) {}
73
operator ==(const VideoResolution & other) const74 bool VideoResolution::operator==(const VideoResolution& other) const {
75 if (spec_ != Spec::kNone && spec_ == other.spec_) {
76 // If there is some particular spec set, then it doesn't matter what
77 // values we have in other fields.
78 return true;
79 }
80 return width_ == other.width_ && height_ == other.height_ &&
81 fps_ == other.fps_ && spec_ == other.spec_;
82 }
operator !=(const VideoResolution & other) const83 bool VideoResolution::operator!=(const VideoResolution& other) const {
84 return !(*this == other);
85 }
86
IsRegular() const87 bool VideoResolution::IsRegular() const {
88 return spec_ == Spec::kNone;
89 }
ToString() const90 std::string VideoResolution::ToString() const {
91 rtc::StringBuilder out;
92 out << "{ width=" << width_ << ", height=" << height_ << ", fps=" << fps_
93 << ", spec=" << SpecToString(spec_) << " }";
94 return out.Release();
95 }
96
VideoDumpOptions(absl::string_view output_directory,int sampling_modulo,bool export_frame_ids,std::function<std::unique_ptr<test::VideoFrameWriter> (absl::string_view file_name_prefix,const VideoResolution & resolution)> video_frame_writer_factory)97 VideoDumpOptions::VideoDumpOptions(
98 absl::string_view output_directory,
99 int sampling_modulo,
100 bool export_frame_ids,
101 std::function<std::unique_ptr<test::VideoFrameWriter>(
102 absl::string_view file_name_prefix,
103 const VideoResolution& resolution)> video_frame_writer_factory)
104 : output_directory_(output_directory),
105 sampling_modulo_(sampling_modulo),
106 export_frame_ids_(export_frame_ids),
107 video_frame_writer_factory_(video_frame_writer_factory) {
108 RTC_CHECK_GT(sampling_modulo, 0);
109 }
110
VideoDumpOptions(absl::string_view output_directory,bool export_frame_ids)111 VideoDumpOptions::VideoDumpOptions(absl::string_view output_directory,
112 bool export_frame_ids)
113 : VideoDumpOptions(output_directory,
114 kDefaultSamplingModulo,
115 export_frame_ids) {}
116
117 std::unique_ptr<test::VideoFrameWriter>
CreateInputDumpVideoFrameWriter(absl::string_view stream_label,const VideoResolution & resolution) const118 VideoDumpOptions::CreateInputDumpVideoFrameWriter(
119 absl::string_view stream_label,
120 const VideoResolution& resolution) const {
121 std::unique_ptr<test::VideoFrameWriter> writer = video_frame_writer_factory_(
122 GetInputDumpFileName(stream_label, resolution), resolution);
123 absl::optional<std::string> frame_ids_file =
124 GetInputFrameIdsDumpFileName(stream_label, resolution);
125 if (frame_ids_file.has_value()) {
126 writer = CreateVideoFrameWithIdsWriter(std::move(writer), *frame_ids_file);
127 }
128 return writer;
129 }
130
131 std::unique_ptr<test::VideoFrameWriter>
CreateOutputDumpVideoFrameWriter(absl::string_view stream_label,absl::string_view receiver,const VideoResolution & resolution) const132 VideoDumpOptions::CreateOutputDumpVideoFrameWriter(
133 absl::string_view stream_label,
134 absl::string_view receiver,
135 const VideoResolution& resolution) const {
136 std::unique_ptr<test::VideoFrameWriter> writer = video_frame_writer_factory_(
137 GetOutputDumpFileName(stream_label, receiver, resolution), resolution);
138 absl::optional<std::string> frame_ids_file =
139 GetOutputFrameIdsDumpFileName(stream_label, receiver, resolution);
140 if (frame_ids_file.has_value()) {
141 writer = CreateVideoFrameWithIdsWriter(std::move(writer), *frame_ids_file);
142 }
143 return writer;
144 }
145
146 std::unique_ptr<test::VideoFrameWriter>
Y4mVideoFrameWriterFactory(absl::string_view file_name_prefix,const VideoResolution & resolution)147 VideoDumpOptions::Y4mVideoFrameWriterFactory(
148 absl::string_view file_name_prefix,
149 const VideoResolution& resolution) {
150 return std::make_unique<test::Y4mVideoFrameWriterImpl>(
151 std::string(file_name_prefix) + ".y4m", resolution.width(),
152 resolution.height(), resolution.fps());
153 }
154
GetInputDumpFileName(absl::string_view stream_label,const VideoResolution & resolution) const155 std::string VideoDumpOptions::GetInputDumpFileName(
156 absl::string_view stream_label,
157 const VideoResolution& resolution) const {
158 rtc::StringBuilder file_name;
159 file_name << stream_label;
160 AppendResolution(resolution, file_name);
161 return test::JoinFilename(output_directory_, file_name.Release());
162 }
163
GetInputFrameIdsDumpFileName(absl::string_view stream_label,const VideoResolution & resolution) const164 absl::optional<std::string> VideoDumpOptions::GetInputFrameIdsDumpFileName(
165 absl::string_view stream_label,
166 const VideoResolution& resolution) const {
167 if (!export_frame_ids_) {
168 return absl::nullopt;
169 }
170 return GetInputDumpFileName(stream_label, resolution) + ".frame_ids.txt";
171 }
172
GetOutputDumpFileName(absl::string_view stream_label,absl::string_view receiver,const VideoResolution & resolution) const173 std::string VideoDumpOptions::GetOutputDumpFileName(
174 absl::string_view stream_label,
175 absl::string_view receiver,
176 const VideoResolution& resolution) const {
177 rtc::StringBuilder file_name;
178 file_name << stream_label << "_" << receiver;
179 AppendResolution(resolution, file_name);
180 return test::JoinFilename(output_directory_, file_name.Release());
181 }
182
GetOutputFrameIdsDumpFileName(absl::string_view stream_label,absl::string_view receiver,const VideoResolution & resolution) const183 absl::optional<std::string> VideoDumpOptions::GetOutputFrameIdsDumpFileName(
184 absl::string_view stream_label,
185 absl::string_view receiver,
186 const VideoResolution& resolution) const {
187 if (!export_frame_ids_) {
188 return absl::nullopt;
189 }
190 return GetOutputDumpFileName(stream_label, receiver, resolution) +
191 ".frame_ids.txt";
192 }
193
ToString() const194 std::string VideoDumpOptions::ToString() const {
195 rtc::StringBuilder out;
196 out << "{ output_directory_=" << output_directory_
197 << ", sampling_modulo_=" << sampling_modulo_
198 << ", export_frame_ids_=" << export_frame_ids_ << " }";
199 return out.Release();
200 }
201
VideoConfig(const VideoResolution & resolution)202 VideoConfig::VideoConfig(const VideoResolution& resolution)
203 : width(resolution.width()),
204 height(resolution.height()),
205 fps(resolution.fps()) {
206 RTC_CHECK(resolution.IsRegular());
207 }
VideoConfig(size_t width,size_t height,int32_t fps)208 VideoConfig::VideoConfig(size_t width, size_t height, int32_t fps)
209 : width(width), height(height), fps(fps) {}
VideoConfig(std::string stream_label,size_t width,size_t height,int32_t fps)210 VideoConfig::VideoConfig(std::string stream_label,
211 size_t width,
212 size_t height,
213 int32_t fps)
214 : width(width),
215 height(height),
216 fps(fps),
217 stream_label(std::move(stream_label)) {}
218
AudioConfig(std::string stream_label)219 AudioConfig::AudioConfig(std::string stream_label)
220 : stream_label(std::move(stream_label)) {}
221
VideoCodecConfig(std::string name)222 VideoCodecConfig::VideoCodecConfig(std::string name)
223 : name(std::move(name)), required_params() {}
VideoCodecConfig(std::string name,std::map<std::string,std::string> required_params)224 VideoCodecConfig::VideoCodecConfig(
225 std::string name,
226 std::map<std::string, std::string> required_params)
227 : name(std::move(name)), required_params(std::move(required_params)) {}
228
GetMaxResolution(rtc::ArrayView<const VideoConfig> video_configs)229 absl::optional<VideoResolution> VideoSubscription::GetMaxResolution(
230 rtc::ArrayView<const VideoConfig> video_configs) {
231 std::vector<VideoResolution> resolutions;
232 for (const auto& video_config : video_configs) {
233 resolutions.push_back(video_config.GetResolution());
234 }
235 return GetMaxResolution(resolutions);
236 }
237
GetMaxResolution(rtc::ArrayView<const VideoResolution> resolutions)238 absl::optional<VideoResolution> VideoSubscription::GetMaxResolution(
239 rtc::ArrayView<const VideoResolution> resolutions) {
240 if (resolutions.empty()) {
241 return absl::nullopt;
242 }
243
244 VideoResolution max_resolution;
245 for (const VideoResolution& resolution : resolutions) {
246 if (max_resolution.width() < resolution.width()) {
247 max_resolution.set_width(resolution.width());
248 }
249 if (max_resolution.height() < resolution.height()) {
250 max_resolution.set_height(resolution.height());
251 }
252 if (max_resolution.fps() < resolution.fps()) {
253 max_resolution.set_fps(resolution.fps());
254 }
255 }
256 return max_resolution;
257 }
258
operator ==(const VideoSubscription & other) const259 bool VideoSubscription::operator==(const VideoSubscription& other) const {
260 return default_resolution_ == other.default_resolution_ &&
261 peers_resolution_ == other.peers_resolution_;
262 }
operator !=(const VideoSubscription & other) const263 bool VideoSubscription::operator!=(const VideoSubscription& other) const {
264 return !(*this == other);
265 }
266
SubscribeToPeer(absl::string_view peer_name,VideoResolution resolution)267 VideoSubscription& VideoSubscription::SubscribeToPeer(
268 absl::string_view peer_name,
269 VideoResolution resolution) {
270 peers_resolution_[std::string(peer_name)] = resolution;
271 return *this;
272 }
273
SubscribeToAllPeers(VideoResolution resolution)274 VideoSubscription& VideoSubscription::SubscribeToAllPeers(
275 VideoResolution resolution) {
276 default_resolution_ = resolution;
277 return *this;
278 }
279
GetResolutionForPeer(absl::string_view peer_name) const280 absl::optional<VideoResolution> VideoSubscription::GetResolutionForPeer(
281 absl::string_view peer_name) const {
282 auto it = peers_resolution_.find(std::string(peer_name));
283 if (it == peers_resolution_.end()) {
284 return default_resolution_;
285 }
286 return it->second;
287 }
288
GetSubscribedPeers() const289 std::vector<std::string> VideoSubscription::GetSubscribedPeers() const {
290 std::vector<std::string> subscribed_streams;
291 subscribed_streams.reserve(peers_resolution_.size());
292 for (const auto& entry : peers_resolution_) {
293 subscribed_streams.push_back(entry.first);
294 }
295 return subscribed_streams;
296 }
297
ToString() const298 std::string VideoSubscription::ToString() const {
299 rtc::StringBuilder out;
300 out << "{ default_resolution_=[";
301 if (default_resolution_.has_value()) {
302 out << default_resolution_->ToString();
303 } else {
304 out << "undefined";
305 }
306 out << "], {";
307 for (const auto& [peer_name, resolution] : peers_resolution_) {
308 out << "[" << peer_name << ": " << resolution.ToString() << "], ";
309 }
310 out << "} }";
311 return out.Release();
312 }
313 } // namespace webrtc_pc_e2e
314 } // namespace webrtc
315