v4l2-input.c 18 KB

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