From d6a88607479937a9941f1ea2c82a7e5c66b20647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20S=C3=B6derlund?= Date: Sun, 14 Apr 2019 01:48:57 +0200 Subject: libcamera: pipeline_handler: Keep track of MediaDevice MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of requiring each pipeline handle implementation to keep track of calling release() on its media devices upon deletion, keep track of them in the base class. Add a helper that pipeline handlers shall use to acquire a media device instead of directly interacting with the DeviceEnumerator. This also means that pipeline handler implementations do no need to keep a shared_ptr<> reference to the media devices they store locally as the PipelineHandler base class will keep a shared_ptr<> reference to all media devices consumed for the entire lifetime of the pipeline handler implementation. Centrally keeping track of media devices will also be beneficial to implement exclusive access to pipelines across processes. Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- src/libcamera/pipeline_handler.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/libcamera/pipeline_handler.cpp') diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp index 4ecd6c49..c92ee782 100644 --- a/src/libcamera/pipeline_handler.cpp +++ b/src/libcamera/pipeline_handler.cpp @@ -11,6 +11,7 @@ #include #include +#include "device_enumerator.h" #include "log.h" #include "media_device.h" #include "utils.h" @@ -116,6 +117,8 @@ PipelineHandler::PipelineHandler(CameraManager *manager) PipelineHandler::~PipelineHandler() { + for (std::shared_ptr media : mediaDevices_) + media->release(); }; /** @@ -149,6 +152,35 @@ PipelineHandler::~PipelineHandler() * created, or false otherwise */ +/** + * \brief Search and acquire a MediDevice matching a device pattern + * \param[in] enumerator Enumerator containing all media devices in the system + * \param[in] dm Device match pattern + * + * Search the device \a enumerator for an available media device matching the + * device match pattern \a dm. Matching media device that have previously been + * acquired by MediaDevice::acquire() are not considered. If a match is found, + * the media device is acquired and returned. The caller shall not release the + * device explicitly, it will be automatically released when the pipeline + * handler is destroyed. + * + * \return A pointer to the matching MediaDevice, or nullptr if no match is found + */ +MediaDevice *PipelineHandler::acquireMediaDevice(DeviceEnumerator *enumerator, + const DeviceMatch &dm) +{ + std::shared_ptr media = enumerator->search(dm); + if (!media) + return nullptr; + + if (!media->acquire()) + return nullptr; + + mediaDevices_.push_back(media); + + return media.get(); +} + /** * \fn PipelineHandler::streamConfiguration() * \brief Retrieve a group of stream configurations for a specified camera -- cgit v1.2.1