v4l2-input.c 20 KB

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