v4l2-input.c 25 KB

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