libcamera: include/libcamera/internal/pipeline_handler.h Source File

libcamera v0.0.1 Supporting cameras in Linux since 2019

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.h - Pipeline handler infrastructure
6 */
7
8#pragma once
9
10#include <memory>
11#include <queue>
12#include <set>
13#include <string>
14#include <sys/types.h>
15#include <vector>
16
17#include <libcamera/base/mutex.h>
18#include <libcamera/base/object.h>
19
20#include <libcamera/controls.h>
21#include <libcamera/stream.h>
22
23#include "libcamera/internal/ipa_proxy.h"
24
25namespace libcamera {
26
27class Camera;
28class CameraConfiguration;
29class CameraManager;
30class DeviceEnumerator;
31class DeviceMatch;
32class FrameBuffer;
33class MediaDevice;
34class PipelineHandler;
35class Request;
36
37 class PipelineHandler : public std::enable_shared_from_this<PipelineHandler>,
38 public Object
39{
40public:
41 PipelineHandler(CameraManager *manager);
42 virtual ~PipelineHandler();
43
44 virtual bool match(DeviceEnumerator *enumerator) = 0;
45 MediaDevice *acquireMediaDevice(DeviceEnumerator *enumerator,
46 const DeviceMatch &dm);
47
48 bool acquire();
49 void release();
50
51 virtual CameraConfiguration *generateConfiguration(Camera *camera,
52 const StreamRoles &roles) = 0;
53 virtual int configure(Camera *camera, CameraConfiguration *config) = 0;
54
55 virtual int exportFrameBuffers(Camera *camera, Stream *stream,
56 std::vector<std::unique_ptr<FrameBuffer>> *buffers) = 0;
57
58 virtual int start(Camera *camera, const ControlList *controls) = 0;
59 void stop(Camera *camera);
60 bool hasPendingRequests(const Camera *camera) const;
61
62 void registerRequest(Request *request);
63 void queueRequest(Request *request);
64
65 bool completeBuffer(Request *request, FrameBuffer *buffer);
66 void completeRequest(Request *request);
67
68 const char *name() const { return name_; }
69
70protected:
71 void registerCamera(std::shared_ptr<Camera> camera);
72 void hotplugMediaDevice(MediaDevice *media);
73
74 virtual int queueRequestDevice(Camera *camera, Request *request) = 0;
75 virtual void stopDevice(Camera *camera) = 0;
76
77 CameraManager *manager_;
78
79private:
80 void unlockMediaDevices();
81
82 void mediaDeviceDisconnected(MediaDevice *media);
83 virtual void disconnect();
84
85 void doQueueRequest(Request *request);
86 void doQueueRequests();
87
88 std::vector<std::shared_ptr<MediaDevice>> mediaDevices_;
89 std::vector<std::weak_ptr<Camera>> cameras_;
90
91 std::queue<Request *> waitingRequests_;
92
93 const char *name_;
94
95 Mutex lock_;
96 unsigned int useCount_ LIBCAMERA_TSA_GUARDED_BY(lock_);
97
98 friend class PipelineHandlerFactoryBase;
99};
100
101 class PipelineHandlerFactoryBase
102{
103public:
104 PipelineHandlerFactoryBase(const char *name);
105 virtual ~PipelineHandlerFactoryBase() = default;
106
107 std::shared_ptr<PipelineHandler> create(CameraManager *manager) const;
108
109 const std::string &name() const { return name_; }
110
111 static std::vector<PipelineHandlerFactoryBase *> &factories();
112
113private:
114 static void registerType(PipelineHandlerFactoryBase *factory);
115
116 virtual std::unique_ptr<PipelineHandler>
117 createInstance(CameraManager *manager) const = 0;
118
119 std::string name_;
120};
121
122template<typename _PipelineHandler>
123 class PipelineHandlerFactory final : public PipelineHandlerFactoryBase
124{
125public:
126 PipelineHandlerFactory(const char *name)
127 : PipelineHandlerFactoryBase(name)
128 {
129 }
130
131 std::unique_ptr<PipelineHandler>
132 createInstance(CameraManager *manager) const override
133 {
134 return std::make_unique<_PipelineHandler>(manager);
135 }
136};
137
138#define REGISTER_PIPELINE_HANDLER(handler) \
139static PipelineHandlerFactory<handler> global_##handler##Factory(#handler);
140
141} /* namespace libcamera */
libcamera::CameraConfiguration
Hold configuration for streams of the camera.
Definition: camera.h:33
libcamera::CameraManager
Provide access and manage all cameras in the system.
Definition: camera_manager.h:24
libcamera::Camera
Camera device.
Definition: camera.h:87
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:50
libcamera::MediaDevice
The MediaDevice represents a Media Controller device with its full graph of connected objects.
Definition: media_device.h:26
libcamera::Mutex
std::mutex wrapper with clang thread safety annotation
Definition: mutex.h:122
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:102
libcamera::PipelineHandlerFactoryBase::name
const std::string & name() const
Retrieve the factory name.
Definition: pipeline_handler.h:109
libcamera::PipelineHandlerFactoryBase::PipelineHandlerFactoryBase
PipelineHandlerFactoryBase(const char *name)
Construct a pipeline handler factory base.
Definition: pipeline_handler.cpp:663
libcamera::PipelineHandlerFactoryBase::factories
static std::vector< PipelineHandlerFactoryBase * > & factories()
Retrieve the list of all pipeline handler factories.
Definition: pipeline_handler.cpp:708
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:676
libcamera::PipelineHandlerFactory
Registration of PipelineHandler classes and creation of instances.
Definition: pipeline_handler.h:124
libcamera::PipelineHandlerFactory::PipelineHandlerFactory
PipelineHandlerFactory(const char *name)
Construct a pipeline handler factory.
Definition: pipeline_handler.h:126
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:132
libcamera::PipelineHandler
Create and manage cameras based on a set of media devices.
Definition: pipeline_handler.h:39
libcamera::PipelineHandler::queueRequestDevice
virtual int queueRequestDevice(Camera *camera, Request *request)=0
Queue a request to the device.
libcamera::PipelineHandler::acquire
bool acquire()
Acquire exclusive access to the pipeline handler for the process.
Definition: pipeline_handler.cpp:164
libcamera::PipelineHandler::hotplugMediaDevice
void hotplugMediaDevice(MediaDevice *media)
Enable hotplug handling for a media device.
Definition: pipeline_handler.cpp:572
libcamera::PipelineHandler::stopDevice
virtual void stopDevice(Camera *camera)=0
Stop capturing from all running streams.
libcamera::PipelineHandler::PipelineHandler
PipelineHandler(CameraManager *manager)
Construct a PipelineHandler instance.
Definition: pipeline_handler.cpp:69
libcamera::PipelineHandler::completeRequest
void completeRequest(Request *request)
Signal request completion.
Definition: pipeline_handler.cpp:505
libcamera::PipelineHandler::name
const char * name() const
Retrieve the pipeline handler name.
Definition: pipeline_handler.h:68
libcamera::PipelineHandler::start
virtual int start(Camera *camera, const ControlList *controls)=0
Start capturing from a group of streams.
libcamera::PipelineHandler::acquireMediaDevice
MediaDevice * acquireMediaDevice(DeviceEnumerator *enumerator, const DeviceMatch &dm)
Search and acquire a MediaDevice matching a device pattern.
Definition: pipeline_handler.cpp:129
libcamera::PipelineHandler::generateConfiguration
virtual CameraConfiguration * generateConfiguration(Camera *camera, const StreamRoles &roles)=0
Generate a camera configuration for a specified camera.
libcamera::PipelineHandler::match
virtual bool match(DeviceEnumerator *enumerator)=0
Match media devices and create camera instances.
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::configure
virtual int configure(Camera *camera, CameraConfiguration *config)=0
Configure a group of streams for capture.
libcamera::PipelineHandler::registerCamera
void registerCamera(std::shared_ptr< Camera > camera)
Register a camera to the camera manager and pipeline handler.
Definition: pipeline_handler.cpp:533
libcamera::PipelineHandler::registerRequest
void registerRequest(Request *request)
Register a request for use by the pipeline handler.
Definition: pipeline_handler.cpp:366
libcamera::PipelineHandler::queueRequest
void queueRequest(Request *request)
Queue a request.
Definition: pipeline_handler.cpp:398
libcamera::PipelineHandler::hasPendingRequests
bool hasPendingRequests(const Camera *camera) const
Determine if the camera has any requests pending.
Definition: pipeline_handler.cpp:353
libcamera::PipelineHandler::release
void release()
Release exclusive access to the pipeline handler.
Definition: pipeline_handler.cpp:199
libcamera::PipelineHandler::stop
void stop(Camera *camera)
Stop capturing from all running streams and cancel pending requests.
Definition: pipeline_handler.cpp:314
libcamera::PipelineHandler::completeBuffer
bool completeBuffer(Request *request, FrameBuffer *buffer)
Complete a buffer for a request.
Definition: pipeline_handler.cpp:484
libcamera::PipelineHandler::manager_
CameraManager * manager_
The Camera manager associated with the pipeline handler.
Definition: pipeline_handler.h:77
libcamera::Request
A frame capture request.
Definition: request.h:31
libcamera::Stream
Video stream for a camera.
Definition: stream.h:74
controls.h
Framework to manage controls related to an object.
ipa_proxy.h
IPA Proxy.
mutex.h
Mutex classes with clang thread safety annotation.
libcamera::controls::controls
const ControlIdMap controls
List of all supported libcamera controls.
Definition: control_ids.cpp:1301
libcamera
Top-level libcamera namespace.
Definition: backtrace.h:17
libcamera::StreamRoles
std::vector< StreamRole > StreamRoles
A vector of StreamRole.
Definition: stream.h:71
object.h
Base object to support automatic signal disconnection.
stream.h
Video stream for a Camera.