1
0

v4l2-udev.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. Copyright (C) 2014 by Leonhard Oelke <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <sys/eventfd.h>
  15. #include <poll.h>
  16. #include <unistd.h>
  17. #include <libudev.h>
  18. #include <util/threading.h>
  19. #include <util/darray.h>
  20. #include <obs.h>
  21. #include "v4l2-udev.h"
  22. /** udev action enum */
  23. enum udev_action {
  24. UDEV_ACTION_ADDED,
  25. UDEV_ACTION_REMOVED,
  26. UDEV_ACTION_UNKNOWN
  27. };
  28. static const char *udev_signals[] = {"void device_added(string device)",
  29. "void device_removed(string device)",
  30. NULL};
  31. /* global data */
  32. static uint_fast32_t udev_refs = 0;
  33. static pthread_mutex_t udev_mutex = PTHREAD_MUTEX_INITIALIZER;
  34. static pthread_t udev_thread;
  35. static os_event_t *udev_event;
  36. static int udev_event_fd;
  37. static signal_handler_t *udev_signalhandler = NULL;
  38. /**
  39. * udev gives us the device action as string, so we convert it here ...
  40. *
  41. * @param action the udev action as string
  42. * @return the udev action as enum value
  43. */
  44. static enum udev_action udev_action_to_enum(const char *action)
  45. {
  46. if (!action)
  47. return UDEV_ACTION_UNKNOWN;
  48. if (!strncmp("add", action, 3))
  49. return UDEV_ACTION_ADDED;
  50. if (!strncmp("remove", action, 6))
  51. return UDEV_ACTION_REMOVED;
  52. return UDEV_ACTION_UNKNOWN;
  53. }
  54. /**
  55. * Call all registered callbacks with the event
  56. *
  57. * @param dev udev device that had an event occuring
  58. */
  59. static inline void udev_signal_event(struct udev_device *dev)
  60. {
  61. const char *node;
  62. enum udev_action action;
  63. struct calldata data;
  64. pthread_mutex_lock(&udev_mutex);
  65. node = udev_device_get_devnode(dev);
  66. action = udev_action_to_enum(udev_device_get_action(dev));
  67. calldata_init(&data);
  68. calldata_set_string(&data, "device", node);
  69. switch (action) {
  70. case UDEV_ACTION_ADDED:
  71. signal_handler_signal(udev_signalhandler, "device_added",
  72. &data);
  73. break;
  74. case UDEV_ACTION_REMOVED:
  75. signal_handler_signal(udev_signalhandler, "device_removed",
  76. &data);
  77. break;
  78. default:
  79. break;
  80. }
  81. calldata_free(&data);
  82. pthread_mutex_unlock(&udev_mutex);
  83. }
  84. /**
  85. * Event listener thread
  86. */
  87. static void *udev_event_thread(void *vptr)
  88. {
  89. UNUSED_PARAMETER(vptr);
  90. int fd;
  91. struct udev *udev;
  92. struct udev_monitor *mon;
  93. struct udev_device *dev;
  94. /* set up udev monitoring */
  95. os_set_thread_name("v4l2: udev");
  96. udev = udev_new();
  97. mon = udev_monitor_new_from_netlink(udev, "udev");
  98. udev_monitor_filter_add_match_subsystem_devtype(mon, "video4linux",
  99. NULL);
  100. if (udev_monitor_enable_receiving(mon) < 0)
  101. return NULL;
  102. /* set up fds */
  103. fd = udev_monitor_get_fd(mon);
  104. while (os_event_try(udev_event) == EAGAIN) {
  105. struct pollfd fds[2];
  106. fds[0].fd = fd;
  107. fds[0].events = POLLIN;
  108. fds[0].revents = 0;
  109. fds[1].fd = udev_event_fd;
  110. fds[1].events = POLLIN;
  111. if (poll(fds, 2, 1000) <= 0)
  112. continue;
  113. if (!(fds[0].revents & POLLIN))
  114. continue;
  115. dev = udev_monitor_receive_device(mon);
  116. if (!dev)
  117. continue;
  118. udev_signal_event(dev);
  119. udev_device_unref(dev);
  120. }
  121. udev_monitor_unref(mon);
  122. udev_unref(udev);
  123. return NULL;
  124. }
  125. void v4l2_init_udev(void)
  126. {
  127. pthread_mutex_lock(&udev_mutex);
  128. /* set up udev */
  129. if (udev_refs == 0) {
  130. if (os_event_init(&udev_event, OS_EVENT_TYPE_MANUAL) != 0)
  131. goto fail;
  132. if ((udev_event_fd = eventfd(0, EFD_CLOEXEC)) < 0)
  133. goto fail;
  134. if (pthread_create(&udev_thread, NULL, udev_event_thread,
  135. NULL) != 0) {
  136. close(udev_event_fd);
  137. goto fail;
  138. }
  139. udev_signalhandler = signal_handler_create();
  140. if (!udev_signalhandler) {
  141. close(udev_event_fd);
  142. goto fail;
  143. }
  144. signal_handler_add_array(udev_signalhandler, udev_signals);
  145. }
  146. udev_refs++;
  147. fail:
  148. pthread_mutex_unlock(&udev_mutex);
  149. }
  150. void v4l2_unref_udev(void)
  151. {
  152. pthread_mutex_lock(&udev_mutex);
  153. /* unref udev monitor */
  154. if (udev_refs && --udev_refs == 0) {
  155. os_event_signal(udev_event);
  156. eventfd_write(udev_event_fd, 1);
  157. pthread_join(udev_thread, NULL);
  158. os_event_destroy(udev_event);
  159. close(udev_event_fd);
  160. if (udev_signalhandler)
  161. signal_handler_destroy(udev_signalhandler);
  162. udev_signalhandler = NULL;
  163. }
  164. pthread_mutex_unlock(&udev_mutex);
  165. }
  166. signal_handler_t *v4l2_get_udev_signalhandler(void)
  167. {
  168. return udev_signalhandler;
  169. }