v4l2-controls.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. Copyright (C) 2019 by Gary Kramlich <[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 <fcntl.h>
  15. #include <linux/videodev2.h>
  16. #include <libv4l2.h>
  17. #include "v4l2-controls.h"
  18. #define blog(level, msg, ...) blog(level, "v4l2-controls: " msg, ##__VA_ARGS__)
  19. #if defined(__LP64__)
  20. #define UINT_TO_POINTER(val) ((void *)(unsigned long)(val))
  21. #define POINTER_TO_UINT(p) ((unsigned int)(unsigned long)(p))
  22. #else
  23. #define UINT_TO_POINTER(val) ((void *)(unsigned int)(val))
  24. #define POINTER_TO_UINT(p) ((unsigned int)(unsigned int)(p))
  25. #endif
  26. static bool v4l2_control_changed(void *data, obs_properties_t *props,
  27. obs_property_t *prop, obs_data_t *settings)
  28. {
  29. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  30. O_RDWR | O_NONBLOCK);
  31. bool ret = false;
  32. (void)props;
  33. if (dev == -1)
  34. return false;
  35. struct v4l2_control control;
  36. control.id = POINTER_TO_UINT(data);
  37. switch (obs_property_get_type(prop)) {
  38. case OBS_PROPERTY_BOOL:
  39. control.value =
  40. obs_data_get_bool(settings, obs_property_name(prop));
  41. break;
  42. case OBS_PROPERTY_INT:
  43. case OBS_PROPERTY_LIST:
  44. control.value =
  45. obs_data_get_int(settings, obs_property_name(prop));
  46. break;
  47. default:
  48. blog(LOG_ERROR, "unknown property type for %s",
  49. obs_property_name(prop));
  50. v4l2_close(dev);
  51. return ret;
  52. }
  53. if (0 != v4l2_ioctl(dev, VIDIOC_S_CTRL, &control)) {
  54. ret = true;
  55. }
  56. v4l2_close(dev);
  57. return ret;
  58. }
  59. static int_fast32_t v4l2_update_controls_menu(int_fast32_t dev,
  60. obs_properties_t *props,
  61. struct v4l2_queryctrl *qctrl)
  62. {
  63. obs_property_t *prop;
  64. struct v4l2_querymenu qmenu;
  65. prop = obs_properties_add_list(props, (char *)qctrl->name,
  66. (char *)qctrl->name, OBS_COMBO_TYPE_LIST,
  67. OBS_COMBO_FORMAT_INT);
  68. obs_property_set_modified_callback2(prop, v4l2_control_changed,
  69. UINT_TO_POINTER(qctrl->id));
  70. memset(&qmenu, 0, sizeof(qmenu));
  71. qmenu.id = qctrl->id;
  72. for (qmenu.index = qctrl->minimum;
  73. qmenu.index <= (uint32_t)qctrl->maximum;
  74. qmenu.index += qctrl->step) {
  75. if (0 == v4l2_ioctl(dev, VIDIOC_QUERYMENU, &qmenu)) {
  76. obs_property_list_add_int(prop, (char *)qmenu.name,
  77. qmenu.index);
  78. }
  79. }
  80. return 0;
  81. }
  82. #define INVALID_CONTROL_FLAGS \
  83. (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_READ_ONLY | \
  84. V4L2_CTRL_FLAG_VOLATILE)
  85. static inline bool valid_control(struct v4l2_queryctrl *qctrl)
  86. {
  87. return (qctrl->flags & INVALID_CONTROL_FLAGS) == 0;
  88. }
  89. static inline void add_control_property(obs_properties_t *props,
  90. obs_data_t *settings, int_fast32_t dev,
  91. struct v4l2_queryctrl *qctrl)
  92. {
  93. obs_property_t *prop = NULL;
  94. if (!valid_control(qctrl)) {
  95. return;
  96. }
  97. switch (qctrl->type) {
  98. case V4L2_CTRL_TYPE_INTEGER:
  99. prop = obs_properties_add_int_slider(
  100. props, (char *)qctrl->name, (char *)qctrl->name,
  101. qctrl->minimum, qctrl->maximum, qctrl->step);
  102. obs_data_set_default_int(settings, (char *)qctrl->name,
  103. qctrl->default_value);
  104. obs_property_set_modified_callback2(prop, v4l2_control_changed,
  105. UINT_TO_POINTER(qctrl->id));
  106. break;
  107. case V4L2_CTRL_TYPE_BOOLEAN:
  108. prop = obs_properties_add_bool(props, (char *)qctrl->name,
  109. (char *)qctrl->name);
  110. obs_data_set_default_bool(settings, (char *)qctrl->name,
  111. qctrl->default_value);
  112. obs_property_set_modified_callback2(prop, v4l2_control_changed,
  113. UINT_TO_POINTER(qctrl->id));
  114. break;
  115. case V4L2_CTRL_TYPE_MENU:
  116. case V4L2_CTRL_TYPE_INTEGER_MENU:
  117. v4l2_update_controls_menu(dev, props, qctrl);
  118. obs_data_set_default_int(settings, (char *)qctrl->name,
  119. qctrl->default_value);
  120. blog(LOG_INFO, "setting default for %s to %d",
  121. (char *)qctrl->name, qctrl->default_value);
  122. break;
  123. }
  124. }
  125. int_fast32_t v4l2_update_controls(int_fast32_t dev, obs_properties_t *props,
  126. obs_data_t *settings)
  127. {
  128. struct v4l2_queryctrl qctrl;
  129. if (!dev || !props)
  130. return -1;
  131. memset(&qctrl, 0, sizeof(qctrl));
  132. qctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
  133. while (0 == v4l2_ioctl(dev, VIDIOC_QUERYCTRL, &qctrl)) {
  134. add_control_property(props, settings, dev, &qctrl);
  135. qctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
  136. }
  137. return 0;
  138. }