diff options
Diffstat (limited to 'modules/webrtc')
18 files changed, 415 insertions, 428 deletions
diff --git a/modules/webrtc/SCsub b/modules/webrtc/SCsub index e6b9959840..e315633f55 100644 --- a/modules/webrtc/SCsub +++ b/modules/webrtc/SCsub @@ -5,7 +5,7 @@ Import("env_modules") env_webrtc = env_modules.Clone() -if env["platform"] == "javascript": +if env["platform"] == "web": # Our JavaScript/C++ interface. env.AddJSLibraries(["library_godot_webrtc.js"]) diff --git a/modules/webrtc/doc_classes/WebRTCDataChannelExtension.xml b/modules/webrtc/doc_classes/WebRTCDataChannelExtension.xml index 5387deaa47..a10ea25b8c 100644 --- a/modules/webrtc/doc_classes/WebRTCDataChannelExtension.xml +++ b/modules/webrtc/doc_classes/WebRTCDataChannelExtension.xml @@ -48,7 +48,7 @@ </description> </method> <method name="_get_packet" qualifiers="virtual"> - <return type="int" /> + <return type="int" enum="Error" /> <param index="0" name="r_buffer" type="const uint8_t **" /> <param index="1" name="r_buffer_size" type="int32_t*" /> <description> @@ -60,12 +60,12 @@ </description> </method> <method name="_get_ready_state" qualifiers="virtual const"> - <return type="int" /> + <return type="int" enum="WebRTCDataChannel.ChannelState" /> <description> </description> </method> <method name="_get_write_mode" qualifiers="virtual const"> - <return type="int" /> + <return type="int" enum="WebRTCDataChannel.WriteMode" /> <description> </description> </method> @@ -80,12 +80,12 @@ </description> </method> <method name="_poll" qualifiers="virtual"> - <return type="int" /> + <return type="int" enum="Error" /> <description> </description> </method> <method name="_put_packet" qualifiers="virtual"> - <return type="int" /> + <return type="int" enum="Error" /> <param index="0" name="p_buffer" type="const uint8_t*" /> <param index="1" name="p_buffer_size" type="int" /> <description> @@ -93,7 +93,7 @@ </method> <method name="_set_write_mode" qualifiers="virtual"> <return type="void" /> - <param index="0" name="p_write_mode" type="int" /> + <param index="0" name="p_write_mode" type="int" enum="WebRTCDataChannel.WriteMode" /> <description> </description> </method> diff --git a/modules/webrtc/doc_classes/WebRTCMultiplayerPeer.xml b/modules/webrtc/doc_classes/WebRTCMultiplayerPeer.xml index 927888fe21..5266a36637 100644 --- a/modules/webrtc/doc_classes/WebRTCMultiplayerPeer.xml +++ b/modules/webrtc/doc_classes/WebRTCMultiplayerPeer.xml @@ -6,7 +6,7 @@ <description> This class constructs a full mesh of [WebRTCPeerConnection] (one connection for each peer) that can be used as a [member MultiplayerAPI.multiplayer_peer]. You can add each [WebRTCPeerConnection] via [method add_peer] or remove them via [method remove_peer]. Peers must be added in [constant WebRTCPeerConnection.STATE_NEW] state to allow it to create the appropriate channels. This class will not create offers nor set descriptions, it will only poll them, and notify connections and disconnections. - [signal MultiplayerPeer.connection_succeeded] and [signal MultiplayerPeer.server_disconnected] will not be emitted unless [code]server_compatibility[/code] is [code]true[/code] in [method initialize]. Beside that data transfer works like in a [MultiplayerPeer]. + When creating the peer via [method create_client] or [method create_server] the [method MultiplayerPeer.is_server_relay_supported] method will return [code]true[/code] enabling peer exchange and packet relaying when supported by the [MultiplayerAPI] implementation. [b]Note:[/b] When exporting to Android, make sure to enable the [code]INTERNET[/code] permission in the Android export preset before exporting the project or using one-click deploy. Otherwise, network communication of any kind will be blocked by Android. </description> <tutorials> @@ -22,10 +22,29 @@ Three channels will be created for reliable, unreliable, and ordered transport. The value of [code]unreliable_lifetime[/code] will be passed to the [code]maxPacketLifetime[/code] option when creating unreliable and ordered channels (see [method WebRTCPeerConnection.create_data_channel]). </description> </method> - <method name="close"> - <return type="void" /> + <method name="create_client"> + <return type="int" enum="Error" /> + <param index="0" name="peer_id" type="int" /> + <param index="1" name="channels_config" type="Array" default="[]" /> + <description> + Initialize the multiplayer peer as a client with the given [code]peer_id[/code] (must be between 2 and 2147483647). In this mode, you should only call [method add_peer] once and with [code]peer_id[/code] of [code]1[/code]. This mode enables [method MultiplayerPeer.is_server_relay_supported], allowing the upper [MultiplayerAPI] layer to perform peer exchange and packet relaying. + You can optionally specify a [code]channels_config[/code] array of [enum MultiplayerPeer.TransferMode] which will be used to create extra channels (WebRTC only supports one transfer mode per channel). + </description> + </method> + <method name="create_mesh"> + <return type="int" enum="Error" /> + <param index="0" name="peer_id" type="int" /> + <param index="1" name="channels_config" type="Array" default="[]" /> + <description> + Initialize the multiplayer peer as a mesh (i.e. all peers connect to each other) with the given [code]peer_id[/code] (must be between 1 and 2147483647). + </description> + </method> + <method name="create_server"> + <return type="int" enum="Error" /> + <param index="0" name="channels_config" type="Array" default="[]" /> <description> - Close all the add peer connections and channels, freeing all resources. + Initialize the multiplayer peer as a server (with unique ID of [code]1[/code]). This mode enables [method MultiplayerPeer.is_server_relay_supported], allowing the upper [MultiplayerAPI] layer to perform peer exchange and packet relaying. + You can optionally specify a [code]channels_config[/code] array of [enum MultiplayerPeer.TransferMode] which will be used to create extra channels (WebRTC only supports one transfer mode per channel). </description> </method> <method name="get_peer"> @@ -48,18 +67,6 @@ Returns [code]true[/code] if the given [code]peer_id[/code] is in the peers map (it might not be connected though). </description> </method> - <method name="initialize"> - <return type="int" enum="Error" /> - <param index="0" name="peer_id" type="int" /> - <param index="1" name="server_compatibility" type="bool" default="false" /> - <param index="2" name="channels_config" type="Array" default="[]" /> - <description> - Initialize the multiplayer peer with the given [code]peer_id[/code] (must be between 1 and 2147483647). - If [code]server_compatibilty[/code] is [code]false[/code] (default), the multiplayer peer will be immediately in state [constant MultiplayerPeer.CONNECTION_CONNECTED] and [signal MultiplayerPeer.connection_succeeded] will not be emitted. - If [code]server_compatibilty[/code] is [code]true[/code] the peer will suppress all [signal MultiplayerPeer.peer_connected] signals until a peer with id [constant MultiplayerPeer.TARGET_PEER_SERVER] connects and then emit [signal MultiplayerPeer.connection_succeeded]. After that the signal [signal MultiplayerPeer.peer_connected] will be emitted for every already connected peer, and any new peer that might connect. If the server peer disconnects after that, signal [signal MultiplayerPeer.server_disconnected] will be emitted and state will become [constant MultiplayerPeer.CONNECTION_CONNECTED]. - You can optionally specify a [code]channels_config[/code] array of [enum MultiplayerPeer.TransferMode] which will be used to create extra channels (WebRTC only supports one transfer mode per channel). - </description> - </method> <method name="remove_peer"> <return type="void" /> <param index="0" name="peer_id" type="int" /> diff --git a/modules/webrtc/doc_classes/WebRTCPeerConnection.xml b/modules/webrtc/doc_classes/WebRTCPeerConnection.xml index e99aeb4f51..4ecc71ddbb 100644 --- a/modules/webrtc/doc_classes/WebRTCPeerConnection.xml +++ b/modules/webrtc/doc_classes/WebRTCPeerConnection.xml @@ -67,6 +67,18 @@ Returns the connection state. See [enum ConnectionState]. </description> </method> + <method name="get_gathering_state" qualifiers="const"> + <return type="int" enum="WebRTCPeerConnection.GatheringState" /> + <description> + Returns the ICE [enum GatheringState] of the connection. This lets you detect, for example, when collection of ICE candidates has finished. + </description> + </method> + <method name="get_signaling_state" qualifiers="const"> + <return type="int" enum="WebRTCPeerConnection.SignalingState" /> + <description> + Returns the [enum SignalingState] on the local end of the connection while connecting or reconnecting to another peer. + </description> + </method> <method name="initialize"> <return type="int" enum="Error" /> <param index="0" name="configuration" type="Dictionary" default="{}" /> @@ -165,5 +177,32 @@ <constant name="STATE_CLOSED" value="5" enum="ConnectionState"> The peer connection is closed (after calling [method close] for example). </constant> + <constant name="GATHERING_STATE_NEW" value="0" enum="GatheringState"> + The peer connection was just created and hasn't done any networking yet. + </constant> + <constant name="GATHERING_STATE_GATHERING" value="1" enum="GatheringState"> + The ICE agent is in the process of gathering candidates for the connection. + </constant> + <constant name="GATHERING_STATE_COMPLETE" value="2" enum="GatheringState"> + The ICE agent has finished gathering candidates. If something happens that requires collecting new candidates, such as a new interface being added or the addition of a new ICE server, the state will revert to gathering to gather those candidates. + </constant> + <constant name="SIGNALING_STATE_STABLE" value="0" enum="SignalingState"> + There is no ongoing exchange of offer and answer underway. This may mean that the [WebRTCPeerConnection] is new ([constant STATE_NEW]) or that negotiation is complete and a connection has been established ([constant STATE_CONNECTED]). + </constant> + <constant name="SIGNALING_STATE_HAVE_LOCAL_OFFER" value="1" enum="SignalingState"> + The local peer has called [method set_local_description], passing in SDP representing an offer (usually created by calling [method create_offer]), and the offer has been applied successfully. + </constant> + <constant name="SIGNALING_STATE_HAVE_REMOTE_OFFER" value="2" enum="SignalingState"> + The remote peer has created an offer and used the signaling server to deliver it to the local peer, which has set the offer as the remote description by calling [method set_remote_description]. + </constant> + <constant name="SIGNALING_STATE_HAVE_LOCAL_PRANSWER" value="3" enum="SignalingState"> + The offer sent by the remote peer has been applied and an answer has been created and applied by calling [method set_local_description]. This provisional answer describes the supported media formats and so forth, but may not have a complete set of ICE candidates included. Further candidates will be delivered separately later. + </constant> + <constant name="SIGNALING_STATE_HAVE_REMOTE_PRANSWER" value="4" enum="SignalingState"> + A provisional answer has been received and successfully applied in response to an offer previously sent and established by calling [method set_local_description]. + </constant> + <constant name="SIGNALING_STATE_CLOSED" value="5" enum="SignalingState"> + The [WebRTCPeerConnection] has been closed. + </constant> </constants> </class> diff --git a/modules/webrtc/doc_classes/WebRTCPeerConnectionExtension.xml b/modules/webrtc/doc_classes/WebRTCPeerConnectionExtension.xml index e22e939a66..474d2f6a89 100644 --- a/modules/webrtc/doc_classes/WebRTCPeerConnectionExtension.xml +++ b/modules/webrtc/doc_classes/WebRTCPeerConnectionExtension.xml @@ -8,7 +8,7 @@ </tutorials> <methods> <method name="_add_ice_candidate" qualifiers="virtual"> - <return type="int" /> + <return type="int" enum="Error" /> <param index="0" name="p_sdp_mid_name" type="String" /> <param index="1" name="p_sdp_mline_index" type="int" /> <param index="2" name="p_sdp_name" type="String" /> @@ -28,35 +28,45 @@ </description> </method> <method name="_create_offer" qualifiers="virtual"> - <return type="int" /> + <return type="int" enum="Error" /> <description> </description> </method> <method name="_get_connection_state" qualifiers="virtual const"> - <return type="int" /> + <return type="int" enum="WebRTCPeerConnection.ConnectionState" /> + <description> + </description> + </method> + <method name="_get_gathering_state" qualifiers="virtual const"> + <return type="int" enum="WebRTCPeerConnection.GatheringState" /> + <description> + </description> + </method> + <method name="_get_signaling_state" qualifiers="virtual const"> + <return type="int" enum="WebRTCPeerConnection.SignalingState" /> <description> </description> </method> <method name="_initialize" qualifiers="virtual"> - <return type="int" /> + <return type="int" enum="Error" /> <param index="0" name="p_config" type="Dictionary" /> <description> </description> </method> <method name="_poll" qualifiers="virtual"> - <return type="int" /> + <return type="int" enum="Error" /> <description> </description> </method> <method name="_set_local_description" qualifiers="virtual"> - <return type="int" /> + <return type="int" enum="Error" /> <param index="0" name="p_type" type="String" /> <param index="1" name="p_sdp" type="String" /> <description> </description> </method> <method name="_set_remote_description" qualifiers="virtual"> - <return type="int" /> + <return type="int" enum="Error" /> <param index="0" name="p_type" type="String" /> <param index="1" name="p_sdp" type="String" /> <description> diff --git a/modules/webrtc/library_godot_webrtc.js b/modules/webrtc/library_godot_webrtc.js index e57e4299e0..e6604eecd7 100644 --- a/modules/webrtc/library_godot_webrtc.js +++ b/modules/webrtc/library_godot_webrtc.js @@ -220,64 +220,123 @@ mergeInto(LibraryManager.library, GodotRTCDataChannel); const GodotRTCPeerConnection = { $GodotRTCPeerConnection__deps: ['$IDHandler', '$GodotRuntime', '$GodotRTCDataChannel'], $GodotRTCPeerConnection: { - onstatechange: function (p_id, p_conn, callback, event) { - const ref = IDHandler.get(p_id); - if (!ref) { - return; - } - let state; - switch (p_conn.iceConnectionState) { - case 'new': - state = 0; - break; - case 'checking': - state = 1; - break; - case 'connected': - case 'completed': - state = 2; - break; - case 'disconnected': - state = 3; - break; - case 'failed': - state = 4; - break; - case 'closed': - default: - state = 5; - break; - } - callback(state); + // Enums + ConnectionState: { + 'new': 0, + 'connecting': 1, + 'connected': 2, + 'disconnected': 3, + 'failed': 4, + 'closed': 5, }, - onicecandidate: function (p_id, callback, event) { - const ref = IDHandler.get(p_id); - if (!ref || !event.candidate) { - return; + ConnectionStateCompat: { + // Using values from IceConnectionState for browsers that do not support ConnectionState (notably Firefox). + 'new': 0, + 'checking': 1, + 'connected': 2, + 'completed': 2, + 'disconnected': 3, + 'failed': 4, + 'closed': 5, + }, + + IceGatheringState: { + 'new': 0, + 'gathering': 1, + 'complete': 2, + }, + + SignalingState: { + 'stable': 0, + 'have-local-offer': 1, + 'have-remote-offer': 2, + 'have-local-pranswer': 3, + 'have-remote-pranswer': 4, + 'closed': 5, + }, + + // Callbacks + create: function (config, onConnectionChange, onSignalingChange, onIceGatheringChange, onIceCandidate, onDataChannel) { + let conn = null; + try { + conn = new RTCPeerConnection(config); + } catch (e) { + GodotRuntime.error(e); + return 0; } - const c = event.candidate; - const candidate_str = GodotRuntime.allocString(c.candidate); - const mid_str = GodotRuntime.allocString(c.sdpMid); - callback(mid_str, c.sdpMLineIndex, candidate_str); - GodotRuntime.free(candidate_str); - GodotRuntime.free(mid_str); + const id = IDHandler.add(conn); + + if ('connectionState' in conn && conn['connectionState'] !== undefined) { + // Use "connectionState" if supported + conn.onconnectionstatechange = function (event) { + if (!IDHandler.get(id)) { + return; + } + onConnectionChange(GodotRTCPeerConnection.ConnectionState[conn.connectionState] || 0); + }; + } else { + // Fall back to using "iceConnectionState" when "connectionState" is not supported (notably Firefox). + conn.oniceconnectionstatechange = function (event) { + if (!IDHandler.get(id)) { + return; + } + onConnectionChange(GodotRTCPeerConnection.ConnectionStateCompat[conn.iceConnectionState] || 0); + }; + } + conn.onicegatheringstatechange = function (event) { + if (!IDHandler.get(id)) { + return; + } + onIceGatheringChange(GodotRTCPeerConnection.IceGatheringState[conn.iceGatheringState] || 0); + }; + conn.onsignalingstatechange = function (event) { + if (!IDHandler.get(id)) { + return; + } + onSignalingChange(GodotRTCPeerConnection.SignalingState[conn.signalingState] || 0); + }; + conn.onicecandidate = function (event) { + if (!IDHandler.get(id)) { + return; + } + const c = event.candidate; + if (!c || !c.candidate) { + return; + } + const candidate_str = GodotRuntime.allocString(c.candidate); + const mid_str = GodotRuntime.allocString(c.sdpMid); + onIceCandidate(mid_str, c.sdpMLineIndex, candidate_str); + GodotRuntime.free(candidate_str); + GodotRuntime.free(mid_str); + }; + conn.ondatachannel = function (event) { + if (!IDHandler.get(id)) { + return; + } + const cid = IDHandler.add(event.channel); + onDataChannel(cid); + }; + return id; }, - ondatachannel: function (p_id, callback, event) { - const ref = IDHandler.get(p_id); - if (!ref) { + destroy: function (p_id) { + const conn = IDHandler.get(p_id); + if (!conn) { return; } - - const cid = IDHandler.add(event.channel); - callback(cid); + conn.onconnectionstatechange = null; + conn.oniceconnectionstatechange = null; + conn.onicegatheringstatechange = null; + conn.onsignalingstatechange = null; + conn.onicecandidate = null; + conn.ondatachannel = null; + IDHandler.remove(p_id); }, onsession: function (p_id, callback, session) { - const ref = IDHandler.get(p_id); - if (!ref) { + if (!IDHandler.get(p_id)) { return; } const type_str = GodotRuntime.allocString(session.type); @@ -297,27 +356,19 @@ const GodotRTCPeerConnection = { }, }, - godot_js_rtc_pc_create__sig: 'iiiiii', - godot_js_rtc_pc_create: function (p_config, p_ref, p_on_state_change, p_on_candidate, p_on_datachannel) { - const onstatechange = GodotRuntime.get_func(p_on_state_change).bind(null, p_ref); - const oncandidate = GodotRuntime.get_func(p_on_candidate).bind(null, p_ref); - const ondatachannel = GodotRuntime.get_func(p_on_datachannel).bind(null, p_ref); - - const config = JSON.parse(GodotRuntime.parseString(p_config)); - let conn = null; - try { - conn = new RTCPeerConnection(config); - } catch (e) { - GodotRuntime.error(e); - return 0; - } - - const base = GodotRTCPeerConnection; - const id = IDHandler.add(conn); - conn.oniceconnectionstatechange = base.onstatechange.bind(null, id, conn, onstatechange); - conn.onicecandidate = base.onicecandidate.bind(null, id, oncandidate); - conn.ondatachannel = base.ondatachannel.bind(null, id, ondatachannel); - return id; + godot_js_rtc_pc_create__sig: 'iiiiiiii', + godot_js_rtc_pc_create: function (p_config, p_ref, p_on_connection_state_change, p_on_ice_gathering_state_change, p_on_signaling_state_change, p_on_ice_candidate, p_on_datachannel) { + const wrap = function (p_func) { + return GodotRuntime.get_func(p_func).bind(null, p_ref); + }; + return GodotRTCPeerConnection.create( + JSON.parse(GodotRuntime.parseString(p_config)), + wrap(p_on_connection_state_change), + wrap(p_on_signaling_state_change), + wrap(p_on_ice_gathering_state_change), + wrap(p_on_ice_candidate), + wrap(p_on_datachannel) + ); }, godot_js_rtc_pc_close__sig: 'vi', @@ -331,14 +382,7 @@ const GodotRTCPeerConnection = { godot_js_rtc_pc_destroy__sig: 'vi', godot_js_rtc_pc_destroy: function (p_id) { - const ref = IDHandler.get(p_id); - if (!ref) { - return; - } - ref.oniceconnectionstatechange = null; - ref.onicecandidate = null; - ref.ondatachannel = null; - IDHandler.remove(p_id); + GodotRTCPeerConnection.destroy(p_id); }, godot_js_rtc_pc_offer_create__sig: 'viiii', diff --git a/modules/webrtc/webrtc_data_channel_extension.cpp b/modules/webrtc/webrtc_data_channel_extension.cpp index b7ea8d22bb..4e16b77e81 100644 --- a/modules/webrtc/webrtc_data_channel_extension.cpp +++ b/modules/webrtc/webrtc_data_channel_extension.cpp @@ -56,160 +56,20 @@ void WebRTCDataChannelExtension::_bind_methods() { GDVIRTUAL_BIND(_get_buffered_amount); } -int WebRTCDataChannelExtension::get_available_packet_count() const { - int count; - if (GDVIRTUAL_CALL(_get_available_packet_count, count)) { - return count; - } - WARN_PRINT_ONCE("WebRTCDataChannelExtension::_get_available_packet_count is unimplemented!"); - return -1; -} - Error WebRTCDataChannelExtension::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { - int err; + Error err; if (GDVIRTUAL_CALL(_get_packet, r_buffer, &r_buffer_size, err)) { - return (Error)err; + return err; } WARN_PRINT_ONCE("WebRTCDataChannelExtension::_get_packet_native is unimplemented!"); return FAILED; } Error WebRTCDataChannelExtension::put_packet(const uint8_t *p_buffer, int p_buffer_size) { - int err; + Error err; if (GDVIRTUAL_CALL(_put_packet, p_buffer, p_buffer_size, err)) { - return (Error)err; + return err; } WARN_PRINT_ONCE("WebRTCDataChannelExtension::_put_packet_native is unimplemented!"); return FAILED; } - -int WebRTCDataChannelExtension::get_max_packet_size() const { - int size; - if (GDVIRTUAL_CALL(_get_max_packet_size, size)) { - return size; - } - WARN_PRINT_ONCE("WebRTCDataChannelExtension::_get_max_packet_size is unimplemented!"); - return 0; -} - -Error WebRTCDataChannelExtension::poll() { - int err; - if (GDVIRTUAL_CALL(_poll, err)) { - return (Error)err; - } - WARN_PRINT_ONCE("WebRTCDataChannelExtension::_poll is unimplemented!"); - return ERR_UNCONFIGURED; -} - -void WebRTCDataChannelExtension::close() { - if (GDVIRTUAL_CALL(_close)) { - return; - } - WARN_PRINT_ONCE("WebRTCDataChannelExtension::_close is unimplemented!"); -} - -void WebRTCDataChannelExtension::set_write_mode(WriteMode p_mode) { - if (GDVIRTUAL_CALL(_set_write_mode, p_mode)) { - return; - } - WARN_PRINT_ONCE("WebRTCDataChannelExtension::_set_write_mode is unimplemented!"); -} - -WebRTCDataChannel::WriteMode WebRTCDataChannelExtension::get_write_mode() const { - int mode; - if (GDVIRTUAL_CALL(_get_write_mode, mode)) { - return (WriteMode)mode; - } - WARN_PRINT_ONCE("WebRTCDataChannelExtension::_get_write_mode is unimplemented!"); - return WRITE_MODE_BINARY; -} - -bool WebRTCDataChannelExtension::was_string_packet() const { - bool was_string; - if (GDVIRTUAL_CALL(_was_string_packet, was_string)) { - return was_string; - } - WARN_PRINT_ONCE("WebRTCDataChannelExtension::_was_string_packet is unimplemented!"); - return false; -} - -WebRTCDataChannel::ChannelState WebRTCDataChannelExtension::get_ready_state() const { - int state; - if (GDVIRTUAL_CALL(_get_ready_state, state)) { - return (ChannelState)state; - } - WARN_PRINT_ONCE("WebRTCDataChannelExtension::_get_ready_state is unimplemented!"); - return STATE_CLOSED; -} - -String WebRTCDataChannelExtension::get_label() const { - String label; - if (GDVIRTUAL_CALL(_get_label, label)) { - return label; - } - WARN_PRINT_ONCE("WebRTCDataChannelExtension::_get_label is unimplemented!"); - return label; -} - -bool WebRTCDataChannelExtension::is_ordered() const { - bool ordered; - if (GDVIRTUAL_CALL(_is_ordered, ordered)) { - return ordered; - } - WARN_PRINT_ONCE("WebRTCDataChannelExtension::_is_ordered is unimplemented!"); - return false; -} - -int WebRTCDataChannelExtension::get_id() const { - int id; - if (GDVIRTUAL_CALL(_get_id, id)) { - return id; - } - WARN_PRINT_ONCE("WebRTCDataChannelExtension::_get_id is unimplemented!"); - return -1; -} - -int WebRTCDataChannelExtension::get_max_packet_life_time() const { - int lifetime; - if (GDVIRTUAL_CALL(_get_max_packet_life_time, lifetime)) { - return lifetime; - } - WARN_PRINT_ONCE("WebRTCDataChannelExtension::_get_max_packet_life_time is unimplemented!"); - return -1; -} - -int WebRTCDataChannelExtension::get_max_retransmits() const { - int retransmits; - if (GDVIRTUAL_CALL(_get_max_retransmits, retransmits)) { - return retransmits; - } - WARN_PRINT_ONCE("WebRTCDataChannelExtension::_get_max_retransmits is unimplemented!"); - return -1; -} - -String WebRTCDataChannelExtension::get_protocol() const { - String protocol; - if (GDVIRTUAL_CALL(_get_protocol, protocol)) { - return protocol; - } - WARN_PRINT_ONCE("WebRTCDataChannelExtension::_get_protocol is unimplemented!"); - return protocol; -} - -bool WebRTCDataChannelExtension::is_negotiated() const { - bool negotiated; - if (GDVIRTUAL_CALL(_is_negotiated, negotiated)) { - return negotiated; - } - WARN_PRINT_ONCE("WebRTCDataChannelExtension::_is_negotiated is unimplemented!"); - return false; -} - -int WebRTCDataChannelExtension::get_buffered_amount() const { - int amount; - if (GDVIRTUAL_CALL(_get_buffered_amount, amount)) { - return amount; - } - WARN_PRINT_ONCE("WebRTCDataChannelExtension::_get_buffered_amount is unimplemented!"); - return -1; -} diff --git a/modules/webrtc/webrtc_data_channel_extension.h b/modules/webrtc/webrtc_data_channel_extension.h index 83bb627815..467163ed93 100644 --- a/modules/webrtc/webrtc_data_channel_extension.h +++ b/modules/webrtc/webrtc_data_channel_extension.h @@ -33,6 +33,7 @@ #include "webrtc_data_channel.h" +#include "core/extension/ext_wrappers.gen.inc" #include "core/object/gdvirtual.gen.inc" #include "core/object/script_language.h" #include "core/variant/native_ptr.h" @@ -44,53 +45,33 @@ protected: static void _bind_methods(); public: - virtual void set_write_mode(WriteMode mode) override; - virtual WriteMode get_write_mode() const override; - virtual bool was_string_packet() const override; + EXBIND0R(Error, poll); + EXBIND0(close); - virtual ChannelState get_ready_state() const override; - virtual String get_label() const override; - virtual bool is_ordered() const override; - virtual int get_id() const override; - virtual int get_max_packet_life_time() const override; - virtual int get_max_retransmits() const override; - virtual String get_protocol() const override; - virtual bool is_negotiated() const override; - virtual int get_buffered_amount() const override; + EXBIND1(set_write_mode, WriteMode); + EXBIND0RC(WriteMode, get_write_mode); - virtual Error poll() override; - virtual void close() override; + EXBIND0RC(bool, was_string_packet); + + EXBIND0RC(ChannelState, get_ready_state); + EXBIND0RC(String, get_label); + EXBIND0RC(bool, is_ordered); + EXBIND0RC(int, get_id); + EXBIND0RC(int, get_max_packet_life_time); + EXBIND0RC(int, get_max_retransmits); + EXBIND0RC(String, get_protocol); + EXBIND0RC(bool, is_negotiated); + EXBIND0RC(int, get_buffered_amount); /** Inherited from PacketPeer: **/ - virtual int get_available_packet_count() const override; + EXBIND0RC(int, get_available_packet_count); + EXBIND0RC(int, get_max_packet_size); virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; ///< buffer is GONE after next get_packet virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override; - virtual int get_max_packet_size() const override; - /** GDExtension **/ - GDVIRTUAL0RC(int, _get_available_packet_count); - GDVIRTUAL2R(int, _get_packet, GDNativeConstPtr<const uint8_t *>, GDNativePtr<int>); - GDVIRTUAL2R(int, _put_packet, GDNativeConstPtr<const uint8_t>, int); - GDVIRTUAL0RC(int, _get_max_packet_size); - - GDVIRTUAL0R(int, _poll); - GDVIRTUAL0(_close); - - GDVIRTUAL1(_set_write_mode, int); - GDVIRTUAL0RC(int, _get_write_mode); - - GDVIRTUAL0RC(bool, _was_string_packet); - - GDVIRTUAL0RC(int, _get_ready_state); - GDVIRTUAL0RC(String, _get_label); - GDVIRTUAL0RC(bool, _is_ordered); - GDVIRTUAL0RC(int, _get_id); - GDVIRTUAL0RC(int, _get_max_packet_life_time); - GDVIRTUAL0RC(int, _get_max_retransmits); - GDVIRTUAL0RC(String, _get_protocol); - GDVIRTUAL0RC(bool, _is_negotiated); - GDVIRTUAL0RC(int, _get_buffered_amount); + GDVIRTUAL2R(Error, _get_packet, GDNativeConstPtr<const uint8_t *>, GDNativePtr<int>); + GDVIRTUAL2R(Error, _put_packet, GDNativeConstPtr<const uint8_t>, int); WebRTCDataChannelExtension() {} }; diff --git a/modules/webrtc/webrtc_data_channel_js.cpp b/modules/webrtc/webrtc_data_channel_js.cpp index 0fb074b0c2..232f6998d3 100644 --- a/modules/webrtc/webrtc_data_channel_js.cpp +++ b/modules/webrtc/webrtc_data_channel_js.cpp @@ -28,7 +28,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifdef JAVASCRIPT_ENABLED +#ifdef WEB_ENABLED #include "webrtc_data_channel_js.h" diff --git a/modules/webrtc/webrtc_data_channel_js.h b/modules/webrtc/webrtc_data_channel_js.h index d059ec31ed..0caa76885a 100644 --- a/modules/webrtc/webrtc_data_channel_js.h +++ b/modules/webrtc/webrtc_data_channel_js.h @@ -31,7 +31,7 @@ #ifndef WEBRTC_DATA_CHANNEL_JS_H #define WEBRTC_DATA_CHANNEL_JS_H -#ifdef JAVASCRIPT_ENABLED +#ifdef WEB_ENABLED #include "webrtc_data_channel.h" @@ -89,6 +89,6 @@ public: ~WebRTCDataChannelJS(); }; -#endif // JAVASCRIPT_ENABLED +#endif // WEB_ENABLED #endif // WEBRTC_DATA_CHANNEL_JS_H diff --git a/modules/webrtc/webrtc_multiplayer_peer.cpp b/modules/webrtc/webrtc_multiplayer_peer.cpp index e03b6b2473..38c33a2dbc 100644 --- a/modules/webrtc/webrtc_multiplayer_peer.cpp +++ b/modules/webrtc/webrtc_multiplayer_peer.cpp @@ -34,13 +34,14 @@ #include "core/os/os.h" void WebRTCMultiplayerPeer::_bind_methods() { - ClassDB::bind_method(D_METHOD("initialize", "peer_id", "server_compatibility", "channels_config"), &WebRTCMultiplayerPeer::initialize, DEFVAL(false), DEFVAL(Array())); + ClassDB::bind_method(D_METHOD("create_server", "channels_config"), &WebRTCMultiplayerPeer::create_server, DEFVAL(Array())); + ClassDB::bind_method(D_METHOD("create_client", "peer_id", "channels_config"), &WebRTCMultiplayerPeer::create_client, DEFVAL(Array())); + ClassDB::bind_method(D_METHOD("create_mesh", "peer_id", "channels_config"), &WebRTCMultiplayerPeer::create_mesh, DEFVAL(Array())); ClassDB::bind_method(D_METHOD("add_peer", "peer", "peer_id", "unreliable_lifetime"), &WebRTCMultiplayerPeer::add_peer, DEFVAL(1)); ClassDB::bind_method(D_METHOD("remove_peer", "peer_id"), &WebRTCMultiplayerPeer::remove_peer); ClassDB::bind_method(D_METHOD("has_peer", "peer_id"), &WebRTCMultiplayerPeer::has_peer); ClassDB::bind_method(D_METHOD("get_peer", "peer_id"), &WebRTCMultiplayerPeer::get_peer); ClassDB::bind_method(D_METHOD("get_peers"), &WebRTCMultiplayerPeer::get_peers); - ClassDB::bind_method(D_METHOD("close"), &WebRTCMultiplayerPeer::close); } void WebRTCMultiplayerPeer::set_target_peer(int p_peer_id) { @@ -52,6 +53,15 @@ int WebRTCMultiplayerPeer::get_packet_peer() const { return next_packet_peer; } +int WebRTCMultiplayerPeer::get_packet_channel() const { + return next_packet_channel < CH_RESERVED_MAX ? 0 : next_packet_channel - CH_RESERVED_MAX + 1; +} + +MultiplayerPeer::TransferMode WebRTCMultiplayerPeer::get_packet_mode() const { + ERR_FAIL_INDEX_V(next_packet_channel, channels_modes.size(), TRANSFER_MODE_RELIABLE); + return channels_modes[next_packet_channel]; +} + bool WebRTCMultiplayerPeer::is_server() const { return unique_id == TARGET_PEER_SERVER; } @@ -113,24 +123,14 @@ void WebRTCMultiplayerPeer::poll() { // Signal newly connected peers for (int &E : add) { // Already connected to server: simply notify new peer. - // NOTE: Mesh is always connected. - if (connection_status == CONNECTION_CONNECTED) { - emit_signal(SNAME("peer_connected"), E); - } - - // Server emulation mode suppresses peer_conencted until server connects. - if (server_compat && E == TARGET_PEER_SERVER) { + if (network_mode == MODE_CLIENT) { + ERR_CONTINUE(E != TARGET_PEER_SERVER); // Bug. // Server connected. connection_status = CONNECTION_CONNECTED; emit_signal(SNAME("peer_connected"), TARGET_PEER_SERVER); emit_signal(SNAME("connection_succeeded")); - // Notify of all previously connected peers - for (const KeyValue<int, Ref<ConnectedPeer>> &F : peer_map) { - if (F.key != 1 && F.value->connected) { - emit_signal(SNAME("peer_connected"), F.key); - } - } - break; // Because we already notified of all newly added peers. + } else { + emit_signal(SNAME("peer_connected"), E); } } // Fetch next packet @@ -150,11 +150,14 @@ void WebRTCMultiplayerPeer::_find_next_peer() { ++E; continue; } + int idx = 0; for (const Ref<WebRTCDataChannel> &F : E->value->channels) { if (F->get_available_packet_count()) { + next_packet_channel = idx; next_packet_peer = E->key; return; } + idx++; } ++E; } @@ -165,11 +168,14 @@ void WebRTCMultiplayerPeer::_find_next_peer() { ++E; continue; } + int idx = 0; for (const Ref<WebRTCDataChannel> &F : E->value->channels) { if (F->get_available_packet_count()) { + next_packet_channel = idx; next_packet_peer = E->key; return; } + idx++; } if (E->key == (int)next_packet_peer) { break; @@ -177,6 +183,7 @@ void WebRTCMultiplayerPeer::_find_next_peer() { ++E; } // No packet found + next_packet_channel = 0; next_packet_peer = 0; } @@ -184,11 +191,28 @@ MultiplayerPeer::ConnectionStatus WebRTCMultiplayerPeer::get_connection_status() return connection_status; } -Error WebRTCMultiplayerPeer::initialize(int p_self_id, bool p_server_compat, Array p_channels_config) { +Error WebRTCMultiplayerPeer::create_server(Array p_channels_config) { + return _initialize(1, MODE_SERVER, p_channels_config); +} + +Error WebRTCMultiplayerPeer::create_client(int p_self_id, Array p_channels_config) { + ERR_FAIL_COND_V_MSG(p_self_id == 1, ERR_INVALID_PARAMETER, "Clients cannot have ID 1."); + return _initialize(p_self_id, MODE_CLIENT, p_channels_config); +} + +Error WebRTCMultiplayerPeer::create_mesh(int p_self_id, Array p_channels_config) { + return _initialize(p_self_id, MODE_MESH, p_channels_config); +} + +Error WebRTCMultiplayerPeer::_initialize(int p_self_id, NetworkMode p_mode, Array p_channels_config) { ERR_FAIL_COND_V(p_self_id < 1 || p_self_id > ~(1 << 31), ERR_INVALID_PARAMETER); channels_config.clear(); + channels_modes.clear(); + channels_modes.push_back(TRANSFER_MODE_RELIABLE); + channels_modes.push_back(TRANSFER_MODE_UNRELIABLE_ORDERED); + channels_modes.push_back(TRANSFER_MODE_UNRELIABLE); for (int i = 0; i < p_channels_config.size(); i++) { - ERR_FAIL_COND_V_MSG(p_channels_config[i].get_type() != Variant::INT, ERR_INVALID_PARAMETER, "The 'channels_config' array must contain only enum values from 'MultiplayerPeer.Multiplayer::TransferMode'"); + ERR_FAIL_COND_V_MSG(p_channels_config[i].get_type() != Variant::INT, ERR_INVALID_PARAMETER, "The 'channels_config' array must contain only enum values from 'MultiplayerPeer.TransferMode'"); int mode = p_channels_config[i].operator int(); // Initialize data channel configurations. Dictionary cfg; @@ -207,16 +231,17 @@ Error WebRTCMultiplayerPeer::initialize(int p_self_id, bool p_server_compat, Arr case TRANSFER_MODE_RELIABLE: break; default: - ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, vformat("The 'channels_config' array must contain only enum values from 'MultiplayerPeer.Multiplayer::TransferMode'. Got: %d", mode)); + ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, vformat("The 'channels_config' array must contain only enum values from 'MultiplayerPeer.TransferMode'. Got: %d", mode)); } channels_config.push_back(cfg); + channels_modes.push_back((TransferMode)mode); } unique_id = p_self_id; - server_compat = p_server_compat; + network_mode = p_mode; // Mesh and server are always connected - if (!server_compat || p_self_id == 1) { + if (p_mode != MODE_CLIENT) { connection_status = CONNECTION_CONNECTED; } else { connection_status = CONNECTION_CONNECTING; @@ -224,6 +249,10 @@ Error WebRTCMultiplayerPeer::initialize(int p_self_id, bool p_server_compat, Arr return OK; } +bool WebRTCMultiplayerPeer::is_server_relay_supported() const { + return network_mode == MODE_SERVER || network_mode == MODE_CLIENT; +} + int WebRTCMultiplayerPeer::get_unique_id() const { ERR_FAIL_COND_V(connection_status == CONNECTION_DISCONNECTED, 1); return unique_id; @@ -261,7 +290,10 @@ Dictionary WebRTCMultiplayerPeer::get_peers() { } Error WebRTCMultiplayerPeer::add_peer(Ref<WebRTCPeerConnection> p_peer, int p_peer_id, int p_unreliable_lifetime) { - ERR_FAIL_COND_V(p_peer_id < 0 || p_peer_id > ~(1 << 31), ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V(network_mode == MODE_NONE, ERR_UNCONFIGURED); + ERR_FAIL_COND_V(network_mode == MODE_CLIENT && p_peer_id != 1, ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V(network_mode == MODE_SERVER && p_peer_id == 1, ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V(p_peer_id < 1 || p_peer_id > ~(1 << 31), ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(p_unreliable_lifetime < 0, ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(is_refusing_new_connections(), ERR_UNAUTHORIZED); // Peer must be valid, and in new state (to create data channels) @@ -308,10 +340,21 @@ void WebRTCMultiplayerPeer::remove_peer(int p_peer_id) { if (peer->connected) { peer->connected = false; emit_signal(SNAME("peer_disconnected"), p_peer_id); - if (server_compat && p_peer_id == TARGET_PEER_SERVER) { - emit_signal(SNAME("server_disconnected")); + if (network_mode == MODE_CLIENT && p_peer_id == TARGET_PEER_SERVER) { + connection_status = CONNECTION_DISCONNECTED; + } + } +} + +void WebRTCMultiplayerPeer::disconnect_peer(int p_peer_id, bool p_force) { + ERR_FAIL_COND(!peer_map.has(p_peer_id)); + if (p_force) { + peer_map.erase(p_peer_id); + if (network_mode == MODE_CLIENT && p_peer_id == TARGET_PEER_SERVER) { connection_status = CONNECTION_DISCONNECTED; } + } else { + peer_map[p_peer_id]->connection->close(); // Will be removed during next poll. } } @@ -403,7 +446,9 @@ void WebRTCMultiplayerPeer::close() { channels_config.clear(); unique_id = 0; next_packet_peer = 0; + next_packet_channel = 0; target_peer = 0; + network_mode = MODE_NONE; connection_status = CONNECTION_DISCONNECTED; } diff --git a/modules/webrtc/webrtc_multiplayer_peer.h b/modules/webrtc/webrtc_multiplayer_peer.h index ea7c60036b..0556ef029c 100644 --- a/modules/webrtc/webrtc_multiplayer_peer.h +++ b/modules/webrtc/webrtc_multiplayer_peer.h @@ -48,6 +48,13 @@ private: CH_RESERVED_MAX = 3 }; + enum NetworkMode { + MODE_NONE, + MODE_SERVER, + MODE_CLIENT, + MODE_MESH, + }; + class ConnectedPeer : public RefCounted { public: Ref<WebRTCPeerConnection> connection; @@ -67,43 +74,53 @@ private: int client_count = 0; ConnectionStatus connection_status = CONNECTION_DISCONNECTED; int next_packet_peer = 0; - bool server_compat = false; + int next_packet_channel = 0; + NetworkMode network_mode = MODE_NONE; HashMap<int, Ref<ConnectedPeer>> peer_map; + List<TransferMode> channels_modes; List<Dictionary> channels_config; void _peer_to_dict(Ref<ConnectedPeer> p_connected_peer, Dictionary &r_dict); void _find_next_peer(); + Ref<ConnectedPeer> _get_next_peer(); + Error _initialize(int p_self_id, NetworkMode p_mode, Array p_channels_config = Array()); public: WebRTCMultiplayerPeer() {} ~WebRTCMultiplayerPeer(); - Error initialize(int p_self_id, bool p_server_compat = false, Array p_channels_config = Array()); + Error create_server(Array p_channels_config = Array()); + Error create_client(int p_self_id, Array p_channels_config = Array()); + Error create_mesh(int p_self_id, Array p_channels_config = Array()); Error add_peer(Ref<WebRTCPeerConnection> p_peer, int p_peer_id, int p_unreliable_lifetime = 1); void remove_peer(int p_peer_id); bool has_peer(int p_peer_id); Dictionary get_peer(int p_peer_id); Dictionary get_peers(); - void close(); // PacketPeer - Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; ///< buffer is GONE after next get_packet - Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override; - int get_available_packet_count() const override; - int get_max_packet_size() const override; + virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; ///< buffer is GONE after next get_packet + virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override; + virtual int get_available_packet_count() const override; + virtual int get_max_packet_size() const override; // MultiplayerPeer - void set_target_peer(int p_peer_id) override; + virtual void set_target_peer(int p_peer_id) override; - int get_unique_id() const override; - int get_packet_peer() const override; + virtual int get_unique_id() const override; + virtual int get_packet_peer() const override; + virtual int get_packet_channel() const override; + virtual TransferMode get_packet_mode() const override; - bool is_server() const override; + virtual bool is_server() const override; + virtual bool is_server_relay_supported() const override; - void poll() override; + virtual void poll() override; + virtual void close() override; + virtual void disconnect_peer(int p_peer_id, bool p_force = false) override; - ConnectionStatus get_connection_status() const override; + virtual ConnectionStatus get_connection_status() const override; }; #endif // WEBRTC_MULTIPLAYER_PEER_H diff --git a/modules/webrtc/webrtc_peer_connection.cpp b/modules/webrtc/webrtc_peer_connection.cpp index 75716017d7..5aa891d35c 100644 --- a/modules/webrtc/webrtc_peer_connection.cpp +++ b/modules/webrtc/webrtc_peer_connection.cpp @@ -30,7 +30,7 @@ #include "webrtc_peer_connection.h" -#ifdef JAVASCRIPT_ENABLED +#ifdef WEB_ENABLED #include "webrtc_peer_connection_js.h" #endif @@ -44,7 +44,7 @@ void WebRTCPeerConnection::set_default_extension(const StringName &p_extension) } WebRTCPeerConnection *WebRTCPeerConnection::create() { -#ifdef JAVASCRIPT_ENABLED +#ifdef WEB_ENABLED return memnew(WebRTCPeerConnectionJS); #else if (default_extension == String()) { @@ -69,6 +69,8 @@ void WebRTCPeerConnection::_bind_methods() { ClassDB::bind_method(D_METHOD("close"), &WebRTCPeerConnection::close); ClassDB::bind_method(D_METHOD("get_connection_state"), &WebRTCPeerConnection::get_connection_state); + ClassDB::bind_method(D_METHOD("get_gathering_state"), &WebRTCPeerConnection::get_gathering_state); + ClassDB::bind_method(D_METHOD("get_signaling_state"), &WebRTCPeerConnection::get_signaling_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"))); @@ -80,6 +82,17 @@ void WebRTCPeerConnection::_bind_methods() { BIND_ENUM_CONSTANT(STATE_DISCONNECTED); BIND_ENUM_CONSTANT(STATE_FAILED); BIND_ENUM_CONSTANT(STATE_CLOSED); + + BIND_ENUM_CONSTANT(GATHERING_STATE_NEW); + BIND_ENUM_CONSTANT(GATHERING_STATE_GATHERING); + BIND_ENUM_CONSTANT(GATHERING_STATE_COMPLETE); + + BIND_ENUM_CONSTANT(SIGNALING_STATE_STABLE); + BIND_ENUM_CONSTANT(SIGNALING_STATE_HAVE_LOCAL_OFFER); + BIND_ENUM_CONSTANT(SIGNALING_STATE_HAVE_REMOTE_OFFER); + BIND_ENUM_CONSTANT(SIGNALING_STATE_HAVE_LOCAL_PRANSWER); + BIND_ENUM_CONSTANT(SIGNALING_STATE_HAVE_REMOTE_PRANSWER); + BIND_ENUM_CONSTANT(SIGNALING_STATE_CLOSED); } WebRTCPeerConnection::WebRTCPeerConnection() { diff --git a/modules/webrtc/webrtc_peer_connection.h b/modules/webrtc/webrtc_peer_connection.h index 122ea3d00f..76f29f9d68 100644 --- a/modules/webrtc/webrtc_peer_connection.h +++ b/modules/webrtc/webrtc_peer_connection.h @@ -47,6 +47,21 @@ public: STATE_CLOSED }; + enum GatheringState { + GATHERING_STATE_NEW, + GATHERING_STATE_GATHERING, + GATHERING_STATE_COMPLETE, + }; + + enum SignalingState { + SIGNALING_STATE_STABLE, + SIGNALING_STATE_HAVE_LOCAL_OFFER, + SIGNALING_STATE_HAVE_REMOTE_OFFER, + SIGNALING_STATE_HAVE_LOCAL_PRANSWER, + SIGNALING_STATE_HAVE_REMOTE_PRANSWER, + SIGNALING_STATE_CLOSED, + }; + private: static StringName default_extension; @@ -57,6 +72,8 @@ public: static void set_default_extension(const StringName &p_name); virtual ConnectionState get_connection_state() const = 0; + virtual GatheringState get_gathering_state() const = 0; + virtual SignalingState get_signaling_state() const = 0; virtual Error initialize(Dictionary p_config = Dictionary()) = 0; virtual Ref<WebRTCDataChannel> create_data_channel(String p_label, Dictionary p_options = Dictionary()) = 0; @@ -74,5 +91,7 @@ public: }; VARIANT_ENUM_CAST(WebRTCPeerConnection::ConnectionState); +VARIANT_ENUM_CAST(WebRTCPeerConnection::GatheringState); +VARIANT_ENUM_CAST(WebRTCPeerConnection::SignalingState); #endif // WEBRTC_PEER_CONNECTION_H diff --git a/modules/webrtc/webrtc_peer_connection_extension.cpp b/modules/webrtc/webrtc_peer_connection_extension.cpp index 85c04b3b19..592a1f8a97 100644 --- a/modules/webrtc/webrtc_peer_connection_extension.cpp +++ b/modules/webrtc/webrtc_peer_connection_extension.cpp @@ -32,6 +32,8 @@ void WebRTCPeerConnectionExtension::_bind_methods() { GDVIRTUAL_BIND(_get_connection_state); + GDVIRTUAL_BIND(_get_gathering_state); + GDVIRTUAL_BIND(_get_signaling_state); GDVIRTUAL_BIND(_initialize, "p_config"); GDVIRTUAL_BIND(_create_data_channel, "p_label", "p_config"); GDVIRTUAL_BIND(_create_offer); @@ -42,24 +44,6 @@ void WebRTCPeerConnectionExtension::_bind_methods() { GDVIRTUAL_BIND(_close); } -WebRTCPeerConnection::ConnectionState WebRTCPeerConnectionExtension::get_connection_state() const { - int state; - if (GDVIRTUAL_CALL(_get_connection_state, state)) { - return (ConnectionState)state; - } - WARN_PRINT_ONCE("WebRTCPeerConnectionExtension::_get_connection_state is unimplemented!"); - return STATE_DISCONNECTED; -} - -Error WebRTCPeerConnectionExtension::initialize(Dictionary p_config) { - int err; - if (GDVIRTUAL_CALL(_initialize, p_config, err)) { - return (Error)err; - } - WARN_PRINT_ONCE("WebRTCPeerConnectionExtension::_initialize is unimplemented!"); - return ERR_UNCONFIGURED; -} - Ref<WebRTCDataChannel> WebRTCPeerConnectionExtension::create_data_channel(String p_label, Dictionary p_options) { Object *ret = nullptr; if (GDVIRTUAL_CALL(_create_data_channel, p_label, p_options, ret)) { @@ -70,55 +54,3 @@ Ref<WebRTCDataChannel> WebRTCPeerConnectionExtension::create_data_channel(String WARN_PRINT_ONCE("WebRTCPeerConnectionExtension::_create_data_channel is unimplemented!"); return nullptr; } - -Error WebRTCPeerConnectionExtension::create_offer() { - int err; - if (GDVIRTUAL_CALL(_create_offer, err)) { - return (Error)err; - } - WARN_PRINT_ONCE("WebRTCPeerConnectionExtension::_create_offer is unimplemented!"); - return ERR_UNCONFIGURED; -} - -Error WebRTCPeerConnectionExtension::set_local_description(String p_type, String p_sdp) { - int err; - if (GDVIRTUAL_CALL(_set_local_description, p_type, p_sdp, err)) { - return (Error)err; - } - WARN_PRINT_ONCE("WebRTCPeerConnectionExtension::_set_local_description is unimplemented!"); - return ERR_UNCONFIGURED; -} - -Error WebRTCPeerConnectionExtension::set_remote_description(String p_type, String p_sdp) { - int err; - if (GDVIRTUAL_CALL(_set_remote_description, p_type, p_sdp, err)) { - return (Error)err; - } - WARN_PRINT_ONCE("WebRTCPeerConnectionExtension::_set_remote_description is unimplemented!"); - return ERR_UNCONFIGURED; -} - -Error WebRTCPeerConnectionExtension::add_ice_candidate(String p_sdp_mid_name, int p_sdp_mline_index, String p_sdp_name) { - int err; - if (GDVIRTUAL_CALL(_add_ice_candidate, p_sdp_mid_name, p_sdp_mline_index, p_sdp_name, err)) { - return (Error)err; - } - WARN_PRINT_ONCE("WebRTCPeerConnectionExtension::_add_ice_candidate is unimplemented!"); - return ERR_UNCONFIGURED; -} - -Error WebRTCPeerConnectionExtension::poll() { - int err; - if (GDVIRTUAL_CALL(_poll, err)) { - return (Error)err; - } - WARN_PRINT_ONCE("WebRTCPeerConnectionExtension::_poll is unimplemented!"); - return ERR_UNCONFIGURED; -} - -void WebRTCPeerConnectionExtension::close() { - if (GDVIRTUAL_CALL(_close)) { - return; - } - WARN_PRINT_ONCE("WebRTCPeerConnectionExtension::_close is unimplemented!"); -} diff --git a/modules/webrtc/webrtc_peer_connection_extension.h b/modules/webrtc/webrtc_peer_connection_extension.h index bde19c173b..085069debb 100644 --- a/modules/webrtc/webrtc_peer_connection_extension.h +++ b/modules/webrtc/webrtc_peer_connection_extension.h @@ -33,6 +33,7 @@ #include "webrtc_peer_connection.h" +#include "core/extension/ext_wrappers.gen.inc" #include "core/object/gdvirtual.gen.inc" #include "core/object/script_language.h" #include "core/variant/native_ptr.h" @@ -44,27 +45,23 @@ protected: static void _bind_methods(); public: - virtual ConnectionState get_connection_state() const override; - - virtual Error initialize(Dictionary p_config = Dictionary()) override; + // FIXME Can't be directly exposed due to issues in exchanging Ref(s) between godot and extensions. + // See godot-cpp GH-652 . virtual Ref<WebRTCDataChannel> create_data_channel(String p_label, Dictionary p_options = Dictionary()) override; - virtual Error create_offer() override; - virtual Error set_remote_description(String type, String sdp) override; - virtual Error set_local_description(String type, String sdp) override; - virtual Error add_ice_candidate(String p_sdp_mid_name, int p_sdp_mline_index, String p_sdp_name) override; - virtual Error poll() override; - virtual void close() override; + GDVIRTUAL2R(Object *, _create_data_channel, String, Dictionary); + // EXBIND2R(Ref<WebRTCDataChannel>, create_data_channel, String, Dictionary); /** GDExtension **/ - GDVIRTUAL0RC(int, _get_connection_state); - GDVIRTUAL1R(int, _initialize, Dictionary); - GDVIRTUAL2R(Object *, _create_data_channel, String, Dictionary); - GDVIRTUAL0R(int, _create_offer); - GDVIRTUAL2R(int, _set_remote_description, String, String); - GDVIRTUAL2R(int, _set_local_description, String, String); - GDVIRTUAL3R(int, _add_ice_candidate, String, int, String); - GDVIRTUAL0R(int, _poll); - GDVIRTUAL0(_close); + EXBIND0RC(ConnectionState, get_connection_state); + EXBIND0RC(GatheringState, get_gathering_state); + EXBIND0RC(SignalingState, get_signaling_state); + EXBIND1R(Error, initialize, Dictionary); + EXBIND0R(Error, create_offer); + EXBIND2R(Error, set_remote_description, String, String); + EXBIND2R(Error, set_local_description, String, String); + EXBIND3R(Error, add_ice_candidate, String, int, String); + EXBIND0R(Error, poll); + EXBIND0(close); WebRTCPeerConnectionExtension() {} }; diff --git a/modules/webrtc/webrtc_peer_connection_js.cpp b/modules/webrtc/webrtc_peer_connection_js.cpp index ee3a302fa2..a371312ae9 100644 --- a/modules/webrtc/webrtc_peer_connection_js.cpp +++ b/modules/webrtc/webrtc_peer_connection_js.cpp @@ -28,7 +28,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifdef JAVASCRIPT_ENABLED +#ifdef WEB_ENABLED #include "webrtc_peer_connection_js.h" @@ -51,6 +51,16 @@ void WebRTCPeerConnectionJS::_on_connection_state_changed(void *p_obj, int p_sta peer->_conn_state = (ConnectionState)p_state; } +void WebRTCPeerConnectionJS::_on_gathering_state_changed(void *p_obj, int p_state) { + WebRTCPeerConnectionJS *peer = static_cast<WebRTCPeerConnectionJS *>(p_obj); + peer->_gathering_state = (GatheringState)p_state; +} + +void WebRTCPeerConnectionJS::_on_signaling_state_changed(void *p_obj, int p_state) { + WebRTCPeerConnectionJS *peer = static_cast<WebRTCPeerConnectionJS *>(p_obj); + peer->_signaling_state = (SignalingState)p_state; +} + void WebRTCPeerConnectionJS::_on_error(void *p_obj) { ERR_PRINT("RTCPeerConnection error!"); } @@ -100,7 +110,7 @@ Error WebRTCPeerConnectionJS::initialize(Dictionary p_config) { _conn_state = STATE_NEW; String config = Variant(p_config).to_json_string(); - _js_id = godot_js_rtc_pc_create(config.utf8().get_data(), this, &_on_connection_state_changed, &_on_ice_candidate, &_on_data_channel); + _js_id = godot_js_rtc_pc_create(config.utf8().get_data(), this, &_on_connection_state_changed, &_on_gathering_state_changed, &_on_signaling_state_changed, &_on_ice_candidate, &_on_data_channel); return _js_id ? OK : FAILED; } @@ -117,14 +127,19 @@ Error WebRTCPeerConnectionJS::poll() { return OK; } +WebRTCPeerConnection::GatheringState WebRTCPeerConnectionJS::get_gathering_state() const { + return _gathering_state; +} + +WebRTCPeerConnection::SignalingState WebRTCPeerConnectionJS::get_signaling_state() const { + return _signaling_state; +} + WebRTCPeerConnection::ConnectionState WebRTCPeerConnectionJS::get_connection_state() const { return _conn_state; } WebRTCPeerConnectionJS::WebRTCPeerConnectionJS() { - _conn_state = STATE_NEW; - _js_id = 0; - Dictionary config; initialize(config); } diff --git a/modules/webrtc/webrtc_peer_connection_js.h b/modules/webrtc/webrtc_peer_connection_js.h index 76b8c7fff8..e62ad6af28 100644 --- a/modules/webrtc/webrtc_peer_connection_js.h +++ b/modules/webrtc/webrtc_peer_connection_js.h @@ -31,17 +31,19 @@ #ifndef WEBRTC_PEER_CONNECTION_JS_H #define WEBRTC_PEER_CONNECTION_JS_H -#ifdef JAVASCRIPT_ENABLED +#ifdef WEB_ENABLED #include "webrtc_peer_connection.h" extern "C" { typedef void (*RTCOnIceConnectionStateChange)(void *p_obj, int p_state); +typedef void (*RTCOnIceGatheringStateChange)(void *p_obj, int p_state); +typedef void (*RTCOnSignalingStateChange)(void *p_obj, int p_state); typedef void (*RTCOnIceCandidate)(void *p_obj, const char *p_mid, int p_mline_idx, const char *p_candidate); typedef void (*RTCOnDataChannel)(void *p_obj, int p_id); typedef void (*RTCOnSession)(void *p_obj, const char *p_type, const char *p_sdp); typedef void (*RTCOnError)(void *p_obj); -extern int godot_js_rtc_pc_create(const char *p_config, void *p_obj, RTCOnIceConnectionStateChange p_on_state_change, RTCOnIceCandidate p_on_candidate, RTCOnDataChannel p_on_datachannel); +extern int godot_js_rtc_pc_create(const char *p_config, void *p_obj, RTCOnIceConnectionStateChange p_on_connection_state_change, RTCOnIceGatheringStateChange p_on_gathering_state_change, RTCOnSignalingStateChange p_on_signaling_state_change, RTCOnIceCandidate p_on_candidate, RTCOnDataChannel p_on_datachannel); extern void godot_js_rtc_pc_close(int p_id); extern void godot_js_rtc_pc_destroy(int p_id); extern void godot_js_rtc_pc_offer_create(int p_id, void *p_obj, RTCOnSession p_on_session, RTCOnError p_on_error); @@ -55,10 +57,14 @@ class WebRTCPeerConnectionJS : public WebRTCPeerConnection { GDCLASS(WebRTCPeerConnectionJS, WebRTCPeerConnection); private: - int _js_id; - ConnectionState _conn_state; + int _js_id = 0; + ConnectionState _conn_state = STATE_NEW; + GatheringState _gathering_state = GATHERING_STATE_NEW; + SignalingState _signaling_state = SIGNALING_STATE_STABLE; static void _on_connection_state_changed(void *p_obj, int p_state); + static void _on_gathering_state_changed(void *p_obj, int p_state); + static void _on_signaling_state_changed(void *p_obj, int p_state); static void _on_ice_candidate(void *p_obj, const char *p_mid_name, int p_mline_idx, const char *p_candidate); static void _on_data_channel(void *p_obj, int p_channel); static void _on_session_created(void *p_obj, const char *p_type, const char *p_session); @@ -66,6 +72,8 @@ private: public: virtual ConnectionState get_connection_state() const override; + virtual GatheringState get_gathering_state() const override; + virtual SignalingState get_signaling_state() const override; virtual Error initialize(Dictionary configuration = Dictionary()) override; virtual Ref<WebRTCDataChannel> create_data_channel(String p_channel_name, Dictionary p_channel_config = Dictionary()) override; |