v4l2-controls.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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, obs_property_t *prop, obs_data_t *settings)
  27. {
  28. int dev = v4l2_open(obs_data_get_string(settings, "device_id"), O_RDWR | O_NONBLOCK);
  29. bool ret = false;
  30. (void)props;
  31. if (dev == -1)
  32. return false;
  33. struct v4l2_control control;
  34. control.id = POINTER_TO_UINT(data);
  35. switch (obs_property_get_type(prop)) {
  36. case OBS_PROPERTY_BOOL:
  37. control.value = obs_data_get_bool(settings, obs_property_name(prop));
  38. break;
  39. case OBS_PROPERTY_INT:
  40. case OBS_PROPERTY_LIST:
  41. control.value = obs_data_get_int(settings, obs_property_name(prop));
  42. break;
  43. default:
  44. blog(LOG_ERROR, "unknown property type for %s", obs_property_name(prop));
  45. v4l2_close(dev);
  46. return ret;
  47. }
  48. if (0 != v4l2_ioctl(dev, VIDIOC_S_CTRL, &control)) {
  49. ret = true;
  50. }
  51. v4l2_close(dev);
  52. return ret;
  53. }
  54. static bool v4l2_update_controls_menu(int_fast32_t dev, obs_properties_t *props, struct v4l2_queryctrl *qctrl)
  55. {
  56. obs_property_t *prop;
  57. struct v4l2_querymenu qmenu;
  58. prop = obs_properties_add_list(props, (char *)qctrl->name, (char *)qctrl->name, OBS_COMBO_TYPE_LIST,
  59. OBS_COMBO_FORMAT_INT);
  60. obs_property_set_modified_callback2(prop, v4l2_control_changed, UINT_TO_POINTER(qctrl->id));
  61. memset(&qmenu, 0, sizeof(qmenu));
  62. qmenu.id = qctrl->id;
  63. for (qmenu.index = qctrl->minimum; qmenu.index <= (uint32_t)qctrl->maximum; qmenu.index += qctrl->step) {
  64. if (0 == v4l2_ioctl(dev, VIDIOC_QUERYMENU, &qmenu)) {
  65. obs_property_list_add_int(prop, (char *)qmenu.name, qmenu.index);
  66. }
  67. }
  68. if (obs_property_list_item_count(prop) == 0) {
  69. obs_properties_remove_by_name(props, (char *)qctrl->name);
  70. return false;
  71. }
  72. return true;
  73. }
  74. #define INVALID_CONTROL_FLAGS (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_VOLATILE)
  75. static inline bool valid_control(struct v4l2_queryctrl *qctrl)
  76. {
  77. return (qctrl->flags & INVALID_CONTROL_FLAGS) == 0;
  78. }
  79. static inline void add_control_property(obs_properties_t *props, obs_data_t *settings, int_fast32_t dev,
  80. struct v4l2_queryctrl *qctrl)
  81. {
  82. obs_property_t *prop = NULL;
  83. if (!valid_control(qctrl)) {
  84. return;
  85. }
  86. switch (qctrl->type) {
  87. case V4L2_CTRL_TYPE_INTEGER:
  88. prop = obs_properties_add_int_slider(props, (char *)qctrl->name, (char *)qctrl->name, qctrl->minimum,
  89. qctrl->maximum, qctrl->step);
  90. obs_data_set_default_int(settings, (char *)qctrl->name, qctrl->default_value);
  91. obs_property_set_modified_callback2(prop, v4l2_control_changed, UINT_TO_POINTER(qctrl->id));
  92. break;
  93. case V4L2_CTRL_TYPE_BOOLEAN:
  94. prop = obs_properties_add_bool(props, (char *)qctrl->name, (char *)qctrl->name);
  95. obs_data_set_default_bool(settings, (char *)qctrl->name, qctrl->default_value);
  96. obs_property_set_modified_callback2(prop, v4l2_control_changed, UINT_TO_POINTER(qctrl->id));
  97. break;
  98. case V4L2_CTRL_TYPE_MENU:
  99. case V4L2_CTRL_TYPE_INTEGER_MENU:
  100. if (v4l2_update_controls_menu(dev, props, qctrl)) {
  101. obs_data_set_default_int(settings, (char *)qctrl->name, qctrl->default_value);
  102. blog(LOG_INFO, "setting default for %s to %d", (char *)qctrl->name, qctrl->default_value);
  103. }
  104. break;
  105. }
  106. }
  107. int_fast32_t v4l2_update_controls(int_fast32_t dev, obs_properties_t *props, obs_data_t *settings)
  108. {
  109. struct v4l2_queryctrl qctrl;
  110. if (!dev || !props)
  111. return -1;
  112. memset(&qctrl, 0, sizeof(qctrl));
  113. qctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
  114. while (0 == v4l2_ioctl(dev, VIDIOC_QUERYCTRL, &qctrl)) {
  115. add_control_property(props, settings, dev, &qctrl);
  116. qctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
  117. }
  118. return 0;
  119. }