blob: 2554f1f499bd337a00655cda0336431beffb9561 [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"
21
22namespace 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.
38class 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 Anton8d3444d2017-10-20 15:30:51 -070058 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
59 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 16:20:19 -070060 // 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 Anton8d3444d2017-10-20 15:30:51 -070063 std::unique_ptr<SessionDescriptionInterface> CreateOfferAndSetAsLocal(
64 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
65 // Calls CreateOfferAndSetAsLocal with default options.
Steve Anton94286cb2017-09-26 16:20:19 -070066 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 Anton8d3444d2017-10-20 15:30:51 -070072 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
73 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 16:20:19 -070074 // 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 Anton8d3444d2017-10-20 15:30:51 -070077 std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal(
78 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
79 // Calls CreateAnswerAndSetAsLocal with default options.
Steve Anton94286cb2017-09-26 16:20:19 -070080 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 Anton8d3444d2017-10-20 15:30:51 -070085 bool SetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc,
86 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 16:20:19 -070087 // 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 Anton8d3444d2017-10-20 15:30:51 -070090 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
91 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 16:20:19 -070092
Steve Anton8d3444d2017-10-20 15:30:51 -070093 // 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 Anton94286cb2017-09-26 16:20:19 -0700107
Steve Antonf1c6db12017-10-13 11:13:35 -0700108 // Returns true if ICE has finished gathering candidates.
109 bool IsIceGatheringDone();
110
Steve Anton6f25b092017-10-23 09:39:20 -0700111 // 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 Anton94286cb2017-09-26 16:20:19 -0700118 private:
119 std::unique_ptr<SessionDescriptionInterface> CreateSdp(
Steve Anton8d3444d2017-10-20 15:30:51 -0700120 std::function<void(CreateSessionDescriptionObserver*)> fn,
121 std::string* error_out);
122 bool SetSdp(std::function<void(SetSessionDescriptionObserver*)> fn,
123 std::string* error_out);
Steve Anton94286cb2017-09-26 16:20:19 -0700124
125 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
Olga Sharonovaf2662f02017-10-20 09:42:08 +0000126 std::unique_ptr<MockPeerConnectionObserver> observer_;
Steve Anton8d3444d2017-10-20 15:30:51 -0700127 rtc::scoped_refptr<PeerConnectionInterface> pc_;
Steve Anton94286cb2017-09-26 16:20:19 -0700128};
129
130} // namespace webrtc
131
132#endif // PC_PEERCONNECTIONWRAPPER_H_