webrtc_peer_connection_js.cpp 5.7 KB

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