camera_linux.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. usleep(1000000);
  74. }
  75. }
  76. bool CameraLinux::_has_device(const String &p_device_name) {
  77. for (int i = 0; i < feeds.size(); i++) {
  78. Ref<CameraFeedLinux> feed = (Ref<CameraFeedLinux>)feeds[i];
  79. if (feed.is_null()) {
  80. continue;
  81. }
  82. if (feed->get_device_name() == p_device_name) {
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. void CameraLinux::_add_device(const String &p_device_name) {
  89. int file_descriptor = _open_device(p_device_name);
  90. if (file_descriptor != -1) {
  91. if (_is_video_capture_device(file_descriptor)) {
  92. Ref<CameraFeedLinux> feed = memnew(CameraFeedLinux(p_device_name));
  93. add_feed(feed);
  94. }
  95. }
  96. close(file_descriptor);
  97. }
  98. int CameraLinux::_open_device(const String &p_device_name) {
  99. struct stat s;
  100. if (stat(p_device_name.ascii().get_data(), &s) == -1) {
  101. return -1;
  102. }
  103. if (!S_ISCHR(s.st_mode)) {
  104. return -1;
  105. }
  106. return open(p_device_name.ascii().get_data(), O_RDWR | O_NONBLOCK, 0);
  107. }
  108. // TODO any cheaper/cleaner way to check if file descriptor is invalid?
  109. bool CameraLinux::_is_active(const String &p_device_name) {
  110. struct v4l2_capability capability;
  111. bool result = false;
  112. int file_descriptor = _open_device(p_device_name);
  113. if (file_descriptor != -1 && ioctl(file_descriptor, VIDIOC_QUERYCAP, &capability) != -1) {
  114. result = true;
  115. }
  116. close(file_descriptor);
  117. return result;
  118. }
  119. bool CameraLinux::_is_video_capture_device(int p_file_descriptor) {
  120. struct v4l2_capability capability;
  121. if (ioctl(p_file_descriptor, VIDIOC_QUERYCAP, &capability) == -1) {
  122. return false;
  123. }
  124. if (!(capability.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  125. return false;
  126. }
  127. if (!(capability.capabilities & V4L2_CAP_STREAMING)) {
  128. return false;
  129. }
  130. return _can_query_format(p_file_descriptor, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  131. }
  132. bool CameraLinux::_can_query_format(int p_file_descriptor, int p_type) {
  133. struct v4l2_format format;
  134. memset(&format, 0, sizeof(format));
  135. format.type = p_type;
  136. return ioctl(p_file_descriptor, VIDIOC_G_FMT, &format) != -1;
  137. }
  138. inline void CameraLinux::set_monitoring_feeds(bool p_monitoring_feeds) {
  139. if (p_monitoring_feeds == monitoring_feeds) {
  140. return;
  141. }
  142. CameraServer::set_monitoring_feeds(p_monitoring_feeds);
  143. if (p_monitoring_feeds) {
  144. camera_thread.start(CameraLinux::camera_thread_func, this);
  145. } else {
  146. exit_flag.set();
  147. if (camera_thread.is_started()) {
  148. camera_thread.wait_to_finish();
  149. }
  150. }
  151. }
  152. CameraLinux::~CameraLinux() {
  153. exit_flag.set();
  154. if (camera_thread.is_started()) {
  155. camera_thread.wait_to_finish();
  156. }
  157. }