webrtc_peer_connection.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**************************************************************************/
  2. /* webrtc_peer_connection.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.h"
  31. #ifdef WEB_ENABLED
  32. #include "webrtc_peer_connection_js.h"
  33. #endif
  34. #include "webrtc_peer_connection_extension.h"
  35. StringName WebRTCPeerConnection::default_extension;
  36. void WebRTCPeerConnection::set_default_extension(const StringName &p_extension) {
  37. ERR_FAIL_COND_MSG(!ClassDB::is_parent_class(p_extension, WebRTCPeerConnectionExtension::get_class_static()), vformat("Can't make %s the default WebRTC extension since it does not extend WebRTCPeerConnectionExtension.", p_extension));
  38. default_extension = StringName(p_extension, true);
  39. }
  40. WebRTCPeerConnection *WebRTCPeerConnection::create(bool p_notify_postinitialize) {
  41. #ifdef WEB_ENABLED
  42. return static_cast<WebRTCPeerConnection *>(ClassDB::creator<WebRTCPeerConnectionJS>(p_notify_postinitialize));
  43. #else
  44. if (default_extension == StringName()) {
  45. WARN_PRINT_ONCE("No default WebRTC extension configured.");
  46. return static_cast<WebRTCPeerConnection *>(ClassDB::creator<WebRTCPeerConnectionExtension>(p_notify_postinitialize));
  47. }
  48. Object *obj = nullptr;
  49. if (p_notify_postinitialize) {
  50. obj = ClassDB::instantiate(default_extension);
  51. } else {
  52. obj = ClassDB::instantiate_without_postinitialization(default_extension);
  53. }
  54. return Object::cast_to<WebRTCPeerConnectionExtension>(obj);
  55. #endif
  56. }
  57. void WebRTCPeerConnection::_bind_methods() {
  58. ClassDB::bind_static_method(get_class_static(), D_METHOD("set_default_extension", "extension_class"), &WebRTCPeerConnectionExtension::set_default_extension);
  59. ClassDB::bind_method(D_METHOD("initialize", "configuration"), &WebRTCPeerConnection::initialize, DEFVAL(Dictionary()));
  60. ClassDB::bind_method(D_METHOD("create_data_channel", "label", "options"), &WebRTCPeerConnection::create_data_channel, DEFVAL(Dictionary()));
  61. ClassDB::bind_method(D_METHOD("create_offer"), &WebRTCPeerConnection::create_offer);
  62. ClassDB::bind_method(D_METHOD("set_local_description", "type", "sdp"), &WebRTCPeerConnection::set_local_description);
  63. ClassDB::bind_method(D_METHOD("set_remote_description", "type", "sdp"), &WebRTCPeerConnection::set_remote_description);
  64. ClassDB::bind_method(D_METHOD("add_ice_candidate", "media", "index", "name"), &WebRTCPeerConnection::add_ice_candidate);
  65. ClassDB::bind_method(D_METHOD("poll"), &WebRTCPeerConnection::poll);
  66. ClassDB::bind_method(D_METHOD("close"), &WebRTCPeerConnection::close);
  67. ClassDB::bind_method(D_METHOD("get_connection_state"), &WebRTCPeerConnection::get_connection_state);
  68. ClassDB::bind_method(D_METHOD("get_gathering_state"), &WebRTCPeerConnection::get_gathering_state);
  69. ClassDB::bind_method(D_METHOD("get_signaling_state"), &WebRTCPeerConnection::get_signaling_state);
  70. ADD_SIGNAL(MethodInfo("session_description_created", PropertyInfo(Variant::STRING, "type"), PropertyInfo(Variant::STRING, "sdp")));
  71. ADD_SIGNAL(MethodInfo("ice_candidate_created", PropertyInfo(Variant::STRING, "media"), PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::STRING, "name")));
  72. ADD_SIGNAL(MethodInfo("data_channel_received", PropertyInfo(Variant::OBJECT, "channel", PROPERTY_HINT_RESOURCE_TYPE, "WebRTCDataChannel")));
  73. BIND_ENUM_CONSTANT(STATE_NEW);
  74. BIND_ENUM_CONSTANT(STATE_CONNECTING);
  75. BIND_ENUM_CONSTANT(STATE_CONNECTED);
  76. BIND_ENUM_CONSTANT(STATE_DISCONNECTED);
  77. BIND_ENUM_CONSTANT(STATE_FAILED);
  78. BIND_ENUM_CONSTANT(STATE_CLOSED);
  79. BIND_ENUM_CONSTANT(GATHERING_STATE_NEW);
  80. BIND_ENUM_CONSTANT(GATHERING_STATE_GATHERING);
  81. BIND_ENUM_CONSTANT(GATHERING_STATE_COMPLETE);
  82. BIND_ENUM_CONSTANT(SIGNALING_STATE_STABLE);
  83. BIND_ENUM_CONSTANT(SIGNALING_STATE_HAVE_LOCAL_OFFER);
  84. BIND_ENUM_CONSTANT(SIGNALING_STATE_HAVE_REMOTE_OFFER);
  85. BIND_ENUM_CONSTANT(SIGNALING_STATE_HAVE_LOCAL_PRANSWER);
  86. BIND_ENUM_CONSTANT(SIGNALING_STATE_HAVE_REMOTE_PRANSWER);
  87. BIND_ENUM_CONSTANT(SIGNALING_STATE_CLOSED);
  88. }
  89. WebRTCPeerConnection::WebRTCPeerConnection() {
  90. }
  91. WebRTCPeerConnection::~WebRTCPeerConnection() {
  92. }