1
0

v4l2-input.c 24 KB

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