From 729b1e9941c0eeb0d51608c313ae2096ce13b2ba Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Sat, 11 May 2019 01:46:27 +0200 Subject: WebRTC refactor. Data channels, STUN/TURN support. A big refactor to the WebRTC module. API is now considered quite stable. Highlights: - Renamed `WebRTCPeer` to `WebRTCPeerConnection`. - `WebRTCPeerConnection` no longer act as `PacketPeer`, it only handle the connection itself (a bit like `TCP_Server`) - Added new `WebRTCDataChannel` class which inherits from `PacketPeer` to handle data transfer. - Add `WebRTCPeerConnection.initialize` method to create a new connection with the desired configuration provided as dictionary ([see MDN docs](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/RTCPeerConnection#RTCConfiguration_dictionary)). - Add `WebRTCPeerConnection.create_data_channel` method to create a data channel for the given connection. The connection must be in `STATE_NEW` as specified by the standard ([see MDN docs for options](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/createDataChannel#RTCDataChannelInit_dictionary)). - Add a `data_channel_received` signal to `WebRTCPeerConnection` for in-band (not negotiated) channels. - Renamed `WebRTCPeerConnection` `offer_created` signal to `session_description_created`. - Renamed `WebRTCPeerConnection` `new_ice_candidate` signal to `ice_candidate_created` --- modules/gdnative/config.py | 3 +- .../doc_classes/WebRTCDataChannelGDNative.xml | 13 + .../doc_classes/WebRTCPeerConnectionGDNative.xml | 13 + .../gdnative/doc_classes/WebRTCPeerGDNative.xml | 13 - modules/gdnative/gdnative_api.json | 19 +- modules/gdnative/include/net/godot_net.h | 32 +- modules/gdnative/include/net/godot_webrtc.h | 122 ++++++ modules/gdnative/net/webrtc_gdnative.cpp | 60 +++ modules/gdnative/net/webrtc_peer_gdnative.cpp | 45 -- modules/webrtc/config.py | 3 +- modules/webrtc/doc_classes/WebRTCDataChannel.xml | 95 +++++ modules/webrtc/doc_classes/WebRTCPeer.xml | 109 ----- .../webrtc/doc_classes/WebRTCPeerConnection.xml | 129 ++++++ modules/webrtc/register_types.cpp | 22 +- modules/webrtc/register_types.h | 4 +- modules/webrtc/webrtc_data_channel.cpp | 64 +++ modules/webrtc/webrtc_data_channel.h | 85 ++++ modules/webrtc/webrtc_data_channel_gdnative.cpp | 136 ++++++ modules/webrtc/webrtc_data_channel_gdnative.h | 80 ++++ modules/webrtc/webrtc_data_channel_js.cpp | 352 ++++++++++++++++ modules/webrtc/webrtc_data_channel_js.h | 93 +++++ modules/webrtc/webrtc_peer.cpp | 81 ---- modules/webrtc/webrtc_peer.h | 86 ---- modules/webrtc/webrtc_peer_connection.cpp | 75 ++++ modules/webrtc/webrtc_peer_connection.h | 74 ++++ modules/webrtc/webrtc_peer_connection_gdnative.cpp | 124 ++++++ modules/webrtc/webrtc_peer_connection_gdnative.h | 73 ++++ modules/webrtc/webrtc_peer_connection_js.cpp | 314 ++++++++++++++ modules/webrtc/webrtc_peer_connection_js.h | 66 +++ modules/webrtc/webrtc_peer_gdnative.cpp | 114 ------ modules/webrtc/webrtc_peer_gdnative.h | 78 ---- modules/webrtc/webrtc_peer_js.cpp | 455 --------------------- modules/webrtc/webrtc_peer_js.h | 88 ---- 33 files changed, 2007 insertions(+), 1113 deletions(-) create mode 100644 modules/gdnative/doc_classes/WebRTCDataChannelGDNative.xml create mode 100644 modules/gdnative/doc_classes/WebRTCPeerConnectionGDNative.xml delete mode 100644 modules/gdnative/doc_classes/WebRTCPeerGDNative.xml create mode 100644 modules/gdnative/include/net/godot_webrtc.h create mode 100644 modules/gdnative/net/webrtc_gdnative.cpp delete mode 100644 modules/gdnative/net/webrtc_peer_gdnative.cpp create mode 100644 modules/webrtc/doc_classes/WebRTCDataChannel.xml delete mode 100644 modules/webrtc/doc_classes/WebRTCPeer.xml create mode 100644 modules/webrtc/doc_classes/WebRTCPeerConnection.xml create mode 100644 modules/webrtc/webrtc_data_channel.cpp create mode 100644 modules/webrtc/webrtc_data_channel.h create mode 100644 modules/webrtc/webrtc_data_channel_gdnative.cpp create mode 100644 modules/webrtc/webrtc_data_channel_gdnative.h create mode 100644 modules/webrtc/webrtc_data_channel_js.cpp create mode 100644 modules/webrtc/webrtc_data_channel_js.h delete mode 100644 modules/webrtc/webrtc_peer.cpp delete mode 100644 modules/webrtc/webrtc_peer.h create mode 100644 modules/webrtc/webrtc_peer_connection.cpp create mode 100644 modules/webrtc/webrtc_peer_connection.h create mode 100644 modules/webrtc/webrtc_peer_connection_gdnative.cpp create mode 100644 modules/webrtc/webrtc_peer_connection_gdnative.h create mode 100644 modules/webrtc/webrtc_peer_connection_js.cpp create mode 100644 modules/webrtc/webrtc_peer_connection_js.h delete mode 100644 modules/webrtc/webrtc_peer_gdnative.cpp delete mode 100644 modules/webrtc/webrtc_peer_gdnative.h delete mode 100644 modules/webrtc/webrtc_peer_js.cpp delete mode 100644 modules/webrtc/webrtc_peer_js.h (limited to 'modules') diff --git a/modules/gdnative/config.py b/modules/gdnative/config.py index fde7f1a6e0..7898de5523 100644 --- a/modules/gdnative/config.py +++ b/modules/gdnative/config.py @@ -16,7 +16,8 @@ def get_doc_classes(): "ResourceFormatLoaderVideoStreamGDNative", "StreamPeerGDNative", "VideoStreamGDNative", - "WebRTCPeerGDNative", + "WebRTCPeerConnectionGDNative", + "WebRTCDataChannelGDNative", ] def get_doc_path(): diff --git a/modules/gdnative/doc_classes/WebRTCDataChannelGDNative.xml b/modules/gdnative/doc_classes/WebRTCDataChannelGDNative.xml new file mode 100644 index 0000000000..ac18ec6020 --- /dev/null +++ b/modules/gdnative/doc_classes/WebRTCDataChannelGDNative.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/modules/gdnative/doc_classes/WebRTCPeerConnectionGDNative.xml b/modules/gdnative/doc_classes/WebRTCPeerConnectionGDNative.xml new file mode 100644 index 0000000000..44cb8e5db8 --- /dev/null +++ b/modules/gdnative/doc_classes/WebRTCPeerConnectionGDNative.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/modules/gdnative/doc_classes/WebRTCPeerGDNative.xml b/modules/gdnative/doc_classes/WebRTCPeerGDNative.xml deleted file mode 100644 index 478889e031..0000000000 --- a/modules/gdnative/doc_classes/WebRTCPeerGDNative.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - diff --git a/modules/gdnative/gdnative_api.json b/modules/gdnative/gdnative_api.json index 9882a89794..93f4d75330 100644 --- a/modules/gdnative/gdnative_api.json +++ b/modules/gdnative/gdnative_api.json @@ -6436,11 +6436,26 @@ "next": null, "api": [ { - "name": "godot_net_bind_webrtc_peer", + "name": "godot_net_set_webrtc_library", + "return_type": "godot_error", + "arguments": [ + ["const godot_net_webrtc_library *", "p_library"] + ] + }, + { + "name": "godot_net_bind_webrtc_peer_connection", + "return_type": "void", + "arguments": [ + ["godot_object *", "p_obj"], + ["const godot_net_webrtc_peer_connection *", "p_interface"] + ] + }, + { + "name": "godot_net_bind_webrtc_data_channel", "return_type": "void", "arguments": [ ["godot_object *", "p_obj"], - ["const godot_net_webrtc_peer *", "p_interface"] + ["const godot_net_webrtc_data_channel *", "p_interface"] ] } ] diff --git a/modules/gdnative/include/net/godot_net.h b/modules/gdnative/include/net/godot_net.h index c1bc9daab5..3a411755c1 100644 --- a/modules/gdnative/include/net/godot_net.h +++ b/modules/gdnative/include/net/godot_net.h @@ -111,37 +111,11 @@ typedef struct { /* Binds a MultiplayerPeerGDNative to the provided interface */ void GDAPI godot_net_bind_multiplayer_peer(godot_object *p_obj, const godot_net_multiplayer_peer *); -typedef struct { - godot_gdnative_api_version version; /* version of our API */ - - godot_object *data; /* User reference */ - - /* This is PacketPeer */ - godot_error (*get_packet)(void *, const uint8_t **, int *); - godot_error (*put_packet)(void *, const uint8_t *, int); - godot_int (*get_available_packet_count)(const void *); - godot_int (*get_max_packet_size)(const void *); - - /* This is WebRTCPeer */ - void (*set_write_mode)(void *, godot_int); - godot_int (*get_write_mode)(const void *); - bool (*was_string_packet)(const void *); - godot_int (*get_connection_state)(const void *); - - godot_error (*create_offer)(void *); - godot_error (*set_remote_description)(void *, const char *, const char *); - godot_error (*set_local_description)(void *, const char *, const char *); - godot_error (*add_ice_candidate)(void *, const char *, int, const char *); - godot_error (*poll)(void *); - - void *next; /* For extension? */ -} godot_net_webrtc_peer; - -/* Binds a PacketPeerGDNative to the provided interface */ -void GDAPI godot_net_bind_webrtc_peer(godot_object *p_obj, const godot_net_webrtc_peer *); - #ifdef __cplusplus } #endif +// WebRTC Bindings +#include "net/godot_webrtc.h" + #endif /* GODOT_NATIVENET_H */ diff --git a/modules/gdnative/include/net/godot_webrtc.h b/modules/gdnative/include/net/godot_webrtc.h new file mode 100644 index 0000000000..783f7b727d --- /dev/null +++ b/modules/gdnative/include/net/godot_webrtc.h @@ -0,0 +1,122 @@ +/*************************************************************************/ +/* godot_webrtc.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef GODOT_NATIVEWEBRTC_H +#define GODOT_NATIVEWEBRTC_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define GODOT_NET_WEBRTC_API_MAJOR 3 +#define GODOT_NET_WEBRTC_API_MINOR 2 + +/* Library Interface (used to set default GDNative WebRTC implementation */ +typedef struct { + godot_gdnative_api_version version; /* version of our API */ + + /* Called when the library is unset as default interface via godot_net_set_webrtc_library */ + void (*unregistered)(); + + /* Used by WebRTCPeerConnection create when GDNative is the default implementation. */ + /* Takes a pointer to WebRTCPeerConnectionGDNative, should bind and return OK, failure if binding was unsuccessful. */ + godot_error (*create_peer_connection)(godot_object *); + + void *next; /* For extension */ +} godot_net_webrtc_library; + +/* WebRTCPeerConnection interface */ +typedef struct { + godot_gdnative_api_version version; /* version of our API */ + + godot_object *data; /* User reference */ + + /* This is WebRTCPeerConnection */ + godot_int (*get_connection_state)(const void *); + + godot_error (*initialize)(void *, const godot_dictionary *); + godot_object *(*create_data_channel)(void *, const char *p_channel_name, const godot_dictionary *); + godot_error (*create_offer)(void *); + godot_error (*create_answer)(void *); /* unused for now, should be done automatically on set_local_description */ + godot_error (*set_remote_description)(void *, const char *, const char *); + godot_error (*set_local_description)(void *, const char *, const char *); + godot_error (*add_ice_candidate)(void *, const char *, int, const char *); + godot_error (*poll)(void *); + void (*close)(void *); + + void *next; /* For extension? */ +} godot_net_webrtc_peer_connection; + +/* WebRTCDataChannel interface */ +typedef struct { + godot_gdnative_api_version version; /* version of our API */ + + godot_object *data; /* User reference */ + + /* This is PacketPeer */ + godot_error (*get_packet)(void *, const uint8_t **, int *); + godot_error (*put_packet)(void *, const uint8_t *, int); + godot_int (*get_available_packet_count)(const void *); + godot_int (*get_max_packet_size)(const void *); + + /* This is WebRTCDataChannel */ + void (*set_write_mode)(void *, godot_int); + godot_int (*get_write_mode)(const void *); + bool (*was_string_packet)(const void *); + + godot_int (*get_ready_state)(const void *); + const char *(*get_label)(const void *); + bool (*is_ordered)(const void *); + int (*get_id)(const void *); + int (*get_max_packet_life_time)(const void *); + int (*get_max_retransmits)(const void *); + const char *(*get_protocol)(const void *); + bool (*is_negotiated)(const void *); + + godot_error (*poll)(void *); + void (*close)(void *); + + void *next; /* For extension? */ +} godot_net_webrtc_data_channel; + +/* Set the default GDNative library */ +godot_error GDAPI godot_net_set_webrtc_library(const godot_net_webrtc_library *); +/* Binds a WebRTCPeerConnectionGDNative to the provided interface */ +void GDAPI godot_net_bind_webrtc_peer_connection(godot_object *p_obj, const godot_net_webrtc_peer_connection *); +/* Binds a WebRTCDataChannelGDNative to the provided interface */ +void GDAPI godot_net_bind_webrtc_data_channel(godot_object *p_obj, const godot_net_webrtc_data_channel *); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/modules/gdnative/net/webrtc_gdnative.cpp b/modules/gdnative/net/webrtc_gdnative.cpp new file mode 100644 index 0000000000..d77fa057c5 --- /dev/null +++ b/modules/gdnative/net/webrtc_gdnative.cpp @@ -0,0 +1,60 @@ +/*************************************************************************/ +/* webrtc_gdnative.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "modules/gdnative/gdnative.h" +#include "modules/gdnative/include/net/godot_net.h" + +#ifdef WEBRTC_GDNATIVE_ENABLED +#include "modules/webrtc/webrtc_data_channel_gdnative.h" +#include "modules/webrtc/webrtc_peer_connection_gdnative.h" +#endif + +extern "C" { + +void GDAPI godot_net_bind_webrtc_peer_connection(godot_object *p_obj, const godot_net_webrtc_peer_connection *p_impl) { +#ifdef WEBRTC_GDNATIVE_ENABLED + ((WebRTCPeerConnectionGDNative *)p_obj)->set_native_webrtc_peer_connection(p_impl); +#endif +} + +void GDAPI godot_net_bind_webrtc_data_channel(godot_object *p_obj, const godot_net_webrtc_data_channel *p_impl) { +#ifdef WEBRTC_GDNATIVE_ENABLED + ((WebRTCDataChannelGDNative *)p_obj)->set_native_webrtc_data_channel(p_impl); +#endif +} + +godot_error GDAPI godot_net_set_webrtc_library(const godot_net_webrtc_library *p_lib) { +#ifdef WEBRTC_GDNATIVE_ENABLED + return (godot_error)WebRTCPeerConnectionGDNative::set_default_library(p_lib); +#else + return ERR_UNAVAILABLE; +#endif +} +} diff --git a/modules/gdnative/net/webrtc_peer_gdnative.cpp b/modules/gdnative/net/webrtc_peer_gdnative.cpp deleted file mode 100644 index 60b1ed4fe4..0000000000 --- a/modules/gdnative/net/webrtc_peer_gdnative.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/*************************************************************************/ -/* packet_peer_gdnative.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "modules/gdnative/gdnative.h" -#include "modules/gdnative/include/net/godot_net.h" - -#ifdef WEBRTC_GDNATIVE_ENABLED -#include "modules/webrtc/webrtc_peer_gdnative.h" -#endif - -extern "C" { - -void GDAPI godot_net_bind_webrtc_peer(godot_object *p_obj, const godot_net_webrtc_peer *p_impl) { -#ifdef WEBRTC_GDNATIVE_ENABLED - ((WebRTCPeerGDNative *)p_obj)->set_native_webrtc_peer(p_impl); -#endif -} -} diff --git a/modules/webrtc/config.py b/modules/webrtc/config.py index 5ed245bad2..2e3a18ad0e 100644 --- a/modules/webrtc/config.py +++ b/modules/webrtc/config.py @@ -6,7 +6,8 @@ def configure(env): def get_doc_classes(): return [ - "WebRTCPeer" + "WebRTCPeerConnection", + "WebRTCDataChannel" ] def get_doc_path(): diff --git a/modules/webrtc/doc_classes/WebRTCDataChannel.xml b/modules/webrtc/doc_classes/WebRTCDataChannel.xml new file mode 100644 index 0000000000..dcc14d4ddb --- /dev/null +++ b/modules/webrtc/doc_classes/WebRTCDataChannel.xml @@ -0,0 +1,95 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/modules/webrtc/doc_classes/WebRTCPeer.xml b/modules/webrtc/doc_classes/WebRTCPeer.xml deleted file mode 100644 index 18d1345623..0000000000 --- a/modules/webrtc/doc_classes/WebRTCPeer.xml +++ /dev/null @@ -1,109 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/modules/webrtc/doc_classes/WebRTCPeerConnection.xml b/modules/webrtc/doc_classes/WebRTCPeerConnection.xml new file mode 100644 index 0000000000..8b14c60deb --- /dev/null +++ b/modules/webrtc/doc_classes/WebRTCPeerConnection.xml @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/modules/webrtc/register_types.cpp b/modules/webrtc/register_types.cpp index ee7a766bd9..44e072cc89 100644 --- a/modules/webrtc/register_types.cpp +++ b/modules/webrtc/register_types.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -29,27 +29,31 @@ /*************************************************************************/ #include "register_types.h" -#include "webrtc_peer.h" +#include "webrtc_data_channel.h" +#include "webrtc_peer_connection.h" #ifdef JAVASCRIPT_ENABLED #include "emscripten.h" -#include "webrtc_peer_js.h" +#include "webrtc_peer_connection_js.h" #endif #ifdef WEBRTC_GDNATIVE_ENABLED -#include "webrtc_peer_gdnative.h" +#include "webrtc_data_channel_gdnative.h" +#include "webrtc_peer_connection_gdnative.h" #endif void register_webrtc_types() { #ifdef JAVASCRIPT_ENABLED - WebRTCPeerJS::make_default(); + WebRTCPeerConnectionJS::make_default(); #elif defined(WEBRTC_GDNATIVE_ENABLED) - WebRTCPeerGDNative::make_default(); + WebRTCPeerConnectionGDNative::make_default(); #endif - ClassDB::register_custom_instance_class(); + ClassDB::register_custom_instance_class(); #ifdef WEBRTC_GDNATIVE_ENABLED - ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); #endif + ClassDB::register_virtual_class(); } void unregister_webrtc_types() {} diff --git a/modules/webrtc/register_types.h b/modules/webrtc/register_types.h index 18a5dcc5aa..4923547a95 100644 --- a/modules/webrtc/register_types.h +++ b/modules/webrtc/register_types.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ diff --git a/modules/webrtc/webrtc_data_channel.cpp b/modules/webrtc/webrtc_data_channel.cpp new file mode 100644 index 0000000000..2bd30e68f5 --- /dev/null +++ b/modules/webrtc/webrtc_data_channel.cpp @@ -0,0 +1,64 @@ +/*************************************************************************/ +/* webrtc_data_channel.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "webrtc_data_channel.h" + +void WebRTCDataChannel::_bind_methods() { + ClassDB::bind_method(D_METHOD("poll"), &WebRTCDataChannel::poll); + ClassDB::bind_method(D_METHOD("close"), &WebRTCDataChannel::close); + + ClassDB::bind_method(D_METHOD("was_string_packet"), &WebRTCDataChannel::was_string_packet); + ClassDB::bind_method(D_METHOD("set_write_mode", "write_mode"), &WebRTCDataChannel::set_write_mode); + ClassDB::bind_method(D_METHOD("get_write_mode"), &WebRTCDataChannel::get_write_mode); + ClassDB::bind_method(D_METHOD("get_ready_state"), &WebRTCDataChannel::get_ready_state); + ClassDB::bind_method(D_METHOD("get_label"), &WebRTCDataChannel::get_label); + ClassDB::bind_method(D_METHOD("is_ordered"), &WebRTCDataChannel::is_ordered); + ClassDB::bind_method(D_METHOD("get_id"), &WebRTCDataChannel::get_id); + ClassDB::bind_method(D_METHOD("get_max_packet_life_time"), &WebRTCDataChannel::get_max_packet_life_time); + ClassDB::bind_method(D_METHOD("get_max_retransmits"), &WebRTCDataChannel::get_max_retransmits); + ClassDB::bind_method(D_METHOD("get_protocol"), &WebRTCDataChannel::get_protocol); + ClassDB::bind_method(D_METHOD("is_negotiated"), &WebRTCDataChannel::is_negotiated); + + ADD_PROPERTY(PropertyInfo(Variant::INT, "write_mode", PROPERTY_HINT_ENUM), "set_write_mode", "get_write_mode"); + + BIND_ENUM_CONSTANT(WRITE_MODE_TEXT); + BIND_ENUM_CONSTANT(WRITE_MODE_BINARY); + + BIND_ENUM_CONSTANT(STATE_CONNECTING); + BIND_ENUM_CONSTANT(STATE_OPEN); + BIND_ENUM_CONSTANT(STATE_CLOSING); + BIND_ENUM_CONSTANT(STATE_CLOSED); +} + +WebRTCDataChannel::WebRTCDataChannel() { +} + +WebRTCDataChannel::~WebRTCDataChannel() { +} diff --git a/modules/webrtc/webrtc_data_channel.h b/modules/webrtc/webrtc_data_channel.h new file mode 100644 index 0000000000..0b161da784 --- /dev/null +++ b/modules/webrtc/webrtc_data_channel.h @@ -0,0 +1,85 @@ +/*************************************************************************/ +/* webrtc_data_channel.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef WEBRTC_DATA_CHANNEL_H +#define WEBRTC_DATA_CHANNEL_H + +#include "core/io/packet_peer.h" + +class WebRTCDataChannel : public PacketPeer { + GDCLASS(WebRTCDataChannel, PacketPeer); + +public: + enum WriteMode { + WRITE_MODE_TEXT, + WRITE_MODE_BINARY, + }; + + enum ChannelState { + STATE_CONNECTING, + STATE_OPEN, + STATE_CLOSING, + STATE_CLOSED + }; + +protected: + static void _bind_methods(); + +public: + virtual void set_write_mode(WriteMode mode) = 0; + virtual WriteMode get_write_mode() const = 0; + virtual bool was_string_packet() const = 0; + + virtual ChannelState get_ready_state() const = 0; + virtual String get_label() const = 0; + virtual bool is_ordered() const = 0; + virtual int get_id() const = 0; + virtual int get_max_packet_life_time() const = 0; + virtual int get_max_retransmits() const = 0; + virtual String get_protocol() const = 0; + virtual bool is_negotiated() const = 0; + + virtual Error poll() = 0; + virtual void close() = 0; + + /** Inherited from PacketPeer: **/ + virtual int get_available_packet_count() const = 0; + virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) = 0; ///< buffer is GONE after next get_packet + virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) = 0; + + virtual int get_max_packet_size() const = 0; + + WebRTCDataChannel(); + ~WebRTCDataChannel(); +}; + +VARIANT_ENUM_CAST(WebRTCDataChannel::WriteMode); +VARIANT_ENUM_CAST(WebRTCDataChannel::ChannelState); +#endif // WEBRTC_DATA_CHANNEL_H diff --git a/modules/webrtc/webrtc_data_channel_gdnative.cpp b/modules/webrtc/webrtc_data_channel_gdnative.cpp new file mode 100644 index 0000000000..4f33491af2 --- /dev/null +++ b/modules/webrtc/webrtc_data_channel_gdnative.cpp @@ -0,0 +1,136 @@ +/*************************************************************************/ +/* webrtc_data_channel_gdnative.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifdef WEBRTC_GDNATIVE_ENABLED + +#include "webrtc_data_channel_gdnative.h" +#include "core/io/resource_loader.h" +#include "modules/gdnative/nativescript/nativescript.h" + +void WebRTCDataChannelGDNative::_bind_methods() { +} + +WebRTCDataChannelGDNative::WebRTCDataChannelGDNative() { + interface = NULL; +} + +WebRTCDataChannelGDNative::~WebRTCDataChannelGDNative() { +} + +Error WebRTCDataChannelGDNative::poll() { + ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED); + return (Error)interface->poll(interface->data); +} + +void WebRTCDataChannelGDNative::close() { + ERR_FAIL_COND(interface == NULL); + interface->close(interface->data); +} + +void WebRTCDataChannelGDNative::set_write_mode(WriteMode p_mode) { + ERR_FAIL_COND(interface == NULL); + interface->set_write_mode(interface->data, p_mode); +} + +WebRTCDataChannel::WriteMode WebRTCDataChannelGDNative::get_write_mode() const { + ERR_FAIL_COND_V(interface == NULL, WRITE_MODE_BINARY); + return (WriteMode)interface->get_write_mode(interface->data); +} + +bool WebRTCDataChannelGDNative::was_string_packet() const { + ERR_FAIL_COND_V(interface == NULL, false); + return interface->was_string_packet(interface->data); +} + +WebRTCDataChannel::ChannelState WebRTCDataChannelGDNative::get_ready_state() const { + ERR_FAIL_COND_V(interface == NULL, STATE_CLOSED); + return (ChannelState)interface->get_ready_state(interface->data); +} + +String WebRTCDataChannelGDNative::get_label() const { + ERR_FAIL_COND_V(interface == NULL, ""); + return String(interface->get_label(interface->data)); +} + +bool WebRTCDataChannelGDNative::is_ordered() const { + ERR_FAIL_COND_V(interface == NULL, false); + return interface->is_ordered(interface->data); +} + +int WebRTCDataChannelGDNative::get_id() const { + ERR_FAIL_COND_V(interface == NULL, -1); + return interface->get_id(interface->data); +} + +int WebRTCDataChannelGDNative::get_max_packet_life_time() const { + ERR_FAIL_COND_V(interface == NULL, -1); + return interface->get_max_packet_life_time(interface->data); +} + +int WebRTCDataChannelGDNative::get_max_retransmits() const { + ERR_FAIL_COND_V(interface == NULL, -1); + return interface->get_max_retransmits(interface->data); +} + +String WebRTCDataChannelGDNative::get_protocol() const { + ERR_FAIL_COND_V(interface == NULL, ""); + return String(interface->get_protocol(interface->data)); +} + +bool WebRTCDataChannelGDNative::is_negotiated() const { + ERR_FAIL_COND_V(interface == NULL, false); + return interface->is_negotiated(interface->data); +} + +Error WebRTCDataChannelGDNative::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { + ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED); + return (Error)interface->get_packet(interface->data, r_buffer, &r_buffer_size); +} + +Error WebRTCDataChannelGDNative::put_packet(const uint8_t *p_buffer, int p_buffer_size) { + ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED); + return (Error)interface->put_packet(interface->data, p_buffer, p_buffer_size); +} + +int WebRTCDataChannelGDNative::get_max_packet_size() const { + ERR_FAIL_COND_V(interface == NULL, 0); + return interface->get_max_packet_size(interface->data); +} + +int WebRTCDataChannelGDNative::get_available_packet_count() const { + ERR_FAIL_COND_V(interface == NULL, 0); + return interface->get_available_packet_count(interface->data); +} + +void WebRTCDataChannelGDNative::set_native_webrtc_data_channel(const godot_net_webrtc_data_channel *p_impl) { + interface = p_impl; +} + +#endif // WEBRTC_GDNATIVE_ENABLED diff --git a/modules/webrtc/webrtc_data_channel_gdnative.h b/modules/webrtc/webrtc_data_channel_gdnative.h new file mode 100644 index 0000000000..3685f86353 --- /dev/null +++ b/modules/webrtc/webrtc_data_channel_gdnative.h @@ -0,0 +1,80 @@ +/*************************************************************************/ +/* webrtc_data_channel_gdnative.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifdef WEBRTC_GDNATIVE_ENABLED + +#ifndef WEBRTC_DATA_CHANNEL_GDNATIVE_H +#define WEBRTC_DATA_CHANNEL_GDNATIVE_H + +#include "modules/gdnative/include/net/godot_net.h" +#include "webrtc_data_channel.h" + +class WebRTCDataChannelGDNative : public WebRTCDataChannel { + GDCLASS(WebRTCDataChannelGDNative, WebRTCDataChannel); + +protected: + static void _bind_methods(); + +private: + const godot_net_webrtc_data_channel *interface; + +public: + void set_native_webrtc_data_channel(const godot_net_webrtc_data_channel *p_impl); + + virtual void set_write_mode(WriteMode mode); + virtual WriteMode get_write_mode() const; + virtual bool was_string_packet() const; + + virtual ChannelState get_ready_state() const; + virtual String get_label() const; + virtual bool is_ordered() const; + virtual int get_id() const; + virtual int get_max_packet_life_time() const; + virtual int get_max_retransmits() const; + virtual String get_protocol() const; + virtual bool is_negotiated() const; + + virtual Error poll(); + virtual void close(); + + /** Inherited from PacketPeer: **/ + virtual int get_available_packet_count() const; + virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size); ///< buffer is GONE after next get_packet + virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size); + + virtual int get_max_packet_size() const; + + WebRTCDataChannelGDNative(); + ~WebRTCDataChannelGDNative(); +}; + +#endif // WEBRTC_DATA_CHANNEL_GDNATIVE_H + +#endif // WEBRTC_GDNATIVE_ENABLED diff --git a/modules/webrtc/webrtc_data_channel_js.cpp b/modules/webrtc/webrtc_data_channel_js.cpp new file mode 100644 index 0000000000..2e7c64aa72 --- /dev/null +++ b/modules/webrtc/webrtc_data_channel_js.cpp @@ -0,0 +1,352 @@ +/*************************************************************************/ +/* webrtc_data_channel_js.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifdef JAVASCRIPT_ENABLED + +#include "webrtc_data_channel_js.h" +#include "emscripten.h" + +extern "C" { +EMSCRIPTEN_KEEPALIVE void _emrtc_on_ch_error(void *obj) { + WebRTCDataChannelJS *peer = static_cast(obj); + peer->_on_error(); +} + +EMSCRIPTEN_KEEPALIVE void _emrtc_on_ch_open(void *obj) { + WebRTCDataChannelJS *peer = static_cast(obj); + peer->_on_open(); +} + +EMSCRIPTEN_KEEPALIVE void _emrtc_on_ch_close(void *obj) { + WebRTCDataChannelJS *peer = static_cast(obj); + peer->_on_close(); +} + +EMSCRIPTEN_KEEPALIVE void _emrtc_on_ch_message(void *obj, uint8_t *p_data, uint32_t p_size, bool p_is_string) { + WebRTCDataChannelJS *peer = static_cast(obj); + peer->_on_message(p_data, p_size, p_is_string); +} +} + +void WebRTCDataChannelJS::_on_open() { + in_buffer.resize(16); +} + +void WebRTCDataChannelJS::_on_close() { + close(); +} + +void WebRTCDataChannelJS::_on_error() { + close(); +} + +void WebRTCDataChannelJS::_on_message(uint8_t *p_data, uint32_t p_size, bool p_is_string) { + if (in_buffer.space_left() < p_size + 5) { + ERR_EXPLAIN("Buffer full! Dropping data"); + ERR_FAIL(); + } + + uint8_t is_string = p_is_string ? 1 : 0; + in_buffer.write((uint8_t *)&p_size, 4); + in_buffer.write((uint8_t *)&is_string, 1); + in_buffer.write(p_data, p_size); + queue_count++; +} + +void WebRTCDataChannelJS::close() { + in_buffer.resize(0); + queue_count = 0; + _was_string = false; + /* clang-format off */ + EM_ASM({ + var dict = Module.IDHandler.get($0); + if (!dict) return; + var channel = dict["channel"]; + channel.onopen = null; + channel.onclose = null; + channel.onerror = null; + channel.onmessage = null; + channel.close(); + }, _js_id); + /* clang-format on */ +} + +Error WebRTCDataChannelJS::poll() { + return OK; +} + +WebRTCDataChannelJS::ChannelState WebRTCDataChannelJS::get_ready_state() const { + /* clang-format off */ + return (ChannelState) EM_ASM_INT({ + var dict = Module.IDHandler.get($0); + if (!dict) return 3; // CLOSED + var channel = dict["channel"]; + switch(channel.readyState) { + case "connecting": + return 0; + case "open": + return 1; + case "closing": + return 2; + case "closed": + return 3; + } + return 3; // CLOSED + }, _js_id); + /* clang-format on */ +} + +int WebRTCDataChannelJS::get_available_packet_count() const { + return queue_count; +} + +Error WebRTCDataChannelJS::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { + ERR_FAIL_COND_V(get_ready_state() != STATE_OPEN, ERR_UNCONFIGURED); + + if (queue_count == 0) + return ERR_UNAVAILABLE; + + uint32_t to_read = 0; + uint32_t left = 0; + uint8_t is_string = 0; + r_buffer_size = 0; + + in_buffer.read((uint8_t *)&to_read, 4); + --queue_count; + left = in_buffer.data_left(); + + if (left < to_read + 1) { + in_buffer.advance_read(left); + return FAILED; + } + + in_buffer.read(&is_string, 1); + _was_string = is_string == 1; + in_buffer.read(packet_buffer, to_read); + *r_buffer = packet_buffer; + r_buffer_size = to_read; + + return OK; +} + +Error WebRTCDataChannelJS::put_packet(const uint8_t *p_buffer, int p_buffer_size) { + ERR_FAIL_COND_V(get_ready_state() != STATE_OPEN, ERR_UNCONFIGURED); + + int is_bin = _write_mode == WebRTCDataChannel::WRITE_MODE_BINARY ? 1 : 0; + + /* clang-format off */ + EM_ASM({ + var dict = Module.IDHandler.get($0); + var channel = dict["channel"]; + var bytes_array = new Uint8Array($2); + var i = 0; + + for(i=0; i<$2; i++) { + bytes_array[i] = getValue($1+i, 'i8'); + } + + if ($3) { + channel.send(bytes_array.buffer); + } else { + var string = new TextDecoder("utf-8").decode(bytes_array); + channel.send(string); + } + }, _js_id, p_buffer, p_buffer_size, is_bin); + /* clang-format on */ + + return OK; +} + +int WebRTCDataChannelJS::get_max_packet_size() const { + return 1200; +} + +void WebRTCDataChannelJS::set_write_mode(WriteMode p_mode) { + _write_mode = p_mode; +} + +WebRTCDataChannel::WriteMode WebRTCDataChannelJS::get_write_mode() const { + return _write_mode; +} + +bool WebRTCDataChannelJS::was_string_packet() const { + return _was_string; +} + +String WebRTCDataChannelJS::get_label() const { + return _label; +} + +/* clang-format off */ +#define _JS_GET(PROP) \ +EM_ASM_INT({ \ + var dict = Module.IDHandler.get($0); \ + if (!dict || !dict["channel"]) { \ + return 0; \ + }; \ + return dict["channel"].PROP; \ +}, _js_id) +/* clang-format on */ + +bool WebRTCDataChannelJS::is_ordered() const { + return _JS_GET(ordered); +} + +int WebRTCDataChannelJS::get_id() const { + return _JS_GET(id); +} + +int WebRTCDataChannelJS::get_max_packet_life_time() const { + return _JS_GET(maxPacketLifeTime); +} + +int WebRTCDataChannelJS::get_max_retransmits() const { + return _JS_GET(maxRetransmits); +} + +String WebRTCDataChannelJS::get_protocol() const { + return _protocol; +} + +bool WebRTCDataChannelJS::is_negotiated() const { + return _JS_GET(negotiated); +} + +WebRTCDataChannelJS::WebRTCDataChannelJS() { + queue_count = 0; + _was_string = false; + _write_mode = WRITE_MODE_BINARY; + _js_id = 0; +} + +WebRTCDataChannelJS::WebRTCDataChannelJS(int js_id) { + queue_count = 0; + _was_string = false; + _write_mode = WRITE_MODE_BINARY; + _js_id = js_id; + + /* clang-format off */ + EM_ASM({ + var c_ptr = $0; + var dict = Module.IDHandler.get($1); + if (!dict) return; + var channel = dict["channel"]; + dict["ptr"] = c_ptr; + + channel.binaryType = "arraybuffer"; + channel.onopen = function (evt) { + ccall("_emrtc_on_ch_open", + "void", + ["number"], + [c_ptr] + ); + }; + channel.onclose = function (evt) { + ccall("_emrtc_on_ch_close", + "void", + ["number"], + [c_ptr] + ); + }; + channel.onerror = function (evt) { + ccall("_emrtc_on_ch_error", + "void", + ["number"], + [c_ptr] + ); + }; + channel.onmessage = function(event) { + var buffer; + var is_string = 0; + if (event.data instanceof ArrayBuffer) { + buffer = new Uint8Array(event.data); + } else if (event.data instanceof Blob) { + console.error("Blob type not supported"); + return; + } else if (typeof event.data === "string") { + is_string = 1; + var enc = new TextEncoder("utf-8"); + buffer = new Uint8Array(enc.encode(event.data)); + } else { + console.error("Unknown message type"); + return; + } + var len = buffer.length*buffer.BYTES_PER_ELEMENT; + var out = Module._malloc(len); + Module.HEAPU8.set(buffer, out); + ccall("_emrtc_on_ch_message", + "void", + ["number", "number", "number", "number"], + [c_ptr, out, len, is_string] + ); + Module._free(out); + } + + }, this, js_id); + // Parse label + char *str; + str = (char *)EM_ASM_INT({ + var dict = Module.IDHandler.get($0); + if (!dict || !dict["channel"]) return 0; + var str = dict["channel"].label; + var len = lengthBytesUTF8(str)+1; + var ptr = _malloc(str); + stringToUTF8(str, ptr, len+1); + return ptr; + }, js_id); + if(str != NULL) { + _label.parse_utf8(str); + EM_ASM({ _free($0) }, str); + } + str = (char *)EM_ASM_INT({ + var dict = Module.IDHandler.get($0); + if (!dict || !dict["channel"]) return 0; + var str = dict["channel"].protocol; + var len = lengthBytesUTF8(str)+1; + var ptr = _malloc(str); + stringToUTF8(str, ptr, len+1); + return ptr; + }, js_id); + if(str != NULL) { + _protocol.parse_utf8(str); + EM_ASM({ _free($0) }, str); + } + /* clang-format on */ +} + +WebRTCDataChannelJS::~WebRTCDataChannelJS() { + close(); + /* clang-format off */ + EM_ASM({ + Module.IDHandler.remove($0); + }, _js_id); + /* clang-format on */ +}; +#endif diff --git a/modules/webrtc/webrtc_data_channel_js.h b/modules/webrtc/webrtc_data_channel_js.h new file mode 100644 index 0000000000..b87f8e9326 --- /dev/null +++ b/modules/webrtc/webrtc_data_channel_js.h @@ -0,0 +1,93 @@ +/*************************************************************************/ +/* webrtc_data_channel_js.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifdef JAVASCRIPT_ENABLED + +#ifndef WEBRTC_DATA_CHANNEL_JS_H +#define WEBRTC_DATA_CHANNEL_JS_H + +#include "webrtc_data_channel.h" + +class WebRTCDataChannelJS : public WebRTCDataChannel { + GDCLASS(WebRTCDataChannelJS, WebRTCDataChannel); + +private: + String _label; + String _protocol; + + bool _was_string; + WriteMode _write_mode; + + enum { + PACKET_BUFFER_SIZE = 65536 - 5 // 4 bytes for the size, 1 for for type + }; + + int _js_id; + RingBuffer in_buffer; + int queue_count; + uint8_t packet_buffer[PACKET_BUFFER_SIZE]; + +public: + void _on_open(); + void _on_close(); + void _on_error(); + void _on_message(uint8_t *p_data, uint32_t p_size, bool p_is_string); + + virtual void set_write_mode(WriteMode mode); + virtual WriteMode get_write_mode() const; + virtual bool was_string_packet() const; + + virtual ChannelState get_ready_state() const; + virtual String get_label() const; + virtual bool is_ordered() const; + virtual int get_id() const; + virtual int get_max_packet_life_time() const; + virtual int get_max_retransmits() const; + virtual String get_protocol() const; + virtual bool is_negotiated() const; + + virtual Error poll(); + virtual void close(); + + /** Inherited from PacketPeer: **/ + virtual int get_available_packet_count() const; + virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size); ///< buffer is GONE after next get_packet + virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size); + + virtual int get_max_packet_size() const; + + WebRTCDataChannelJS(); + WebRTCDataChannelJS(int js_id); + ~WebRTCDataChannelJS(); +}; + +#endif // WEBRTC_DATA_CHANNEL_JS_H + +#endif // JAVASCRIPT_ENABLED diff --git a/modules/webrtc/webrtc_peer.cpp b/modules/webrtc/webrtc_peer.cpp deleted file mode 100644 index 30c4505df9..0000000000 --- a/modules/webrtc/webrtc_peer.cpp +++ /dev/null @@ -1,81 +0,0 @@ -/*************************************************************************/ -/* webrtc_peer.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "webrtc_peer.h" - -WebRTCPeer *(*WebRTCPeer::_create)() = NULL; - -Ref WebRTCPeer::create_ref() { - - if (!_create) - return Ref(); - return Ref(_create()); -} - -WebRTCPeer *WebRTCPeer::create() { - - if (!_create) - return NULL; - return _create(); -} - -void WebRTCPeer::_bind_methods() { - ClassDB::bind_method(D_METHOD("create_offer"), &WebRTCPeer::create_offer); - ClassDB::bind_method(D_METHOD("set_local_description", "type", "sdp"), &WebRTCPeer::set_local_description); - ClassDB::bind_method(D_METHOD("set_remote_description", "type", "sdp"), &WebRTCPeer::set_remote_description); - ClassDB::bind_method(D_METHOD("poll"), &WebRTCPeer::poll); - ClassDB::bind_method(D_METHOD("add_ice_candidate", "media", "index", "name"), &WebRTCPeer::add_ice_candidate); - - ClassDB::bind_method(D_METHOD("was_string_packet"), &WebRTCPeer::was_string_packet); - ClassDB::bind_method(D_METHOD("set_write_mode", "write_mode"), &WebRTCPeer::set_write_mode); - ClassDB::bind_method(D_METHOD("get_write_mode"), &WebRTCPeer::get_write_mode); - ClassDB::bind_method(D_METHOD("get_connection_state"), &WebRTCPeer::get_connection_state); - - ADD_SIGNAL(MethodInfo("offer_created", PropertyInfo(Variant::STRING, "type"), PropertyInfo(Variant::STRING, "sdp"))); - ADD_SIGNAL(MethodInfo("new_ice_candidate", PropertyInfo(Variant::STRING, "media"), PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::STRING, "name"))); - - ADD_PROPERTY(PropertyInfo(Variant::INT, "write_mode", PROPERTY_HINT_ENUM), "set_write_mode", "get_write_mode"); - - BIND_ENUM_CONSTANT(WRITE_MODE_TEXT); - BIND_ENUM_CONSTANT(WRITE_MODE_BINARY); - - BIND_ENUM_CONSTANT(STATE_NEW); - BIND_ENUM_CONSTANT(STATE_CONNECTING); - BIND_ENUM_CONSTANT(STATE_CONNECTED); - BIND_ENUM_CONSTANT(STATE_DISCONNECTED); - BIND_ENUM_CONSTANT(STATE_FAILED); - BIND_ENUM_CONSTANT(STATE_CLOSED); -} - -WebRTCPeer::WebRTCPeer() { -} - -WebRTCPeer::~WebRTCPeer() { -} diff --git a/modules/webrtc/webrtc_peer.h b/modules/webrtc/webrtc_peer.h deleted file mode 100644 index e141c14655..0000000000 --- a/modules/webrtc/webrtc_peer.h +++ /dev/null @@ -1,86 +0,0 @@ -/*************************************************************************/ -/* webrtc_peer.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef WEBRTC_PEER_H -#define WEBRTC_PEER_H - -#include "core/io/packet_peer.h" - -class WebRTCPeer : public PacketPeer { - GDCLASS(WebRTCPeer, PacketPeer); - -public: - enum WriteMode { - WRITE_MODE_TEXT, - WRITE_MODE_BINARY, - }; - - enum ConnectionState { - STATE_NEW, - STATE_CONNECTING, - STATE_CONNECTED, - STATE_DISCONNECTED, - STATE_FAILED, - STATE_CLOSED - }; - -protected: - static void _bind_methods(); - static WebRTCPeer *(*_create)(); - -public: - virtual void set_write_mode(WriteMode mode) = 0; - virtual WriteMode get_write_mode() const = 0; - virtual bool was_string_packet() const = 0; - virtual ConnectionState get_connection_state() const = 0; - - virtual Error create_offer() = 0; - virtual Error set_remote_description(String type, String sdp) = 0; - virtual Error set_local_description(String type, String sdp) = 0; - virtual Error add_ice_candidate(String sdpMidName, int sdpMlineIndexName, String sdpName) = 0; - virtual Error poll() = 0; - - /** Inherited from PacketPeer: **/ - virtual int get_available_packet_count() const = 0; - virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) = 0; ///< buffer is GONE after next get_packet - virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) = 0; - - virtual int get_max_packet_size() const = 0; - - static Ref create_ref(); - static WebRTCPeer *create(); - - WebRTCPeer(); - ~WebRTCPeer(); -}; - -VARIANT_ENUM_CAST(WebRTCPeer::WriteMode); -VARIANT_ENUM_CAST(WebRTCPeer::ConnectionState); -#endif // WEBRTC_PEER_H diff --git a/modules/webrtc/webrtc_peer_connection.cpp b/modules/webrtc/webrtc_peer_connection.cpp new file mode 100644 index 0000000000..69c7a51a40 --- /dev/null +++ b/modules/webrtc/webrtc_peer_connection.cpp @@ -0,0 +1,75 @@ +/*************************************************************************/ +/* webrtc_peer_connection.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "webrtc_peer_connection.h" + +WebRTCPeerConnection *(*WebRTCPeerConnection::_create)() = NULL; + +Ref WebRTCPeerConnection::create_ref() { + + return create(); +} + +WebRTCPeerConnection *WebRTCPeerConnection::create() { + + if (!_create) + return NULL; + return _create(); +} + +void WebRTCPeerConnection::_bind_methods() { + ClassDB::bind_method(D_METHOD("initialize", "configuration"), &WebRTCPeerConnection::initialize, DEFVAL(Dictionary())); + ClassDB::bind_method(D_METHOD("create_data_channel", "label", "options"), &WebRTCPeerConnection::create_data_channel, DEFVAL(Dictionary())); + ClassDB::bind_method(D_METHOD("create_offer"), &WebRTCPeerConnection::create_offer); + ClassDB::bind_method(D_METHOD("set_local_description", "type", "sdp"), &WebRTCPeerConnection::set_local_description); + ClassDB::bind_method(D_METHOD("set_remote_description", "type", "sdp"), &WebRTCPeerConnection::set_remote_description); + ClassDB::bind_method(D_METHOD("add_ice_candidate", "media", "index", "name"), &WebRTCPeerConnection::add_ice_candidate); + ClassDB::bind_method(D_METHOD("poll"), &WebRTCPeerConnection::poll); + ClassDB::bind_method(D_METHOD("close"), &WebRTCPeerConnection::close); + + ClassDB::bind_method(D_METHOD("get_connection_state"), &WebRTCPeerConnection::get_connection_state); + + ADD_SIGNAL(MethodInfo("session_description_created", PropertyInfo(Variant::STRING, "type"), PropertyInfo(Variant::STRING, "sdp"))); + ADD_SIGNAL(MethodInfo("ice_candidate_created", PropertyInfo(Variant::STRING, "media"), PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::STRING, "name"))); + ADD_SIGNAL(MethodInfo("data_channel_received", PropertyInfo(Variant::OBJECT, "channel"))); + + BIND_ENUM_CONSTANT(STATE_NEW); + BIND_ENUM_CONSTANT(STATE_CONNECTING); + BIND_ENUM_CONSTANT(STATE_CONNECTED); + BIND_ENUM_CONSTANT(STATE_DISCONNECTED); + BIND_ENUM_CONSTANT(STATE_FAILED); + BIND_ENUM_CONSTANT(STATE_CLOSED); +} + +WebRTCPeerConnection::WebRTCPeerConnection() { +} + +WebRTCPeerConnection::~WebRTCPeerConnection() { +} diff --git a/modules/webrtc/webrtc_peer_connection.h b/modules/webrtc/webrtc_peer_connection.h new file mode 100644 index 0000000000..7be1390dab --- /dev/null +++ b/modules/webrtc/webrtc_peer_connection.h @@ -0,0 +1,74 @@ +/*************************************************************************/ +/* webrtc_peer_connection.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef WEBRTC_PEER_CONNECTION_H +#define WEBRTC_PEER_CONNECTION_H + +#include "core/io/packet_peer.h" +#include "modules/webrtc/webrtc_data_channel.h" + +class WebRTCPeerConnection : public Reference { + GDCLASS(WebRTCPeerConnection, Reference); + +public: + enum ConnectionState { + STATE_NEW, + STATE_CONNECTING, + STATE_CONNECTED, + STATE_DISCONNECTED, + STATE_FAILED, + STATE_CLOSED + }; + +protected: + static void _bind_methods(); + static WebRTCPeerConnection *(*_create)(); + +public: + virtual ConnectionState get_connection_state() const = 0; + + virtual Error initialize(Dictionary p_config = Dictionary()) = 0; + virtual Ref create_data_channel(String p_label, Dictionary p_options = Dictionary()) = 0; + virtual Error create_offer() = 0; + virtual Error set_remote_description(String type, String sdp) = 0; + virtual Error set_local_description(String type, String sdp) = 0; + virtual Error add_ice_candidate(String sdpMidName, int sdpMlineIndexName, String sdpName) = 0; + virtual Error poll() = 0; + virtual void close() = 0; + + static Ref create_ref(); + static WebRTCPeerConnection *create(); + + WebRTCPeerConnection(); + ~WebRTCPeerConnection(); +}; + +VARIANT_ENUM_CAST(WebRTCPeerConnection::ConnectionState); +#endif // WEBRTC_PEER_CONNECTION_H diff --git a/modules/webrtc/webrtc_peer_connection_gdnative.cpp b/modules/webrtc/webrtc_peer_connection_gdnative.cpp new file mode 100644 index 0000000000..af98aa750a --- /dev/null +++ b/modules/webrtc/webrtc_peer_connection_gdnative.cpp @@ -0,0 +1,124 @@ +/*************************************************************************/ +/* webrtc_peer_connection_gdnative.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifdef WEBRTC_GDNATIVE_ENABLED + +#include "webrtc_peer_connection_gdnative.h" + +#include "core/io/resource_loader.h" +#include "modules/gdnative/nativescript/nativescript.h" +#include "webrtc_data_channel_gdnative.h" + +const godot_net_webrtc_library *WebRTCPeerConnectionGDNative::default_library = NULL; + +Error WebRTCPeerConnectionGDNative::set_default_library(const godot_net_webrtc_library *p_lib) { + if (default_library) { + const godot_net_webrtc_library *old = default_library; + default_library = NULL; + old->unregistered(); + } + default_library = p_lib; + return OK; // Maybe add version check and fail accordingly +} + +WebRTCPeerConnection *WebRTCPeerConnectionGDNative::_create() { + + WebRTCPeerConnectionGDNative *obj = memnew(WebRTCPeerConnectionGDNative); + ERR_EXPLAIN("Default GDNative WebRTC implementation not defined."); + ERR_FAIL_COND_V(!default_library, obj); + + // Call GDNative constructor + Error err = (Error)default_library->create_peer_connection(obj); + ERR_EXPLAIN("GDNative default library constructor returned an error"); + ERR_FAIL_COND_V(err != OK, obj); + + return obj; +} + +void WebRTCPeerConnectionGDNative::_bind_methods() { +} + +WebRTCPeerConnectionGDNative::WebRTCPeerConnectionGDNative() { + interface = NULL; +} + +WebRTCPeerConnectionGDNative::~WebRTCPeerConnectionGDNative() { +} + +Error WebRTCPeerConnectionGDNative::initialize(Dictionary p_config) { + ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED); + return (Error)interface->initialize(interface->data, (const godot_dictionary *)&p_config); +} + +Ref WebRTCPeerConnectionGDNative::create_data_channel(String p_label, Dictionary p_options) { + ERR_FAIL_COND_V(interface == NULL, NULL); + return (WebRTCDataChannel *)interface->create_data_channel(interface->data, p_label.utf8().get_data(), (const godot_dictionary *)&p_options); +} + +Error WebRTCPeerConnectionGDNative::create_offer() { + ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED); + return (Error)interface->create_offer(interface->data); +} + +Error WebRTCPeerConnectionGDNative::set_local_description(String p_type, String p_sdp) { + ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED); + return (Error)interface->set_local_description(interface->data, p_type.utf8().get_data(), p_sdp.utf8().get_data()); +} + +Error WebRTCPeerConnectionGDNative::set_remote_description(String p_type, String p_sdp) { + ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED); + return (Error)interface->set_remote_description(interface->data, p_type.utf8().get_data(), p_sdp.utf8().get_data()); +} + +Error WebRTCPeerConnectionGDNative::add_ice_candidate(String sdpMidName, int sdpMlineIndexName, String sdpName) { + ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED); + return (Error)interface->add_ice_candidate(interface->data, sdpMidName.utf8().get_data(), sdpMlineIndexName, sdpName.utf8().get_data()); +} + +Error WebRTCPeerConnectionGDNative::poll() { + ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED); + return (Error)interface->poll(interface->data); +} + +void WebRTCPeerConnectionGDNative::close() { + ERR_FAIL_COND(interface == NULL); + interface->close(interface->data); +} + +WebRTCPeerConnection::ConnectionState WebRTCPeerConnectionGDNative::get_connection_state() const { + ERR_FAIL_COND_V(interface == NULL, STATE_DISCONNECTED); + return (ConnectionState)interface->get_connection_state(interface->data); +} + +void WebRTCPeerConnectionGDNative::set_native_webrtc_peer_connection(const godot_net_webrtc_peer_connection *p_impl) { + interface = p_impl; +} + +#endif // WEBRTC_GDNATIVE_ENABLED diff --git a/modules/webrtc/webrtc_peer_connection_gdnative.h b/modules/webrtc/webrtc_peer_connection_gdnative.h new file mode 100644 index 0000000000..0a281c3d89 --- /dev/null +++ b/modules/webrtc/webrtc_peer_connection_gdnative.h @@ -0,0 +1,73 @@ +/*************************************************************************/ +/* webrtc_peer_connection_gdnative.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifdef WEBRTC_GDNATIVE_ENABLED + +#ifndef WEBRTC_PEER_CONNECTION_GDNATIVE_H +#define WEBRTC_PEER_CONNECTION_GDNATIVE_H + +#include "modules/gdnative/include/net/godot_net.h" +#include "webrtc_peer_connection.h" + +class WebRTCPeerConnectionGDNative : public WebRTCPeerConnection { + GDCLASS(WebRTCPeerConnectionGDNative, WebRTCPeerConnection); + +protected: + static void _bind_methods(); + static WebRTCPeerConnection *_create(); + +private: + static const godot_net_webrtc_library *default_library; + const godot_net_webrtc_peer_connection *interface; + +public: + static Error set_default_library(const godot_net_webrtc_library *p_library); + static void make_default() { WebRTCPeerConnection::_create = WebRTCPeerConnectionGDNative::_create; } + + void set_native_webrtc_peer_connection(const godot_net_webrtc_peer_connection *p_impl); + + virtual ConnectionState get_connection_state() const; + + virtual Error initialize(Dictionary p_config = Dictionary()); + virtual Ref create_data_channel(String p_label, Dictionary p_options = Dictionary()); + virtual Error create_offer(); + virtual Error set_remote_description(String type, String sdp); + virtual Error set_local_description(String type, String sdp); + virtual Error add_ice_candidate(String sdpMidName, int sdpMlineIndexName, String sdpName); + virtual Error poll(); + virtual void close(); + + WebRTCPeerConnectionGDNative(); + ~WebRTCPeerConnectionGDNative(); +}; + +#endif // WEBRTC_PEER_CONNECTION_GDNATIVE_H + +#endif // WEBRTC_GDNATIVE_ENABLED diff --git a/modules/webrtc/webrtc_peer_connection_js.cpp b/modules/webrtc/webrtc_peer_connection_js.cpp new file mode 100644 index 0000000000..9758ab3644 --- /dev/null +++ b/modules/webrtc/webrtc_peer_connection_js.cpp @@ -0,0 +1,314 @@ +/*************************************************************************/ +/* webrtc_peer_connection_js.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifdef JAVASCRIPT_ENABLED + +#include "webrtc_peer_connection_js.h" + +#include "webrtc_data_channel_js.h" + +#include "core/io/json.h" +#include "emscripten.h" + +extern "C" { +EMSCRIPTEN_KEEPALIVE void _emrtc_on_ice_candidate(void *obj, char *p_MidName, int p_MlineIndexName, char *p_sdpName) { + WebRTCPeerConnectionJS *peer = static_cast(obj); + peer->emit_signal("ice_candidate_created", String(p_MidName), p_MlineIndexName, String(p_sdpName)); +} + +EMSCRIPTEN_KEEPALIVE void _emrtc_session_description_created(void *obj, char *p_type, char *p_offer) { + WebRTCPeerConnectionJS *peer = static_cast(obj); + peer->emit_signal("session_description_created", String(p_type), String(p_offer)); +} + +EMSCRIPTEN_KEEPALIVE void _emrtc_on_connection_state_changed(void *obj) { + WebRTCPeerConnectionJS *peer = static_cast(obj); + peer->_on_connection_state_changed(); +} + +EMSCRIPTEN_KEEPALIVE void _emrtc_on_error() { + ERR_PRINT("RTCPeerConnection error!"); +} + +EMSCRIPTEN_KEEPALIVE void _emrtc_emit_channel(void *obj, int p_id) { + WebRTCPeerConnectionJS *peer = static_cast(obj); + peer->emit_signal("data_channel_received", Ref(new WebRTCDataChannelJS(p_id))); +} +} + +void _emrtc_create_pc(int p_id, const Dictionary &p_config) { + String config = JSON::print(p_config); + /* clang-format off */ + EM_ASM({ + var dict = Module.IDHandler.get($0); + var c_ptr = dict["ptr"]; + var config = JSON.parse(UTF8ToString($1)); + // Setup local connaction + var conn = null; + try { + conn = new RTCPeerConnection(config); + } catch (e) { + console.log(e); + return; + } + conn.oniceconnectionstatechange = function(event) { + if (!Module.IDHandler.get($0)) return; + ccall("_emrtc_on_connection_state_changed", "void", ["number"], [c_ptr]); + }; + conn.onicecandidate = function(event) { + if (!Module.IDHandler.get($0)) return; + if (!event.candidate) return; + + var c = event.candidate; + // should emit on ice candidate + ccall("_emrtc_on_ice_candidate", + "void", + ["number", "string", "number", "string"], + [c_ptr, c.sdpMid, c.sdpMLineIndex, c.candidate] + ); + }; + conn.ondatachannel = function (evt) { + var dict = Module.IDHandler.get($0); + if (!dict) { + return; + } + var id = Module.IDHandler.add({"channel": evt.channel, "ptr": null}); + ccall("_emrtc_emit_channel", + "void", + ["number", "number"], + [c_ptr, id] + ); + }; + dict["conn"] = conn; + }, p_id, config.utf8().get_data()); + /* clang-format on */ +} + +void WebRTCPeerConnectionJS::_on_connection_state_changed() { + /* clang-format off */ + _conn_state = (ConnectionState)EM_ASM_INT({ + var dict = Module.IDHandler.get($0); + if (!dict) return 5; // CLOSED + var conn = dict["conn"]; + switch(conn.iceConnectionState) { + case "new": + return 0; + case "checking": + return 1; + case "connected": + case "completed": + return 2; + case "disconnected": + return 3; + case "failed": + return 4; + case "closed": + return 5; + } + return 5; // CLOSED + }, _js_id); + /* clang-format on */ +} + +void WebRTCPeerConnectionJS::close() { + /* clang-format off */ + EM_ASM({ + var dict = Module.IDHandler.get($0); + if (!dict) return; + if (dict["conn"]) { + dict["conn"].close(); + } + }, _js_id); + /* clang-format on */ + _conn_state = STATE_CLOSED; +} + +Error WebRTCPeerConnectionJS::create_offer() { + ERR_FAIL_COND_V(_conn_state != STATE_NEW, FAILED); + + _conn_state = STATE_CONNECTING; + /* clang-format off */ + EM_ASM({ + var dict = Module.IDHandler.get($0); + var conn = dict["conn"]; + var c_ptr = dict["ptr"]; + var onError = function(error) { + console.error(error); + ccall("_emrtc_on_error", "void", [], []); + }; + var onCreated = function(offer) { + ccall("_emrtc_session_description_created", + "void", + ["number", "string", "string"], + [c_ptr, offer.type, offer.sdp] + ); + }; + conn.createOffer().then(onCreated).catch(onError); + }, _js_id); + /* clang-format on */ + return OK; +} + +Error WebRTCPeerConnectionJS::set_local_description(String type, String sdp) { + /* clang-format off */ + EM_ASM({ + var dict = Module.IDHandler.get($0); + var conn = dict["conn"]; + var c_ptr = dict["ptr"]; + var type = UTF8ToString($1); + var sdp = UTF8ToString($2); + var onError = function(error) { + console.error(error); + ccall("_emrtc_on_error", "void", [], []); + }; + conn.setLocalDescription({ + "sdp": sdp, + "type": type + }).catch(onError); + }, _js_id, type.utf8().get_data(), sdp.utf8().get_data()); + /* clang-format on */ + return OK; +} + +Error WebRTCPeerConnectionJS::set_remote_description(String type, String sdp) { + if (type == "offer") { + ERR_FAIL_COND_V(_conn_state != STATE_NEW, FAILED); + _conn_state = STATE_CONNECTING; + } + /* clang-format off */ + EM_ASM({ + var dict = Module.IDHandler.get($0); + var conn = dict["conn"]; + var c_ptr = dict["ptr"]; + var type = UTF8ToString($1); + var sdp = UTF8ToString($2); + + var onError = function(error) { + console.error(error); + ccall("_emrtc_on_error", "void", [], []); + }; + var onCreated = function(offer) { + ccall("_emrtc_session_description_created", + "void", + ["number", "string", "string"], + [c_ptr, offer.type, offer.sdp] + ); + }; + var onSet = function() { + if (type != "offer") { + return; + } + conn.createAnswer().then(onCreated); + }; + conn.setRemoteDescription({ + "sdp": sdp, + "type": type + }).then(onSet).catch(onError); + }, _js_id, type.utf8().get_data(), sdp.utf8().get_data()); + /* clang-format on */ + return OK; +} + +Error WebRTCPeerConnectionJS::add_ice_candidate(String sdpMidName, int sdpMlineIndexName, String sdpName) { + /* clang-format off */ + EM_ASM({ + var dict = Module.IDHandler.get($0); + var conn = dict["conn"]; + var c_ptr = dict["ptr"]; + var sdpMidName = UTF8ToString($1); + var sdpMlineIndexName = UTF8ToString($2); + var sdpName = UTF8ToString($3); + conn.addIceCandidate(new RTCIceCandidate({ + "candidate": sdpName, + "sdpMid": sdpMidName, + "sdpMlineIndex": sdpMlineIndexName + })); + }, _js_id, sdpMidName.utf8().get_data(), sdpMlineIndexName, sdpName.utf8().get_data()); + /* clang-format on */ + return OK; +} + +Error WebRTCPeerConnectionJS::initialize(Dictionary p_config) { + _emrtc_create_pc(_js_id, p_config); + return OK; +} + +Ref WebRTCPeerConnectionJS::create_data_channel(String p_channel, Dictionary p_channel_config) { + String config = JSON::print(p_channel_config); + /* clang-format off */ + int id = EM_ASM_INT({ + try { + var dict = Module.IDHandler.get($0); + if (!dict) return 0; + var label = UTF8ToString($1); + var config = JSON.parse(UTF8ToString($2)); + var conn = dict["conn"]; + return Module.IDHandler.add({ + "channel": conn.createDataChannel(label, config), + "ptr": null + }) + } catch (e) { + return 0; + } + }, _js_id, p_channel.utf8().get_data(), config.utf8().get_data()); + /* clang-format on */ + ERR_FAIL_COND_V(id == 0, NULL); + return memnew(WebRTCDataChannelJS(id)); +} + +Error WebRTCPeerConnectionJS::poll() { + return OK; +} + +WebRTCPeerConnection::ConnectionState WebRTCPeerConnectionJS::get_connection_state() const { + return _conn_state; +} + +WebRTCPeerConnectionJS::WebRTCPeerConnectionJS() { + _conn_state = STATE_NEW; + + /* clang-format off */ + _js_id = EM_ASM_INT({ + return Module.IDHandler.add({"conn": null, "ptr": $0}); + }, this); + /* clang-format on */ + Dictionary config; + _emrtc_create_pc(_js_id, config); +} + +WebRTCPeerConnectionJS::~WebRTCPeerConnectionJS() { + close(); + /* clang-format off */ + EM_ASM({ + Module.IDHandler.remove($0); + }, _js_id); + /* clang-format on */ +}; +#endif diff --git a/modules/webrtc/webrtc_peer_connection_js.h b/modules/webrtc/webrtc_peer_connection_js.h new file mode 100644 index 0000000000..43c0e3d6ee --- /dev/null +++ b/modules/webrtc/webrtc_peer_connection_js.h @@ -0,0 +1,66 @@ +/*************************************************************************/ +/* webrtc_peer_connection_js.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef WEBRTC_PEER_CONNECTION_JS_H +#define WEBRTC_PEER_CONNECTION_JS_H + +#ifdef JAVASCRIPT_ENABLED + +#include "webrtc_peer_connection.h" + +class WebRTCPeerConnectionJS : public WebRTCPeerConnection { + +private: + int _js_id; + ConnectionState _conn_state; + +public: + static WebRTCPeerConnection *_create() { return memnew(WebRTCPeerConnectionJS); } + static void make_default() { WebRTCPeerConnection::_create = WebRTCPeerConnectionJS::_create; } + + void _on_connection_state_changed(); + virtual ConnectionState get_connection_state() const; + + virtual Error initialize(Dictionary configuration = Dictionary()); + virtual Ref create_data_channel(String p_channel_name, Dictionary p_channel_config = Dictionary()); + virtual Error create_offer(); + virtual Error set_remote_description(String type, String sdp); + virtual Error set_local_description(String type, String sdp); + virtual Error add_ice_candidate(String sdpMidName, int sdpMlineIndexName, String sdpName); + virtual Error poll(); + virtual void close(); + + WebRTCPeerConnectionJS(); + ~WebRTCPeerConnectionJS(); +}; + +#endif + +#endif // WEBRTC_PEER_CONNECTION_JS_H diff --git a/modules/webrtc/webrtc_peer_gdnative.cpp b/modules/webrtc/webrtc_peer_gdnative.cpp deleted file mode 100644 index f782944980..0000000000 --- a/modules/webrtc/webrtc_peer_gdnative.cpp +++ /dev/null @@ -1,114 +0,0 @@ -/*************************************************************************/ -/* webrtc_peer_gdnative.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifdef WEBRTC_GDNATIVE_ENABLED - -#include "webrtc_peer_gdnative.h" - -void WebRTCPeerGDNative::_bind_methods() { -} - -WebRTCPeerGDNative::WebRTCPeerGDNative() { - interface = NULL; -} - -WebRTCPeerGDNative::~WebRTCPeerGDNative() { -} - -Error WebRTCPeerGDNative::create_offer() { - ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED); - return (Error)interface->create_offer(interface->data); -} - -Error WebRTCPeerGDNative::set_local_description(String p_type, String p_sdp) { - ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED); - return (Error)interface->set_local_description(interface->data, p_type.utf8().get_data(), p_sdp.utf8().get_data()); -} - -Error WebRTCPeerGDNative::set_remote_description(String p_type, String p_sdp) { - ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED); - return (Error)interface->set_remote_description(interface->data, p_type.utf8().get_data(), p_sdp.utf8().get_data()); -} - -Error WebRTCPeerGDNative::add_ice_candidate(String sdpMidName, int sdpMlineIndexName, String sdpName) { - ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED); - return (Error)interface->add_ice_candidate(interface->data, sdpMidName.utf8().get_data(), sdpMlineIndexName, sdpName.utf8().get_data()); -} - -Error WebRTCPeerGDNative::poll() { - ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED); - return (Error)interface->poll(interface->data); -} - -void WebRTCPeerGDNative::set_write_mode(WriteMode p_mode) { - ERR_FAIL_COND(interface == NULL); - interface->set_write_mode(interface->data, p_mode); -} - -WebRTCPeer::WriteMode WebRTCPeerGDNative::get_write_mode() const { - ERR_FAIL_COND_V(interface == NULL, WRITE_MODE_BINARY); - return (WriteMode)interface->get_write_mode(interface->data); -} - -bool WebRTCPeerGDNative::was_string_packet() const { - ERR_FAIL_COND_V(interface == NULL, false); - return interface->was_string_packet(interface->data); -} - -WebRTCPeer::ConnectionState WebRTCPeerGDNative::get_connection_state() const { - ERR_FAIL_COND_V(interface == NULL, STATE_DISCONNECTED); - return STATE_DISCONNECTED; -} - -Error WebRTCPeerGDNative::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { - ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED); - return (Error)interface->get_packet(interface->data, r_buffer, &r_buffer_size); -} - -Error WebRTCPeerGDNative::put_packet(const uint8_t *p_buffer, int p_buffer_size) { - ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED); - return (Error)interface->put_packet(interface->data, p_buffer, p_buffer_size); -} - -int WebRTCPeerGDNative::get_max_packet_size() const { - ERR_FAIL_COND_V(interface == NULL, 0); - return interface->get_max_packet_size(interface->data); -} - -int WebRTCPeerGDNative::get_available_packet_count() const { - ERR_FAIL_COND_V(interface == NULL, 0); - return interface->get_available_packet_count(interface->data); -} - -void WebRTCPeerGDNative::set_native_webrtc_peer(const godot_net_webrtc_peer *p_impl) { - interface = p_impl; -} - -#endif // WEBRTC_GDNATIVE_ENABLED diff --git a/modules/webrtc/webrtc_peer_gdnative.h b/modules/webrtc/webrtc_peer_gdnative.h deleted file mode 100644 index 6786cec8ea..0000000000 --- a/modules/webrtc/webrtc_peer_gdnative.h +++ /dev/null @@ -1,78 +0,0 @@ -/*************************************************************************/ -/* webrtc_peer_gdnative.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifdef WEBRTC_GDNATIVE_ENABLED - -#ifndef WEBRTC_PEER_GDNATIVE_H -#define WEBRTC_PEER_GDNATIVE_H - -#include "modules/gdnative/include/net/godot_net.h" -#include "webrtc_peer.h" - -class WebRTCPeerGDNative : public WebRTCPeer { - GDCLASS(WebRTCPeerGDNative, WebRTCPeer); - -protected: - static void _bind_methods(); - -private: - const godot_net_webrtc_peer *interface; - -public: - static WebRTCPeer *_create() { return memnew(WebRTCPeerGDNative); } - static void make_default() { WebRTCPeer::_create = WebRTCPeerGDNative::_create; } - - void set_native_webrtc_peer(const godot_net_webrtc_peer *p_impl); - - virtual void set_write_mode(WriteMode mode); - virtual WriteMode get_write_mode() const; - virtual bool was_string_packet() const; - virtual ConnectionState get_connection_state() const; - - virtual Error create_offer(); - virtual Error set_remote_description(String type, String sdp); - virtual Error set_local_description(String type, String sdp); - virtual Error add_ice_candidate(String sdpMidName, int sdpMlineIndexName, String sdpName); - virtual Error poll(); - - /** Inherited from PacketPeer: **/ - virtual int get_available_packet_count() const; - virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size); ///< buffer is GONE after next get_packet - virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size); - - virtual int get_max_packet_size() const; - - WebRTCPeerGDNative(); - ~WebRTCPeerGDNative(); -}; - -#endif // WEBRTC_PEER_GDNATIVE_H - -#endif // WEBRTC_GDNATIVE_ENABLED diff --git a/modules/webrtc/webrtc_peer_js.cpp b/modules/webrtc/webrtc_peer_js.cpp deleted file mode 100644 index 1282e075ab..0000000000 --- a/modules/webrtc/webrtc_peer_js.cpp +++ /dev/null @@ -1,455 +0,0 @@ -/*************************************************************************/ -/* webrtc_peer_js.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifdef JAVASCRIPT_ENABLED - -#include "webrtc_peer_js.h" -#include "emscripten.h" - -extern "C" { -EMSCRIPTEN_KEEPALIVE void _emrtc_on_ice_candidate(void *obj, char *p_MidName, int p_MlineIndexName, char *p_sdpName) { - WebRTCPeerJS *peer = static_cast(obj); - peer->emit_signal("new_ice_candidate", String(p_MidName), p_MlineIndexName, String(p_sdpName)); -} - -EMSCRIPTEN_KEEPALIVE void _emrtc_offer_created(void *obj, char *p_type, char *p_offer) { - WebRTCPeerJS *peer = static_cast(obj); - peer->emit_signal("offer_created", String(p_type), String(p_offer)); -} - -EMSCRIPTEN_KEEPALIVE void _emrtc_on_error(void *obj) { - WebRTCPeerJS *peer = static_cast(obj); - peer->_on_error(); -} - -EMSCRIPTEN_KEEPALIVE void _emrtc_on_open(void *obj) { - WebRTCPeerJS *peer = static_cast(obj); - peer->_on_open(); -} - -EMSCRIPTEN_KEEPALIVE void _emrtc_on_close(void *obj) { - WebRTCPeerJS *peer = static_cast(obj); - peer->_on_close(); -} - -EMSCRIPTEN_KEEPALIVE void _emrtc_on_message(void *obj, uint8_t *p_data, uint32_t p_size, bool p_is_string) { - WebRTCPeerJS *peer = static_cast(obj); - peer->_on_message(p_data, p_size, p_is_string); -} - -EMSCRIPTEN_KEEPALIVE void _emrtc_bind_channel(int p_id) { - /* clang-format off */ - EM_ASM({ - if (!Module.IDHandler.has($0)) { - return; // Godot Object is gone! - } - var dict = Module.IDHandler.get($0); - var channel = dict["channel"]; - var c_ptr = dict["ptr"]; - - channel.onopen = function (evt) { - ccall("_emrtc_on_open", - "void", - ["number"], - [c_ptr] - ); - }; - channel.onclose = function (evt) { - ccall("_emrtc_on_close", - "void", - ["number"], - [c_ptr] - ); - }; - channel.onerror = function (evt) { - ccall("_emrtc_on_error", - "void", - ["number"], - [c_ptr] - ); - }; - - channel.binaryType = "arraybuffer"; - channel.onmessage = function(event) { - var buffer; - var is_string = 0; - if (event.data instanceof ArrayBuffer) { - - buffer = new Uint8Array(event.data); - - } else if (event.data instanceof Blob) { - - alert("Blob type not supported"); - return; - - } else if (typeof event.data === "string") { - - is_string = 1; - var enc = new TextEncoder("utf-8"); - buffer = new Uint8Array(enc.encode(event.data)); - - } else { - - alert("Unknown message type"); - return; - - } - var len = buffer.length*buffer.BYTES_PER_ELEMENT; - var out = Module._malloc(len); - Module.HEAPU8.set(buffer, out); - ccall("_emrtc_on_message", - "void", - ["number", "number", "number", "number"], - [c_ptr, out, len, is_string] - ); - Module._free(out); - } - }, p_id); - /* clang-format on */ -} -} - -void _emrtc_create_pc(int p_id) { - /* clang-format off */ - EM_ASM({ - var dict = Module.IDHandler.get($0); - var c_ptr = dict["ptr"]; - // Setup local connaction - var conn = new RTCPeerConnection(); - conn.onicecandidate = function(event) { - if (!Module.IDHandler.get($0)) return; - if (!event.candidate) return; - - var c = event.candidate; - // should emit on ice candidate - ccall("_emrtc_on_ice_candidate", - "void", - ["number", "string", "number", "string"], - [c_ptr, c.sdpMid, c.sdpMLineIndex, c.candidate] - ); - }; - conn.ondatachannel = function (evt) { - var dict = Module.IDHandler.get($0); - if (!dict || dict["channel"]) { - return; - } - var channel = evt.channel; - dict["channel"] = channel; - ccall("_emrtc_bind_channel", - "void", - ["number"], - [$0] - ); - }; - dict["conn"] = conn; - }, p_id); - /* clang-format on */ -} - -void WebRTCPeerJS::_on_open() { - in_buffer.resize(16); - _conn_state = STATE_CONNECTED; -} - -void WebRTCPeerJS::_on_close() { - close(); -} - -void WebRTCPeerJS::_on_error() { - close(); - _conn_state = STATE_FAILED; -} - -void WebRTCPeerJS::_on_message(uint8_t *p_data, uint32_t p_size, bool p_is_string) { - if (in_buffer.space_left() < p_size + 5) { - ERR_EXPLAIN("Buffer full! Dropping data"); - ERR_FAIL(); - } - - uint8_t is_string = p_is_string ? 1 : 0; - in_buffer.write((uint8_t *)&p_size, 4); - in_buffer.write((uint8_t *)&is_string, 1); - in_buffer.write(p_data, p_size); - queue_count++; -} - -void WebRTCPeerJS::close() { - in_buffer.resize(0); - queue_count = 0; - _was_string = false; - /* clang-format off */ - EM_ASM({ - var dict = Module.IDHandler.get($0); - if (!dict) return; - if (dict["channel"]) { - dict["channel"].close(); - dict["channel"] = null; - } - if (dict["conn"]) { - dict["conn"].close(); - } - }, _js_id); - /* clang-format on */ - _conn_state = STATE_CLOSED; -} - -Error WebRTCPeerJS::create_offer() { - ERR_FAIL_COND_V(_conn_state != STATE_NEW, FAILED); - - _conn_state = STATE_CONNECTING; - /* clang-format off */ - EM_ASM({ - var dict = Module.IDHandler.get($0); - var conn = dict["conn"]; - var c_ptr = dict["ptr"]; - var onError = function(error) { - console.log(error); - ccall("_emrtc_on_error", - "void", - ["number"], - [c_ptr] - ); - }; - var onCreated = function(offer) { - ccall("_emrtc_offer_created", - "void", - ["number", "string", "string"], - [c_ptr, offer.type, offer.sdp] - ); - }; - - var channel = conn.createDataChannel("default"); - dict["channel"] = channel; - ccall("_emrtc_bind_channel", - "void", - ["number"], - [$0] - ); - conn.createOffer().then(onCreated).catch(onError); - }, _js_id); - /* clang-format on */ - return OK; -} - -Error WebRTCPeerJS::set_local_description(String type, String sdp) { - /* clang-format off */ - EM_ASM({ - var dict = Module.IDHandler.get($0); - var conn = dict["conn"]; - var c_ptr = dict["ptr"]; - var type = UTF8ToString($1); - var sdp = UTF8ToString($2); - var onError = function(error) { - console.log(error); - ccall("_emrtc_on_error", - "void", - ["number"], - [c_ptr] - ); - }; - conn.setLocalDescription({ - "sdp": sdp, - "type": type - }).catch(onError); - }, _js_id, type.utf8().get_data(), sdp.utf8().get_data()); - /* clang-format on */ - return OK; -} - -Error WebRTCPeerJS::set_remote_description(String type, String sdp) { - if (type == "offer") { - ERR_FAIL_COND_V(_conn_state != STATE_NEW, FAILED); - _conn_state = STATE_CONNECTING; - } - /* clang-format off */ - EM_ASM({ - var dict = Module.IDHandler.get($0); - var conn = dict["conn"]; - var c_ptr = dict["ptr"]; - var type = UTF8ToString($1); - var sdp = UTF8ToString($2); - - var onError = function(error) { - console.log(error); - ccall("_emrtc_on_error", - "void", - ["number"], - [c_ptr] - ); - }; - var onCreated = function(offer) { - ccall("_emrtc_offer_created", - "void", - ["number", "string", "string"], - [c_ptr, offer.type, offer.sdp] - ); - }; - var onSet = function() { - if (type != "offer") { - return; - } - conn.createAnswer().then(onCreated); - }; - conn.setRemoteDescription({ - "sdp": sdp, - "type": type - }).then(onSet).catch(onError); - }, _js_id, type.utf8().get_data(), sdp.utf8().get_data()); - /* clang-format on */ - return OK; -} - -Error WebRTCPeerJS::add_ice_candidate(String sdpMidName, int sdpMlineIndexName, String sdpName) { - /* clang-format off */ - EM_ASM({ - var dict = Module.IDHandler.get($0); - var conn = dict["conn"]; - var c_ptr = dict["ptr"]; - var sdpMidName = UTF8ToString($1); - var sdpMlineIndexName = UTF8ToString($2); - var sdpName = UTF8ToString($3); - conn.addIceCandidate(new RTCIceCandidate({ - "candidate": sdpName, - "sdpMid": sdpMidName, - "sdpMlineIndex": sdpMlineIndexName - })); - }, _js_id, sdpMidName.utf8().get_data(), sdpMlineIndexName, sdpName.utf8().get_data()); - /* clang-format on */ - return OK; -} - -Error WebRTCPeerJS::poll() { - return OK; -} - -WebRTCPeer::ConnectionState WebRTCPeerJS::get_connection_state() const { - return _conn_state; -} - -int WebRTCPeerJS::get_available_packet_count() const { - return queue_count; -} - -Error WebRTCPeerJS::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { - ERR_FAIL_COND_V(_conn_state != STATE_CONNECTED, ERR_UNCONFIGURED); - - if (queue_count == 0) - return ERR_UNAVAILABLE; - - uint32_t to_read = 0; - uint32_t left = 0; - uint8_t is_string = 0; - r_buffer_size = 0; - - in_buffer.read((uint8_t *)&to_read, 4); - --queue_count; - left = in_buffer.data_left(); - - if (left < to_read + 1) { - in_buffer.advance_read(left); - return FAILED; - } - - in_buffer.read(&is_string, 1); - _was_string = is_string == 1; - in_buffer.read(packet_buffer, to_read); - *r_buffer = packet_buffer; - r_buffer_size = to_read; - - return OK; -} - -Error WebRTCPeerJS::put_packet(const uint8_t *p_buffer, int p_buffer_size) { - ERR_FAIL_COND_V(_conn_state != STATE_CONNECTED, ERR_UNCONFIGURED); - - int is_bin = _write_mode == WebRTCPeer::WRITE_MODE_BINARY ? 1 : 0; - - /* clang-format off */ - EM_ASM({ - var dict = Module.IDHandler.get($0); - var channel = dict["channel"]; - var bytes_array = new Uint8Array($2); - var i = 0; - - for(i=0; i<$2; i++) { - bytes_array[i] = getValue($1+i, 'i8'); - } - - if ($3) { - channel.send(bytes_array.buffer); - } else { - var string = new TextDecoder("utf-8").decode(bytes_array); - channel.send(string); - } - }, _js_id, p_buffer, p_buffer_size, is_bin); - /* clang-format on */ - - return OK; -} - -int WebRTCPeerJS::get_max_packet_size() const { - return 1200; -} - -void WebRTCPeerJS::set_write_mode(WriteMode p_mode) { - _write_mode = p_mode; -} - -WebRTCPeer::WriteMode WebRTCPeerJS::get_write_mode() const { - return _write_mode; -} - -bool WebRTCPeerJS::was_string_packet() const { - return _was_string; -} - -WebRTCPeerJS::WebRTCPeerJS() { - queue_count = 0; - _was_string = false; - _write_mode = WRITE_MODE_BINARY; - _conn_state = STATE_NEW; - - /* clang-format off */ - _js_id = EM_ASM_INT({ - return Module.IDHandler.add({"conn": null, "ptr": $0, "channel": null}); - }, this); - /* clang-format on */ - _emrtc_create_pc(_js_id); -} - -WebRTCPeerJS::~WebRTCPeerJS() { - close(); - /* clang-format off */ - EM_ASM({ - Module.IDHandler.remove($0); - }, _js_id); - /* clang-format on */ -}; -#endif diff --git a/modules/webrtc/webrtc_peer_js.h b/modules/webrtc/webrtc_peer_js.h deleted file mode 100644 index 02f0c9b55d..0000000000 --- a/modules/webrtc/webrtc_peer_js.h +++ /dev/null @@ -1,88 +0,0 @@ -/*************************************************************************/ -/* webrtc_peer_js.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef WEBRTC_PEER_JS_H -#define WEBRTC_PEER_JS_H - -#ifdef JAVASCRIPT_ENABLED - -#include "webrtc_peer.h" - -class WebRTCPeerJS : public WebRTCPeer { - -private: - enum { - PACKET_BUFFER_SIZE = 65536 - 5 // 4 bytes for the size, 1 for for type - }; - - bool _was_string; - WriteMode _write_mode; - - int _js_id; - RingBuffer in_buffer; - int queue_count; - uint8_t packet_buffer[PACKET_BUFFER_SIZE]; - ConnectionState _conn_state; - -public: - static WebRTCPeer *_create() { return memnew(WebRTCPeerJS); } - static void make_default() { WebRTCPeer::_create = WebRTCPeerJS::_create; } - - virtual void set_write_mode(WriteMode mode); - virtual WriteMode get_write_mode() const; - virtual bool was_string_packet() const; - virtual ConnectionState get_connection_state() const; - - virtual Error create_offer(); - virtual Error set_remote_description(String type, String sdp); - virtual Error set_local_description(String type, String sdp); - virtual Error add_ice_candidate(String sdpMidName, int sdpMlineIndexName, String sdpName); - virtual Error poll(); - - /** Inherited from PacketPeer: **/ - virtual int get_available_packet_count() const; - virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size); ///< buffer is GONE after next get_packet - virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size); - - virtual int get_max_packet_size() const; - - void close(); - void _on_open(); - void _on_close(); - void _on_error(); - void _on_message(uint8_t *p_data, uint32_t p_size, bool p_is_string); - - WebRTCPeerJS(); - ~WebRTCPeerJS(); -}; - -#endif - -#endif // WEBRTC_PEER_JS_H -- cgit v1.2.3