From 2b314587638f0a14b95cf579ba963c2237560537 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Sat, 3 Jul 2021 18:33:55 +0300 Subject: libcamera: pipeline: simple: Store all entity devices in common map Merge the SimplePipelineHandler videos_ and subdevs_ maps, which respectively store V4L2 video devices and subdevices associated with entities, into a single entities_ map that contains an EntityData structure. This gathers all data about entities in a single place, allowing for easy extension of entity data in the future. Signed-off-by: Laurent Pinchart Tested-by: Martin Kepplinger --- src/libcamera/pipeline/simple/simple.cpp | 65 ++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/libcamera/pipeline/simple/simple.cpp b/src/libcamera/pipeline/simple/simple.cpp index a91ee639..c3fd7e1f 100644 --- a/src/libcamera/pipeline/simple/simple.cpp +++ b/src/libcamera/pipeline/simple/simple.cpp @@ -267,6 +267,11 @@ protected: private: static constexpr unsigned int kNumInternalBuffers = 3; + struct EntityData { + std::unique_ptr video; + std::unique_ptr subdev; + }; + SimpleCameraData *cameraData(Camera *camera) { return static_cast(camera->_d()); @@ -279,8 +284,7 @@ private: void converterOutputDone(FrameBuffer *buffer); MediaDevice *media_; - std::map> videos_; - std::map subdevs_; + std::map entities_; std::unique_ptr converter_; @@ -1069,24 +1073,25 @@ bool SimplePipelineHandler::match(DeviceEnumerator *enumerator) return false; /* - * Create and open V4L2Subdevice instances for all entities - * corresponding to a V4L2 subdevice. + * Insert all entities in the global entities list. Create and open + * V4L2Subdevice instances for each entity corresponding to a V4L2 + * subdevice. */ for (MediaEntity *entity : entities) { - if (entity->type() != MediaEntity::Type::V4L2Subdevice) - continue; - - auto elem = subdevs_.emplace(std::piecewise_construct, - std::forward_as_tuple(entity), - std::forward_as_tuple(entity)); - V4L2Subdevice *subdev = &elem.first->second; - int ret = subdev->open(); - if (ret < 0) { - LOG(SimplePipeline, Error) - << "Failed to open " << subdev->deviceNode() - << ": " << strerror(-ret); - return false; + std::unique_ptr subdev; + + if (entity->type() == MediaEntity::Type::V4L2Subdevice) { + subdev = std::make_unique(entity); + int ret = subdev->open(); + if (ret < 0) { + LOG(SimplePipeline, Error) + << "Failed to open " << subdev->deviceNode() + << ": " << strerror(-ret); + return false; + } } + + entities_[entity] = { nullptr, std::move(subdev) }; } /* Initialize each pipeline and register a corresponding camera. */ @@ -1120,28 +1125,30 @@ V4L2VideoDevice *SimplePipelineHandler::video(const MediaEntity *entity) * by constructing a new one. */ - auto iter = videos_.find(entity); - if (iter != videos_.end()) - return iter->second.get(); + auto iter = entities_.find(entity); + if (iter == entities_.end()) + return nullptr; + + EntityData &data = iter->second; + if (data.video) + return data.video.get(); - std::unique_ptr video = - std::make_unique(entity); - if (video->open() < 0) + data.video = std::make_unique(entity); + if (data.video->open() < 0) return nullptr; - video->bufferReady.connect(this, &SimplePipelineHandler::bufferReady); + data.video->bufferReady.connect(this, &SimplePipelineHandler::bufferReady); - auto element = videos_.emplace(entity, std::move(video)); - return element.first->second.get(); + return data.video.get(); } V4L2Subdevice *SimplePipelineHandler::subdev(const MediaEntity *entity) { - auto iter = subdevs_.find(entity); - if (iter == subdevs_.end()) + auto iter = entities_.find(entity); + if (iter == entities_.end()) return nullptr; - return &iter->second; + return iter->second.subdev.get(); } /* ----------------------------------------------------------------------------- -- cgit v1.2.1