camera_server.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*************************************************************************/
  2. /* camera_server.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #include "camera_server.h"
  31. #include "servers/camera/camera_feed.h"
  32. #include "visual_server.h"
  33. ////////////////////////////////////////////////////////
  34. // CameraServer
  35. CameraServer::CreateFunc CameraServer::create_func = nullptr;
  36. void CameraServer::_bind_methods() {
  37. ClassDB::bind_method(D_METHOD("get_feed", "index"), &CameraServer::get_feed);
  38. ClassDB::bind_method(D_METHOD("get_feed_count"), &CameraServer::get_feed_count);
  39. ClassDB::bind_method(D_METHOD("feeds"), &CameraServer::get_feeds);
  40. ClassDB::bind_method(D_METHOD("add_feed", "feed"), &CameraServer::add_feed);
  41. ClassDB::bind_method(D_METHOD("remove_feed", "feed"), &CameraServer::remove_feed);
  42. ADD_SIGNAL(MethodInfo("camera_feed_added", PropertyInfo(Variant::INT, "id")));
  43. ADD_SIGNAL(MethodInfo("camera_feed_removed", PropertyInfo(Variant::INT, "id")));
  44. BIND_ENUM_CONSTANT(FEED_RGBA_IMAGE);
  45. BIND_ENUM_CONSTANT(FEED_YCBCR_IMAGE);
  46. BIND_ENUM_CONSTANT(FEED_Y_IMAGE);
  47. BIND_ENUM_CONSTANT(FEED_CBCR_IMAGE);
  48. };
  49. CameraServer *CameraServer::singleton = nullptr;
  50. CameraServer *CameraServer::get_singleton() {
  51. return singleton;
  52. };
  53. int CameraServer::get_free_id() {
  54. bool id_exists = true;
  55. int newid = 0;
  56. // find a free id
  57. while (id_exists) {
  58. newid++;
  59. id_exists = false;
  60. for (int i = 0; i < feeds.size() && !id_exists; i++) {
  61. if (feeds[i]->get_id() == newid) {
  62. id_exists = true;
  63. };
  64. };
  65. };
  66. return newid;
  67. };
  68. int CameraServer::get_feed_index(int p_id) {
  69. for (int i = 0; i < feeds.size(); i++) {
  70. if (feeds[i]->get_id() == p_id) {
  71. return i;
  72. };
  73. };
  74. return -1;
  75. };
  76. Ref<CameraFeed> CameraServer::get_feed_by_id(int p_id) {
  77. int index = get_feed_index(p_id);
  78. if (index == -1) {
  79. return nullptr;
  80. } else {
  81. return feeds[index];
  82. }
  83. };
  84. void CameraServer::add_feed(const Ref<CameraFeed> &p_feed) {
  85. ERR_FAIL_COND(p_feed.is_null());
  86. // add our feed
  87. feeds.push_back(p_feed);
  88. // record for debugging
  89. #ifdef DEBUG_ENABLED
  90. print_line("Registered camera " + p_feed->get_name() + " with id " + itos(p_feed->get_id()) + " position " + itos(p_feed->get_position()) + " at index " + itos(feeds.size() - 1));
  91. #endif
  92. // let whomever is interested know
  93. emit_signal("camera_feed_added", p_feed->get_id());
  94. };
  95. void CameraServer::remove_feed(const Ref<CameraFeed> &p_feed) {
  96. for (int i = 0; i < feeds.size(); i++) {
  97. if (feeds[i] == p_feed) {
  98. int feed_id = p_feed->get_id();
  99. // record for debugging
  100. #ifdef DEBUG_ENABLED
  101. print_line("Removed camera " + p_feed->get_name() + " with id " + itos(feed_id) + " position " + itos(p_feed->get_position()));
  102. #endif
  103. // remove it from our array, if this results in our feed being unreferenced it will be destroyed
  104. feeds.remove(i);
  105. // let whomever is interested know
  106. emit_signal("camera_feed_removed", feed_id);
  107. return;
  108. };
  109. };
  110. };
  111. Ref<CameraFeed> CameraServer::get_feed(int p_index) {
  112. ERR_FAIL_INDEX_V(p_index, feeds.size(), nullptr);
  113. return feeds[p_index];
  114. };
  115. int CameraServer::get_feed_count() {
  116. return feeds.size();
  117. };
  118. Array CameraServer::get_feeds() {
  119. Array return_feeds;
  120. int cc = get_feed_count();
  121. return_feeds.resize(cc);
  122. for (int i = 0; i < feeds.size(); i++) {
  123. return_feeds[i] = get_feed(i);
  124. };
  125. return return_feeds;
  126. };
  127. RID CameraServer::feed_texture(int p_id, CameraServer::FeedImage p_texture) {
  128. int index = get_feed_index(p_id);
  129. ERR_FAIL_COND_V(index == -1, RID());
  130. Ref<CameraFeed> feed = get_feed(index);
  131. return feed->get_texture(p_texture);
  132. };
  133. CameraServer::CameraServer() {
  134. singleton = this;
  135. };
  136. CameraServer::~CameraServer() {
  137. singleton = nullptr;
  138. };