blob: f7de67ecc8a2ad8603e15f5845da5be5cc671797 [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
Steve Anton8e20f172018-03-06 10:55:04 -080019#include "api/fakemetricsobserver.h"
Steve Anton94286cb2017-09-26 16:20:19 -070020#include "api/peerconnectioninterface.h"
21#include "pc/test/mockpeerconnectionobservers.h"
Mirko Bonadei2a823102017-11-13 11:50:33 +010022#include "rtc_base/function_view.h"
Steve Anton94286cb2017-09-26 16:20:19 -070023
24namespace webrtc {
25
26// Class that wraps a PeerConnection so that it is easier to use in unit tests.
27// Namely, gives a synchronous API for the event-callback-based API of
28// PeerConnection and provides an observer object that stores information from
29// PeerConnectionObserver callbacks.
30//
31// This is intended to be subclassed if additional information needs to be
32// stored with the PeerConnection (e.g., fake PeerConnection parameters so that
33// tests can be written against those interactions). The base
34// PeerConnectionWrapper should only have helper methods that are broadly
35// useful. More specific helper methods should be created in the test-specific
36// subclass.
37//
38// The wrapper is intended to be constructed by specialized factory methods on
39// a test fixture class then used as a local variable in each test case.
40class PeerConnectionWrapper {
41 public:
42 // Constructs a PeerConnectionWrapper from the given PeerConnection.
43 // The given PeerConnectionFactory should be the factory that created the
44 // PeerConnection and the MockPeerConnectionObserver should be the observer
45 // that is watching the PeerConnection.
46 PeerConnectionWrapper(
47 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory,
48 rtc::scoped_refptr<PeerConnectionInterface> pc,
49 std::unique_ptr<MockPeerConnectionObserver> observer);
50 virtual ~PeerConnectionWrapper();
51
52 PeerConnectionFactoryInterface* pc_factory();
53 PeerConnectionInterface* pc();
54 MockPeerConnectionObserver* observer();
55
56 // Calls the underlying PeerConnection's CreateOffer method and returns the
57 // resulting SessionDescription once it is available. If the method call
58 // failed, null is returned.
59 std::unique_ptr<SessionDescriptionInterface> CreateOffer(
Steve Anton8d3444d2017-10-20 15:30:51 -070060 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
61 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 16:20:19 -070062 // Calls CreateOffer with default options.
63 std::unique_ptr<SessionDescriptionInterface> CreateOffer();
64 // Calls CreateOffer and sets a copy of the offer as the local description.
Steve Anton8d3444d2017-10-20 15:30:51 -070065 std::unique_ptr<SessionDescriptionInterface> CreateOfferAndSetAsLocal(
66 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
67 // Calls CreateOfferAndSetAsLocal with default options.
Steve Anton94286cb2017-09-26 16:20:19 -070068 std::unique_ptr<SessionDescriptionInterface> CreateOfferAndSetAsLocal();
69
70 // Calls the underlying PeerConnection's CreateAnswer method and returns the
71 // resulting SessionDescription once it is available. If the method call
72 // failed, null is returned.
73 std::unique_ptr<SessionDescriptionInterface> CreateAnswer(
Steve Anton8d3444d2017-10-20 15:30:51 -070074 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
75 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 16:20:19 -070076 // Calls CreateAnswer with the default options.
77 std::unique_ptr<SessionDescriptionInterface> CreateAnswer();
78 // Calls CreateAnswer and sets a copy of the offer as the local description.
Steve Anton8d3444d2017-10-20 15:30:51 -070079 std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal(
80 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
81 // Calls CreateAnswerAndSetAsLocal with default options.
Steve Anton94286cb2017-09-26 16:20:19 -070082 std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal();
83
84 // Calls the underlying PeerConnection's SetLocalDescription method with the
85 // given session description and waits for the success/failure response.
86 // Returns true if the description was successfully set.
Steve Anton8d3444d2017-10-20 15:30:51 -070087 bool SetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc,
88 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 16:20:19 -070089 // Calls the underlying PeerConnection's SetRemoteDescription method with the
90 // given session description and waits for the success/failure response.
91 // Returns true if the description was successfully set.
Steve Anton8d3444d2017-10-20 15:30:51 -070092 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
93 std::string* error_out = nullptr);
Henrik Boström31638672017-11-23 17:48:32 +010094 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
95 RTCError* error_out);
Steve Anton94286cb2017-09-26 16:20:19 -070096
Steve Antondcc3c022017-12-22 16:02:54 -080097 // Does a round of offer/answer with the local PeerConnectionWrapper
98 // generating the offer and the given PeerConnectionWrapper generating the
99 // answer.
100 // Equivalent to:
Steve Anton22da89f2018-01-25 13:58:07 -0800101 // 1. this->CreateOffer(offer_options)
Steve Antondcc3c022017-12-22 16:02:54 -0800102 // 2. this->SetLocalDescription(offer)
103 // 3. answerer->SetRemoteDescription(offer)
Steve Anton22da89f2018-01-25 13:58:07 -0800104 // 4. answerer->CreateAnswer(answer_options)
Steve Antondcc3c022017-12-22 16:02:54 -0800105 // 5. answerer->SetLocalDescription(answer)
106 // 6. this->SetRemoteDescription(answer)
107 // Returns true if all steps succeed, false otherwise.
108 // Suggested usage:
109 // ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
110 bool ExchangeOfferAnswerWith(PeerConnectionWrapper* answerer);
Steve Anton22da89f2018-01-25 13:58:07 -0800111 bool ExchangeOfferAnswerWith(
112 PeerConnectionWrapper* answerer,
113 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_options,
114 const PeerConnectionInterface::RTCOfferAnswerOptions& answer_options);
Steve Antondcc3c022017-12-22 16:02:54 -0800115
Steve Anton9158ef62017-11-27 13:01:52 -0800116 // The following are wrappers for the underlying PeerConnection's
117 // AddTransceiver method. They return the result of calling AddTransceiver
118 // with the given arguments, DCHECKing if there is an error.
119 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
120 cricket::MediaType media_type);
121 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
122 cricket::MediaType media_type,
123 const RtpTransceiverInit& init);
124 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
125 rtc::scoped_refptr<MediaStreamTrackInterface> track);
126 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
127 rtc::scoped_refptr<MediaStreamTrackInterface> track,
128 const RtpTransceiverInit& init);
129
130 // Returns a new dummy audio track with the given label.
131 rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
132 const std::string& label);
133
134 // Returns a new dummy video track with the given label.
135 rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
136 const std::string& label);
137
Steve Anton2d6c76a2018-01-05 17:10:52 -0800138 // Wrapper for the underlying PeerConnection's AddTrack method. DCHECKs if
139 // AddTrack fails.
140 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
141 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800142 const std::vector<std::string>& stream_ids = {});
Steve Anton2d6c76a2018-01-05 17:10:52 -0800143
Steve Anton8d3444d2017-10-20 15:30:51 -0700144 // Calls the underlying PeerConnection's AddTrack method with an audio media
145 // stream track not bound to any source.
146 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack(
147 const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800148 const std::vector<std::string>& stream_ids = {});
Steve Anton8d3444d2017-10-20 15:30:51 -0700149
150 // Calls the underlying PeerConnection's AddTrack method with a video media
Niels Möllere8ae5df2018-05-29 09:13:57 +0200151 // stream track fed by a FakeVideoTrackSource.
Steve Anton8d3444d2017-10-20 15:30:51 -0700152 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack(
153 const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800154 const std::vector<std::string>& stream_ids = {});
Steve Anton8d3444d2017-10-20 15:30:51 -0700155
Steve Antonfa2260d2017-12-28 16:38:23 -0800156 // Calls the underlying PeerConnection's CreateDataChannel method with default
157 // initialization parameters.
158 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
159 const std::string& label);
160
Steve Anton8d3444d2017-10-20 15:30:51 -0700161 // Returns the signaling state of the underlying PeerConnection.
162 PeerConnectionInterface::SignalingState signaling_state();
Steve Anton94286cb2017-09-26 16:20:19 -0700163
Steve Antonf1c6db12017-10-13 11:13:35 -0700164 // Returns true if ICE has finished gathering candidates.
165 bool IsIceGatheringDone();
166
Steve Anton6f25b092017-10-23 09:39:20 -0700167 // Returns true if ICE has established a connection.
168 bool IsIceConnected();
169
170 // Calls GetStats() on the underlying PeerConnection and returns the resulting
171 // report. If GetStats() fails, this method returns null and fails the test.
172 rtc::scoped_refptr<const RTCStatsReport> GetStats();
173
Steve Anton8e20f172018-03-06 10:55:04 -0800174 // Creates a new FakeMetricsObserver and registers it with the PeerConnection
175 // as the UMA observer.
176 rtc::scoped_refptr<FakeMetricsObserver> RegisterFakeMetricsObserver();
177
Steve Anton94286cb2017-09-26 16:20:19 -0700178 private:
179 std::unique_ptr<SessionDescriptionInterface> CreateSdp(
Mirko Bonadei2a823102017-11-13 11:50:33 +0100180 rtc::FunctionView<void(CreateSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 15:30:51 -0700181 std::string* error_out);
Mirko Bonadei2a823102017-11-13 11:50:33 +0100182 bool SetSdp(rtc::FunctionView<void(SetSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 15:30:51 -0700183 std::string* error_out);
Steve Anton94286cb2017-09-26 16:20:19 -0700184
185 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
Olga Sharonovaf2662f02017-10-20 09:42:08 +0000186 std::unique_ptr<MockPeerConnectionObserver> observer_;
Steve Anton8d3444d2017-10-20 15:30:51 -0700187 rtc::scoped_refptr<PeerConnectionInterface> pc_;
Steve Anton8e20f172018-03-06 10:55:04 -0800188 rtc::scoped_refptr<FakeMetricsObserver> fake_metrics_observer_;
Steve Anton94286cb2017-09-26 16:20:19 -0700189};
190
191} // namespace webrtc
192
193#endif // PC_PEERCONNECTIONWRAPPER_H_