[{"id":3311,"web_url":"https://patchwork.libcamera.org/comment/3311/","msgid":"<20200103113003.GC4847@pendragon.ideasonboard.com>","date":"2020-01-03T11:30:03","subject":"Re: [libcamera-devel] [PATCH v5 2/4] libcamera: camera_manager,\n\tpipeline_handler: allow retrieving cameras by device numbers","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Paul,\n\nThank you for the patch.\n\nOn Fri, Jan 03, 2020 at 12:41:18AM -0500, Paul Elder wrote:\n> The V4L2 compatibility layer will need a way to map device numbers to\n> libcamera Camera instances. Expose a method in the camera manager to\n> retrieve Camera instances by devnum. The mapping from device numbers to\n> Camera instances is optionally declared by pipeline handlers when they\n> register cameras with the camera manager.\n> \n> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> \n> ---\n> Changes in v5:\n> - remove devnum -> Camera mapping in CameraManager::removeCamera()\n> - other cosmetic changes\n> \n> Changes in v4:\n> - squashed 4/6 from v3\n> - simplified registering devnum -> camera mappings\n>   - old (v3) system: pipeline handlers have their own map, that camera\n>     manager aggregates\n>   - new (v4) system: pipeline handlers, when they register the camera\n>     with the camera manager, (optionally) declare the devnum along with it\n> \n> New in v3\n> ---\n>  include/libcamera/camera_manager.h       |  6 ++-\n>  src/libcamera/camera_manager.cpp         | 61 ++++++++++++++++++++----\n>  src/libcamera/include/pipeline_handler.h |  3 +-\n>  src/libcamera/pipeline_handler.cpp       | 13 ++++-\n>  4 files changed, 69 insertions(+), 14 deletions(-)\n> \n> diff --git a/include/libcamera/camera_manager.h b/include/libcamera/camera_manager.h\n> index 8331898c..09419766 100644\n> --- a/include/libcamera/camera_manager.h\n> +++ b/include/libcamera/camera_manager.h\n> @@ -7,8 +7,10 @@\n>  #ifndef __LIBCAMERA_CAMERA_MANAGER_H__\n>  #define __LIBCAMERA_CAMERA_MANAGER_H__\n>  \n> +#include <map>\n>  #include <memory>\n>  #include <string>\n> +#include <sys/types.h>\n>  #include <vector>\n>  \n>  #include <libcamera/object.h>\n> @@ -33,8 +35,9 @@ public:\n>  \n>  \tconst std::vector<std::shared_ptr<Camera>> &cameras() const { return cameras_; }\n>  \tstd::shared_ptr<Camera> get(const std::string &name);\n> +\tstd::shared_ptr<Camera> get(dev_t devnum);\n>  \n> -\tvoid addCamera(std::shared_ptr<Camera> camera);\n> +\tvoid addCamera(std::shared_ptr<Camera> camera, dev_t devnum);\n>  \tvoid removeCamera(Camera *camera);\n>  \n>  \tstatic const std::string &version() { return version_; }\n> @@ -46,6 +49,7 @@ private:\n>  \tstd::unique_ptr<DeviceEnumerator> enumerator_;\n>  \tstd::vector<std::shared_ptr<PipelineHandler>> pipes_;\n>  \tstd::vector<std::shared_ptr<Camera>> cameras_;\n> +\tstd::map<dev_t, std::weak_ptr<Camera>> camerasByDevnum_;\n>  \n>  \tstatic const std::string version_;\n>  \tstatic CameraManager *self_;\n> diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp\n> index 7c6f72bb..88f8112c 100644\n> --- a/src/libcamera/camera_manager.cpp\n> +++ b/src/libcamera/camera_manager.cpp\n> @@ -180,15 +180,42 @@ std::shared_ptr<Camera> CameraManager::get(const std::string &name)\n>  \treturn nullptr;\n>  }\n>  \n> +/**\n> + * \\brief Retrieve a camera based on device number\n> + * \\param[in] devnum Device number of camera to get\n> + *\n> + * This method is meant solely for the use of the V4L2 compatibility\n> + * layer, to map device nodes to Camera instances. Applications shall\n> + * not use it and shall instead retrieve cameras by name.\n> + *\n> + * Before calling this function the caller is responsible for ensuring that\n> + * the camera manager is running.\n> + *\n> + * \\return Shared pointer to Camera object, which is empty if the camera is\n> + * not found\n> + */\n> +std::shared_ptr<Camera> CameraManager::get(dev_t devnum)\n> +{\n> +\tauto iter = camerasByDevnum_.find(devnum);\n> +\tif (iter == camerasByDevnum_.end())\n> +\t\treturn nullptr;\n> +\n> +\treturn iter->second.lock();\n> +}\n> +\n>  /**\n>   * \\brief Add a camera to the camera manager\n>   * \\param[in] camera The camera to be added\n> + * \\param[in] devnum The device number to associate with \\a camera\n>   *\n>   * This function is called by pipeline handlers to register the cameras they\n>   * handle with the camera manager. Registered cameras are immediately made\n>   * available to the system.\n> + *\n> + * \\a devnum is used by the V4L2 compatibility layer to map V4L2 device nodes\n> + * to Camera instances.\n>   */\n> -void CameraManager::addCamera(std::shared_ptr<Camera> camera)\n> +void CameraManager::addCamera(std::shared_ptr<Camera> camera, dev_t devnum)\n>  {\n>  \tfor (std::shared_ptr<Camera> c : cameras_) {\n>  \t\tif (c->name() == camera->name()) {\n> @@ -200,6 +227,11 @@ void CameraManager::addCamera(std::shared_ptr<Camera> camera)\n>  \t}\n>  \n>  \tcameras_.push_back(std::move(camera));\n> +\n> +\tif (devnum) {\n> +\t\tunsigned int index = cameras_.size() - 1;\n> +\t\tcamerasByDevnum_[devnum] = cameras_[index];\n> +\t}\n>  }\n>  \n>  /**\n> @@ -212,15 +244,24 @@ void CameraManager::addCamera(std::shared_ptr<Camera> camera)\n>   */\n>  void CameraManager::removeCamera(Camera *camera)\n>  {\n> -\tfor (auto iter = cameras_.begin(); iter != cameras_.end(); ++iter) {\n> -\t\tif (iter->get() == camera) {\n> -\t\t\tLOG(Camera, Debug)\n> -\t\t\t\t<< \"Unregistering camera '\"\n> -\t\t\t\t<< camera->name() << \"'\";\n> -\t\t\tcameras_.erase(iter);\n> -\t\t\treturn;\n> -\t\t}\n> -\t}\n> +\tauto iter_d = std::find_if(camerasByDevnum_.begin(), camerasByDevnum_.end(),\n> +\t\t\t\t   [camera](const std::pair<dev_t, std::weak_ptr<Camera>> &p) {\n> +\t\t\t\t\t   return p.second.lock().get() == camera;\n> +\t\t\t\t   });\n> +\tif (iter_d != camerasByDevnum_.end())\n> +\t\tcamerasByDevnum_.erase(iter_d);\n\nI would move this after the LOG() message, as if the camera isn't found\nin cameras_, it's pointless to look it up in camerasByDevnum_.\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> +\n> +\tauto iter = std::find_if(cameras_.begin(), cameras_.end(),\n> +\t\t\t\t [camera](std::shared_ptr<Camera> &c) {\n> +\t\t\t\t\t return c.get() == camera;\n> +\t\t\t\t });\n> +\tif (iter == cameras_.end())\n> +\t\treturn;\n> +\n> +\tLOG(Camera, Debug)\n> +\t\t<< \"Unregistering camera '\" << camera->name() << \"'\";\n> +\n> +\tcameras_.erase(iter);\n>  }\n>  \n>  /**\n> diff --git a/src/libcamera/include/pipeline_handler.h b/src/libcamera/include/pipeline_handler.h\n> index f3622631..067baef5 100644\n> --- a/src/libcamera/include/pipeline_handler.h\n> +++ b/src/libcamera/include/pipeline_handler.h\n> @@ -12,6 +12,7 @@\n>  #include <memory>\n>  #include <set>\n>  #include <string>\n> +#include <sys/sysmacros.h>\n>  #include <vector>\n>  \n>  #include <ipa/ipa_interface.h>\n> @@ -86,7 +87,7 @@ public:\n>  \n>  protected:\n>  \tvoid registerCamera(std::shared_ptr<Camera> camera,\n> -\t\t\t    std::unique_ptr<CameraData> data);\n> +\t\t\t    std::unique_ptr<CameraData> data, dev_t devnum = 0);\n>  \tvoid hotplugMediaDevice(MediaDevice *media);\n>  \n>  \tvirtual int queueRequestDevice(Camera *camera, Request *request) = 0;\n> diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp\n> index 5badf31c..698dd525 100644\n> --- a/src/libcamera/pipeline_handler.cpp\n> +++ b/src/libcamera/pipeline_handler.cpp\n> @@ -7,6 +7,8 @@\n>  \n>  #include \"pipeline_handler.h\"\n>  \n> +#include <sys/sysmacros.h>\n> +\n>  #include <libcamera/buffer.h>\n>  #include <libcamera/camera.h>\n>  #include <libcamera/camera_manager.h>\n> @@ -438,19 +440,26 @@ void PipelineHandler::completeRequest(Camera *camera, Request *request)\n>   * \\brief Register a camera to the camera manager and pipeline handler\n>   * \\param[in] camera The camera to be added\n>   * \\param[in] data Pipeline-specific data for the camera\n> + * \\param[in] devnum Device number of the camera (optional)\n>   *\n>   * This method is called by pipeline handlers to register the cameras they\n>   * handle with the camera manager. It associates the pipeline-specific \\a data\n>   * with the camera, for later retrieval with cameraData(). Ownership of \\a data\n>   * is transferred to the PipelineHandler.\n> + *\n> + * \\a devnum is the device number (as returned by makedev) that the \\a camera\n> + * is to be associated with. This is for the V4L2 compatibility layer to map\n> + * device nodes to Camera instances based on the device number\n> + * registered by this method in \\a devnum.\n>   */\n>  void PipelineHandler::registerCamera(std::shared_ptr<Camera> camera,\n> -\t\t\t\t     std::unique_ptr<CameraData> data)\n> +\t\t\t\t     std::unique_ptr<CameraData> data,\n> +\t\t\t\t     dev_t devnum)\n>  {\n>  \tdata->camera_ = camera.get();\n>  \tcameraData_[camera.get()] = std::move(data);\n>  \tcameras_.push_back(camera);\n> -\tmanager_->addCamera(std::move(camera));\n> +\tmanager_->addCamera(std::move(camera), devnum);\n>  }\n>  \n>  /**\n> -- \n> 2.24.1\n>","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7741360465\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  3 Jan 2020 12:30:13 +0100 (CET)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id D825F30F;\n\tFri,  3 Jan 2020 12:30:12 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1578051013;\n\tbh=bLhm4rCzlKUh0vbYlxxLExaRX7o+NMXN8kjzU0hBkYM=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=vPEJokv3c6ORyIICb1siEjB+gWruVTT+TeVsQiiWmk8XTXVswDlVhCHTWMg+z5PEU\n\tnJHpbWCy2DT96SQ85CbXx0htvvMgNpTJ4lTzy/orjJhFC0JF/I0vMeTix0pSFtJgpx\n\tiRnAjx0tPZDZZI3UDiCJ1LXjNLatMQzucJ+a0lh8=","Date":"Fri, 3 Jan 2020 13:30:03 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Paul Elder <paul.elder@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org, Jacopo Mondi <jacopo@jmondi.org>","Message-ID":"<20200103113003.GC4847@pendragon.ideasonboard.com>","References":"<20200103054120.30979-1-paul.elder@ideasonboard.com>\n\t<20200103054120.30979-3-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20200103054120.30979-3-paul.elder@ideasonboard.com>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH v5 2/4] libcamera: camera_manager,\n\tpipeline_handler: allow retrieving cameras by device numbers","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Fri, 03 Jan 2020 11:30:13 -0000"}}]