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