v4l2-input.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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. struct v4l2_data {
  40. /* settings */
  41. char *device_id;
  42. int input;
  43. int pixfmt;
  44. int resolution;
  45. int framerate;
  46. /* internal data */
  47. obs_source_t source;
  48. pthread_t thread;
  49. os_event_t event;
  50. int_fast32_t dev;
  51. int width;
  52. int height;
  53. int linesize;
  54. struct v4l2_buffer_data buffers;
  55. /* statistics */
  56. uint64_t frames;
  57. };
  58. /**
  59. * Prepare the output frame structure for obs and compute plane offsets
  60. *
  61. * Basically all data apart from memory pointers and the timestamp is known
  62. * before the capture starts. This function prepares the obs_source_frame
  63. * struct with all the data that is already known.
  64. *
  65. * v4l2 uses a continuous memory segment for all planes so we simply compute
  66. * offsets to add to the start address in order to give obs the correct data
  67. * pointers for the individual planes.
  68. */
  69. static void v4l2_prep_obs_frame(struct v4l2_data *data,
  70. struct obs_source_frame *frame, size_t *plane_offsets)
  71. {
  72. memset(frame, 0, sizeof(struct obs_source_frame));
  73. memset(plane_offsets, 0, sizeof(size_t) * MAX_AV_PLANES);
  74. frame->width = data->width;
  75. frame->height = data->height;
  76. frame->format = v4l2_to_obs_video_format(data->pixfmt);
  77. video_format_get_parameters(VIDEO_CS_DEFAULT, VIDEO_RANGE_PARTIAL,
  78. frame->color_matrix, frame->color_range_min,
  79. frame->color_range_max);
  80. switch(data->pixfmt) {
  81. case V4L2_PIX_FMT_NV12:
  82. frame->linesize[0] = data->linesize;
  83. frame->linesize[1] = data->linesize / 2;
  84. plane_offsets[1] = data->linesize * data->height;
  85. break;
  86. case V4L2_PIX_FMT_YVU420:
  87. frame->linesize[0] = data->linesize;
  88. frame->linesize[1] = data->linesize / 2;
  89. frame->linesize[2] = data->linesize / 2;
  90. plane_offsets[1] = data->linesize * data->height * 5 / 4;
  91. plane_offsets[2] = data->linesize * data->height;
  92. break;
  93. case V4L2_PIX_FMT_YUV420:
  94. frame->linesize[0] = data->linesize;
  95. frame->linesize[1] = data->linesize / 2;
  96. frame->linesize[2] = data->linesize / 2;
  97. plane_offsets[1] = data->linesize * data->height;
  98. plane_offsets[2] = data->linesize * data->height * 5 / 4;
  99. break;
  100. default:
  101. frame->linesize[0] = data->linesize;
  102. break;
  103. }
  104. }
  105. /*
  106. * Worker thread to get video data
  107. */
  108. static void *v4l2_thread(void *vptr)
  109. {
  110. V4L2_DATA(vptr);
  111. int r;
  112. fd_set fds;
  113. uint8_t *start;
  114. struct timeval tv;
  115. struct v4l2_buffer buf;
  116. struct obs_source_frame out;
  117. size_t plane_offsets[MAX_AV_PLANES];
  118. if (v4l2_start_capture(data->dev, &data->buffers) < 0)
  119. goto exit;
  120. data->frames = 0;
  121. FD_ZERO(&fds);
  122. FD_SET(data->dev, &fds);
  123. v4l2_prep_obs_frame(data, &out, plane_offsets);
  124. while (os_event_try(data->event) == EAGAIN) {
  125. tv.tv_sec = 1;
  126. tv.tv_usec = 0;
  127. r = select(data->dev + 1, &fds, NULL, NULL, &tv);
  128. if (r < 0) {
  129. if (errno == EINTR)
  130. continue;
  131. blog(LOG_DEBUG, "select failed");
  132. break;
  133. } else if (r == 0) {
  134. blog(LOG_DEBUG, "select timeout");
  135. continue;
  136. }
  137. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  138. buf.memory = V4L2_MEMORY_MMAP;
  139. if (v4l2_ioctl(data->dev, VIDIOC_DQBUF, &buf) < 0) {
  140. if (errno == EAGAIN)
  141. continue;
  142. blog(LOG_DEBUG, "failed to dequeue buffer");
  143. break;
  144. }
  145. out.timestamp = timeval2ns(buf.timestamp);
  146. start = (uint8_t *) data->buffers.info[buf.index].start;
  147. for (uint_fast32_t i = 0; i < MAX_AV_PLANES; ++i)
  148. out.data[i] = start + plane_offsets[i];
  149. obs_source_output_video(data->source, &out);
  150. if (v4l2_ioctl(data->dev, VIDIOC_QBUF, &buf) < 0) {
  151. blog(LOG_DEBUG, "failed to enqueue buffer");
  152. break;
  153. }
  154. data->frames++;
  155. }
  156. blog(LOG_INFO, "Stopped capture after %"PRIu64" frames", data->frames);
  157. exit:
  158. v4l2_stop_capture(data->dev);
  159. return NULL;
  160. }
  161. static const char* v4l2_getname(void)
  162. {
  163. return obs_module_text("V4L2Input");
  164. }
  165. static void v4l2_defaults(obs_data_t settings)
  166. {
  167. obs_data_set_default_int(settings, "input", -1);
  168. obs_data_set_default_int(settings, "pixelformat", -1);
  169. obs_data_set_default_int(settings, "resolution", -1);
  170. obs_data_set_default_int(settings, "framerate", -1);
  171. }
  172. /*
  173. * List available devices
  174. */
  175. static void v4l2_device_list(obs_property_t prop, obs_data_t settings)
  176. {
  177. UNUSED_PARAMETER(settings);
  178. DIR *dirp;
  179. struct dirent *dp;
  180. struct dstr device;
  181. dirp = opendir("/sys/class/video4linux");
  182. if (!dirp)
  183. return;
  184. obs_property_list_clear(prop);
  185. dstr_init_copy(&device, "/dev/");
  186. while ((dp = readdir(dirp)) != NULL) {
  187. int fd;
  188. uint32_t caps;
  189. struct v4l2_capability video_cap;
  190. if (dp->d_type == DT_DIR)
  191. continue;
  192. dstr_resize(&device, 5);
  193. dstr_cat(&device, dp->d_name);
  194. if ((fd = v4l2_open(device.array, O_RDWR | O_NONBLOCK)) == -1) {
  195. blog(LOG_INFO, "Unable to open %s", device.array);
  196. continue;
  197. }
  198. if (v4l2_ioctl(fd, VIDIOC_QUERYCAP, &video_cap) == -1) {
  199. blog(LOG_INFO, "Failed to query capabilities for %s",
  200. device.array);
  201. close(fd);
  202. continue;
  203. }
  204. caps = (video_cap.capabilities & V4L2_CAP_DEVICE_CAPS)
  205. ? video_cap.device_caps
  206. : video_cap.capabilities;
  207. if (!(caps & V4L2_CAP_VIDEO_CAPTURE)) {
  208. blog(LOG_INFO, "%s seems to not support video capture",
  209. device.array);
  210. close(fd);
  211. continue;
  212. }
  213. obs_property_list_add_string(prop, (char *) video_cap.card,
  214. device.array);
  215. blog(LOG_INFO, "Found device '%s' at %s", video_cap.card,
  216. device.array);
  217. close(fd);
  218. }
  219. closedir(dirp);
  220. dstr_free(&device);
  221. }
  222. /*
  223. * List inputs for device
  224. */
  225. static void v4l2_input_list(int_fast32_t dev, obs_property_t prop)
  226. {
  227. struct v4l2_input in;
  228. memset(&in, 0, sizeof(in));
  229. obs_property_list_clear(prop);
  230. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  231. while (v4l2_ioctl(dev, VIDIOC_ENUMINPUT, &in) == 0) {
  232. if (in.type & V4L2_INPUT_TYPE_CAMERA) {
  233. obs_property_list_add_int(prop, (char *) in.name,
  234. in.index);
  235. blog(LOG_INFO, "Found input '%s' (Index %d)", in.name,
  236. in.index);
  237. }
  238. in.index++;
  239. }
  240. }
  241. /*
  242. * List formats for device
  243. */
  244. static void v4l2_format_list(int dev, obs_property_t prop)
  245. {
  246. struct v4l2_fmtdesc fmt;
  247. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  248. fmt.index = 0;
  249. struct dstr buffer;
  250. dstr_init(&buffer);
  251. obs_property_list_clear(prop);
  252. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  253. while (v4l2_ioctl(dev, VIDIOC_ENUM_FMT, &fmt) == 0) {
  254. dstr_copy(&buffer, (char *) fmt.description);
  255. if (fmt.flags & V4L2_FMT_FLAG_EMULATED)
  256. dstr_cat(&buffer, " (Emulated)");
  257. if (v4l2_to_obs_video_format(fmt.pixelformat)
  258. != VIDEO_FORMAT_NONE) {
  259. obs_property_list_add_int(prop, buffer.array,
  260. fmt.pixelformat);
  261. blog(LOG_INFO, "Pixelformat: %s (available)",
  262. buffer.array);
  263. } else {
  264. blog(LOG_INFO, "Pixelformat: %s (unavailable)",
  265. buffer.array);
  266. }
  267. fmt.index++;
  268. }
  269. dstr_free(&buffer);
  270. }
  271. /*
  272. * List resolutions for device and format
  273. */
  274. static void v4l2_resolution_list(int dev, uint_fast32_t pixelformat,
  275. obs_property_t prop)
  276. {
  277. struct v4l2_frmsizeenum frmsize;
  278. frmsize.pixel_format = pixelformat;
  279. frmsize.index = 0;
  280. struct dstr buffer;
  281. dstr_init(&buffer);
  282. obs_property_list_clear(prop);
  283. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  284. v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize);
  285. switch(frmsize.type) {
  286. case V4L2_FRMSIZE_TYPE_DISCRETE:
  287. while (v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize) == 0) {
  288. dstr_printf(&buffer, "%dx%d", frmsize.discrete.width,
  289. frmsize.discrete.height);
  290. obs_property_list_add_int(prop, buffer.array,
  291. v4l2_pack_tuple(frmsize.discrete.width,
  292. frmsize.discrete.height));
  293. frmsize.index++;
  294. }
  295. break;
  296. default:
  297. blog(LOG_INFO, "Stepwise and Continuous framesizes "
  298. "are currently hardcoded");
  299. for (const int *packed = v4l2_framesizes; *packed; ++packed) {
  300. int width;
  301. int height;
  302. v4l2_unpack_tuple(&width, &height, *packed);
  303. dstr_printf(&buffer, "%dx%d", width, height);
  304. obs_property_list_add_int(prop, buffer.array, *packed);
  305. }
  306. break;
  307. }
  308. dstr_free(&buffer);
  309. }
  310. /*
  311. * List framerates for device and resolution
  312. */
  313. static void v4l2_framerate_list(int dev, uint_fast32_t pixelformat,
  314. uint_fast32_t width, uint_fast32_t height, obs_property_t prop)
  315. {
  316. struct v4l2_frmivalenum frmival;
  317. frmival.pixel_format = pixelformat;
  318. frmival.width = width;
  319. frmival.height = height;
  320. frmival.index = 0;
  321. struct dstr buffer;
  322. dstr_init(&buffer);
  323. obs_property_list_clear(prop);
  324. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  325. v4l2_ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS, &frmival);
  326. switch(frmival.type) {
  327. case V4L2_FRMIVAL_TYPE_DISCRETE:
  328. while (v4l2_ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS,
  329. &frmival) == 0) {
  330. float fps = (float) frmival.discrete.denominator /
  331. frmival.discrete.numerator;
  332. int pack = v4l2_pack_tuple(frmival.discrete.numerator,
  333. frmival.discrete.denominator);
  334. dstr_printf(&buffer, "%.2f", fps);
  335. obs_property_list_add_int(prop, buffer.array, pack);
  336. frmival.index++;
  337. }
  338. break;
  339. default:
  340. blog(LOG_INFO, "Stepwise and Continuous framerates "
  341. "are currently hardcoded");
  342. for (const int *packed = v4l2_framerates; *packed; ++packed) {
  343. int num;
  344. int denom;
  345. v4l2_unpack_tuple(&num, &denom, *packed);
  346. float fps = (float) denom / num;
  347. dstr_printf(&buffer, "%.2f", fps);
  348. obs_property_list_add_int(prop, buffer.array, *packed);
  349. }
  350. break;
  351. }
  352. dstr_free(&buffer);
  353. }
  354. /*
  355. * Device selected callback
  356. */
  357. static bool device_selected(obs_properties_t props, obs_property_t p,
  358. obs_data_t settings)
  359. {
  360. UNUSED_PARAMETER(p);
  361. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  362. O_RDWR | O_NONBLOCK);
  363. if (dev == -1)
  364. return false;
  365. obs_property_t prop = obs_properties_get(props, "input");
  366. v4l2_input_list(dev, prop);
  367. obs_property_modified(prop, settings);
  368. v4l2_close(dev);
  369. return true;
  370. }
  371. /*
  372. * Input selected callback
  373. */
  374. static bool input_selected(obs_properties_t props, obs_property_t p,
  375. obs_data_t settings)
  376. {
  377. UNUSED_PARAMETER(p);
  378. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  379. O_RDWR | O_NONBLOCK);
  380. if (dev == -1)
  381. return false;
  382. obs_property_t prop = obs_properties_get(props, "pixelformat");
  383. v4l2_format_list(dev, prop);
  384. obs_property_modified(prop, settings);
  385. v4l2_close(dev);
  386. return true;
  387. }
  388. /*
  389. * Format selected callback
  390. */
  391. static bool format_selected(obs_properties_t props, obs_property_t p,
  392. obs_data_t settings)
  393. {
  394. UNUSED_PARAMETER(p);
  395. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  396. O_RDWR | O_NONBLOCK);
  397. if (dev == -1)
  398. return false;
  399. obs_property_t prop = obs_properties_get(props, "resolution");
  400. v4l2_resolution_list(dev, obs_data_get_int(settings, "pixelformat"),
  401. prop);
  402. obs_property_modified(prop, settings);
  403. v4l2_close(dev);
  404. return true;
  405. }
  406. /*
  407. * Resolution selected callback
  408. */
  409. static bool resolution_selected(obs_properties_t props, obs_property_t p,
  410. obs_data_t settings)
  411. {
  412. UNUSED_PARAMETER(p);
  413. int width, height;
  414. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  415. O_RDWR | O_NONBLOCK);
  416. if (dev == -1)
  417. return false;
  418. obs_property_t prop = obs_properties_get(props, "framerate");
  419. v4l2_unpack_tuple(&width, &height, obs_data_get_int(settings,
  420. "resolution"));
  421. v4l2_framerate_list(dev, obs_data_get_int(settings, "pixelformat"),
  422. width, height, prop);
  423. obs_property_modified(prop, settings);
  424. v4l2_close(dev);
  425. return true;
  426. }
  427. static obs_properties_t v4l2_properties(void)
  428. {
  429. obs_properties_t props = obs_properties_create();
  430. obs_property_t device_list = obs_properties_add_list(props,
  431. "device_id", obs_module_text("Device"),
  432. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  433. obs_property_t input_list = obs_properties_add_list(props,
  434. "input", obs_module_text("Input"),
  435. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  436. obs_property_t format_list = obs_properties_add_list(props,
  437. "pixelformat", obs_module_text("VideoFormat"),
  438. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  439. obs_property_t resolution_list = obs_properties_add_list(props,
  440. "resolution", obs_module_text("Resolution"),
  441. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  442. obs_properties_add_list(props,
  443. "framerate", obs_module_text("FrameRate"),
  444. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  445. v4l2_device_list(device_list, NULL);
  446. obs_property_set_modified_callback(device_list, device_selected);
  447. obs_property_set_modified_callback(input_list, input_selected);
  448. obs_property_set_modified_callback(format_list, format_selected);
  449. obs_property_set_modified_callback(resolution_list,
  450. resolution_selected);
  451. return props;
  452. }
  453. static void v4l2_terminate(struct v4l2_data *data)
  454. {
  455. if (data->thread) {
  456. os_event_signal(data->event);
  457. pthread_join(data->thread, NULL);
  458. os_event_destroy(data->event);
  459. data->thread = 0;
  460. }
  461. v4l2_destroy_mmap(&data->buffers);
  462. if (data->dev != -1) {
  463. v4l2_close(data->dev);
  464. data->dev = -1;
  465. }
  466. }
  467. static void v4l2_destroy(void *vptr)
  468. {
  469. V4L2_DATA(vptr);
  470. if (!data)
  471. return;
  472. v4l2_terminate(data);
  473. if (data->device_id)
  474. bfree(data->device_id);
  475. bfree(data);
  476. }
  477. /**
  478. * Initialize the v4l2 device
  479. *
  480. * This function:
  481. * - tries to open the device
  482. * - sets pixelformat and requested resolution
  483. * - sets the requested framerate
  484. * - maps the buffers
  485. * - starts the capture thread
  486. */
  487. static void v4l2_init(struct v4l2_data *data)
  488. {
  489. int fps_num, fps_denom;
  490. blog(LOG_INFO, "Start capture from %s", data->device_id);
  491. data->dev = v4l2_open(data->device_id, O_RDWR | O_NONBLOCK);
  492. if (data->dev == -1) {
  493. blog(LOG_ERROR, "Unable to open device");
  494. goto fail;
  495. }
  496. /* set input */
  497. if (v4l2_set_input(data->dev, &data->input) < 0) {
  498. blog(LOG_ERROR, "Unable to set input %d", data->input);
  499. goto fail;
  500. }
  501. blog(LOG_INFO, "Input: %d", data->input);
  502. /* set pixel format and resolution */
  503. if (v4l2_set_format(data->dev, &data->resolution, &data->pixfmt,
  504. &data->linesize) < 0) {
  505. blog(LOG_ERROR, "Unable to set format");
  506. goto fail;
  507. }
  508. if (v4l2_to_obs_video_format(data->pixfmt) == VIDEO_FORMAT_NONE) {
  509. blog(LOG_ERROR, "Selected video format not supported");
  510. goto fail;
  511. }
  512. v4l2_unpack_tuple(&data->width, &data->height, data->resolution);
  513. blog(LOG_INFO, "Resolution: %dx%d", data->width, data->height);
  514. blog(LOG_INFO, "Pixelformat: %d", data->pixfmt);
  515. blog(LOG_INFO, "Linesize: %d Bytes", data->linesize);
  516. /* set framerate */
  517. if (v4l2_set_framerate(data->dev, &data->framerate) < 0) {
  518. blog(LOG_ERROR, "Unable to set framerate");
  519. goto fail;
  520. }
  521. v4l2_unpack_tuple(&fps_num, &fps_denom, data->framerate);
  522. blog(LOG_INFO, "Framerate: %.2f fps", (float) fps_denom / fps_num);
  523. /* map buffers */
  524. if (v4l2_create_mmap(data->dev, &data->buffers) < 0) {
  525. blog(LOG_ERROR, "Failed to map buffers");
  526. goto fail;
  527. }
  528. /* start the capture thread */
  529. if (os_event_init(&data->event, OS_EVENT_TYPE_MANUAL) != 0)
  530. goto fail;
  531. if (pthread_create(&data->thread, NULL, v4l2_thread, data) != 0)
  532. goto fail;
  533. return;
  534. fail:
  535. blog(LOG_ERROR, "Initialization failed");
  536. v4l2_terminate(data);
  537. }
  538. /**
  539. * Update the settings for the v4l2 source
  540. *
  541. * Since there are very few settings that can be changed without restarting the
  542. * stream we don't bother to even try. Whenever this is called the currently
  543. * active stream (if exists) is stopped, the settings are updated and finally
  544. * the new stream is started.
  545. */
  546. static void v4l2_update(void *vptr, obs_data_t settings)
  547. {
  548. V4L2_DATA(vptr);
  549. v4l2_terminate(data);
  550. if (data->device_id)
  551. bfree(data->device_id);
  552. data->device_id = bstrdup(obs_data_get_string(settings, "device_id"));
  553. data->input = obs_data_get_int(settings, "input");
  554. data->pixfmt = obs_data_get_int(settings, "pixelformat");
  555. data->resolution = obs_data_get_int(settings, "resolution");
  556. data->framerate = obs_data_get_int(settings, "framerate");
  557. v4l2_init(data);
  558. }
  559. static void *v4l2_create(obs_data_t settings, obs_source_t source)
  560. {
  561. struct v4l2_data *data = bzalloc(sizeof(struct v4l2_data));
  562. data->dev = -1;
  563. data->source = source;
  564. v4l2_update(data, settings);
  565. return data;
  566. }
  567. struct obs_source_info v4l2_input = {
  568. .id = "v4l2_input",
  569. .type = OBS_SOURCE_TYPE_INPUT,
  570. .output_flags = OBS_SOURCE_ASYNC_VIDEO,
  571. .get_name = v4l2_getname,
  572. .create = v4l2_create,
  573. .destroy = v4l2_destroy,
  574. .update = v4l2_update,
  575. .get_defaults = v4l2_defaults,
  576. .get_properties = v4l2_properties
  577. };