v4l2-input.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*
  2. Copyright (C) 2014 by Leonhard Oelke <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <string.h>
  18. #include <inttypes.h>
  19. #include <fcntl.h>
  20. #include <dirent.h>
  21. #include <sys/time.h>
  22. #include <sys/types.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/select.h>
  25. #include <linux/videodev2.h>
  26. #include <libv4l2.h>
  27. #include <util/threading.h>
  28. #include <util/bmem.h>
  29. #include <util/dstr.h>
  30. #include <obs-module.h>
  31. #include "v4l2-helpers.h"
  32. #define V4L2_DATA(voidptr) struct v4l2_data *data = voidptr;
  33. #define timeval2ns(tv) \
  34. (((uint64_t) tv.tv_sec * 1000000000) + ((uint64_t) tv.tv_usec * 1000))
  35. #define blog(level, msg, ...) blog(level, "v4l2-input: " msg, ##__VA_ARGS__)
  36. /**
  37. * Data structure for the v4l2 source
  38. *
  39. * The data is divided into two sections, data being used inside and outside
  40. * the capture thread. Data used by the capture thread must not be modified
  41. * from the outside while the thread is running.
  42. *
  43. * Data members prefixed with "set_" are settings from the source properties
  44. * and may be used from outside the capture thread.
  45. */
  46. struct v4l2_data {
  47. /* data used outside of the capture thread */
  48. obs_source_t source;
  49. pthread_t thread;
  50. os_event_t event;
  51. char *set_device;
  52. int_fast32_t set_input;
  53. int_fast32_t set_pixfmt;
  54. int_fast32_t set_res;
  55. int_fast32_t set_fps;
  56. /* data used within the capture thread */
  57. int_fast32_t dev;
  58. uint64_t frames;
  59. int_fast32_t width;
  60. int_fast32_t height;
  61. int_fast32_t pixfmt;
  62. uint_fast32_t linesize;
  63. struct v4l2_buffer_data buffers;
  64. };
  65. /*
  66. * used to store framerate and resolution values
  67. */
  68. static int pack_tuple(int a, int b)
  69. {
  70. return (a << 16) | (b & 0xffff);
  71. }
  72. static void unpack_tuple(int *a, int *b, int packed)
  73. {
  74. *a = packed >> 16;
  75. *b = packed & 0xffff;
  76. }
  77. /**
  78. * Prepare the output frame structure for obs and compute plane offsets
  79. *
  80. * Basically all data apart from memory pointers and the timestamp is known
  81. * before the capture starts. This function prepares the obs_source_frame
  82. * struct with all the data that is already known.
  83. *
  84. * v4l2 uses a continuous memory segment for all planes so we simply compute
  85. * offsets to add to the start address in order to give obs the correct data
  86. * pointers for the individual planes.
  87. */
  88. static void v4l2_prep_obs_frame(struct v4l2_data *data,
  89. struct obs_source_frame *frame, size_t *plane_offsets)
  90. {
  91. memset(frame, 0, sizeof(struct obs_source_frame));
  92. memset(plane_offsets, 0, sizeof(size_t) * MAX_AV_PLANES);
  93. frame->width = data->width;
  94. frame->height = data->height;
  95. frame->format = v4l2_to_obs_video_format(data->pixfmt);
  96. video_format_get_parameters(VIDEO_CS_DEFAULT, VIDEO_RANGE_PARTIAL,
  97. frame->color_matrix, frame->color_range_min,
  98. frame->color_range_max);
  99. switch(data->pixfmt) {
  100. case V4L2_PIX_FMT_NV12:
  101. frame->linesize[0] = data->linesize;
  102. frame->linesize[1] = data->linesize / 2;
  103. plane_offsets[1] = data->linesize * data->height;
  104. break;
  105. case V4L2_PIX_FMT_YVU420:
  106. frame->linesize[0] = data->linesize;
  107. frame->linesize[1] = data->linesize / 2;
  108. frame->linesize[2] = data->linesize / 2;
  109. plane_offsets[1] = data->linesize * data->height * 5 / 4;
  110. plane_offsets[2] = data->linesize * data->height;
  111. break;
  112. case V4L2_PIX_FMT_YUV420:
  113. frame->linesize[0] = data->linesize;
  114. frame->linesize[1] = data->linesize / 2;
  115. frame->linesize[2] = data->linesize / 2;
  116. plane_offsets[1] = data->linesize * data->height;
  117. plane_offsets[2] = data->linesize * data->height * 5 / 4;
  118. break;
  119. default:
  120. frame->linesize[0] = data->linesize;
  121. break;
  122. }
  123. }
  124. /*
  125. * Worker thread to get video data
  126. */
  127. static void *v4l2_thread(void *vptr)
  128. {
  129. V4L2_DATA(vptr);
  130. int r;
  131. fd_set fds;
  132. uint8_t *start;
  133. struct timeval tv;
  134. struct v4l2_buffer buf;
  135. struct obs_source_frame out;
  136. size_t plane_offsets[MAX_AV_PLANES];
  137. if (v4l2_start_capture(data->dev, &data->buffers) < 0)
  138. goto exit;
  139. data->frames = 0;
  140. FD_ZERO(&fds);
  141. FD_SET(data->dev, &fds);
  142. v4l2_prep_obs_frame(data, &out, plane_offsets);
  143. while (os_event_try(data->event) == EAGAIN) {
  144. tv.tv_sec = 1;
  145. tv.tv_usec = 0;
  146. r = select(data->dev + 1, &fds, NULL, NULL, &tv);
  147. if (r < 0) {
  148. if (errno == EINTR)
  149. continue;
  150. blog(LOG_DEBUG, "select failed");
  151. break;
  152. } else if (r == 0) {
  153. blog(LOG_DEBUG, "select timeout");
  154. continue;
  155. }
  156. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  157. buf.memory = V4L2_MEMORY_MMAP;
  158. if (v4l2_ioctl(data->dev, VIDIOC_DQBUF, &buf) < 0) {
  159. if (errno == EAGAIN)
  160. continue;
  161. blog(LOG_DEBUG, "failed to dequeue buffer");
  162. break;
  163. }
  164. out.timestamp = timeval2ns(buf.timestamp);
  165. start = (uint8_t *) data->buffers.info[buf.index].start;
  166. for (uint_fast32_t i = 0; i < MAX_AV_PLANES; ++i)
  167. out.data[i] = start + plane_offsets[i];
  168. obs_source_output_video(data->source, &out);
  169. if (v4l2_ioctl(data->dev, VIDIOC_QBUF, &buf) < 0) {
  170. blog(LOG_DEBUG, "failed to enqueue buffer");
  171. break;
  172. }
  173. data->frames++;
  174. }
  175. blog(LOG_INFO, "Stopped capture after %"PRIu64" frames", data->frames);
  176. exit:
  177. v4l2_stop_capture(data->dev);
  178. return NULL;
  179. }
  180. static const char* v4l2_getname(void)
  181. {
  182. return obs_module_text("V4L2Input");
  183. }
  184. static void v4l2_defaults(obs_data_t settings)
  185. {
  186. obs_data_set_default_int(settings, "input", 0);
  187. obs_data_set_default_int(settings, "pixelformat", V4L2_PIX_FMT_YUYV);
  188. obs_data_set_default_int(settings, "resolution",
  189. pack_tuple(640, 480));
  190. obs_data_set_default_int(settings, "framerate", pack_tuple(1, 30));
  191. }
  192. /*
  193. * List available devices
  194. */
  195. static void v4l2_device_list(obs_property_t prop, obs_data_t settings)
  196. {
  197. DIR *dirp;
  198. struct dirent *dp;
  199. int fd;
  200. struct v4l2_capability video_cap;
  201. struct dstr device;
  202. bool first = true;
  203. dirp = opendir("/sys/class/video4linux");
  204. if (!dirp)
  205. return;
  206. obs_property_list_clear(prop);
  207. dstr_init_copy(&device, "/dev/");
  208. while ((dp = readdir(dirp)) != NULL) {
  209. if (dp->d_type == DT_DIR)
  210. continue;
  211. dstr_resize(&device, 5);
  212. dstr_cat(&device, dp->d_name);
  213. if ((fd = v4l2_open(device.array, O_RDWR | O_NONBLOCK)) == -1) {
  214. blog(LOG_INFO, "Unable to open %s", device.array);
  215. continue;
  216. }
  217. if (v4l2_ioctl(fd, VIDIOC_QUERYCAP, &video_cap) == -1) {
  218. blog(LOG_INFO, "Failed to query capabilities for %s",
  219. device.array);
  220. } else if (video_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) {
  221. obs_property_list_add_string(prop,
  222. (char *) video_cap.card,
  223. device.array);
  224. if (first) {
  225. obs_data_set_string(settings,
  226. "device_id", device.array);
  227. first = false;
  228. }
  229. blog(LOG_INFO, "Found device '%s' at %s",
  230. video_cap.card, device.array);
  231. }
  232. else {
  233. blog(LOG_INFO, "%s seems to not support video capture",
  234. device.array);
  235. }
  236. close(fd);
  237. }
  238. closedir(dirp);
  239. dstr_free(&device);
  240. }
  241. /*
  242. * List inputs for device
  243. */
  244. static void v4l2_input_list(int_fast32_t dev, obs_property_t prop)
  245. {
  246. struct v4l2_input in;
  247. memset(&in, 0, sizeof(in));
  248. obs_property_list_clear(prop);
  249. while (v4l2_ioctl(dev, VIDIOC_ENUMINPUT, &in) == 0) {
  250. if (in.type & V4L2_INPUT_TYPE_CAMERA) {
  251. obs_property_list_add_int(prop, (char *) in.name,
  252. in.index);
  253. blog(LOG_INFO, "Found input '%s'", in.name);
  254. }
  255. in.index++;
  256. }
  257. }
  258. /*
  259. * List formats for device
  260. */
  261. static void v4l2_format_list(int dev, obs_property_t prop)
  262. {
  263. struct v4l2_fmtdesc fmt;
  264. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  265. fmt.index = 0;
  266. struct dstr buffer;
  267. dstr_init(&buffer);
  268. obs_property_list_clear(prop);
  269. while (v4l2_ioctl(dev, VIDIOC_ENUM_FMT, &fmt) == 0) {
  270. dstr_copy(&buffer, (char *) fmt.description);
  271. if (fmt.flags & V4L2_FMT_FLAG_EMULATED)
  272. dstr_cat(&buffer, " (Emulated)");
  273. if (v4l2_to_obs_video_format(fmt.pixelformat)
  274. != VIDEO_FORMAT_NONE) {
  275. obs_property_list_add_int(prop, buffer.array,
  276. fmt.pixelformat);
  277. blog(LOG_INFO, "Pixelformat: %s (available)",
  278. buffer.array);
  279. } else {
  280. blog(LOG_INFO, "Pixelformat: %s (unavailable)",
  281. buffer.array);
  282. }
  283. fmt.index++;
  284. }
  285. dstr_free(&buffer);
  286. }
  287. /*
  288. * List resolutions for device and format
  289. */
  290. static void v4l2_resolution_list(int dev, uint_fast32_t pixelformat,
  291. obs_property_t prop)
  292. {
  293. struct v4l2_frmsizeenum frmsize;
  294. frmsize.pixel_format = pixelformat;
  295. frmsize.index = 0;
  296. struct dstr buffer;
  297. dstr_init(&buffer);
  298. obs_property_list_clear(prop);
  299. v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize);
  300. switch(frmsize.type) {
  301. case V4L2_FRMSIZE_TYPE_DISCRETE:
  302. while (v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize) == 0) {
  303. dstr_printf(&buffer, "%dx%d", frmsize.discrete.width,
  304. frmsize.discrete.height);
  305. obs_property_list_add_int(prop, buffer.array,
  306. pack_tuple(frmsize.discrete.width,
  307. frmsize.discrete.height));
  308. frmsize.index++;
  309. }
  310. break;
  311. default:
  312. blog(LOG_INFO, "Stepwise and Continuous framesizes "
  313. "are currently hardcoded");
  314. for (const int *packed = v4l2_framesizes; *packed; ++packed) {
  315. int width;
  316. int height;
  317. unpack_tuple(&width, &height, *packed);
  318. dstr_printf(&buffer, "%dx%d", width, height);
  319. obs_property_list_add_int(prop, buffer.array, *packed);
  320. }
  321. break;
  322. }
  323. dstr_free(&buffer);
  324. }
  325. /*
  326. * List framerates for device and resolution
  327. */
  328. static void v4l2_framerate_list(int dev, uint_fast32_t pixelformat,
  329. uint_fast32_t width, uint_fast32_t height, obs_property_t prop)
  330. {
  331. struct v4l2_frmivalenum frmival;
  332. frmival.pixel_format = pixelformat;
  333. frmival.width = width;
  334. frmival.height = height;
  335. frmival.index = 0;
  336. struct dstr buffer;
  337. dstr_init(&buffer);
  338. obs_property_list_clear(prop);
  339. v4l2_ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS, &frmival);
  340. switch(frmival.type) {
  341. case V4L2_FRMIVAL_TYPE_DISCRETE:
  342. while (v4l2_ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS,
  343. &frmival) == 0) {
  344. float fps = (float) frmival.discrete.denominator /
  345. frmival.discrete.numerator;
  346. int pack = pack_tuple(frmival.discrete.numerator,
  347. frmival.discrete.denominator);
  348. dstr_printf(&buffer, "%.2f", fps);
  349. obs_property_list_add_int(prop, buffer.array, pack);
  350. frmival.index++;
  351. }
  352. break;
  353. default:
  354. blog(LOG_INFO, "Stepwise and Continuous framerates "
  355. "are currently hardcoded");
  356. for (const int *packed = v4l2_framerates; *packed; ++packed) {
  357. int num;
  358. int denom;
  359. unpack_tuple(&num, &denom, *packed);
  360. float fps = (float) denom / num;
  361. dstr_printf(&buffer, "%.2f", fps);
  362. obs_property_list_add_int(prop, buffer.array, *packed);
  363. }
  364. break;
  365. }
  366. dstr_free(&buffer);
  367. }
  368. /*
  369. * Device selected callback
  370. */
  371. static bool device_selected(obs_properties_t props, obs_property_t p,
  372. obs_data_t settings)
  373. {
  374. UNUSED_PARAMETER(p);
  375. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  376. O_RDWR | O_NONBLOCK);
  377. if (dev == -1)
  378. return false;
  379. obs_property_t prop = obs_properties_get(props, "input");
  380. v4l2_input_list(dev, prop);
  381. obs_property_modified(prop, settings);
  382. v4l2_close(dev);
  383. return true;
  384. }
  385. /*
  386. * Input selected callback
  387. */
  388. static bool input_selected(obs_properties_t props, obs_property_t p,
  389. obs_data_t settings)
  390. {
  391. UNUSED_PARAMETER(p);
  392. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  393. O_RDWR | O_NONBLOCK);
  394. if (dev == -1)
  395. return false;
  396. obs_property_t prop = obs_properties_get(props, "pixelformat");
  397. v4l2_format_list(dev, prop);
  398. obs_property_modified(prop, settings);
  399. v4l2_close(dev);
  400. return true;
  401. }
  402. /*
  403. * Format selected callback
  404. */
  405. static bool format_selected(obs_properties_t props, obs_property_t p,
  406. obs_data_t settings)
  407. {
  408. UNUSED_PARAMETER(p);
  409. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  410. O_RDWR | O_NONBLOCK);
  411. if (dev == -1)
  412. return false;
  413. obs_property_t prop = obs_properties_get(props, "resolution");
  414. v4l2_resolution_list(dev, obs_data_get_int(settings, "pixelformat"),
  415. prop);
  416. obs_property_modified(prop, settings);
  417. v4l2_close(dev);
  418. return true;
  419. }
  420. /*
  421. * Resolution selected callback
  422. */
  423. static bool resolution_selected(obs_properties_t props, obs_property_t p,
  424. obs_data_t settings)
  425. {
  426. UNUSED_PARAMETER(p);
  427. int width, height;
  428. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  429. O_RDWR | O_NONBLOCK);
  430. if (dev == -1)
  431. return false;
  432. obs_property_t prop = obs_properties_get(props, "framerate");
  433. unpack_tuple(&width, &height, obs_data_get_int(settings,
  434. "resolution"));
  435. v4l2_framerate_list(dev, obs_data_get_int(settings, "pixelformat"),
  436. width, height, prop);
  437. obs_property_modified(prop, settings);
  438. v4l2_close(dev);
  439. return true;
  440. }
  441. static obs_properties_t v4l2_properties(void)
  442. {
  443. obs_properties_t props = obs_properties_create();
  444. obs_property_t device_list = obs_properties_add_list(props,
  445. "device_id", obs_module_text("Device"),
  446. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  447. obs_property_t input_list = obs_properties_add_list(props,
  448. "input", obs_module_text("Input"),
  449. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  450. obs_property_t format_list = obs_properties_add_list(props,
  451. "pixelformat", obs_module_text("VideoFormat"),
  452. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  453. obs_property_t resolution_list = obs_properties_add_list(props,
  454. "resolution", obs_module_text("Resolution"),
  455. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  456. obs_properties_add_list(props,
  457. "framerate", obs_module_text("FrameRate"),
  458. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  459. v4l2_device_list(device_list, NULL);
  460. obs_property_set_modified_callback(device_list, device_selected);
  461. obs_property_set_modified_callback(input_list, input_selected);
  462. obs_property_set_modified_callback(format_list, format_selected);
  463. obs_property_set_modified_callback(resolution_list,
  464. resolution_selected);
  465. return props;
  466. }
  467. static void v4l2_terminate(struct v4l2_data *data)
  468. {
  469. if (data->thread) {
  470. os_event_signal(data->event);
  471. pthread_join(data->thread, NULL);
  472. os_event_destroy(data->event);
  473. data->thread = 0;
  474. }
  475. v4l2_destroy_mmap(&data->buffers);
  476. if (data->dev != -1) {
  477. v4l2_close(data->dev);
  478. data->dev = -1;
  479. }
  480. }
  481. static void v4l2_destroy(void *vptr)
  482. {
  483. V4L2_DATA(vptr);
  484. if (!data)
  485. return;
  486. v4l2_terminate(data);
  487. if (data->set_device)
  488. bfree(data->set_device);
  489. bfree(data);
  490. }
  491. /**
  492. * Initialize the v4l2 device
  493. *
  494. * This function:
  495. * - tries to open the device
  496. * - sets pixelformat and requested resolution
  497. * - sets the requested framerate
  498. * - maps the buffers
  499. * - starts the capture thread
  500. */
  501. static void v4l2_init(struct v4l2_data *data)
  502. {
  503. struct v4l2_format fmt;
  504. struct v4l2_streamparm par;
  505. struct dstr fps;
  506. int width, height;
  507. int fps_num, fps_denom;
  508. blog(LOG_INFO, "Start capture from %s", data->set_device);
  509. data->dev = v4l2_open(data->set_device, O_RDWR | O_NONBLOCK);
  510. if (data->dev == -1) {
  511. blog(LOG_ERROR, "Unable to open device");
  512. goto fail;
  513. }
  514. /* set input */
  515. if (v4l2_ioctl(data->dev, VIDIOC_S_INPUT, &data->set_input) < 0) {
  516. blog(LOG_ERROR, "Unable to set input");
  517. goto fail;
  518. }
  519. /* set pixel format and resolution */
  520. unpack_tuple(&width, &height, data->set_res);
  521. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  522. fmt.fmt.pix.width = width;
  523. fmt.fmt.pix.height = height;
  524. fmt.fmt.pix.pixelformat = data->set_pixfmt;
  525. fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
  526. if (v4l2_ioctl(data->dev, VIDIOC_S_FMT, &fmt) < 0) {
  527. blog(LOG_ERROR, "Unable to set format");
  528. goto fail;
  529. }
  530. data->width = fmt.fmt.pix.width;
  531. data->height = fmt.fmt.pix.height;
  532. data->pixfmt = fmt.fmt.pix.pixelformat;
  533. data->linesize = fmt.fmt.pix.bytesperline;
  534. blog(LOG_INFO, "Resolution: %"PRIuFAST32"x%"PRIuFAST32,
  535. data->width, data->height);
  536. blog(LOG_INFO, "Linesize: %"PRIuFAST32" Bytes", data->linesize);
  537. /* set framerate */
  538. unpack_tuple(&fps_num, &fps_denom, data->set_fps);
  539. par.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  540. par.parm.capture.timeperframe.numerator = fps_num;
  541. par.parm.capture.timeperframe.denominator = fps_denom;
  542. if (v4l2_ioctl(data->dev, VIDIOC_S_PARM, &par) < 0) {
  543. blog(LOG_ERROR, "Unable to set framerate");
  544. goto fail;
  545. }
  546. dstr_init(&fps);
  547. dstr_printf(&fps, "%.2f",
  548. (float) par.parm.capture.timeperframe.denominator
  549. / par.parm.capture.timeperframe.numerator);
  550. blog(LOG_INFO, "Framerate: %s fps", fps.array);
  551. dstr_free(&fps);
  552. /* map buffers */
  553. if (v4l2_create_mmap(data->dev, &data->buffers) < 0) {
  554. blog(LOG_ERROR, "Failed to map buffers");
  555. goto fail;
  556. }
  557. /* start the capture thread */
  558. if (os_event_init(&data->event, OS_EVENT_TYPE_MANUAL) != 0)
  559. goto fail;
  560. if (pthread_create(&data->thread, NULL, v4l2_thread, data) != 0)
  561. goto fail;
  562. return;
  563. fail:
  564. blog(LOG_ERROR, "Initialization failed");
  565. v4l2_terminate(data);
  566. }
  567. static void v4l2_update(void *vptr, obs_data_t settings)
  568. {
  569. V4L2_DATA(vptr);
  570. bool restart = false;
  571. const char *new_device;
  572. new_device = obs_data_get_string(settings, "device_id");
  573. if (strlen(new_device) == 0) {
  574. v4l2_device_list(NULL, settings);
  575. new_device = obs_data_get_string(settings, "device_id");
  576. }
  577. if (!data->set_device || strcmp(data->set_device, new_device) != 0) {
  578. if (data->set_device)
  579. bfree(data->set_device);
  580. data->set_device = bstrdup(new_device);
  581. restart = true;
  582. }
  583. if (data->set_input != obs_data_get_int(settings, "input")) {
  584. data->set_input = obs_data_get_int(settings, "input");
  585. restart = true;
  586. }
  587. if (data->set_pixfmt != obs_data_get_int(settings, "pixelformat")) {
  588. data->set_pixfmt = obs_data_get_int(settings, "pixelformat");
  589. restart = true;
  590. }
  591. if (data->set_res != obs_data_get_int(settings, "resolution")) {
  592. data->set_res = obs_data_get_int(settings, "resolution");
  593. restart = true;
  594. }
  595. if (data->set_fps != obs_data_get_int(settings, "framerate")) {
  596. data->set_fps = obs_data_get_int(settings, "framerate");
  597. restart = true;
  598. }
  599. if (restart) {
  600. v4l2_terminate(data);
  601. v4l2_init(data);
  602. }
  603. }
  604. static void *v4l2_create(obs_data_t settings, obs_source_t source)
  605. {
  606. struct v4l2_data *data = bzalloc(sizeof(struct v4l2_data));
  607. data->dev = -1;
  608. data->source = source;
  609. v4l2_update(data, settings);
  610. return data;
  611. }
  612. struct obs_source_info v4l2_input = {
  613. .id = "v4l2_input",
  614. .type = OBS_SOURCE_TYPE_INPUT,
  615. .output_flags = OBS_SOURCE_ASYNC_VIDEO,
  616. .get_name = v4l2_getname,
  617. .create = v4l2_create,
  618. .destroy = v4l2_destroy,
  619. .update = v4l2_update,
  620. .get_defaults = v4l2_defaults,
  621. .get_properties = v4l2_properties
  622. };