From patchwork Thu Jan 24 10:16:49 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 363 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C513B60CA4 for ; Thu, 24 Jan 2019 11:16:59 +0100 (CET) Received: from pendragon.bb.dnainternet.fi (dfj612yhrgyx302h3jwwy-3.rev.dnainternet.fi [IPv6:2001:14ba:21f5:5b00:ce28:277f:58d7:3ca4]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 5946C2F6; Thu, 24 Jan 2019 11:16:59 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1548325019; bh=aQGQ8RhvH7WMn6uwvrDcTO+ad1MMOuufb7zWhDXlJyk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wne1UQNDzU9EQdXJgXQDJyqCkdjFEGqeam7r0zwkDPYcfhW7qJMQxQeZoUsG3RnD1 x/luYYFF9m/+TJBhqyTLloDzo/SLZEvHADwhhK2pLzT7rbWoqoC0UFz1Kwz/8hqp07 Vi25xAYhOzzHSI0LAE4cNCQCgiiaf+J9vXyvbfgI= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Thu, 24 Jan 2019 12:16:49 +0200 Message-Id: <20190124101651.9993-9-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190124101651.9993-1-laurent.pinchart@ideasonboard.com> References: <20190124101651.9993-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 08/10] libcamera: pipeline_handler: Add camera disconnection support X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 24 Jan 2019 10:17:01 -0000 From: Niklas Söderlund Pipeline handlers are responsible for creating camera instances, but also for destroying them when devices are unplugged. As camera objects are reference-counted this isn't a straightforward operation and involves the camera manager and camera object itself. Add two helper methods in the PipelineHandler base class to register a camera and to register a media device with the pipeline handler. When registering a camera, the registerCamera() helper method will add it to the camera manager. When registering a media device, the registerMediaDevice() helper method will listen to device disconnection events, and disconnect all cameras created by the pipeline handler as a response. Under the hood the PipelineHandler class needs to keep track of registered cameras in order to handle disconnection. They can't be stored as shared pointers as this would create a circular dependency (the Camera class owns a shared pointer to the pipeline handler). Store them as weak pointers instead. This is safe as a reference to the camera is stored in the camera manager, and doesn't get removed until the camera is unregistered from the manager by the PipelineHandler. Signed-off-by: Niklas Söderlund Signed-off-by: Laurent Pinchart --- src/libcamera/include/pipeline_handler.h | 10 ++++ src/libcamera/pipeline/ipu3/ipu3.cpp | 3 +- src/libcamera/pipeline/uvcvideo.cpp | 3 +- src/libcamera/pipeline/vimc.cpp | 3 +- src/libcamera/pipeline_handler.cpp | 71 ++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 6 deletions(-) diff --git a/src/libcamera/include/pipeline_handler.h b/src/libcamera/include/pipeline_handler.h index e1d6369eb0c4..804cce4807ee 100644 --- a/src/libcamera/include/pipeline_handler.h +++ b/src/libcamera/include/pipeline_handler.h @@ -16,6 +16,7 @@ namespace libcamera { class CameraManager; class DeviceEnumerator; +class MediaDevice; class PipelineHandler : public std::enable_shared_from_this { @@ -27,6 +28,15 @@ public: protected: CameraManager *manager_; + + void registerCamera(std::shared_ptr camera); + void hotplugMediaDevice(MediaDevice *media); + +private: + virtual void disconnect(); + void mediaDeviceDisconnected(MediaDevice *media); + + std::vector> cameras_; }; class PipelineHandlerFactory diff --git a/src/libcamera/pipeline/ipu3/ipu3.cpp b/src/libcamera/pipeline/ipu3/ipu3.cpp index 9831f74fe53f..3161e71420ed 100644 --- a/src/libcamera/pipeline/ipu3/ipu3.cpp +++ b/src/libcamera/pipeline/ipu3/ipu3.cpp @@ -9,7 +9,6 @@ #include #include -#include #include "device_enumerator.h" #include "log.h" @@ -169,7 +168,7 @@ void PipelineHandlerIPU3::registerCameras() std::string cameraName = sensor->name() + " " + std::to_string(id); std::shared_ptr camera = Camera::create(this, cameraName); - manager_->addCamera(std::move(camera)); + registerCamera(std::move(camera)); LOG(IPU3, Info) << "Registered Camera[" << numCameras << "] \"" diff --git a/src/libcamera/pipeline/uvcvideo.cpp b/src/libcamera/pipeline/uvcvideo.cpp index 73bad6714bb4..c8f1bf553bfe 100644 --- a/src/libcamera/pipeline/uvcvideo.cpp +++ b/src/libcamera/pipeline/uvcvideo.cpp @@ -6,7 +6,6 @@ */ #include -#include #include "device_enumerator.h" #include "media_device.h" @@ -49,7 +48,7 @@ bool PipelineHandlerUVC::match(DeviceEnumerator *enumerator) dev_->acquire(); std::shared_ptr camera = Camera::create(this, dev_->model()); - manager_->addCamera(std::move(camera)); + registerCamera(std::move(camera)); return true; } diff --git a/src/libcamera/pipeline/vimc.cpp b/src/libcamera/pipeline/vimc.cpp index 521b20d3a120..b714a07688e9 100644 --- a/src/libcamera/pipeline/vimc.cpp +++ b/src/libcamera/pipeline/vimc.cpp @@ -6,7 +6,6 @@ */ #include -#include #include "device_enumerator.h" #include "media_device.h" @@ -58,7 +57,7 @@ bool PipeHandlerVimc::match(DeviceEnumerator *enumerator) dev_->acquire(); std::shared_ptr camera = Camera::create(this, "Dummy VIMC Camera"); - manager_->addCamera(std::move(camera)); + registerCamera(std::move(camera)); return true; } diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp index 3850ea8fadb5..f0aa2f8022c2 100644 --- a/src/libcamera/pipeline_handler.cpp +++ b/src/libcamera/pipeline_handler.cpp @@ -5,7 +5,11 @@ * pipeline_handler.cpp - Pipeline handler infrastructure */ +#include +#include + #include "log.h" +#include "media_device.h" #include "pipeline_handler.h" /** @@ -97,6 +101,73 @@ PipelineHandler::~PipelineHandler() * constant for the whole lifetime of the pipeline handler. */ +/** + * \brief Register a camera to the camera manager and pipeline handler + * \param[in] camera The camera to be added + * + * This function is called by pipeline handlers to register the cameras they + * handle with the camera manager. + */ +void PipelineHandler::registerCamera(std::shared_ptr camera) +{ + cameras_.push_back(camera); + manager_->addCamera(std::move(camera)); +} + +/** + * \brief Handle hotplugging of a media device + * \param[in] media The media device + * + * This function enables hotplug handling, and especially hot-unplug handling, + * of the \a media device. It shall be called by pipeline handlers for all the + * media devices that can be disconnected. + * + * When a media device passed to this function is later unplugged, the pipeline + * handler gets notified and automatically disconnects all the cameras it has + * registered without requiring any manual intervention. + */ +void PipelineHandler::hotplugMediaDevice(MediaDevice *media) +{ + media->disconnected.connect(this, &PipelineHandler::mediaDeviceDisconnected); +} + +/** + * \brief Device disconnection handler + * + * This virtual function is called to notify the pipeline handler that the + * device it handles has been disconnected. It notifies all cameras created by + * the pipeline handler that they have been disconnected, and unregisters them + * from the camera manager. + * + * The function can be overloaded by pipeline handlers to perform custom + * operations at disconnection time. Any overloaded version shall call the + * PipelineHandler::disconnect() base function for proper hot-unplug operation. + */ +void PipelineHandler::disconnect() +{ + for (std::weak_ptr ptr : cameras_) { + std::shared_ptr camera = ptr.lock(); + if (!camera) + continue; + + camera->disconnect(); + manager_->removeCamera(camera.get()); + } + + cameras_.clear(); +} + +/** + * \brief Slot for the MediaDevice disconnected signal + */ +void PipelineHandler::mediaDeviceDisconnected(MediaDevice *media) +{ + if (cameras_.empty()) + return; + + disconnect(); +} + /** * \class PipelineHandlerFactory * \brief Registration of PipelineHandler classes and creation of instances