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