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> |
| 16 | |
| 17 | #include "api/jsepsessiondescription.h" |
| 18 | #include "media/base/fakevideocapturer.h" |
Steve Anton | 97a9f76 | 2017-10-06 10:14:03 -0700 | [diff] [blame] | 19 | #include "pc/sdputils.h" |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 20 | #include "rtc_base/gunit.h" |
| 21 | #include "rtc_base/ptr_util.h" |
| 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | namespace { |
Olga Sharonova | b49b661 | 2017-10-20 12:56:51 +0000 | [diff] [blame] | 26 | const uint32_t kWaitTimeout = 10000U; |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | PeerConnectionWrapper::PeerConnectionWrapper( |
| 30 | rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory, |
| 31 | rtc::scoped_refptr<PeerConnectionInterface> pc, |
| 32 | std::unique_ptr<MockPeerConnectionObserver> observer) |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame^] | 33 | : pc_factory_(pc_factory), observer_(std::move(observer)), pc_(pc) { |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 34 | RTC_DCHECK(pc_factory_); |
| 35 | RTC_DCHECK(pc_); |
| 36 | RTC_DCHECK(observer_); |
| 37 | observer_->SetPeerConnectionInterface(pc_.get()); |
| 38 | } |
| 39 | |
| 40 | PeerConnectionWrapper::~PeerConnectionWrapper() = default; |
| 41 | |
| 42 | PeerConnectionFactoryInterface* PeerConnectionWrapper::pc_factory() { |
| 43 | return pc_factory_.get(); |
| 44 | } |
| 45 | |
| 46 | PeerConnectionInterface* PeerConnectionWrapper::pc() { |
| 47 | return pc_.get(); |
| 48 | } |
| 49 | |
| 50 | MockPeerConnectionObserver* PeerConnectionWrapper::observer() { |
| 51 | return observer_.get(); |
| 52 | } |
| 53 | |
| 54 | std::unique_ptr<SessionDescriptionInterface> |
| 55 | PeerConnectionWrapper::CreateOffer() { |
| 56 | return CreateOffer(PeerConnectionInterface::RTCOfferAnswerOptions()); |
| 57 | } |
| 58 | |
| 59 | std::unique_ptr<SessionDescriptionInterface> PeerConnectionWrapper::CreateOffer( |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame^] | 60 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 61 | std::string* error_out) { |
| 62 | return CreateSdp( |
| 63 | [this, options](CreateSessionDescriptionObserver* observer) { |
| 64 | pc()->CreateOffer(observer, options); |
| 65 | }, |
| 66 | error_out); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | std::unique_ptr<SessionDescriptionInterface> |
| 70 | PeerConnectionWrapper::CreateOfferAndSetAsLocal() { |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame^] | 71 | return CreateOfferAndSetAsLocal( |
| 72 | PeerConnectionInterface::RTCOfferAnswerOptions()); |
| 73 | } |
| 74 | |
| 75 | std::unique_ptr<SessionDescriptionInterface> |
| 76 | PeerConnectionWrapper::CreateOfferAndSetAsLocal( |
| 77 | const PeerConnectionInterface::RTCOfferAnswerOptions& options) { |
| 78 | auto offer = CreateOffer(options); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 79 | if (!offer) { |
| 80 | return nullptr; |
| 81 | } |
| 82 | EXPECT_TRUE(SetLocalDescription(CloneSessionDescription(offer.get()))); |
| 83 | return offer; |
| 84 | } |
| 85 | |
| 86 | std::unique_ptr<SessionDescriptionInterface> |
| 87 | PeerConnectionWrapper::CreateAnswer() { |
| 88 | return CreateAnswer(PeerConnectionInterface::RTCOfferAnswerOptions()); |
| 89 | } |
| 90 | |
| 91 | std::unique_ptr<SessionDescriptionInterface> |
| 92 | PeerConnectionWrapper::CreateAnswer( |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame^] | 93 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 94 | std::string* error_out) { |
| 95 | return CreateSdp( |
| 96 | [this, options](CreateSessionDescriptionObserver* observer) { |
| 97 | pc()->CreateAnswer(observer, options); |
| 98 | }, |
| 99 | error_out); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | std::unique_ptr<SessionDescriptionInterface> |
| 103 | PeerConnectionWrapper::CreateAnswerAndSetAsLocal() { |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame^] | 104 | return CreateAnswerAndSetAsLocal( |
| 105 | PeerConnectionInterface::RTCOfferAnswerOptions()); |
| 106 | } |
| 107 | |
| 108 | std::unique_ptr<SessionDescriptionInterface> |
| 109 | PeerConnectionWrapper::CreateAnswerAndSetAsLocal( |
| 110 | const PeerConnectionInterface::RTCOfferAnswerOptions& options) { |
| 111 | auto answer = CreateAnswer(options); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 112 | if (!answer) { |
| 113 | return nullptr; |
| 114 | } |
| 115 | EXPECT_TRUE(SetLocalDescription(CloneSessionDescription(answer.get()))); |
| 116 | return answer; |
| 117 | } |
| 118 | |
| 119 | std::unique_ptr<SessionDescriptionInterface> PeerConnectionWrapper::CreateSdp( |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame^] | 120 | std::function<void(CreateSessionDescriptionObserver*)> fn, |
| 121 | std::string* error_out) { |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 122 | rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer( |
| 123 | new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>()); |
| 124 | fn(observer); |
Olga Sharonova | b49b661 | 2017-10-20 12:56:51 +0000 | [diff] [blame] | 125 | EXPECT_EQ_WAIT(true, observer->called(), kWaitTimeout); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame^] | 126 | if (error_out && !observer->result()) { |
| 127 | *error_out = observer->error(); |
| 128 | } |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 129 | return observer->MoveDescription(); |
| 130 | } |
| 131 | |
| 132 | bool PeerConnectionWrapper::SetLocalDescription( |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame^] | 133 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 134 | std::string* error_out) { |
| 135 | return SetSdp( |
| 136 | [this, &desc](SetSessionDescriptionObserver* observer) { |
| 137 | pc()->SetLocalDescription(observer, desc.release()); |
| 138 | }, |
| 139 | error_out); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | bool PeerConnectionWrapper::SetRemoteDescription( |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame^] | 143 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 144 | std::string* error_out) { |
| 145 | return SetSdp( |
| 146 | [this, &desc](SetSessionDescriptionObserver* observer) { |
| 147 | pc()->SetRemoteDescription(observer, desc.release()); |
| 148 | }, |
| 149 | error_out); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | bool PeerConnectionWrapper::SetSdp( |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame^] | 153 | std::function<void(SetSessionDescriptionObserver*)> fn, |
| 154 | std::string* error_out) { |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 155 | rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer( |
| 156 | new rtc::RefCountedObject<MockSetSessionDescriptionObserver>()); |
| 157 | fn(observer); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame^] | 158 | EXPECT_EQ_WAIT(true, observer->called(), kWaitTimeout); |
| 159 | if (error_out && !observer->result()) { |
| 160 | *error_out = observer->error(); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 161 | } |
| 162 | return observer->result(); |
| 163 | } |
| 164 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame^] | 165 | rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddAudioTrack( |
| 166 | const std::string& track_label, |
| 167 | std::vector<MediaStreamInterface*> streams) { |
| 168 | auto media_stream_track = |
| 169 | pc_factory()->CreateAudioTrack(track_label, nullptr); |
| 170 | return pc()->AddTrack(media_stream_track, streams); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame^] | 173 | rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddVideoTrack( |
| 174 | const std::string& track_label, |
| 175 | std::vector<MediaStreamInterface*> streams) { |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 176 | auto video_source = pc_factory()->CreateVideoSource( |
| 177 | rtc::MakeUnique<cricket::FakeVideoCapturer>()); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame^] | 178 | auto media_stream_track = |
| 179 | pc_factory()->CreateVideoTrack(track_label, video_source); |
| 180 | return pc()->AddTrack(media_stream_track, streams); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame^] | 183 | PeerConnectionInterface::SignalingState |
| 184 | PeerConnectionWrapper::signaling_state() { |
| 185 | return pc()->signaling_state(); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 188 | bool PeerConnectionWrapper::IsIceGatheringDone() { |
Olga Sharonova | b49b661 | 2017-10-20 12:56:51 +0000 | [diff] [blame] | 189 | return observer()->ice_complete_; |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 192 | } // namespace webrtc |