[libcamera-devel] [RFC v3 2/5] libcamera: Request: expose Camera from Request

Kieran Bingham kieran.bingham at ideasonboard.com
Thu Dec 9 10:46:47 CET 2021


Quoting Tomi Valkeinen (2021-12-09 09:29:03)
> Request has Camera as a private member. Expose this so that users can
> more easily associate a received Request to a Camera.
> 

Aha, I created a very similar patch in the past, but only got one ack so
it didn't get in:

https://patchwork.libcamera.org/patch/9328/

That one only returned a direct pointer to the Camera, and I think given
it's the public API, returning a shared_ptr is safer, as it keeps the
camera alive while the application still has a reference.


Thinking futher to really horrible corner cases, if we're shutting down
perhaps - the Camera might be destructed while the Request is still
available. In that instance I think the Camera would then be invalid. It
would be helpful if we could somehow make sure we invalidate the
pointers in the Request when the camera is destroyed.

(I think once created, the ownership of the Request goes to the
application, so they could be 'kept' in the application after the
destruction of the Camera... of course that's really corner cases as it
would certainly be invalid to access Requests after shutting down the
CameraManager or such).


Anyway, I think this is probably better than mine for the shared_ptr
reasoning so:

Reviewed-by: Kieran Bingham <kieran.bingham at ideasonboard.com>

> Signed-off-by: Tomi Valkeinen <tomi.valkeinen at ideasonboard.com>
> ---
>  include/libcamera/request.h | 2 ++
>  src/libcamera/request.cpp   | 5 +++++
>  2 files changed, 7 insertions(+)
> 
> diff --git a/include/libcamera/request.h b/include/libcamera/request.h
> index f434335b..6e8987b6 100644
> --- a/include/libcamera/request.h
> +++ b/include/libcamera/request.h
> @@ -60,6 +60,8 @@ public:
>  
>         std::string toString() const;
>  
> +       std::shared_ptr<Camera> camera() const;
> +
>  private:
>         LIBCAMERA_DISABLE_COPY(Request)
>  
> diff --git a/src/libcamera/request.cpp b/src/libcamera/request.cpp
> index 17fefab7..3651e8ca 100644
> --- a/src/libcamera/request.cpp
> +++ b/src/libcamera/request.cpp
> @@ -218,6 +218,11 @@ FrameBuffer *Request::findBuffer(const Stream *stream) const
>         return it->second;
>  }
>  
> +std::shared_ptr<Camera> Request::camera() const
> +{
> +       return camera_->shared_from_this();
> +}
> +
>  /**
>   * \fn Request::metadata()
>   * \brief Retrieve the request's metadata
> -- 
> 2.25.1
>


More information about the libcamera-devel mailing list