v4l2-input.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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. #ifndef V4L2_CAP_DEVICE_CAPS
  255. caps = video_cap.capabilities;
  256. #else
  257. /* ... since Linux 3.3 */
  258. caps = (video_cap.capabilities & V4L2_CAP_DEVICE_CAPS)
  259. ? video_cap.device_caps
  260. : video_cap.capabilities;
  261. #endif
  262. if (!(caps & V4L2_CAP_VIDEO_CAPTURE)) {
  263. blog(LOG_INFO, "%s seems to not support video capture",
  264. device.array);
  265. v4l2_close(fd);
  266. continue;
  267. }
  268. obs_property_list_add_string(prop, (char *) video_cap.card,
  269. device.array);
  270. blog(LOG_INFO, "Found device '%s' at %s", video_cap.card,
  271. device.array);
  272. /* check if this is the currently used device */
  273. if (cur_device_name && !strcmp(cur_device_name, device.array))
  274. cur_device_found = true;
  275. v4l2_close(fd);
  276. }
  277. /* add currently selected device if not present, but disable it ... */
  278. if (!cur_device_found && cur_device_name && strlen(cur_device_name)) {
  279. cur_device_index = obs_property_list_add_string(prop,
  280. cur_device_name, cur_device_name);
  281. obs_property_list_item_disable(prop, cur_device_index, true);
  282. }
  283. closedir(dirp);
  284. dstr_free(&device);
  285. }
  286. /*
  287. * List inputs for device
  288. */
  289. static void v4l2_input_list(int_fast32_t dev, obs_property_t *prop)
  290. {
  291. struct v4l2_input in;
  292. memset(&in, 0, sizeof(in));
  293. obs_property_list_clear(prop);
  294. while (v4l2_ioctl(dev, VIDIOC_ENUMINPUT, &in) == 0) {
  295. obs_property_list_add_int(prop, (char *) in.name, in.index);
  296. blog(LOG_INFO, "Found input '%s' (Index %d)", in.name,
  297. in.index);
  298. in.index++;
  299. }
  300. }
  301. /*
  302. * List formats for device
  303. */
  304. static void v4l2_format_list(int dev, obs_property_t *prop)
  305. {
  306. struct v4l2_fmtdesc fmt;
  307. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  308. fmt.index = 0;
  309. struct dstr buffer;
  310. dstr_init(&buffer);
  311. obs_property_list_clear(prop);
  312. while (v4l2_ioctl(dev, VIDIOC_ENUM_FMT, &fmt) == 0) {
  313. dstr_copy(&buffer, (char *) fmt.description);
  314. if (fmt.flags & V4L2_FMT_FLAG_EMULATED)
  315. dstr_cat(&buffer, " (Emulated)");
  316. if (v4l2_to_obs_video_format(fmt.pixelformat)
  317. != VIDEO_FORMAT_NONE) {
  318. obs_property_list_add_int(prop, buffer.array,
  319. fmt.pixelformat);
  320. blog(LOG_INFO, "Pixelformat: %s (available)",
  321. buffer.array);
  322. } else {
  323. blog(LOG_INFO, "Pixelformat: %s (unavailable)",
  324. buffer.array);
  325. }
  326. fmt.index++;
  327. }
  328. dstr_free(&buffer);
  329. }
  330. /*
  331. * List video standards for the device
  332. */
  333. static void v4l2_standard_list(int dev, obs_property_t *prop)
  334. {
  335. struct v4l2_standard std;
  336. std.index = 0;
  337. obs_property_list_clear(prop);
  338. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  339. while (v4l2_ioctl(dev, VIDIOC_ENUMSTD, &std) == 0) {
  340. obs_property_list_add_int(prop, (char *) std.name, std.id);
  341. std.index++;
  342. }
  343. }
  344. /*
  345. * List dv timings for the device
  346. */
  347. static void v4l2_dv_timing_list(int dev, obs_property_t *prop)
  348. {
  349. struct v4l2_dv_timings dvt;
  350. struct dstr buf;
  351. int index = 0;
  352. dstr_init(&buf);
  353. obs_property_list_clear(prop);
  354. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  355. while (v4l2_enum_dv_timing(dev, &dvt, index) == 0) {
  356. /* i do not pretend to understand, this is from qv4l2 ... */
  357. double h = (double) dvt.bt.height + dvt.bt.vfrontporch +
  358. dvt.bt.vsync + dvt.bt.vbackporch +
  359. dvt.bt.il_vfrontporch + dvt.bt.il_vsync +
  360. dvt.bt.il_vbackporch;
  361. double w = (double) dvt.bt.width + dvt.bt.hfrontporch +
  362. dvt.bt.hsync + dvt.bt.hbackporch;
  363. double i = (dvt.bt.interlaced) ? 2.0f : 1.0f;
  364. double rate = (double) dvt.bt.pixelclock / (w * (h / i));
  365. dstr_printf(&buf, "%ux%u%c %.2f",
  366. dvt.bt.width, dvt.bt.height,
  367. (dvt.bt.interlaced) ? 'i' : 'p', rate);
  368. obs_property_list_add_int(prop, buf.array, index);
  369. index++;
  370. }
  371. dstr_free(&buf);
  372. }
  373. /*
  374. * List resolutions for device and format
  375. */
  376. static void v4l2_resolution_list(int dev, uint_fast32_t pixelformat,
  377. obs_property_t *prop)
  378. {
  379. struct v4l2_frmsizeenum frmsize;
  380. frmsize.pixel_format = pixelformat;
  381. frmsize.index = 0;
  382. struct dstr buffer;
  383. dstr_init(&buffer);
  384. obs_property_list_clear(prop);
  385. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  386. v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize);
  387. switch(frmsize.type) {
  388. case V4L2_FRMSIZE_TYPE_DISCRETE:
  389. while (v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize) == 0) {
  390. dstr_printf(&buffer, "%dx%d", frmsize.discrete.width,
  391. frmsize.discrete.height);
  392. obs_property_list_add_int(prop, buffer.array,
  393. v4l2_pack_tuple(frmsize.discrete.width,
  394. frmsize.discrete.height));
  395. frmsize.index++;
  396. }
  397. break;
  398. default:
  399. blog(LOG_INFO, "Stepwise and Continuous framesizes "
  400. "are currently hardcoded");
  401. for (const int *packed = v4l2_framesizes; *packed; ++packed) {
  402. int width;
  403. int height;
  404. v4l2_unpack_tuple(&width, &height, *packed);
  405. dstr_printf(&buffer, "%dx%d", width, height);
  406. obs_property_list_add_int(prop, buffer.array, *packed);
  407. }
  408. break;
  409. }
  410. dstr_free(&buffer);
  411. }
  412. /*
  413. * List framerates for device and resolution
  414. */
  415. static void v4l2_framerate_list(int dev, uint_fast32_t pixelformat,
  416. uint_fast32_t width, uint_fast32_t height, obs_property_t *prop)
  417. {
  418. struct v4l2_frmivalenum frmival;
  419. frmival.pixel_format = pixelformat;
  420. frmival.width = width;
  421. frmival.height = height;
  422. frmival.index = 0;
  423. struct dstr buffer;
  424. dstr_init(&buffer);
  425. obs_property_list_clear(prop);
  426. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  427. v4l2_ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS, &frmival);
  428. switch(frmival.type) {
  429. case V4L2_FRMIVAL_TYPE_DISCRETE:
  430. while (v4l2_ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS,
  431. &frmival) == 0) {
  432. float fps = (float) frmival.discrete.denominator /
  433. frmival.discrete.numerator;
  434. int pack = v4l2_pack_tuple(frmival.discrete.numerator,
  435. frmival.discrete.denominator);
  436. dstr_printf(&buffer, "%.2f", fps);
  437. obs_property_list_add_int(prop, buffer.array, pack);
  438. frmival.index++;
  439. }
  440. break;
  441. default:
  442. blog(LOG_INFO, "Stepwise and Continuous framerates "
  443. "are currently hardcoded");
  444. for (const int *packed = v4l2_framerates; *packed; ++packed) {
  445. int num;
  446. int denom;
  447. v4l2_unpack_tuple(&num, &denom, *packed);
  448. float fps = (float) denom / num;
  449. dstr_printf(&buffer, "%.2f", fps);
  450. obs_property_list_add_int(prop, buffer.array, *packed);
  451. }
  452. break;
  453. }
  454. dstr_free(&buffer);
  455. }
  456. /*
  457. * Device selected callback
  458. */
  459. static bool device_selected(obs_properties_t *props, obs_property_t *p,
  460. obs_data_t *settings)
  461. {
  462. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  463. O_RDWR | O_NONBLOCK);
  464. v4l2_props_set_enabled(props, p, (dev == -1) ? false : true);
  465. if (dev == -1)
  466. return false;
  467. obs_property_t *prop = obs_properties_get(props, "input");
  468. v4l2_input_list(dev, prop);
  469. v4l2_close(dev);
  470. obs_property_modified(prop, settings);
  471. return true;
  472. }
  473. /*
  474. * Input selected callback
  475. */
  476. static bool input_selected(obs_properties_t *props, obs_property_t *p,
  477. obs_data_t *settings)
  478. {
  479. UNUSED_PARAMETER(p);
  480. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  481. O_RDWR | O_NONBLOCK);
  482. if (dev == -1)
  483. return false;
  484. obs_property_t *prop = obs_properties_get(props, "pixelformat");
  485. v4l2_format_list(dev, prop);
  486. v4l2_close(dev);
  487. obs_property_modified(prop, settings);
  488. return true;
  489. }
  490. /*
  491. * Format selected callback
  492. */
  493. static bool format_selected(obs_properties_t *props, obs_property_t *p,
  494. obs_data_t *settings)
  495. {
  496. UNUSED_PARAMETER(p);
  497. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  498. O_RDWR | O_NONBLOCK);
  499. if (dev == -1)
  500. return false;
  501. int input = (int) obs_data_get_int(settings, "input");
  502. uint32_t caps = 0;
  503. if (v4l2_get_input_caps(dev, input, &caps) < 0)
  504. return false;
  505. caps &= V4L2_IN_CAP_STD | V4L2_IN_CAP_DV_TIMINGS;
  506. obs_property_t *resolution = obs_properties_get(props, "resolution");
  507. obs_property_t *framerate = obs_properties_get(props, "framerate");
  508. obs_property_t *standard = obs_properties_get(props, "standard");
  509. obs_property_t *dv_timing = obs_properties_get(props, "dv_timing");
  510. obs_property_set_visible(resolution, (!caps) ? true : false);
  511. obs_property_set_visible(framerate, (!caps) ? true : false);
  512. obs_property_set_visible(standard,
  513. (caps & V4L2_IN_CAP_STD) ? true : false);
  514. obs_property_set_visible(dv_timing,
  515. (caps & V4L2_IN_CAP_DV_TIMINGS) ? true : false);
  516. if (!caps) {
  517. v4l2_resolution_list(dev, obs_data_get_int(
  518. settings, "pixelformat"), resolution);
  519. }
  520. if (caps & V4L2_IN_CAP_STD)
  521. v4l2_standard_list(dev, standard);
  522. if (caps & V4L2_IN_CAP_DV_TIMINGS)
  523. v4l2_dv_timing_list(dev, dv_timing);
  524. v4l2_close(dev);
  525. if (!caps)
  526. obs_property_modified(resolution, settings);
  527. if (caps & V4L2_IN_CAP_STD)
  528. obs_property_modified(standard, settings);
  529. if (caps & V4L2_IN_CAP_DV_TIMINGS)
  530. obs_property_modified(dv_timing, settings);
  531. return true;
  532. }
  533. /*
  534. * Resolution selected callback
  535. */
  536. static bool resolution_selected(obs_properties_t *props, obs_property_t *p,
  537. obs_data_t *settings)
  538. {
  539. UNUSED_PARAMETER(p);
  540. int width, height;
  541. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  542. O_RDWR | O_NONBLOCK);
  543. if (dev == -1)
  544. return false;
  545. obs_property_t *prop = obs_properties_get(props, "framerate");
  546. v4l2_unpack_tuple(&width, &height, obs_data_get_int(settings,
  547. "resolution"));
  548. v4l2_framerate_list(dev, obs_data_get_int(settings, "pixelformat"),
  549. width, height, prop);
  550. v4l2_close(dev);
  551. obs_property_modified(prop, settings);
  552. return true;
  553. }
  554. #if HAVE_UDEV
  555. /**
  556. * Device added callback
  557. *
  558. * If everything went fine we can start capturing again when the device is
  559. * reconnected
  560. */
  561. static void device_added(const char *dev, void *vptr)
  562. {
  563. V4L2_DATA(vptr);
  564. obs_source_update_properties(data->source);
  565. if (strcmp(data->device_id, dev))
  566. return;
  567. blog(LOG_INFO, "Device %s reconnected", dev);
  568. v4l2_init(data);
  569. }
  570. /**
  571. * Device removed callback
  572. *
  573. * We stop recording here so we don't block the device node
  574. */
  575. static void device_removed(const char *dev, void *vptr)
  576. {
  577. V4L2_DATA(vptr);
  578. obs_source_update_properties(data->source);
  579. if (strcmp(data->device_id, dev))
  580. return;
  581. blog(LOG_INFO, "Device %s disconnected", dev);
  582. v4l2_terminate(data);
  583. }
  584. #endif
  585. static obs_properties_t *v4l2_properties(void *vptr)
  586. {
  587. V4L2_DATA(vptr);
  588. obs_properties_t *props = obs_properties_create();
  589. obs_property_t *device_list = obs_properties_add_list(props,
  590. "device_id", obs_module_text("Device"),
  591. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  592. obs_property_t *input_list = obs_properties_add_list(props,
  593. "input", obs_module_text("Input"),
  594. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  595. obs_property_t *format_list = obs_properties_add_list(props,
  596. "pixelformat", obs_module_text("VideoFormat"),
  597. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  598. obs_property_t *standard_list = obs_properties_add_list(props,
  599. "standard", obs_module_text("VideoStandard"),
  600. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  601. obs_property_set_visible(standard_list, false);
  602. obs_property_t *dv_timing_list = obs_properties_add_list(props,
  603. "dv_timing", obs_module_text("DVTiming"),
  604. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  605. obs_property_set_visible(dv_timing_list, false);
  606. obs_property_t *resolution_list = obs_properties_add_list(props,
  607. "resolution", obs_module_text("Resolution"),
  608. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  609. obs_properties_add_list(props,
  610. "framerate", obs_module_text("FrameRate"),
  611. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  612. obs_properties_add_bool(props,
  613. "buffering", obs_module_text("UseBuffering"));
  614. obs_data_t *settings = obs_source_get_settings(data->source);
  615. v4l2_device_list(device_list, settings);
  616. obs_data_release(settings);
  617. obs_property_set_modified_callback(device_list, device_selected);
  618. obs_property_set_modified_callback(input_list, input_selected);
  619. obs_property_set_modified_callback(format_list, format_selected);
  620. obs_property_set_modified_callback(resolution_list,
  621. resolution_selected);
  622. return props;
  623. }
  624. static void v4l2_terminate(struct v4l2_data *data)
  625. {
  626. if (data->thread) {
  627. os_event_signal(data->event);
  628. pthread_join(data->thread, NULL);
  629. os_event_destroy(data->event);
  630. data->thread = 0;
  631. }
  632. v4l2_destroy_mmap(&data->buffers);
  633. if (data->dev != -1) {
  634. v4l2_close(data->dev);
  635. data->dev = -1;
  636. }
  637. }
  638. static void v4l2_destroy(void *vptr)
  639. {
  640. V4L2_DATA(vptr);
  641. if (!data)
  642. return;
  643. v4l2_terminate(data);
  644. if (data->device_id)
  645. bfree(data->device_id);
  646. #if HAVE_UDEV
  647. v4l2_unref_udev(data->udev);
  648. #endif
  649. bfree(data);
  650. }
  651. /**
  652. * Initialize the v4l2 device
  653. *
  654. * This function:
  655. * - tries to open the device
  656. * - sets pixelformat and requested resolution
  657. * - sets the requested framerate
  658. * - maps the buffers
  659. * - starts the capture thread
  660. */
  661. static void v4l2_init(struct v4l2_data *data)
  662. {
  663. uint32_t input_caps;
  664. int fps_num, fps_denom;
  665. blog(LOG_INFO, "Start capture from %s", data->device_id);
  666. data->dev = v4l2_open(data->device_id, O_RDWR | O_NONBLOCK);
  667. if (data->dev == -1) {
  668. blog(LOG_ERROR, "Unable to open device");
  669. goto fail;
  670. }
  671. /* set input */
  672. if (v4l2_set_input(data->dev, &data->input) < 0) {
  673. blog(LOG_ERROR, "Unable to set input %d", data->input);
  674. goto fail;
  675. }
  676. blog(LOG_INFO, "Input: %d", data->input);
  677. if (v4l2_get_input_caps(data->dev, -1, &input_caps) < 0) {
  678. blog(LOG_ERROR, "Unable to get input capabilities");
  679. goto fail;
  680. }
  681. /* set video standard if supported */
  682. if (input_caps & V4L2_IN_CAP_STD) {
  683. if (v4l2_set_standard(data->dev, &data->standard) < 0) {
  684. blog(LOG_ERROR, "Unable to set video standard");
  685. goto fail;
  686. }
  687. data->resolution = -1;
  688. data->framerate = -1;
  689. }
  690. /* set dv timing if supported */
  691. if (input_caps & V4L2_IN_CAP_DV_TIMINGS) {
  692. if (v4l2_set_dv_timing(data->dev, &data->dv_timing) < 0) {
  693. blog(LOG_ERROR, "Unable to set dv timing");
  694. goto fail;
  695. }
  696. data->resolution = -1;
  697. data->framerate = -1;
  698. }
  699. /* set pixel format and resolution */
  700. if (v4l2_set_format(data->dev, &data->resolution, &data->pixfmt,
  701. &data->linesize) < 0) {
  702. blog(LOG_ERROR, "Unable to set format");
  703. goto fail;
  704. }
  705. if (v4l2_to_obs_video_format(data->pixfmt) == VIDEO_FORMAT_NONE) {
  706. blog(LOG_ERROR, "Selected video format not supported");
  707. goto fail;
  708. }
  709. v4l2_unpack_tuple(&data->width, &data->height, data->resolution);
  710. blog(LOG_INFO, "Resolution: %dx%d", data->width, data->height);
  711. blog(LOG_INFO, "Pixelformat: %s", V4L2_FOURCC_STR(data->pixfmt));
  712. blog(LOG_INFO, "Linesize: %d Bytes", data->linesize);
  713. /* set framerate */
  714. if (v4l2_set_framerate(data->dev, &data->framerate) < 0) {
  715. blog(LOG_ERROR, "Unable to set framerate");
  716. goto fail;
  717. }
  718. v4l2_unpack_tuple(&fps_num, &fps_denom, data->framerate);
  719. blog(LOG_INFO, "Framerate: %.2f fps", (float) fps_denom / fps_num);
  720. /* map buffers */
  721. if (v4l2_create_mmap(data->dev, &data->buffers) < 0) {
  722. blog(LOG_ERROR, "Failed to map buffers");
  723. goto fail;
  724. }
  725. /* start the capture thread */
  726. if (os_event_init(&data->event, OS_EVENT_TYPE_MANUAL) != 0)
  727. goto fail;
  728. if (pthread_create(&data->thread, NULL, v4l2_thread, data) != 0)
  729. goto fail;
  730. return;
  731. fail:
  732. blog(LOG_ERROR, "Initialization failed");
  733. v4l2_terminate(data);
  734. }
  735. /** Update source flags depending on the settings */
  736. static void v4l2_update_source_flags(struct v4l2_data *data,
  737. obs_data_t *settings)
  738. {
  739. uint32_t flags = obs_source_get_flags(data->source);
  740. flags = (obs_data_get_bool(settings, "buffering"))
  741. ? flags & ~OBS_SOURCE_FLAG_UNBUFFERED
  742. : flags | OBS_SOURCE_FLAG_UNBUFFERED;
  743. obs_source_set_flags(data->source, flags);
  744. }
  745. /**
  746. * Update the settings for the v4l2 source
  747. *
  748. * Since there are very few settings that can be changed without restarting the
  749. * stream we don't bother to even try. Whenever this is called the currently
  750. * active stream (if exists) is stopped, the settings are updated and finally
  751. * the new stream is started.
  752. */
  753. static void v4l2_update(void *vptr, obs_data_t *settings)
  754. {
  755. V4L2_DATA(vptr);
  756. v4l2_terminate(data);
  757. if (data->device_id)
  758. bfree(data->device_id);
  759. data->device_id = bstrdup(obs_data_get_string(settings, "device_id"));
  760. data->input = obs_data_get_int(settings, "input");
  761. data->pixfmt = obs_data_get_int(settings, "pixelformat");
  762. data->standard = obs_data_get_int(settings, "standard");
  763. data->dv_timing = obs_data_get_int(settings, "dv_timing");
  764. data->resolution = obs_data_get_int(settings, "resolution");
  765. data->framerate = obs_data_get_int(settings, "framerate");
  766. v4l2_update_source_flags(data, settings);
  767. v4l2_init(data);
  768. }
  769. static void *v4l2_create(obs_data_t *settings, obs_source_t *source)
  770. {
  771. struct v4l2_data *data = bzalloc(sizeof(struct v4l2_data));
  772. data->dev = -1;
  773. data->source = source;
  774. /* Bitch about build problems ... */
  775. #ifndef V4L2_CAP_DEVICE_CAPS
  776. blog(LOG_WARNING, "Plugin built without device caps support!");
  777. #endif
  778. v4l2_update(data, settings);
  779. #if HAVE_UDEV
  780. data->udev = v4l2_init_udev();
  781. v4l2_set_device_added_callback(data->udev, &device_added, data);
  782. v4l2_set_device_removed_callback(data->udev, &device_removed, data);
  783. #endif
  784. return data;
  785. }
  786. struct obs_source_info v4l2_input = {
  787. .id = "v4l2_input",
  788. .type = OBS_SOURCE_TYPE_INPUT,
  789. .output_flags = OBS_SOURCE_ASYNC_VIDEO,
  790. .get_name = v4l2_getname,
  791. .create = v4l2_create,
  792. .destroy = v4l2_destroy,
  793. .update = v4l2_update,
  794. .get_defaults = v4l2_defaults,
  795. .get_properties = v4l2_properties
  796. };