1
0

v4l2-input.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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-controls.h"
  33. #include "v4l2-helpers.h"
  34. #include "v4l2-mjpeg.h"
  35. #if HAVE_UDEV
  36. #include "v4l2-udev.h"
  37. #endif
  38. /* The new dv timing api was introduced in Linux 3.4
  39. * Currently we simply disable dv timings when this is not defined */
  40. #if !defined(VIDIOC_ENUM_DV_TIMINGS) || !defined(V4L2_IN_CAP_DV_TIMINGS)
  41. #define V4L2_IN_CAP_DV_TIMINGS 0
  42. #endif
  43. #define V4L2_DATA(voidptr) struct v4l2_data *data = voidptr;
  44. #define timeval2ns(tv) \
  45. (((uint64_t)tv.tv_sec * 1000000000) + ((uint64_t)tv.tv_usec * 1000))
  46. #define V4L2_FOURCC_STR(code) \
  47. (char[5]) \
  48. { \
  49. code & 0xFF, (code >> 8) & 0xFF, (code >> 16) & 0xFF, \
  50. (code >> 24) & 0xFF, 0 \
  51. }
  52. #define blog(level, msg, ...) blog(level, "v4l2-input: " msg, ##__VA_ARGS__)
  53. /**
  54. * Data structure for the v4l2 source
  55. */
  56. struct v4l2_data {
  57. /* settings */
  58. char *device_id;
  59. int input;
  60. int pixfmt;
  61. int standard;
  62. int dv_timing;
  63. int resolution;
  64. int framerate;
  65. int color_range;
  66. /* internal data */
  67. obs_source_t *source;
  68. pthread_t thread;
  69. os_event_t *event;
  70. struct v4l2_mjpeg_decoder mjpeg_decoder;
  71. bool framerate_unchanged;
  72. bool resolution_unchanged;
  73. int_fast32_t dev;
  74. int width;
  75. int height;
  76. int linesize;
  77. struct v4l2_buffer_data buffers;
  78. bool auto_reset;
  79. int timeout_frames;
  80. };
  81. /* forward declarations */
  82. static void v4l2_init(struct v4l2_data *data);
  83. static void v4l2_terminate(struct v4l2_data *data);
  84. static void v4l2_update(void *vptr, obs_data_t *settings);
  85. /**
  86. * Prepare the output frame structure for obs and compute plane offsets
  87. * For encoded formats (mjpeg) this clears the frame and plane offsets,
  88. * which will be filled in after decoding.
  89. *
  90. * Basically all data apart from memory pointers and the timestamp is known
  91. * before the capture starts. This function prepares the obs_source_frame
  92. * struct with all the data that is already known.
  93. *
  94. * v4l2 uses a continuous memory segment for all planes so we simply compute
  95. * offsets to add to the start address in order to give obs the correct data
  96. * pointers for the individual planes.
  97. *
  98. */
  99. static void v4l2_prep_obs_frame(struct v4l2_data *data,
  100. struct obs_source_frame *frame,
  101. size_t *plane_offsets)
  102. {
  103. memset(frame, 0, sizeof(struct obs_source_frame));
  104. memset(plane_offsets, 0, sizeof(size_t) * MAX_AV_PLANES);
  105. const enum video_format format = v4l2_to_obs_video_format(data->pixfmt);
  106. frame->width = data->width;
  107. frame->height = data->height;
  108. frame->format = format;
  109. video_format_get_parameters_for_format(VIDEO_CS_DEFAULT,
  110. data->color_range, format,
  111. frame->color_matrix,
  112. frame->color_range_min,
  113. frame->color_range_max);
  114. switch (data->pixfmt) {
  115. case V4L2_PIX_FMT_NV12:
  116. frame->linesize[0] = data->linesize;
  117. frame->linesize[1] = data->linesize;
  118. plane_offsets[1] = data->linesize * data->height;
  119. break;
  120. case V4L2_PIX_FMT_YVU420:
  121. frame->linesize[0] = data->linesize;
  122. frame->linesize[1] = data->linesize / 2;
  123. frame->linesize[2] = data->linesize / 2;
  124. plane_offsets[1] = data->linesize * data->height * 5 / 4;
  125. plane_offsets[2] = data->linesize * data->height;
  126. break;
  127. case V4L2_PIX_FMT_YUV420:
  128. frame->linesize[0] = data->linesize;
  129. frame->linesize[1] = data->linesize / 2;
  130. frame->linesize[2] = data->linesize / 2;
  131. plane_offsets[1] = data->linesize * data->height;
  132. plane_offsets[2] = data->linesize * data->height * 5 / 4;
  133. break;
  134. default:
  135. frame->linesize[0] = data->linesize;
  136. break;
  137. }
  138. }
  139. /*
  140. * Worker thread to get video data
  141. */
  142. static void *v4l2_thread(void *vptr)
  143. {
  144. V4L2_DATA(vptr);
  145. int r;
  146. fd_set fds;
  147. uint8_t *start;
  148. uint64_t frames;
  149. uint64_t first_ts;
  150. struct timeval tv;
  151. struct v4l2_buffer buf;
  152. struct obs_source_frame out;
  153. size_t plane_offsets[MAX_AV_PLANES];
  154. int fps_num, fps_denom;
  155. float ffps;
  156. uint64_t timeout_usec;
  157. blog(LOG_DEBUG, "%s: new capture thread", data->device_id);
  158. os_set_thread_name("v4l2: capture");
  159. /* Get framerate and calculate appropriate select timeout value. */
  160. v4l2_unpack_tuple(&fps_num, &fps_denom, data->framerate);
  161. ffps = (float)fps_denom / fps_num;
  162. blog(LOG_DEBUG, "%s: framerate: %.2f fps", data->device_id, ffps);
  163. /* Timeout set to 5 frame periods. */
  164. timeout_usec = (1000000 * data->timeout_frames) / ffps;
  165. blog(LOG_INFO,
  166. "%s: select timeout set to %" PRIu64 " (%dx frame periods)",
  167. data->device_id, timeout_usec, data->timeout_frames);
  168. if (v4l2_start_capture(data->dev, &data->buffers) < 0)
  169. goto exit;
  170. blog(LOG_DEBUG, "%s: new capture started", data->device_id);
  171. frames = 0;
  172. first_ts = 0;
  173. v4l2_prep_obs_frame(data, &out, plane_offsets);
  174. blog(LOG_DEBUG, "%s: obs frame prepared", data->device_id);
  175. while (os_event_try(data->event) == EAGAIN) {
  176. FD_ZERO(&fds);
  177. FD_SET(data->dev, &fds);
  178. /* Set timeout timevalue. */
  179. tv.tv_sec = 0;
  180. tv.tv_usec = timeout_usec;
  181. r = select(data->dev + 1, &fds, NULL, NULL, &tv);
  182. if (r < 0) {
  183. if (errno == EINTR)
  184. continue;
  185. blog(LOG_ERROR, "%s: select failed", data->device_id);
  186. break;
  187. } else if (r == 0) {
  188. blog(LOG_ERROR, "%s: select timed out",
  189. data->device_id);
  190. #ifdef _DEBUG
  191. v4l2_query_all_buffers(data->dev, &data->buffers);
  192. #endif
  193. if (v4l2_ioctl(data->dev, VIDIOC_LOG_STATUS) < 0) {
  194. blog(LOG_ERROR, "%s: failed to log status",
  195. data->device_id);
  196. }
  197. if (data->auto_reset) {
  198. if (v4l2_reset_capture(data->dev,
  199. &data->buffers) == 0)
  200. blog(LOG_INFO,
  201. "%s: stream reset successful",
  202. data->device_id);
  203. else
  204. blog(LOG_ERROR, "%s: failed to reset",
  205. data->device_id);
  206. }
  207. continue;
  208. }
  209. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  210. buf.memory = V4L2_MEMORY_MMAP;
  211. if (v4l2_ioctl(data->dev, VIDIOC_DQBUF, &buf) < 0) {
  212. if (errno == EAGAIN) {
  213. blog(LOG_DEBUG, "%s: ioctl dqbuf eagain",
  214. data->device_id);
  215. continue;
  216. }
  217. blog(LOG_ERROR, "%s: failed to dequeue buffer",
  218. data->device_id);
  219. break;
  220. }
  221. blog(LOG_DEBUG,
  222. "%s: ts: %06ld buf id #%d, flags 0x%08X, seq #%d, len %d, used %d",
  223. data->device_id, buf.timestamp.tv_usec, buf.index,
  224. buf.flags, buf.sequence, buf.length, buf.bytesused);
  225. out.timestamp = timeval2ns(buf.timestamp);
  226. if (!frames)
  227. first_ts = out.timestamp;
  228. out.timestamp -= first_ts;
  229. start = (uint8_t *)data->buffers.info[buf.index].start;
  230. if (data->pixfmt == V4L2_PIX_FMT_MJPEG) {
  231. if (v4l2_decode_mjpeg(&out, start, buf.bytesused,
  232. &data->mjpeg_decoder) < 0) {
  233. blog(LOG_ERROR, "failed to unpack jpeg");
  234. break;
  235. }
  236. } else {
  237. for (uint_fast32_t i = 0; i < MAX_AV_PLANES; ++i)
  238. out.data[i] = start + plane_offsets[i];
  239. }
  240. obs_source_output_video(data->source, &out);
  241. if (v4l2_ioctl(data->dev, VIDIOC_QBUF, &buf) < 0) {
  242. blog(LOG_ERROR, "%s: failed to enqueue buffer",
  243. data->device_id);
  244. break;
  245. }
  246. frames++;
  247. }
  248. blog(LOG_INFO, "%s: Stopped capture after %" PRIu64 " frames",
  249. data->device_id, frames);
  250. exit:
  251. v4l2_stop_capture(data->dev);
  252. return NULL;
  253. }
  254. static const char *v4l2_getname(void *unused)
  255. {
  256. UNUSED_PARAMETER(unused);
  257. return obs_module_text("V4L2Input");
  258. }
  259. static void v4l2_defaults(obs_data_t *settings)
  260. {
  261. obs_data_set_default_int(settings, "input", -1);
  262. obs_data_set_default_int(settings, "pixelformat", -1);
  263. obs_data_set_default_int(settings, "standard", -1);
  264. obs_data_set_default_int(settings, "dv_timing", -1);
  265. obs_data_set_default_int(settings, "resolution", -1);
  266. obs_data_set_default_int(settings, "framerate", -1);
  267. obs_data_set_default_int(settings, "color_range", VIDEO_RANGE_DEFAULT);
  268. obs_data_set_default_bool(settings, "buffering", true);
  269. obs_data_set_default_bool(settings, "auto_reset", false);
  270. obs_data_set_default_int(settings, "timeout_frames", 5);
  271. }
  272. /**
  273. * Enable/Disable all properties for the source.
  274. *
  275. * @note A property that should be ignored can be specified
  276. *
  277. * @param props the source properties
  278. * @param ignore ignore this property
  279. * @param enable enable/disable all properties
  280. */
  281. static void v4l2_props_set_enabled(obs_properties_t *props,
  282. obs_property_t *ignore, bool enable)
  283. {
  284. if (!props)
  285. return;
  286. for (obs_property_t *prop = obs_properties_first(props); prop != NULL;
  287. obs_property_next(&prop)) {
  288. if (prop == ignore)
  289. continue;
  290. obs_property_set_enabled(prop, enable);
  291. }
  292. }
  293. /*
  294. * List available devices
  295. */
  296. static void v4l2_device_list(obs_property_t *prop, obs_data_t *settings)
  297. {
  298. DIR *dirp;
  299. struct dirent *dp;
  300. struct dstr device;
  301. bool cur_device_found;
  302. size_t cur_device_index;
  303. const char *cur_device_name;
  304. #ifdef __FreeBSD__
  305. dirp = opendir("/dev");
  306. #else
  307. dirp = opendir("/sys/class/video4linux");
  308. #endif
  309. if (!dirp)
  310. return;
  311. cur_device_found = false;
  312. cur_device_name = obs_data_get_string(settings, "device_id");
  313. obs_property_list_clear(prop);
  314. dstr_init_copy(&device, "/dev/");
  315. while ((dp = readdir(dirp)) != NULL) {
  316. int fd;
  317. uint32_t caps;
  318. struct v4l2_capability video_cap;
  319. #ifdef __FreeBSD__
  320. if (strstr(dp->d_name, "video") == NULL)
  321. continue;
  322. #endif
  323. if (dp->d_type == DT_DIR)
  324. continue;
  325. dstr_resize(&device, 5);
  326. dstr_cat(&device, dp->d_name);
  327. if ((fd = v4l2_open(device.array, O_RDWR | O_NONBLOCK)) == -1) {
  328. blog(LOG_INFO, "Unable to open %s", device.array);
  329. continue;
  330. }
  331. if (v4l2_ioctl(fd, VIDIOC_QUERYCAP, &video_cap) == -1) {
  332. blog(LOG_INFO, "Failed to query capabilities for %s",
  333. device.array);
  334. v4l2_close(fd);
  335. continue;
  336. }
  337. #ifndef V4L2_CAP_DEVICE_CAPS
  338. caps = video_cap.capabilities;
  339. #else
  340. /* ... since Linux 3.3 */
  341. caps = (video_cap.capabilities & V4L2_CAP_DEVICE_CAPS)
  342. ? video_cap.device_caps
  343. : video_cap.capabilities;
  344. #endif
  345. if (!(caps & V4L2_CAP_VIDEO_CAPTURE)) {
  346. blog(LOG_INFO, "%s seems to not support video capture",
  347. device.array);
  348. v4l2_close(fd);
  349. continue;
  350. }
  351. /* make sure device names are unique */
  352. char unique_device_name[68];
  353. sprintf(unique_device_name, "%s (%s)", video_cap.card,
  354. video_cap.bus_info);
  355. obs_property_list_add_string(prop, unique_device_name,
  356. device.array);
  357. blog(LOG_INFO, "Found device '%s' at %s", video_cap.card,
  358. device.array);
  359. /* check if this is the currently used device */
  360. if (cur_device_name && !strcmp(cur_device_name, device.array))
  361. cur_device_found = true;
  362. v4l2_close(fd);
  363. }
  364. /* add currently selected device if not present, but disable it ... */
  365. if (!cur_device_found && cur_device_name && strlen(cur_device_name)) {
  366. cur_device_index = obs_property_list_add_string(
  367. prop, cur_device_name, cur_device_name);
  368. obs_property_list_item_disable(prop, cur_device_index, true);
  369. }
  370. closedir(dirp);
  371. dstr_free(&device);
  372. }
  373. /*
  374. * List inputs for device
  375. */
  376. static void v4l2_input_list(int_fast32_t dev, obs_property_t *prop)
  377. {
  378. struct v4l2_input in;
  379. memset(&in, 0, sizeof(in));
  380. obs_property_list_clear(prop);
  381. while (v4l2_ioctl(dev, VIDIOC_ENUMINPUT, &in) == 0) {
  382. obs_property_list_add_int(prop, (char *)in.name, in.index);
  383. blog(LOG_INFO, "Found input '%s' (Index %d)", in.name,
  384. in.index);
  385. in.index++;
  386. }
  387. }
  388. /*
  389. * List formats for device
  390. */
  391. static void v4l2_format_list(int dev, obs_property_t *prop)
  392. {
  393. struct v4l2_fmtdesc fmt;
  394. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  395. fmt.index = 0;
  396. struct dstr buffer;
  397. dstr_init(&buffer);
  398. obs_property_list_clear(prop);
  399. while (v4l2_ioctl(dev, VIDIOC_ENUM_FMT, &fmt) == 0) {
  400. dstr_copy(&buffer, (char *)fmt.description);
  401. if (fmt.flags & V4L2_FMT_FLAG_EMULATED)
  402. dstr_cat(&buffer, " (Emulated)");
  403. if (v4l2_to_obs_video_format(fmt.pixelformat) !=
  404. VIDEO_FORMAT_NONE ||
  405. fmt.pixelformat == V4L2_PIX_FMT_MJPEG) {
  406. obs_property_list_add_int(prop, buffer.array,
  407. fmt.pixelformat);
  408. blog(LOG_INFO, "Pixelformat: %s (available)",
  409. buffer.array);
  410. } else {
  411. blog(LOG_INFO, "Pixelformat: %s (unavailable)",
  412. buffer.array);
  413. }
  414. fmt.index++;
  415. }
  416. dstr_free(&buffer);
  417. }
  418. /*
  419. * List video standards for the device
  420. */
  421. static void v4l2_standard_list(int dev, obs_property_t *prop)
  422. {
  423. struct v4l2_standard std;
  424. std.index = 0;
  425. obs_property_list_clear(prop);
  426. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  427. while (v4l2_ioctl(dev, VIDIOC_ENUMSTD, &std) == 0) {
  428. obs_property_list_add_int(prop, (char *)std.name, std.id);
  429. std.index++;
  430. }
  431. }
  432. /*
  433. * List dv timings for the device
  434. */
  435. static void v4l2_dv_timing_list(int dev, obs_property_t *prop)
  436. {
  437. struct v4l2_dv_timings dvt;
  438. struct dstr buf;
  439. int index = 0;
  440. dstr_init(&buf);
  441. obs_property_list_clear(prop);
  442. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  443. while (v4l2_enum_dv_timing(dev, &dvt, index) == 0) {
  444. /* i do not pretend to understand, this is from qv4l2 ... */
  445. double h = (double)dvt.bt.height + dvt.bt.vfrontporch +
  446. dvt.bt.vsync + dvt.bt.vbackporch +
  447. dvt.bt.il_vfrontporch + dvt.bt.il_vsync +
  448. dvt.bt.il_vbackporch;
  449. double w = (double)dvt.bt.width + dvt.bt.hfrontporch +
  450. dvt.bt.hsync + dvt.bt.hbackporch;
  451. double i = (dvt.bt.interlaced) ? 2.0f : 1.0f;
  452. double rate = (double)dvt.bt.pixelclock / (w * (h / i));
  453. dstr_printf(&buf, "%ux%u%c %.2f", dvt.bt.width, dvt.bt.height,
  454. (dvt.bt.interlaced) ? 'i' : 'p', rate);
  455. obs_property_list_add_int(prop, buf.array, index);
  456. index++;
  457. }
  458. dstr_free(&buf);
  459. }
  460. /*
  461. * List resolutions for device and format
  462. */
  463. static void v4l2_resolution_list(int dev, uint_fast32_t pixelformat,
  464. obs_property_t *prop)
  465. {
  466. struct v4l2_frmsizeenum frmsize;
  467. frmsize.pixel_format = pixelformat;
  468. frmsize.index = 0;
  469. struct dstr buffer;
  470. dstr_init(&buffer);
  471. obs_property_list_clear(prop);
  472. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  473. v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize);
  474. switch (frmsize.type) {
  475. case V4L2_FRMSIZE_TYPE_DISCRETE:
  476. while (v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize) == 0) {
  477. dstr_printf(&buffer, "%dx%d", frmsize.discrete.width,
  478. frmsize.discrete.height);
  479. obs_property_list_add_int(
  480. prop, buffer.array,
  481. v4l2_pack_tuple(frmsize.discrete.width,
  482. frmsize.discrete.height));
  483. frmsize.index++;
  484. }
  485. break;
  486. default:
  487. blog(LOG_INFO, "Stepwise and Continuous framesizes "
  488. "are currently hardcoded");
  489. for (const int *packed = v4l2_framesizes; *packed; ++packed) {
  490. int width;
  491. int height;
  492. v4l2_unpack_tuple(&width, &height, *packed);
  493. dstr_printf(&buffer, "%dx%d", width, height);
  494. obs_property_list_add_int(prop, buffer.array, *packed);
  495. }
  496. break;
  497. }
  498. dstr_free(&buffer);
  499. }
  500. /*
  501. * List framerates for device and resolution
  502. */
  503. static void v4l2_framerate_list(int dev, uint_fast32_t pixelformat,
  504. uint_fast32_t width, uint_fast32_t height,
  505. obs_property_t *prop)
  506. {
  507. struct v4l2_frmivalenum frmival;
  508. frmival.pixel_format = pixelformat;
  509. frmival.width = width;
  510. frmival.height = height;
  511. frmival.index = 0;
  512. struct dstr buffer;
  513. dstr_init(&buffer);
  514. obs_property_list_clear(prop);
  515. obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
  516. v4l2_ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS, &frmival);
  517. switch (frmival.type) {
  518. case V4L2_FRMIVAL_TYPE_DISCRETE:
  519. while (v4l2_ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS, &frmival) ==
  520. 0) {
  521. float fps = (float)frmival.discrete.denominator /
  522. frmival.discrete.numerator;
  523. int pack =
  524. v4l2_pack_tuple(frmival.discrete.numerator,
  525. frmival.discrete.denominator);
  526. dstr_printf(&buffer, "%.2f", fps);
  527. obs_property_list_add_int(prop, buffer.array, pack);
  528. frmival.index++;
  529. }
  530. break;
  531. default:
  532. blog(LOG_INFO, "Stepwise and Continuous framerates "
  533. "are currently hardcoded");
  534. for (const int *packed = v4l2_framerates; *packed; ++packed) {
  535. int num;
  536. int denom;
  537. v4l2_unpack_tuple(&num, &denom, *packed);
  538. float fps = (float)denom / num;
  539. dstr_printf(&buffer, "%.2f", fps);
  540. obs_property_list_add_int(prop, buffer.array, *packed);
  541. }
  542. break;
  543. }
  544. dstr_free(&buffer);
  545. }
  546. /*
  547. * Device selected callback
  548. */
  549. static bool device_selected(obs_properties_t *props, obs_property_t *p,
  550. obs_data_t *settings)
  551. {
  552. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  553. O_RDWR | O_NONBLOCK);
  554. v4l2_props_set_enabled(props, p, (dev == -1) ? false : true);
  555. if (dev == -1)
  556. return false;
  557. obs_property_t *prop = obs_properties_get(props, "input");
  558. obs_properties_t *ctrl_props_new = obs_properties_create();
  559. obs_properties_remove_by_name(props, "controls");
  560. v4l2_input_list(dev, prop);
  561. v4l2_update_controls(dev, ctrl_props_new, settings);
  562. v4l2_close(dev);
  563. obs_properties_add_group(props, "controls",
  564. obs_module_text("CameraCtrls"),
  565. OBS_GROUP_NORMAL, ctrl_props_new);
  566. obs_property_modified(prop, settings);
  567. return true;
  568. }
  569. /*
  570. * Input selected callback
  571. */
  572. static bool input_selected(obs_properties_t *props, obs_property_t *p,
  573. obs_data_t *settings)
  574. {
  575. UNUSED_PARAMETER(p);
  576. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  577. O_RDWR | O_NONBLOCK);
  578. if (dev == -1)
  579. return false;
  580. obs_property_t *prop = obs_properties_get(props, "pixelformat");
  581. v4l2_format_list(dev, prop);
  582. v4l2_close(dev);
  583. obs_property_modified(prop, settings);
  584. return true;
  585. }
  586. /*
  587. * Format selected callback
  588. */
  589. static bool format_selected(obs_properties_t *props, obs_property_t *p,
  590. obs_data_t *settings)
  591. {
  592. UNUSED_PARAMETER(p);
  593. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  594. O_RDWR | O_NONBLOCK);
  595. if (dev == -1)
  596. return false;
  597. int input = (int)obs_data_get_int(settings, "input");
  598. uint32_t caps = 0;
  599. if (v4l2_get_input_caps(dev, input, &caps) < 0)
  600. return false;
  601. caps &= V4L2_IN_CAP_STD | V4L2_IN_CAP_DV_TIMINGS;
  602. obs_property_t *resolution = obs_properties_get(props, "resolution");
  603. obs_property_t *framerate = obs_properties_get(props, "framerate");
  604. obs_property_t *standard = obs_properties_get(props, "standard");
  605. obs_property_t *dv_timing = obs_properties_get(props, "dv_timing");
  606. obs_property_set_visible(resolution, (!caps) ? true : false);
  607. obs_property_set_visible(framerate, (!caps) ? true : false);
  608. obs_property_set_visible(standard,
  609. (caps & V4L2_IN_CAP_STD) ? true : false);
  610. obs_property_set_visible(
  611. dv_timing, (caps & V4L2_IN_CAP_DV_TIMINGS) ? true : false);
  612. if (!caps) {
  613. v4l2_resolution_list(dev,
  614. obs_data_get_int(settings, "pixelformat"),
  615. resolution);
  616. }
  617. if (caps & V4L2_IN_CAP_STD)
  618. v4l2_standard_list(dev, standard);
  619. if (caps & V4L2_IN_CAP_DV_TIMINGS)
  620. v4l2_dv_timing_list(dev, dv_timing);
  621. v4l2_close(dev);
  622. if (!caps)
  623. obs_property_modified(resolution, settings);
  624. if (caps & V4L2_IN_CAP_STD)
  625. obs_property_modified(standard, settings);
  626. if (caps & V4L2_IN_CAP_DV_TIMINGS)
  627. obs_property_modified(dv_timing, settings);
  628. return true;
  629. }
  630. /*
  631. * Resolution selected callback
  632. */
  633. static bool resolution_selected(obs_properties_t *props, obs_property_t *p,
  634. obs_data_t *settings)
  635. {
  636. UNUSED_PARAMETER(p);
  637. int width, height;
  638. int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
  639. O_RDWR | O_NONBLOCK);
  640. if (dev == -1)
  641. return false;
  642. obs_property_t *prop = obs_properties_get(props, "framerate");
  643. v4l2_unpack_tuple(&width, &height,
  644. obs_data_get_int(settings, "resolution"));
  645. v4l2_framerate_list(dev, obs_data_get_int(settings, "pixelformat"),
  646. width, height, prop);
  647. v4l2_close(dev);
  648. obs_property_modified(prop, settings);
  649. return true;
  650. }
  651. #if HAVE_UDEV
  652. /**
  653. * Device added callback
  654. *
  655. * If everything went fine we can start capturing again when the device is
  656. * reconnected
  657. */
  658. static void device_added(void *vptr, calldata_t *calldata)
  659. {
  660. V4L2_DATA(vptr);
  661. obs_source_update_properties(data->source);
  662. const char *dev;
  663. calldata_get_string(calldata, "device", &dev);
  664. if (strcmp(data->device_id, dev))
  665. return;
  666. blog(LOG_INFO, "Device %s reconnected", dev);
  667. v4l2_init(data);
  668. }
  669. /**
  670. * Device removed callback
  671. *
  672. * We stop recording here so we don't block the device node
  673. */
  674. static void device_removed(void *vptr, calldata_t *calldata)
  675. {
  676. V4L2_DATA(vptr);
  677. obs_source_update_properties(data->source);
  678. const char *dev;
  679. calldata_get_string(calldata, "device", &dev);
  680. if (strcmp(data->device_id, dev))
  681. return;
  682. blog(LOG_INFO, "Device %s disconnected", dev);
  683. v4l2_terminate(data);
  684. }
  685. #endif
  686. static obs_properties_t *v4l2_properties(void *vptr)
  687. {
  688. V4L2_DATA(vptr);
  689. obs_properties_t *props = obs_properties_create();
  690. obs_property_t *device_list = obs_properties_add_list(
  691. props, "device_id", obs_module_text("Device"),
  692. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  693. obs_property_t *input_list = obs_properties_add_list(
  694. props, "input", obs_module_text("Input"), OBS_COMBO_TYPE_LIST,
  695. OBS_COMBO_FORMAT_INT);
  696. obs_property_t *format_list = obs_properties_add_list(
  697. props, "pixelformat", obs_module_text("VideoFormat"),
  698. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  699. obs_property_t *standard_list = obs_properties_add_list(
  700. props, "standard", obs_module_text("VideoStandard"),
  701. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  702. obs_property_set_visible(standard_list, false);
  703. obs_property_t *dv_timing_list = obs_properties_add_list(
  704. props, "dv_timing", obs_module_text("DVTiming"),
  705. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  706. obs_property_set_visible(dv_timing_list, false);
  707. obs_property_t *resolution_list = obs_properties_add_list(
  708. props, "resolution", obs_module_text("Resolution"),
  709. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  710. obs_properties_add_list(props, "framerate",
  711. obs_module_text("FrameRate"),
  712. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  713. obs_property_t *color_range_list = obs_properties_add_list(
  714. props, "color_range", obs_module_text("ColorRange"),
  715. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  716. obs_property_list_add_int(color_range_list,
  717. obs_module_text("ColorRange.Default"),
  718. VIDEO_RANGE_DEFAULT);
  719. obs_property_list_add_int(color_range_list,
  720. obs_module_text("ColorRange.Partial"),
  721. VIDEO_RANGE_PARTIAL);
  722. obs_property_list_add_int(color_range_list,
  723. obs_module_text("ColorRange.Full"),
  724. VIDEO_RANGE_FULL);
  725. obs_properties_add_bool(props, "buffering",
  726. obs_module_text("UseBuffering"));
  727. obs_properties_add_bool(props, "auto_reset",
  728. obs_module_text("AutoresetOnTimeout"));
  729. obs_properties_add_int(props, "timeout_frames",
  730. obs_module_text("FramesUntilTimeout"), 2, 120,
  731. 1);
  732. // a group to contain the camera control
  733. obs_properties_t *ctrl_props = obs_properties_create();
  734. obs_properties_add_group(props, "controls",
  735. obs_module_text("CameraCtrls"),
  736. OBS_GROUP_NORMAL, ctrl_props);
  737. obs_data_t *settings = obs_source_get_settings(data->source);
  738. v4l2_device_list(device_list, settings);
  739. obs_data_release(settings);
  740. obs_property_set_modified_callback(device_list, device_selected);
  741. obs_property_set_modified_callback(input_list, input_selected);
  742. obs_property_set_modified_callback(format_list, format_selected);
  743. obs_property_set_modified_callback(resolution_list,
  744. resolution_selected);
  745. return props;
  746. }
  747. static void v4l2_terminate(struct v4l2_data *data)
  748. {
  749. if (data->thread) {
  750. os_event_signal(data->event);
  751. pthread_join(data->thread, NULL);
  752. os_event_destroy(data->event);
  753. data->thread = 0;
  754. }
  755. v4l2_destroy_mjpeg(&data->mjpeg_decoder);
  756. v4l2_destroy_mmap(&data->buffers);
  757. if (data->dev != -1) {
  758. v4l2_close(data->dev);
  759. data->dev = -1;
  760. }
  761. }
  762. static void v4l2_destroy(void *vptr)
  763. {
  764. V4L2_DATA(vptr);
  765. if (!data)
  766. return;
  767. v4l2_terminate(data);
  768. if (data->device_id)
  769. bfree(data->device_id);
  770. #if HAVE_UDEV
  771. signal_handler_t *sh = v4l2_get_udev_signalhandler();
  772. signal_handler_disconnect(sh, "device_added", device_added, data);
  773. signal_handler_disconnect(sh, "device_removed", device_removed, data);
  774. v4l2_unref_udev();
  775. #endif
  776. bfree(data);
  777. }
  778. /**
  779. * Initialize the v4l2 device
  780. *
  781. * This function:
  782. * - tries to open the device
  783. * - sets pixelformat and requested resolution
  784. * - sets the requested framerate
  785. * - maps the buffers
  786. * - starts the capture thread
  787. */
  788. static void v4l2_init(struct v4l2_data *data)
  789. {
  790. uint32_t input_caps;
  791. int fps_num, fps_denom;
  792. blog(LOG_INFO, "Start capture from %s", data->device_id);
  793. data->dev = v4l2_open(data->device_id, O_RDWR | O_NONBLOCK);
  794. if (data->dev == -1) {
  795. blog(LOG_ERROR, "Unable to open device");
  796. goto fail;
  797. }
  798. /* set input */
  799. if (v4l2_set_input(data->dev, &data->input) < 0) {
  800. blog(LOG_ERROR, "Unable to set input %d", data->input);
  801. goto fail;
  802. }
  803. blog(LOG_INFO, "Input: %d", data->input);
  804. if (v4l2_get_input_caps(data->dev, -1, &input_caps) < 0) {
  805. blog(LOG_ERROR, "Unable to get input capabilities");
  806. goto fail;
  807. }
  808. /* set video standard if supported */
  809. if (input_caps & V4L2_IN_CAP_STD) {
  810. if (v4l2_set_standard(data->dev, &data->standard) < 0) {
  811. blog(LOG_ERROR, "Unable to set video standard");
  812. goto fail;
  813. }
  814. data->resolution = -1;
  815. data->framerate = -1;
  816. }
  817. /* set dv timing if supported */
  818. if (input_caps & V4L2_IN_CAP_DV_TIMINGS) {
  819. if (v4l2_set_dv_timing(data->dev, &data->dv_timing) < 0) {
  820. blog(LOG_ERROR, "Unable to set dv timing");
  821. goto fail;
  822. }
  823. data->resolution = -1;
  824. data->framerate = -1;
  825. }
  826. /* set pixel format and resolution */
  827. if (v4l2_set_format(data->dev, &data->resolution, &data->pixfmt,
  828. &data->linesize) < 0) {
  829. blog(LOG_ERROR, "Unable to set format");
  830. goto fail;
  831. }
  832. if (v4l2_to_obs_video_format(data->pixfmt) == VIDEO_FORMAT_NONE &&
  833. data->pixfmt != V4L2_PIX_FMT_MJPEG) {
  834. blog(LOG_ERROR, "Selected video format not supported");
  835. goto fail;
  836. }
  837. v4l2_unpack_tuple(&data->width, &data->height, data->resolution);
  838. blog(LOG_INFO, "Resolution: %dx%d", data->width, data->height);
  839. blog(LOG_INFO, "Pixelformat: %s", V4L2_FOURCC_STR(data->pixfmt));
  840. blog(LOG_INFO, "Linesize: %d Bytes", data->linesize);
  841. /* set framerate */
  842. if (v4l2_set_framerate(data->dev, &data->framerate) < 0) {
  843. blog(LOG_ERROR, "Unable to set framerate");
  844. goto fail;
  845. }
  846. v4l2_unpack_tuple(&fps_num, &fps_denom, data->framerate);
  847. blog(LOG_INFO, "Framerate: %.2f fps", (float)fps_denom / fps_num);
  848. /* map buffers */
  849. if (v4l2_create_mmap(data->dev, &data->buffers) < 0) {
  850. blog(LOG_ERROR, "Failed to map buffers");
  851. goto fail;
  852. }
  853. if (v4l2_init_mjpeg(&data->mjpeg_decoder) < 0) {
  854. blog(LOG_ERROR, "Failed to initialize mjpeg decoder");
  855. goto fail;
  856. }
  857. /* start the capture thread */
  858. if (os_event_init(&data->event, OS_EVENT_TYPE_MANUAL) != 0)
  859. goto fail;
  860. if (pthread_create(&data->thread, NULL, v4l2_thread, data) != 0)
  861. goto fail;
  862. return;
  863. fail:
  864. blog(LOG_ERROR, "Initialization failed");
  865. v4l2_terminate(data);
  866. }
  867. /** Update source flags depending on the settings */
  868. static void v4l2_update_source_flags(struct v4l2_data *data,
  869. obs_data_t *settings)
  870. {
  871. obs_source_set_async_unbuffered(
  872. data->source, !obs_data_get_bool(settings, "buffering"));
  873. }
  874. /**
  875. * Checking if any of the settings have changed so that we can restart the
  876. * stream
  877. */
  878. static bool v4l2_settings_changed(struct v4l2_data *data, obs_data_t *settings)
  879. {
  880. bool res = false;
  881. if (obs_data_get_string(settings, "device_id") != NULL &&
  882. data->device_id != NULL) {
  883. res |= strcmp(data->device_id,
  884. obs_data_get_string(settings, "device_id")) != 0;
  885. res |= data->input != obs_data_get_int(settings, "input");
  886. res |= data->pixfmt !=
  887. obs_data_get_int(settings, "pixelformat");
  888. res |= data->standard != obs_data_get_int(settings, "standard");
  889. res |= data->dv_timing !=
  890. obs_data_get_int(settings, "dv_timing");
  891. if (obs_data_get_int(settings, "resolution") == -1 &&
  892. !data->resolution_unchanged) {
  893. data->resolution_unchanged = true;
  894. res |= true;
  895. } else if (obs_data_get_int(settings, "resolution") == -1 &&
  896. data->resolution_unchanged) {
  897. res |= false;
  898. } else {
  899. data->resolution_unchanged = false;
  900. res |= (data->resolution !=
  901. obs_data_get_int(settings, "resolution")) &&
  902. (obs_data_get_int(settings, "resolution") != -1);
  903. }
  904. if (obs_data_get_int(settings, "framerate") == -1 &&
  905. !data->framerate_unchanged) {
  906. data->framerate_unchanged = true;
  907. res |= true;
  908. } else if (obs_data_get_int(settings, "framerate") == -1 &&
  909. data->framerate_unchanged) {
  910. res |= false;
  911. } else {
  912. data->framerate_unchanged = false;
  913. res |= (data->framerate !=
  914. obs_data_get_int(settings, "framerate")) &&
  915. (obs_data_get_int(settings, "framerate") != -1);
  916. }
  917. res |= data->color_range !=
  918. obs_data_get_int(settings, "color_range");
  919. } else {
  920. res = true;
  921. }
  922. return res;
  923. }
  924. /**
  925. * Update the settings for the v4l2 source
  926. *
  927. * There are a few settings that can be changed without restarting the stream
  928. * Whenever this is called the currently active stream (if exists) is stopped,
  929. * the settings are updated and finally the new stream is started.
  930. */
  931. static void v4l2_update(void *vptr, obs_data_t *settings)
  932. {
  933. V4L2_DATA(vptr);
  934. bool needs_restart = v4l2_settings_changed(data, settings);
  935. if (needs_restart)
  936. v4l2_terminate(data);
  937. if (data->device_id)
  938. bfree(data->device_id);
  939. data->device_id = bstrdup(obs_data_get_string(settings, "device_id"));
  940. data->input = obs_data_get_int(settings, "input");
  941. data->pixfmt = obs_data_get_int(settings, "pixelformat");
  942. data->standard = obs_data_get_int(settings, "standard");
  943. data->dv_timing = obs_data_get_int(settings, "dv_timing");
  944. data->resolution = obs_data_get_int(settings, "resolution");
  945. data->framerate = obs_data_get_int(settings, "framerate");
  946. data->color_range = obs_data_get_int(settings, "color_range");
  947. data->auto_reset = obs_data_get_bool(settings, "auto_reset");
  948. data->timeout_frames = obs_data_get_int(settings, "timeout_frames");
  949. v4l2_update_source_flags(data, settings);
  950. if (needs_restart)
  951. v4l2_init(data);
  952. }
  953. static void *v4l2_create(obs_data_t *settings, obs_source_t *source)
  954. {
  955. struct v4l2_data *data = bzalloc(sizeof(struct v4l2_data));
  956. data->dev = -1;
  957. data->source = source;
  958. data->resolution_unchanged = false;
  959. data->framerate_unchanged = false;
  960. /* Bitch about build problems ... */
  961. #ifndef V4L2_CAP_DEVICE_CAPS
  962. blog(LOG_WARNING, "Plugin built without device caps support!");
  963. #endif
  964. #if !defined(VIDIOC_ENUM_DV_TIMINGS) || !defined(V4L2_IN_CAP_DV_TIMINGS)
  965. blog(LOG_WARNING, "Plugin built without dv-timing support!");
  966. #endif
  967. v4l2_update(data, settings);
  968. #if HAVE_UDEV
  969. v4l2_init_udev();
  970. signal_handler_t *sh = v4l2_get_udev_signalhandler();
  971. signal_handler_connect(sh, "device_added", &device_added, data);
  972. signal_handler_connect(sh, "device_removed", &device_removed, data);
  973. #endif
  974. return data;
  975. }
  976. struct obs_source_info v4l2_input = {
  977. .id = "v4l2_input",
  978. .type = OBS_SOURCE_TYPE_INPUT,
  979. .output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_DO_NOT_DUPLICATE,
  980. .get_name = v4l2_getname,
  981. .create = v4l2_create,
  982. .destroy = v4l2_destroy,
  983. .update = v4l2_update,
  984. .get_defaults = v4l2_defaults,
  985. .get_properties = v4l2_properties,
  986. .icon_type = OBS_ICON_TYPE_CAMERA,
  987. };