blob: 1a3dd3109a92f2d6cb474793295d15cae1133094 [file] [log] [blame]
wu@webrtc.org364f2042013-11-20 21:49:411/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
wu@webrtc.org364f2042013-11-20 21:49:413 *
kjellanderb24317b2016-02-10 15:54:434 * 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.
wu@webrtc.org364f2042013-11-20 21:49:419 */
10
Jonas Olssona4d87372019-07-05 17:08:3311#include "pc/test/peer_connection_test_wrapper.h"
12
Yves Gerey3e707812018-11-28 15:47:4913#include <stddef.h>
Jonas Olssona4d87372019-07-05 17:08:3314
Mirko Bonadei317a1f02019-09-17 15:06:1815#include <memory>
Steve Anton36b29d12017-10-30 16:57:4216#include <string>
kwiberg0eb15ed2015-12-17 11:04:1517#include <utility>
Steve Anton191c39f2018-01-25 03:35:5518#include <vector>
kwiberg0eb15ed2015-12-17 11:04:1519
Florent Castelli43a5dd82023-04-12 10:45:0720#include "absl/strings/match.h"
Yves Gerey3e707812018-11-28 15:47:4921#include "absl/types/optional.h"
22#include "api/audio/audio_mixer.h"
Mirko Bonadei2ff3f492018-11-22 08:00:1323#include "api/create_peerconnection_factory.h"
Florent Castelli43a5dd82023-04-12 10:45:0724#include "api/media_types.h"
Artem Titovd15a5752021-02-10 13:31:2425#include "api/sequence_checker.h"
Yves Gerey3e707812018-11-28 15:47:4926#include "api/video_codecs/video_decoder_factory.h"
Åsa Persson13fffbb2023-03-31 11:13:3727#include "api/video_codecs/video_decoder_factory_template.h"
28#include "api/video_codecs/video_decoder_factory_template_dav1d_adapter.h"
29#include "api/video_codecs/video_decoder_factory_template_libvpx_vp8_adapter.h"
30#include "api/video_codecs/video_decoder_factory_template_libvpx_vp9_adapter.h"
31#include "api/video_codecs/video_decoder_factory_template_open_h264_adapter.h"
Yves Gerey3e707812018-11-28 15:47:4932#include "api/video_codecs/video_encoder_factory.h"
philipel2cafacf2023-04-03 13:17:1733#include "api/video_codecs/video_encoder_factory_template.h"
34#include "api/video_codecs/video_encoder_factory_template_libaom_av1_adapter.h"
35#include "api/video_codecs/video_encoder_factory_template_libvpx_vp8_adapter.h"
36#include "api/video_codecs/video_encoder_factory_template_libvpx_vp9_adapter.h"
37#include "api/video_codecs/video_encoder_factory_template_open_h264_adapter.h"
38#include "media/engine/simulcast_encoder_adapter.h"
Yves Gerey3e707812018-11-28 15:47:4939#include "modules/audio_device/include/audio_device.h"
Anders Carlsson67537952018-05-03 09:28:2940#include "modules/audio_processing/include/audio_processing.h"
Steve Anton10542f22019-01-11 17:11:0041#include "p2p/base/fake_port_allocator.h"
42#include "p2p/base/port_allocator.h"
43#include "pc/test/fake_periodic_video_source.h"
Steve Anton10542f22019-01-11 17:11:0044#include "pc/test/fake_rtc_certificate_generator.h"
45#include "pc/test/mock_peer_connection_observers.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3146#include "rtc_base/gunit.h"
Yves Gerey3e707812018-11-28 15:47:4947#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 17:11:0048#include "rtc_base/rtc_certificate_generator.h"
49#include "rtc_base/string_encode.h"
Steve Anton10542f22019-01-11 17:11:0050#include "rtc_base/time_utils.h"
Yves Gerey3e707812018-11-28 15:47:4951#include "test/gtest.h"
wu@webrtc.org364f2042013-11-20 21:49:4152
wu@webrtc.org364f2042013-11-20 21:49:4153using webrtc::FakeVideoTrackRenderer;
54using webrtc::IceCandidateInterface;
wu@webrtc.org364f2042013-11-20 21:49:4155using webrtc::MediaStreamInterface;
Steve Anton191c39f2018-01-25 03:35:5556using webrtc::MediaStreamTrackInterface;
wu@webrtc.org364f2042013-11-20 21:49:4157using webrtc::MockSetSessionDescriptionObserver;
58using webrtc::PeerConnectionInterface;
Steve Anton191c39f2018-01-25 03:35:5559using webrtc::RtpReceiverInterface;
Steve Antona3a92c22017-12-07 18:27:4160using webrtc::SdpType;
wu@webrtc.org364f2042013-11-20 21:49:4161using webrtc::SessionDescriptionInterface;
62using webrtc::VideoTrackInterface;
63
Steve Antona3a92c22017-12-07 18:27:4164namespace {
Seth Hampson845e8782018-03-02 19:34:1065const char kStreamIdBase[] = "stream_id";
Steve Antona3a92c22017-12-07 18:27:4166const char kVideoTrackLabelBase[] = "video_track";
67const char kAudioTrackLabelBase[] = "audio_track";
68constexpr int kMaxWait = 10000;
69constexpr int kTestAudioFrameCount = 3;
70constexpr int kTestVideoFrameCount = 3;
philipel2cafacf2023-04-03 13:17:1771
72class FuzzyMatchedVideoEncoderFactory : public webrtc::VideoEncoderFactory {
73 public:
74 std::vector<webrtc::SdpVideoFormat> GetSupportedFormats() const override {
75 return factory_.GetSupportedFormats();
76 }
77
78 std::unique_ptr<webrtc::VideoEncoder> CreateVideoEncoder(
79 const webrtc::SdpVideoFormat& format) override {
80 if (absl::optional<webrtc::SdpVideoFormat> original_format =
81 webrtc::FuzzyMatchSdpVideoFormat(factory_.GetSupportedFormats(),
82 format)) {
83 return std::make_unique<webrtc::SimulcastEncoderAdapter>(
84 &factory_, *original_format);
85 }
86
87 return nullptr;
88 }
89
90 CodecSupport QueryCodecSupport(
91 const webrtc::SdpVideoFormat& format,
92 absl::optional<std::string> scalability_mode) const override {
93 return factory_.QueryCodecSupport(format, scalability_mode);
94 }
95
96 private:
97 webrtc::VideoEncoderFactoryTemplate<webrtc::LibvpxVp8EncoderTemplateAdapter,
98 webrtc::LibvpxVp9EncoderTemplateAdapter,
99 webrtc::OpenH264EncoderTemplateAdapter,
100 webrtc::LibaomAv1EncoderTemplateAdapter>
101 factory_;
102};
Steve Antona3a92c22017-12-07 18:27:41103} // namespace
104
wu@webrtc.org364f2042013-11-20 21:49:41105void PeerConnectionTestWrapper::Connect(PeerConnectionTestWrapper* caller,
106 PeerConnectionTestWrapper* callee) {
107 caller->SignalOnIceCandidateReady.connect(
108 callee, &PeerConnectionTestWrapper::AddIceCandidate);
109 callee->SignalOnIceCandidateReady.connect(
110 caller, &PeerConnectionTestWrapper::AddIceCandidate);
111
Yves Gerey665174f2018-06-19 13:03:05112 caller->SignalOnSdpReady.connect(callee,
113 &PeerConnectionTestWrapper::ReceiveOfferSdp);
wu@webrtc.org364f2042013-11-20 21:49:41114 callee->SignalOnSdpReady.connect(
115 caller, &PeerConnectionTestWrapper::ReceiveAnswerSdp);
116}
117
danilchape9021a32016-05-17 08:52:02118PeerConnectionTestWrapper::PeerConnectionTestWrapper(
119 const std::string& name,
Byoungchan Leed58f5262022-06-27 09:05:22120 rtc::SocketServer* socket_server,
danilchape9021a32016-05-17 08:52:02121 rtc::Thread* network_thread,
122 rtc::Thread* worker_thread)
123 : name_(name),
Byoungchan Leed58f5262022-06-27 09:05:22124 socket_server_(socket_server),
danilchape9021a32016-05-17 08:52:02125 network_thread_(network_thread),
Henrik Boström4c1e7cc2020-06-11 10:26:53126 worker_thread_(worker_thread),
127 pending_negotiation_(false) {
Sebastian Janssonc01367d2019-04-08 13:20:44128 pc_thread_checker_.Detach();
Yves Gerey59cfd352018-11-26 15:22:20129}
wu@webrtc.org364f2042013-11-20 21:49:41130
Yves Gerey59cfd352018-11-26 15:22:20131PeerConnectionTestWrapper::~PeerConnectionTestWrapper() {
132 RTC_DCHECK_RUN_ON(&pc_thread_checker_);
Henrik Boström89f095c2023-04-03 15:57:47133 // To avoid flaky bot failures, make sure fake sources are stopped prior to
134 // closing the peer connections. See https://crbug.com/webrtc/15018.
135 StopFakeVideoSources();
Yves Gerey59cfd352018-11-26 15:22:20136 // Either network_thread or worker_thread might be active at this point.
137 // Relying on ~PeerConnection to properly wait for them doesn't work,
138 // as a vptr race might occur (before we enter the destruction body).
139 // See: bugs.webrtc.org/9847
140 if (pc()) {
141 pc()->Close();
142 }
143}
wu@webrtc.org364f2042013-11-20 21:49:41144
145bool PeerConnectionTestWrapper::CreatePc(
kwiberg9e5b11e2017-04-19 10:47:57146 const webrtc::PeerConnectionInterface::RTCConfiguration& config,
147 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
148 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory) {
kwibergd1fe2812016-04-27 13:47:29149 std::unique_ptr<cricket::PortAllocator> port_allocator(
Byoungchan Leed58f5262022-06-27 09:05:22150 new cricket::FakePortAllocator(
151 network_thread_,
Sameer Vijaykar0793ee72023-01-23 15:31:29152 std::make_unique<rtc::BasicPacketSocketFactory>(socket_server_),
153 &field_trials_));
wu@webrtc.org364f2042013-11-20 21:49:41154
Yves Gerey59cfd352018-11-26 15:22:20155 RTC_DCHECK_RUN_ON(&pc_thread_checker_);
156
deadbeefee8c6d32015-08-13 21:27:18157 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
Niels Möllerafb246b2022-04-20 12:26:50158 if (fake_audio_capture_module_ == nullptr) {
wu@webrtc.org364f2042013-11-20 21:49:41159 return false;
160 }
161
162 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 08:52:02163 network_thread_, worker_thread_, rtc::Thread::Current(),
Anders Carlsson67537952018-05-03 09:28:29164 rtc::scoped_refptr<webrtc::AudioDeviceModule>(fake_audio_capture_module_),
165 audio_encoder_factory, audio_decoder_factory,
philipel2cafacf2023-04-03 13:17:17166 std::make_unique<FuzzyMatchedVideoEncoderFactory>(),
Åsa Persson13fffbb2023-03-31 11:13:37167 std::make_unique<webrtc::VideoDecoderFactoryTemplate<
168 webrtc::LibvpxVp8DecoderTemplateAdapter,
169 webrtc::LibvpxVp9DecoderTemplateAdapter,
170 webrtc::OpenH264DecoderTemplateAdapter,
171 webrtc::Dav1dDecoderTemplateAdapter>>(),
172 nullptr /* audio_mixer */, nullptr /* audio_processing */);
wu@webrtc.org364f2042013-11-20 21:49:41173 if (!peer_connection_factory_) {
174 return false;
175 }
176
Henrik Boströmd79599d2016-06-01 11:58:50177 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator(
deadbeef1b54a5f2017-01-24 03:39:57178 new FakeRTCCertificateGenerator());
Harald Alvestrandf33f7a22021-05-09 14:58:57179 webrtc::PeerConnectionDependencies deps(this);
180 deps.allocator = std::move(port_allocator);
181 deps.cert_generator = std::move(cert_generator);
182 auto result = peer_connection_factory_->CreatePeerConnectionOrError(
183 config, std::move(deps));
184 if (result.ok()) {
185 peer_connection_ = result.MoveValue();
186 return true;
187 } else {
188 return false;
189 }
wu@webrtc.org364f2042013-11-20 21:49:41190}
191
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52192rtc::scoped_refptr<webrtc::DataChannelInterface>
jiayl@webrtc.org1a6c6282014-06-12 21:59:29193PeerConnectionTestWrapper::CreateDataChannel(
194 const std::string& label,
195 const webrtc::DataChannelInit& init) {
Harald Alvestranda9af50f2021-05-21 13:33:51196 auto result = peer_connection_->CreateDataChannelOrError(label, &init);
197 if (!result.ok()) {
198 RTC_LOG(LS_ERROR) << "CreateDataChannel failed: "
199 << ToString(result.error().type()) << " "
200 << result.error().message();
201 return nullptr;
202 }
203 return result.MoveValue();
jiayl@webrtc.org1a6c6282014-06-12 21:59:29204}
205
Florent Castelli43a5dd82023-04-12 10:45:07206absl::optional<webrtc::RtpCodecCapability>
207PeerConnectionTestWrapper::FindFirstSendCodecWithName(
208 cricket::MediaType media_type,
209 const std::string& name) const {
210 std::vector<webrtc::RtpCodecCapability> codecs =
211 peer_connection_factory_->GetRtpSenderCapabilities(media_type).codecs;
212 for (const auto& codec : codecs) {
213 if (absl::EqualsIgnoreCase(codec.name, name)) {
214 return codec;
215 }
216 }
217 return absl::nullopt;
218}
219
Henrik Boström4c1e7cc2020-06-11 10:26:53220void PeerConnectionTestWrapper::WaitForNegotiation() {
221 EXPECT_TRUE_WAIT(!pending_negotiation_, kMaxWait);
222}
223
224void PeerConnectionTestWrapper::OnSignalingChange(
225 webrtc::PeerConnectionInterface::SignalingState new_state) {
226 if (new_state == webrtc::PeerConnectionInterface::SignalingState::kStable) {
227 pending_negotiation_ = false;
228 }
229}
230
Steve Anton191c39f2018-01-25 03:35:55231void PeerConnectionTestWrapper::OnAddTrack(
232 rtc::scoped_refptr<RtpReceiverInterface> receiver,
233 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
234 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": OnAddTrack";
235 if (receiver->track()->kind() == MediaStreamTrackInterface::kVideoKind) {
236 auto* video_track =
237 static_cast<VideoTrackInterface*>(receiver->track().get());
Mirko Bonadei317a1f02019-09-17 15:06:18238 renderer_ = std::make_unique<FakeVideoTrackRenderer>(video_track);
wu@webrtc.org364f2042013-11-20 21:49:41239 }
240}
241
242void PeerConnectionTestWrapper::OnIceCandidate(
243 const IceCandidateInterface* candidate) {
244 std::string sdp;
245 EXPECT_TRUE(candidate->ToString(&sdp));
wu@webrtc.org364f2042013-11-20 21:49:41246 SignalOnIceCandidateReady(candidate->sdp_mid(), candidate->sdp_mline_index(),
247 sdp);
248}
249
jiayl@webrtc.org1a6c6282014-06-12 21:59:29250void PeerConnectionTestWrapper::OnDataChannel(
Taylor Brandstetter98cde262016-05-31 20:02:21251 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
Niels Möllerafb246b2022-04-20 12:26:50252 SignalOnDataChannel(data_channel.get());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29253}
254
wu@webrtc.org364f2042013-11-20 21:49:41255void PeerConnectionTestWrapper::OnSuccess(SessionDescriptionInterface* desc) {
Artem Titov880fa812021-07-30 20:30:23256 // This callback should take the ownership of `desc`.
kwibergd1fe2812016-04-27 13:47:29257 std::unique_ptr<SessionDescriptionInterface> owned_desc(desc);
wu@webrtc.org364f2042013-11-20 21:49:41258 std::string sdp;
259 EXPECT_TRUE(desc->ToString(&sdp));
260
Mirko Bonadei675513b2017-11-09 10:09:25261 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": "
Steve Antona3a92c22017-12-07 18:27:41262 << webrtc::SdpTypeToString(desc->GetType())
263 << " sdp created: " << sdp;
wu@webrtc.org364f2042013-11-20 21:49:41264
Steve Antona3a92c22017-12-07 18:27:41265 SetLocalDescription(desc->GetType(), sdp);
wu@webrtc.org364f2042013-11-20 21:49:41266
267 SignalOnSdpReady(sdp);
268}
269
270void PeerConnectionTestWrapper::CreateOffer(
Niels Möllerf06f9232018-08-07 10:32:18271 const webrtc::PeerConnectionInterface::RTCOfferAnswerOptions& options) {
Mirko Bonadei675513b2017-11-09 10:09:25272 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": CreateOffer.";
Henrik Boström4c1e7cc2020-06-11 10:26:53273 pending_negotiation_ = true;
Niels Möllerf06f9232018-08-07 10:32:18274 peer_connection_->CreateOffer(this, options);
wu@webrtc.org364f2042013-11-20 21:49:41275}
276
277void PeerConnectionTestWrapper::CreateAnswer(
Niels Möllerf06f9232018-08-07 10:32:18278 const webrtc::PeerConnectionInterface::RTCOfferAnswerOptions& options) {
Mirko Bonadei675513b2017-11-09 10:09:25279 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
280 << ": CreateAnswer.";
Henrik Boström4c1e7cc2020-06-11 10:26:53281 pending_negotiation_ = true;
Niels Möllerf06f9232018-08-07 10:32:18282 peer_connection_->CreateAnswer(this, options);
wu@webrtc.org364f2042013-11-20 21:49:41283}
284
285void PeerConnectionTestWrapper::ReceiveOfferSdp(const std::string& sdp) {
Steve Antona3a92c22017-12-07 18:27:41286 SetRemoteDescription(SdpType::kOffer, sdp);
Niels Möllerf06f9232018-08-07 10:32:18287 CreateAnswer(webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
wu@webrtc.org364f2042013-11-20 21:49:41288}
289
290void PeerConnectionTestWrapper::ReceiveAnswerSdp(const std::string& sdp) {
Steve Antona3a92c22017-12-07 18:27:41291 SetRemoteDescription(SdpType::kAnswer, sdp);
wu@webrtc.org364f2042013-11-20 21:49:41292}
293
Steve Antona3a92c22017-12-07 18:27:41294void PeerConnectionTestWrapper::SetLocalDescription(SdpType type,
wu@webrtc.org364f2042013-11-20 21:49:41295 const std::string& sdp) {
Mirko Bonadei675513b2017-11-09 10:09:25296 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
Steve Antona3a92c22017-12-07 18:27:41297 << ": SetLocalDescription " << webrtc::SdpTypeToString(type)
298 << " " << sdp;
wu@webrtc.org364f2042013-11-20 21:49:41299
Tommi87f70902021-04-27 12:43:08300 auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
wu@webrtc.org364f2042013-11-20 21:49:41301 peer_connection_->SetLocalDescription(
Niels Möllerafb246b2022-04-20 12:26:50302 observer.get(), webrtc::CreateSessionDescription(type, sdp).release());
wu@webrtc.org364f2042013-11-20 21:49:41303}
304
Steve Antona3a92c22017-12-07 18:27:41305void PeerConnectionTestWrapper::SetRemoteDescription(SdpType type,
wu@webrtc.org364f2042013-11-20 21:49:41306 const std::string& sdp) {
Mirko Bonadei675513b2017-11-09 10:09:25307 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
Steve Antona3a92c22017-12-07 18:27:41308 << ": SetRemoteDescription " << webrtc::SdpTypeToString(type)
309 << " " << sdp;
wu@webrtc.org364f2042013-11-20 21:49:41310
Tommi87f70902021-04-27 12:43:08311 auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
wu@webrtc.org364f2042013-11-20 21:49:41312 peer_connection_->SetRemoteDescription(
Niels Möllerafb246b2022-04-20 12:26:50313 observer.get(), webrtc::CreateSessionDescription(type, sdp).release());
wu@webrtc.org364f2042013-11-20 21:49:41314}
315
316void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid,
317 int sdp_mline_index,
318 const std::string& candidate) {
kwibergd1fe2812016-04-27 13:47:29319 std::unique_ptr<webrtc::IceCandidateInterface> owned_candidate(
sergeyu@chromium.org5bc25c42013-12-05 00:24:06320 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, candidate, NULL));
321 EXPECT_TRUE(peer_connection_->AddIceCandidate(owned_candidate.get()));
wu@webrtc.org364f2042013-11-20 21:49:41322}
323
324void PeerConnectionTestWrapper::WaitForCallEstablished() {
325 WaitForConnection();
326 WaitForAudio();
327 WaitForVideo();
328}
329
330void PeerConnectionTestWrapper::WaitForConnection() {
331 EXPECT_TRUE_WAIT(CheckForConnection(), kMaxWait);
Mirko Bonadei675513b2017-11-09 10:09:25332 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": Connected.";
wu@webrtc.org364f2042013-11-20 21:49:41333}
334
335bool PeerConnectionTestWrapper::CheckForConnection() {
336 return (peer_connection_->ice_connection_state() ==
mallinath@webrtc.org385857d2014-02-14 00:56:12337 PeerConnectionInterface::kIceConnectionConnected) ||
338 (peer_connection_->ice_connection_state() ==
339 PeerConnectionInterface::kIceConnectionCompleted);
wu@webrtc.org364f2042013-11-20 21:49:41340}
341
342void PeerConnectionTestWrapper::WaitForAudio() {
343 EXPECT_TRUE_WAIT(CheckForAudio(), kMaxWait);
Mirko Bonadei675513b2017-11-09 10:09:25344 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
345 << ": Got enough audio frames.";
wu@webrtc.org364f2042013-11-20 21:49:41346}
347
348bool PeerConnectionTestWrapper::CheckForAudio() {
349 return (fake_audio_capture_module_->frames_received() >=
350 kTestAudioFrameCount);
351}
352
353void PeerConnectionTestWrapper::WaitForVideo() {
354 EXPECT_TRUE_WAIT(CheckForVideo(), kMaxWait);
Mirko Bonadei675513b2017-11-09 10:09:25355 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
356 << ": Got enough video frames.";
wu@webrtc.org364f2042013-11-20 21:49:41357}
358
359bool PeerConnectionTestWrapper::CheckForVideo() {
360 if (!renderer_) {
361 return false;
362 }
363 return (renderer_->num_rendered_frames() >= kTestVideoFrameCount);
364}
365
366void PeerConnectionTestWrapper::GetAndAddUserMedia(
Yves Gerey665174f2018-06-19 13:03:05367 bool audio,
368 const cricket::AudioOptions& audio_options,
Niels Möller5c4ddad2019-02-12 11:30:58369 bool video) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52370 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
Niels Möller5c4ddad2019-02-12 11:30:58371 GetUserMedia(audio, audio_options, video);
Mirko Bonadei739baf02019-01-27 16:29:42372 for (const auto& audio_track : stream->GetAudioTracks()) {
Seth Hampson13b8bad2018-03-13 23:05:28373 EXPECT_TRUE(peer_connection_->AddTrack(audio_track, {stream->id()}).ok());
Steve Anton191c39f2018-01-25 03:35:55374 }
Mirko Bonadei739baf02019-01-27 16:29:42375 for (const auto& video_track : stream->GetVideoTracks()) {
Seth Hampson13b8bad2018-03-13 23:05:28376 EXPECT_TRUE(peer_connection_->AddTrack(video_track, {stream->id()}).ok());
Steve Anton191c39f2018-01-25 03:35:55377 }
wu@webrtc.org364f2042013-11-20 21:49:41378}
379
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52380rtc::scoped_refptr<webrtc::MediaStreamInterface>
Niels Möller2d02e082018-05-21 09:23:35381PeerConnectionTestWrapper::GetUserMedia(
Yves Gerey665174f2018-06-19 13:03:05382 bool audio,
383 const cricket::AudioOptions& audio_options,
Henrik Boströmfd4ddd12023-02-08 10:29:20384 bool video,
385 webrtc::Resolution resolution) {
Seth Hampson845e8782018-03-02 19:34:10386 std::string stream_id =
387 kStreamIdBase + rtc::ToString(num_get_user_media_calls_++);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52388 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
Seth Hampson845e8782018-03-02 19:34:10389 peer_connection_factory_->CreateLocalMediaStream(stream_id);
wu@webrtc.org364f2042013-11-20 21:49:41390
391 if (audio) {
Niels Möller2d02e082018-05-21 09:23:35392 cricket::AudioOptions options = audio_options;
wu@webrtc.org364f2042013-11-20 21:49:41393 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 09:23:35394 options.highpass_filter = false;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52395 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 09:23:35396 peer_connection_factory_->CreateAudioSource(options);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52397 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
wu@webrtc.org364f2042013-11-20 21:49:41398 peer_connection_factory_->CreateAudioTrack(kAudioTrackLabelBase,
Niels Möllerafb246b2022-04-20 12:26:50399 source.get()));
Harald Alvestrand2f7ad282022-04-21 11:35:43400 stream->AddTrack(audio_track);
wu@webrtc.org364f2042013-11-20 21:49:41401 }
402
403 if (video) {
404 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
Niels Möllera1cc73f2018-05-28 14:20:42405 webrtc::FakePeriodicVideoSource::Config config;
406 config.frame_interval_ms = 100;
Johannes Kron965e7942018-09-13 13:36:20407 config.timestamp_offset_ms = rtc::TimeMillis();
Henrik Boströmfd4ddd12023-02-08 10:29:20408 config.width = resolution.width;
409 config.height = resolution.height;
wu@webrtc.org364f2042013-11-20 21:49:41410
Tommi87f70902021-04-27 12:43:08411 auto source = rtc::make_ref_counted<webrtc::FakePeriodicVideoTrackSource>(
412 config, /* remote */ false);
Henrik Boström89f095c2023-04-03 15:57:47413 fake_video_sources_.push_back(source);
Niels Möllera1cc73f2018-05-28 14:20:42414
Seth Hampson845e8782018-03-02 19:34:10415 std::string videotrack_label = stream_id + kVideoTrackLabelBase;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52416 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
Harald Alvestrand041ecb82023-03-20 14:13:42417 peer_connection_factory_->CreateVideoTrack(source, videotrack_label));
wu@webrtc.org364f2042013-11-20 21:49:41418
Harald Alvestrand2f7ad282022-04-21 11:35:43419 stream->AddTrack(video_track);
wu@webrtc.org364f2042013-11-20 21:49:41420 }
421 return stream;
422}
Henrik Boström89f095c2023-04-03 15:57:47423
424void PeerConnectionTestWrapper::StopFakeVideoSources() {
425 for (const auto& fake_video_source : fake_video_sources_) {
426 fake_video_source->fake_periodic_source().Stop();
427 }
428 fake_video_sources_.clear();
429}