1 /*************************************************************************/
2 /*  webrtc_peer_connection_gdnative.cpp                                  */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 
31 #ifdef WEBRTC_GDNATIVE_ENABLED
32 
33 #include "webrtc_peer_connection_gdnative.h"
34 
35 #include "core/io/resource_loader.h"
36 #include "modules/gdnative/nativescript/nativescript.h"
37 #include "webrtc_data_channel_gdnative.h"
38 
39 const godot_net_webrtc_library *WebRTCPeerConnectionGDNative::default_library = NULL;
40 
set_default_library(const godot_net_webrtc_library * p_lib)41 Error WebRTCPeerConnectionGDNative::set_default_library(const godot_net_webrtc_library *p_lib) {
42 	if (default_library) {
43 		const godot_net_webrtc_library *old = default_library;
44 		default_library = NULL;
45 		old->unregistered();
46 	}
47 	default_library = p_lib;
48 	return OK; // Maybe add version check and fail accordingly
49 }
50 
_create()51 WebRTCPeerConnection *WebRTCPeerConnectionGDNative::_create() {
52 
53 	WebRTCPeerConnectionGDNative *obj = memnew(WebRTCPeerConnectionGDNative);
54 	ERR_FAIL_COND_V_MSG(!default_library, obj, "Default GDNative WebRTC implementation not defined.");
55 
56 	// Call GDNative constructor
57 	Error err = (Error)default_library->create_peer_connection(obj);
58 	ERR_FAIL_COND_V_MSG(err != OK, obj, "GDNative default library constructor returned an error.");
59 
60 	return obj;
61 }
62 
_bind_methods()63 void WebRTCPeerConnectionGDNative::_bind_methods() {
64 }
65 
WebRTCPeerConnectionGDNative()66 WebRTCPeerConnectionGDNative::WebRTCPeerConnectionGDNative() {
67 	interface = NULL;
68 }
69 
~WebRTCPeerConnectionGDNative()70 WebRTCPeerConnectionGDNative::~WebRTCPeerConnectionGDNative() {
71 }
72 
initialize(Dictionary p_config)73 Error WebRTCPeerConnectionGDNative::initialize(Dictionary p_config) {
74 	ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED);
75 	return (Error)interface->initialize(interface->data, (const godot_dictionary *)&p_config);
76 }
77 
create_data_channel(String p_label,Dictionary p_options)78 Ref<WebRTCDataChannel> WebRTCPeerConnectionGDNative::create_data_channel(String p_label, Dictionary p_options) {
79 	ERR_FAIL_COND_V(interface == NULL, NULL);
80 	return (WebRTCDataChannel *)interface->create_data_channel(interface->data, p_label.utf8().get_data(), (const godot_dictionary *)&p_options);
81 }
82 
create_offer()83 Error WebRTCPeerConnectionGDNative::create_offer() {
84 	ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED);
85 	return (Error)interface->create_offer(interface->data);
86 }
87 
set_local_description(String p_type,String p_sdp)88 Error WebRTCPeerConnectionGDNative::set_local_description(String p_type, String p_sdp) {
89 	ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED);
90 	return (Error)interface->set_local_description(interface->data, p_type.utf8().get_data(), p_sdp.utf8().get_data());
91 }
92 
set_remote_description(String p_type,String p_sdp)93 Error WebRTCPeerConnectionGDNative::set_remote_description(String p_type, String p_sdp) {
94 	ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED);
95 	return (Error)interface->set_remote_description(interface->data, p_type.utf8().get_data(), p_sdp.utf8().get_data());
96 }
97 
add_ice_candidate(String sdpMidName,int sdpMlineIndexName,String sdpName)98 Error WebRTCPeerConnectionGDNative::add_ice_candidate(String sdpMidName, int sdpMlineIndexName, String sdpName) {
99 	ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED);
100 	return (Error)interface->add_ice_candidate(interface->data, sdpMidName.utf8().get_data(), sdpMlineIndexName, sdpName.utf8().get_data());
101 }
102 
poll()103 Error WebRTCPeerConnectionGDNative::poll() {
104 	ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED);
105 	return (Error)interface->poll(interface->data);
106 }
107 
close()108 void WebRTCPeerConnectionGDNative::close() {
109 	ERR_FAIL_COND(interface == NULL);
110 	interface->close(interface->data);
111 }
112 
get_connection_state() const113 WebRTCPeerConnection::ConnectionState WebRTCPeerConnectionGDNative::get_connection_state() const {
114 	ERR_FAIL_COND_V(interface == NULL, STATE_DISCONNECTED);
115 	return (ConnectionState)interface->get_connection_state(interface->data);
116 }
117 
set_native_webrtc_peer_connection(const godot_net_webrtc_peer_connection * p_impl)118 void WebRTCPeerConnectionGDNative::set_native_webrtc_peer_connection(const godot_net_webrtc_peer_connection *p_impl) {
119 	interface = p_impl;
120 }
121 
122 #endif // WEBRTC_GDNATIVE_ENABLED
123