v4l2-input.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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 <util/platform.h>
  31. #include <obs-module.h>
  32. #include "v4l2-helpers.h"
  33. #if HAVE_UDEV
  34. #include "v4l2-udev.h"
  35. #endif
  36. #define V4L2_DATA(voidptr) struct v4l2_data *data = voidptr;
  37. #define timeval2ns(tv) \
  38. (((uint64_t) tv.tv_sec * 1000000000) + ((uint64_t) tv.tv_usec * 1000))
  39. #define blog(level, msg, ...) blog(level, "v4l2-input: " msg, ##__VA_ARGS__)
  40. /**
  41. * Data structure for the v4l2 source
  42. */
  43. struct v4l2_data {
  44. /* settings */
  45. char *device_id;
  46. int input;
  47. int pixfmt;
  48. int resolution;
  49. int framerate;
  50. bool sys_timing;
  51. /* internal data */
  52. obs_source_t *source;
  53. pthread_t thread;
  54. os_event_t *event;
  55. void *udev;
  56. int_fast32_t dev;
  57. int width;
  58. int height;
  59. int linesize;
  60. struct v4l2_buffer_data buffers;
  61. };
  62. /* forward declarations */
  63. static void v4l2_init(struct v4l2_data *data);
  64. static void v4l2_terminate(struct v4l2_data *data);
  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. uint64_t frames;
  122. uint64_t first_ts;
  123. struct timeval tv;
  124. struct v4l2_buffer buf;
  125. struct obs_source_frame out;
  126. size_t plane_offsets[MAX_AV_PLANES];
  127. if (v4l2_start_capture(data->dev, &data->buffers) < 0)
  128. goto exit;
  129. frames = 0;
  130. first_ts = 0;
  131. v4l2_prep_obs_frame(data, &out, plane_offsets);
  132. while (os_event_try(data->event) == EAGAIN) {
  133. FD_ZERO(&fds);
  134. FD_SET(data->dev, &fds);
  135. tv.tv_sec = 1;
  136. tv.tv_usec = 0;
  137. r = select(data->dev + 1, &fds, NULL, NULL, &tv);
  138. if (r < 0) {
  139. if (errno == EINTR)
  140. continue;
  141. blog(LOG_DEBUG, "select failed");
  142. break;
  143. } else if (r == 0) {
  144. blog(LOG_DEBUG, "select timeout");
  145. continue;
  146. }
  147. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  148. buf.memory = V4L2_MEMORY_MMAP;
  149. if (v4l2_ioctl(data->dev, VIDIOC_DQBUF, &buf) < 0) {
  150. if (errno == EAGAIN)
  151. continue;
  152. blog(LOG_DEBUG, "failed to dequeue buffer");
  153. break;
  154. }
  155. out.timestamp = data->sys_timing ?
  156. os_gettime_ns() : timeval2ns(buf.timestamp);
  157. if (!frames)
  158. first_ts = out.timestamp;
  159. out.timestamp -= first_ts;
  160. start = (uint8_t *) data->buffers.info[buf.index].start;
  161. for (uint_fast32_t i = 0; i < MAX_AV_PLANES; ++i)
  162. out.data[i] = start + plane_offsets[i];
  163. obs_source_output_video(data->source, &out);
  164. if (v4l2_ioctl(data->dev, VIDIOC_QBUF, &buf) < 0) {
  165. blog(LOG_DEBUG, "failed to enqueue buffer");
  166. break;
  167. }
  168. frames++;
  169. }
  170. blog(LOG_INFO, "Stopped capture after %"PRIu64" frames", frames);
  171. exit:
  172. v4l2_stop_capture(data->dev);
  173. return NULL;
  174. }
  175. static const char* v4l2_getname(void)
  176. {
  177. return obs_module_text("V4L2Input");
  178. }
  179. static void v4l2_defaults(obs_data_t *settings)
  180. {
  181. obs_data_set_default_int(settings, "input", -1);
  182. obs_data_set_default_int(settings, "pixelformat", -1);
  183. obs_data_set_default_int(settings, "standard", -1);
  184. obs_data_set_default_int(settings, "resolution", -1);
  185. obs_data_set_default_int(settings, "framerate", -1);
  186. obs_data_set_default_bool(settings, "system_timing", false);
  187. }
  188. /**
  189. * Enable/Disable all properties for the source.
  190. *
  191. * @note A property that should be ignored can be specified
  192. *
  193. * @param props the source properties
  194. * @param ignore ignore this property
  195. * @param enable enable/disable all properties
  196. */
  197. static void v4l2_props_set_enabled(obs_properties_t *props,
  198. obs_property_t *ignore, bool enable)
  199. {
  200. if (!props)
  201. return;
  202. for (obs_property_t *prop = obs_properties_first(props); prop != NULL;
  203. obs_property_next(&prop)) {
  204. if (prop == ignore)
  205. continue;
  206. obs_property_set_enabled(prop, enable);
  207. }
  208. }
  209. /*
  210. * List available devices
  211. */
  212. static void v4l2_device_list(obs_property_t *prop, obs_data_t *settings)
  213. {
  214. DIR *dirp;
  215. struct dirent *dp;
  216. struct dstr device;
  217. bool cur_device_found;
  218. size_t cur_device_index;
  219. const char *cur_device_name;
  220. dirp = opendir("/sys/class/video4linux");
  221. if (!dirp)
  222. return;
  223. cur_device_found = false;
  224. cur_device_name = obs_data_get_string(settings, "device_id");
  225. obs_property_list_clear(prop);
  226. dstr_init_copy(&device, "/dev/");
  227. while ((dp = readdir(dirp)) != NULL) {
  228. int fd;
  229. uint32_t caps;
  230. struct v4l2_capability video_cap;
  231. if (dp->d_type == DT_DIR)
  232. continue;
  233. dstr_resize(&device, 5);
  234. dstr_cat(&device, dp->d_name);
  235. if ((fd = v4l2_open(device.array, O_RDWR | O_NONBLOCK)) == -1) {
  236. blog(LOG_INFO, "Unable to open %s", device.array);
  237. continue;
  238. }
  239. if (v4l2_ioctl(fd, VIDIOC_QUERYCAP, &video_cap) == -1) {
  240. blog(LOG_INFO, "Failed to query capabilities for %s",
  241. device.array);
  242. v4l2_close(fd);
  243. continue;
  244. }
  245. caps = (video_cap.capabilities & V4L2_CAP_DEVICE_CAPS)
  246. ? video_cap.device_caps
  247. : video_cap.capabilities;
  248. if (!(caps & V4L2_CAP_VIDEO_CAPTURE)) {
  249. blog(LOG_INFO, "%s seems to not support video capture",
  250. device.array);
  251. v4l2_close(fd);
  252. continue;
  253. }
  254. obs_property_list_add_string(prop, (char *) video_cap.card,
  255. device.array);
  256. blog(LOG_INFO, "Found device '%s' at %s", video_cap.card,
  257. device.array);
  258. /* check if this is the currently used device */
  259. if (cur_device_name && !strcmp(cur_device_name, device.array))
  260. cur_device_found = true;
  261. v4l2_close(fd);
  262. }
  263. /* add currently selected device if not present, but disable it ... */
  264. if (!cur_device_found && cur_device_name && strlen(cur_device_name)) {
  265. cur_device_index = obs_property_list_add_string(prop,
  266. cur_device_name, cur_device_name);
  267. obs_property_list_item_disable(prop, cur_device_index, true);
  268. }
  269. closedir(dirp);
  270. dstr_free(&device);
  271. }
  272. /*
  273. * List inputs for device
  274. */
  275. static void v4l2_input_list(int_fast32_t dev, obs_property_t *prop)
  276. {
  277. struct v4l2_input in;
  278. memset(&in, 0, sizeof(in));
  279. obs_property_list_clear(prop);
  280. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  281. while (v4l2_ioctl(dev, VIDIOC_ENUMINPUT, &in) == 0) {
  282. obs_property_list_add_int(prop, (char *) in.name, in.index);
  283. blog(LOG_INFO, "Found input '%s' (Index %d)", in.name,
  284. in.index);
  285. in.index++;
  286. }
  287. }
  288. /*
  289. * List formats for device
  290. */
  291. static void v4l2_format_list(int dev, obs_property_t *prop)
  292. {
  293. struct v4l2_fmtdesc fmt;
  294. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  295. fmt.index = 0;
  296. struct dstr buffer;
  297. dstr_init(&buffer);
  298. obs_property_list_clear(prop);
  299. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  300. while (v4l2_ioctl(dev, VIDIOC_ENUM_FMT, &fmt) == 0) {
  301. dstr_copy(&buffer, (char *) fmt.description);
  302. if (fmt.flags & V4L2_FMT_FLAG_EMULATED)
  303. dstr_cat(&buffer, " (Emulated)");
  304. if (v4l2_to_obs_video_format(fmt.pixelformat)
  305. != VIDEO_FORMAT_NONE) {
  306. obs_property_list_add_int(prop, buffer.array,
  307. fmt.pixelformat);
  308. blog(LOG_INFO, "Pixelformat: %s (available)",
  309. buffer.array);
  310. } else {
  311. blog(LOG_INFO, "Pixelformat: %s (unavailable)",
  312. buffer.array);
  313. }
  314. fmt.index++;
  315. }
  316. dstr_free(&buffer);
  317. }
  318. /*
  319. * List video standards for the device
  320. */
  321. static void v4l2_standard_list(int dev, obs_property_t *prop)
  322. {
  323. struct v4l2_standard std;
  324. std.index = 0;
  325. obs_property_list_clear(prop);
  326. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  327. while (v4l2_ioctl(dev, VIDIOC_ENUMSTD, &std) == 0) {
  328. obs_property_list_add_int(prop, (char *) std.name, std.id);
  329. std.index++;
  330. }
  331. }
  332. /*
  333. * List resolutions for device and format
  334. */
  335. static void v4l2_resolution_list(int dev, uint_fast32_t pixelformat,
  336. obs_property_t *prop)
  337. {
  338. struct v4l2_frmsizeenum frmsize;
  339. frmsize.pixel_format = pixelformat;
  340. frmsize.index = 0;
  341. struct dstr buffer;
  342. dstr_init(&buffer);
  343. obs_property_list_clear(prop);
  344. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  345. v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize);
  346. switch(frmsize.type) {
  347. case V4L2_FRMSIZE_TYPE_DISCRETE:
  348. while (v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize) == 0) {
  349. dstr_printf(&buffer, "%dx%d", frmsize.discrete.width,
  350. frmsize.discrete.height);
  351. obs_property_list_add_int(prop, buffer.array,
  352. v4l2_pack_tuple(frmsize.discrete.width,
  353. frmsize.discrete.height));
  354. frmsize.index++;
  355. }
  356. break;
  357. default:
  358. blog(LOG_INFO, "Stepwise and Continuous framesizes "
  359. "are currently hardcoded");
  360. for (const int *packed = v4l2_framesizes; *packed; ++packed) {
  361. int width;
  362. int height;
  363. v4l2_unpack_tuple(&width, &height, *packed);
  364. dstr_printf(&buffer, "%dx%d", width, height);
  365. obs_property_list_add_int(prop, buffer.array, *packed);
  366. }
  367. break;
  368. }
  369. dstr_free(&buffer);
  370. }
  371. /*
  372. * List framerates for device and resolution
  373. */
  374. static void v4l2_framerate_list(int dev, uint_fast32_t pixelformat,
  375. uint_fast32_t width, uint_fast32_t height, obs_property_t *prop)
  376. {
  377. struct v4l2_frmivalenum frmival;
  378. frmival.pixel_format = pixelformat;
  379. frmival.width = width;
  380. frmival.height = height;
  381. frmival.index = 0;
  382. struct dstr buffer;
  383. dstr_init(&buffer);
  384. obs_property_list_clear(prop);
  385. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  386. v4l2_ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS, &frmival);
  387. switch(frmival.type) {
  388. case V4L2_FRMIVAL_TYPE_DISCRETE:
  389. while (v4l2_ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS,
  390. &frmival) == 0) {
  391. float fps = (float) frmival.discrete.denominator /
  392. frmival.discrete.numerator;
  393. int pack = v4l2_pack_tuple(frmival.discrete.numerator,
  394. frmival.discrete.denominator);
  395. dstr_printf(&buffer, "%.2f", fps);
  396. obs_property_list_add_int(prop, buffer.array, pack);
  397. frmival.index++;
  398. }
  399. break;
  400. default:
  401. blog(LOG_INFO, "Stepwise and Continuous framerates "
  402. "are currently hardcoded");
  403. for (const int *packed = v4l2_framerates; *packed; ++packed) {
  404. int num;
  405. int denom;
  406. v4l2_unpack_tuple(&num, &denom, *packed);
  407. float fps = (float) denom / num;
  408. dstr_printf(&buffer, "%.2f", fps);
  409. obs_property_list_add_int(prop, buffer.array, *packed);
  410. }
  411. break;
  412. }
  413. dstr_free(&buffer);
  414. }
  415. /*
  416. * Device selected callback
  417. */
  418. static bool device_selected(obs_properties_t *props, obs_property_t *p,
  419. obs_data_t *settings)
  420. {
  421. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  422. O_RDWR | O_NONBLOCK);
  423. v4l2_props_set_enabled(props, p, (dev == -1) ? false : true);
  424. if (dev == -1)
  425. return false;
  426. obs_property_t *prop = obs_properties_get(props, "input");
  427. v4l2_input_list(dev, prop);
  428. v4l2_close(dev);
  429. obs_property_modified(prop, settings);
  430. return true;
  431. }
  432. /*
  433. * Input selected callback
  434. */
  435. static bool input_selected(obs_properties_t *props, obs_property_t *p,
  436. obs_data_t *settings)
  437. {
  438. UNUSED_PARAMETER(p);
  439. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  440. O_RDWR | O_NONBLOCK);
  441. if (dev == -1)
  442. return false;
  443. obs_property_t *prop = obs_properties_get(props, "pixelformat");
  444. v4l2_format_list(dev, prop);
  445. v4l2_close(dev);
  446. obs_property_modified(prop, settings);
  447. return true;
  448. }
  449. /*
  450. * Format selected callback
  451. */
  452. static bool format_selected(obs_properties_t *props, obs_property_t *p,
  453. obs_data_t *settings)
  454. {
  455. UNUSED_PARAMETER(p);
  456. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  457. O_RDWR | O_NONBLOCK);
  458. if (dev == -1)
  459. return false;
  460. obs_property_t *prop = obs_properties_get(props, "resolution");
  461. v4l2_resolution_list(dev, obs_data_get_int(settings, "pixelformat"),
  462. prop);
  463. v4l2_close(dev);
  464. obs_property_modified(prop, settings);
  465. return true;
  466. }
  467. /*
  468. * Resolution selected callback
  469. */
  470. static bool resolution_selected(obs_properties_t *props, obs_property_t *p,
  471. obs_data_t *settings)
  472. {
  473. UNUSED_PARAMETER(p);
  474. int width, height;
  475. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  476. O_RDWR | O_NONBLOCK);
  477. if (dev == -1)
  478. return false;
  479. obs_property_t *prop = obs_properties_get(props, "framerate");
  480. v4l2_unpack_tuple(&width, &height, obs_data_get_int(settings,
  481. "resolution"));
  482. v4l2_framerate_list(dev, obs_data_get_int(settings, "pixelformat"),
  483. width, height, prop);
  484. v4l2_close(dev);
  485. obs_property_modified(prop, settings);
  486. return true;
  487. }
  488. #if HAVE_UDEV
  489. /**
  490. * Device added callback
  491. *
  492. * If everything went fine we can start capturing again when the device is
  493. * reconnected
  494. */
  495. static void device_added(const char *dev, void *vptr)
  496. {
  497. V4L2_DATA(vptr);
  498. obs_source_update_properties(data->source);
  499. if (strcmp(data->device_id, dev))
  500. return;
  501. blog(LOG_INFO, "Device %s reconnected", dev);
  502. v4l2_init(data);
  503. }
  504. /**
  505. * Device removed callback
  506. *
  507. * We stop recording here so we don't block the device node
  508. */
  509. static void device_removed(const char *dev, void *vptr)
  510. {
  511. V4L2_DATA(vptr);
  512. obs_source_update_properties(data->source);
  513. if (strcmp(data->device_id, dev))
  514. return;
  515. blog(LOG_INFO, "Device %s disconnected", dev);
  516. v4l2_terminate(data);
  517. }
  518. #endif
  519. static obs_properties_t *v4l2_properties(void *vptr)
  520. {
  521. V4L2_DATA(vptr);
  522. obs_properties_t *props = obs_properties_create();
  523. obs_property_t *device_list = obs_properties_add_list(props,
  524. "device_id", obs_module_text("Device"),
  525. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  526. obs_property_t *input_list = obs_properties_add_list(props,
  527. "input", obs_module_text("Input"),
  528. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  529. obs_property_t *format_list = obs_properties_add_list(props,
  530. "pixelformat", obs_module_text("VideoFormat"),
  531. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  532. obs_property_t *standard_list = obs_properties_add_list(props,
  533. "standard", obs_module_text("VideoStandard"),
  534. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  535. obs_property_set_visible(standard_list, false);
  536. obs_property_t *resolution_list = obs_properties_add_list(props,
  537. "resolution", obs_module_text("Resolution"),
  538. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  539. obs_properties_add_list(props,
  540. "framerate", obs_module_text("FrameRate"),
  541. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  542. obs_properties_add_bool(props,
  543. "system_timing", obs_module_text("UseSystemTiming"));
  544. obs_data_t *settings = obs_source_get_settings(data->source);
  545. v4l2_device_list(device_list, settings);
  546. obs_data_release(settings);
  547. obs_property_set_modified_callback(device_list, device_selected);
  548. obs_property_set_modified_callback(input_list, input_selected);
  549. obs_property_set_modified_callback(format_list, format_selected);
  550. obs_property_set_modified_callback(resolution_list,
  551. resolution_selected);
  552. return props;
  553. }
  554. static void v4l2_terminate(struct v4l2_data *data)
  555. {
  556. if (data->thread) {
  557. os_event_signal(data->event);
  558. pthread_join(data->thread, NULL);
  559. os_event_destroy(data->event);
  560. data->thread = 0;
  561. }
  562. v4l2_destroy_mmap(&data->buffers);
  563. if (data->dev != -1) {
  564. v4l2_close(data->dev);
  565. data->dev = -1;
  566. }
  567. }
  568. static void v4l2_destroy(void *vptr)
  569. {
  570. V4L2_DATA(vptr);
  571. if (!data)
  572. return;
  573. v4l2_terminate(data);
  574. if (data->device_id)
  575. bfree(data->device_id);
  576. #if HAVE_UDEV
  577. v4l2_unref_udev(data->udev);
  578. #endif
  579. bfree(data);
  580. }
  581. /**
  582. * Initialize the v4l2 device
  583. *
  584. * This function:
  585. * - tries to open the device
  586. * - sets pixelformat and requested resolution
  587. * - sets the requested framerate
  588. * - maps the buffers
  589. * - starts the capture thread
  590. */
  591. static void v4l2_init(struct v4l2_data *data)
  592. {
  593. int fps_num, fps_denom;
  594. blog(LOG_INFO, "Start capture from %s", data->device_id);
  595. data->dev = v4l2_open(data->device_id, O_RDWR | O_NONBLOCK);
  596. if (data->dev == -1) {
  597. blog(LOG_ERROR, "Unable to open device");
  598. goto fail;
  599. }
  600. /* set input */
  601. if (v4l2_set_input(data->dev, &data->input) < 0) {
  602. blog(LOG_ERROR, "Unable to set input %d", data->input);
  603. goto fail;
  604. }
  605. blog(LOG_INFO, "Input: %d", data->input);
  606. /* set pixel format and resolution */
  607. if (v4l2_set_format(data->dev, &data->resolution, &data->pixfmt,
  608. &data->linesize) < 0) {
  609. blog(LOG_ERROR, "Unable to set format");
  610. goto fail;
  611. }
  612. if (v4l2_to_obs_video_format(data->pixfmt) == VIDEO_FORMAT_NONE) {
  613. blog(LOG_ERROR, "Selected video format not supported");
  614. goto fail;
  615. }
  616. v4l2_unpack_tuple(&data->width, &data->height, data->resolution);
  617. blog(LOG_INFO, "Resolution: %dx%d", data->width, data->height);
  618. blog(LOG_INFO, "Pixelformat: %d", data->pixfmt);
  619. blog(LOG_INFO, "Linesize: %d Bytes", data->linesize);
  620. /* set framerate */
  621. if (v4l2_set_framerate(data->dev, &data->framerate) < 0) {
  622. blog(LOG_ERROR, "Unable to set framerate");
  623. goto fail;
  624. }
  625. v4l2_unpack_tuple(&fps_num, &fps_denom, data->framerate);
  626. blog(LOG_INFO, "Framerate: %.2f fps", (float) fps_denom / fps_num);
  627. /* map buffers */
  628. if (v4l2_create_mmap(data->dev, &data->buffers) < 0) {
  629. blog(LOG_ERROR, "Failed to map buffers");
  630. goto fail;
  631. }
  632. /* start the capture thread */
  633. if (os_event_init(&data->event, OS_EVENT_TYPE_MANUAL) != 0)
  634. goto fail;
  635. if (pthread_create(&data->thread, NULL, v4l2_thread, data) != 0)
  636. goto fail;
  637. return;
  638. fail:
  639. blog(LOG_ERROR, "Initialization failed");
  640. v4l2_terminate(data);
  641. }
  642. /**
  643. * Update the settings for the v4l2 source
  644. *
  645. * Since there are very few settings that can be changed without restarting the
  646. * stream we don't bother to even try. Whenever this is called the currently
  647. * active stream (if exists) is stopped, the settings are updated and finally
  648. * the new stream is started.
  649. */
  650. static void v4l2_update(void *vptr, obs_data_t *settings)
  651. {
  652. V4L2_DATA(vptr);
  653. v4l2_terminate(data);
  654. if (data->device_id)
  655. bfree(data->device_id);
  656. data->device_id = bstrdup(obs_data_get_string(settings, "device_id"));
  657. data->input = obs_data_get_int(settings, "input");
  658. data->pixfmt = obs_data_get_int(settings, "pixelformat");
  659. data->resolution = obs_data_get_int(settings, "resolution");
  660. data->framerate = obs_data_get_int(settings, "framerate");
  661. data->sys_timing = obs_data_get_bool(settings, "system_timing");
  662. v4l2_init(data);
  663. }
  664. static void *v4l2_create(obs_data_t *settings, obs_source_t *source)
  665. {
  666. struct v4l2_data *data = bzalloc(sizeof(struct v4l2_data));
  667. data->dev = -1;
  668. data->source = source;
  669. v4l2_update(data, settings);
  670. #if HAVE_UDEV
  671. data->udev = v4l2_init_udev();
  672. v4l2_set_device_added_callback(data->udev, &device_added, data);
  673. v4l2_set_device_removed_callback(data->udev, &device_removed, data);
  674. #endif
  675. return data;
  676. }
  677. struct obs_source_info v4l2_input = {
  678. .id = "v4l2_input",
  679. .type = OBS_SOURCE_TYPE_INPUT,
  680. .output_flags = OBS_SOURCE_ASYNC_VIDEO,
  681. .get_name = v4l2_getname,
  682. .create = v4l2_create,
  683. .destroy = v4l2_destroy,
  684. .update = v4l2_update,
  685. .get_defaults = v4l2_defaults,
  686. .get_properties = v4l2_properties
  687. };