diff options
author | Fabio Alessandrelli <fabio.alessandrelli@gmail.com> | 2021-09-22 23:16:23 +0200 |
---|---|---|
committer | Fabio Alessandrelli <fabio.alessandrelli@gmail.com> | 2022-06-15 21:38:12 +0200 |
commit | e19b3701260d4a855736c623b8c8a270413f041e (patch) | |
tree | 905bce590cbc44c4077defa52276bac245c22a2a /src/WebRTCLibPeerConnection.hpp | |
parent | 8c18112f5dcf96bf24fab14ab4470c00ab7f7f70 (diff) | |
download | fork-godot-webrtc-native-e19b3701260d4a855736c623b8c8a270413f041e.tar.gz fork-godot-webrtc-native-e19b3701260d4a855736c623b8c8a270413f041e.tar.bz2 fork-godot-webrtc-native-e19b3701260d4a855736c623b8c8a270413f041e.zip |
Use libdatachannel library, add Godot 4 support.
Diffstat (limited to 'src/WebRTCLibPeerConnection.hpp')
-rw-r--r-- | src/WebRTCLibPeerConnection.hpp | 111 |
1 files changed, 34 insertions, 77 deletions
diff --git a/src/WebRTCLibPeerConnection.hpp b/src/WebRTCLibPeerConnection.hpp index a265bc1..1a759b2 100644 --- a/src/WebRTCLibPeerConnection.hpp +++ b/src/WebRTCLibPeerConnection.hpp @@ -31,99 +31,61 @@ #ifndef WEBRTC_PEER_H #define WEBRTC_PEER_H +#ifdef GDNATIVE_WEBRTC #include <Godot.hpp> // Godot.hpp must go first, or windows builds breaks -#include "api/peer_connection_interface.h" // interface for all things needed from WebRTC -#include "media/base/media_engine.h" // needed for CreateModularPeerConnectionFactory -#include <mutex> - #include "net/WebRTCPeerConnectionNative.hpp" +#define WebRTCPeerConnectionExtension WebRTCPeerConnectionNative +#if !defined(GDCLASS) +#define GDCLASS(arg1, arg2) GODOT_CLASS(arg1, arg2) +#endif +#else +#include <godot_cpp/classes/web_rtc_peer_connection_extension.hpp> +#endif + +#include "rtc/rtc.hpp" + +#include <mutex> +#include <queue> namespace godot_webrtc { -class WebRTCLibPeerConnection : public WebRTCPeerConnectionNative { - GODOT_CLASS(WebRTCLibPeerConnection, WebRTCPeerConnectionNative); +class WebRTCLibPeerConnection : public godot::WebRTCPeerConnectionExtension { + GDCLASS(WebRTCLibPeerConnection, WebRTCPeerConnectionExtension); private: - godot_error _create_pc(webrtc::PeerConnectionInterface::RTCConfiguration &config); + std::shared_ptr<rtc::PeerConnection> peer_connection = nullptr; + godot::Array candidates; + + godot::Error _create_pc(rtc::Configuration &r_config); + godot::Error _parse_ice_server(rtc::Configuration &r_config, godot::Dictionary p_server); + godot::Error _parse_channel_config(rtc::DataChannelInit &r_config, const godot::Dictionary &p_dict); - static std::unique_ptr<rtc::Thread> signaling_thread; +protected: + static void _bind_methods() {} public: - static void _register_methods(); + static void _register_methods() {} static void initialize_signaling(); static void deinitialize_signaling(); void _init(); - ConnectionState get_connection_state() const; + int64_t _get_connection_state() const override; - godot_error initialize(const godot_dictionary *p_config); - godot_object *create_data_channel(const char *p_channel, const godot_dictionary *p_channel_config); - godot_error create_offer(); - godot_error set_remote_description(const char *type, const char *sdp); - godot_error set_local_description(const char *type, const char *sdp); - godot_error add_ice_candidate(const char *sdpMidName, int sdpMlineIndexName, const char *sdpName); - godot_error poll(); - void close(); + int64_t _initialize(const godot::Dictionary &p_config) override; + godot::Object *_create_data_channel(const godot::String &p_channel, const godot::Dictionary &p_channel_config) override; + int64_t _create_offer() override; + int64_t _set_remote_description(const godot::String &type, const godot::String &sdp) override; + int64_t _set_local_description(const godot::String &type, const godot::String &sdp) override; + int64_t _add_ice_candidate(const godot::String &sdpMidName, int64_t sdpMlineIndexName, const godot::String &sdpName) override; + int64_t _poll() override; + void _close() override; WebRTCLibPeerConnection(); ~WebRTCLibPeerConnection(); - /* helper functions */ private: - void queue_signal(godot::String p_name, int p_argc, const godot::Variant &p_arg1 = godot::Variant(), const godot::Variant &p_arg2 = godot::Variant(), const godot::Variant &p_arg3 = godot::Variant()); - void queue_packet(uint8_t *, int); - - /** PeerConnectionObserver callback functions **/ - class GodotPCO : public webrtc::PeerConnectionObserver { - public: - WebRTCLibPeerConnection *parent; - - GodotPCO(WebRTCLibPeerConnection *p_parent) { - parent = p_parent; - } - void OnIceCandidate(const webrtc::IceCandidateInterface *candidate) override; - - void OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState new_state) override {} - void OnAddStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override {} - void OnRemoveStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override {} - void OnDataChannel(rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) override {} - void OnRenegotiationNeeded() override {} - void OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state) override {} - void OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state) override {} - }; - - /** CreateSessionDescriptionObserver callback functions **/ - class GodotCSDO : public webrtc::CreateSessionDescriptionObserver { - public: - WebRTCLibPeerConnection *parent = nullptr; - - GodotCSDO(WebRTCLibPeerConnection *p_parent) { - parent = p_parent; - } - void OnSuccess(webrtc::SessionDescriptionInterface *desc) override; - void OnFailure(webrtc::RTCError error) override { - ERR_PRINT(godot::String(error.message())); - } - }; - - /** SetSessionDescriptionObserver callback functions **/ - class GodotSSDO : public webrtc::SetSessionDescriptionObserver { - public: - WebRTCLibPeerConnection *parent = nullptr; - bool make_offer = false; - - GodotSSDO(WebRTCLibPeerConnection *p_parent) { - parent = p_parent; - } - void OnSuccess() override; - void OnFailure(webrtc::RTCError error) override { - make_offer = false; - ERR_PRINT(godot::String(error.message())); - } - }; - class Signal { godot::String method; godot::Variant argv[3]; @@ -151,15 +113,10 @@ private: } }; - GodotPCO pco; - rtc::scoped_refptr<GodotSSDO> ptr_ssdo; - rtc::scoped_refptr<GodotCSDO> ptr_csdo; - std::mutex *mutex_signal_queue = nullptr; std::queue<Signal> signal_queue; - rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory; - rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection; + void queue_signal(godot::String p_name, int p_argc, const godot::Variant &p_arg1 = godot::Variant(), const godot::Variant &p_arg2 = godot::Variant(), const godot::Variant &p_arg3 = godot::Variant()); }; } // namespace godot_webrtc |