From patchwork Wed Aug 5 15:14:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 9226 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id C89FCBD86F for ; Wed, 5 Aug 2020 15:14:48 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9310F605AE; Wed, 5 Aug 2020 17:14:48 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=uajain.com header.i=@uajain.com header.b="IxljR5fS"; dkim-atps=neutral Received: from o1.f.az.sendgrid.net (o1.f.az.sendgrid.net [208.117.55.132]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 7403C60392 for ; Wed, 5 Aug 2020 17:14:46 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uajain.com; h=from:subject:in-reply-to:references:mime-version:to:cc: content-transfer-encoding:content-type; s=s1; bh=vsmP+YwBsxogne+hm5MTk+A6/YWvfvAk7Tg9eZacMxg=; b=IxljR5fSm7sgB/yl9fWLNcYr4+2vlGja1H5rj6N/S76JISA473Dl4JFH/Wir2tn0J4O+ LdETvSNLNAI6awAbBtzCE4Koog7rHCY+i/DQhF0zCG7kMCqpWHeohmVWyO9y8d+nhXjMg8 +j5EOqVz6t8reRv2IK8I819DHxz8qo6Uw= Received: by filterdrecv-p3las1-559bd7b968-wk85c with SMTP id filterdrecv-p3las1-559bd7b968-wk85c-19-5F2ACCE4-7F 2020-08-05 15:14:44.744677232 +0000 UTC m=+593915.478323937 Received: from mail.uajain.com (unknown) by ismtpd0003p1maa1.sendgrid.net (SG) with ESMTP id MyAD5_rmSZyzyU9OTWX_2w for ; Wed, 05 Aug 2020 15:14:44.089 +0000 (UTC) From: Umang Jain Date: Wed, 05 Aug 2020 15:14:44 +0000 (UTC) Message-Id: <20200805151437.157155-3-email@uajain.com> In-Reply-To: <20200805151437.157155-1-email@uajain.com> References: <20200805151437.157155-1-email@uajain.com> Mime-Version: 1.0 X-SG-EID: 1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPc+mYHlj5pFD2lBxsaGLVsOCDqbDvgI+twqiPnkVPbvDJz4I3xKGOwPVaxLzK3xEsQWyiGUgWI/Vw+4+V3/Im8zNYu+tKuuyiFBgrwFs1U5Ooch9vtROwq/JYq9ZMyva4ThsfEBdHzfN8zVUe2XrG9M+//LPMPZm5bnYxfqMkWCnAjp65uHrfc0kYsD9SkyJVZc2pghy8JDwSSqsxN+520mA== To: libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [PATCH 2/2] android: camera_hal_manager: Support camera hotplug X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Extend the support for camera hotplug from libcamera's CameraManager to CameraHalManager. Use camera module callbacks to let the framework know about the hotplug events and change the status of cameras being being hotplugged or unplugged via camera_device_status_change(). Signed-off-by: Umang Jain --- src/android/camera_device.h | 1 + src/android/camera_hal_manager.cpp | 40 ++++++++++++++++++++++++++++-- src/android/camera_hal_manager.h | 3 +++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/android/camera_device.h b/src/android/camera_device.h index 4e5fb98..fa9706c 100644 --- a/src/android/camera_device.h +++ b/src/android/camera_device.h @@ -47,6 +47,7 @@ public: unsigned int id() const { return id_; } camera3_device_t *camera3Device() { return &camera3Device_; } + const libcamera::Camera *getCamera() { return camera_.get(); }; int facing() const { return facing_; } int orientation() const { return orientation_; } diff --git a/src/android/camera_hal_manager.cpp b/src/android/camera_hal_manager.cpp index 3ddcab5..b498278 100644 --- a/src/android/camera_hal_manager.cpp +++ b/src/android/camera_hal_manager.cpp @@ -59,8 +59,6 @@ int CameraHalManager::init() /* * For each Camera registered in the system, a CameraDevice * gets created here to wraps a libcamera Camera instance. - * - * \todo Support camera hotplug. */ unsigned int index = 0; for (auto &cam : cameraManager_->cameras()) { @@ -73,6 +71,10 @@ int CameraHalManager::init() ++index; } + /* Support camera hotplug */ + cameraManager_->cameraAdded.connect(this, &CameraHalManager::cameraAdded); + cameraManager_->cameraRemoved.connect(this, &CameraHalManager::cameraRemoved); + return 0; } @@ -93,6 +95,40 @@ CameraDevice *CameraHalManager::open(unsigned int id, return camera; } +void CameraHalManager::cameraAdded(std::shared_ptr cam) +{ + unsigned int id = cameras_.size(); + CameraDevice *camera = new CameraDevice(id, cam); + int ret = camera->initialize(); + if (ret) { + LOG(HAL, Error) << "Failed to initialize camera: " << cam->name(); + return; + } + + cameras_.emplace_back(camera); + callbacks_->camera_device_status_change(callbacks_, id, + CAMERA_DEVICE_STATUS_PRESENT); + LOG(HAL, Debug) << "Camera " << cam->name() << " ID:" << id + << " added and initialized successfully."; +} + +void CameraHalManager::cameraRemoved(std::shared_ptr cam) +{ + auto iter = std::find_if(cameras_.begin(), cameras_.end(), + [cam](std::unique_ptr &camera) { + return cam.get() == camera->getCamera(); + }); + if (iter == cameras_.end()) + return; + + unsigned int id = (*iter)->id(); + callbacks_->camera_device_status_change(callbacks_, id, + CAMERA_DEVICE_STATUS_NOT_PRESENT); + cameras_.erase(iter); + LOG(HAL, Debug) << "Camera " << cam->name() << " ID:" << id + << " removed successfully."; +} + unsigned int CameraHalManager::numCameras() const { return cameraManager_->cameras().size(); diff --git a/src/android/camera_hal_manager.h b/src/android/camera_hal_manager.h index 4345b1e..52ab9e2 100644 --- a/src/android/camera_hal_manager.h +++ b/src/android/camera_hal_manager.h @@ -33,6 +33,9 @@ public: int setCallbacks(const camera_module_callbacks_t *callbacks); private: + void cameraAdded(std::shared_ptr cam); + void cameraRemoved(std::shared_ptr cam); + libcamera::CameraManager *cameraManager_; const camera_module_callbacks_t *callbacks_;