v4l2-input.c 24 KB

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