v4l2-controls.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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(__i386__)
  20. #define UINT_TO_POINTER(val) ((void *)(unsigned int)(val))
  21. #define POINTER_TO_UINT(p) ((unsigned int)(unsigned int)(p))
  22. #elif defined(__x86_64__)
  23. #define UINT_TO_POINTER(val) ((void *)(unsigned long)(val))
  24. #define POINTER_TO_UINT(p) ((unsigned int)(unsigned long)(p))
  25. #else
  26. #error "unknown platform, this code won't work"
  27. #endif
  28. static bool v4l2_control_changed(void *data, obs_properties_t *props,
  29. obs_property_t *prop, obs_data_t *settings)
  30. {
  31. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  32. O_RDWR | O_NONBLOCK);
  33. bool ret = false;
  34. (void)props;
  35. if (dev == -1)
  36. return false;
  37. struct v4l2_control control;
  38. control.id = POINTER_TO_UINT(data);
  39. control.value = obs_data_get_int(settings, obs_property_name(prop));
  40. if (0 != v4l2_ioctl(dev, VIDIOC_S_CTRL, &control)) {
  41. ret = true;
  42. }
  43. v4l2_close(dev);
  44. return ret;
  45. }
  46. static int_fast32_t v4l2_update_controls_menu(int_fast32_t dev,
  47. obs_properties_t *props,
  48. struct v4l2_queryctrl *qctrl)
  49. {
  50. obs_property_t *prop;
  51. struct v4l2_querymenu qmenu;
  52. prop = obs_properties_add_list(props, (char *)qctrl->name,
  53. (char *)qctrl->name, OBS_COMBO_TYPE_LIST,
  54. OBS_COMBO_FORMAT_INT);
  55. obs_property_set_modified_callback2(prop, v4l2_control_changed,
  56. UINT_TO_POINTER(qctrl->id));
  57. memset(&qmenu, 0, sizeof(qmenu));
  58. qmenu.id = qctrl->id;
  59. for (qmenu.index = qctrl->minimum;
  60. qmenu.index <= (uint32_t)qctrl->maximum;
  61. qmenu.index += qctrl->step) {
  62. if (0 == v4l2_ioctl(dev, VIDIOC_QUERYMENU, &qmenu)) {
  63. obs_property_list_add_int(prop, (char *)qmenu.name,
  64. qmenu.value);
  65. }
  66. }
  67. return 0;
  68. }
  69. int_fast32_t v4l2_update_controls(int_fast32_t dev, obs_properties_t *props,
  70. obs_data_t *settings)
  71. {
  72. struct v4l2_queryctrl qctrl;
  73. obs_property_t *prop = NULL;
  74. if (!dev || !props)
  75. return -1;
  76. memset(&qctrl, 0, sizeof(qctrl));
  77. qctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
  78. while (0 == v4l2_ioctl(dev, VIDIOC_QUERYCTRL, &qctrl)) {
  79. if (qctrl.flags & V4L2_CTRL_FLAG_DISABLED) {
  80. blog(LOG_INFO, "found control %s but it is disabled",
  81. qctrl.name);
  82. continue;
  83. }
  84. if (qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY) {
  85. blog(LOG_INFO, "found control %s but it is readonly",
  86. qctrl.name);
  87. continue;
  88. }
  89. if (qctrl.flags & V4L2_CTRL_FLAG_VOLATILE) {
  90. blog(LOG_INFO, "found control %s but it is volatile",
  91. qctrl.name);
  92. continue;
  93. }
  94. switch (qctrl.type) {
  95. case V4L2_CTRL_TYPE_INTEGER:
  96. prop = obs_properties_add_int_slider(
  97. props, (char *)qctrl.name, (char *)qctrl.name,
  98. qctrl.minimum, qctrl.maximum, qctrl.step);
  99. obs_data_set_default_int(settings, (char *)qctrl.name,
  100. qctrl.default_value);
  101. obs_property_set_modified_callback2(
  102. prop, v4l2_control_changed,
  103. UINT_TO_POINTER(qctrl.id));
  104. break;
  105. case V4L2_CTRL_TYPE_BOOLEAN:
  106. prop = obs_properties_add_bool(
  107. props, (char *)qctrl.name, (char *)qctrl.name);
  108. obs_data_set_default_bool(settings, (char *)qctrl.name,
  109. qctrl.default_value);
  110. obs_property_set_modified_callback2(
  111. prop, v4l2_control_changed,
  112. UINT_TO_POINTER(qctrl.id));
  113. break;
  114. case V4L2_CTRL_TYPE_MENU:
  115. case V4L2_CTRL_TYPE_INTEGER_MENU:
  116. v4l2_update_controls_menu(dev, props, &qctrl);
  117. obs_data_set_default_int(settings, (char *)qctrl.name,
  118. qctrl.default_value);
  119. blog(LOG_INFO, "setting default for %s to %d",
  120. (char *)qctrl.name, qctrl.default_value);
  121. break;
  122. }
  123. qctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
  124. }
  125. return 0;
  126. }