v4l2-input.c 32 KB

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