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