libcamera: pipeline_handler: Add camera disconnection support

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 <niklas.soderlund@ragnatech.se>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
This commit is contained in:
Niklas Söderlund
2019-01-22 16:47:40 +01:00
committed by Laurent Pinchart
parent 8897260976
commit 607a9d7f56
5 changed files with 84 additions and 6 deletions

View File

@@ -16,6 +16,7 @@ namespace libcamera {
class CameraManager;
class DeviceEnumerator;
class MediaDevice;
class PipelineHandler : public std::enable_shared_from_this<PipelineHandler>
{
@@ -27,6 +28,15 @@ public:
protected:
CameraManager *manager_;
void registerCamera(std::shared_ptr<Camera> camera);
void hotplugMediaDevice(MediaDevice *media);
private:
virtual void disconnect();
void mediaDeviceDisconnected(MediaDevice *media);
std::vector<std::weak_ptr<Camera>> cameras_;
};
class PipelineHandlerFactory