[4/9] libcamera: v4l2_videodevice: Add bind context
diff mbox series

Message ID 20250725-multicontext-v1-4-ea558291e101@ideasonboard.com
State New
Headers show
Series
  • libcamera: Support for multi-context operations
Related show

Commit Message

Jacopo Mondi July 25, 2025, 10:33 a.m. UTC
Add a function to the V4L2VideoDevice class that allows to
bind a video device to a media device context using the
VIDIOC_BIND_CONTEXT ioctl.

Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
---
 include/libcamera/internal/v4l2_videodevice.h |  2 ++
 src/libcamera/v4l2_videodevice.cpp            | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+)

Comments

Isaac Scott July 25, 2025, 10:36 a.m. UTC | #1
Hi Jacopo,

Thanks for the patch!

On Fri, 2025-07-25 at 12:33 +0200, Jacopo Mondi wrote:
> Add a function to the V4L2VideoDevice class that allows to
> bind a video device to a media device context using the
> VIDIOC_BIND_CONTEXT ioctl.
> 
> Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
> ---
>  include/libcamera/internal/v4l2_videodevice.h |  2 ++
>  src/libcamera/v4l2_videodevice.cpp            | 18
> ++++++++++++++++++
>  2 files changed, 20 insertions(+)
> 
> diff --git a/include/libcamera/internal/v4l2_videodevice.h
> b/include/libcamera/internal/v4l2_videodevice.h
> index
> ae6a76cb02090ddadf5ad1331a47fd68585cf586..765574b3e0b52b625e927a50045
> a61f8c2a0ac6e 100644
> --- a/include/libcamera/internal/v4l2_videodevice.h
> +++ b/include/libcamera/internal/v4l2_videodevice.h
> @@ -202,6 +202,8 @@ public:
>  
>  	const V4L2Capability &caps() const { return caps_; }
>  
> +	int bindContext(unsigned int context);
> +
>  	int getFormat(V4L2DeviceFormat *format);
>  	int tryFormat(V4L2DeviceFormat *format);
>  	int setFormat(V4L2DeviceFormat *format);
> diff --git a/src/libcamera/v4l2_videodevice.cpp
> b/src/libcamera/v4l2_videodevice.cpp
> index
> 76bdad40ca8d7a024e4644888737371eff1f09fd..a854f3b10201eaa4f643d0e15fc
> 04b3c49fbeffe 100644
> --- a/src/libcamera/v4l2_videodevice.cpp
> +++ b/src/libcamera/v4l2_videodevice.cpp
> @@ -795,6 +795,24 @@ std::string V4L2VideoDevice::logPrefix() const
>  		(V4L2_TYPE_IS_OUTPUT(bufferType_) ? ":out]" :
> ":cap]");
>  }
>  
> +/**
> + * \brief Bind a video device to a media device context
> + * \param[in] context The file descriptor that identifies the media
> device
> + * context
> + * \return 0 on success or a negative error code otherwise
> + */
> +int V4L2VideoDevice::bindContext(unsigned int context)
> +{
> +	struct v4l2_context c = {};
> +	c.context_fd = context;
> +
> +	int ret = ioctl(VIDIOC_BIND_CONTEXT, &c);
> +	if (ret)
> +		LOG(V4L2, Error) << "Unable to bind context: " <<
> strerror(-ret);
> +
> +	return ret;
> +}

Reviewed-by: Isaac Scott <isaac.scott@ideasonboard.com>

> +
>  /**
>   * \brief Retrieve the image format set on the V4L2 video device
>   * \param[out] format The image format applied on the video device

Patch
diff mbox series

diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h
index ae6a76cb02090ddadf5ad1331a47fd68585cf586..765574b3e0b52b625e927a50045a61f8c2a0ac6e 100644
--- a/include/libcamera/internal/v4l2_videodevice.h
+++ b/include/libcamera/internal/v4l2_videodevice.h
@@ -202,6 +202,8 @@  public:
 
 	const V4L2Capability &caps() const { return caps_; }
 
+	int bindContext(unsigned int context);
+
 	int getFormat(V4L2DeviceFormat *format);
 	int tryFormat(V4L2DeviceFormat *format);
 	int setFormat(V4L2DeviceFormat *format);
diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
index 76bdad40ca8d7a024e4644888737371eff1f09fd..a854f3b10201eaa4f643d0e15fc04b3c49fbeffe 100644
--- a/src/libcamera/v4l2_videodevice.cpp
+++ b/src/libcamera/v4l2_videodevice.cpp
@@ -795,6 +795,24 @@  std::string V4L2VideoDevice::logPrefix() const
 		(V4L2_TYPE_IS_OUTPUT(bufferType_) ? ":out]" : ":cap]");
 }
 
+/**
+ * \brief Bind a video device to a media device context
+ * \param[in] context The file descriptor that identifies the media device
+ * context
+ * \return 0 on success or a negative error code otherwise
+ */
+int V4L2VideoDevice::bindContext(unsigned int context)
+{
+	struct v4l2_context c = {};
+	c.context_fd = context;
+
+	int ret = ioctl(VIDIOC_BIND_CONTEXT, &c);
+	if (ret)
+		LOG(V4L2, Error) << "Unable to bind context: " << strerror(-ret);
+
+	return ret;
+}
+
 /**
  * \brief Retrieve the image format set on the V4L2 video device
  * \param[out] format The image format applied on the video device