webrtc_peer_connection_gdnative.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**************************************************************************/
  2. /* webrtc_peer_connection_gdnative.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 WEBRTC_GDNATIVE_ENABLED
  31. #include "webrtc_peer_connection_gdnative.h"
  32. #include "core/io/resource_loader.h"
  33. #include "modules/gdnative/nativescript/nativescript.h"
  34. #include "webrtc_data_channel_gdnative.h"
  35. const godot_net_webrtc_library *WebRTCPeerConnectionGDNative::default_library = nullptr;
  36. Error WebRTCPeerConnectionGDNative::set_default_library(const godot_net_webrtc_library *p_lib) {
  37. if (default_library) {
  38. const godot_net_webrtc_library *old = default_library;
  39. default_library = nullptr;
  40. old->unregistered();
  41. }
  42. default_library = p_lib;
  43. return OK; // Maybe add version check and fail accordingly
  44. }
  45. WebRTCPeerConnection *WebRTCPeerConnectionGDNative::_create() {
  46. WebRTCPeerConnectionGDNative *obj = memnew(WebRTCPeerConnectionGDNative);
  47. ERR_FAIL_COND_V_MSG(!default_library, obj, "Default GDNative WebRTC implementation not defined.");
  48. // Call GDNative constructor
  49. Error err = (Error)default_library->create_peer_connection(obj);
  50. ERR_FAIL_COND_V_MSG(err != OK, obj, "GDNative default library constructor returned an error.");
  51. return obj;
  52. }
  53. void WebRTCPeerConnectionGDNative::_bind_methods() {
  54. }
  55. WebRTCPeerConnectionGDNative::WebRTCPeerConnectionGDNative() {
  56. interface = nullptr;
  57. }
  58. WebRTCPeerConnectionGDNative::~WebRTCPeerConnectionGDNative() {
  59. }
  60. Error WebRTCPeerConnectionGDNative::initialize(Dictionary p_config) {
  61. ERR_FAIL_COND_V(interface == nullptr, ERR_UNCONFIGURED);
  62. return (Error)interface->initialize(interface->data, (const godot_dictionary *)&p_config);
  63. }
  64. Ref<WebRTCDataChannel> WebRTCPeerConnectionGDNative::create_data_channel(String p_label, Dictionary p_options) {
  65. ERR_FAIL_COND_V(interface == nullptr, nullptr);
  66. return (WebRTCDataChannel *)interface->create_data_channel(interface->data, p_label.utf8().get_data(), (const godot_dictionary *)&p_options);
  67. }
  68. Error WebRTCPeerConnectionGDNative::create_offer() {
  69. ERR_FAIL_COND_V(interface == nullptr, ERR_UNCONFIGURED);
  70. return (Error)interface->create_offer(interface->data);
  71. }
  72. Error WebRTCPeerConnectionGDNative::set_local_description(String p_type, String p_sdp) {
  73. ERR_FAIL_COND_V(interface == nullptr, ERR_UNCONFIGURED);
  74. return (Error)interface->set_local_description(interface->data, p_type.utf8().get_data(), p_sdp.utf8().get_data());
  75. }
  76. Error WebRTCPeerConnectionGDNative::set_remote_description(String p_type, String p_sdp) {
  77. ERR_FAIL_COND_V(interface == nullptr, ERR_UNCONFIGURED);
  78. return (Error)interface->set_remote_description(interface->data, p_type.utf8().get_data(), p_sdp.utf8().get_data());
  79. }
  80. Error WebRTCPeerConnectionGDNative::add_ice_candidate(String sdpMidName, int sdpMlineIndexName, String sdpName) {
  81. ERR_FAIL_COND_V(interface == nullptr, ERR_UNCONFIGURED);
  82. return (Error)interface->add_ice_candidate(interface->data, sdpMidName.utf8().get_data(), sdpMlineIndexName, sdpName.utf8().get_data());
  83. }
  84. Error WebRTCPeerConnectionGDNative::poll() {
  85. ERR_FAIL_COND_V(interface == nullptr, ERR_UNCONFIGURED);
  86. return (Error)interface->poll(interface->data);
  87. }
  88. void WebRTCPeerConnectionGDNative::close() {
  89. ERR_FAIL_COND(interface == nullptr);
  90. interface->close(interface->data);
  91. }
  92. WebRTCPeerConnection::ConnectionState WebRTCPeerConnectionGDNative::get_connection_state() const {
  93. ERR_FAIL_COND_V(interface == nullptr, STATE_DISCONNECTED);
  94. return (ConnectionState)interface->get_connection_state(interface->data);
  95. }
  96. void WebRTCPeerConnectionGDNative::set_native_webrtc_peer_connection(const godot_net_webrtc_peer_connection *p_impl) {
  97. interface = p_impl;
  98. }
  99. #endif // WEBRTC_GDNATIVE_ENABLED