xref: /aosp_15_r20/external/webrtc/test/pc/e2e/test_peer.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2019 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 TEST_PC_E2E_TEST_PEER_H_
12 #define TEST_PC_E2E_TEST_PEER_H_
13 
14 #include <memory>
15 #include <vector>
16 
17 #include "absl/memory/memory.h"
18 #include "absl/strings/string_view.h"
19 #include "api/function_view.h"
20 #include "api/scoped_refptr.h"
21 #include "api/sequence_checker.h"
22 #include "api/set_remote_description_observer_interface.h"
23 #include "api/task_queue/pending_task_safety_flag.h"
24 #include "api/test/frame_generator_interface.h"
25 #include "api/test/pclf/media_configuration.h"
26 #include "api/test/pclf/media_quality_test_params.h"
27 #include "api/test/pclf/peer_configurer.h"
28 #include "pc/peer_connection_wrapper.h"
29 #include "rtc_base/logging.h"
30 #include "rtc_base/synchronization/mutex.h"
31 #include "test/pc/e2e/stats_provider.h"
32 
33 namespace webrtc {
34 namespace webrtc_pc_e2e {
35 
36 // Describes a single participant in the call.
37 class TestPeer final : public StatsProvider {
38  public:
39   ~TestPeer() override = default;
40 
params()41   const Params& params() const { return params_; }
42 
43   ConfigurableParams configurable_params() const;
44   void AddVideoConfig(VideoConfig config);
45   // Removes video config with specified name. Crashes if the config with
46   // specified name isn't found.
47   void RemoveVideoConfig(absl::string_view stream_label);
48   void SetVideoSubscription(VideoSubscription subscription);
49 
50   void GetStats(RTCStatsCollectorCallback* callback) override;
51 
ReleaseVideoSource(size_t i)52   PeerConfigurer::VideoSource ReleaseVideoSource(size_t i) {
53     RTC_CHECK(wrapper_) << "TestPeer is already closed";
54     return std::move(video_sources_[i]);
55   }
56 
pc_factory()57   PeerConnectionFactoryInterface* pc_factory() {
58     RTC_CHECK(wrapper_) << "TestPeer is already closed";
59     return wrapper_->pc_factory();
60   }
pc()61   PeerConnectionInterface* pc() {
62     RTC_CHECK(wrapper_) << "TestPeer is already closed";
63     return wrapper_->pc();
64   }
observer()65   MockPeerConnectionObserver* observer() {
66     RTC_CHECK(wrapper_) << "TestPeer is already closed";
67     return wrapper_->observer();
68   }
69 
70   // Tell underlying `PeerConnection` to create an Offer.
71   // `observer` will be invoked on the signaling thread when offer is created.
CreateOffer(rtc::scoped_refptr<CreateSessionDescriptionObserver> observer)72   void CreateOffer(
73       rtc::scoped_refptr<CreateSessionDescriptionObserver> observer) {
74     RTC_CHECK(wrapper_) << "TestPeer is already closed";
75     pc()->CreateOffer(observer.get(), params_.rtc_offer_answer_options);
76   }
CreateOffer()77   std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
78     RTC_CHECK(wrapper_) << "TestPeer is already closed";
79     return wrapper_->CreateOffer(params_.rtc_offer_answer_options);
80   }
81 
CreateAnswer()82   std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
83     RTC_CHECK(wrapper_) << "TestPeer is already closed";
84     return wrapper_->CreateAnswer();
85   }
86 
87   bool SetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc,
88                            std::string* error_out = nullptr) {
89     RTC_CHECK(wrapper_) << "TestPeer is already closed";
90     return wrapper_->SetLocalDescription(std::move(desc), error_out);
91   }
92 
93   // `error_out` will be set only if returned value is false.
94   bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
95                             std::string* error_out = nullptr);
96 
AddTransceiver(cricket::MediaType media_type,const RtpTransceiverInit & init)97   rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
98       cricket::MediaType media_type,
99       const RtpTransceiverInit& init) {
100     RTC_CHECK(wrapper_) << "TestPeer is already closed";
101     return wrapper_->AddTransceiver(media_type, init);
102   }
103 
104   rtc::scoped_refptr<RtpSenderInterface> AddTrack(
105       rtc::scoped_refptr<MediaStreamTrackInterface> track,
106       const std::vector<std::string>& stream_ids = {}) {
107     RTC_CHECK(wrapper_) << "TestPeer is already closed";
108     return wrapper_->AddTrack(track, stream_ids);
109   }
110 
CreateDataChannel(const std::string & label)111   rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
112       const std::string& label) {
113     RTC_CHECK(wrapper_) << "TestPeer is already closed";
114     return wrapper_->CreateDataChannel(label);
115   }
116 
signaling_state()117   PeerConnectionInterface::SignalingState signaling_state() {
118     RTC_CHECK(wrapper_) << "TestPeer is already closed";
119     return wrapper_->signaling_state();
120   }
121 
IsIceGatheringDone()122   bool IsIceGatheringDone() {
123     RTC_CHECK(wrapper_) << "TestPeer is already closed";
124     return wrapper_->IsIceGatheringDone();
125   }
126 
IsIceConnected()127   bool IsIceConnected() {
128     RTC_CHECK(wrapper_) << "TestPeer is already closed";
129     return wrapper_->IsIceConnected();
130   }
131 
GetStats()132   rtc::scoped_refptr<const RTCStatsReport> GetStats() {
133     RTC_CHECK(wrapper_) << "TestPeer is already closed";
134     return wrapper_->GetStats();
135   }
136 
DetachAecDump()137   void DetachAecDump() {
138     RTC_CHECK(wrapper_) << "TestPeer is already closed";
139     if (audio_processing_) {
140       audio_processing_->DetachAecDump();
141     }
142   }
143 
144   // Adds provided `candidates` to the owned peer connection.
145   bool AddIceCandidates(
146       std::vector<std::unique_ptr<IceCandidateInterface>> candidates);
147 
148   // Closes underlying peer connection and destroys all related objects freeing
149   // up related resources.
150   void Close();
151 
152  protected:
153   friend class TestPeerFactory;
154   TestPeer(rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory,
155            rtc::scoped_refptr<PeerConnectionInterface> pc,
156            std::unique_ptr<MockPeerConnectionObserver> observer,
157            Params params,
158            ConfigurableParams configurable_params,
159            std::vector<PeerConfigurer::VideoSource> video_sources,
160            rtc::scoped_refptr<AudioProcessing> audio_processing,
161            std::unique_ptr<rtc::Thread> worker_thread);
162 
163  private:
164   const Params params_;
165 
166   mutable Mutex mutex_;
167   ConfigurableParams configurable_params_ RTC_GUARDED_BY(mutex_);
168 
169   // Safety flag to protect all tasks posted on the signaling thread to not be
170   // executed after `wrapper_` object is destructed.
171   rtc::scoped_refptr<PendingTaskSafetyFlag> signaling_thread_task_safety_ =
172       nullptr;
173 
174   // Keeps ownership of worker thread. It has to be destroyed after `wrapper_`.
175   // `worker_thread_`can be null if the Peer use only one thread as both the
176   // worker thread and network thread.
177   std::unique_ptr<rtc::Thread> worker_thread_;
178   std::unique_ptr<PeerConnectionWrapper> wrapper_;
179   std::vector<PeerConfigurer::VideoSource> video_sources_;
180   rtc::scoped_refptr<AudioProcessing> audio_processing_;
181 
182   std::vector<std::unique_ptr<IceCandidateInterface>> remote_ice_candidates_;
183 };
184 
185 }  // namespace webrtc_pc_e2e
186 }  // namespace webrtc
187 
188 #endif  // TEST_PC_E2E_TEST_PEER_H_
189