v4l2-input.c 26 KB

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