v4l2-input.c 25 KB

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