Florent Castelli | 023be3c | 2022-03-15 15:01:52 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 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 | #ifndef RTC_TOOLS_DATA_CHANNEL_BENCHMARK_PEER_CONNECTION_CLIENT_H_ |
| 11 | #define RTC_TOOLS_DATA_CHANNEL_BENCHMARK_PEER_CONNECTION_CLIENT_H_ |
| 12 | |
| 13 | #include <stdint.h> |
| 14 | |
| 15 | #include <memory> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
| 19 | #include "api/jsep.h" |
| 20 | #include "api/peer_connection_interface.h" |
| 21 | #include "api/rtp_receiver_interface.h" |
| 22 | #include "api/scoped_refptr.h" |
| 23 | #include "api/set_local_description_observer_interface.h" |
| 24 | #include "rtc_base/logging.h" |
Florent Castelli | 023be3c | 2022-03-15 15:01:52 | [diff] [blame] | 25 | #include "rtc_base/thread.h" |
| 26 | #include "rtc_tools/data_channel_benchmark/signaling_interface.h" |
| 27 | |
| 28 | namespace webrtc { |
| 29 | |
| 30 | // Handles all the details for creating a PeerConnection and negotiation using a |
| 31 | // SignalingInterface object. |
| 32 | class PeerConnectionClient : public webrtc::PeerConnectionObserver { |
| 33 | public: |
| 34 | explicit PeerConnectionClient(webrtc::PeerConnectionFactoryInterface* factory, |
| 35 | webrtc::SignalingInterface* signaling); |
| 36 | |
| 37 | ~PeerConnectionClient() override; |
| 38 | |
| 39 | PeerConnectionClient(const PeerConnectionClient&) = delete; |
| 40 | PeerConnectionClient& operator=(const PeerConnectionClient&) = delete; |
| 41 | |
| 42 | // Set the local description and send offer using the SignalingInterface, |
| 43 | // initiating the negotiation process. |
| 44 | bool StartPeerConnection(); |
| 45 | |
| 46 | // Whether the peer connection is connected to the remote peer. |
| 47 | bool IsConnected(); |
| 48 | |
| 49 | // Disconnect from the call. |
| 50 | void Disconnect(); |
| 51 | |
| 52 | rtc::scoped_refptr<webrtc::PeerConnectionInterface> peerConnection() { |
| 53 | return peer_connection_; |
| 54 | } |
| 55 | |
| 56 | // Set a callback to run when a DataChannel is created by the remote peer. |
| 57 | void SetOnDataChannel( |
| 58 | std::function<void(rtc::scoped_refptr<webrtc::DataChannelInterface>)> |
| 59 | callback); |
| 60 | |
| 61 | std::vector<rtc::scoped_refptr<webrtc::DataChannelInterface>>& |
| 62 | dataChannels() { |
| 63 | return data_channels_; |
| 64 | } |
| 65 | |
| 66 | // Creates a default PeerConnectionFactory object. |
| 67 | static rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> |
| 68 | CreateDefaultFactory(rtc::Thread* signaling_thread); |
| 69 | |
| 70 | private: |
| 71 | void AddIceCandidate( |
| 72 | std::unique_ptr<webrtc::IceCandidateInterface> candidate); |
| 73 | bool SetRemoteDescription( |
| 74 | std::unique_ptr<webrtc::SessionDescriptionInterface> desc); |
| 75 | |
| 76 | // Initialize the PeerConnection with a given PeerConnectionFactory. |
| 77 | bool InitializePeerConnection( |
| 78 | webrtc::PeerConnectionFactoryInterface* factory); |
| 79 | void DeletePeerConnection(); |
| 80 | |
| 81 | // PeerConnectionObserver implementation. |
| 82 | void OnSignalingChange( |
| 83 | webrtc::PeerConnectionInterface::SignalingState new_state) override { |
| 84 | RTC_LOG(LS_INFO) << __FUNCTION__ << " new state: " << new_state; |
| 85 | } |
| 86 | void OnDataChannel( |
| 87 | rtc::scoped_refptr<webrtc::DataChannelInterface> channel) override; |
| 88 | void OnNegotiationNeededEvent(uint32_t event_id) override; |
| 89 | void OnIceConnectionChange( |
| 90 | webrtc::PeerConnectionInterface::IceConnectionState new_state) override; |
| 91 | void OnIceGatheringChange( |
| 92 | webrtc::PeerConnectionInterface::IceGatheringState new_state) override; |
| 93 | void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override; |
| 94 | void OnIceConnectionReceivingChange(bool receiving) override { |
| 95 | RTC_LOG(LS_INFO) << __FUNCTION__ << " receiving? " << receiving; |
| 96 | } |
| 97 | |
| 98 | rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_; |
| 99 | std::function<void(rtc::scoped_refptr<webrtc::DataChannelInterface>)> |
| 100 | on_data_channel_callback_; |
| 101 | std::vector<rtc::scoped_refptr<webrtc::DataChannelInterface>> data_channels_; |
| 102 | webrtc::SignalingInterface* signaling_; |
| 103 | }; |
| 104 | |
| 105 | } // namespace webrtc |
| 106 | |
| 107 | #endif // RTC_TOOLS_DATA_CHANNEL_BENCHMARK_PEER_CONNECTION_CLIENT_H_ |