wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 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. |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 9 | */ |
| 10 | |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 11 | #include "pc/test/peer_connection_test_wrapper.h" |
| 12 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 13 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 14 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 15 | #include <memory> |
Steve Anton | 36b29d1 | 2017-10-30 16:57:42 | [diff] [blame] | 16 | #include <string> |
kwiberg | 0eb15ed | 2015-12-17 11:04:15 | [diff] [blame] | 17 | #include <utility> |
Steve Anton | 191c39f | 2018-01-25 03:35:55 | [diff] [blame] | 18 | #include <vector> |
kwiberg | 0eb15ed | 2015-12-17 11:04:15 | [diff] [blame] | 19 | |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 20 | #include "absl/strings/match.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 21 | #include "absl/types/optional.h" |
| 22 | #include "api/audio/audio_mixer.h" |
Mirko Bonadei | 2ff3f49 | 2018-11-22 08:00:13 | [diff] [blame] | 23 | #include "api/create_peerconnection_factory.h" |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 24 | #include "api/media_types.h" |
Artem Titov | d15a575 | 2021-02-10 13:31:24 | [diff] [blame] | 25 | #include "api/sequence_checker.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 26 | #include "api/video_codecs/video_decoder_factory.h" |
Åsa Persson | 13fffbb | 2023-03-31 11:13:37 | [diff] [blame] | 27 | #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 Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 32 | #include "api/video_codecs/video_encoder_factory.h" |
philipel | 2cafacf | 2023-04-03 13:17:17 | [diff] [blame] | 33 | #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 Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 39 | #include "modules/audio_device/include/audio_device.h" |
Anders Carlsson | 6753795 | 2018-05-03 09:28:29 | [diff] [blame] | 40 | #include "modules/audio_processing/include/audio_processing.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 41 | #include "p2p/base/fake_port_allocator.h" |
| 42 | #include "p2p/base/port_allocator.h" |
| 43 | #include "pc/test/fake_periodic_video_source.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 44 | #include "pc/test/fake_rtc_certificate_generator.h" |
| 45 | #include "pc/test/mock_peer_connection_observers.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 46 | #include "rtc_base/gunit.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 47 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 48 | #include "rtc_base/rtc_certificate_generator.h" |
| 49 | #include "rtc_base/string_encode.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 50 | #include "rtc_base/time_utils.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 51 | #include "test/gtest.h" |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 52 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 53 | using webrtc::FakeVideoTrackRenderer; |
| 54 | using webrtc::IceCandidateInterface; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 55 | using webrtc::MediaStreamInterface; |
Steve Anton | 191c39f | 2018-01-25 03:35:55 | [diff] [blame] | 56 | using webrtc::MediaStreamTrackInterface; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 57 | using webrtc::MockSetSessionDescriptionObserver; |
| 58 | using webrtc::PeerConnectionInterface; |
Steve Anton | 191c39f | 2018-01-25 03:35:55 | [diff] [blame] | 59 | using webrtc::RtpReceiverInterface; |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 60 | using webrtc::SdpType; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 61 | using webrtc::SessionDescriptionInterface; |
| 62 | using webrtc::VideoTrackInterface; |
| 63 | |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 64 | namespace { |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 65 | const char kStreamIdBase[] = "stream_id"; |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 66 | const char kVideoTrackLabelBase[] = "video_track"; |
| 67 | const char kAudioTrackLabelBase[] = "audio_track"; |
| 68 | constexpr int kMaxWait = 10000; |
| 69 | constexpr int kTestAudioFrameCount = 3; |
| 70 | constexpr int kTestVideoFrameCount = 3; |
philipel | 2cafacf | 2023-04-03 13:17:17 | [diff] [blame] | 71 | |
| 72 | class 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 Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 103 | } // namespace |
| 104 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 105 | void 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 Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 112 | caller->SignalOnSdpReady.connect(callee, |
| 113 | &PeerConnectionTestWrapper::ReceiveOfferSdp); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 114 | callee->SignalOnSdpReady.connect( |
| 115 | caller, &PeerConnectionTestWrapper::ReceiveAnswerSdp); |
| 116 | } |
| 117 | |
danilchap | e9021a3 | 2016-05-17 08:52:02 | [diff] [blame] | 118 | PeerConnectionTestWrapper::PeerConnectionTestWrapper( |
| 119 | const std::string& name, |
Byoungchan Lee | d58f526 | 2022-06-27 09:05:22 | [diff] [blame] | 120 | rtc::SocketServer* socket_server, |
danilchap | e9021a3 | 2016-05-17 08:52:02 | [diff] [blame] | 121 | rtc::Thread* network_thread, |
| 122 | rtc::Thread* worker_thread) |
| 123 | : name_(name), |
Byoungchan Lee | d58f526 | 2022-06-27 09:05:22 | [diff] [blame] | 124 | socket_server_(socket_server), |
danilchap | e9021a3 | 2016-05-17 08:52:02 | [diff] [blame] | 125 | network_thread_(network_thread), |
Henrik Boström | 4c1e7cc | 2020-06-11 10:26:53 | [diff] [blame] | 126 | worker_thread_(worker_thread), |
| 127 | pending_negotiation_(false) { |
Sebastian Jansson | c01367d | 2019-04-08 13:20:44 | [diff] [blame] | 128 | pc_thread_checker_.Detach(); |
Yves Gerey | 59cfd35 | 2018-11-26 15:22:20 | [diff] [blame] | 129 | } |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 130 | |
Yves Gerey | 59cfd35 | 2018-11-26 15:22:20 | [diff] [blame] | 131 | PeerConnectionTestWrapper::~PeerConnectionTestWrapper() { |
| 132 | RTC_DCHECK_RUN_ON(&pc_thread_checker_); |
Henrik Boström | 89f095c | 2023-04-03 15:57:47 | [diff] [blame] | 133 | // 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 Gerey | 59cfd35 | 2018-11-26 15:22:20 | [diff] [blame] | 136 | // 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.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 144 | |
| 145 | bool PeerConnectionTestWrapper::CreatePc( |
kwiberg | 9e5b11e | 2017-04-19 10:47:57 | [diff] [blame] | 146 | const webrtc::PeerConnectionInterface::RTCConfiguration& config, |
| 147 | rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory, |
| 148 | rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory) { |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 149 | std::unique_ptr<cricket::PortAllocator> port_allocator( |
Byoungchan Lee | d58f526 | 2022-06-27 09:05:22 | [diff] [blame] | 150 | new cricket::FakePortAllocator( |
| 151 | network_thread_, |
Sameer Vijaykar | 0793ee7 | 2023-01-23 15:31:29 | [diff] [blame] | 152 | std::make_unique<rtc::BasicPacketSocketFactory>(socket_server_), |
| 153 | &field_trials_)); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 154 | |
Yves Gerey | 59cfd35 | 2018-11-26 15:22:20 | [diff] [blame] | 155 | RTC_DCHECK_RUN_ON(&pc_thread_checker_); |
| 156 | |
deadbeef | ee8c6d3 | 2015-08-13 21:27:18 | [diff] [blame] | 157 | fake_audio_capture_module_ = FakeAudioCaptureModule::Create(); |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 158 | if (fake_audio_capture_module_ == nullptr) { |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 159 | return false; |
| 160 | } |
| 161 | |
| 162 | peer_connection_factory_ = webrtc::CreatePeerConnectionFactory( |
danilchap | e9021a3 | 2016-05-17 08:52:02 | [diff] [blame] | 163 | network_thread_, worker_thread_, rtc::Thread::Current(), |
Anders Carlsson | 6753795 | 2018-05-03 09:28:29 | [diff] [blame] | 164 | rtc::scoped_refptr<webrtc::AudioDeviceModule>(fake_audio_capture_module_), |
| 165 | audio_encoder_factory, audio_decoder_factory, |
philipel | 2cafacf | 2023-04-03 13:17:17 | [diff] [blame] | 166 | std::make_unique<FuzzyMatchedVideoEncoderFactory>(), |
Åsa Persson | 13fffbb | 2023-03-31 11:13:37 | [diff] [blame] | 167 | 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.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 173 | if (!peer_connection_factory_) { |
| 174 | return false; |
| 175 | } |
| 176 | |
Henrik Boström | d79599d | 2016-06-01 11:58:50 | [diff] [blame] | 177 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator( |
deadbeef | 1b54a5f | 2017-01-24 03:39:57 | [diff] [blame] | 178 | new FakeRTCCertificateGenerator()); |
Harald Alvestrand | f33f7a2 | 2021-05-09 14:58:57 | [diff] [blame] | 179 | 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.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 190 | } |
| 191 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 192 | rtc::scoped_refptr<webrtc::DataChannelInterface> |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 | [diff] [blame] | 193 | PeerConnectionTestWrapper::CreateDataChannel( |
| 194 | const std::string& label, |
| 195 | const webrtc::DataChannelInit& init) { |
Harald Alvestrand | a9af50f | 2021-05-21 13:33:51 | [diff] [blame] | 196 | 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.org | 1a6c628 | 2014-06-12 21:59:29 | [diff] [blame] | 204 | } |
| 205 | |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 206 | absl::optional<webrtc::RtpCodecCapability> |
| 207 | PeerConnectionTestWrapper::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öm | 4c1e7cc | 2020-06-11 10:26:53 | [diff] [blame] | 220 | void PeerConnectionTestWrapper::WaitForNegotiation() { |
| 221 | EXPECT_TRUE_WAIT(!pending_negotiation_, kMaxWait); |
| 222 | } |
| 223 | |
| 224 | void PeerConnectionTestWrapper::OnSignalingChange( |
| 225 | webrtc::PeerConnectionInterface::SignalingState new_state) { |
| 226 | if (new_state == webrtc::PeerConnectionInterface::SignalingState::kStable) { |
| 227 | pending_negotiation_ = false; |
| 228 | } |
| 229 | } |
| 230 | |
Steve Anton | 191c39f | 2018-01-25 03:35:55 | [diff] [blame] | 231 | void 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 Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 238 | renderer_ = std::make_unique<FakeVideoTrackRenderer>(video_track); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | void PeerConnectionTestWrapper::OnIceCandidate( |
| 243 | const IceCandidateInterface* candidate) { |
| 244 | std::string sdp; |
| 245 | EXPECT_TRUE(candidate->ToString(&sdp)); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 246 | SignalOnIceCandidateReady(candidate->sdp_mid(), candidate->sdp_mline_index(), |
| 247 | sdp); |
| 248 | } |
| 249 | |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 | [diff] [blame] | 250 | void PeerConnectionTestWrapper::OnDataChannel( |
Taylor Brandstetter | 98cde26 | 2016-05-31 20:02:21 | [diff] [blame] | 251 | rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) { |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 252 | SignalOnDataChannel(data_channel.get()); |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 | [diff] [blame] | 253 | } |
| 254 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 255 | void PeerConnectionTestWrapper::OnSuccess(SessionDescriptionInterface* desc) { |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 256 | // This callback should take the ownership of `desc`. |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 257 | std::unique_ptr<SessionDescriptionInterface> owned_desc(desc); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 258 | std::string sdp; |
| 259 | EXPECT_TRUE(desc->ToString(&sdp)); |
| 260 | |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 261 | RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": " |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 262 | << webrtc::SdpTypeToString(desc->GetType()) |
| 263 | << " sdp created: " << sdp; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 264 | |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 265 | SetLocalDescription(desc->GetType(), sdp); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 266 | |
| 267 | SignalOnSdpReady(sdp); |
| 268 | } |
| 269 | |
| 270 | void PeerConnectionTestWrapper::CreateOffer( |
Niels Möller | f06f923 | 2018-08-07 10:32:18 | [diff] [blame] | 271 | const webrtc::PeerConnectionInterface::RTCOfferAnswerOptions& options) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 272 | RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": CreateOffer."; |
Henrik Boström | 4c1e7cc | 2020-06-11 10:26:53 | [diff] [blame] | 273 | pending_negotiation_ = true; |
Niels Möller | f06f923 | 2018-08-07 10:32:18 | [diff] [blame] | 274 | peer_connection_->CreateOffer(this, options); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | void PeerConnectionTestWrapper::CreateAnswer( |
Niels Möller | f06f923 | 2018-08-07 10:32:18 | [diff] [blame] | 278 | const webrtc::PeerConnectionInterface::RTCOfferAnswerOptions& options) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 279 | RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ |
| 280 | << ": CreateAnswer."; |
Henrik Boström | 4c1e7cc | 2020-06-11 10:26:53 | [diff] [blame] | 281 | pending_negotiation_ = true; |
Niels Möller | f06f923 | 2018-08-07 10:32:18 | [diff] [blame] | 282 | peer_connection_->CreateAnswer(this, options); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | void PeerConnectionTestWrapper::ReceiveOfferSdp(const std::string& sdp) { |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 286 | SetRemoteDescription(SdpType::kOffer, sdp); |
Niels Möller | f06f923 | 2018-08-07 10:32:18 | [diff] [blame] | 287 | CreateAnswer(webrtc::PeerConnectionInterface::RTCOfferAnswerOptions()); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void PeerConnectionTestWrapper::ReceiveAnswerSdp(const std::string& sdp) { |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 291 | SetRemoteDescription(SdpType::kAnswer, sdp); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 292 | } |
| 293 | |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 294 | void PeerConnectionTestWrapper::SetLocalDescription(SdpType type, |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 295 | const std::string& sdp) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 296 | RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 297 | << ": SetLocalDescription " << webrtc::SdpTypeToString(type) |
| 298 | << " " << sdp; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 299 | |
Tommi | 87f7090 | 2021-04-27 12:43:08 | [diff] [blame] | 300 | auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>(); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 301 | peer_connection_->SetLocalDescription( |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 302 | observer.get(), webrtc::CreateSessionDescription(type, sdp).release()); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 303 | } |
| 304 | |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 305 | void PeerConnectionTestWrapper::SetRemoteDescription(SdpType type, |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 306 | const std::string& sdp) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 307 | RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 308 | << ": SetRemoteDescription " << webrtc::SdpTypeToString(type) |
| 309 | << " " << sdp; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 310 | |
Tommi | 87f7090 | 2021-04-27 12:43:08 | [diff] [blame] | 311 | auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>(); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 312 | peer_connection_->SetRemoteDescription( |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 313 | observer.get(), webrtc::CreateSessionDescription(type, sdp).release()); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid, |
| 317 | int sdp_mline_index, |
| 318 | const std::string& candidate) { |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 319 | std::unique_ptr<webrtc::IceCandidateInterface> owned_candidate( |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 | [diff] [blame] | 320 | webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, candidate, NULL)); |
| 321 | EXPECT_TRUE(peer_connection_->AddIceCandidate(owned_candidate.get())); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | void PeerConnectionTestWrapper::WaitForCallEstablished() { |
| 325 | WaitForConnection(); |
| 326 | WaitForAudio(); |
| 327 | WaitForVideo(); |
| 328 | } |
| 329 | |
| 330 | void PeerConnectionTestWrapper::WaitForConnection() { |
| 331 | EXPECT_TRUE_WAIT(CheckForConnection(), kMaxWait); |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 332 | RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": Connected."; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | bool PeerConnectionTestWrapper::CheckForConnection() { |
| 336 | return (peer_connection_->ice_connection_state() == |
mallinath@webrtc.org | 385857d | 2014-02-14 00:56:12 | [diff] [blame] | 337 | PeerConnectionInterface::kIceConnectionConnected) || |
| 338 | (peer_connection_->ice_connection_state() == |
| 339 | PeerConnectionInterface::kIceConnectionCompleted); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void PeerConnectionTestWrapper::WaitForAudio() { |
| 343 | EXPECT_TRUE_WAIT(CheckForAudio(), kMaxWait); |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 344 | RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ |
| 345 | << ": Got enough audio frames."; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | bool PeerConnectionTestWrapper::CheckForAudio() { |
| 349 | return (fake_audio_capture_module_->frames_received() >= |
| 350 | kTestAudioFrameCount); |
| 351 | } |
| 352 | |
| 353 | void PeerConnectionTestWrapper::WaitForVideo() { |
| 354 | EXPECT_TRUE_WAIT(CheckForVideo(), kMaxWait); |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 355 | RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ |
| 356 | << ": Got enough video frames."; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | bool PeerConnectionTestWrapper::CheckForVideo() { |
| 360 | if (!renderer_) { |
| 361 | return false; |
| 362 | } |
| 363 | return (renderer_->num_rendered_frames() >= kTestVideoFrameCount); |
| 364 | } |
| 365 | |
| 366 | void PeerConnectionTestWrapper::GetAndAddUserMedia( |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 367 | bool audio, |
| 368 | const cricket::AudioOptions& audio_options, |
Niels Möller | 5c4ddad | 2019-02-12 11:30:58 | [diff] [blame] | 369 | bool video) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 370 | rtc::scoped_refptr<webrtc::MediaStreamInterface> stream = |
Niels Möller | 5c4ddad | 2019-02-12 11:30:58 | [diff] [blame] | 371 | GetUserMedia(audio, audio_options, video); |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 372 | for (const auto& audio_track : stream->GetAudioTracks()) { |
Seth Hampson | 13b8bad | 2018-03-13 23:05:28 | [diff] [blame] | 373 | EXPECT_TRUE(peer_connection_->AddTrack(audio_track, {stream->id()}).ok()); |
Steve Anton | 191c39f | 2018-01-25 03:35:55 | [diff] [blame] | 374 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 375 | for (const auto& video_track : stream->GetVideoTracks()) { |
Seth Hampson | 13b8bad | 2018-03-13 23:05:28 | [diff] [blame] | 376 | EXPECT_TRUE(peer_connection_->AddTrack(video_track, {stream->id()}).ok()); |
Steve Anton | 191c39f | 2018-01-25 03:35:55 | [diff] [blame] | 377 | } |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 378 | } |
| 379 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 380 | rtc::scoped_refptr<webrtc::MediaStreamInterface> |
Niels Möller | 2d02e08 | 2018-05-21 09:23:35 | [diff] [blame] | 381 | PeerConnectionTestWrapper::GetUserMedia( |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 382 | bool audio, |
| 383 | const cricket::AudioOptions& audio_options, |
Henrik Boström | fd4ddd1 | 2023-02-08 10:29:20 | [diff] [blame] | 384 | bool video, |
| 385 | webrtc::Resolution resolution) { |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 386 | std::string stream_id = |
| 387 | kStreamIdBase + rtc::ToString(num_get_user_media_calls_++); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 388 | rtc::scoped_refptr<webrtc::MediaStreamInterface> stream = |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 389 | peer_connection_factory_->CreateLocalMediaStream(stream_id); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 390 | |
| 391 | if (audio) { |
Niels Möller | 2d02e08 | 2018-05-21 09:23:35 | [diff] [blame] | 392 | cricket::AudioOptions options = audio_options; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 393 | // Disable highpass filter so that we can get all the test audio frames. |
Niels Möller | 2d02e08 | 2018-05-21 09:23:35 | [diff] [blame] | 394 | options.highpass_filter = false; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 395 | rtc::scoped_refptr<webrtc::AudioSourceInterface> source = |
Niels Möller | 2d02e08 | 2018-05-21 09:23:35 | [diff] [blame] | 396 | peer_connection_factory_->CreateAudioSource(options); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 397 | rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track( |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 398 | peer_connection_factory_->CreateAudioTrack(kAudioTrackLabelBase, |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 399 | source.get())); |
Harald Alvestrand | 2f7ad28 | 2022-04-21 11:35:43 | [diff] [blame] | 400 | stream->AddTrack(audio_track); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | if (video) { |
| 404 | // Set max frame rate to 10fps to reduce the risk of the tests to be flaky. |
Niels Möller | a1cc73f | 2018-05-28 14:20:42 | [diff] [blame] | 405 | webrtc::FakePeriodicVideoSource::Config config; |
| 406 | config.frame_interval_ms = 100; |
Johannes Kron | 965e794 | 2018-09-13 13:36:20 | [diff] [blame] | 407 | config.timestamp_offset_ms = rtc::TimeMillis(); |
Henrik Boström | fd4ddd1 | 2023-02-08 10:29:20 | [diff] [blame] | 408 | config.width = resolution.width; |
| 409 | config.height = resolution.height; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 410 | |
Tommi | 87f7090 | 2021-04-27 12:43:08 | [diff] [blame] | 411 | auto source = rtc::make_ref_counted<webrtc::FakePeriodicVideoTrackSource>( |
| 412 | config, /* remote */ false); |
Henrik Boström | 89f095c | 2023-04-03 15:57:47 | [diff] [blame] | 413 | fake_video_sources_.push_back(source); |
Niels Möller | a1cc73f | 2018-05-28 14:20:42 | [diff] [blame] | 414 | |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 415 | std::string videotrack_label = stream_id + kVideoTrackLabelBase; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 416 | rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track( |
Harald Alvestrand | 041ecb8 | 2023-03-20 14:13:42 | [diff] [blame] | 417 | peer_connection_factory_->CreateVideoTrack(source, videotrack_label)); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 418 | |
Harald Alvestrand | 2f7ad28 | 2022-04-21 11:35:43 | [diff] [blame] | 419 | stream->AddTrack(video_track); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 420 | } |
| 421 | return stream; |
| 422 | } |
Henrik Boström | 89f095c | 2023-04-03 15:57:47 | [diff] [blame] | 423 | |
| 424 | void 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 | } |