blob: b775e851e2d3e6ae779f7c4cd6d3d69f8c1636f0 [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:
Steve Anton22da89f2018-01-25 13:58:07 -0800100 // 1. this->CreateOffer(offer_options)
Steve Antondcc3c022017-12-22 16:02:54 -0800101 // 2. this->SetLocalDescription(offer)
102 // 3. answerer->SetRemoteDescription(offer)
Steve Anton22da89f2018-01-25 13:58:07 -0800103 // 4. answerer->CreateAnswer(answer_options)
Steve Antondcc3c022017-12-22 16:02:54 -0800104 // 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);
Steve Anton22da89f2018-01-25 13:58:07 -0800110 bool ExchangeOfferAnswerWith(
111 PeerConnectionWrapper* answerer,
112 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_options,
113 const PeerConnectionInterface::RTCOfferAnswerOptions& answer_options);
Steve Antondcc3c022017-12-22 16:02:54 -0800114
Steve Anton9158ef62017-11-27 13:01:52 -0800115 // The following are wrappers for the underlying PeerConnection's
116 // AddTransceiver method. They return the result of calling AddTransceiver
117 // with the given arguments, DCHECKing if there is an error.
118 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
119 cricket::MediaType media_type);
120 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
121 cricket::MediaType media_type,
122 const RtpTransceiverInit& init);
123 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
124 rtc::scoped_refptr<MediaStreamTrackInterface> track);
125 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
126 rtc::scoped_refptr<MediaStreamTrackInterface> track,
127 const RtpTransceiverInit& init);
128
129 // Returns a new dummy audio track with the given label.
130 rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
131 const std::string& label);
132
133 // Returns a new dummy video track with the given label.
134 rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
135 const std::string& label);
136
Steve Anton2d6c76a2018-01-05 17:10:52 -0800137 // Wrapper for the underlying PeerConnection's AddTrack method. DCHECKs if
138 // AddTrack fails.
139 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
140 rtc::scoped_refptr<MediaStreamTrackInterface> track,
141 const std::vector<std::string>& stream_labels = {});
142
Steve Anton8d3444d2017-10-20 15:30:51 -0700143 // Calls the underlying PeerConnection's AddTrack method with an audio media
144 // stream track not bound to any source.
145 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack(
146 const std::string& track_label,
Steve Antonef65ef12018-01-10 17:15:20 -0800147 const std::vector<std::string>& stream_labels = {});
Steve Anton8d3444d2017-10-20 15:30:51 -0700148
149 // Calls the underlying PeerConnection's AddTrack method with a video media
150 // stream track fed by a fake video capturer.
151 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack(
152 const std::string& track_label,
Steve Antonef65ef12018-01-10 17:15:20 -0800153 const std::vector<std::string>& stream_labels = {});
Steve Anton8d3444d2017-10-20 15:30:51 -0700154
Steve Antonfa2260d2017-12-28 16:38:23 -0800155 // Calls the underlying PeerConnection's CreateDataChannel method with default
156 // initialization parameters.
157 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
158 const std::string& label);
159
Steve Anton8d3444d2017-10-20 15:30:51 -0700160 // Returns the signaling state of the underlying PeerConnection.
161 PeerConnectionInterface::SignalingState signaling_state();
Steve Anton94286cb2017-09-26 16:20:19 -0700162
Steve Antonf1c6db12017-10-13 11:13:35 -0700163 // Returns true if ICE has finished gathering candidates.
164 bool IsIceGatheringDone();
165
Steve Anton6f25b092017-10-23 09:39:20 -0700166 // Returns true if ICE has established a connection.
167 bool IsIceConnected();
168
169 // Calls GetStats() on the underlying PeerConnection and returns the resulting
170 // report. If GetStats() fails, this method returns null and fails the test.
171 rtc::scoped_refptr<const RTCStatsReport> GetStats();
172
Steve Anton94286cb2017-09-26 16:20:19 -0700173 private:
174 std::unique_ptr<SessionDescriptionInterface> CreateSdp(
Mirko Bonadei2a823102017-11-13 11:50:33 +0100175 rtc::FunctionView<void(CreateSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 15:30:51 -0700176 std::string* error_out);
Mirko Bonadei2a823102017-11-13 11:50:33 +0100177 bool SetSdp(rtc::FunctionView<void(SetSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 15:30:51 -0700178 std::string* error_out);
Steve Anton94286cb2017-09-26 16:20:19 -0700179
180 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
Olga Sharonovaf2662f02017-10-20 09:42:08 +0000181 std::unique_ptr<MockPeerConnectionObserver> observer_;
Steve Anton8d3444d2017-10-20 15:30:51 -0700182 rtc::scoped_refptr<PeerConnectionInterface> pc_;
Steve Anton94286cb2017-09-26 16:20:19 -0700183};
184
185} // namespace webrtc
186
187#endif // PC_PEERCONNECTIONWRAPPER_H_