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 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 18 | #include "absl/memory/memory.h" |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 19 | #include "api/jsepsessiondescription.h" |
Steve Anton | 97a9f76 | 2017-10-06 10:14:03 -0700 | [diff] [blame] | 20 | #include "pc/sdputils.h" |
Niels Möller | e8ae5df | 2018-05-29 09:13:57 +0200 | [diff] [blame] | 21 | #include "pc/test/fakevideotracksource.h" |
Mirko Bonadei | 2a82310 | 2017-11-13 11:50:33 +0100 | [diff] [blame] | 22 | #include "rtc_base/function_view.h" |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 23 | #include "rtc_base/gunit.h" |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 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) { |
Niels Möller | e8ae5df | 2018-05-29 09:13:57 +0200 | [diff] [blame] | 272 | return pc_factory()->CreateVideoTrack(label, FakeVideoTrackSource::Create()); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 275 | rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddTrack( |
| 276 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 277 | const std::vector<std::string>& stream_ids) { |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 278 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> result = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 279 | pc()->AddTrack(track, stream_ids); |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 280 | EXPECT_EQ(RTCErrorType::NONE, result.error().type()); |
| 281 | return result.MoveValue(); |
| 282 | } |
| 283 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 284 | rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddAudioTrack( |
| 285 | const std::string& track_label, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 286 | const std::vector<std::string>& stream_ids) { |
| 287 | return AddTrack(CreateAudioTrack(track_label), stream_ids); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 290 | rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddVideoTrack( |
| 291 | const std::string& track_label, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 292 | const std::vector<std::string>& stream_ids) { |
| 293 | return AddTrack(CreateVideoTrack(track_label), stream_ids); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 296 | rtc::scoped_refptr<DataChannelInterface> |
| 297 | PeerConnectionWrapper::CreateDataChannel(const std::string& label) { |
| 298 | return pc()->CreateDataChannel(label, nullptr); |
| 299 | } |
| 300 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 301 | PeerConnectionInterface::SignalingState |
| 302 | PeerConnectionWrapper::signaling_state() { |
| 303 | return pc()->signaling_state(); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 306 | bool PeerConnectionWrapper::IsIceGatheringDone() { |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 307 | return observer()->ice_gathering_complete_; |
| 308 | } |
| 309 | |
| 310 | bool PeerConnectionWrapper::IsIceConnected() { |
| 311 | return observer()->ice_connected_; |
| 312 | } |
| 313 | |
| 314 | rtc::scoped_refptr<const webrtc::RTCStatsReport> |
| 315 | PeerConnectionWrapper::GetStats() { |
| 316 | rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback( |
| 317 | new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>()); |
| 318 | pc()->GetStats(callback); |
| 319 | EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout); |
| 320 | return callback->report(); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 323 | } // namespace webrtc |