1
0

v4l2-input.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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 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' (Index %d)", in.name,
  255. in.index);
  256. }
  257. in.index++;
  258. }
  259. }
  260. /*
  261. * List formats for device
  262. */
  263. static void v4l2_format_list(int dev, obs_property_t prop)
  264. {
  265. struct v4l2_fmtdesc fmt;
  266. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  267. fmt.index = 0;
  268. struct dstr buffer;
  269. dstr_init(&buffer);
  270. obs_property_list_clear(prop);
  271. while (v4l2_ioctl(dev, VIDIOC_ENUM_FMT, &fmt) == 0) {
  272. dstr_copy(&buffer, (char *) fmt.description);
  273. if (fmt.flags & V4L2_FMT_FLAG_EMULATED)
  274. dstr_cat(&buffer, " (Emulated)");
  275. if (v4l2_to_obs_video_format(fmt.pixelformat)
  276. != VIDEO_FORMAT_NONE) {
  277. obs_property_list_add_int(prop, buffer.array,
  278. fmt.pixelformat);
  279. blog(LOG_INFO, "Pixelformat: %s (available)",
  280. buffer.array);
  281. } else {
  282. blog(LOG_INFO, "Pixelformat: %s (unavailable)",
  283. buffer.array);
  284. }
  285. fmt.index++;
  286. }
  287. dstr_free(&buffer);
  288. }
  289. /*
  290. * List resolutions for device and format
  291. */
  292. static void v4l2_resolution_list(int dev, uint_fast32_t pixelformat,
  293. obs_property_t prop)
  294. {
  295. struct v4l2_frmsizeenum frmsize;
  296. frmsize.pixel_format = pixelformat;
  297. frmsize.index = 0;
  298. struct dstr buffer;
  299. dstr_init(&buffer);
  300. obs_property_list_clear(prop);
  301. v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize);
  302. switch(frmsize.type) {
  303. case V4L2_FRMSIZE_TYPE_DISCRETE:
  304. while (v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize) == 0) {
  305. dstr_printf(&buffer, "%dx%d", frmsize.discrete.width,
  306. frmsize.discrete.height);
  307. obs_property_list_add_int(prop, buffer.array,
  308. pack_tuple(frmsize.discrete.width,
  309. frmsize.discrete.height));
  310. frmsize.index++;
  311. }
  312. break;
  313. default:
  314. blog(LOG_INFO, "Stepwise and Continuous framesizes "
  315. "are currently hardcoded");
  316. for (const int *packed = v4l2_framesizes; *packed; ++packed) {
  317. int width;
  318. int height;
  319. unpack_tuple(&width, &height, *packed);
  320. dstr_printf(&buffer, "%dx%d", width, height);
  321. obs_property_list_add_int(prop, buffer.array, *packed);
  322. }
  323. break;
  324. }
  325. dstr_free(&buffer);
  326. }
  327. /*
  328. * List framerates for device and resolution
  329. */
  330. static void v4l2_framerate_list(int dev, uint_fast32_t pixelformat,
  331. uint_fast32_t width, uint_fast32_t height, obs_property_t prop)
  332. {
  333. struct v4l2_frmivalenum frmival;
  334. frmival.pixel_format = pixelformat;
  335. frmival.width = width;
  336. frmival.height = height;
  337. frmival.index = 0;
  338. struct dstr buffer;
  339. dstr_init(&buffer);
  340. obs_property_list_clear(prop);
  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 = 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. 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. 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. static obs_properties_t v4l2_properties(void)
  444. {
  445. obs_properties_t props = obs_properties_create();
  446. obs_property_t device_list = obs_properties_add_list(props,
  447. "device_id", obs_module_text("Device"),
  448. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  449. obs_property_t input_list = obs_properties_add_list(props,
  450. "input", obs_module_text("Input"),
  451. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  452. obs_property_t format_list = obs_properties_add_list(props,
  453. "pixelformat", obs_module_text("VideoFormat"),
  454. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  455. obs_property_t resolution_list = obs_properties_add_list(props,
  456. "resolution", obs_module_text("Resolution"),
  457. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  458. obs_properties_add_list(props,
  459. "framerate", obs_module_text("FrameRate"),
  460. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  461. v4l2_device_list(device_list, NULL);
  462. obs_property_set_modified_callback(device_list, device_selected);
  463. obs_property_set_modified_callback(input_list, input_selected);
  464. obs_property_set_modified_callback(format_list, format_selected);
  465. obs_property_set_modified_callback(resolution_list,
  466. resolution_selected);
  467. return props;
  468. }
  469. static void v4l2_terminate(struct v4l2_data *data)
  470. {
  471. if (data->thread) {
  472. os_event_signal(data->event);
  473. pthread_join(data->thread, NULL);
  474. os_event_destroy(data->event);
  475. data->thread = 0;
  476. }
  477. v4l2_destroy_mmap(&data->buffers);
  478. if (data->dev != -1) {
  479. v4l2_close(data->dev);
  480. data->dev = -1;
  481. }
  482. }
  483. static void v4l2_destroy(void *vptr)
  484. {
  485. V4L2_DATA(vptr);
  486. if (!data)
  487. return;
  488. v4l2_terminate(data);
  489. if (data->set_device)
  490. bfree(data->set_device);
  491. bfree(data);
  492. }
  493. /**
  494. * Initialize the v4l2 device
  495. *
  496. * This function:
  497. * - tries to open the device
  498. * - sets pixelformat and requested resolution
  499. * - sets the requested framerate
  500. * - maps the buffers
  501. * - starts the capture thread
  502. */
  503. static void v4l2_init(struct v4l2_data *data)
  504. {
  505. struct v4l2_format fmt;
  506. struct v4l2_streamparm par;
  507. struct dstr fps;
  508. int width, height;
  509. int fps_num, fps_denom;
  510. blog(LOG_INFO, "Start capture from %s", data->set_device);
  511. data->dev = v4l2_open(data->set_device, O_RDWR | O_NONBLOCK);
  512. if (data->dev == -1) {
  513. blog(LOG_ERROR, "Unable to open device");
  514. goto fail;
  515. }
  516. /* set input */
  517. if (v4l2_set_input(data->dev, &data->set_input) < 0) {
  518. blog(LOG_ERROR, "Unable to set input %d",
  519. data->set_input);
  520. goto fail;
  521. }
  522. blog(LOG_INFO, "Input: %d", data->set_input);
  523. /* set pixel format and resolution */
  524. unpack_tuple(&width, &height, data->set_res);
  525. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  526. fmt.fmt.pix.width = width;
  527. fmt.fmt.pix.height = height;
  528. fmt.fmt.pix.pixelformat = data->set_pixfmt;
  529. fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
  530. if (v4l2_ioctl(data->dev, VIDIOC_S_FMT, &fmt) < 0) {
  531. blog(LOG_ERROR, "Unable to set format");
  532. goto fail;
  533. }
  534. data->width = fmt.fmt.pix.width;
  535. data->height = fmt.fmt.pix.height;
  536. data->pixfmt = fmt.fmt.pix.pixelformat;
  537. data->linesize = fmt.fmt.pix.bytesperline;
  538. blog(LOG_INFO, "Resolution: %"PRIuFAST32"x%"PRIuFAST32,
  539. data->width, data->height);
  540. blog(LOG_INFO, "Linesize: %"PRIuFAST32" Bytes", data->linesize);
  541. /* set framerate */
  542. unpack_tuple(&fps_num, &fps_denom, data->set_fps);
  543. par.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  544. par.parm.capture.timeperframe.numerator = fps_num;
  545. par.parm.capture.timeperframe.denominator = fps_denom;
  546. if (v4l2_ioctl(data->dev, VIDIOC_S_PARM, &par) < 0) {
  547. blog(LOG_ERROR, "Unable to set framerate");
  548. goto fail;
  549. }
  550. dstr_init(&fps);
  551. dstr_printf(&fps, "%.2f",
  552. (float) par.parm.capture.timeperframe.denominator
  553. / par.parm.capture.timeperframe.numerator);
  554. blog(LOG_INFO, "Framerate: %s fps", fps.array);
  555. dstr_free(&fps);
  556. /* map buffers */
  557. if (v4l2_create_mmap(data->dev, &data->buffers) < 0) {
  558. blog(LOG_ERROR, "Failed to map buffers");
  559. goto fail;
  560. }
  561. /* start the capture thread */
  562. if (os_event_init(&data->event, OS_EVENT_TYPE_MANUAL) != 0)
  563. goto fail;
  564. if (pthread_create(&data->thread, NULL, v4l2_thread, data) != 0)
  565. goto fail;
  566. return;
  567. fail:
  568. blog(LOG_ERROR, "Initialization failed");
  569. v4l2_terminate(data);
  570. }
  571. static void v4l2_update(void *vptr, obs_data_t settings)
  572. {
  573. V4L2_DATA(vptr);
  574. bool restart = false;
  575. const char *new_device;
  576. new_device = obs_data_get_string(settings, "device_id");
  577. if (!data->set_device || strcmp(data->set_device, new_device) != 0) {
  578. if (data->set_device)
  579. bfree(data->set_device);
  580. data->set_device = bstrdup(new_device);
  581. restart = true;
  582. }
  583. if (data->set_input != obs_data_get_int(settings, "input")) {
  584. data->set_input = obs_data_get_int(settings, "input");
  585. restart = true;
  586. }
  587. if (data->set_pixfmt != obs_data_get_int(settings, "pixelformat")) {
  588. data->set_pixfmt = obs_data_get_int(settings, "pixelformat");
  589. restart = true;
  590. }
  591. if (data->set_res != obs_data_get_int(settings, "resolution")) {
  592. data->set_res = obs_data_get_int(settings, "resolution");
  593. restart = true;
  594. }
  595. if (data->set_fps != obs_data_get_int(settings, "framerate")) {
  596. data->set_fps = obs_data_get_int(settings, "framerate");
  597. restart = true;
  598. }
  599. if (restart) {
  600. v4l2_terminate(data);
  601. v4l2_init(data);
  602. }
  603. }
  604. static void *v4l2_create(obs_data_t settings, obs_source_t source)
  605. {
  606. struct v4l2_data *data = bzalloc(sizeof(struct v4l2_data));
  607. data->dev = -1;
  608. data->source = source;
  609. v4l2_update(data, settings);
  610. return data;
  611. }
  612. struct obs_source_info v4l2_input = {
  613. .id = "v4l2_input",
  614. .type = OBS_SOURCE_TYPE_INPUT,
  615. .output_flags = OBS_SOURCE_ASYNC_VIDEO,
  616. .get_name = v4l2_getname,
  617. .create = v4l2_create,
  618. .destroy = v4l2_destroy,
  619. .update = v4l2_update,
  620. .get_defaults = v4l2_defaults,
  621. .get_properties = v4l2_properties
  622. };