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" |
| 19 | #include "rtc_base/gunit.h" |
| 20 | #include "rtc_base/ptr_util.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | namespace { |
| 25 | const uint32_t kWaitTimeout = 10000U; |
| 26 | } |
| 27 | |
| 28 | PeerConnectionWrapper::PeerConnectionWrapper( |
| 29 | rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory, |
| 30 | rtc::scoped_refptr<PeerConnectionInterface> pc, |
| 31 | std::unique_ptr<MockPeerConnectionObserver> observer) |
| 32 | : pc_factory_(pc_factory), pc_(pc), observer_(std::move(observer)) { |
| 33 | RTC_DCHECK(pc_factory_); |
| 34 | RTC_DCHECK(pc_); |
| 35 | RTC_DCHECK(observer_); |
| 36 | observer_->SetPeerConnectionInterface(pc_.get()); |
| 37 | } |
| 38 | |
| 39 | PeerConnectionWrapper::~PeerConnectionWrapper() = default; |
| 40 | |
| 41 | PeerConnectionFactoryInterface* PeerConnectionWrapper::pc_factory() { |
| 42 | return pc_factory_.get(); |
| 43 | } |
| 44 | |
| 45 | PeerConnectionInterface* PeerConnectionWrapper::pc() { |
| 46 | return pc_.get(); |
| 47 | } |
| 48 | |
| 49 | MockPeerConnectionObserver* PeerConnectionWrapper::observer() { |
| 50 | return observer_.get(); |
| 51 | } |
| 52 | |
| 53 | std::unique_ptr<SessionDescriptionInterface> |
| 54 | PeerConnectionWrapper::CreateOffer() { |
| 55 | return CreateOffer(PeerConnectionInterface::RTCOfferAnswerOptions()); |
| 56 | } |
| 57 | |
| 58 | std::unique_ptr<SessionDescriptionInterface> PeerConnectionWrapper::CreateOffer( |
| 59 | const PeerConnectionInterface::RTCOfferAnswerOptions& options) { |
| 60 | return CreateSdp([this, options](CreateSessionDescriptionObserver* observer) { |
| 61 | pc()->CreateOffer(observer, options); |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | std::unique_ptr<SessionDescriptionInterface> |
| 66 | PeerConnectionWrapper::CreateOfferAndSetAsLocal() { |
| 67 | auto offer = CreateOffer(); |
| 68 | if (!offer) { |
| 69 | return nullptr; |
| 70 | } |
| 71 | EXPECT_TRUE(SetLocalDescription(CloneSessionDescription(offer.get()))); |
| 72 | return offer; |
| 73 | } |
| 74 | |
| 75 | std::unique_ptr<SessionDescriptionInterface> |
| 76 | PeerConnectionWrapper::CreateAnswer() { |
| 77 | return CreateAnswer(PeerConnectionInterface::RTCOfferAnswerOptions()); |
| 78 | } |
| 79 | |
| 80 | std::unique_ptr<SessionDescriptionInterface> |
| 81 | PeerConnectionWrapper::CreateAnswer( |
| 82 | const PeerConnectionInterface::RTCOfferAnswerOptions& options) { |
| 83 | return CreateSdp([this, options](CreateSessionDescriptionObserver* observer) { |
| 84 | pc()->CreateAnswer(observer, options); |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | std::unique_ptr<SessionDescriptionInterface> |
| 89 | PeerConnectionWrapper::CreateAnswerAndSetAsLocal() { |
| 90 | auto answer = CreateAnswer(); |
| 91 | if (!answer) { |
| 92 | return nullptr; |
| 93 | } |
| 94 | EXPECT_TRUE(SetLocalDescription(CloneSessionDescription(answer.get()))); |
| 95 | return answer; |
| 96 | } |
| 97 | |
| 98 | std::unique_ptr<SessionDescriptionInterface> PeerConnectionWrapper::CreateSdp( |
| 99 | std::function<void(CreateSessionDescriptionObserver*)> fn) { |
| 100 | rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer( |
| 101 | new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>()); |
| 102 | fn(observer); |
| 103 | EXPECT_EQ_WAIT(true, observer->called(), kWaitTimeout); |
| 104 | return observer->MoveDescription(); |
| 105 | } |
| 106 | |
| 107 | bool PeerConnectionWrapper::SetLocalDescription( |
| 108 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 109 | return SetSdp([this, &desc](SetSessionDescriptionObserver* observer) { |
| 110 | pc()->SetLocalDescription(observer, desc.release()); |
| 111 | }); |
| 112 | } |
| 113 | |
| 114 | bool PeerConnectionWrapper::SetRemoteDescription( |
| 115 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 116 | return SetSdp([this, &desc](SetSessionDescriptionObserver* observer) { |
| 117 | pc()->SetRemoteDescription(observer, desc.release()); |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | bool PeerConnectionWrapper::SetSdp( |
| 122 | std::function<void(SetSessionDescriptionObserver*)> fn) { |
| 123 | rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer( |
| 124 | new rtc::RefCountedObject<MockSetSessionDescriptionObserver>()); |
| 125 | fn(observer); |
| 126 | if (pc()->signaling_state() != PeerConnectionInterface::kClosed) { |
| 127 | EXPECT_EQ_WAIT(true, observer->called(), kWaitTimeout); |
| 128 | } |
| 129 | return observer->result(); |
| 130 | } |
| 131 | |
| 132 | std::unique_ptr<SessionDescriptionInterface> |
| 133 | PeerConnectionWrapper::CloneSessionDescription( |
| 134 | const SessionDescriptionInterface* desc) { |
| 135 | std::unique_ptr<JsepSessionDescription> clone( |
| 136 | new JsepSessionDescription(desc->type())); |
| 137 | RTC_DCHECK(clone->Initialize(desc->description()->Copy(), desc->session_id(), |
| 138 | desc->session_version())); |
| 139 | return clone; |
| 140 | } |
| 141 | |
| 142 | void PeerConnectionWrapper::AddAudioStream(const std::string& stream_label, |
| 143 | const std::string& track_label) { |
| 144 | auto stream = pc_factory()->CreateLocalMediaStream(stream_label); |
| 145 | auto audio_track = pc_factory()->CreateAudioTrack(track_label, nullptr); |
| 146 | EXPECT_TRUE(pc()->AddTrack(audio_track, {stream})); |
| 147 | EXPECT_TRUE_WAIT(observer()->renegotiation_needed_, kWaitTimeout); |
| 148 | observer()->renegotiation_needed_ = false; |
| 149 | } |
| 150 | |
| 151 | void PeerConnectionWrapper::AddVideoStream(const std::string& stream_label, |
| 152 | const std::string& track_label) { |
| 153 | auto stream = pc_factory()->CreateLocalMediaStream(stream_label); |
| 154 | auto video_source = pc_factory()->CreateVideoSource( |
| 155 | rtc::MakeUnique<cricket::FakeVideoCapturer>()); |
| 156 | auto video_track = pc_factory()->CreateVideoTrack(track_label, video_source); |
| 157 | EXPECT_TRUE(pc()->AddTrack(video_track, {stream})); |
| 158 | EXPECT_TRUE_WAIT(observer()->renegotiation_needed_, kWaitTimeout); |
| 159 | observer()->renegotiation_needed_ = false; |
| 160 | } |
| 161 | |
| 162 | void PeerConnectionWrapper::AddAudioVideoStream( |
| 163 | const std::string& stream_label, |
| 164 | const std::string& audio_track_label, |
| 165 | const std::string& video_track_label) { |
| 166 | auto stream = pc_factory()->CreateLocalMediaStream(stream_label); |
| 167 | auto audio_track = pc_factory()->CreateAudioTrack(audio_track_label, nullptr); |
| 168 | EXPECT_TRUE(pc()->AddTrack(audio_track, {stream})); |
| 169 | auto video_source = pc_factory()->CreateVideoSource( |
| 170 | rtc::MakeUnique<cricket::FakeVideoCapturer>()); |
| 171 | auto video_track = |
| 172 | pc_factory()->CreateVideoTrack(video_track_label, video_source); |
| 173 | EXPECT_TRUE(pc()->AddTrack(video_track, {stream})); |
| 174 | EXPECT_TRUE_WAIT(observer()->renegotiation_needed_, kWaitTimeout); |
| 175 | observer()->renegotiation_needed_ = false; |
| 176 | } |
| 177 | |
| 178 | } // namespace webrtc |