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 | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 182 | rtc::scoped_refptr<RtpTransceiverInterface> |
| 183 | PeerConnectionWrapper::AddTransceiver(cricket::MediaType media_type) { |
| 184 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result = |
| 185 | pc()->AddTransceiver(media_type); |
| 186 | EXPECT_EQ(RTCErrorType::NONE, result.error().type()); |
| 187 | return result.MoveValue(); |
| 188 | } |
| 189 | |
| 190 | rtc::scoped_refptr<RtpTransceiverInterface> |
| 191 | PeerConnectionWrapper::AddTransceiver(cricket::MediaType media_type, |
| 192 | const RtpTransceiverInit& init) { |
| 193 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result = |
| 194 | pc()->AddTransceiver(media_type, init); |
| 195 | EXPECT_EQ(RTCErrorType::NONE, result.error().type()); |
| 196 | return result.MoveValue(); |
| 197 | } |
| 198 | |
| 199 | rtc::scoped_refptr<RtpTransceiverInterface> |
| 200 | PeerConnectionWrapper::AddTransceiver( |
| 201 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 202 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result = |
| 203 | pc()->AddTransceiver(track); |
| 204 | EXPECT_EQ(RTCErrorType::NONE, result.error().type()); |
| 205 | return result.MoveValue(); |
| 206 | } |
| 207 | |
| 208 | rtc::scoped_refptr<RtpTransceiverInterface> |
| 209 | PeerConnectionWrapper::AddTransceiver( |
| 210 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 211 | const RtpTransceiverInit& init) { |
| 212 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result = |
| 213 | pc()->AddTransceiver(track, init); |
| 214 | EXPECT_EQ(RTCErrorType::NONE, result.error().type()); |
| 215 | return result.MoveValue(); |
| 216 | } |
| 217 | |
| 218 | rtc::scoped_refptr<AudioTrackInterface> PeerConnectionWrapper::CreateAudioTrack( |
| 219 | const std::string& label) { |
| 220 | return pc_factory()->CreateAudioTrack(label, nullptr); |
| 221 | } |
| 222 | |
| 223 | rtc::scoped_refptr<VideoTrackInterface> PeerConnectionWrapper::CreateVideoTrack( |
| 224 | const std::string& label) { |
| 225 | auto video_source = pc_factory()->CreateVideoSource( |
| 226 | rtc::MakeUnique<cricket::FakeVideoCapturer>()); |
| 227 | return pc_factory()->CreateVideoTrack(label, video_source); |
| 228 | } |
| 229 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 230 | rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddAudioTrack( |
| 231 | const std::string& track_label, |
| 232 | std::vector<MediaStreamInterface*> streams) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 233 | return pc()->AddTrack(CreateAudioTrack(track_label), std::move(streams)); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 236 | rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddVideoTrack( |
| 237 | const std::string& track_label, |
| 238 | std::vector<MediaStreamInterface*> streams) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 239 | return pc()->AddTrack(CreateVideoTrack(track_label), std::move(streams)); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 242 | PeerConnectionInterface::SignalingState |
| 243 | PeerConnectionWrapper::signaling_state() { |
| 244 | return pc()->signaling_state(); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 247 | bool PeerConnectionWrapper::IsIceGatheringDone() { |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 248 | return observer()->ice_gathering_complete_; |
| 249 | } |
| 250 | |
| 251 | bool PeerConnectionWrapper::IsIceConnected() { |
| 252 | return observer()->ice_connected_; |
| 253 | } |
| 254 | |
| 255 | rtc::scoped_refptr<const webrtc::RTCStatsReport> |
| 256 | PeerConnectionWrapper::GetStats() { |
| 257 | rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback( |
| 258 | new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>()); |
| 259 | pc()->GetStats(callback); |
| 260 | EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout); |
| 261 | return callback->report(); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 264 | } // namespace webrtc |