webrtc_peer_connection_js.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #ifdef JAVASCRIPT_ENABLED
  31. #include "webrtc_peer_connection_js.h"
  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("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("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_error(void *p_obj) {
  47. ERR_PRINT("RTCPeerConnection error!");
  48. }
  49. void WebRTCPeerConnectionJS::_on_data_channel(void *p_obj, int p_id) {
  50. WebRTCPeerConnectionJS *peer = static_cast<WebRTCPeerConnectionJS *>(p_obj);
  51. peer->emit_signal("data_channel_received", Ref<WebRTCDataChannelJS>(new WebRTCDataChannelJS(p_id)));
  52. }
  53. void WebRTCPeerConnectionJS::close() {
  54. godot_js_rtc_pc_close(_js_id);
  55. _conn_state = STATE_CLOSED;
  56. }
  57. Error WebRTCPeerConnectionJS::create_offer() {
  58. ERR_FAIL_COND_V(_conn_state != STATE_NEW, FAILED);
  59. _conn_state = STATE_CONNECTING;
  60. godot_js_rtc_pc_offer_create(_js_id, this, &_on_session_created, &_on_error);
  61. return OK;
  62. }
  63. Error WebRTCPeerConnectionJS::set_local_description(String type, String sdp) {
  64. godot_js_rtc_pc_local_description_set(_js_id, type.utf8().get_data(), sdp.utf8().get_data(), this, &_on_error);
  65. return OK;
  66. }
  67. Error WebRTCPeerConnectionJS::set_remote_description(String type, String sdp) {
  68. if (type == "offer") {
  69. ERR_FAIL_COND_V(_conn_state != STATE_NEW, FAILED);
  70. _conn_state = STATE_CONNECTING;
  71. }
  72. godot_js_rtc_pc_remote_description_set(_js_id, type.utf8().get_data(), sdp.utf8().get_data(), this, &_on_session_created, &_on_error);
  73. return OK;
  74. }
  75. Error WebRTCPeerConnectionJS::add_ice_candidate(String sdpMidName, int sdpMlineIndexName, String sdpName) {
  76. godot_js_rtc_pc_ice_candidate_add(_js_id, sdpMidName.utf8().get_data(), sdpMlineIndexName, sdpName.utf8().get_data());
  77. return OK;
  78. }
  79. Error WebRTCPeerConnectionJS::initialize(Dictionary p_config) {
  80. if (_js_id) {
  81. godot_js_rtc_pc_destroy(_js_id);
  82. _js_id = 0;
  83. }
  84. _conn_state = STATE_NEW;
  85. String config = Variant(p_config).to_json_string();
  86. _js_id = godot_js_rtc_pc_create(config.utf8().get_data(), this, &_on_connection_state_changed, &_on_ice_candidate, &_on_data_channel);
  87. return _js_id ? OK : FAILED;
  88. }
  89. Ref<WebRTCDataChannel> WebRTCPeerConnectionJS::create_data_channel(String p_channel, Dictionary p_channel_config) {
  90. ERR_FAIL_COND_V(_conn_state != STATE_NEW, nullptr);
  91. String config = Variant(p_channel_config).to_json_string();
  92. int id = godot_js_rtc_pc_datachannel_create(_js_id, p_channel.utf8().get_data(), config.utf8().get_data());
  93. ERR_FAIL_COND_V(id == 0, nullptr);
  94. return memnew(WebRTCDataChannelJS(id));
  95. }
  96. Error WebRTCPeerConnectionJS::poll() {
  97. return OK;
  98. }
  99. WebRTCPeerConnection::ConnectionState WebRTCPeerConnectionJS::get_connection_state() const {
  100. return _conn_state;
  101. }
  102. WebRTCPeerConnectionJS::WebRTCPeerConnectionJS() {
  103. _conn_state = STATE_NEW;
  104. _js_id = 0;
  105. Dictionary config;
  106. initialize(config);
  107. }
  108. WebRTCPeerConnectionJS::~WebRTCPeerConnectionJS() {
  109. close();
  110. if (_js_id) {
  111. godot_js_rtc_pc_destroy(_js_id);
  112. _js_id = 0;
  113. }
  114. };
  115. #endif