v4l2-input.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. (code >> 24) & 0xFF, \
  47. (code >> 16) & 0xFF, \
  48. (code >> 8) & 0xFF, \
  49. code & 0xFF, \
  50. 0 \
  51. }
  52. #define blog(level, msg, ...) blog(level, "v4l2-input: " msg, ##__VA_ARGS__)
  53. /**
  54. * Data structure for the v4l2 source
  55. */
  56. struct v4l2_data {
  57. /* settings */
  58. char *device_id;
  59. int input;
  60. int pixfmt;
  61. int standard;
  62. int dv_timing;
  63. int resolution;
  64. int framerate;
  65. int color_range;
  66. /* internal data */
  67. obs_source_t *source;
  68. pthread_t thread;
  69. os_event_t *event;
  70. int_fast32_t dev;
  71. int width;
  72. int height;
  73. int linesize;
  74. struct v4l2_buffer_data buffers;
  75. };
  76. /* forward declarations */
  77. static void v4l2_init(struct v4l2_data *data);
  78. static void v4l2_terminate(struct v4l2_data *data);
  79. /**
  80. * Prepare the output frame structure for obs and compute plane offsets
  81. *
  82. * Basically all data apart from memory pointers and the timestamp is known
  83. * before the capture starts. This function prepares the obs_source_frame
  84. * struct with all the data that is already known.
  85. *
  86. * v4l2 uses a continuous memory segment for all planes so we simply compute
  87. * offsets to add to the start address in order to give obs the correct data
  88. * pointers for the individual planes.
  89. */
  90. static void v4l2_prep_obs_frame(struct v4l2_data *data,
  91. struct obs_source_frame *frame, 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_PARTIAL);
  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(prop,
  299. 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",
  385. dvt.bt.width, dvt.bt.height,
  386. (dvt.bt.interlaced) ? 'i' : 'p', rate);
  387. obs_property_list_add_int(prop, buf.array, index);
  388. index++;
  389. }
  390. dstr_free(&buf);
  391. }
  392. /*
  393. * List resolutions for device and format
  394. */
  395. static void v4l2_resolution_list(int dev, uint_fast32_t pixelformat,
  396. obs_property_t *prop)
  397. {
  398. struct v4l2_frmsizeenum frmsize;
  399. frmsize.pixel_format = pixelformat;
  400. frmsize.index = 0;
  401. struct dstr buffer;
  402. dstr_init(&buffer);
  403. obs_property_list_clear(prop);
  404. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  405. v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize);
  406. switch(frmsize.type) {
  407. case V4L2_FRMSIZE_TYPE_DISCRETE:
  408. while (v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize) == 0) {
  409. dstr_printf(&buffer, "%dx%d", frmsize.discrete.width,
  410. frmsize.discrete.height);
  411. obs_property_list_add_int(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, 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,
  450. &frmival) == 0) {
  451. float fps = (float) frmival.discrete.denominator /
  452. frmival.discrete.numerator;
  453. int pack = v4l2_pack_tuple(frmival.discrete.numerator,
  454. frmival.discrete.denominator);
  455. dstr_printf(&buffer, "%.2f", fps);
  456. obs_property_list_add_int(prop, buffer.array, pack);
  457. frmival.index++;
  458. }
  459. break;
  460. default:
  461. blog(LOG_INFO, "Stepwise and Continuous framerates "
  462. "are currently hardcoded");
  463. for (const int *packed = v4l2_framerates; *packed; ++packed) {
  464. int num;
  465. int denom;
  466. v4l2_unpack_tuple(&num, &denom, *packed);
  467. float fps = (float) denom / num;
  468. dstr_printf(&buffer, "%.2f", fps);
  469. obs_property_list_add_int(prop, buffer.array, *packed);
  470. }
  471. break;
  472. }
  473. dstr_free(&buffer);
  474. }
  475. /*
  476. * Device selected callback
  477. */
  478. static bool device_selected(obs_properties_t *props, obs_property_t *p,
  479. obs_data_t *settings)
  480. {
  481. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  482. O_RDWR | O_NONBLOCK);
  483. v4l2_props_set_enabled(props, p, (dev == -1) ? false : true);
  484. if (dev == -1)
  485. return false;
  486. obs_property_t *prop = obs_properties_get(props, "input");
  487. v4l2_input_list(dev, prop);
  488. v4l2_close(dev);
  489. obs_property_modified(prop, settings);
  490. return true;
  491. }
  492. /*
  493. * Input selected callback
  494. */
  495. static bool input_selected(obs_properties_t *props, obs_property_t *p,
  496. obs_data_t *settings)
  497. {
  498. UNUSED_PARAMETER(p);
  499. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  500. O_RDWR | O_NONBLOCK);
  501. if (dev == -1)
  502. return false;
  503. obs_property_t *prop = obs_properties_get(props, "pixelformat");
  504. v4l2_format_list(dev, prop);
  505. v4l2_close(dev);
  506. obs_property_modified(prop, settings);
  507. return true;
  508. }
  509. /*
  510. * Format selected callback
  511. */
  512. static bool format_selected(obs_properties_t *props, obs_property_t *p,
  513. obs_data_t *settings)
  514. {
  515. UNUSED_PARAMETER(p);
  516. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  517. O_RDWR | O_NONBLOCK);
  518. if (dev == -1)
  519. return false;
  520. int input = (int) obs_data_get_int(settings, "input");
  521. uint32_t caps = 0;
  522. if (v4l2_get_input_caps(dev, input, &caps) < 0)
  523. return false;
  524. caps &= V4L2_IN_CAP_STD | V4L2_IN_CAP_DV_TIMINGS;
  525. obs_property_t *resolution = obs_properties_get(props, "resolution");
  526. obs_property_t *framerate = obs_properties_get(props, "framerate");
  527. obs_property_t *standard = obs_properties_get(props, "standard");
  528. obs_property_t *dv_timing = obs_properties_get(props, "dv_timing");
  529. obs_property_set_visible(resolution, (!caps) ? true : false);
  530. obs_property_set_visible(framerate, (!caps) ? true : false);
  531. obs_property_set_visible(standard,
  532. (caps & V4L2_IN_CAP_STD) ? true : false);
  533. obs_property_set_visible(dv_timing,
  534. (caps & V4L2_IN_CAP_DV_TIMINGS) ? true : false);
  535. if (!caps) {
  536. v4l2_resolution_list(dev, obs_data_get_int(
  537. settings, "pixelformat"), resolution);
  538. }
  539. if (caps & V4L2_IN_CAP_STD)
  540. v4l2_standard_list(dev, standard);
  541. if (caps & V4L2_IN_CAP_DV_TIMINGS)
  542. v4l2_dv_timing_list(dev, dv_timing);
  543. v4l2_close(dev);
  544. if (!caps)
  545. obs_property_modified(resolution, settings);
  546. if (caps & V4L2_IN_CAP_STD)
  547. obs_property_modified(standard, settings);
  548. if (caps & V4L2_IN_CAP_DV_TIMINGS)
  549. obs_property_modified(dv_timing, settings);
  550. return true;
  551. }
  552. /*
  553. * Resolution selected callback
  554. */
  555. static bool resolution_selected(obs_properties_t *props, obs_property_t *p,
  556. obs_data_t *settings)
  557. {
  558. UNUSED_PARAMETER(p);
  559. int width, height;
  560. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  561. O_RDWR | O_NONBLOCK);
  562. if (dev == -1)
  563. return false;
  564. obs_property_t *prop = obs_properties_get(props, "framerate");
  565. v4l2_unpack_tuple(&width, &height, obs_data_get_int(settings,
  566. "resolution"));
  567. v4l2_framerate_list(dev, obs_data_get_int(settings, "pixelformat"),
  568. width, height, prop);
  569. v4l2_close(dev);
  570. obs_property_modified(prop, settings);
  571. return true;
  572. }
  573. #if HAVE_UDEV
  574. /**
  575. * Device added callback
  576. *
  577. * If everything went fine we can start capturing again when the device is
  578. * reconnected
  579. */
  580. static void device_added(void *vptr, calldata_t *calldata)
  581. {
  582. V4L2_DATA(vptr);
  583. obs_source_update_properties(data->source);
  584. const char *dev;
  585. calldata_get_string(calldata, "device", &dev);
  586. if (strcmp(data->device_id, dev))
  587. return;
  588. blog(LOG_INFO, "Device %s reconnected", dev);
  589. v4l2_init(data);
  590. }
  591. /**
  592. * Device removed callback
  593. *
  594. * We stop recording here so we don't block the device node
  595. */
  596. static void device_removed(void *vptr, calldata_t *calldata)
  597. {
  598. V4L2_DATA(vptr);
  599. obs_source_update_properties(data->source);
  600. const char *dev;
  601. calldata_get_string(calldata, "device", &dev);
  602. if (strcmp(data->device_id, dev))
  603. return;
  604. blog(LOG_INFO, "Device %s disconnected", dev);
  605. v4l2_terminate(data);
  606. }
  607. #endif
  608. static obs_properties_t *v4l2_properties(void *vptr)
  609. {
  610. V4L2_DATA(vptr);
  611. obs_properties_t *props = obs_properties_create();
  612. obs_property_t *device_list = obs_properties_add_list(props,
  613. "device_id", obs_module_text("Device"),
  614. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  615. obs_property_t *input_list = obs_properties_add_list(props,
  616. "input", obs_module_text("Input"),
  617. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  618. obs_property_t *format_list = obs_properties_add_list(props,
  619. "pixelformat", obs_module_text("VideoFormat"),
  620. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  621. obs_property_t *standard_list = obs_properties_add_list(props,
  622. "standard", obs_module_text("VideoStandard"),
  623. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  624. obs_property_set_visible(standard_list, false);
  625. obs_property_t *dv_timing_list = obs_properties_add_list(props,
  626. "dv_timing", obs_module_text("DVTiming"),
  627. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  628. obs_property_set_visible(dv_timing_list, false);
  629. obs_property_t *resolution_list = obs_properties_add_list(props,
  630. "resolution", obs_module_text("Resolution"),
  631. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  632. obs_properties_add_list(props,
  633. "framerate", obs_module_text("FrameRate"),
  634. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  635. obs_property_t *color_range_list = obs_properties_add_list(props,
  636. "color_range", obs_module_text("ColorRange"),
  637. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  638. obs_property_list_add_int(color_range_list, obs_module_text("ColorRange.Partial"), VIDEO_RANGE_PARTIAL);
  639. obs_property_list_add_int(color_range_list, obs_module_text("ColorRange.Full"), VIDEO_RANGE_FULL);
  640. obs_properties_add_bool(props,
  641. "buffering", obs_module_text("UseBuffering"));
  642. obs_data_t *settings = obs_source_get_settings(data->source);
  643. v4l2_device_list(device_list, settings);
  644. obs_data_release(settings);
  645. obs_property_set_modified_callback(device_list, device_selected);
  646. obs_property_set_modified_callback(input_list, input_selected);
  647. obs_property_set_modified_callback(format_list, format_selected);
  648. obs_property_set_modified_callback(resolution_list,
  649. resolution_selected);
  650. return props;
  651. }
  652. static void v4l2_terminate(struct v4l2_data *data)
  653. {
  654. if (data->thread) {
  655. os_event_signal(data->event);
  656. pthread_join(data->thread, NULL);
  657. os_event_destroy(data->event);
  658. data->thread = 0;
  659. }
  660. v4l2_destroy_mmap(&data->buffers);
  661. if (data->dev != -1) {
  662. v4l2_close(data->dev);
  663. data->dev = -1;
  664. }
  665. }
  666. static void v4l2_destroy(void *vptr)
  667. {
  668. V4L2_DATA(vptr);
  669. if (!data)
  670. return;
  671. v4l2_terminate(data);
  672. if (data->device_id)
  673. bfree(data->device_id);
  674. #if HAVE_UDEV
  675. signal_handler_t *sh = v4l2_get_udev_signalhandler();
  676. signal_handler_disconnect(sh, "device_added", device_added, data);
  677. signal_handler_disconnect(sh, "device_removed", device_removed, data);
  678. v4l2_unref_udev();
  679. #endif
  680. bfree(data);
  681. }
  682. /**
  683. * Initialize the v4l2 device
  684. *
  685. * This function:
  686. * - tries to open the device
  687. * - sets pixelformat and requested resolution
  688. * - sets the requested framerate
  689. * - maps the buffers
  690. * - starts the capture thread
  691. */
  692. static void v4l2_init(struct v4l2_data *data)
  693. {
  694. uint32_t input_caps;
  695. int fps_num, fps_denom;
  696. blog(LOG_INFO, "Start capture from %s", data->device_id);
  697. data->dev = v4l2_open(data->device_id, O_RDWR | O_NONBLOCK);
  698. if (data->dev == -1) {
  699. blog(LOG_ERROR, "Unable to open device");
  700. goto fail;
  701. }
  702. /* set input */
  703. if (v4l2_set_input(data->dev, &data->input) < 0) {
  704. blog(LOG_ERROR, "Unable to set input %d", data->input);
  705. goto fail;
  706. }
  707. blog(LOG_INFO, "Input: %d", data->input);
  708. if (v4l2_get_input_caps(data->dev, -1, &input_caps) < 0) {
  709. blog(LOG_ERROR, "Unable to get input capabilities");
  710. goto fail;
  711. }
  712. /* set video standard if supported */
  713. if (input_caps & V4L2_IN_CAP_STD) {
  714. if (v4l2_set_standard(data->dev, &data->standard) < 0) {
  715. blog(LOG_ERROR, "Unable to set video standard");
  716. goto fail;
  717. }
  718. data->resolution = -1;
  719. data->framerate = -1;
  720. }
  721. /* set dv timing if supported */
  722. if (input_caps & V4L2_IN_CAP_DV_TIMINGS) {
  723. if (v4l2_set_dv_timing(data->dev, &data->dv_timing) < 0) {
  724. blog(LOG_ERROR, "Unable to set dv timing");
  725. goto fail;
  726. }
  727. data->resolution = -1;
  728. data->framerate = -1;
  729. }
  730. /* set pixel format and resolution */
  731. if (v4l2_set_format(data->dev, &data->resolution, &data->pixfmt,
  732. &data->linesize) < 0) {
  733. blog(LOG_ERROR, "Unable to set format");
  734. goto fail;
  735. }
  736. if (v4l2_to_obs_video_format(data->pixfmt) == VIDEO_FORMAT_NONE) {
  737. blog(LOG_ERROR, "Selected video format not supported");
  738. goto fail;
  739. }
  740. v4l2_unpack_tuple(&data->width, &data->height, data->resolution);
  741. blog(LOG_INFO, "Resolution: %dx%d", data->width, data->height);
  742. blog(LOG_INFO, "Pixelformat: %s", V4L2_FOURCC_STR(data->pixfmt));
  743. blog(LOG_INFO, "Linesize: %d Bytes", data->linesize);
  744. /* set framerate */
  745. if (v4l2_set_framerate(data->dev, &data->framerate) < 0) {
  746. blog(LOG_ERROR, "Unable to set framerate");
  747. goto fail;
  748. }
  749. v4l2_unpack_tuple(&fps_num, &fps_denom, data->framerate);
  750. blog(LOG_INFO, "Framerate: %.2f fps", (float) fps_denom / fps_num);
  751. /* map buffers */
  752. if (v4l2_create_mmap(data->dev, &data->buffers) < 0) {
  753. blog(LOG_ERROR, "Failed to map buffers");
  754. goto fail;
  755. }
  756. /* start the capture thread */
  757. if (os_event_init(&data->event, OS_EVENT_TYPE_MANUAL) != 0)
  758. goto fail;
  759. if (pthread_create(&data->thread, NULL, v4l2_thread, data) != 0)
  760. goto fail;
  761. return;
  762. fail:
  763. blog(LOG_ERROR, "Initialization failed");
  764. v4l2_terminate(data);
  765. }
  766. /** Update source flags depending on the settings */
  767. static void v4l2_update_source_flags(struct v4l2_data *data,
  768. obs_data_t *settings)
  769. {
  770. obs_source_set_async_unbuffered(data->source,
  771. !obs_data_get_bool(settings, "buffering"));
  772. }
  773. /**
  774. * Update the settings for the v4l2 source
  775. *
  776. * Since there are very few settings that can be changed without restarting the
  777. * stream we don't bother to even try. Whenever this is called the currently
  778. * active stream (if exists) is stopped, the settings are updated and finally
  779. * the new stream is started.
  780. */
  781. static void v4l2_update(void *vptr, obs_data_t *settings)
  782. {
  783. V4L2_DATA(vptr);
  784. v4l2_terminate(data);
  785. if (data->device_id)
  786. bfree(data->device_id);
  787. data->device_id = bstrdup(obs_data_get_string(settings, "device_id"));
  788. data->input = obs_data_get_int(settings, "input");
  789. data->pixfmt = obs_data_get_int(settings, "pixelformat");
  790. data->standard = obs_data_get_int(settings, "standard");
  791. data->dv_timing = obs_data_get_int(settings, "dv_timing");
  792. data->resolution = obs_data_get_int(settings, "resolution");
  793. data->framerate = obs_data_get_int(settings, "framerate");
  794. data->color_range = obs_data_get_int(settings, "color_range");
  795. v4l2_update_source_flags(data, settings);
  796. v4l2_init(data);
  797. }
  798. static void *v4l2_create(obs_data_t *settings, obs_source_t *source)
  799. {
  800. struct v4l2_data *data = bzalloc(sizeof(struct v4l2_data));
  801. data->dev = -1;
  802. data->source = source;
  803. /* Bitch about build problems ... */
  804. #ifndef V4L2_CAP_DEVICE_CAPS
  805. blog(LOG_WARNING, "Plugin built without device caps support!");
  806. #endif
  807. #if !defined(VIDIOC_ENUM_DV_TIMINGS) || !defined(V4L2_IN_CAP_DV_TIMINGS)
  808. blog(LOG_WARNING, "Plugin built without dv-timing support!");
  809. #endif
  810. v4l2_update(data, settings);
  811. #if HAVE_UDEV
  812. v4l2_init_udev();
  813. signal_handler_t *sh = v4l2_get_udev_signalhandler();
  814. signal_handler_connect(sh, "device_added", &device_added, data);
  815. signal_handler_connect(sh, "device_removed", &device_removed, data);
  816. #endif
  817. return data;
  818. }
  819. struct obs_source_info v4l2_input = {
  820. .id = "v4l2_input",
  821. .type = OBS_SOURCE_TYPE_INPUT,
  822. .output_flags = OBS_SOURCE_ASYNC_VIDEO |
  823. OBS_SOURCE_DO_NOT_DUPLICATE,
  824. .get_name = v4l2_getname,
  825. .create = v4l2_create,
  826. .destroy = v4l2_destroy,
  827. .update = v4l2_update,
  828. .get_defaults = v4l2_defaults,
  829. .get_properties = v4l2_properties
  830. };