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( |
| 57 | const PeerConnectionInterface::RTCOfferAnswerOptions& options); |
| 58 | // Calls CreateOffer with default options. |
| 59 | std::unique_ptr<SessionDescriptionInterface> CreateOffer(); |
| 60 | // Calls CreateOffer and sets a copy of the offer as the local description. |
| 61 | std::unique_ptr<SessionDescriptionInterface> CreateOfferAndSetAsLocal(); |
| 62 | |
| 63 | // Calls the underlying PeerConnection's CreateAnswer 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> CreateAnswer( |
| 67 | const PeerConnectionInterface::RTCOfferAnswerOptions& options); |
| 68 | // Calls CreateAnswer with the default options. |
| 69 | std::unique_ptr<SessionDescriptionInterface> CreateAnswer(); |
| 70 | // Calls CreateAnswer and sets a copy of the offer as the local description. |
| 71 | std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal(); |
| 72 | |
| 73 | // Calls the underlying PeerConnection's SetLocalDescription method with the |
| 74 | // given session description and waits for the success/failure response. |
| 75 | // Returns true if the description was successfully set. |
| 76 | bool SetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc); |
| 77 | // Calls the underlying PeerConnection's SetRemoteDescription method with the |
| 78 | // given session description and waits for the success/failure response. |
| 79 | // Returns true if the description was successfully set. |
| 80 | bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc); |
| 81 | |
| 82 | // Adds a new stream with one audio track to the underlying PeerConnection. |
| 83 | void AddAudioStream(const std::string& stream_label, |
| 84 | const std::string& track_label); |
| 85 | // Adds a new stream with one video track to the underlying PeerConnection. |
| 86 | void AddVideoStream(const std::string& stream_label, |
| 87 | const std::string& track_label); |
| 88 | // Adds a new stream with one audio and one video track to the underlying |
| 89 | // PeerConnection. |
| 90 | void AddAudioVideoStream(const std::string& stream_label, |
| 91 | const std::string& audio_track_label, |
| 92 | const std::string& video_track_label); |
| 93 | |
| 94 | private: |
| 95 | std::unique_ptr<SessionDescriptionInterface> CreateSdp( |
| 96 | std::function<void(CreateSessionDescriptionObserver*)> fn); |
| 97 | bool SetSdp(std::function<void(SetSessionDescriptionObserver*)> fn); |
| 98 | std::unique_ptr<SessionDescriptionInterface> CloneSessionDescription( |
| 99 | const SessionDescriptionInterface* desc); |
| 100 | |
| 101 | rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_; |
| 102 | rtc::scoped_refptr<PeerConnectionInterface> pc_; |
| 103 | std::unique_ptr<MockPeerConnectionObserver> observer_; |
| 104 | }; |
| 105 | |
| 106 | } // namespace webrtc |
| 107 | |
| 108 | #endif // PC_PEERCONNECTIONWRAPPER_H_ |