libcamera  v0.3.1+12-19bbca3c
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 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 
24 
25 namespace libcamera {
26 
27 class Camera;
28 class CameraConfiguration;
29 class CameraManager;
30 class DeviceEnumerator;
31 class DeviceMatch;
32 class FrameBuffer;
33 class MediaDevice;
34 class PipelineHandler;
35 class Request;
36 
37 class PipelineHandler : public std::enable_shared_from_this<PipelineHandler>,
38  public Object
39 {
40 public:
42  virtual ~PipelineHandler();
43 
44  virtual bool match(DeviceEnumerator *enumerator) = 0;
46  const DeviceMatch &dm);
47 
48  bool acquire();
49  void release(Camera *camera);
50 
51  virtual std::unique_ptr<CameraConfiguration> generateConfiguration(Camera *camera,
52  Span<const StreamRole> 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  std::string configurationFile(const std::string &subdir,
69  const std::string &name) const;
70 
71  const char *name() const { return name_; }
72 
73 protected:
74  void registerCamera(std::shared_ptr<Camera> camera);
75  void hotplugMediaDevice(MediaDevice *media);
76 
77  virtual int queueRequestDevice(Camera *camera, Request *request) = 0;
78  virtual void stopDevice(Camera *camera) = 0;
79 
80  virtual void releaseDevice(Camera *camera);
81 
83 
84 private:
85  void unlockMediaDevices();
86 
87  void mediaDeviceDisconnected(MediaDevice *media);
88  virtual void disconnect();
89 
90  void doQueueRequest(Request *request);
91  void doQueueRequests();
92 
93  std::vector<std::shared_ptr<MediaDevice>> mediaDevices_;
94  std::vector<std::weak_ptr<Camera>> cameras_;
95 
96  std::queue<Request *> waitingRequests_;
97 
98  const char *name_;
99 
100  Mutex lock_;
101  unsigned int useCount_ LIBCAMERA_TSA_GUARDED_BY(lock_);
102 
103  friend class PipelineHandlerFactoryBase;
104 };
105 
107 {
108 public:
109  PipelineHandlerFactoryBase(const char *name);
110  virtual ~PipelineHandlerFactoryBase() = default;
111 
112  std::shared_ptr<PipelineHandler> create(CameraManager *manager) const;
113 
114  const std::string &name() const { return name_; }
115 
116  static std::vector<PipelineHandlerFactoryBase *> &factories();
117  static const PipelineHandlerFactoryBase *getFactoryByName(const std::string &name);
118 
119 private:
120  static void registerType(PipelineHandlerFactoryBase *factory);
121 
122  virtual std::unique_ptr<PipelineHandler>
123  createInstance(CameraManager *manager) const = 0;
124 
125  std::string name_;
126 };
127 
128 template<typename _PipelineHandler>
130 {
131 public:
134  {
135  }
136 
137  std::unique_ptr<PipelineHandler>
138  createInstance(CameraManager *manager) const override
139  {
140  return std::make_unique<_PipelineHandler>(manager);
141  }
142 };
143 
144 #define REGISTER_PIPELINE_HANDLER(handler, name) \
145  static PipelineHandlerFactory<handler> global_##handler##Factory(name);
146 
147 } /* namespace libcamera */
virtual int queueRequestDevice(Camera *camera, Request *request)=0
Queue a request to the device.
The MediaDevice represents a Media Controller device with its full graph of connected objects...
Definition: media_device.h:25
std::mutex wrapper with clang thread safety annotation
Definition: mutex.h:121
std::unique_ptr< PipelineHandler > createInstance(CameraManager *manager) const override
Create an instance of the PipelineHandler corresponding to the factory.
Definition: pipeline_handler.h:138
const std::string & name() const
Retrieve the factory name.
Definition: pipeline_handler.h:114
bool acquire()
Acquire exclusive access to the pipeline handler for the process.
Definition: pipeline_handler.cpp:166
void queueRequest(Request *request)
Queue a request.
Definition: pipeline_handler.cpp:413
bool hasPendingRequests(const Camera *camera) const
Determine if the camera has any requests pending.
Definition: pipeline_handler.cpp:368
Video stream for a camera.
Definition: stream.h:74
PipelineHandlerFactory(const char *name)
Construct a pipeline handler factory.
Definition: pipeline_handler.h:132
Top-level libcamera namespace.
Definition: backtrace.h:17
CameraManager * manager_
The Camera manager associated with the pipeline handler.
Definition: pipeline_handler.h:82
virtual void stopDevice(Camera *camera)=0
Stop capturing from all running streams.
PipelineHandler(CameraManager *manager)
Construct a PipelineHandler instance.
Definition: pipeline_handler.cpp:71
void registerRequest(Request *request)
Register a request for use by the pipeline handler.
Definition: pipeline_handler.cpp:381
Frame buffer data and its associated dynamic metadata.
Definition: framebuffer.h:49
bool completeBuffer(Request *request, FrameBuffer *buffer)
Complete a buffer for a request.
Definition: pipeline_handler.cpp:499
Video stream for a Camera.
Hold configuration for streams of the camera.
Definition: camera.h:59
IPA Proxy.
void hotplugMediaDevice(MediaDevice *media)
Enable hotplug handling for a media device.
Definition: pipeline_handler.cpp:650
virtual int start(Camera *camera, const ControlList *controls)=0
Start capturing from a group of streams.
A frame capture request.
Definition: request.h:30
void release(Camera *camera)
Release exclusive access to the pipeline handler.
Definition: pipeline_handler.cpp:202
Create and manage cameras based on a set of media devices.
Definition: pipeline_handler.h:37
Camera device.
Definition: camera.h:113
const char * name() const
Retrieve the pipeline handler name.
Definition: pipeline_handler.h:71
virtual void releaseDevice(Camera *camera)
Release resources associated with this camera.
Definition: pipeline_handler.cpp:222
Base class for pipeline handler factories.
Definition: pipeline_handler.h:106
Description of a media device search pattern.
Definition: device_enumerator.h:20
Mutex classes with clang thread safety annotation.
MediaDevice * acquireMediaDevice(DeviceEnumerator *enumerator, const DeviceMatch &dm)
Search and acquire a MediaDevice matching a device pattern.
Definition: pipeline_handler.cpp:131
void stop(Camera *camera)
Stop capturing from all running streams and cancel pending requests.
Definition: pipeline_handler.cpp:329
Provide access and manage all cameras in the system.
Definition: camera_manager.h:23
Enumerate, store and search media devices.
Definition: device_enumerator.h:34
void registerCamera(std::shared_ptr< Camera > camera)
Register a camera to the camera manager and pipeline handler.
Definition: pipeline_handler.cpp:604
Base object to support automatic signal disconnection.
const ControlIdMap controls
List of all supported libcamera controls.
Definition: controls_ids.cpp:1645
virtual int exportFrameBuffers(Camera *camera, Stream *stream, std::vector< std::unique_ptr< FrameBuffer >> *buffers)=0
Allocate and export buffers for stream.
void completeRequest(Request *request)
Signal request completion.
Definition: pipeline_handler.cpp:520
Associate a list of ControlId with their values for an object.
Definition: controls.h:349
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:557
Framework to manage controls related to an object.
virtual int configure(Camera *camera, CameraConfiguration *config)=0
Configure a group of streams for capture.
Base object to support automatic signal disconnection.
Definition: object.h:24
virtual std::unique_ptr< CameraConfiguration > generateConfiguration(Camera *camera, Span< const StreamRole > roles)=0
Generate a camera configuration for a specified camera.
Registration of PipelineHandler classes and creation of instances.
Definition: pipeline_handler.h:129
virtual bool match(DeviceEnumerator *enumerator)=0
Match media devices and create camera instances.