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