camera_linux.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**************************************************************************/
  2. /* camera_linux.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_linux.h"
  31. #include "camera_feed_linux.h"
  32. #include <dirent.h>
  33. #include <fcntl.h>
  34. #include <sys/ioctl.h>
  35. #include <sys/stat.h>
  36. #include <unistd.h>
  37. void CameraLinux::camera_thread_func(void *p_camera_linux) {
  38. if (p_camera_linux) {
  39. CameraLinux *camera_linux = (CameraLinux *)p_camera_linux;
  40. camera_linux->_update_devices();
  41. }
  42. }
  43. void CameraLinux::_update_devices() {
  44. while (!exit_flag.is_set()) {
  45. {
  46. MutexLock lock(camera_mutex);
  47. for (int i = feeds.size() - 1; i >= 0; i--) {
  48. Ref<CameraFeedLinux> feed = (Ref<CameraFeedLinux>)feeds[i];
  49. if (feed.is_null()) {
  50. continue;
  51. }
  52. String device_name = feed->get_device_name();
  53. if (!_is_active(device_name)) {
  54. remove_feed(feed);
  55. }
  56. }
  57. struct dirent **devices;
  58. int count = scandir("/dev", &devices, nullptr, alphasort);
  59. if (count != -1) {
  60. for (int i = 0; i < count; i++) {
  61. struct dirent *device = devices[i];
  62. if (strncmp(device->d_name, "video", 5) == 0) {
  63. String device_name = String("/dev/") + String(device->d_name);
  64. if (!_has_device(device_name)) {
  65. _add_device(device_name);
  66. }
  67. }
  68. free(device);
  69. }
  70. }
  71. free(devices);
  72. }
  73. call_deferred("emit_signal", SNAME(CameraServer::feeds_updated_signal_name));
  74. usleep(1000000);
  75. }
  76. }
  77. bool CameraLinux::_has_device(const String &p_device_name) {
  78. for (int i = 0; i < feeds.size(); i++) {
  79. Ref<CameraFeedLinux> feed = (Ref<CameraFeedLinux>)feeds[i];
  80. if (feed.is_null()) {
  81. continue;
  82. }
  83. if (feed->get_device_name() == p_device_name) {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. void CameraLinux::_add_device(const String &p_device_name) {
  90. int file_descriptor = _open_device(p_device_name);
  91. if (file_descriptor != -1) {
  92. if (_is_video_capture_device(file_descriptor)) {
  93. Ref<CameraFeedLinux> feed = memnew(CameraFeedLinux(p_device_name));
  94. add_feed(feed);
  95. }
  96. }
  97. close(file_descriptor);
  98. }
  99. int CameraLinux::_open_device(const String &p_device_name) {
  100. struct stat s;
  101. if (stat(p_device_name.ascii().get_data(), &s) == -1) {
  102. return -1;
  103. }
  104. if (!S_ISCHR(s.st_mode)) {
  105. return -1;
  106. }
  107. return open(p_device_name.ascii().get_data(), O_RDWR | O_NONBLOCK, 0);
  108. }
  109. // TODO any cheaper/cleaner way to check if file descriptor is invalid?
  110. bool CameraLinux::_is_active(const String &p_device_name) {
  111. struct v4l2_capability capability;
  112. bool result = false;
  113. int file_descriptor = _open_device(p_device_name);
  114. if (file_descriptor != -1 && ioctl(file_descriptor, VIDIOC_QUERYCAP, &capability) != -1) {
  115. result = true;
  116. }
  117. close(file_descriptor);
  118. return result;
  119. }
  120. bool CameraLinux::_is_video_capture_device(int p_file_descriptor) {
  121. struct v4l2_capability capability;
  122. if (ioctl(p_file_descriptor, VIDIOC_QUERYCAP, &capability) == -1) {
  123. return false;
  124. }
  125. if (!(capability.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  126. return false;
  127. }
  128. if (!(capability.capabilities & V4L2_CAP_STREAMING)) {
  129. return false;
  130. }
  131. return _can_query_format(p_file_descriptor, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  132. }
  133. bool CameraLinux::_can_query_format(int p_file_descriptor, int p_type) {
  134. struct v4l2_format format;
  135. memset(&format, 0, sizeof(format));
  136. format.type = p_type;
  137. return ioctl(p_file_descriptor, VIDIOC_G_FMT, &format) != -1;
  138. }
  139. inline void CameraLinux::set_monitoring_feeds(bool p_monitoring_feeds) {
  140. if (p_monitoring_feeds == monitoring_feeds) {
  141. return;
  142. }
  143. CameraServer::set_monitoring_feeds(p_monitoring_feeds);
  144. if (p_monitoring_feeds) {
  145. exit_flag.clear();
  146. camera_thread.start(CameraLinux::camera_thread_func, this);
  147. } else {
  148. exit_flag.set();
  149. if (camera_thread.is_started()) {
  150. camera_thread.wait_to_finish();
  151. }
  152. }
  153. }
  154. CameraLinux::~CameraLinux() {
  155. exit_flag.set();
  156. if (camera_thread.is_started()) {
  157. camera_thread.wait_to_finish();
  158. }
  159. }