camera_linux.cpp 5.5 KB

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