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 | |
| 27 | namespace { |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 28 | const uint32_t kDefaultTimeout = 10000U; |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | PeerConnectionWrapper::PeerConnectionWrapper( |
| 32 | rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory, |
| 33 | rtc::scoped_refptr<PeerConnectionInterface> pc, |
| 34 | std::unique_ptr<MockPeerConnectionObserver> observer) |
Mirko Bonadei | 2a82310 | 2017-11-13 11:50:33 +0100 | [diff] [blame] | 35 | : pc_factory_(std::move(pc_factory)), |
| 36 | observer_(std::move(observer)), |
| 37 | pc_(std::move(pc)) { |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 38 | RTC_DCHECK(pc_factory_); |
| 39 | RTC_DCHECK(pc_); |
| 40 | RTC_DCHECK(observer_); |
| 41 | observer_->SetPeerConnectionInterface(pc_.get()); |
| 42 | } |
| 43 | |
| 44 | PeerConnectionWrapper::~PeerConnectionWrapper() = default; |
| 45 | |
| 46 | PeerConnectionFactoryInterface* PeerConnectionWrapper::pc_factory() { |
| 47 | return pc_factory_.get(); |
| 48 | } |
| 49 | |
| 50 | PeerConnectionInterface* PeerConnectionWrapper::pc() { |
| 51 | return pc_.get(); |
| 52 | } |
| 53 | |
| 54 | MockPeerConnectionObserver* PeerConnectionWrapper::observer() { |
| 55 | return observer_.get(); |
| 56 | } |
| 57 | |
| 58 | std::unique_ptr<SessionDescriptionInterface> |
| 59 | PeerConnectionWrapper::CreateOffer() { |
| 60 | return CreateOffer(PeerConnectionInterface::RTCOfferAnswerOptions()); |
| 61 | } |
| 62 | |
| 63 | std::unique_ptr<SessionDescriptionInterface> PeerConnectionWrapper::CreateOffer( |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 64 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 65 | std::string* error_out) { |
| 66 | return CreateSdp( |
| 67 | [this, options](CreateSessionDescriptionObserver* observer) { |
| 68 | pc()->CreateOffer(observer, options); |
| 69 | }, |
| 70 | error_out); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | std::unique_ptr<SessionDescriptionInterface> |
| 74 | PeerConnectionWrapper::CreateOfferAndSetAsLocal() { |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 75 | return CreateOfferAndSetAsLocal( |
| 76 | PeerConnectionInterface::RTCOfferAnswerOptions()); |
| 77 | } |
| 78 | |
| 79 | std::unique_ptr<SessionDescriptionInterface> |
| 80 | PeerConnectionWrapper::CreateOfferAndSetAsLocal( |
| 81 | const PeerConnectionInterface::RTCOfferAnswerOptions& options) { |
| 82 | auto offer = CreateOffer(options); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 83 | if (!offer) { |
| 84 | return nullptr; |
| 85 | } |
| 86 | EXPECT_TRUE(SetLocalDescription(CloneSessionDescription(offer.get()))); |
| 87 | return offer; |
| 88 | } |
| 89 | |
| 90 | std::unique_ptr<SessionDescriptionInterface> |
| 91 | PeerConnectionWrapper::CreateAnswer() { |
| 92 | return CreateAnswer(PeerConnectionInterface::RTCOfferAnswerOptions()); |
| 93 | } |
| 94 | |
| 95 | std::unique_ptr<SessionDescriptionInterface> |
| 96 | PeerConnectionWrapper::CreateAnswer( |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 97 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 98 | std::string* error_out) { |
| 99 | return CreateSdp( |
| 100 | [this, options](CreateSessionDescriptionObserver* observer) { |
| 101 | pc()->CreateAnswer(observer, options); |
| 102 | }, |
| 103 | error_out); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | std::unique_ptr<SessionDescriptionInterface> |
| 107 | PeerConnectionWrapper::CreateAnswerAndSetAsLocal() { |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 108 | return CreateAnswerAndSetAsLocal( |
| 109 | PeerConnectionInterface::RTCOfferAnswerOptions()); |
| 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) { |
| 184 | RTC_DCHECK(answerer); |
| 185 | if (answerer == this) { |
| 186 | RTC_LOG(LS_ERROR) << "Cannot exchange offer/answer with ourself!"; |
| 187 | return false; |
| 188 | } |
| 189 | auto offer = CreateOffer(); |
| 190 | EXPECT_TRUE(offer); |
| 191 | if (!offer) { |
| 192 | return false; |
| 193 | } |
| 194 | bool set_local_offer = |
| 195 | SetLocalDescription(CloneSessionDescription(offer.get())); |
| 196 | EXPECT_TRUE(set_local_offer); |
| 197 | if (!set_local_offer) { |
| 198 | return false; |
| 199 | } |
| 200 | bool set_remote_offer = answerer->SetRemoteDescription(std::move(offer)); |
| 201 | EXPECT_TRUE(set_remote_offer); |
| 202 | if (!set_remote_offer) { |
| 203 | return false; |
| 204 | } |
| 205 | auto answer = answerer->CreateAnswer(); |
| 206 | EXPECT_TRUE(answer); |
| 207 | if (!answer) { |
| 208 | return false; |
| 209 | } |
| 210 | bool set_local_answer = |
| 211 | answerer->SetLocalDescription(CloneSessionDescription(answer.get())); |
| 212 | EXPECT_TRUE(set_local_answer); |
| 213 | if (!set_local_answer) { |
| 214 | return false; |
| 215 | } |
| 216 | bool set_remote_answer = SetRemoteDescription(std::move(answer)); |
| 217 | EXPECT_TRUE(set_remote_answer); |
| 218 | return set_remote_answer; |
| 219 | } |
| 220 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 221 | rtc::scoped_refptr<RtpTransceiverInterface> |
| 222 | PeerConnectionWrapper::AddTransceiver(cricket::MediaType media_type) { |
| 223 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result = |
| 224 | pc()->AddTransceiver(media_type); |
| 225 | EXPECT_EQ(RTCErrorType::NONE, result.error().type()); |
| 226 | return result.MoveValue(); |
| 227 | } |
| 228 | |
| 229 | rtc::scoped_refptr<RtpTransceiverInterface> |
| 230 | PeerConnectionWrapper::AddTransceiver(cricket::MediaType media_type, |
| 231 | const RtpTransceiverInit& init) { |
| 232 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result = |
| 233 | pc()->AddTransceiver(media_type, init); |
| 234 | EXPECT_EQ(RTCErrorType::NONE, result.error().type()); |
| 235 | return result.MoveValue(); |
| 236 | } |
| 237 | |
| 238 | rtc::scoped_refptr<RtpTransceiverInterface> |
| 239 | PeerConnectionWrapper::AddTransceiver( |
| 240 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 241 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result = |
| 242 | pc()->AddTransceiver(track); |
| 243 | EXPECT_EQ(RTCErrorType::NONE, result.error().type()); |
| 244 | return result.MoveValue(); |
| 245 | } |
| 246 | |
| 247 | rtc::scoped_refptr<RtpTransceiverInterface> |
| 248 | PeerConnectionWrapper::AddTransceiver( |
| 249 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 250 | const RtpTransceiverInit& init) { |
| 251 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result = |
| 252 | pc()->AddTransceiver(track, init); |
| 253 | EXPECT_EQ(RTCErrorType::NONE, result.error().type()); |
| 254 | return result.MoveValue(); |
| 255 | } |
| 256 | |
| 257 | rtc::scoped_refptr<AudioTrackInterface> PeerConnectionWrapper::CreateAudioTrack( |
| 258 | const std::string& label) { |
| 259 | return pc_factory()->CreateAudioTrack(label, nullptr); |
| 260 | } |
| 261 | |
| 262 | rtc::scoped_refptr<VideoTrackInterface> PeerConnectionWrapper::CreateVideoTrack( |
| 263 | const std::string& label) { |
| 264 | auto video_source = pc_factory()->CreateVideoSource( |
| 265 | rtc::MakeUnique<cricket::FakeVideoCapturer>()); |
| 266 | return pc_factory()->CreateVideoTrack(label, video_source); |
| 267 | } |
| 268 | |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 269 | rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddTrack( |
| 270 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 271 | const std::vector<std::string>& stream_labels) { |
| 272 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> result = |
| 273 | pc()->AddTrack(track, stream_labels); |
| 274 | EXPECT_EQ(RTCErrorType::NONE, result.error().type()); |
| 275 | return result.MoveValue(); |
| 276 | } |
| 277 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 278 | rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddAudioTrack( |
| 279 | const std::string& track_label, |
| 280 | std::vector<MediaStreamInterface*> streams) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 281 | return pc()->AddTrack(CreateAudioTrack(track_label), std::move(streams)); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 284 | rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddVideoTrack( |
| 285 | const std::string& track_label, |
| 286 | std::vector<MediaStreamInterface*> streams) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 287 | return pc()->AddTrack(CreateVideoTrack(track_label), std::move(streams)); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 290 | rtc::scoped_refptr<DataChannelInterface> |
| 291 | PeerConnectionWrapper::CreateDataChannel(const std::string& label) { |
| 292 | return pc()->CreateDataChannel(label, nullptr); |
| 293 | } |
| 294 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 295 | PeerConnectionInterface::SignalingState |
| 296 | PeerConnectionWrapper::signaling_state() { |
| 297 | return pc()->signaling_state(); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 300 | bool PeerConnectionWrapper::IsIceGatheringDone() { |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 301 | return observer()->ice_gathering_complete_; |
| 302 | } |
| 303 | |
| 304 | bool PeerConnectionWrapper::IsIceConnected() { |
| 305 | return observer()->ice_connected_; |
| 306 | } |
| 307 | |
| 308 | rtc::scoped_refptr<const webrtc::RTCStatsReport> |
| 309 | PeerConnectionWrapper::GetStats() { |
| 310 | rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback( |
| 311 | new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>()); |
| 312 | pc()->GetStats(callback); |
| 313 | EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout); |
| 314 | return callback->report(); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 317 | } // namespace webrtc |