v4l2-input.c 18 KB

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