blob: b5aa16342b31f97ebc9243d426e70da51cc79262 [file] [log] [blame]
Steve Anton94286cb2017-09-26 16:20:19 -07001/*
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 Anton36b29d12017-10-30 09:57:42 -070017#include <vector>
Steve Anton94286cb2017-09-26 16:20:19 -070018
19#include "api/peerconnectioninterface.h"
20#include "pc/test/mockpeerconnectionobservers.h"
Mirko Bonadei2a823102017-11-13 11:50:33 +010021#include "rtc_base/function_view.h"
Steve Anton94286cb2017-09-26 16:20:19 -070022
23namespace 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.
39class 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 Anton8d3444d2017-10-20 15:30:51 -070059 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
60 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 16:20:19 -070061 // 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 Anton8d3444d2017-10-20 15:30:51 -070064 std::unique_ptr<SessionDescriptionInterface> CreateOfferAndSetAsLocal(
65 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
66 // Calls CreateOfferAndSetAsLocal with default options.
Steve Anton94286cb2017-09-26 16:20:19 -070067 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 Anton8d3444d2017-10-20 15:30:51 -070073 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
74 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 16:20:19 -070075 // 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 Anton8d3444d2017-10-20 15:30:51 -070078 std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal(
79 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
80 // Calls CreateAnswerAndSetAsLocal with default options.
Steve Anton94286cb2017-09-26 16:20:19 -070081 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 Anton8d3444d2017-10-20 15:30:51 -070086 bool SetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc,
87 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 16:20:19 -070088 // 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 Anton8d3444d2017-10-20 15:30:51 -070091 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
92 std::string* error_out = nullptr);
Henrik Boström31638672017-11-23 17:48:32 +010093 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
94 RTCError* error_out);
Steve Anton94286cb2017-09-26 16:20:19 -070095
Steve Antondcc3c022017-12-22 16:02:54 -080096 // Does a round of offer/answer with the local PeerConnectionWrapper
97 // generating the offer and the given PeerConnectionWrapper generating the
98 // answer.
99 // Equivalent to:
100 // 1. this->CreateOffer()
101 // 2. this->SetLocalDescription(offer)
102 // 3. answerer->SetRemoteDescription(offer)
103 // 4. answerer->CreateAnswer()
104 // 5. answerer->SetLocalDescription(answer)
105 // 6. this->SetRemoteDescription(answer)
106 // Returns true if all steps succeed, false otherwise.
107 // Suggested usage:
108 // ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
109 bool ExchangeOfferAnswerWith(PeerConnectionWrapper* answerer);
110
Steve Anton9158ef62017-11-27 13:01:52 -0800111 // The following are wrappers for the underlying PeerConnection's
112 // AddTransceiver method. They return the result of calling AddTransceiver
113 // with the given arguments, DCHECKing if there is an error.
114 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
115 cricket::MediaType media_type);
116 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
117 cricket::MediaType media_type,
118 const RtpTransceiverInit& init);
119 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
120 rtc::scoped_refptr<MediaStreamTrackInterface> track);
121 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
122 rtc::scoped_refptr<MediaStreamTrackInterface> track,
123 const RtpTransceiverInit& init);
124
125 // Returns a new dummy audio track with the given label.
126 rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
127 const std::string& label);
128
129 // Returns a new dummy video track with the given label.
130 rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
131 const std::string& label);
132
Steve Anton2d6c76a2018-01-05 17:10:52 -0800133 // Wrapper for the underlying PeerConnection's AddTrack method. DCHECKs if
134 // AddTrack fails.
135 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
136 rtc::scoped_refptr<MediaStreamTrackInterface> track,
137 const std::vector<std::string>& stream_labels = {});
138
Steve Anton8d3444d2017-10-20 15:30:51 -0700139 // Calls the underlying PeerConnection's AddTrack method with an audio media
140 // stream track not bound to any source.
141 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack(
142 const std::string& track_label,
Steve Antonef65ef12018-01-10 17:15:20 -0800143 const std::vector<std::string>& stream_labels = {});
Steve Anton8d3444d2017-10-20 15:30:51 -0700144
145 // Calls the underlying PeerConnection's AddTrack method with a video media
146 // stream track fed by a fake video capturer.
147 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack(
148 const std::string& track_label,
Steve Antonef65ef12018-01-10 17:15:20 -0800149 const std::vector<std::string>& stream_labels = {});
Steve Anton8d3444d2017-10-20 15:30:51 -0700150
Steve Antonfa2260d2017-12-28 16:38:23 -0800151 // Calls the underlying PeerConnection's CreateDataChannel method with default
152 // initialization parameters.
153 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
154 const std::string& label);
155
Steve Anton8d3444d2017-10-20 15:30:51 -0700156 // Returns the signaling state of the underlying PeerConnection.
157 PeerConnectionInterface::SignalingState signaling_state();
Steve Anton94286cb2017-09-26 16:20:19 -0700158
Steve Antonf1c6db12017-10-13 11:13:35 -0700159 // Returns true if ICE has finished gathering candidates.
160 bool IsIceGatheringDone();
161
Steve Anton6f25b092017-10-23 09:39:20 -0700162 // Returns true if ICE has established a connection.
163 bool IsIceConnected();
164
165 // Calls GetStats() on the underlying PeerConnection and returns the resulting
166 // report. If GetStats() fails, this method returns null and fails the test.
167 rtc::scoped_refptr<const RTCStatsReport> GetStats();
168
Steve Anton94286cb2017-09-26 16:20:19 -0700169 private:
170 std::unique_ptr<SessionDescriptionInterface> CreateSdp(
Mirko Bonadei2a823102017-11-13 11:50:33 +0100171 rtc::FunctionView<void(CreateSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 15:30:51 -0700172 std::string* error_out);
Mirko Bonadei2a823102017-11-13 11:50:33 +0100173 bool SetSdp(rtc::FunctionView<void(SetSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 15:30:51 -0700174 std::string* error_out);
Steve Anton94286cb2017-09-26 16:20:19 -0700175
176 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
Olga Sharonovaf2662f02017-10-20 09:42:08 +0000177 std::unique_ptr<MockPeerConnectionObserver> observer_;
Steve Anton8d3444d2017-10-20 15:30:51 -0700178 rtc::scoped_refptr<PeerConnectionInterface> pc_;
Steve Anton94286cb2017-09-26 16:20:19 -0700179};
180
181} // namespace webrtc
182
183#endif // PC_PEERCONNECTIONWRAPPER_H_