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