libcamera v0.3.2 Supporting cameras in Linux since 2019

Loading...
Searching...
No Matches
pipeline_handler.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2018, Google Inc.
4 *
5 * Pipeline handler infrastructure
6 */
7
8#pragma once
9
10#include <memory>
11#include <queue>
12#include <string>
13#include <sys/types.h>
14#include <vector>
15
16#include <libcamera/base/object.h>
17
18#include <libcamera/controls.h>
19#include <libcamera/stream.h>
20
21 namespace libcamera {
22
23 class Camera;
24 class CameraConfiguration;
25 class CameraManager;
26 class DeviceEnumerator;
27 class DeviceMatch;
28 class FrameBuffer;
29 class MediaDevice;
30 class PipelineHandler;
31 class Request;
32
33 class PipelineHandler : public std::enable_shared_from_this<PipelineHandler>,
34 public Object
35{
36 public:
37 PipelineHandler(CameraManager *manager);
38 virtual ~PipelineHandler();
39
40 virtual bool match(DeviceEnumerator *enumerator) = 0;
41 MediaDevice *acquireMediaDevice(DeviceEnumerator *enumerator,
42 const DeviceMatch &dm);
43
44 bool acquire(Camera *camera);
45 void release(Camera *camera);
46
47 virtual std::unique_ptr<CameraConfiguration> generateConfiguration(Camera *camera,
48 Span<const StreamRole> roles) = 0;
49 virtual int configure(Camera *camera, CameraConfiguration *config) = 0;
50
51 virtual int exportFrameBuffers(Camera *camera, Stream *stream,
52 std::vector<std::unique_ptr<FrameBuffer>> *buffers) = 0;
53
54 virtual int start(Camera *camera, const ControlList *controls) = 0;
55 void stop(Camera *camera);
56 bool hasPendingRequests(const Camera *camera) const;
57
58 void registerRequest(Request *request);
59 void queueRequest(Request *request);
60
61 bool completeBuffer(Request *request, FrameBuffer *buffer);
62 void completeRequest(Request *request);
63
64 std::string configurationFile(const std::string &subdir,
65 const std::string &name) const;
66
67 const char *name() const { return name_; }
68
69 CameraManager *cameraManager() const { return manager_; }
70
71 protected:
72 void registerCamera(std::shared_ptr<Camera> camera);
73 void hotplugMediaDevice(MediaDevice *media);
74
75 virtual int queueRequestDevice(Camera *camera, Request *request) = 0;
76 virtual void stopDevice(Camera *camera) = 0;
77
78 virtual bool acquireDevice(Camera *camera);
79 virtual void releaseDevice(Camera *camera);
80
81 CameraManager *manager_;
82
83 private:
84 void unlockMediaDevices();
85
86 void mediaDeviceDisconnected(MediaDevice *media);
87 virtual void disconnect();
88
89 void doQueueRequest(Request *request);
90 void doQueueRequests();
91
92 std::vector<std::shared_ptr<MediaDevice>> mediaDevices_;
93 std::vector<std::weak_ptr<Camera>> cameras_;
94
95 std::queue<Request *> waitingRequests_;
96
97 const char *name_;
98 unsigned int useCount_;
99
100 friend class PipelineHandlerFactoryBase;
101};
102
103 class PipelineHandlerFactoryBase
104{
105 public:
106 PipelineHandlerFactoryBase(const char *name);
107 virtual ~PipelineHandlerFactoryBase() = default;
108
109 std::shared_ptr<PipelineHandler> create(CameraManager *manager) const;
110
111 const std::string &name() const { return name_; }
112
113 static std::vector<PipelineHandlerFactoryBase *> &factories();
114 static const PipelineHandlerFactoryBase *getFactoryByName(const std::string &name);
115
116 private:
117 static void registerType(PipelineHandlerFactoryBase *factory);
118
119 virtual std::unique_ptr<PipelineHandler>
120 createInstance(CameraManager *manager) const = 0;
121
122 std::string name_;
123};
124
125 template<typename _PipelineHandler>
126 class PipelineHandlerFactory final : public PipelineHandlerFactoryBase
127{
128 public:
129 PipelineHandlerFactory(const char *name)
130 : PipelineHandlerFactoryBase(name)
131 {
132 }
133
134 std::unique_ptr<PipelineHandler>
135 createInstance(CameraManager *manager) const override
136 {
137 return std::make_unique<_PipelineHandler>(manager);
138 }
139};
140
141#define REGISTER_PIPELINE_HANDLER(handler, name) \
142 static PipelineHandlerFactory<handler> global_##handler##Factory(name);
143
144} /* namespace libcamera */
libcamera::CameraConfiguration
Hold configuration for streams of the camera.
Definition camera.h:60
libcamera::CameraManager
Provide access and manage all cameras in the system.
Definition camera_manager.h:24
libcamera::Camera
Camera device.
Definition camera.h:115
libcamera::ControlList
Associate a list of ControlId with their values for an object.
Definition controls.h:350
libcamera::DeviceEnumerator
Enumerate, store and search media devices.
Definition device_enumerator.h:35
libcamera::DeviceMatch
Description of a media device search pattern.
Definition device_enumerator.h:21
libcamera::FrameBuffer
Frame buffer data and its associated dynamic metadata.
Definition framebuffer.h:49
libcamera::MediaDevice
The MediaDevice represents a Media Controller device with its full graph of connected objects.
Definition media_device.h:25
libcamera::Object
Base object to support automatic signal disconnection.
Definition object.h:25
libcamera::PipelineHandlerFactoryBase
Base class for pipeline handler factories.
Definition pipeline_handler.h:104
libcamera::PipelineHandlerFactoryBase::name
const std::string & name() const
Retrieve the factory name.
Definition pipeline_handler.h:111
libcamera::PipelineHandlerFactoryBase::getFactoryByName
static const PipelineHandlerFactoryBase * getFactoryByName(const std::string &name)
Return the factory for the pipeline handler with name name.
Definition pipeline_handler.cpp:841
libcamera::PipelineHandlerFactoryBase::PipelineHandlerFactoryBase
PipelineHandlerFactoryBase(const char *name)
Construct a pipeline handler factory base.
Definition pipeline_handler.cpp:780
libcamera::PipelineHandlerFactoryBase::factories
static std::vector< PipelineHandlerFactoryBase * > & factories()
Retrieve the list of all pipeline handler factories.
Definition pipeline_handler.cpp:825
libcamera::PipelineHandlerFactoryBase::create
std::shared_ptr< PipelineHandler > create(CameraManager *manager) const
Create an instance of the PipelineHandler corresponding to the factory.
Definition pipeline_handler.cpp:793
libcamera::PipelineHandlerFactory
Registration of PipelineHandler classes and creation of instances.
Definition pipeline_handler.h:127
libcamera::PipelineHandlerFactory::PipelineHandlerFactory
PipelineHandlerFactory(const char *name)
Construct a pipeline handler factory.
Definition pipeline_handler.h:129
libcamera::PipelineHandlerFactory::createInstance
std::unique_ptr< PipelineHandler > createInstance(CameraManager *manager) const override
Create an instance of the PipelineHandler corresponding to the factory.
Definition pipeline_handler.h:135
libcamera::PipelineHandler
Create and manage cameras based on a set of media devices.
Definition pipeline_handler.h:35
libcamera::PipelineHandler::stopDevice
virtual void stopDevice(Camera *camera)=0
Stop capturing from all running streams.
libcamera::PipelineHandler::cameraManager
CameraManager * cameraManager() const
Retrieve the CameraManager that this pipeline handler belongs to.
Definition pipeline_handler.h:69
libcamera::PipelineHandler::hotplugMediaDevice
void hotplugMediaDevice(MediaDevice *media)
Enable hotplug handling for a media device.
Definition pipeline_handler.cpp:682
libcamera::PipelineHandler::name
const char * name() const
Retrieve the pipeline handler name.
Definition pipeline_handler.h:67
libcamera::PipelineHandler::manager_
CameraManager * manager_
The Camera manager associated with the pipeline handler.
Definition pipeline_handler.h:81
libcamera::PipelineHandler::PipelineHandler
PipelineHandler(CameraManager *manager)
Construct a PipelineHandler instance.
Definition pipeline_handler.cpp:70
libcamera::PipelineHandler::completeRequest
void completeRequest(Request *request)
Signal request completion.
Definition pipeline_handler.cpp:552
libcamera::PipelineHandler::releaseDevice
virtual void releaseDevice(Camera *camera)
Release resources associated with this camera.
Definition pipeline_handler.cpp:254
libcamera::PipelineHandler::queueRequestDevice
virtual int queueRequestDevice(Camera *camera, Request *request)=0
Queue a request to the device.
libcamera::PipelineHandler::acquire
bool acquire(Camera *camera)
Acquire exclusive access to the pipeline handler for the process.
Definition pipeline_handler.cpp:165
libcamera::PipelineHandler::configurationFile
std::string configurationFile(const std::string &subdir, const std::string &name) const
Retrieve the absolute path to a platform configuration file.
Definition pipeline_handler.cpp:589
libcamera::PipelineHandler::acquireDevice
virtual bool acquireDevice(Camera *camera)
Acquire resources associated with this camera.
Definition pipeline_handler.cpp:233
libcamera::PipelineHandler::match
virtual bool match(DeviceEnumerator *enumerator)=0
Match media devices and create camera instances.
libcamera::PipelineHandler::acquireMediaDevice
MediaDevice * acquireMediaDevice(DeviceEnumerator *enumerator, const DeviceMatch &dm)
Search and acquire a MediaDevice matching a device pattern.
Definition pipeline_handler.cpp:130
libcamera::PipelineHandler::registerCamera
void registerCamera(std::shared_ptr< Camera > camera)
Register a camera to the camera manager and pipeline handler.
Definition pipeline_handler.cpp:636
libcamera::PipelineHandler::registerRequest
void registerRequest(Request *request)
Register a request for use by the pipeline handler.
Definition pipeline_handler.cpp:413
libcamera::PipelineHandler::start
virtual int start(Camera *camera, const ControlList *controls)=0
Start capturing from a group of streams.
libcamera::PipelineHandler::queueRequest
void queueRequest(Request *request)
Queue a request.
Definition pipeline_handler.cpp:445
libcamera::PipelineHandler::hasPendingRequests
bool hasPendingRequests(const Camera *camera) const
Determine if the camera has any requests pending.
Definition pipeline_handler.cpp:400
libcamera::PipelineHandler::stop
void stop(Camera *camera)
Stop capturing from all running streams and cancel pending requests.
Definition pipeline_handler.cpp:361
libcamera::PipelineHandler::exportFrameBuffers
virtual int exportFrameBuffers(Camera *camera, Stream *stream, std::vector< std::unique_ptr< FrameBuffer > > *buffers)=0
Allocate and export buffers for stream.
libcamera::PipelineHandler::generateConfiguration
virtual std::unique_ptr< CameraConfiguration > generateConfiguration(Camera *camera, Span< const StreamRole > roles)=0
Generate a camera configuration for a specified camera.
libcamera::PipelineHandler::completeBuffer
bool completeBuffer(Request *request, FrameBuffer *buffer)
Complete a buffer for a request.
Definition pipeline_handler.cpp:531
libcamera::PipelineHandler::configure
virtual int configure(Camera *camera, CameraConfiguration *config)=0
Configure a group of streams for capture.
libcamera::PipelineHandler::release
void release(Camera *camera)
Release exclusive access to the pipeline handler.
Definition pipeline_handler.cpp:203
libcamera::Request
A frame capture request.
Definition request.h:30
libcamera::Stream
Video stream for a camera.
Definition stream.h:74
controls.h
Framework to manage controls related to an object.
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
object.h
Base object to support automatic signal disconnection.
stream.h
Video stream for a Camera.