camera_linux.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. DIR *devices = opendir("/dev");
  58. if (devices) {
  59. struct dirent *device;
  60. while ((device = readdir(devices)) != nullptr) {
  61. if (strncmp(device->d_name, "video", 5) != 0) {
  62. continue;
  63. }
  64. String device_name = String("/dev/") + String(device->d_name);
  65. if (!_has_device(device_name)) {
  66. _add_device(device_name);
  67. }
  68. }
  69. }
  70. closedir(devices);
  71. }
  72. usleep(1000000);
  73. }
  74. }
  75. bool CameraLinux::_has_device(const String &p_device_name) {
  76. for (int i = 0; i < feeds.size(); i++) {
  77. Ref<CameraFeedLinux> feed = (Ref<CameraFeedLinux>)feeds[i];
  78. if (feed.is_null()) {
  79. continue;
  80. }
  81. if (feed->get_device_name() == p_device_name) {
  82. return true;
  83. }
  84. }
  85. return false;
  86. }
  87. void CameraLinux::_add_device(const String &p_device_name) {
  88. int file_descriptor = _open_device(p_device_name);
  89. if (file_descriptor != -1) {
  90. if (_is_video_capture_device(file_descriptor)) {
  91. Ref<CameraFeedLinux> feed = memnew(CameraFeedLinux(p_device_name));
  92. add_feed(feed);
  93. }
  94. }
  95. close(file_descriptor);
  96. }
  97. int CameraLinux::_open_device(const String &p_device_name) {
  98. struct stat s;
  99. if (stat(p_device_name.ascii(), &s) == -1) {
  100. return -1;
  101. }
  102. if (!S_ISCHR(s.st_mode)) {
  103. return -1;
  104. }
  105. return open(p_device_name.ascii(), O_RDWR | O_NONBLOCK, 0);
  106. }
  107. // TODO any cheaper/cleaner way to check if file descriptor is invalid?
  108. bool CameraLinux::_is_active(const String &p_device_name) {
  109. struct v4l2_capability capability;
  110. bool result = false;
  111. int file_descriptor = _open_device(p_device_name);
  112. if (file_descriptor != -1 && ioctl(file_descriptor, VIDIOC_QUERYCAP, &capability) != -1) {
  113. result = true;
  114. }
  115. close(file_descriptor);
  116. return result;
  117. }
  118. bool CameraLinux::_is_video_capture_device(int p_file_descriptor) {
  119. struct v4l2_capability capability;
  120. if (ioctl(p_file_descriptor, VIDIOC_QUERYCAP, &capability) == -1) {
  121. return false;
  122. }
  123. if (!(capability.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  124. return false;
  125. }
  126. if (!(capability.capabilities & V4L2_CAP_STREAMING)) {
  127. return false;
  128. }
  129. return _can_query_format(p_file_descriptor, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  130. }
  131. bool CameraLinux::_can_query_format(int p_file_descriptor, int p_type) {
  132. struct v4l2_format format;
  133. memset(&format, 0, sizeof(format));
  134. format.type = p_type;
  135. return ioctl(p_file_descriptor, VIDIOC_G_FMT, &format) != -1;
  136. }
  137. CameraLinux::CameraLinux() {
  138. camera_thread.start(CameraLinux::camera_thread_func, this);
  139. }
  140. CameraLinux::~CameraLinux() {
  141. exit_flag.set();
  142. camera_thread.wait_to_finish();
  143. }