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 | #ifndef PC_PEERCONNECTIONWRAPPER_H_ |
| 12 | #define PC_PEERCONNECTIONWRAPPER_H_ |
| 13 | |
| 14 | #include <functional> |
| 15 | #include <memory> |
| 16 | #include <string> |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 17 | #include <vector> |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 18 | |
| 19 | #include "api/peerconnectioninterface.h" |
| 20 | #include "pc/test/mockpeerconnectionobservers.h" |
Mirko Bonadei | 2a82310 | 2017-11-13 11:50:33 +0100 | [diff] [blame] | 21 | #include "rtc_base/function_view.h" |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | // Class that wraps a PeerConnection so that it is easier to use in unit tests. |
| 26 | // Namely, gives a synchronous API for the event-callback-based API of |
| 27 | // PeerConnection and provides an observer object that stores information from |
| 28 | // PeerConnectionObserver callbacks. |
| 29 | // |
| 30 | // This is intended to be subclassed if additional information needs to be |
| 31 | // stored with the PeerConnection (e.g., fake PeerConnection parameters so that |
| 32 | // tests can be written against those interactions). The base |
| 33 | // PeerConnectionWrapper should only have helper methods that are broadly |
| 34 | // useful. More specific helper methods should be created in the test-specific |
| 35 | // subclass. |
| 36 | // |
| 37 | // The wrapper is intended to be constructed by specialized factory methods on |
| 38 | // a test fixture class then used as a local variable in each test case. |
| 39 | class PeerConnectionWrapper { |
| 40 | public: |
| 41 | // Constructs a PeerConnectionWrapper from the given PeerConnection. |
| 42 | // The given PeerConnectionFactory should be the factory that created the |
| 43 | // PeerConnection and the MockPeerConnectionObserver should be the observer |
| 44 | // that is watching the PeerConnection. |
| 45 | PeerConnectionWrapper( |
| 46 | rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory, |
| 47 | rtc::scoped_refptr<PeerConnectionInterface> pc, |
| 48 | std::unique_ptr<MockPeerConnectionObserver> observer); |
| 49 | virtual ~PeerConnectionWrapper(); |
| 50 | |
| 51 | PeerConnectionFactoryInterface* pc_factory(); |
| 52 | PeerConnectionInterface* pc(); |
| 53 | MockPeerConnectionObserver* observer(); |
| 54 | |
| 55 | // Calls the underlying PeerConnection's CreateOffer method and returns the |
| 56 | // resulting SessionDescription once it is available. If the method call |
| 57 | // failed, null is returned. |
| 58 | std::unique_ptr<SessionDescriptionInterface> CreateOffer( |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 59 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 60 | std::string* error_out = nullptr); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 61 | // Calls CreateOffer with default options. |
| 62 | std::unique_ptr<SessionDescriptionInterface> CreateOffer(); |
| 63 | // 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] | 64 | std::unique_ptr<SessionDescriptionInterface> CreateOfferAndSetAsLocal( |
| 65 | const PeerConnectionInterface::RTCOfferAnswerOptions& options); |
| 66 | // Calls CreateOfferAndSetAsLocal with default options. |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 67 | std::unique_ptr<SessionDescriptionInterface> CreateOfferAndSetAsLocal(); |
| 68 | |
| 69 | // Calls the underlying PeerConnection's CreateAnswer method and returns the |
| 70 | // resulting SessionDescription once it is available. If the method call |
| 71 | // failed, null is returned. |
| 72 | std::unique_ptr<SessionDescriptionInterface> CreateAnswer( |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 73 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 74 | std::string* error_out = nullptr); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 75 | // Calls CreateAnswer with the default options. |
| 76 | std::unique_ptr<SessionDescriptionInterface> CreateAnswer(); |
| 77 | // 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] | 78 | std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal( |
| 79 | const PeerConnectionInterface::RTCOfferAnswerOptions& options); |
| 80 | // Calls CreateAnswerAndSetAsLocal with default options. |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 81 | std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal(); |
| 82 | |
| 83 | // Calls the underlying PeerConnection's SetLocalDescription method with the |
| 84 | // given session description and waits for the success/failure response. |
| 85 | // Returns true if the description was successfully set. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 86 | bool SetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc, |
| 87 | std::string* error_out = nullptr); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 88 | // Calls the underlying PeerConnection's SetRemoteDescription method with the |
| 89 | // given session description and waits for the success/failure response. |
| 90 | // Returns true if the description was successfully set. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 91 | bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc, |
| 92 | std::string* error_out = nullptr); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 93 | bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc, |
| 94 | RTCError* error_out); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 95 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 96 | // The following are wrappers for the underlying PeerConnection's |
| 97 | // AddTransceiver method. They return the result of calling AddTransceiver |
| 98 | // with the given arguments, DCHECKing if there is an error. |
| 99 | rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver( |
| 100 | cricket::MediaType media_type); |
| 101 | rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver( |
| 102 | cricket::MediaType media_type, |
| 103 | const RtpTransceiverInit& init); |
| 104 | rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver( |
| 105 | rtc::scoped_refptr<MediaStreamTrackInterface> track); |
| 106 | rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver( |
| 107 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 108 | const RtpTransceiverInit& init); |
| 109 | |
| 110 | // Returns a new dummy audio track with the given label. |
| 111 | rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack( |
| 112 | const std::string& label); |
| 113 | |
| 114 | // Returns a new dummy video track with the given label. |
| 115 | rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack( |
| 116 | const std::string& label); |
| 117 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 118 | // Calls the underlying PeerConnection's AddTrack method with an audio media |
| 119 | // stream track not bound to any source. |
| 120 | rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack( |
| 121 | const std::string& track_label, |
| 122 | std::vector<MediaStreamInterface*> streams = {}); |
| 123 | |
| 124 | // Calls the underlying PeerConnection's AddTrack method with a video media |
| 125 | // stream track fed by a fake video capturer. |
| 126 | rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack( |
| 127 | const std::string& track_label, |
| 128 | std::vector<MediaStreamInterface*> streams = {}); |
| 129 | |
| 130 | // Returns the signaling state of the underlying PeerConnection. |
| 131 | PeerConnectionInterface::SignalingState signaling_state(); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 132 | |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 133 | // Returns true if ICE has finished gathering candidates. |
| 134 | bool IsIceGatheringDone(); |
| 135 | |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 136 | // Returns true if ICE has established a connection. |
| 137 | bool IsIceConnected(); |
| 138 | |
| 139 | // Calls GetStats() on the underlying PeerConnection and returns the resulting |
| 140 | // report. If GetStats() fails, this method returns null and fails the test. |
| 141 | rtc::scoped_refptr<const RTCStatsReport> GetStats(); |
| 142 | |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 143 | private: |
| 144 | std::unique_ptr<SessionDescriptionInterface> CreateSdp( |
Mirko Bonadei | 2a82310 | 2017-11-13 11:50:33 +0100 | [diff] [blame] | 145 | rtc::FunctionView<void(CreateSessionDescriptionObserver*)> fn, |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 146 | std::string* error_out); |
Mirko Bonadei | 2a82310 | 2017-11-13 11:50:33 +0100 | [diff] [blame] | 147 | bool SetSdp(rtc::FunctionView<void(SetSessionDescriptionObserver*)> fn, |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 148 | std::string* error_out); |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 149 | |
| 150 | rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_; |
Olga Sharonova | f2662f0 | 2017-10-20 09:42:08 +0000 | [diff] [blame] | 151 | std::unique_ptr<MockPeerConnectionObserver> observer_; |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 152 | rtc::scoped_refptr<PeerConnectionInterface> pc_; |
Steve Anton | 94286cb | 2017-09-26 16:20:19 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | } // namespace webrtc |
| 156 | |
| 157 | #endif // PC_PEERCONNECTIONWRAPPER_H_ |