v4l2-input.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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 const 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 const 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. * This tries to map at least 2, preferably 4, buffers to userspace.
  177. *
  178. * @return 0 on success, -1 on failure
  179. */
  180. static int_fast32_t v4l2_create_mmap(struct v4l2_data *data)
  181. {
  182. struct v4l2_requestbuffers req;
  183. req.count = 4;
  184. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  185. req.memory = V4L2_MEMORY_MMAP;
  186. if (v4l2_ioctl(data->dev, VIDIOC_REQBUFS, &req) < 0) {
  187. blog(LOG_ERROR, "Request for buffers failed !");
  188. return -1;
  189. }
  190. if (req.count < 2) {
  191. blog(LOG_ERROR, "Device returned less than 2 buffers");
  192. return -1;
  193. }
  194. data->buf_count = req.count;
  195. data->buf = bzalloc(req.count * sizeof(struct v4l2_buffer_data));
  196. for (uint_fast32_t i = 0; i < req.count; ++i) {
  197. struct v4l2_buffer buf;
  198. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  199. buf.memory = V4L2_MEMORY_MMAP;
  200. buf.index = i;
  201. if (v4l2_ioctl(data->dev, VIDIOC_QUERYBUF, &buf) < 0) {
  202. blog(LOG_ERROR, "Failed to query buffer details");
  203. return -1;
  204. }
  205. data->buf[i].length = buf.length;
  206. data->buf[i].start = v4l2_mmap(NULL, buf.length,
  207. PROT_READ | PROT_WRITE, MAP_SHARED,
  208. data->dev, buf.m.offset);
  209. if (data->buf[i].start == MAP_FAILED) {
  210. blog(LOG_ERROR, "mmap for buffer failed");
  211. return -1;
  212. }
  213. }
  214. return 0;
  215. }
  216. /**
  217. * Destroy memory mapping for buffers
  218. */
  219. static void v4l2_destroy_mmap(struct v4l2_data *data)
  220. {
  221. for(uint_fast32_t i = 0; i < data->buf_count; ++i) {
  222. if (data->buf[i].start != MAP_FAILED)
  223. v4l2_munmap(data->buf[i].start, data->buf[i].length);
  224. }
  225. data->buf_count = 0;
  226. bfree(data->buf);
  227. }
  228. /**
  229. * Prepare the output frame structure for obs and compute plane offsets
  230. *
  231. * Basically all data apart from memory pointers and the timestamp is known
  232. * before the capture starts. This function prepares the obs_source_frame
  233. * struct with all the data that is already known.
  234. *
  235. * v4l2 uses a continuous memory segment for all planes so we simply compute
  236. * offsets to add to the start address in order to give obs the correct data
  237. * pointers for the individual planes.
  238. */
  239. static void v4l2_prep_obs_frame(struct v4l2_data *data,
  240. struct obs_source_frame *frame, size_t *plane_offsets)
  241. {
  242. memset(frame, 0, sizeof(struct obs_source_frame));
  243. memset(plane_offsets, 0, sizeof(size_t) * MAX_AV_PLANES);
  244. frame->width = data->width;
  245. frame->height = data->height;
  246. frame->format = v4l2_to_obs_video_format(data->pixfmt);
  247. video_format_get_parameters(VIDEO_CS_DEFAULT, VIDEO_RANGE_PARTIAL,
  248. frame->color_matrix, frame->color_range_min,
  249. frame->color_range_max);
  250. switch(data->pixfmt) {
  251. case V4L2_PIX_FMT_NV12:
  252. frame->linesize[0] = data->linesize;
  253. frame->linesize[1] = data->linesize / 2;
  254. plane_offsets[1] = data->linesize * data->height;
  255. break;
  256. case V4L2_PIX_FMT_YVU420:
  257. frame->linesize[0] = data->linesize;
  258. frame->linesize[1] = data->linesize / 2;
  259. frame->linesize[2] = data->linesize / 2;
  260. plane_offsets[1] = data->linesize * data->height * 5 / 4;
  261. plane_offsets[2] = data->linesize * data->height;
  262. break;
  263. case V4L2_PIX_FMT_YUV420:
  264. frame->linesize[0] = data->linesize;
  265. frame->linesize[1] = data->linesize / 2;
  266. frame->linesize[2] = data->linesize / 2;
  267. plane_offsets[1] = data->linesize * data->height;
  268. plane_offsets[2] = data->linesize * data->height * 5 / 4;
  269. break;
  270. default:
  271. frame->linesize[0] = data->linesize;
  272. break;
  273. }
  274. }
  275. /*
  276. * Worker thread to get video data
  277. */
  278. static void *v4l2_thread(void *vptr)
  279. {
  280. V4L2_DATA(vptr);
  281. int r;
  282. fd_set fds;
  283. uint8_t *start;
  284. struct timeval tv;
  285. struct v4l2_buffer buf;
  286. struct obs_source_frame out;
  287. size_t plane_offsets[MAX_AV_PLANES];
  288. if (v4l2_start_capture(data) < 0)
  289. goto exit;
  290. data->frames = 0;
  291. FD_ZERO(&fds);
  292. FD_SET(data->dev, &fds);
  293. v4l2_prep_obs_frame(data, &out, plane_offsets);
  294. while (os_event_try(data->event) == EAGAIN) {
  295. tv.tv_sec = 1;
  296. tv.tv_usec = 0;
  297. r = select(data->dev + 1, &fds, NULL, NULL, &tv);
  298. if (r < 0) {
  299. if (errno == EINTR)
  300. continue;
  301. blog(LOG_DEBUG, "select failed");
  302. break;
  303. } else if (r == 0) {
  304. blog(LOG_DEBUG, "select timeout");
  305. continue;
  306. }
  307. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  308. buf.memory = V4L2_MEMORY_MMAP;
  309. if (v4l2_ioctl(data->dev, VIDIOC_DQBUF, &buf) < 0) {
  310. if (errno == EAGAIN)
  311. continue;
  312. blog(LOG_DEBUG, "failed to dequeue buffer");
  313. break;
  314. }
  315. out.timestamp = timeval2ns(buf.timestamp);
  316. start = (uint8_t *) data->buf[buf.index].start;
  317. for (uint_fast32_t i = 0; i < MAX_AV_PLANES; ++i)
  318. out.data[i] = start + plane_offsets[i];
  319. obs_source_output_video(data->source, &out);
  320. if (v4l2_ioctl(data->dev, VIDIOC_QBUF, &buf) < 0) {
  321. blog(LOG_DEBUG, "failed to enqueue buffer");
  322. break;
  323. }
  324. data->frames++;
  325. }
  326. blog(LOG_INFO, "Stopped capture after %"PRIu64" frames", data->frames);
  327. exit:
  328. v4l2_stop_capture(data);
  329. return NULL;
  330. }
  331. static const char* v4l2_getname(void)
  332. {
  333. return obs_module_text("V4L2Input");
  334. }
  335. static void v4l2_defaults(obs_data_t settings)
  336. {
  337. obs_data_set_default_int(settings, "pixelformat", V4L2_PIX_FMT_YUYV);
  338. obs_data_set_default_int(settings, "resolution",
  339. pack_tuple(640, 480));
  340. obs_data_set_default_int(settings, "framerate", pack_tuple(1, 30));
  341. }
  342. /*
  343. * List available devices
  344. */
  345. static void v4l2_device_list(obs_property_t prop, obs_data_t settings)
  346. {
  347. DIR *dirp;
  348. struct dirent *dp;
  349. int fd;
  350. struct v4l2_capability video_cap;
  351. struct dstr device;
  352. bool first = true;
  353. dirp = opendir("/sys/class/video4linux");
  354. if (!dirp)
  355. return;
  356. obs_property_list_clear(prop);
  357. dstr_init_copy(&device, "/dev/");
  358. while ((dp = readdir(dirp)) != NULL) {
  359. if (dp->d_type == DT_DIR)
  360. continue;
  361. dstr_resize(&device, 5);
  362. dstr_cat(&device, dp->d_name);
  363. if ((fd = v4l2_open(device.array, O_RDWR | O_NONBLOCK)) == -1) {
  364. blog(LOG_INFO, "Unable to open %s", device.array);
  365. continue;
  366. }
  367. if (v4l2_ioctl(fd, VIDIOC_QUERYCAP, &video_cap) == -1) {
  368. blog(LOG_INFO, "Failed to query capabilities for %s",
  369. device.array);
  370. } else if (video_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) {
  371. obs_property_list_add_string(prop,
  372. (char *) video_cap.card,
  373. device.array);
  374. if (first) {
  375. obs_data_set_string(settings,
  376. "device_id", device.array);
  377. first = false;
  378. }
  379. blog(LOG_INFO, "Found device '%s' at %s",
  380. video_cap.card, device.array);
  381. }
  382. else {
  383. blog(LOG_INFO, "%s seems to not support video capture",
  384. device.array);
  385. }
  386. close(fd);
  387. }
  388. closedir(dirp);
  389. dstr_free(&device);
  390. }
  391. /*
  392. * List formats for device
  393. */
  394. static void v4l2_format_list(int dev, obs_property_t prop)
  395. {
  396. struct v4l2_fmtdesc fmt;
  397. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  398. fmt.index = 0;
  399. struct dstr buffer;
  400. dstr_init(&buffer);
  401. obs_property_list_clear(prop);
  402. while (v4l2_ioctl(dev, VIDIOC_ENUM_FMT, &fmt) == 0) {
  403. dstr_copy(&buffer, (char *) fmt.description);
  404. if (fmt.flags & V4L2_FMT_FLAG_EMULATED)
  405. dstr_cat(&buffer, " (Emulated)");
  406. if (v4l2_to_obs_video_format(fmt.pixelformat)
  407. != VIDEO_FORMAT_NONE) {
  408. obs_property_list_add_int(prop, buffer.array,
  409. fmt.pixelformat);
  410. blog(LOG_INFO, "Pixelformat: %s (available)",
  411. buffer.array);
  412. } else {
  413. blog(LOG_INFO, "Pixelformat: %s (unavailable)",
  414. buffer.array);
  415. }
  416. fmt.index++;
  417. }
  418. dstr_free(&buffer);
  419. }
  420. /*
  421. * List resolutions for device and format
  422. */
  423. static void v4l2_resolution_list(int dev, uint_fast32_t pixelformat,
  424. obs_property_t prop)
  425. {
  426. struct v4l2_frmsizeenum frmsize;
  427. frmsize.pixel_format = pixelformat;
  428. frmsize.index = 0;
  429. struct dstr buffer;
  430. dstr_init(&buffer);
  431. obs_property_list_clear(prop);
  432. v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize);
  433. switch(frmsize.type) {
  434. case V4L2_FRMSIZE_TYPE_DISCRETE:
  435. while (v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize) == 0) {
  436. dstr_printf(&buffer, "%dx%d", frmsize.discrete.width,
  437. frmsize.discrete.height);
  438. obs_property_list_add_int(prop, buffer.array,
  439. pack_tuple(frmsize.discrete.width,
  440. frmsize.discrete.height));
  441. frmsize.index++;
  442. }
  443. break;
  444. default:
  445. blog(LOG_INFO, "Stepwise and Continuous framesizes "
  446. "are currently hardcoded");
  447. for (uint_fast32_t i = 0; ; ++i) {
  448. int packed = fixed_framesizes[i];
  449. if (!packed)
  450. break;
  451. int width;
  452. int height;
  453. unpack_tuple(&width, &height, packed);
  454. dstr_printf(&buffer, "%dx%d", width, height);
  455. obs_property_list_add_int(prop, buffer.array, packed);
  456. }
  457. break;
  458. }
  459. dstr_free(&buffer);
  460. }
  461. /*
  462. * List framerates for device and resolution
  463. */
  464. static void v4l2_framerate_list(int dev, uint_fast32_t pixelformat,
  465. uint_fast32_t width, uint_fast32_t height, obs_property_t prop)
  466. {
  467. struct v4l2_frmivalenum frmival;
  468. frmival.pixel_format = pixelformat;
  469. frmival.width = width;
  470. frmival.height = height;
  471. frmival.index = 0;
  472. struct dstr buffer;
  473. dstr_init(&buffer);
  474. obs_property_list_clear(prop);
  475. v4l2_ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS, &frmival);
  476. switch(frmival.type) {
  477. case V4L2_FRMIVAL_TYPE_DISCRETE:
  478. while (v4l2_ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS,
  479. &frmival) == 0) {
  480. float fps = (float) frmival.discrete.denominator /
  481. frmival.discrete.numerator;
  482. int pack = pack_tuple(frmival.discrete.numerator,
  483. frmival.discrete.denominator);
  484. dstr_printf(&buffer, "%.2f", fps);
  485. obs_property_list_add_int(prop, buffer.array, pack);
  486. frmival.index++;
  487. }
  488. break;
  489. default:
  490. blog(LOG_INFO, "Stepwise and Continuous framerates "
  491. "are currently hardcoded");
  492. for (uint_fast32_t i = 0; ; ++i) {
  493. int packed = fixed_framerates[i];
  494. if (!packed)
  495. break;
  496. int num;
  497. int denom;
  498. unpack_tuple(&num, &denom, packed);
  499. float fps = (float) denom / num;
  500. dstr_printf(&buffer, "%.2f", fps);
  501. obs_property_list_add_int(prop, buffer.array, packed);
  502. }
  503. break;
  504. }
  505. dstr_free(&buffer);
  506. }
  507. /*
  508. * Device selected callback
  509. */
  510. static bool device_selected(obs_properties_t props, obs_property_t p,
  511. obs_data_t settings)
  512. {
  513. UNUSED_PARAMETER(p);
  514. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  515. O_RDWR | O_NONBLOCK);
  516. if (dev == -1)
  517. return false;
  518. obs_property_t prop = obs_properties_get(props, "pixelformat");
  519. v4l2_format_list(dev, prop);
  520. obs_property_modified(prop, settings);
  521. v4l2_close(dev);
  522. return true;
  523. }
  524. /*
  525. * Format selected callback
  526. */
  527. static bool format_selected(obs_properties_t props, obs_property_t p,
  528. obs_data_t settings)
  529. {
  530. UNUSED_PARAMETER(p);
  531. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  532. O_RDWR | O_NONBLOCK);
  533. if (dev == -1)
  534. return false;
  535. obs_property_t prop = obs_properties_get(props, "resolution");
  536. v4l2_resolution_list(dev, obs_data_get_int(settings, "pixelformat"),
  537. prop);
  538. obs_property_modified(prop, settings);
  539. v4l2_close(dev);
  540. return true;
  541. }
  542. /*
  543. * Resolution selected callback
  544. */
  545. static bool resolution_selected(obs_properties_t props, obs_property_t p,
  546. obs_data_t settings)
  547. {
  548. UNUSED_PARAMETER(p);
  549. int width, height;
  550. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  551. O_RDWR | O_NONBLOCK);
  552. if (dev == -1)
  553. return false;
  554. obs_property_t prop = obs_properties_get(props, "framerate");
  555. unpack_tuple(&width, &height, obs_data_get_int(settings,
  556. "resolution"));
  557. v4l2_framerate_list(dev, obs_data_get_int(settings, "pixelformat"),
  558. width, height, prop);
  559. obs_property_modified(prop, settings);
  560. v4l2_close(dev);
  561. return true;
  562. }
  563. static obs_properties_t v4l2_properties(void)
  564. {
  565. /* TODO: locale */
  566. obs_properties_t props = obs_properties_create();
  567. obs_property_t device_list = obs_properties_add_list(props,
  568. "device_id", obs_module_text("Device"),
  569. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  570. obs_property_t format_list = obs_properties_add_list(props,
  571. "pixelformat", obs_module_text("VideoFormat"),
  572. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  573. obs_property_t resolution_list = obs_properties_add_list(props,
  574. "resolution", obs_module_text("Resolution"),
  575. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  576. obs_properties_add_list(props,
  577. "framerate", obs_module_text("FrameRate"),
  578. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  579. v4l2_device_list(device_list, NULL);
  580. obs_property_set_modified_callback(device_list, device_selected);
  581. obs_property_set_modified_callback(format_list, format_selected);
  582. obs_property_set_modified_callback(resolution_list,
  583. resolution_selected);
  584. return props;
  585. }
  586. static void v4l2_terminate(struct v4l2_data *data)
  587. {
  588. if (data->thread) {
  589. os_event_signal(data->event);
  590. pthread_join(data->thread, NULL);
  591. os_event_destroy(data->event);
  592. }
  593. if (data->buf_count)
  594. v4l2_destroy_mmap(data);
  595. if (data->dev != -1) {
  596. v4l2_close(data->dev);
  597. data->dev = -1;
  598. }
  599. }
  600. static void v4l2_destroy(void *vptr)
  601. {
  602. V4L2_DATA(vptr);
  603. if (!data)
  604. return;
  605. v4l2_terminate(data);
  606. if (data->set_device)
  607. bfree(data->set_device);
  608. bfree(data);
  609. }
  610. /**
  611. * Initialize the v4l2 device
  612. *
  613. * This function:
  614. * - tries to open the device
  615. * - sets pixelformat and requested resolution
  616. * - sets the requested framerate
  617. * - maps the buffers
  618. * - starts the capture thread
  619. */
  620. static void v4l2_init(struct v4l2_data *data)
  621. {
  622. struct v4l2_format fmt;
  623. struct v4l2_streamparm par;
  624. struct dstr fps;
  625. int width, height;
  626. int fps_num, fps_denom;
  627. blog(LOG_INFO, "Start capture from %s", data->set_device);
  628. data->dev = v4l2_open(data->set_device, O_RDWR | O_NONBLOCK);
  629. if (data->dev == -1) {
  630. blog(LOG_ERROR, "Unable to open device");
  631. goto fail;
  632. }
  633. /* set pixel format and resolution */
  634. unpack_tuple(&width, &height, data->set_res);
  635. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  636. fmt.fmt.pix.width = width;
  637. fmt.fmt.pix.height = height;
  638. fmt.fmt.pix.pixelformat = data->set_pixfmt;
  639. fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
  640. if (v4l2_ioctl(data->dev, VIDIOC_S_FMT, &fmt) < 0) {
  641. blog(LOG_ERROR, "Unable to set format");
  642. goto fail;
  643. }
  644. data->width = fmt.fmt.pix.width;
  645. data->height = fmt.fmt.pix.height;
  646. data->pixfmt = fmt.fmt.pix.pixelformat;
  647. data->linesize = fmt.fmt.pix.bytesperline;
  648. blog(LOG_INFO, "Resolution: %"PRIuFAST32"x%"PRIuFAST32,
  649. data->width, data->height);
  650. blog(LOG_INFO, "Linesize: %"PRIuFAST32" Bytes", data->linesize);
  651. /* set framerate */
  652. unpack_tuple(&fps_num, &fps_denom, data->set_fps);
  653. par.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  654. par.parm.capture.timeperframe.numerator = fps_num;
  655. par.parm.capture.timeperframe.denominator = fps_denom;
  656. if (v4l2_ioctl(data->dev, VIDIOC_S_PARM, &par) < 0) {
  657. blog(LOG_ERROR, "Unable to set framerate");
  658. goto fail;
  659. }
  660. dstr_init(&fps);
  661. dstr_printf(&fps, "%.2f",
  662. (float) par.parm.capture.timeperframe.denominator
  663. / par.parm.capture.timeperframe.numerator);
  664. blog(LOG_INFO, "Framerate: %s fps", fps.array);
  665. dstr_free(&fps);
  666. /* map buffers */
  667. if (v4l2_create_mmap(data) < 0) {
  668. blog(LOG_ERROR, "Failed to map buffers");
  669. goto fail;
  670. }
  671. /* start the capture thread */
  672. if (os_event_init(&data->event, OS_EVENT_TYPE_MANUAL) != 0)
  673. goto fail;
  674. if (pthread_create(&data->thread, NULL, v4l2_thread, data) != 0)
  675. goto fail;
  676. return;
  677. fail:
  678. blog(LOG_ERROR, "Initialization failed");
  679. v4l2_terminate(data);
  680. }
  681. static void v4l2_update(void *vptr, obs_data_t settings)
  682. {
  683. V4L2_DATA(vptr);
  684. bool restart = false;
  685. const char *new_device;
  686. new_device = obs_data_get_string(settings, "device_id");
  687. if (strlen(new_device) == 0) {
  688. v4l2_device_list(NULL, settings);
  689. new_device = obs_data_get_string(settings, "device_id");
  690. }
  691. if (!data->set_device || strcmp(data->set_device, new_device) != 0) {
  692. if (data->set_device)
  693. bfree(data->set_device);
  694. data->set_device = bstrdup(new_device);
  695. restart = true;
  696. }
  697. if (data->set_pixfmt != obs_data_get_int(settings, "pixelformat")) {
  698. data->set_pixfmt = obs_data_get_int(settings, "pixelformat");
  699. restart = true;
  700. }
  701. if (data->set_res != obs_data_get_int(settings, "resolution")) {
  702. data->set_res = obs_data_get_int(settings, "resolution");
  703. restart = true;
  704. }
  705. if (data->set_fps != obs_data_get_int(settings, "framerate")) {
  706. data->set_fps = obs_data_get_int(settings, "framerate");
  707. restart = true;
  708. }
  709. if (restart) {
  710. v4l2_terminate(data);
  711. v4l2_init(data);
  712. }
  713. }
  714. static void *v4l2_create(obs_data_t settings, obs_source_t source)
  715. {
  716. UNUSED_PARAMETER(settings);
  717. struct v4l2_data *data = bzalloc(sizeof(struct v4l2_data));
  718. data->dev = -1;
  719. data->source = source;
  720. v4l2_update(data, settings);
  721. return data;
  722. }
  723. struct obs_source_info v4l2_input = {
  724. .id = "v4l2_input",
  725. .type = OBS_SOURCE_TYPE_INPUT,
  726. .output_flags = OBS_SOURCE_ASYNC_VIDEO,
  727. .get_name = v4l2_getname,
  728. .create = v4l2_create,
  729. .destroy = v4l2_destroy,
  730. .update = v4l2_update,
  731. .get_defaults = v4l2_defaults,
  732. .get_properties = v4l2_properties
  733. };