From 9889a1223028a14ac2fd56323fc813b62087c918 Mon Sep 17 00:00:00 2001 From: Paul Elder Date: Tue, 28 Aug 2018 02:10:01 -0400 Subject: v4l2: add v4l2_set_frame_rate We would like to be able to set the frame rate of video sources. This patch adds such a function for v4l2-based video sources to use. Signed-off-by: Paul Elder Reviewed-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- lib/v4l2.c | 20 ++++++++++++++++++++ lib/v4l2.h | 14 ++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/v4l2.c b/lib/v4l2.c index 9030270..de6ed88 100644 --- a/lib/v4l2.c +++ b/lib/v4l2.c @@ -500,6 +500,26 @@ int v4l2_set_format(struct v4l2_device *dev, struct v4l2_pix_format *format) return 0; } +int v4l2_set_frame_rate(struct v4l2_device *dev, unsigned int fps) +{ + struct v4l2_streamparm parm; + int ret; + + memset(&parm, 0, sizeof parm); + parm.type = dev->type; + parm.parm.capture.timeperframe.numerator = 1; + parm.parm.capture.timeperframe.denominator = fps; + + ret = ioctl(dev->fd, VIDIOC_S_PARM, &parm); + if (ret < 0) { + printf("%s: unable to set frame rate (%d).\n", dev->name, errno); + return -errno; + } + + dev->fps = fps; + return 0; +} + /* ----------------------------------------------------------------------------- * Buffers management */ diff --git a/lib/v4l2.h b/lib/v4l2.h index 8d7b3c5..7bacf5d 100644 --- a/lib/v4l2.h +++ b/lib/v4l2.h @@ -31,6 +31,7 @@ struct v4l2_device struct list_entry formats; struct v4l2_pix_format format; struct v4l2_rect crop; + unsigned int fps; struct video_buffer_set buffers; }; @@ -85,6 +86,19 @@ int v4l2_get_format(struct v4l2_device *dev, struct v4l2_pix_format *format); */ int v4l2_set_format(struct v4l2_device *dev, struct v4l2_pix_format *format); +/* + * v4l2_set_frame_rate - Set the frame rate + * @dev: Device instance + * @fps: Frame rate + * + * Set the frame rate specified by @fps. + * The device can modify the requested format and size, in which case @dev->fps + * will be updated to reflect the modified setting. + * + * Return 0 on success or a negative error code on failure. + */ +int v4l2_set_frame_rate(struct v4l2_device *dev, unsigned int fps); + /* * v4l2_get_crop - Retrieve the current crop rectangle * @dev: Device instance -- cgit v1.2.3