v4l2-input.c 22 KB

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