xref: /aosp_15_r20/external/webrtc/pc/test/peer_connection_test_wrapper.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2013 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_TEST_PEER_CONNECTION_TEST_WRAPPER_H_
12 #define PC_TEST_PEER_CONNECTION_TEST_WRAPPER_H_
13 
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 #include "api/audio_codecs/audio_decoder_factory.h"
19 #include "api/audio_codecs/audio_encoder_factory.h"
20 #include "api/audio_options.h"
21 #include "api/data_channel_interface.h"
22 #include "api/jsep.h"
23 #include "api/media_stream_interface.h"
24 #include "api/peer_connection_interface.h"
25 #include "api/rtc_error.h"
26 #include "api/rtp_receiver_interface.h"
27 #include "api/scoped_refptr.h"
28 #include "api/sequence_checker.h"
29 #include "pc/test/fake_audio_capture_module.h"
30 #include "pc/test/fake_video_track_renderer.h"
31 #include "rtc_base/third_party/sigslot/sigslot.h"
32 #include "rtc_base/thread.h"
33 
34 class PeerConnectionTestWrapper
35     : public webrtc::PeerConnectionObserver,
36       public webrtc::CreateSessionDescriptionObserver,
37       public sigslot::has_slots<> {
38  public:
39   static void Connect(PeerConnectionTestWrapper* caller,
40                       PeerConnectionTestWrapper* callee);
41 
42   PeerConnectionTestWrapper(const std::string& name,
43                             rtc::SocketServer* socket_server,
44                             rtc::Thread* network_thread,
45                             rtc::Thread* worker_thread);
46   virtual ~PeerConnectionTestWrapper();
47 
48   bool CreatePc(
49       const webrtc::PeerConnectionInterface::RTCConfiguration& config,
50       rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
51       rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory);
52 
pc_factory()53   rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory()
54       const {
55     return peer_connection_factory_;
56   }
pc()57   webrtc::PeerConnectionInterface* pc() { return peer_connection_.get(); }
58 
59   rtc::scoped_refptr<webrtc::DataChannelInterface> CreateDataChannel(
60       const std::string& label,
61       const webrtc::DataChannelInit& init);
62 
63   void WaitForNegotiation();
64 
65   // Implements PeerConnectionObserver.
66   void OnSignalingChange(
67       webrtc::PeerConnectionInterface::SignalingState new_state) override;
68   void OnAddTrack(
69       rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
70       const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
71           streams) override;
72   void OnDataChannel(
73       rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) override;
OnRenegotiationNeeded()74   void OnRenegotiationNeeded() override {}
OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state)75   void OnIceConnectionChange(
76       webrtc::PeerConnectionInterface::IceConnectionState new_state) override {}
OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state)77   void OnIceGatheringChange(
78       webrtc::PeerConnectionInterface::IceGatheringState new_state) override {}
79   void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override;
80 
81   // Implements CreateSessionDescriptionObserver.
82   void OnSuccess(webrtc::SessionDescriptionInterface* desc) override;
OnFailure(webrtc::RTCError)83   void OnFailure(webrtc::RTCError) override {}
84 
85   void CreateOffer(
86       const webrtc::PeerConnectionInterface::RTCOfferAnswerOptions& options);
87   void CreateAnswer(
88       const webrtc::PeerConnectionInterface::RTCOfferAnswerOptions& options);
89   void ReceiveOfferSdp(const std::string& sdp);
90   void ReceiveAnswerSdp(const std::string& sdp);
91   void AddIceCandidate(const std::string& sdp_mid,
92                        int sdp_mline_index,
93                        const std::string& candidate);
94   void WaitForCallEstablished();
95   void WaitForConnection();
96   void WaitForAudio();
97   void WaitForVideo();
98   void GetAndAddUserMedia(bool audio,
99                           const cricket::AudioOptions& audio_options,
100                           bool video);
101 
102   // sigslots
103   sigslot::signal3<const std::string&, int, const std::string&>
104       SignalOnIceCandidateReady;
105   sigslot::signal1<const std::string&> SignalOnSdpReady;
106   sigslot::signal1<webrtc::DataChannelInterface*> SignalOnDataChannel;
107 
108  private:
109   void SetLocalDescription(webrtc::SdpType type, const std::string& sdp);
110   void SetRemoteDescription(webrtc::SdpType type, const std::string& sdp);
111   bool CheckForConnection();
112   bool CheckForAudio();
113   bool CheckForVideo();
114   rtc::scoped_refptr<webrtc::MediaStreamInterface> GetUserMedia(
115       bool audio,
116       const cricket::AudioOptions& audio_options,
117       bool video);
118 
119   std::string name_;
120   rtc::SocketServer* const socket_server_;
121   rtc::Thread* const network_thread_;
122   rtc::Thread* const worker_thread_;
123   webrtc::SequenceChecker pc_thread_checker_;
124   rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
125   rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
126       peer_connection_factory_;
127   rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
128   std::unique_ptr<webrtc::FakeVideoTrackRenderer> renderer_;
129   int num_get_user_media_calls_ = 0;
130   bool pending_negotiation_;
131 };
132 
133 #endif  // PC_TEST_PEER_CONNECTION_TEST_WRAPPER_H_
134