| 1 | /**************************************************************************/ |
| 2 | /* webrtc_peer_connection_js.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_js.h" |
| 32 | |
| 33 | #ifdef WEB_ENABLED |
| 34 | |
| 35 | #include "webrtc_data_channel_js.h" |
| 36 | |
| 37 | #include <emscripten.h> |
| 38 | |
| 39 | void WebRTCPeerConnectionJS::_on_ice_candidate(void *p_obj, const char *p_mid_name, int p_mline_idx, const char *p_candidate) { |
| 40 | WebRTCPeerConnectionJS *peer = static_cast<WebRTCPeerConnectionJS *>(p_obj); |
| 41 | peer->emit_signal(SNAME("ice_candidate_created" ), String(p_mid_name), p_mline_idx, String(p_candidate)); |
| 42 | } |
| 43 | |
| 44 | void WebRTCPeerConnectionJS::_on_session_created(void *p_obj, const char *p_type, const char *p_session) { |
| 45 | WebRTCPeerConnectionJS *peer = static_cast<WebRTCPeerConnectionJS *>(p_obj); |
| 46 | peer->emit_signal(SNAME("session_description_created" ), String(p_type), String(p_session)); |
| 47 | } |
| 48 | |
| 49 | void WebRTCPeerConnectionJS::_on_connection_state_changed(void *p_obj, int p_state) { |
| 50 | WebRTCPeerConnectionJS *peer = static_cast<WebRTCPeerConnectionJS *>(p_obj); |
| 51 | peer->_conn_state = (ConnectionState)p_state; |
| 52 | } |
| 53 | |
| 54 | void WebRTCPeerConnectionJS::_on_gathering_state_changed(void *p_obj, int p_state) { |
| 55 | WebRTCPeerConnectionJS *peer = static_cast<WebRTCPeerConnectionJS *>(p_obj); |
| 56 | peer->_gathering_state = (GatheringState)p_state; |
| 57 | } |
| 58 | |
| 59 | void WebRTCPeerConnectionJS::_on_signaling_state_changed(void *p_obj, int p_state) { |
| 60 | WebRTCPeerConnectionJS *peer = static_cast<WebRTCPeerConnectionJS *>(p_obj); |
| 61 | peer->_signaling_state = (SignalingState)p_state; |
| 62 | } |
| 63 | |
| 64 | void WebRTCPeerConnectionJS::_on_error(void *p_obj) { |
| 65 | ERR_PRINT("RTCPeerConnection error!" ); |
| 66 | } |
| 67 | |
| 68 | void WebRTCPeerConnectionJS::_on_data_channel(void *p_obj, int p_id) { |
| 69 | WebRTCPeerConnectionJS *peer = static_cast<WebRTCPeerConnectionJS *>(p_obj); |
| 70 | peer->emit_signal(SNAME("data_channel_received" ), Ref<WebRTCDataChannel>(memnew(WebRTCDataChannelJS(p_id)))); |
| 71 | } |
| 72 | |
| 73 | void WebRTCPeerConnectionJS::close() { |
| 74 | godot_js_rtc_pc_close(_js_id); |
| 75 | _conn_state = STATE_CLOSED; |
| 76 | } |
| 77 | |
| 78 | Error WebRTCPeerConnectionJS::create_offer() { |
| 79 | ERR_FAIL_COND_V(_conn_state != STATE_NEW, FAILED); |
| 80 | |
| 81 | _conn_state = STATE_CONNECTING; |
| 82 | godot_js_rtc_pc_offer_create(_js_id, this, &_on_session_created, &_on_error); |
| 83 | return OK; |
| 84 | } |
| 85 | |
| 86 | Error WebRTCPeerConnectionJS::set_local_description(String type, String sdp) { |
| 87 | godot_js_rtc_pc_local_description_set(_js_id, type.utf8().get_data(), sdp.utf8().get_data(), this, &_on_error); |
| 88 | return OK; |
| 89 | } |
| 90 | |
| 91 | Error WebRTCPeerConnectionJS::set_remote_description(String type, String sdp) { |
| 92 | if (type == "offer" ) { |
| 93 | ERR_FAIL_COND_V(_conn_state != STATE_NEW, FAILED); |
| 94 | _conn_state = STATE_CONNECTING; |
| 95 | } |
| 96 | godot_js_rtc_pc_remote_description_set(_js_id, type.utf8().get_data(), sdp.utf8().get_data(), this, &_on_session_created, &_on_error); |
| 97 | return OK; |
| 98 | } |
| 99 | |
| 100 | Error WebRTCPeerConnectionJS::add_ice_candidate(String sdpMidName, int sdpMlineIndexName, String sdpName) { |
| 101 | godot_js_rtc_pc_ice_candidate_add(_js_id, sdpMidName.utf8().get_data(), sdpMlineIndexName, sdpName.utf8().get_data()); |
| 102 | return OK; |
| 103 | } |
| 104 | |
| 105 | Error WebRTCPeerConnectionJS::initialize(Dictionary p_config) { |
| 106 | if (_js_id) { |
| 107 | godot_js_rtc_pc_destroy(_js_id); |
| 108 | _js_id = 0; |
| 109 | } |
| 110 | _conn_state = STATE_NEW; |
| 111 | |
| 112 | String config = Variant(p_config).to_json_string(); |
| 113 | _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); |
| 114 | return _js_id ? OK : FAILED; |
| 115 | } |
| 116 | |
| 117 | Ref<WebRTCDataChannel> WebRTCPeerConnectionJS::create_data_channel(String p_channel, Dictionary p_channel_config) { |
| 118 | ERR_FAIL_COND_V(_conn_state != STATE_NEW, nullptr); |
| 119 | |
| 120 | String config = Variant(p_channel_config).to_json_string(); |
| 121 | int id = godot_js_rtc_pc_datachannel_create(_js_id, p_channel.utf8().get_data(), config.utf8().get_data()); |
| 122 | ERR_FAIL_COND_V(id == 0, nullptr); |
| 123 | return memnew(WebRTCDataChannelJS(id)); |
| 124 | } |
| 125 | |
| 126 | Error WebRTCPeerConnectionJS::poll() { |
| 127 | return OK; |
| 128 | } |
| 129 | |
| 130 | WebRTCPeerConnection::GatheringState WebRTCPeerConnectionJS::get_gathering_state() const { |
| 131 | return _gathering_state; |
| 132 | } |
| 133 | |
| 134 | WebRTCPeerConnection::SignalingState WebRTCPeerConnectionJS::get_signaling_state() const { |
| 135 | return _signaling_state; |
| 136 | } |
| 137 | |
| 138 | WebRTCPeerConnection::ConnectionState WebRTCPeerConnectionJS::get_connection_state() const { |
| 139 | return _conn_state; |
| 140 | } |
| 141 | |
| 142 | WebRTCPeerConnectionJS::WebRTCPeerConnectionJS() { |
| 143 | Dictionary config; |
| 144 | initialize(config); |
| 145 | } |
| 146 | |
| 147 | WebRTCPeerConnectionJS::~WebRTCPeerConnectionJS() { |
| 148 | close(); |
| 149 | if (_js_id) { |
| 150 | godot_js_rtc_pc_destroy(_js_id); |
| 151 | _js_id = 0; |
| 152 | } |
| 153 | }; |
| 154 | #endif |
| 155 | |