webrtc_peer_connection_js.cpp 6.4 KB

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