xref: /aosp_15_r20/external/webrtc/test/pc/e2e/test_peer.cc (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 #include "test/pc/e2e/test_peer.h"
11*d9f75844SAndroid Build Coastguard Worker 
12*d9f75844SAndroid Build Coastguard Worker #include <string>
13*d9f75844SAndroid Build Coastguard Worker #include <utility>
14*d9f75844SAndroid Build Coastguard Worker 
15*d9f75844SAndroid Build Coastguard Worker #include "absl/memory/memory.h"
16*d9f75844SAndroid Build Coastguard Worker #include "absl/strings/string_view.h"
17*d9f75844SAndroid Build Coastguard Worker #include "api/scoped_refptr.h"
18*d9f75844SAndroid Build Coastguard Worker #include "api/test/pclf/media_configuration.h"
19*d9f75844SAndroid Build Coastguard Worker #include "api/test/pclf/peer_configurer.h"
20*d9f75844SAndroid Build Coastguard Worker #include "modules/audio_processing/include/audio_processing.h"
21*d9f75844SAndroid Build Coastguard Worker 
22*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
23*d9f75844SAndroid Build Coastguard Worker namespace webrtc_pc_e2e {
24*d9f75844SAndroid Build Coastguard Worker namespace {
25*d9f75844SAndroid Build Coastguard Worker 
26*d9f75844SAndroid Build Coastguard Worker class SetRemoteDescriptionCallback
27*d9f75844SAndroid Build Coastguard Worker     : public webrtc::SetRemoteDescriptionObserverInterface {
28*d9f75844SAndroid Build Coastguard Worker  public:
OnSetRemoteDescriptionComplete(webrtc::RTCError error)29*d9f75844SAndroid Build Coastguard Worker   void OnSetRemoteDescriptionComplete(webrtc::RTCError error) override {
30*d9f75844SAndroid Build Coastguard Worker     is_called_ = true;
31*d9f75844SAndroid Build Coastguard Worker     error_ = error;
32*d9f75844SAndroid Build Coastguard Worker   }
33*d9f75844SAndroid Build Coastguard Worker 
is_called() const34*d9f75844SAndroid Build Coastguard Worker   bool is_called() const { return is_called_; }
35*d9f75844SAndroid Build Coastguard Worker 
error() const36*d9f75844SAndroid Build Coastguard Worker   webrtc::RTCError error() const { return error_; }
37*d9f75844SAndroid Build Coastguard Worker 
38*d9f75844SAndroid Build Coastguard Worker  private:
39*d9f75844SAndroid Build Coastguard Worker   bool is_called_ = false;
40*d9f75844SAndroid Build Coastguard Worker   webrtc::RTCError error_;
41*d9f75844SAndroid Build Coastguard Worker };
42*d9f75844SAndroid Build Coastguard Worker 
43*d9f75844SAndroid Build Coastguard Worker }  // namespace
44*d9f75844SAndroid Build Coastguard Worker 
configurable_params() const45*d9f75844SAndroid Build Coastguard Worker ConfigurableParams TestPeer::configurable_params() const {
46*d9f75844SAndroid Build Coastguard Worker   MutexLock lock(&mutex_);
47*d9f75844SAndroid Build Coastguard Worker   return configurable_params_;
48*d9f75844SAndroid Build Coastguard Worker }
49*d9f75844SAndroid Build Coastguard Worker 
AddVideoConfig(VideoConfig config)50*d9f75844SAndroid Build Coastguard Worker void TestPeer::AddVideoConfig(VideoConfig config) {
51*d9f75844SAndroid Build Coastguard Worker   MutexLock lock(&mutex_);
52*d9f75844SAndroid Build Coastguard Worker   configurable_params_.video_configs.push_back(std::move(config));
53*d9f75844SAndroid Build Coastguard Worker }
54*d9f75844SAndroid Build Coastguard Worker 
RemoveVideoConfig(absl::string_view stream_label)55*d9f75844SAndroid Build Coastguard Worker void TestPeer::RemoveVideoConfig(absl::string_view stream_label) {
56*d9f75844SAndroid Build Coastguard Worker   MutexLock lock(&mutex_);
57*d9f75844SAndroid Build Coastguard Worker   bool config_removed = false;
58*d9f75844SAndroid Build Coastguard Worker   for (auto it = configurable_params_.video_configs.begin();
59*d9f75844SAndroid Build Coastguard Worker        it != configurable_params_.video_configs.end(); ++it) {
60*d9f75844SAndroid Build Coastguard Worker     if (*it->stream_label == stream_label) {
61*d9f75844SAndroid Build Coastguard Worker       configurable_params_.video_configs.erase(it);
62*d9f75844SAndroid Build Coastguard Worker       config_removed = true;
63*d9f75844SAndroid Build Coastguard Worker       break;
64*d9f75844SAndroid Build Coastguard Worker     }
65*d9f75844SAndroid Build Coastguard Worker   }
66*d9f75844SAndroid Build Coastguard Worker   RTC_CHECK(config_removed) << *params_.name << ": No video config with label ["
67*d9f75844SAndroid Build Coastguard Worker                             << stream_label << "] was found";
68*d9f75844SAndroid Build Coastguard Worker }
69*d9f75844SAndroid Build Coastguard Worker 
SetVideoSubscription(VideoSubscription subscription)70*d9f75844SAndroid Build Coastguard Worker void TestPeer::SetVideoSubscription(VideoSubscription subscription) {
71*d9f75844SAndroid Build Coastguard Worker   MutexLock lock(&mutex_);
72*d9f75844SAndroid Build Coastguard Worker   configurable_params_.video_subscription = std::move(subscription);
73*d9f75844SAndroid Build Coastguard Worker }
74*d9f75844SAndroid Build Coastguard Worker 
GetStats(RTCStatsCollectorCallback * callback)75*d9f75844SAndroid Build Coastguard Worker void TestPeer::GetStats(RTCStatsCollectorCallback* callback) {
76*d9f75844SAndroid Build Coastguard Worker   pc()->signaling_thread()->PostTask(
77*d9f75844SAndroid Build Coastguard Worker       SafeTask(signaling_thread_task_safety_,
78*d9f75844SAndroid Build Coastguard Worker                [this, callback]() { pc()->GetStats(callback); }));
79*d9f75844SAndroid Build Coastguard Worker }
80*d9f75844SAndroid Build Coastguard Worker 
SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,std::string * error_out)81*d9f75844SAndroid Build Coastguard Worker bool TestPeer::SetRemoteDescription(
82*d9f75844SAndroid Build Coastguard Worker     std::unique_ptr<SessionDescriptionInterface> desc,
83*d9f75844SAndroid Build Coastguard Worker     std::string* error_out) {
84*d9f75844SAndroid Build Coastguard Worker   RTC_CHECK(wrapper_) << "TestPeer is already closed";
85*d9f75844SAndroid Build Coastguard Worker 
86*d9f75844SAndroid Build Coastguard Worker   auto observer = rtc::make_ref_counted<SetRemoteDescriptionCallback>();
87*d9f75844SAndroid Build Coastguard Worker   // We're assuming (and asserting) that the PeerConnection implementation of
88*d9f75844SAndroid Build Coastguard Worker   // SetRemoteDescription is synchronous when called on the signaling thread.
89*d9f75844SAndroid Build Coastguard Worker   pc()->SetRemoteDescription(std::move(desc), observer);
90*d9f75844SAndroid Build Coastguard Worker   RTC_CHECK(observer->is_called());
91*d9f75844SAndroid Build Coastguard Worker   if (!observer->error().ok()) {
92*d9f75844SAndroid Build Coastguard Worker     RTC_LOG(LS_ERROR) << *params_.name << ": Failed to set remote description: "
93*d9f75844SAndroid Build Coastguard Worker                       << observer->error().message();
94*d9f75844SAndroid Build Coastguard Worker     if (error_out) {
95*d9f75844SAndroid Build Coastguard Worker       *error_out = observer->error().message();
96*d9f75844SAndroid Build Coastguard Worker     }
97*d9f75844SAndroid Build Coastguard Worker   }
98*d9f75844SAndroid Build Coastguard Worker   return observer->error().ok();
99*d9f75844SAndroid Build Coastguard Worker }
100*d9f75844SAndroid Build Coastguard Worker 
AddIceCandidates(std::vector<std::unique_ptr<IceCandidateInterface>> candidates)101*d9f75844SAndroid Build Coastguard Worker bool TestPeer::AddIceCandidates(
102*d9f75844SAndroid Build Coastguard Worker     std::vector<std::unique_ptr<IceCandidateInterface>> candidates) {
103*d9f75844SAndroid Build Coastguard Worker   RTC_CHECK(wrapper_) << "TestPeer is already closed";
104*d9f75844SAndroid Build Coastguard Worker   bool success = true;
105*d9f75844SAndroid Build Coastguard Worker   for (auto& candidate : candidates) {
106*d9f75844SAndroid Build Coastguard Worker     if (!pc()->AddIceCandidate(candidate.get())) {
107*d9f75844SAndroid Build Coastguard Worker       std::string candidate_str;
108*d9f75844SAndroid Build Coastguard Worker       bool res = candidate->ToString(&candidate_str);
109*d9f75844SAndroid Build Coastguard Worker       RTC_CHECK(res);
110*d9f75844SAndroid Build Coastguard Worker       RTC_LOG(LS_ERROR) << "Failed to add ICE candidate, candidate_str="
111*d9f75844SAndroid Build Coastguard Worker                         << candidate_str;
112*d9f75844SAndroid Build Coastguard Worker       success = false;
113*d9f75844SAndroid Build Coastguard Worker     } else {
114*d9f75844SAndroid Build Coastguard Worker       remote_ice_candidates_.push_back(std::move(candidate));
115*d9f75844SAndroid Build Coastguard Worker     }
116*d9f75844SAndroid Build Coastguard Worker   }
117*d9f75844SAndroid Build Coastguard Worker   return success;
118*d9f75844SAndroid Build Coastguard Worker }
119*d9f75844SAndroid Build Coastguard Worker 
Close()120*d9f75844SAndroid Build Coastguard Worker void TestPeer::Close() {
121*d9f75844SAndroid Build Coastguard Worker   signaling_thread_task_safety_->SetNotAlive();
122*d9f75844SAndroid Build Coastguard Worker   wrapper_->pc()->Close();
123*d9f75844SAndroid Build Coastguard Worker   remote_ice_candidates_.clear();
124*d9f75844SAndroid Build Coastguard Worker   audio_processing_ = nullptr;
125*d9f75844SAndroid Build Coastguard Worker   video_sources_.clear();
126*d9f75844SAndroid Build Coastguard Worker   wrapper_ = nullptr;
127*d9f75844SAndroid Build Coastguard Worker   worker_thread_ = nullptr;
128*d9f75844SAndroid Build Coastguard Worker }
129*d9f75844SAndroid Build Coastguard Worker 
TestPeer(rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory,rtc::scoped_refptr<PeerConnectionInterface> pc,std::unique_ptr<MockPeerConnectionObserver> observer,Params params,ConfigurableParams configurable_params,std::vector<PeerConfigurer::VideoSource> video_sources,rtc::scoped_refptr<AudioProcessing> audio_processing,std::unique_ptr<rtc::Thread> worker_thread)130*d9f75844SAndroid Build Coastguard Worker TestPeer::TestPeer(
131*d9f75844SAndroid Build Coastguard Worker     rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory,
132*d9f75844SAndroid Build Coastguard Worker     rtc::scoped_refptr<PeerConnectionInterface> pc,
133*d9f75844SAndroid Build Coastguard Worker     std::unique_ptr<MockPeerConnectionObserver> observer,
134*d9f75844SAndroid Build Coastguard Worker     Params params,
135*d9f75844SAndroid Build Coastguard Worker     ConfigurableParams configurable_params,
136*d9f75844SAndroid Build Coastguard Worker     std::vector<PeerConfigurer::VideoSource> video_sources,
137*d9f75844SAndroid Build Coastguard Worker     rtc::scoped_refptr<AudioProcessing> audio_processing,
138*d9f75844SAndroid Build Coastguard Worker     std::unique_ptr<rtc::Thread> worker_thread)
139*d9f75844SAndroid Build Coastguard Worker     : params_(std::move(params)),
140*d9f75844SAndroid Build Coastguard Worker       configurable_params_(std::move(configurable_params)),
141*d9f75844SAndroid Build Coastguard Worker       worker_thread_(std::move(worker_thread)),
142*d9f75844SAndroid Build Coastguard Worker       wrapper_(std::make_unique<PeerConnectionWrapper>(std::move(pc_factory),
143*d9f75844SAndroid Build Coastguard Worker                                                        std::move(pc),
144*d9f75844SAndroid Build Coastguard Worker                                                        std::move(observer))),
145*d9f75844SAndroid Build Coastguard Worker       video_sources_(std::move(video_sources)),
146*d9f75844SAndroid Build Coastguard Worker       audio_processing_(audio_processing) {
147*d9f75844SAndroid Build Coastguard Worker   signaling_thread_task_safety_ = PendingTaskSafetyFlag::CreateDetached();
148*d9f75844SAndroid Build Coastguard Worker }
149*d9f75844SAndroid Build Coastguard Worker 
150*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc_pc_e2e
151*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
152