v4l2-input.c 24 KB

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