v4l2-input.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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/mman.h>
  22. #include <sys/time.h>
  23. #include <sys/types.h>
  24. #include <sys/ioctl.h>
  25. #include <sys/select.h>
  26. #include <linux/videodev2.h>
  27. #include <util/threading.h>
  28. #include <util/bmem.h>
  29. #include <util/dstr.h>
  30. #include <obs-module.h>
  31. #define V4L2_DATA(voidptr) struct v4l2_data *data = voidptr;
  32. #define timeval2ns(tv) \
  33. (((uint64_t) tv.tv_sec * 1000000000) + ((uint64_t) tv.tv_usec * 1000))
  34. #define blog(level, msg, ...) blog(level, "v4l2-input: " msg, ##__VA_ARGS__)
  35. struct v4l2_buffer_data {
  36. size_t length;
  37. void *start;
  38. };
  39. struct v4l2_data {
  40. char *device;
  41. pthread_t thread;
  42. os_event_t event;
  43. obs_source_t source;
  44. uint_fast32_t linesize;
  45. uint64_t frames;
  46. int_fast32_t dev;
  47. int_fast32_t pixelformat;
  48. int_fast32_t width;
  49. int_fast32_t height;
  50. int_fast32_t fps_numerator;
  51. int_fast32_t fps_denominator;
  52. uint_fast32_t buf_count;
  53. struct v4l2_buffer_data *buf;
  54. };
  55. static enum video_format v4l2_to_obs_video_format(uint_fast32_t format)
  56. {
  57. switch (format) {
  58. case V4L2_PIX_FMT_YVYU: return VIDEO_FORMAT_YVYU;
  59. case V4L2_PIX_FMT_YUYV: return VIDEO_FORMAT_YUY2;
  60. case V4L2_PIX_FMT_UYVY: return VIDEO_FORMAT_UYVY;
  61. case V4L2_PIX_FMT_NV12: return VIDEO_FORMAT_NV12;
  62. case V4L2_PIX_FMT_YUV420: return VIDEO_FORMAT_I420;
  63. default: return VIDEO_FORMAT_NONE;
  64. }
  65. }
  66. /*
  67. * used to store framerate and resolution values
  68. */
  69. static int pack_tuple(int a, int b)
  70. {
  71. return (a << 16) | (b & 0xffff);
  72. }
  73. static void unpack_tuple(int *a, int *b, int packed)
  74. {
  75. *a = packed >> 16;
  76. *b = packed & 0xffff;
  77. }
  78. /* fixed framesizes as fallback */
  79. static int fixed_framesizes[] =
  80. {
  81. /* 4:3 */
  82. 160<<16 | 120,
  83. 320<<16 | 240,
  84. 480<<16 | 320,
  85. 640<<16 | 480,
  86. 800<<16 | 600,
  87. 1024<<16 | 768,
  88. 1280<<16 | 960,
  89. 1440<<16 | 1050,
  90. 1440<<16 | 1080,
  91. 1600<<16 | 1200,
  92. /* 16:9 */
  93. 640<<16 | 360,
  94. 960<<16 | 540,
  95. 1280<<16 | 720,
  96. 1600<<16 | 900,
  97. 1920<<16 | 1080,
  98. 1920<<16 | 1200,
  99. /* tv */
  100. 432<<16 | 520,
  101. 480<<16 | 320,
  102. 480<<16 | 530,
  103. 486<<16 | 440,
  104. 576<<16 | 310,
  105. 576<<16 | 520,
  106. 576<<16 | 570,
  107. 1024<<16 | 576,
  108. 0
  109. };
  110. /* fixed framerates as fallback */
  111. static int fixed_framerates[] =
  112. {
  113. 1<<16 | 60,
  114. 1<<16 | 50,
  115. 1<<16 | 30,
  116. 1<<16 | 25,
  117. 1<<16 | 20,
  118. 1<<16 | 15,
  119. 1<<16 | 10,
  120. 1<<16 | 5,
  121. 0
  122. };
  123. /*
  124. * start capture
  125. */
  126. static int_fast32_t v4l2_start_capture(struct v4l2_data *data)
  127. {
  128. enum v4l2_buf_type type;
  129. for (uint_fast32_t i = 0; i < data->buf_count; ++i) {
  130. struct v4l2_buffer buf;
  131. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  132. buf.memory = V4L2_MEMORY_MMAP;
  133. buf.index = i;
  134. if (ioctl(data->dev, VIDIOC_QBUF, &buf) < 0) {
  135. blog(LOG_ERROR, "unable to queue buffer");
  136. return -1;
  137. }
  138. }
  139. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  140. if (ioctl(data->dev, VIDIOC_STREAMON, &type) < 0) {
  141. blog(LOG_ERROR, "unable to start stream");
  142. return -1;
  143. }
  144. return 0;
  145. }
  146. /*
  147. * stop capture
  148. */
  149. static int_fast32_t v4l2_stop_capture(struct v4l2_data *data)
  150. {
  151. enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  152. if (ioctl(data->dev, VIDIOC_STREAMOFF, &type) < 0) {
  153. blog(LOG_ERROR, "unable to stop stream");
  154. }
  155. return 0;
  156. }
  157. /*
  158. * create memory mapping for buffers
  159. */
  160. static int_fast32_t v4l2_create_mmap(struct v4l2_data *data)
  161. {
  162. struct v4l2_requestbuffers req;
  163. req.count = 4;
  164. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  165. req.memory = V4L2_MEMORY_MMAP;
  166. if (ioctl(data->dev, VIDIOC_REQBUFS, &req) < 0) {
  167. blog(LOG_DEBUG, "request for buffers failed !");
  168. return -1;
  169. }
  170. if (req.count < 2) {
  171. blog(LOG_DEBUG, "not enough memory !");
  172. return -1;
  173. }
  174. data->buf_count = req.count;
  175. data->buf = bzalloc(req.count * sizeof(struct v4l2_buffer_data));
  176. for (uint_fast32_t i = 0; i < req.count; ++i) {
  177. struct v4l2_buffer buf;
  178. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  179. buf.memory = V4L2_MEMORY_MMAP;
  180. buf.index = i;
  181. if (ioctl(data->dev, VIDIOC_QUERYBUF, &buf) < 0) {
  182. blog(LOG_ERROR, "failed to query buffer");
  183. return -1;
  184. }
  185. data->buf[i].length = buf.length;
  186. data->buf[i].start = mmap(NULL, buf.length,
  187. PROT_READ | PROT_WRITE, MAP_SHARED,
  188. data->dev, buf.m.offset);
  189. if (data->buf[i].start == MAP_FAILED) {
  190. blog(LOG_ERROR, "mmap for buffer failed");
  191. return -1;
  192. }
  193. }
  194. return 0;
  195. }
  196. /*
  197. * destroy memory mapping for buffers
  198. */
  199. static void v4l2_destroy_mmap(struct v4l2_data *data)
  200. {
  201. for(uint_fast32_t i = 0; i < data->buf_count; ++i) {
  202. if (data->buf[i].start != MAP_FAILED)
  203. munmap(data->buf[i].start, data->buf[i].length);
  204. }
  205. data->buf_count = 0;
  206. bfree(data->buf);
  207. }
  208. /*
  209. * Worker thread to get video data
  210. */
  211. static void *v4l2_thread(void *vptr)
  212. {
  213. V4L2_DATA(vptr);
  214. if (v4l2_start_capture(data) < 0)
  215. goto exit;
  216. data->frames = 0;
  217. blog(LOG_INFO, "Started recording from %s", data->device);
  218. while (os_event_try(data->event) == EAGAIN) {
  219. int r;
  220. fd_set fds;
  221. struct timeval tv;
  222. struct v4l2_buffer buf;
  223. struct source_frame out;
  224. FD_ZERO(&fds);
  225. FD_SET(data->dev, &fds);
  226. tv.tv_sec = 1;
  227. tv.tv_usec = 0;
  228. r = select(data->dev + 1, &fds, NULL, NULL, &tv);
  229. if (r < 0) {
  230. if (errno == EINTR)
  231. continue;
  232. blog(LOG_DEBUG, "select failed");
  233. break;
  234. } else if (r == 0) {
  235. blog(LOG_DEBUG, "select timeout");
  236. continue;
  237. }
  238. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  239. buf.memory = V4L2_MEMORY_MMAP;
  240. if (ioctl(data->dev, VIDIOC_DQBUF, &buf) < 0) {
  241. if (errno == EAGAIN)
  242. continue;
  243. blog(LOG_DEBUG, "failed to dequeue buffer");
  244. break;
  245. }
  246. video_format_get_parameters(VIDEO_CS_DEFAULT,
  247. VIDEO_RANGE_PARTIAL, out.color_matrix,
  248. out.color_range_min, out.color_range_max);
  249. out.data[0] = (uint8_t *) data->buf[buf.index].start;
  250. out.linesize[0] = data->linesize;
  251. out.width = data->width;
  252. out.height = data->height;
  253. out.timestamp = timeval2ns(buf.timestamp);
  254. out.format = v4l2_to_obs_video_format(data->pixelformat);
  255. obs_source_output_video(data->source, &out);
  256. if (ioctl(data->dev, VIDIOC_QBUF, &buf) < 0) {
  257. blog(LOG_DEBUG, "failed to enqueue buffer");
  258. break;
  259. }
  260. data->frames++;
  261. }
  262. blog(LOG_INFO, "Stopped recording from %s after %"PRIu64" frames",
  263. data->device, data->frames);
  264. exit:
  265. v4l2_stop_capture(data);
  266. return NULL;
  267. }
  268. static const char* v4l2_getname(void)
  269. {
  270. return obs_module_text("V4L2Input");
  271. }
  272. static void v4l2_defaults(obs_data_t settings)
  273. {
  274. obs_data_set_default_int(settings, "pixelformat", V4L2_PIX_FMT_YUYV);
  275. obs_data_set_default_int(settings, "resolution",
  276. pack_tuple(640, 480));
  277. obs_data_set_default_int(settings, "framerate", pack_tuple(1, 30));
  278. }
  279. /*
  280. * List available devices
  281. */
  282. static void v4l2_device_list(obs_property_t prop, obs_data_t settings)
  283. {
  284. DIR *dirp;
  285. struct dirent *dp;
  286. int fd;
  287. struct v4l2_capability video_cap;
  288. struct dstr device;
  289. bool first = true;
  290. dirp = opendir("/sys/class/video4linux");
  291. if (!dirp)
  292. return;
  293. obs_property_list_clear(prop);
  294. dstr_init_copy(&device, "/dev/");
  295. while ((dp = readdir(dirp)) != NULL) {
  296. if (dp->d_type == DT_DIR)
  297. continue;
  298. dstr_resize(&device, 5);
  299. dstr_cat(&device, dp->d_name);
  300. if ((fd = open(device.array, O_RDWR | O_NONBLOCK)) == -1) {
  301. blog(LOG_INFO, "Unable to open %s", device.array);
  302. continue;
  303. }
  304. if (ioctl(fd, VIDIOC_QUERYCAP, &video_cap) == -1) {
  305. blog(LOG_INFO, "Failed to query capabilities for %s",
  306. device.array);
  307. } else if (video_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) {
  308. obs_property_list_add_string(prop,
  309. (char *) video_cap.card,
  310. device.array);
  311. if (first) {
  312. obs_data_setstring(settings,
  313. "device_id", device.array);
  314. first = false;
  315. }
  316. blog(LOG_INFO, "Found device '%s' at %s",
  317. video_cap.card, device.array);
  318. }
  319. else {
  320. blog(LOG_INFO, "%s seems to not support video capture",
  321. device.array);
  322. }
  323. close(fd);
  324. }
  325. closedir(dirp);
  326. dstr_free(&device);
  327. }
  328. /*
  329. * List formats for device
  330. */
  331. static void v4l2_format_list(int dev, obs_property_t prop)
  332. {
  333. struct v4l2_fmtdesc fmt;
  334. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  335. fmt.index = 0;
  336. obs_property_list_clear(prop);
  337. while (ioctl(dev, VIDIOC_ENUM_FMT, &fmt) == 0) {
  338. if (v4l2_to_obs_video_format(fmt.pixelformat)
  339. != VIDEO_FORMAT_NONE) {
  340. obs_property_list_add_int(prop,
  341. (char *) fmt.description,
  342. fmt.pixelformat);
  343. blog(LOG_INFO, "Pixelformat: %s (available)",
  344. (char *) fmt.description);
  345. } else {
  346. blog(LOG_INFO, "Pixelformat: %s (unavailable)",
  347. (char *) fmt.description);
  348. }
  349. fmt.index++;
  350. }
  351. }
  352. /*
  353. * List resolutions for device and format
  354. */
  355. static void v4l2_resolution_list(int dev, uint_fast32_t pixelformat,
  356. obs_property_t prop)
  357. {
  358. struct v4l2_frmsizeenum frmsize;
  359. frmsize.pixel_format = pixelformat;
  360. frmsize.index = 0;
  361. struct dstr buffer;
  362. dstr_init(&buffer);
  363. obs_property_list_clear(prop);
  364. ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize);
  365. switch(frmsize.type) {
  366. case V4L2_FRMSIZE_TYPE_DISCRETE:
  367. while (ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize) == 0) {
  368. dstr_printf(&buffer, "%dx%d", frmsize.discrete.width,
  369. frmsize.discrete.height);
  370. obs_property_list_add_int(prop, buffer.array,
  371. pack_tuple(frmsize.discrete.width,
  372. frmsize.discrete.height));
  373. frmsize.index++;
  374. }
  375. break;
  376. default:
  377. blog(LOG_INFO, "Stepwise and Continuous framesizes "
  378. "are currently hardcoded");
  379. for (uint_fast32_t i = 0; ; ++i) {
  380. int packed = fixed_framesizes[i];
  381. if (!packed)
  382. break;
  383. int width;
  384. int height;
  385. unpack_tuple(&width, &height, packed);
  386. dstr_printf(&buffer, "%dx%d", width, height);
  387. obs_property_list_add_int(prop, buffer.array, packed);
  388. }
  389. break;
  390. }
  391. dstr_free(&buffer);
  392. }
  393. /*
  394. * List framerates for device and resolution
  395. */
  396. static void v4l2_framerate_list(int dev, uint_fast32_t pixelformat,
  397. uint_fast32_t width, uint_fast32_t height, obs_property_t prop)
  398. {
  399. struct v4l2_frmivalenum frmival;
  400. frmival.pixel_format = pixelformat;
  401. frmival.width = width;
  402. frmival.height = height;
  403. frmival.index = 0;
  404. struct dstr buffer;
  405. dstr_init(&buffer);
  406. obs_property_list_clear(prop);
  407. ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS, &frmival);
  408. switch(frmival.type) {
  409. case V4L2_FRMIVAL_TYPE_DISCRETE:
  410. while (ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS, &frmival) == 0) {
  411. float fps = (float) frmival.discrete.denominator /
  412. frmival.discrete.numerator;
  413. int pack = pack_tuple(frmival.discrete.numerator,
  414. frmival.discrete.denominator);
  415. dstr_printf(&buffer, "%.2f", fps);
  416. obs_property_list_add_int(prop, buffer.array, pack);
  417. frmival.index++;
  418. }
  419. break;
  420. default:
  421. blog(LOG_INFO, "Stepwise and Continuous framerates "
  422. "are currently hardcoded");
  423. for (uint_fast32_t i = 0; ; ++i) {
  424. int packed = fixed_framerates[i];
  425. if (!packed)
  426. break;
  427. int num;
  428. int denom;
  429. unpack_tuple(&num, &denom, packed);
  430. float fps = (float) denom / num;
  431. dstr_printf(&buffer, "%.2f", fps);
  432. obs_property_list_add_int(prop, buffer.array, packed);
  433. }
  434. break;
  435. }
  436. dstr_free(&buffer);
  437. }
  438. /*
  439. * Device selected callback
  440. */
  441. static bool device_selected(obs_properties_t props, obs_property_t p,
  442. obs_data_t settings)
  443. {
  444. UNUSED_PARAMETER(p);
  445. int dev = open(obs_data_getstring(settings, "device_id"),
  446. O_RDWR | O_NONBLOCK);
  447. if (dev == -1)
  448. return false;
  449. obs_property_t prop = obs_properties_get(props, "pixelformat");
  450. v4l2_format_list(dev, prop);
  451. obs_property_modified(prop, settings);
  452. close(dev);
  453. return true;
  454. }
  455. /*
  456. * Format selected callback
  457. */
  458. static bool format_selected(obs_properties_t props, obs_property_t p,
  459. obs_data_t settings)
  460. {
  461. UNUSED_PARAMETER(p);
  462. int dev = open(obs_data_getstring(settings, "device_id"),
  463. O_RDWR | O_NONBLOCK);
  464. if (dev == -1)
  465. return false;
  466. obs_property_t prop = obs_properties_get(props, "resolution");
  467. v4l2_resolution_list(dev, obs_data_getint(settings, "pixelformat"),
  468. prop);
  469. obs_property_modified(prop, settings);
  470. close(dev);
  471. return true;
  472. }
  473. /*
  474. * Resolution selected callback
  475. */
  476. static bool resolution_selected(obs_properties_t props, obs_property_t p,
  477. obs_data_t settings)
  478. {
  479. UNUSED_PARAMETER(p);
  480. int width, height;
  481. int dev = open(obs_data_getstring(settings, "device_id"),
  482. O_RDWR | O_NONBLOCK);
  483. if (dev == -1)
  484. return false;
  485. obs_property_t prop = obs_properties_get(props, "framerate");
  486. unpack_tuple(&width, &height, obs_data_getint(settings,
  487. "resolution"));
  488. v4l2_framerate_list(dev, obs_data_getint(settings, "pixelformat"),
  489. width, height, prop);
  490. obs_property_modified(prop, settings);
  491. close(dev);
  492. return true;
  493. }
  494. static obs_properties_t v4l2_properties(void)
  495. {
  496. /* TODO: locale */
  497. obs_properties_t props = obs_properties_create();
  498. obs_property_t device_list = obs_properties_add_list(props,
  499. "device_id", obs_module_text("Device"),
  500. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  501. obs_property_t format_list = obs_properties_add_list(props,
  502. "pixelformat", obs_module_text("VideoFormat"),
  503. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  504. obs_property_t resolution_list = obs_properties_add_list(props,
  505. "resolution", obs_module_text("Resolution"),
  506. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  507. obs_properties_add_list(props,
  508. "framerate", obs_module_text("FrameRate"),
  509. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  510. v4l2_device_list(device_list, NULL);
  511. obs_property_set_modified_callback(device_list, device_selected);
  512. obs_property_set_modified_callback(format_list, format_selected);
  513. obs_property_set_modified_callback(resolution_list,
  514. resolution_selected);
  515. return props;
  516. }
  517. static uint32_t v4l2_getwidth(void *vptr)
  518. {
  519. V4L2_DATA(vptr);
  520. return data->width;
  521. }
  522. static uint32_t v4l2_getheight(void *vptr)
  523. {
  524. V4L2_DATA(vptr);
  525. return data->height;
  526. }
  527. static void v4l2_terminate(struct v4l2_data *data)
  528. {
  529. if (data->thread) {
  530. os_event_signal(data->event);
  531. pthread_join(data->thread, NULL);
  532. os_event_destroy(data->event);
  533. }
  534. if (data->buf_count)
  535. v4l2_destroy_mmap(data);
  536. if (data->dev != -1) {
  537. close(data->dev);
  538. data->dev = -1;
  539. }
  540. }
  541. static void v4l2_destroy(void *vptr)
  542. {
  543. V4L2_DATA(vptr);
  544. if (!data)
  545. return;
  546. v4l2_terminate(data);
  547. if (data->device)
  548. bfree(data->device);
  549. bfree(data);
  550. }
  551. static void v4l2_init(struct v4l2_data *data)
  552. {
  553. struct v4l2_format fmt;
  554. struct v4l2_streamparm par;
  555. data->dev = open(data->device, O_RDWR | O_NONBLOCK);
  556. if (data->dev == -1) {
  557. blog(LOG_ERROR, "Unable to open device: %s", data->device);
  558. goto fail;
  559. }
  560. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  561. fmt.fmt.pix.width = data->width;
  562. fmt.fmt.pix.height = data->height;
  563. fmt.fmt.pix.pixelformat = data->pixelformat;
  564. fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
  565. if (ioctl(data->dev, VIDIOC_S_FMT, &fmt) < 0) {
  566. blog(LOG_DEBUG, "unable to set format");
  567. goto fail;
  568. }
  569. data->pixelformat = fmt.fmt.pix.pixelformat;
  570. data->width = fmt.fmt.pix.width;
  571. data->height = fmt.fmt.pix.height;
  572. par.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  573. par.parm.capture.timeperframe.numerator = data->fps_numerator;
  574. par.parm.capture.timeperframe.denominator = data->fps_denominator;
  575. if (ioctl(data->dev, VIDIOC_S_PARM, &par) < 0) {
  576. blog(LOG_DEBUG, "unable to set params");
  577. goto fail;
  578. }
  579. data->fps_numerator = par.parm.capture.timeperframe.numerator;
  580. data->fps_denominator = par.parm.capture.timeperframe.denominator;
  581. data->linesize = fmt.fmt.pix.bytesperline;
  582. blog(LOG_DEBUG, "Linesize: %"PRIuFAST32, data->linesize);
  583. if (v4l2_create_mmap(data) < 0) {
  584. blog(LOG_ERROR, "failed to map buffers");
  585. goto fail;
  586. }
  587. if (os_event_init(&data->event, OS_EVENT_TYPE_MANUAL) != 0)
  588. goto fail;
  589. if (pthread_create(&data->thread, NULL, v4l2_thread, data) != 0)
  590. goto fail;
  591. return;
  592. fail:
  593. blog(LOG_DEBUG, "initialization failed");
  594. v4l2_terminate(data);
  595. }
  596. static void v4l2_update(void *vptr, obs_data_t settings)
  597. {
  598. V4L2_DATA(vptr);
  599. bool restart = false;
  600. const char *new_device;
  601. int width, height, fps_num, fps_denom;
  602. new_device = obs_data_getstring(settings, "device_id");
  603. if (strlen(new_device) == 0) {
  604. v4l2_device_list(NULL, settings);
  605. new_device = obs_data_getstring(settings, "device_id");
  606. }
  607. if (!data->device || strcmp(data->device, new_device) != 0) {
  608. if (data->device)
  609. bfree(data->device);
  610. data->device = bstrdup(new_device);
  611. restart = true;
  612. }
  613. if (data->pixelformat != obs_data_getint(settings, "pixelformat")) {
  614. data->pixelformat = obs_data_getint(settings, "pixelformat");
  615. restart = true;
  616. }
  617. unpack_tuple(&width, &height, obs_data_getint(settings,
  618. "resolution"));
  619. if (width != data->width || height != data->height) {
  620. restart = true;
  621. }
  622. unpack_tuple(&fps_num, &fps_denom, obs_data_getint(settings,
  623. "framerate"));
  624. if (fps_num != data->fps_numerator
  625. || fps_denom != data->fps_denominator) {
  626. data->fps_numerator = fps_num;
  627. data->fps_denominator = fps_denom;
  628. restart = true;
  629. }
  630. if (restart) {
  631. v4l2_terminate(data);
  632. /* Wait for v4l2_thread to finish before
  633. * updating width and height */
  634. data->width = width;
  635. data->height = height;
  636. v4l2_init(data);
  637. }
  638. }
  639. static void *v4l2_create(obs_data_t settings, obs_source_t source)
  640. {
  641. UNUSED_PARAMETER(settings);
  642. struct v4l2_data *data = bzalloc(sizeof(struct v4l2_data));
  643. data->dev = -1;
  644. data->source = source;
  645. v4l2_update(data, settings);
  646. blog(LOG_DEBUG, "New input created");
  647. return data;
  648. }
  649. struct obs_source_info v4l2_input = {
  650. .id = "v4l2_input",
  651. .type = OBS_SOURCE_TYPE_INPUT,
  652. .output_flags = OBS_SOURCE_ASYNC_VIDEO,
  653. .getname = v4l2_getname,
  654. .create = v4l2_create,
  655. .destroy = v4l2_destroy,
  656. .update = v4l2_update,
  657. .defaults = v4l2_defaults,
  658. .properties = v4l2_properties,
  659. .getwidth = v4l2_getwidth,
  660. .getheight = v4l2_getheight
  661. };