camera_server.cpp 5.7 KB

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