v4l2-input.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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 <stdio.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <string.h>
  18. #include <inttypes.h>
  19. #include <fcntl.h>
  20. #include <dirent.h>
  21. #include <sys/time.h>
  22. #include <sys/types.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/select.h>
  25. #include <linux/videodev2.h>
  26. #include <libv4l2.h>
  27. #include <util/threading.h>
  28. #include <util/bmem.h>
  29. #include <util/dstr.h>
  30. #include <util/platform.h>
  31. #include <obs-module.h>
  32. #include "v4l2-controls.h"
  33. #include "v4l2-helpers.h"
  34. #if HAVE_UDEV
  35. #include "v4l2-udev.h"
  36. #endif
  37. /* The new dv timing api was introduced in Linux 3.4
  38. * Currently we simply disable dv timings when this is not defined */
  39. #if !defined(VIDIOC_ENUM_DV_TIMINGS) || !defined(V4L2_IN_CAP_DV_TIMINGS)
  40. #define V4L2_IN_CAP_DV_TIMINGS 0
  41. #endif
  42. #define V4L2_DATA(voidptr) struct v4l2_data *data = voidptr;
  43. #define timeval2ns(tv) \
  44. (((uint64_t)tv.tv_sec * 1000000000) + ((uint64_t)tv.tv_usec * 1000))
  45. #define V4L2_FOURCC_STR(code) \
  46. (char[5]) \
  47. { \
  48. (code >> 24) & 0xFF, (code >> 16) & 0xFF, (code >> 8) & 0xFF, \
  49. code & 0xFF, 0 \
  50. }
  51. #define blog(level, msg, ...) blog(level, "v4l2-input: " msg, ##__VA_ARGS__)
  52. /**
  53. * Data structure for the v4l2 source
  54. */
  55. struct v4l2_data {
  56. /* settings */
  57. char *device_id;
  58. int input;
  59. int pixfmt;
  60. int standard;
  61. int dv_timing;
  62. int resolution;
  63. int framerate;
  64. int color_range;
  65. /* internal data */
  66. obs_source_t *source;
  67. pthread_t thread;
  68. os_event_t *event;
  69. int_fast32_t dev;
  70. int width;
  71. int height;
  72. int linesize;
  73. struct v4l2_buffer_data buffers;
  74. };
  75. /* forward declarations */
  76. static void v4l2_init(struct v4l2_data *data);
  77. static void v4l2_terminate(struct v4l2_data *data);
  78. /**
  79. * Prepare the output frame structure for obs and compute plane offsets
  80. *
  81. * Basically all data apart from memory pointers and the timestamp is known
  82. * before the capture starts. This function prepares the obs_source_frame
  83. * struct with all the data that is already known.
  84. *
  85. * v4l2 uses a continuous memory segment for all planes so we simply compute
  86. * offsets to add to the start address in order to give obs the correct data
  87. * pointers for the individual planes.
  88. */
  89. static void v4l2_prep_obs_frame(struct v4l2_data *data,
  90. struct obs_source_frame *frame,
  91. size_t *plane_offsets)
  92. {
  93. memset(frame, 0, sizeof(struct obs_source_frame));
  94. memset(plane_offsets, 0, sizeof(size_t) * MAX_AV_PLANES);
  95. frame->width = data->width;
  96. frame->height = data->height;
  97. frame->format = v4l2_to_obs_video_format(data->pixfmt);
  98. video_format_get_parameters(VIDEO_CS_DEFAULT, data->color_range,
  99. frame->color_matrix, frame->color_range_min,
  100. frame->color_range_max);
  101. switch (data->pixfmt) {
  102. case V4L2_PIX_FMT_NV12:
  103. frame->linesize[0] = data->linesize;
  104. frame->linesize[1] = data->linesize / 2;
  105. plane_offsets[1] = data->linesize * data->height;
  106. break;
  107. case V4L2_PIX_FMT_YVU420:
  108. frame->linesize[0] = data->linesize;
  109. frame->linesize[1] = data->linesize / 2;
  110. frame->linesize[2] = data->linesize / 2;
  111. plane_offsets[1] = data->linesize * data->height * 5 / 4;
  112. plane_offsets[2] = data->linesize * data->height;
  113. break;
  114. case V4L2_PIX_FMT_YUV420:
  115. frame->linesize[0] = data->linesize;
  116. frame->linesize[1] = data->linesize / 2;
  117. frame->linesize[2] = data->linesize / 2;
  118. plane_offsets[1] = data->linesize * data->height;
  119. plane_offsets[2] = data->linesize * data->height * 5 / 4;
  120. break;
  121. default:
  122. frame->linesize[0] = data->linesize;
  123. break;
  124. }
  125. }
  126. /*
  127. * Worker thread to get video data
  128. */
  129. static void *v4l2_thread(void *vptr)
  130. {
  131. V4L2_DATA(vptr);
  132. int r;
  133. fd_set fds;
  134. uint8_t *start;
  135. uint64_t frames;
  136. uint64_t first_ts;
  137. struct timeval tv;
  138. struct v4l2_buffer buf;
  139. struct obs_source_frame out;
  140. size_t plane_offsets[MAX_AV_PLANES];
  141. if (v4l2_start_capture(data->dev, &data->buffers) < 0)
  142. goto exit;
  143. frames = 0;
  144. first_ts = 0;
  145. v4l2_prep_obs_frame(data, &out, plane_offsets);
  146. while (os_event_try(data->event) == EAGAIN) {
  147. FD_ZERO(&fds);
  148. FD_SET(data->dev, &fds);
  149. tv.tv_sec = 1;
  150. tv.tv_usec = 0;
  151. r = select(data->dev + 1, &fds, NULL, NULL, &tv);
  152. if (r < 0) {
  153. if (errno == EINTR)
  154. continue;
  155. blog(LOG_DEBUG, "select failed");
  156. break;
  157. } else if (r == 0) {
  158. blog(LOG_DEBUG, "select timeout");
  159. continue;
  160. }
  161. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  162. buf.memory = V4L2_MEMORY_MMAP;
  163. if (v4l2_ioctl(data->dev, VIDIOC_DQBUF, &buf) < 0) {
  164. if (errno == EAGAIN)
  165. continue;
  166. blog(LOG_DEBUG, "failed to dequeue buffer");
  167. break;
  168. }
  169. out.timestamp = timeval2ns(buf.timestamp);
  170. if (!frames)
  171. first_ts = out.timestamp;
  172. out.timestamp -= first_ts;
  173. start = (uint8_t *)data->buffers.info[buf.index].start;
  174. for (uint_fast32_t i = 0; i < MAX_AV_PLANES; ++i)
  175. out.data[i] = start + plane_offsets[i];
  176. obs_source_output_video(data->source, &out);
  177. if (v4l2_ioctl(data->dev, VIDIOC_QBUF, &buf) < 0) {
  178. blog(LOG_DEBUG, "failed to enqueue buffer");
  179. break;
  180. }
  181. frames++;
  182. }
  183. blog(LOG_INFO, "Stopped capture after %" PRIu64 " frames", frames);
  184. exit:
  185. v4l2_stop_capture(data->dev);
  186. return NULL;
  187. }
  188. static const char *v4l2_getname(void *unused)
  189. {
  190. UNUSED_PARAMETER(unused);
  191. return obs_module_text("V4L2Input");
  192. }
  193. static void v4l2_defaults(obs_data_t *settings)
  194. {
  195. obs_data_set_default_int(settings, "input", -1);
  196. obs_data_set_default_int(settings, "pixelformat", -1);
  197. obs_data_set_default_int(settings, "standard", -1);
  198. obs_data_set_default_int(settings, "dv_timing", -1);
  199. obs_data_set_default_int(settings, "resolution", -1);
  200. obs_data_set_default_int(settings, "framerate", -1);
  201. obs_data_set_default_int(settings, "color_range", VIDEO_RANGE_DEFAULT);
  202. obs_data_set_default_bool(settings, "buffering", true);
  203. }
  204. /**
  205. * Enable/Disable all properties for the source.
  206. *
  207. * @note A property that should be ignored can be specified
  208. *
  209. * @param props the source properties
  210. * @param ignore ignore this property
  211. * @param enable enable/disable all properties
  212. */
  213. static void v4l2_props_set_enabled(obs_properties_t *props,
  214. obs_property_t *ignore, bool enable)
  215. {
  216. if (!props)
  217. return;
  218. for (obs_property_t *prop = obs_properties_first(props); prop != NULL;
  219. obs_property_next(&prop)) {
  220. if (prop == ignore)
  221. continue;
  222. obs_property_set_enabled(prop, enable);
  223. }
  224. }
  225. /*
  226. * List available devices
  227. */
  228. static void v4l2_device_list(obs_property_t *prop, obs_data_t *settings)
  229. {
  230. DIR *dirp;
  231. struct dirent *dp;
  232. struct dstr device;
  233. bool cur_device_found;
  234. size_t cur_device_index;
  235. const char *cur_device_name;
  236. #ifdef __FreeBSD__
  237. dirp = opendir("/dev");
  238. #else
  239. dirp = opendir("/sys/class/video4linux");
  240. #endif
  241. if (!dirp)
  242. return;
  243. cur_device_found = false;
  244. cur_device_name = obs_data_get_string(settings, "device_id");
  245. obs_property_list_clear(prop);
  246. dstr_init_copy(&device, "/dev/");
  247. while ((dp = readdir(dirp)) != NULL) {
  248. int fd;
  249. uint32_t caps;
  250. struct v4l2_capability video_cap;
  251. #ifdef __FreeBSD__
  252. if (strstr(dp->d_name, "video") == NULL)
  253. continue;
  254. #endif
  255. if (dp->d_type == DT_DIR)
  256. continue;
  257. dstr_resize(&device, 5);
  258. dstr_cat(&device, dp->d_name);
  259. if ((fd = v4l2_open(device.array, O_RDWR | O_NONBLOCK)) == -1) {
  260. blog(LOG_INFO, "Unable to open %s", device.array);
  261. continue;
  262. }
  263. if (v4l2_ioctl(fd, VIDIOC_QUERYCAP, &video_cap) == -1) {
  264. blog(LOG_INFO, "Failed to query capabilities for %s",
  265. device.array);
  266. v4l2_close(fd);
  267. continue;
  268. }
  269. #ifndef V4L2_CAP_DEVICE_CAPS
  270. caps = video_cap.capabilities;
  271. #else
  272. /* ... since Linux 3.3 */
  273. caps = (video_cap.capabilities & V4L2_CAP_DEVICE_CAPS)
  274. ? video_cap.device_caps
  275. : video_cap.capabilities;
  276. #endif
  277. if (!(caps & V4L2_CAP_VIDEO_CAPTURE)) {
  278. blog(LOG_INFO, "%s seems to not support video capture",
  279. device.array);
  280. v4l2_close(fd);
  281. continue;
  282. }
  283. /* make sure device names are unique */
  284. char unique_device_name[68];
  285. sprintf(unique_device_name, "%s (%s)", video_cap.card,
  286. video_cap.bus_info);
  287. obs_property_list_add_string(prop, unique_device_name,
  288. device.array);
  289. blog(LOG_INFO, "Found device '%s' at %s", video_cap.card,
  290. device.array);
  291. /* check if this is the currently used device */
  292. if (cur_device_name && !strcmp(cur_device_name, device.array))
  293. cur_device_found = true;
  294. v4l2_close(fd);
  295. }
  296. /* add currently selected device if not present, but disable it ... */
  297. if (!cur_device_found && cur_device_name && strlen(cur_device_name)) {
  298. cur_device_index = obs_property_list_add_string(
  299. prop, cur_device_name, cur_device_name);
  300. obs_property_list_item_disable(prop, cur_device_index, true);
  301. }
  302. closedir(dirp);
  303. dstr_free(&device);
  304. }
  305. /*
  306. * List inputs for device
  307. */
  308. static void v4l2_input_list(int_fast32_t dev, obs_property_t *prop)
  309. {
  310. struct v4l2_input in;
  311. memset(&in, 0, sizeof(in));
  312. obs_property_list_clear(prop);
  313. while (v4l2_ioctl(dev, VIDIOC_ENUMINPUT, &in) == 0) {
  314. obs_property_list_add_int(prop, (char *)in.name, in.index);
  315. blog(LOG_INFO, "Found input '%s' (Index %d)", in.name,
  316. in.index);
  317. in.index++;
  318. }
  319. }
  320. /*
  321. * List formats for device
  322. */
  323. static void v4l2_format_list(int dev, obs_property_t *prop)
  324. {
  325. struct v4l2_fmtdesc fmt;
  326. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  327. fmt.index = 0;
  328. struct dstr buffer;
  329. dstr_init(&buffer);
  330. obs_property_list_clear(prop);
  331. while (v4l2_ioctl(dev, VIDIOC_ENUM_FMT, &fmt) == 0) {
  332. dstr_copy(&buffer, (char *)fmt.description);
  333. if (fmt.flags & V4L2_FMT_FLAG_EMULATED)
  334. dstr_cat(&buffer, " (Emulated)");
  335. if (v4l2_to_obs_video_format(fmt.pixelformat) !=
  336. VIDEO_FORMAT_NONE) {
  337. obs_property_list_add_int(prop, buffer.array,
  338. fmt.pixelformat);
  339. blog(LOG_INFO, "Pixelformat: %s (available)",
  340. buffer.array);
  341. } else {
  342. blog(LOG_INFO, "Pixelformat: %s (unavailable)",
  343. buffer.array);
  344. }
  345. fmt.index++;
  346. }
  347. dstr_free(&buffer);
  348. }
  349. /*
  350. * List video standards for the device
  351. */
  352. static void v4l2_standard_list(int dev, obs_property_t *prop)
  353. {
  354. struct v4l2_standard std;
  355. std.index = 0;
  356. obs_property_list_clear(prop);
  357. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  358. while (v4l2_ioctl(dev, VIDIOC_ENUMSTD, &std) == 0) {
  359. obs_property_list_add_int(prop, (char *)std.name, std.id);
  360. std.index++;
  361. }
  362. }
  363. /*
  364. * List dv timings for the device
  365. */
  366. static void v4l2_dv_timing_list(int dev, obs_property_t *prop)
  367. {
  368. struct v4l2_dv_timings dvt;
  369. struct dstr buf;
  370. int index = 0;
  371. dstr_init(&buf);
  372. obs_property_list_clear(prop);
  373. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  374. while (v4l2_enum_dv_timing(dev, &dvt, index) == 0) {
  375. /* i do not pretend to understand, this is from qv4l2 ... */
  376. double h = (double)dvt.bt.height + dvt.bt.vfrontporch +
  377. dvt.bt.vsync + dvt.bt.vbackporch +
  378. dvt.bt.il_vfrontporch + dvt.bt.il_vsync +
  379. dvt.bt.il_vbackporch;
  380. double w = (double)dvt.bt.width + dvt.bt.hfrontporch +
  381. dvt.bt.hsync + dvt.bt.hbackporch;
  382. double i = (dvt.bt.interlaced) ? 2.0f : 1.0f;
  383. double rate = (double)dvt.bt.pixelclock / (w * (h / i));
  384. dstr_printf(&buf, "%ux%u%c %.2f", dvt.bt.width, dvt.bt.height,
  385. (dvt.bt.interlaced) ? 'i' : 'p', rate);
  386. obs_property_list_add_int(prop, buf.array, index);
  387. index++;
  388. }
  389. dstr_free(&buf);
  390. }
  391. /*
  392. * List resolutions for device and format
  393. */
  394. static void v4l2_resolution_list(int dev, uint_fast32_t pixelformat,
  395. obs_property_t *prop)
  396. {
  397. struct v4l2_frmsizeenum frmsize;
  398. frmsize.pixel_format = pixelformat;
  399. frmsize.index = 0;
  400. struct dstr buffer;
  401. dstr_init(&buffer);
  402. obs_property_list_clear(prop);
  403. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  404. v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize);
  405. switch (frmsize.type) {
  406. case V4L2_FRMSIZE_TYPE_DISCRETE:
  407. while (v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize) == 0) {
  408. dstr_printf(&buffer, "%dx%d", frmsize.discrete.width,
  409. frmsize.discrete.height);
  410. obs_property_list_add_int(
  411. prop, buffer.array,
  412. v4l2_pack_tuple(frmsize.discrete.width,
  413. frmsize.discrete.height));
  414. frmsize.index++;
  415. }
  416. break;
  417. default:
  418. blog(LOG_INFO, "Stepwise and Continuous framesizes "
  419. "are currently hardcoded");
  420. for (const int *packed = v4l2_framesizes; *packed; ++packed) {
  421. int width;
  422. int height;
  423. v4l2_unpack_tuple(&width, &height, *packed);
  424. dstr_printf(&buffer, "%dx%d", width, height);
  425. obs_property_list_add_int(prop, buffer.array, *packed);
  426. }
  427. break;
  428. }
  429. dstr_free(&buffer);
  430. }
  431. /*
  432. * List framerates for device and resolution
  433. */
  434. static void v4l2_framerate_list(int dev, uint_fast32_t pixelformat,
  435. uint_fast32_t width, uint_fast32_t height,
  436. obs_property_t *prop)
  437. {
  438. struct v4l2_frmivalenum frmival;
  439. frmival.pixel_format = pixelformat;
  440. frmival.width = width;
  441. frmival.height = height;
  442. frmival.index = 0;
  443. struct dstr buffer;
  444. dstr_init(&buffer);
  445. obs_property_list_clear(prop);
  446. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  447. v4l2_ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS, &frmival);
  448. switch (frmival.type) {
  449. case V4L2_FRMIVAL_TYPE_DISCRETE:
  450. while (v4l2_ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS, &frmival) ==
  451. 0) {
  452. float fps = (float)frmival.discrete.denominator /
  453. frmival.discrete.numerator;
  454. int pack =
  455. v4l2_pack_tuple(frmival.discrete.numerator,
  456. frmival.discrete.denominator);
  457. dstr_printf(&buffer, "%.2f", fps);
  458. obs_property_list_add_int(prop, buffer.array, pack);
  459. frmival.index++;
  460. }
  461. break;
  462. default:
  463. blog(LOG_INFO, "Stepwise and Continuous framerates "
  464. "are currently hardcoded");
  465. for (const int *packed = v4l2_framerates; *packed; ++packed) {
  466. int num;
  467. int denom;
  468. v4l2_unpack_tuple(&num, &denom, *packed);
  469. float fps = (float)denom / num;
  470. dstr_printf(&buffer, "%.2f", fps);
  471. obs_property_list_add_int(prop, buffer.array, *packed);
  472. }
  473. break;
  474. }
  475. dstr_free(&buffer);
  476. }
  477. /*
  478. * Device selected callback
  479. */
  480. static bool device_selected(obs_properties_t *props, obs_property_t *p,
  481. obs_data_t *settings)
  482. {
  483. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  484. O_RDWR | O_NONBLOCK);
  485. v4l2_props_set_enabled(props, p, (dev == -1) ? false : true);
  486. if (dev == -1)
  487. return false;
  488. obs_property_t *prop = obs_properties_get(props, "input");
  489. v4l2_input_list(dev, prop);
  490. v4l2_update_controls(dev, props, settings);
  491. v4l2_close(dev);
  492. obs_property_modified(prop, settings);
  493. return true;
  494. }
  495. /*
  496. * Input selected callback
  497. */
  498. static bool input_selected(obs_properties_t *props, obs_property_t *p,
  499. obs_data_t *settings)
  500. {
  501. UNUSED_PARAMETER(p);
  502. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  503. O_RDWR | O_NONBLOCK);
  504. if (dev == -1)
  505. return false;
  506. obs_property_t *prop = obs_properties_get(props, "pixelformat");
  507. v4l2_format_list(dev, prop);
  508. v4l2_close(dev);
  509. obs_property_modified(prop, settings);
  510. return true;
  511. }
  512. /*
  513. * Format selected callback
  514. */
  515. static bool format_selected(obs_properties_t *props, obs_property_t *p,
  516. obs_data_t *settings)
  517. {
  518. UNUSED_PARAMETER(p);
  519. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  520. O_RDWR | O_NONBLOCK);
  521. if (dev == -1)
  522. return false;
  523. int input = (int)obs_data_get_int(settings, "input");
  524. uint32_t caps = 0;
  525. if (v4l2_get_input_caps(dev, input, &caps) < 0)
  526. return false;
  527. caps &= V4L2_IN_CAP_STD | V4L2_IN_CAP_DV_TIMINGS;
  528. obs_property_t *resolution = obs_properties_get(props, "resolution");
  529. obs_property_t *framerate = obs_properties_get(props, "framerate");
  530. obs_property_t *standard = obs_properties_get(props, "standard");
  531. obs_property_t *dv_timing = obs_properties_get(props, "dv_timing");
  532. obs_property_set_visible(resolution, (!caps) ? true : false);
  533. obs_property_set_visible(framerate, (!caps) ? true : false);
  534. obs_property_set_visible(standard,
  535. (caps & V4L2_IN_CAP_STD) ? true : false);
  536. obs_property_set_visible(
  537. dv_timing, (caps & V4L2_IN_CAP_DV_TIMINGS) ? true : false);
  538. if (!caps) {
  539. v4l2_resolution_list(dev,
  540. obs_data_get_int(settings, "pixelformat"),
  541. resolution);
  542. }
  543. if (caps & V4L2_IN_CAP_STD)
  544. v4l2_standard_list(dev, standard);
  545. if (caps & V4L2_IN_CAP_DV_TIMINGS)
  546. v4l2_dv_timing_list(dev, dv_timing);
  547. v4l2_close(dev);
  548. if (!caps)
  549. obs_property_modified(resolution, settings);
  550. if (caps & V4L2_IN_CAP_STD)
  551. obs_property_modified(standard, settings);
  552. if (caps & V4L2_IN_CAP_DV_TIMINGS)
  553. obs_property_modified(dv_timing, settings);
  554. return true;
  555. }
  556. /*
  557. * Resolution selected callback
  558. */
  559. static bool resolution_selected(obs_properties_t *props, obs_property_t *p,
  560. obs_data_t *settings)
  561. {
  562. UNUSED_PARAMETER(p);
  563. int width, height;
  564. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  565. O_RDWR | O_NONBLOCK);
  566. if (dev == -1)
  567. return false;
  568. obs_property_t *prop = obs_properties_get(props, "framerate");
  569. v4l2_unpack_tuple(&width, &height,
  570. obs_data_get_int(settings, "resolution"));
  571. v4l2_framerate_list(dev, obs_data_get_int(settings, "pixelformat"),
  572. width, height, prop);
  573. v4l2_close(dev);
  574. obs_property_modified(prop, settings);
  575. return true;
  576. }
  577. #if HAVE_UDEV
  578. /**
  579. * Device added callback
  580. *
  581. * If everything went fine we can start capturing again when the device is
  582. * reconnected
  583. */
  584. static void device_added(void *vptr, calldata_t *calldata)
  585. {
  586. V4L2_DATA(vptr);
  587. obs_source_update_properties(data->source);
  588. const char *dev;
  589. calldata_get_string(calldata, "device", &dev);
  590. if (strcmp(data->device_id, dev))
  591. return;
  592. blog(LOG_INFO, "Device %s reconnected", dev);
  593. v4l2_init(data);
  594. }
  595. /**
  596. * Device removed callback
  597. *
  598. * We stop recording here so we don't block the device node
  599. */
  600. static void device_removed(void *vptr, calldata_t *calldata)
  601. {
  602. V4L2_DATA(vptr);
  603. obs_source_update_properties(data->source);
  604. const char *dev;
  605. calldata_get_string(calldata, "device", &dev);
  606. if (strcmp(data->device_id, dev))
  607. return;
  608. blog(LOG_INFO, "Device %s disconnected", dev);
  609. v4l2_terminate(data);
  610. }
  611. #endif
  612. static obs_properties_t *v4l2_properties(void *vptr)
  613. {
  614. V4L2_DATA(vptr);
  615. obs_properties_t *props = obs_properties_create();
  616. obs_property_t *device_list = obs_properties_add_list(
  617. props, "device_id", obs_module_text("Device"),
  618. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  619. obs_property_t *input_list = obs_properties_add_list(
  620. props, "input", obs_module_text("Input"), OBS_COMBO_TYPE_LIST,
  621. OBS_COMBO_FORMAT_INT);
  622. obs_property_t *format_list = obs_properties_add_list(
  623. props, "pixelformat", obs_module_text("VideoFormat"),
  624. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  625. obs_property_t *standard_list = obs_properties_add_list(
  626. props, "standard", obs_module_text("VideoStandard"),
  627. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  628. obs_property_set_visible(standard_list, false);
  629. obs_property_t *dv_timing_list = obs_properties_add_list(
  630. props, "dv_timing", obs_module_text("DVTiming"),
  631. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  632. obs_property_set_visible(dv_timing_list, false);
  633. obs_property_t *resolution_list = obs_properties_add_list(
  634. props, "resolution", obs_module_text("Resolution"),
  635. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  636. obs_properties_add_list(props, "framerate",
  637. obs_module_text("FrameRate"),
  638. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  639. obs_property_t *color_range_list = obs_properties_add_list(
  640. props, "color_range", obs_module_text("ColorRange"),
  641. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  642. obs_property_list_add_int(color_range_list,
  643. obs_module_text("ColorRange.Default"),
  644. VIDEO_RANGE_DEFAULT);
  645. obs_property_list_add_int(color_range_list,
  646. obs_module_text("ColorRange.Partial"),
  647. VIDEO_RANGE_PARTIAL);
  648. obs_property_list_add_int(color_range_list,
  649. obs_module_text("ColorRange.Full"),
  650. VIDEO_RANGE_FULL);
  651. obs_properties_add_bool(props, "buffering",
  652. obs_module_text("UseBuffering"));
  653. obs_data_t *settings = obs_source_get_settings(data->source);
  654. v4l2_device_list(device_list, settings);
  655. obs_data_release(settings);
  656. obs_property_set_modified_callback(device_list, device_selected);
  657. obs_property_set_modified_callback(input_list, input_selected);
  658. obs_property_set_modified_callback(format_list, format_selected);
  659. obs_property_set_modified_callback(resolution_list,
  660. resolution_selected);
  661. return props;
  662. }
  663. static void v4l2_terminate(struct v4l2_data *data)
  664. {
  665. if (data->thread) {
  666. os_event_signal(data->event);
  667. pthread_join(data->thread, NULL);
  668. os_event_destroy(data->event);
  669. data->thread = 0;
  670. }
  671. v4l2_destroy_mmap(&data->buffers);
  672. if (data->dev != -1) {
  673. v4l2_close(data->dev);
  674. data->dev = -1;
  675. }
  676. }
  677. static void v4l2_destroy(void *vptr)
  678. {
  679. V4L2_DATA(vptr);
  680. if (!data)
  681. return;
  682. v4l2_terminate(data);
  683. if (data->device_id)
  684. bfree(data->device_id);
  685. #if HAVE_UDEV
  686. signal_handler_t *sh = v4l2_get_udev_signalhandler();
  687. signal_handler_disconnect(sh, "device_added", device_added, data);
  688. signal_handler_disconnect(sh, "device_removed", device_removed, data);
  689. v4l2_unref_udev();
  690. #endif
  691. bfree(data);
  692. }
  693. /**
  694. * Initialize the v4l2 device
  695. *
  696. * This function:
  697. * - tries to open the device
  698. * - sets pixelformat and requested resolution
  699. * - sets the requested framerate
  700. * - maps the buffers
  701. * - starts the capture thread
  702. */
  703. static void v4l2_init(struct v4l2_data *data)
  704. {
  705. uint32_t input_caps;
  706. int fps_num, fps_denom;
  707. blog(LOG_INFO, "Start capture from %s", data->device_id);
  708. data->dev = v4l2_open(data->device_id, O_RDWR | O_NONBLOCK);
  709. if (data->dev == -1) {
  710. blog(LOG_ERROR, "Unable to open device");
  711. goto fail;
  712. }
  713. /* set input */
  714. if (v4l2_set_input(data->dev, &data->input) < 0) {
  715. blog(LOG_ERROR, "Unable to set input %d", data->input);
  716. goto fail;
  717. }
  718. blog(LOG_INFO, "Input: %d", data->input);
  719. if (v4l2_get_input_caps(data->dev, -1, &input_caps) < 0) {
  720. blog(LOG_ERROR, "Unable to get input capabilities");
  721. goto fail;
  722. }
  723. /* set video standard if supported */
  724. if (input_caps & V4L2_IN_CAP_STD) {
  725. if (v4l2_set_standard(data->dev, &data->standard) < 0) {
  726. blog(LOG_ERROR, "Unable to set video standard");
  727. goto fail;
  728. }
  729. data->resolution = -1;
  730. data->framerate = -1;
  731. }
  732. /* set dv timing if supported */
  733. if (input_caps & V4L2_IN_CAP_DV_TIMINGS) {
  734. if (v4l2_set_dv_timing(data->dev, &data->dv_timing) < 0) {
  735. blog(LOG_ERROR, "Unable to set dv timing");
  736. goto fail;
  737. }
  738. data->resolution = -1;
  739. data->framerate = -1;
  740. }
  741. /* set pixel format and resolution */
  742. if (v4l2_set_format(data->dev, &data->resolution, &data->pixfmt,
  743. &data->linesize) < 0) {
  744. blog(LOG_ERROR, "Unable to set format");
  745. goto fail;
  746. }
  747. if (v4l2_to_obs_video_format(data->pixfmt) == VIDEO_FORMAT_NONE) {
  748. blog(LOG_ERROR, "Selected video format not supported");
  749. goto fail;
  750. }
  751. v4l2_unpack_tuple(&data->width, &data->height, data->resolution);
  752. blog(LOG_INFO, "Resolution: %dx%d", data->width, data->height);
  753. blog(LOG_INFO, "Pixelformat: %s", V4L2_FOURCC_STR(data->pixfmt));
  754. blog(LOG_INFO, "Linesize: %d Bytes", data->linesize);
  755. /* set framerate */
  756. if (v4l2_set_framerate(data->dev, &data->framerate) < 0) {
  757. blog(LOG_ERROR, "Unable to set framerate");
  758. goto fail;
  759. }
  760. v4l2_unpack_tuple(&fps_num, &fps_denom, data->framerate);
  761. blog(LOG_INFO, "Framerate: %.2f fps", (float)fps_denom / fps_num);
  762. /* map buffers */
  763. if (v4l2_create_mmap(data->dev, &data->buffers) < 0) {
  764. blog(LOG_ERROR, "Failed to map buffers");
  765. goto fail;
  766. }
  767. /* start the capture thread */
  768. if (os_event_init(&data->event, OS_EVENT_TYPE_MANUAL) != 0)
  769. goto fail;
  770. if (pthread_create(&data->thread, NULL, v4l2_thread, data) != 0)
  771. goto fail;
  772. return;
  773. fail:
  774. blog(LOG_ERROR, "Initialization failed");
  775. v4l2_terminate(data);
  776. }
  777. /** Update source flags depending on the settings */
  778. static void v4l2_update_source_flags(struct v4l2_data *data,
  779. obs_data_t *settings)
  780. {
  781. obs_source_set_async_unbuffered(
  782. data->source, !obs_data_get_bool(settings, "buffering"));
  783. }
  784. /**
  785. * Update the settings for the v4l2 source
  786. *
  787. * Since there are very few settings that can be changed without restarting the
  788. * stream we don't bother to even try. Whenever this is called the currently
  789. * active stream (if exists) is stopped, the settings are updated and finally
  790. * the new stream is started.
  791. */
  792. static void v4l2_update(void *vptr, obs_data_t *settings)
  793. {
  794. V4L2_DATA(vptr);
  795. v4l2_terminate(data);
  796. if (data->device_id)
  797. bfree(data->device_id);
  798. data->device_id = bstrdup(obs_data_get_string(settings, "device_id"));
  799. data->input = obs_data_get_int(settings, "input");
  800. data->pixfmt = obs_data_get_int(settings, "pixelformat");
  801. data->standard = obs_data_get_int(settings, "standard");
  802. data->dv_timing = obs_data_get_int(settings, "dv_timing");
  803. data->resolution = obs_data_get_int(settings, "resolution");
  804. data->framerate = obs_data_get_int(settings, "framerate");
  805. data->color_range = obs_data_get_int(settings, "color_range");
  806. v4l2_update_source_flags(data, settings);
  807. v4l2_init(data);
  808. }
  809. static void *v4l2_create(obs_data_t *settings, obs_source_t *source)
  810. {
  811. struct v4l2_data *data = bzalloc(sizeof(struct v4l2_data));
  812. data->dev = -1;
  813. data->source = source;
  814. /* Bitch about build problems ... */
  815. #ifndef V4L2_CAP_DEVICE_CAPS
  816. blog(LOG_WARNING, "Plugin built without device caps support!");
  817. #endif
  818. #if !defined(VIDIOC_ENUM_DV_TIMINGS) || !defined(V4L2_IN_CAP_DV_TIMINGS)
  819. blog(LOG_WARNING, "Plugin built without dv-timing support!");
  820. #endif
  821. v4l2_update(data, settings);
  822. #if HAVE_UDEV
  823. v4l2_init_udev();
  824. signal_handler_t *sh = v4l2_get_udev_signalhandler();
  825. signal_handler_connect(sh, "device_added", &device_added, data);
  826. signal_handler_connect(sh, "device_removed", &device_removed, data);
  827. #endif
  828. return data;
  829. }
  830. struct obs_source_info v4l2_input = {
  831. .id = "v4l2_input",
  832. .type = OBS_SOURCE_TYPE_INPUT,
  833. .output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_DO_NOT_DUPLICATE,
  834. .get_name = v4l2_getname,
  835. .create = v4l2_create,
  836. .destroy = v4l2_destroy,
  837. .update = v4l2_update,
  838. .get_defaults = v4l2_defaults,
  839. .get_properties = v4l2_properties,
  840. .icon_type = OBS_ICON_TYPE_CAMERA,
  841. };