v4l2-input.c 19 KB

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