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