v4l2-input.c 29 KB

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