Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 "pc/peerconnectionwrapper.h" |
| 12 | |
| 13 | #include <memory> |
| 14 | #include <string> |
| 15 | #include <utility> |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 16 | #include <vector> |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 17 | |
| 18 | #include "api/jsepsessiondescription.h" |
| 19 | #include "media/base/fakevideocapturer.h" |
Steve Anton | 97a9f76 | 2017-10-06 10:14:03 -0700 | [diff] [blame] | 20 | #include "pc/sdputils.h" |
Mirko Bonadei | 2a82310 | 2017-11-13 11:50:33 +0100 | [diff] [blame] | 21 | #include "rtc_base/function_view.h" |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 22 | #include "rtc_base/gunit.h" |
| 23 | #include "rtc_base/ptr_util.h" |
| 24 | |
| 25 | namespace webrtc { |
| 26 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 27 | using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions; |
| 28 | |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 29 | namespace { |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 30 | const uint32_t kDefaultTimeout = 10000U; |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | PeerConnectionWrapper::PeerConnectionWrapper( |
| 34 | rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory, |
| 35 | rtc::scoped_refptr<PeerConnectionInterface> pc, |
| 36 | std::unique_ptr<MockPeerConnectionObserver> observer) |
Mirko Bonadei | 2a82310 | 2017-11-13 11:50:33 +0100 | [diff] [blame] | 37 | : pc_factory_(std::move(pc_factory)), |
| 38 | observer_(std::move(observer)), |
| 39 | pc_(std::move(pc)) { |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 40 | RTC_DCHECK(pc_factory_); |
| 41 | RTC_DCHECK(pc_); |
| 42 | RTC_DCHECK(observer_); |
| 43 | observer_->SetPeerConnectionInterface(pc_.get()); |
| 44 | } |
| 45 | |
| 46 | PeerConnectionWrapper::~PeerConnectionWrapper() = default; |
| 47 | |
| 48 | PeerConnectionFactoryInterface* PeerConnectionWrapper::pc_factory() { |
| 49 | return pc_factory_.get(); |
| 50 | } |
| 51 | |
| 52 | PeerConnectionInterface* PeerConnectionWrapper::pc() { |
| 53 | return pc_.get(); |
| 54 | } |
| 55 | |
| 56 | MockPeerConnectionObserver* PeerConnectionWrapper::observer() { |
| 57 | return observer_.get(); |
| 58 | } |
| 59 | |
| 60 | std::unique_ptr<SessionDescriptionInterface> |
| 61 | PeerConnectionWrapper::CreateOffer() { |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 62 | return CreateOffer(RTCOfferAnswerOptions()); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | std::unique_ptr<SessionDescriptionInterface> PeerConnectionWrapper::CreateOffer( |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 66 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 67 | std::string* error_out) { |
| 68 | return CreateSdp( |
| 69 | [this, options](CreateSessionDescriptionObserver* observer) { |
| 70 | pc()->CreateOffer(observer, options); |
| 71 | }, |
| 72 | error_out); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | std::unique_ptr<SessionDescriptionInterface> |
| 76 | PeerConnectionWrapper::CreateOfferAndSetAsLocal() { |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 77 | return CreateOfferAndSetAsLocal(RTCOfferAnswerOptions()); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | std::unique_ptr<SessionDescriptionInterface> |
| 81 | PeerConnectionWrapper::CreateOfferAndSetAsLocal( |
| 82 | const PeerConnectionInterface::RTCOfferAnswerOptions& options) { |
| 83 | auto offer = CreateOffer(options); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 84 | if (!offer) { |
| 85 | return nullptr; |
| 86 | } |
| 87 | EXPECT_TRUE(SetLocalDescription(CloneSessionDescription(offer.get()))); |
| 88 | return offer; |
| 89 | } |
| 90 | |
| 91 | std::unique_ptr<SessionDescriptionInterface> |
| 92 | PeerConnectionWrapper::CreateAnswer() { |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 93 | return CreateAnswer(RTCOfferAnswerOptions()); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | std::unique_ptr<SessionDescriptionInterface> |
| 97 | PeerConnectionWrapper::CreateAnswer( |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 98 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 99 | std::string* error_out) { |
| 100 | return CreateSdp( |
| 101 | [this, options](CreateSessionDescriptionObserver* observer) { |
| 102 | pc()->CreateAnswer(observer, options); |
| 103 | }, |
| 104 | error_out); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | std::unique_ptr<SessionDescriptionInterface> |
| 108 | PeerConnectionWrapper::CreateAnswerAndSetAsLocal() { |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 109 | return CreateAnswerAndSetAsLocal(RTCOfferAnswerOptions()); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | std::unique_ptr<SessionDescriptionInterface> |
| 113 | PeerConnectionWrapper::CreateAnswerAndSetAsLocal( |
| 114 | const PeerConnectionInterface::RTCOfferAnswerOptions& options) { |
| 115 | auto answer = CreateAnswer(options); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 116 | if (!answer) { |
| 117 | return nullptr; |
| 118 | } |
| 119 | EXPECT_TRUE(SetLocalDescription(CloneSessionDescription(answer.get()))); |
| 120 | return answer; |
| 121 | } |
| 122 | |
| 123 | std::unique_ptr<SessionDescriptionInterface> PeerConnectionWrapper::CreateSdp( |
Mirko Bonadei | 2a82310 | 2017-11-13 11:50:33 +0100 | [diff] [blame] | 124 | rtc::FunctionView<void(CreateSessionDescriptionObserver*)> fn, |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 125 | std::string* error_out) { |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 126 | rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer( |
| 127 | new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>()); |
| 128 | fn(observer); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 129 | EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 130 | if (error_out && !observer->result()) { |
| 131 | *error_out = observer->error(); |
| 132 | } |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 133 | return observer->MoveDescription(); |
| 134 | } |
| 135 | |
| 136 | bool PeerConnectionWrapper::SetLocalDescription( |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 137 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 138 | std::string* error_out) { |
| 139 | return SetSdp( |
| 140 | [this, &desc](SetSessionDescriptionObserver* observer) { |
| 141 | pc()->SetLocalDescription(observer, desc.release()); |
| 142 | }, |
| 143 | error_out); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | bool PeerConnectionWrapper::SetRemoteDescription( |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 147 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 148 | std::string* error_out) { |
| 149 | return SetSdp( |
| 150 | [this, &desc](SetSessionDescriptionObserver* observer) { |
| 151 | pc()->SetRemoteDescription(observer, desc.release()); |
| 152 | }, |
| 153 | error_out); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 156 | bool PeerConnectionWrapper::SetRemoteDescription( |
| 157 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 158 | RTCError* error_out) { |
| 159 | rtc::scoped_refptr<MockSetRemoteDescriptionObserver> observer = |
| 160 | new MockSetRemoteDescriptionObserver(); |
| 161 | pc()->SetRemoteDescription(std::move(desc), observer); |
| 162 | EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout); |
| 163 | bool ok = observer->error().ok(); |
| 164 | if (error_out) |
| 165 | *error_out = std::move(observer->error()); |
| 166 | return ok; |
| 167 | } |
| 168 | |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 169 | bool PeerConnectionWrapper::SetSdp( |
Mirko Bonadei | 2a82310 | 2017-11-13 11:50:33 +0100 | [diff] [blame] | 170 | rtc::FunctionView<void(SetSessionDescriptionObserver*)> fn, |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 171 | std::string* error_out) { |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 172 | rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer( |
| 173 | new rtc::RefCountedObject<MockSetSessionDescriptionObserver>()); |
| 174 | fn(observer); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 175 | EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 176 | if (error_out && !observer->result()) { |
| 177 | *error_out = observer->error(); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 178 | } |
| 179 | return observer->result(); |
| 180 | } |
| 181 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 182 | bool PeerConnectionWrapper::ExchangeOfferAnswerWith( |
| 183 | PeerConnectionWrapper* answerer) { |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 184 | return ExchangeOfferAnswerWith(answerer, RTCOfferAnswerOptions(), |
| 185 | RTCOfferAnswerOptions()); |
| 186 | } |
| 187 | |
| 188 | bool PeerConnectionWrapper::ExchangeOfferAnswerWith( |
| 189 | PeerConnectionWrapper* answerer, |
| 190 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_options, |
| 191 | const PeerConnectionInterface::RTCOfferAnswerOptions& answer_options) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 192 | RTC_DCHECK(answerer); |
| 193 | if (answerer == this) { |
| 194 | RTC_LOG(LS_ERROR) << "Cannot exchange offer/answer with ourself!"; |
| 195 | return false; |
| 196 | } |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 197 | auto offer = CreateOffer(offer_options); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 198 | EXPECT_TRUE(offer); |
| 199 | if (!offer) { |
| 200 | return false; |
| 201 | } |
| 202 | bool set_local_offer = |
| 203 | SetLocalDescription(CloneSessionDescription(offer.get())); |
| 204 | EXPECT_TRUE(set_local_offer); |
| 205 | if (!set_local_offer) { |
| 206 | return false; |
| 207 | } |
| 208 | bool set_remote_offer = answerer->SetRemoteDescription(std::move(offer)); |
| 209 | EXPECT_TRUE(set_remote_offer); |
| 210 | if (!set_remote_offer) { |
| 211 | return false; |
| 212 | } |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 213 | auto answer = answerer->CreateAnswer(answer_options); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 214 | EXPECT_TRUE(answer); |
| 215 | if (!answer) { |
| 216 | return false; |
| 217 | } |
| 218 | bool set_local_answer = |
| 219 | answerer->SetLocalDescription(CloneSessionDescription(answer.get())); |
| 220 | EXPECT_TRUE(set_local_answer); |
| 221 | if (!set_local_answer) { |
| 222 | return false; |
| 223 | } |
| 224 | bool set_remote_answer = SetRemoteDescription(std::move(answer)); |
| 225 | EXPECT_TRUE(set_remote_answer); |
| 226 | return set_remote_answer; |
| 227 | } |
| 228 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 229 | rtc::scoped_refptr<RtpTransceiverInterface> |
| 230 | PeerConnectionWrapper::AddTransceiver(cricket::MediaType media_type) { |
| 231 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result = |
| 232 | pc()->AddTransceiver(media_type); |
| 233 | EXPECT_EQ(RTCErrorType::NONE, result.error().type()); |
| 234 | return result.MoveValue(); |
| 235 | } |
| 236 | |
| 237 | rtc::scoped_refptr<RtpTransceiverInterface> |
| 238 | PeerConnectionWrapper::AddTransceiver(cricket::MediaType media_type, |
| 239 | const RtpTransceiverInit& init) { |
| 240 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result = |
| 241 | pc()->AddTransceiver(media_type, init); |
| 242 | EXPECT_EQ(RTCErrorType::NONE, result.error().type()); |
| 243 | return result.MoveValue(); |
| 244 | } |
| 245 | |
| 246 | rtc::scoped_refptr<RtpTransceiverInterface> |
| 247 | PeerConnectionWrapper::AddTransceiver( |
| 248 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 249 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result = |
| 250 | pc()->AddTransceiver(track); |
| 251 | EXPECT_EQ(RTCErrorType::NONE, result.error().type()); |
| 252 | return result.MoveValue(); |
| 253 | } |
| 254 | |
| 255 | rtc::scoped_refptr<RtpTransceiverInterface> |
| 256 | PeerConnectionWrapper::AddTransceiver( |
| 257 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 258 | const RtpTransceiverInit& init) { |
| 259 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result = |
| 260 | pc()->AddTransceiver(track, init); |
| 261 | EXPECT_EQ(RTCErrorType::NONE, result.error().type()); |
| 262 | return result.MoveValue(); |
| 263 | } |
| 264 | |
| 265 | rtc::scoped_refptr<AudioTrackInterface> PeerConnectionWrapper::CreateAudioTrack( |
| 266 | const std::string& label) { |
| 267 | return pc_factory()->CreateAudioTrack(label, nullptr); |
| 268 | } |
| 269 | |
| 270 | rtc::scoped_refptr<VideoTrackInterface> PeerConnectionWrapper::CreateVideoTrack( |
| 271 | const std::string& label) { |
| 272 | auto video_source = pc_factory()->CreateVideoSource( |
| 273 | rtc::MakeUnique<cricket::FakeVideoCapturer>()); |
| 274 | return pc_factory()->CreateVideoTrack(label, video_source); |
| 275 | } |
| 276 | |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 277 | rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddTrack( |
| 278 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 279 | const std::vector<std::string>& stream_labels) { |
| 280 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> result = |
| 281 | pc()->AddTrack(track, stream_labels); |
| 282 | EXPECT_EQ(RTCErrorType::NONE, result.error().type()); |
| 283 | return result.MoveValue(); |
| 284 | } |
| 285 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 286 | rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddAudioTrack( |
| 287 | const std::string& track_label, |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 288 | const std::vector<std::string>& stream_labels) { |
| 289 | return AddTrack(CreateAudioTrack(track_label), stream_labels); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 292 | rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddVideoTrack( |
| 293 | const std::string& track_label, |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 294 | const std::vector<std::string>& stream_labels) { |
| 295 | return AddTrack(CreateVideoTrack(track_label), stream_labels); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 298 | rtc::scoped_refptr<DataChannelInterface> |
| 299 | PeerConnectionWrapper::CreateDataChannel(const std::string& label) { |
| 300 | return pc()->CreateDataChannel(label, nullptr); |
| 301 | } |
| 302 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 303 | PeerConnectionInterface::SignalingState |
| 304 | PeerConnectionWrapper::signaling_state() { |
| 305 | return pc()->signaling_state(); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 308 | bool PeerConnectionWrapper::IsIceGatheringDone() { |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 309 | return observer()->ice_gathering_complete_; |
| 310 | } |
| 311 | |
| 312 | bool PeerConnectionWrapper::IsIceConnected() { |
| 313 | return observer()->ice_connected_; |
| 314 | } |
| 315 | |
| 316 | rtc::scoped_refptr<const webrtc::RTCStatsReport> |
| 317 | PeerConnectionWrapper::GetStats() { |
| 318 | rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback( |
| 319 | new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>()); |
| 320 | pc()->GetStats(callback); |
| 321 | EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout); |
| 322 | return callback->report(); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 325 | } // namespace webrtc |