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 #include "test/pc/e2e/media/media_helper.h"
11
12 #include <string>
13 #include <utility>
14
15 #include "absl/types/variant.h"
16 #include "api/media_stream_interface.h"
17 #include "api/test/create_frame_generator.h"
18 #include "api/test/pclf/media_configuration.h"
19 #include "api/test/pclf/peer_configurer.h"
20 #include "test/frame_generator_capturer.h"
21 #include "test/platform_video_capturer.h"
22 #include "test/testsupport/file_utils.h"
23
24 namespace webrtc {
25 namespace webrtc_pc_e2e {
26
MaybeAddAudio(TestPeer * peer)27 void MediaHelper::MaybeAddAudio(TestPeer* peer) {
28 if (!peer->params().audio_config) {
29 return;
30 }
31 const AudioConfig& audio_config = peer->params().audio_config.value();
32 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
33 peer->pc_factory()->CreateAudioSource(audio_config.audio_options);
34 rtc::scoped_refptr<AudioTrackInterface> track =
35 peer->pc_factory()->CreateAudioTrack(*audio_config.stream_label,
36 source.get());
37 std::string sync_group = audio_config.sync_group
38 ? audio_config.sync_group.value()
39 : audio_config.stream_label.value();
40 peer->AddTrack(track, {sync_group, *audio_config.stream_label});
41 }
42
43 std::vector<rtc::scoped_refptr<TestVideoCapturerVideoTrackSource>>
MaybeAddVideo(TestPeer * peer)44 MediaHelper::MaybeAddVideo(TestPeer* peer) {
45 // Params here valid because of pre-run validation.
46 const Params& params = peer->params();
47 const ConfigurableParams& configurable_params = peer->configurable_params();
48 std::vector<rtc::scoped_refptr<TestVideoCapturerVideoTrackSource>> out;
49 for (size_t i = 0; i < configurable_params.video_configs.size(); ++i) {
50 const VideoConfig& video_config = configurable_params.video_configs[i];
51 // Setup input video source into peer connection.
52 std::unique_ptr<test::TestVideoCapturer> capturer = CreateVideoCapturer(
53 video_config, peer->ReleaseVideoSource(i),
54 video_quality_analyzer_injection_helper_->CreateFramePreprocessor(
55 params.name.value(), video_config));
56 bool is_screencast =
57 video_config.content_hint == VideoTrackInterface::ContentHint::kText ||
58 video_config.content_hint ==
59 VideoTrackInterface::ContentHint::kDetailed;
60 rtc::scoped_refptr<TestVideoCapturerVideoTrackSource> source =
61 rtc::make_ref_counted<TestVideoCapturerVideoTrackSource>(
62 std::move(capturer), is_screencast);
63 out.push_back(source);
64 RTC_LOG(LS_INFO) << "Adding video with video_config.stream_label="
65 << video_config.stream_label.value();
66 rtc::scoped_refptr<VideoTrackInterface> track =
67 peer->pc_factory()->CreateVideoTrack(video_config.stream_label.value(),
68 source.get());
69 if (video_config.content_hint.has_value()) {
70 track->set_content_hint(video_config.content_hint.value());
71 }
72 std::string sync_group = video_config.sync_group
73 ? video_config.sync_group.value()
74 : video_config.stream_label.value();
75 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> sender =
76 peer->AddTrack(track, {sync_group, *video_config.stream_label});
77 RTC_CHECK(sender.ok());
78 if (video_config.temporal_layers_count ||
79 video_config.degradation_preference) {
80 RtpParameters rtp_parameters = sender.value()->GetParameters();
81 if (video_config.temporal_layers_count) {
82 for (auto& encoding_parameters : rtp_parameters.encodings) {
83 encoding_parameters.num_temporal_layers =
84 video_config.temporal_layers_count;
85 }
86 }
87 if (video_config.degradation_preference) {
88 rtp_parameters.degradation_preference =
89 video_config.degradation_preference;
90 }
91 RTCError res = sender.value()->SetParameters(rtp_parameters);
92 RTC_CHECK(res.ok()) << "Failed to set RTP parameters";
93 }
94 }
95 return out;
96 }
97
CreateVideoCapturer(const VideoConfig & video_config,PeerConfigurer::VideoSource source,std::unique_ptr<test::TestVideoCapturer::FramePreprocessor> frame_preprocessor)98 std::unique_ptr<test::TestVideoCapturer> MediaHelper::CreateVideoCapturer(
99 const VideoConfig& video_config,
100 PeerConfigurer::VideoSource source,
101 std::unique_ptr<test::TestVideoCapturer::FramePreprocessor>
102 frame_preprocessor) {
103 CapturingDeviceIndex* capturing_device_index =
104 absl::get_if<CapturingDeviceIndex>(&source);
105 if (capturing_device_index != nullptr) {
106 std::unique_ptr<test::TestVideoCapturer> capturer =
107 test::CreateVideoCapturer(video_config.width, video_config.height,
108 video_config.fps,
109 static_cast<size_t>(*capturing_device_index));
110 RTC_CHECK(capturer)
111 << "Failed to obtain input stream from capturing device #"
112 << *capturing_device_index;
113 capturer->SetFramePreprocessor(std::move(frame_preprocessor));
114 return capturer;
115 }
116
117 auto capturer = std::make_unique<test::FrameGeneratorCapturer>(
118 clock_,
119 absl::get<std::unique_ptr<test::FrameGeneratorInterface>>(
120 std::move(source)),
121 video_config.fps, *task_queue_factory_);
122 capturer->SetFramePreprocessor(std::move(frame_preprocessor));
123 capturer->Init();
124 return capturer;
125 }
126
127 } // namespace webrtc_pc_e2e
128 } // namespace webrtc
129