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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 11 | #ifndef PC_PEER_CONNECTION_WRAPPER_H_ |
| 12 | #define PC_PEER_CONNECTION_WRAPPER_H_ |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 13 | |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 14 | #include <memory> |
| 15 | #include <string> |
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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 18 | #include "api/data_channel_interface.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 19 | #include "api/jsep.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 20 | #include "api/media_stream_interface.h" |
| 21 | #include "api/media_types.h" |
| 22 | #include "api/peer_connection_interface.h" |
| 23 | #include "api/rtc_error.h" |
| 24 | #include "api/rtp_sender_interface.h" |
| 25 | #include "api/rtp_transceiver_interface.h" |
| 26 | #include "api/stats/rtc_stats_report.h" |
| 27 | #include "pc/test/mock_peer_connection_observers.h" |
Mirko Bonadei | 2a82310 | 2017-11-13 11:50:33 +0100 | [diff] [blame] | 28 | #include "rtc_base/function_view.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 29 | #include "rtc_base/scoped_ref_ptr.h" |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 30 | |
| 31 | namespace webrtc { |
| 32 | |
| 33 | // Class that wraps a PeerConnection so that it is easier to use in unit tests. |
| 34 | // Namely, gives a synchronous API for the event-callback-based API of |
| 35 | // PeerConnection and provides an observer object that stores information from |
| 36 | // PeerConnectionObserver callbacks. |
| 37 | // |
| 38 | // This is intended to be subclassed if additional information needs to be |
| 39 | // stored with the PeerConnection (e.g., fake PeerConnection parameters so that |
| 40 | // tests can be written against those interactions). The base |
| 41 | // PeerConnectionWrapper should only have helper methods that are broadly |
| 42 | // useful. More specific helper methods should be created in the test-specific |
| 43 | // subclass. |
| 44 | // |
| 45 | // The wrapper is intended to be constructed by specialized factory methods on |
| 46 | // a test fixture class then used as a local variable in each test case. |
| 47 | class PeerConnectionWrapper { |
| 48 | public: |
| 49 | // Constructs a PeerConnectionWrapper from the given PeerConnection. |
| 50 | // The given PeerConnectionFactory should be the factory that created the |
| 51 | // PeerConnection and the MockPeerConnectionObserver should be the observer |
| 52 | // that is watching the PeerConnection. |
| 53 | PeerConnectionWrapper( |
| 54 | rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory, |
| 55 | rtc::scoped_refptr<PeerConnectionInterface> pc, |
| 56 | std::unique_ptr<MockPeerConnectionObserver> observer); |
| 57 | virtual ~PeerConnectionWrapper(); |
| 58 | |
| 59 | PeerConnectionFactoryInterface* pc_factory(); |
| 60 | PeerConnectionInterface* pc(); |
| 61 | MockPeerConnectionObserver* observer(); |
| 62 | |
| 63 | // Calls the underlying PeerConnection's CreateOffer method and returns the |
| 64 | // resulting SessionDescription once it is available. If the method call |
| 65 | // failed, null is returned. |
| 66 | std::unique_ptr<SessionDescriptionInterface> CreateOffer( |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 67 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 68 | std::string* error_out = nullptr); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 69 | // Calls CreateOffer with default options. |
| 70 | std::unique_ptr<SessionDescriptionInterface> CreateOffer(); |
| 71 | // Calls CreateOffer and sets a copy of the offer as the local description. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 72 | std::unique_ptr<SessionDescriptionInterface> CreateOfferAndSetAsLocal( |
| 73 | const PeerConnectionInterface::RTCOfferAnswerOptions& options); |
| 74 | // Calls CreateOfferAndSetAsLocal with default options. |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 75 | std::unique_ptr<SessionDescriptionInterface> CreateOfferAndSetAsLocal(); |
| 76 | |
| 77 | // Calls the underlying PeerConnection's CreateAnswer method and returns the |
| 78 | // resulting SessionDescription once it is available. If the method call |
| 79 | // failed, null is returned. |
| 80 | std::unique_ptr<SessionDescriptionInterface> CreateAnswer( |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 81 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 82 | std::string* error_out = nullptr); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 83 | // Calls CreateAnswer with the default options. |
| 84 | std::unique_ptr<SessionDescriptionInterface> CreateAnswer(); |
| 85 | // Calls CreateAnswer and sets a copy of the offer as the local description. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 86 | std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal( |
| 87 | const PeerConnectionInterface::RTCOfferAnswerOptions& options); |
| 88 | // Calls CreateAnswerAndSetAsLocal with default options. |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 89 | std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal(); |
| 90 | |
| 91 | // Calls the underlying PeerConnection's SetLocalDescription method with the |
| 92 | // given session description and waits for the success/failure response. |
| 93 | // Returns true if the description was successfully set. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 94 | bool SetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc, |
| 95 | std::string* error_out = nullptr); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 96 | // Calls the underlying PeerConnection's SetRemoteDescription method with the |
| 97 | // given session description and waits for the success/failure response. |
| 98 | // Returns true if the description was successfully set. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 99 | bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc, |
| 100 | std::string* error_out = nullptr); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 101 | bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc, |
| 102 | RTCError* error_out); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 103 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 104 | // Does a round of offer/answer with the local PeerConnectionWrapper |
| 105 | // generating the offer and the given PeerConnectionWrapper generating the |
| 106 | // answer. |
| 107 | // Equivalent to: |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 108 | // 1. this->CreateOffer(offer_options) |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 109 | // 2. this->SetLocalDescription(offer) |
| 110 | // 3. answerer->SetRemoteDescription(offer) |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 111 | // 4. answerer->CreateAnswer(answer_options) |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 112 | // 5. answerer->SetLocalDescription(answer) |
| 113 | // 6. this->SetRemoteDescription(answer) |
| 114 | // Returns true if all steps succeed, false otherwise. |
| 115 | // Suggested usage: |
| 116 | // ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get())); |
| 117 | bool ExchangeOfferAnswerWith(PeerConnectionWrapper* answerer); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 118 | bool ExchangeOfferAnswerWith( |
| 119 | PeerConnectionWrapper* answerer, |
| 120 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_options, |
| 121 | const PeerConnectionInterface::RTCOfferAnswerOptions& answer_options); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 122 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 123 | // The following are wrappers for the underlying PeerConnection's |
| 124 | // AddTransceiver method. They return the result of calling AddTransceiver |
| 125 | // with the given arguments, DCHECKing if there is an error. |
| 126 | rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver( |
| 127 | cricket::MediaType media_type); |
| 128 | rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver( |
| 129 | cricket::MediaType media_type, |
| 130 | const RtpTransceiverInit& init); |
| 131 | rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver( |
| 132 | rtc::scoped_refptr<MediaStreamTrackInterface> track); |
| 133 | rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver( |
| 134 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 135 | const RtpTransceiverInit& init); |
| 136 | |
| 137 | // Returns a new dummy audio track with the given label. |
| 138 | rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack( |
| 139 | const std::string& label); |
| 140 | |
| 141 | // Returns a new dummy video track with the given label. |
| 142 | rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack( |
| 143 | const std::string& label); |
| 144 | |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 145 | // Wrapper for the underlying PeerConnection's AddTrack method. DCHECKs if |
| 146 | // AddTrack fails. |
| 147 | rtc::scoped_refptr<RtpSenderInterface> AddTrack( |
| 148 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 149 | const std::vector<std::string>& stream_ids = {}); |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 150 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 151 | // Calls the underlying PeerConnection's AddTrack method with an audio media |
| 152 | // stream track not bound to any source. |
| 153 | rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack( |
| 154 | const std::string& track_label, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 155 | const std::vector<std::string>& stream_ids = {}); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 156 | |
| 157 | // Calls the underlying PeerConnection's AddTrack method with a video media |
Niels Möller | e8ae5df | 2018-05-29 09:13:57 +0200 | [diff] [blame] | 158 | // stream track fed by a FakeVideoTrackSource. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 159 | rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack( |
| 160 | const std::string& track_label, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 161 | const std::vector<std::string>& stream_ids = {}); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 162 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 163 | // Calls the underlying PeerConnection's CreateDataChannel method with default |
| 164 | // initialization parameters. |
| 165 | rtc::scoped_refptr<DataChannelInterface> CreateDataChannel( |
| 166 | const std::string& label); |
| 167 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 168 | // Returns the signaling state of the underlying PeerConnection. |
| 169 | PeerConnectionInterface::SignalingState signaling_state(); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 170 | |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 171 | // Returns true if ICE has finished gathering candidates. |
| 172 | bool IsIceGatheringDone(); |
| 173 | |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 174 | // Returns true if ICE has established a connection. |
| 175 | bool IsIceConnected(); |
| 176 | |
| 177 | // Calls GetStats() on the underlying PeerConnection and returns the resulting |
| 178 | // report. If GetStats() fails, this method returns null and fails the test. |
| 179 | rtc::scoped_refptr<const RTCStatsReport> GetStats(); |
| 180 | |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 181 | private: |
| 182 | std::unique_ptr<SessionDescriptionInterface> CreateSdp( |
Mirko Bonadei | 2a82310 | 2017-11-13 11:50:33 +0100 | [diff] [blame] | 183 | rtc::FunctionView<void(CreateSessionDescriptionObserver*)> fn, |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 184 | std::string* error_out); |
Mirko Bonadei | 2a82310 | 2017-11-13 11:50:33 +0100 | [diff] [blame] | 185 | bool SetSdp(rtc::FunctionView<void(SetSessionDescriptionObserver*)> fn, |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 186 | std::string* error_out); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 187 | |
| 188 | rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_; |
Olga Sharonova | f2662f0 | 2017-10-20 09:42:08 +0000 | [diff] [blame] | 189 | std::unique_ptr<MockPeerConnectionObserver> observer_; |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 190 | rtc::scoped_refptr<PeerConnectionInterface> pc_; |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | } // namespace webrtc |
| 194 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 195 | #endif // PC_PEER_CONNECTION_WRAPPER_H_ |