| 1 | /**************************************************************************/ |
| 2 | /* webrtc_peer_connection.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 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 | #include "webrtc_peer_connection.h" |
| 32 | |
| 33 | #ifdef WEB_ENABLED |
| 34 | #include "webrtc_peer_connection_js.h" |
| 35 | #endif |
| 36 | |
| 37 | #include "webrtc_peer_connection_extension.h" |
| 38 | |
| 39 | StringName WebRTCPeerConnection::default_extension; |
| 40 | |
| 41 | void WebRTCPeerConnection::set_default_extension(const StringName &p_extension) { |
| 42 | ERR_FAIL_COND_MSG(!ClassDB::is_parent_class(p_extension, WebRTCPeerConnectionExtension::get_class_static()), vformat("Can't make %s the default WebRTC extension since it does not extend WebRTCPeerConnectionExtension." , p_extension)); |
| 43 | default_extension = p_extension; |
| 44 | } |
| 45 | |
| 46 | WebRTCPeerConnection *WebRTCPeerConnection::create() { |
| 47 | #ifdef WEB_ENABLED |
| 48 | return memnew(WebRTCPeerConnectionJS); |
| 49 | #else |
| 50 | if (default_extension == String()) { |
| 51 | WARN_PRINT_ONCE("No default WebRTC extension configured." ); |
| 52 | return memnew(WebRTCPeerConnectionExtension); |
| 53 | } |
| 54 | Object *obj = ClassDB::instantiate(default_extension); |
| 55 | return Object::cast_to<WebRTCPeerConnectionExtension>(obj); |
| 56 | #endif |
| 57 | } |
| 58 | |
| 59 | void WebRTCPeerConnection::_bind_methods() { |
| 60 | ClassDB::bind_static_method(get_class_static(), D_METHOD("set_default_extension" , "extension_class" ), &WebRTCPeerConnectionExtension::set_default_extension); |
| 61 | |
| 62 | ClassDB::bind_method(D_METHOD("initialize" , "configuration" ), &WebRTCPeerConnection::initialize, DEFVAL(Dictionary())); |
| 63 | ClassDB::bind_method(D_METHOD("create_data_channel" , "label" , "options" ), &WebRTCPeerConnection::create_data_channel, DEFVAL(Dictionary())); |
| 64 | ClassDB::bind_method(D_METHOD("create_offer" ), &WebRTCPeerConnection::create_offer); |
| 65 | ClassDB::bind_method(D_METHOD("set_local_description" , "type" , "sdp" ), &WebRTCPeerConnection::set_local_description); |
| 66 | ClassDB::bind_method(D_METHOD("set_remote_description" , "type" , "sdp" ), &WebRTCPeerConnection::set_remote_description); |
| 67 | ClassDB::bind_method(D_METHOD("add_ice_candidate" , "media" , "index" , "name" ), &WebRTCPeerConnection::add_ice_candidate); |
| 68 | ClassDB::bind_method(D_METHOD("poll" ), &WebRTCPeerConnection::poll); |
| 69 | ClassDB::bind_method(D_METHOD("close" ), &WebRTCPeerConnection::close); |
| 70 | |
| 71 | ClassDB::bind_method(D_METHOD("get_connection_state" ), &WebRTCPeerConnection::get_connection_state); |
| 72 | ClassDB::bind_method(D_METHOD("get_gathering_state" ), &WebRTCPeerConnection::get_gathering_state); |
| 73 | ClassDB::bind_method(D_METHOD("get_signaling_state" ), &WebRTCPeerConnection::get_signaling_state); |
| 74 | |
| 75 | ADD_SIGNAL(MethodInfo("session_description_created" , PropertyInfo(Variant::STRING, "type" ), PropertyInfo(Variant::STRING, "sdp" ))); |
| 76 | ADD_SIGNAL(MethodInfo("ice_candidate_created" , PropertyInfo(Variant::STRING, "media" ), PropertyInfo(Variant::INT, "index" ), PropertyInfo(Variant::STRING, "name" ))); |
| 77 | ADD_SIGNAL(MethodInfo("data_channel_received" , PropertyInfo(Variant::OBJECT, "channel" , PROPERTY_HINT_RESOURCE_TYPE, "WebRTCDataChannel" ))); |
| 78 | |
| 79 | BIND_ENUM_CONSTANT(STATE_NEW); |
| 80 | BIND_ENUM_CONSTANT(STATE_CONNECTING); |
| 81 | BIND_ENUM_CONSTANT(STATE_CONNECTED); |
| 82 | BIND_ENUM_CONSTANT(STATE_DISCONNECTED); |
| 83 | BIND_ENUM_CONSTANT(STATE_FAILED); |
| 84 | BIND_ENUM_CONSTANT(STATE_CLOSED); |
| 85 | |
| 86 | BIND_ENUM_CONSTANT(GATHERING_STATE_NEW); |
| 87 | BIND_ENUM_CONSTANT(GATHERING_STATE_GATHERING); |
| 88 | BIND_ENUM_CONSTANT(GATHERING_STATE_COMPLETE); |
| 89 | |
| 90 | BIND_ENUM_CONSTANT(SIGNALING_STATE_STABLE); |
| 91 | BIND_ENUM_CONSTANT(SIGNALING_STATE_HAVE_LOCAL_OFFER); |
| 92 | BIND_ENUM_CONSTANT(SIGNALING_STATE_HAVE_REMOTE_OFFER); |
| 93 | BIND_ENUM_CONSTANT(SIGNALING_STATE_HAVE_LOCAL_PRANSWER); |
| 94 | BIND_ENUM_CONSTANT(SIGNALING_STATE_HAVE_REMOTE_PRANSWER); |
| 95 | BIND_ENUM_CONSTANT(SIGNALING_STATE_CLOSED); |
| 96 | } |
| 97 | |
| 98 | WebRTCPeerConnection::WebRTCPeerConnection() { |
| 99 | } |
| 100 | |
| 101 | WebRTCPeerConnection::~WebRTCPeerConnection() { |
| 102 | } |
| 103 | |