v4l2-input.c 18 KB

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