v4l2-input.c 18 KB

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