libcamera  v0.3.0
Supporting cameras in Linux since 2019
Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
libcamera::PipelineHandler Class Referenceabstract

Create and manage cameras based on a set of media devices. More...

Inheritance diagram for libcamera::PipelineHandler:
Inheritance graph
[legend]
Collaboration diagram for libcamera::PipelineHandler:
Collaboration graph
[legend]

Public Member Functions

 PipelineHandler (CameraManager *manager)
 Construct a PipelineHandler instance. More...
 
virtual bool match (DeviceEnumerator *enumerator)=0
 Match media devices and create camera instances. More...
 
MediaDeviceacquireMediaDevice (DeviceEnumerator *enumerator, const DeviceMatch &dm)
 Search and acquire a MediaDevice matching a device pattern. More...
 
bool acquire ()
 Acquire exclusive access to the pipeline handler for the process. More...
 
void release (Camera *camera)
 Release exclusive access to the pipeline handler. More...
 
virtual std::unique_ptr< CameraConfigurationgenerateConfiguration (Camera *camera, Span< const StreamRole > roles)=0
 Generate a camera configuration for a specified camera. More...
 
virtual int configure (Camera *camera, CameraConfiguration *config)=0
 Configure a group of streams for capture. More...
 
virtual int exportFrameBuffers (Camera *camera, Stream *stream, std::vector< std::unique_ptr< FrameBuffer >> *buffers)=0
 Allocate and export buffers for stream. More...
 
virtual int start (Camera *camera, const ControlList *controls)=0
 Start capturing from a group of streams. More...
 
void stop (Camera *camera)
 Stop capturing from all running streams and cancel pending requests. More...
 
bool hasPendingRequests (const Camera *camera) const
 Determine if the camera has any requests pending. More...
 
void registerRequest (Request *request)
 Register a request for use by the pipeline handler. More...
 
void queueRequest (Request *request)
 Queue a request. More...
 
bool completeBuffer (Request *request, FrameBuffer *buffer)
 Complete a buffer for a request. More...
 
void completeRequest (Request *request)
 Signal request completion. More...
 
std::string configurationFile (const std::string &subdir, const std::string &name) const
 Retrieve the absolute path to a platform configuration file. More...
 
const char * name () const
 Retrieve the pipeline handler name. More...
 
- Public Member Functions inherited from libcamera::Object
 Object (Object *parent=nullptr)
 Construct an Object instance. More...
 
virtual ~Object ()
 Destroy an Object instance. More...
 
void deleteLater ()
 Schedule deletion of the instance in the thread it belongs to. More...
 
void postMessage (std::unique_ptr< Message > msg)
 Post a message to the object's thread. More...
 
template<typename T , typename R , typename... FuncArgs, typename... Args, std::enable_if_t< std::is_base_of< Object, T >::value > * = nullptr>
invokeMethod (R(T::*func)(FuncArgs...), ConnectionType type, Args &&... args)
 Invoke a method asynchronously on an Object instance. More...
 
Threadthread () const
 Retrieve the thread the object is bound to. More...
 
void moveToThread (Thread *thread)
 Move the object and all its children to a different thread. More...
 
Objectparent () const
 Retrieve the object's parent. More...
 

Protected Member Functions

void registerCamera (std::shared_ptr< Camera > camera)
 Register a camera to the camera manager and pipeline handler. More...
 
void hotplugMediaDevice (MediaDevice *media)
 Enable hotplug handling for a media device. More...
 
virtual int queueRequestDevice (Camera *camera, Request *request)=0
 Queue a request to the device. More...
 
virtual void stopDevice (Camera *camera)=0
 Stop capturing from all running streams. More...
 
virtual void releaseDevice (Camera *camera)
 Release resources associated with this camera. More...
 
- Protected Member Functions inherited from libcamera::Object
virtual void message (Message *msg)
 Message handler for the object. More...
 
bool assertThreadBound (const char *message)
 Check if the caller complies with thread-bound constraints. More...
 

Protected Attributes

CameraManagermanager_
 The Camera manager associated with the pipeline handler. More...
 

Friends

class PipelineHandlerFactoryBase
 

Detailed Description

Create and manage cameras based on a set of media devices.

The PipelineHandler matches the media devices provided by a DeviceEnumerator with the pipelines it supports and creates corresponding Camera devices.

Pipeline handler instances are reference-counted through std::shared_ptr<>. They implement std::enable_shared_from_this<> in order to create new std::shared_ptr<> in code paths originating from member functions of the PipelineHandler class where only the 'this' pointer is available.

Constructor & Destructor Documentation

◆ PipelineHandler()

libcamera::PipelineHandler::PipelineHandler ( CameraManager manager)

Construct a PipelineHandler instance.

Parameters
[in]managerThe camera manager

In order to honour the std::enable_shared_from_this<> contract, PipelineHandler instances shall never be constructed manually, but always through the PipelineHandlerFactoryBase::create() function.

Member Function Documentation

◆ acquire()

bool libcamera::PipelineHandler::acquire ( )

Acquire exclusive access to the pipeline handler for the process.

This function locks all the media devices used by the pipeline to ensure that no other process can access them concurrently.

Access to a pipeline handler may be acquired recursively from within the same process. Every successful acquire() call shall be matched with a release() call. This allows concurrent access to the same pipeline handler from different cameras within the same process.

Pipeline handlers shall not call this function directly as the Camera class handles access internally.

Thread Safety:
This function is thread-safe.
Returns
True if the pipeline handler was acquired, false if another process has already acquired it
See also
release()

◆ acquireMediaDevice()

MediaDevice * libcamera::PipelineHandler::acquireMediaDevice ( DeviceEnumerator enumerator,
const DeviceMatch dm 
)

Search and acquire a MediaDevice matching a device pattern.

Parameters
[in]enumeratorEnumerator containing all media devices in the system
[in]dmDevice match pattern

Search the device enumerator for an available media device matching the device match pattern dm. Matching media device that have previously been acquired by MediaDevice::acquire() are not considered. If a match is found, the media device is acquired and returned. The caller shall not release the device explicitly, it will be automatically released when the pipeline handler is destroyed.

Thread Safety:
This function shall be called from the CameraManager thread.
Returns
A pointer to the matching MediaDevice, or nullptr if no match is found

◆ completeBuffer()

bool libcamera::PipelineHandler::completeBuffer ( Request request,
FrameBuffer buffer 
)

Complete a buffer for a request.

Parameters
[in]requestThe request the buffer belongs to
[in]bufferThe buffer that has completed

This function shall be called by pipeline handlers to signal completion of the buffer part of the request. It notifies applications of buffer completion and updates the request's internal buffer tracking. The request is not completed automatically when the last buffer completes to give pipeline handlers a chance to perform any operation that may still be needed. They shall complete requests explicitly with completeRequest().

Thread Safety:
This function shall be called from the CameraManager thread.
Returns
True if all buffers contained in the request have completed, false otherwise

◆ completeRequest()

void libcamera::PipelineHandler::completeRequest ( Request request)

Signal request completion.

Parameters
[in]requestThe request that has completed

The pipeline handler shall call this function to notify the camera that the request has completed. The request is no longer managed by the pipeline handler and shall not be accessed once this function returns.

This function ensures that requests will be returned to the application in submission order, the pipeline handler may call it on any complete request without any ordering constraint.

Thread Safety:
This function shall be called from the CameraManager thread.

◆ configurationFile()

std::string libcamera::PipelineHandler::configurationFile ( const std::string &  subdir,
const std::string &  name 
) const

Retrieve the absolute path to a platform configuration file.

Parameters
[in]subdirThe pipeline handler specific subdirectory name
[in]nameThe configuration file name

This function locates a named platform configuration file and returns its absolute path to the pipeline handler. It searches the following directories, in order:

  • If libcamera is not installed, the src/libcamera/pipeline/<subdir>/data/ directory within the source tree ; otherwise
  • The system data (share/libcamera/pipeline/<subdir>) directory.

The system directories are not searched if libcamera is not installed.

Returns
The full path to the pipeline handler configuration file, or an empty string if no configuration file can be found

◆ configure()

libcamera::PipelineHandler::configure ( Camera camera,
CameraConfiguration config 
)
pure virtual

Configure a group of streams for capture.

Parameters
[in]cameraThe camera to configure
[in]configThe camera configurations to setup

Configure the specified group of streams for camera according to the configuration specified in config. The intended caller of this interface is the Camera class which will receive configuration to apply from the application.

The configuration is guaranteed to have been validated with CameraConfiguration::validate(). The pipeline handler implementation shall not perform further validation and may rely on any custom field stored in its custom CameraConfiguration derived class.

When configuring the camera the pipeline handler shall associate a Stream instance to each StreamConfiguration entry in the CameraConfiguration using the StreamConfiguration::setStream() function.

Thread Safety:
This function is called from the CameraManager thread.
Returns
0 on success or a negative error code otherwise

◆ exportFrameBuffers()

libcamera::PipelineHandler::exportFrameBuffers ( Camera camera,
Stream stream,
std::vector< std::unique_ptr< FrameBuffer >> *  buffers 
)
pure virtual

Allocate and export buffers for stream.

Parameters
[in]cameraThe camera
[in]streamThe stream to allocate buffers for
[out]buffersArray of buffers successfully allocated

This function allocates buffers for the stream from the devices associated with the stream in the corresponding pipeline handler. Those buffers shall be suitable to be added to a Request for the stream, and shall be mappable to the CPU through their associated dmabufs with mmap().

The function may only be called after the Camera has been configured and before it gets started, or after it gets stopped. It shall be called only for streams that are part of the active camera configuration.

The only intended caller is Camera::exportFrameBuffers().

Thread Safety:
This function is called from the CameraManager thread.
Returns
The number of allocated buffers on success or a negative error code otherwise

◆ generateConfiguration()

libcamera::PipelineHandler::generateConfiguration ( Camera camera,
Span< const StreamRole roles 
)
pure virtual

Generate a camera configuration for a specified camera.

Parameters
[in]cameraThe camera to generate a default configuration for
[in]rolesA list of stream roles

Generate a default configuration for the camera for a specified list of stream roles. The caller shall populate the roles with the use-cases it wishes to fetch the default configuration for. The returned configuration can then be examined by the caller to learn about the selected streams and their default parameters.

The intended companion to this is configure() which can be used to change the group of streams parameters.

Thread Safety:
This function may be called from any thread and shall be thread-safe. It shall not modify the state of the camera in the pipeline handler.
Returns
A valid CameraConfiguration if the requested roles can be satisfied, or a null pointer otherwise.

◆ hasPendingRequests()

bool libcamera::PipelineHandler::hasPendingRequests ( const Camera camera) const

Determine if the camera has any requests pending.

Parameters
[in]cameraThe camera to check

This function determines if there are any requests queued to the pipeline awaiting processing.

Returns
True if there are pending requests, or false otherwise

◆ hotplugMediaDevice()

void libcamera::PipelineHandler::hotplugMediaDevice ( MediaDevice media)
protected

Enable hotplug handling for a media device.

Parameters
[in]mediaThe media device

This function enables hotplug handling, and especially hot-unplug handling, of the media device. It shall be called by pipeline handlers for all the media devices that can be disconnected.

When a media device passed to this function is later unplugged, the pipeline handler gets notified and automatically disconnects all the cameras it has registered without requiring any manual intervention.

◆ match()

libcamera::PipelineHandler::match ( DeviceEnumerator enumerator)
pure virtual

Match media devices and create camera instances.

Parameters
[in]enumeratorThe enumerator providing all media devices found in the system

This function is the main entry point of the pipeline handler. It is called by the camera manager with the enumerator passed as an argument. It shall acquire from the enumerator all the media devices it needs for a single pipeline, create one or multiple Camera instances and register them with the camera manager.

If all media devices needed by the pipeline handler are found, they must all be acquired by a call to MediaDevice::acquire(). This function shall then create the corresponding Camera instances, store them internally, and return true. Otherwise it shall not acquire any media device (or shall release all the media devices is has acquired by calling MediaDevice::release()) and return false.

If multiple instances of a pipeline are available in the system, the PipelineHandler class will be instantiated once per instance, and its match() function called for every instance. Each call shall acquire media devices for one pipeline instance, until all compatible media devices are exhausted.

If this function returns true, a new instance of the pipeline handler will be created and its match() function called.

Thread Safety:
This function is called from the CameraManager thread.
Returns
true if media devices have been acquired and camera instances created, or false otherwise

◆ name()

libcamera::PipelineHandler::name ( ) const
inline

Retrieve the pipeline handler name.

Thread Safety:
This function shall be thread-safe.
Returns
The pipeline handler name

◆ queueRequest()

void libcamera::PipelineHandler::queueRequest ( Request request)

Queue a request.

Parameters
[in]requestThe request to queue

This function queues a capture request to the pipeline handler for processing. The request is first added to the internal list of waiting requests which have to be prepared to make sure they are ready for being queued to the pipeline handler.

The queue of waiting requests is iterated and all prepared requests are passed to the pipeline handler in the same order they have been queued by calling this function.

If a Request fails during the preparation phase or if the pipeline handler fails in queuing the request to the hardware the request is cancelled.

Keeping track of queued requests ensures automatic completion of all requests when the pipeline handler is stopped with stop(). Request completion shall be signalled by the pipeline handler using the completeRequest() function.

Thread Safety:
This function is called from the CameraManager thread.

◆ queueRequestDevice()

libcamera::PipelineHandler::queueRequestDevice ( Camera camera,
Request request 
)
protectedpure virtual

Queue a request to the device.

Parameters
[in]cameraThe camera to queue the request to
[in]requestThe request to queue

This function queues a capture request to the device for processing. The request contains a set of buffers associated with streams and a set of parameters. The pipeline handler shall program the device to ensure that the parameters will be applied to the frames captured in the buffers provided in the request.

Thread Safety:
This function is called from the CameraManager thread.
Returns
0 on success or a negative error code otherwise

◆ registerCamera()

void libcamera::PipelineHandler::registerCamera ( std::shared_ptr< Camera camera)
protected

Register a camera to the camera manager and pipeline handler.

Parameters
[in]cameraThe camera to be added

This function is called by pipeline handlers to register the cameras they handle with the camera manager.

Thread Safety:
This function shall be called from the CameraManager thread.

◆ registerRequest()

void libcamera::PipelineHandler::registerRequest ( Request request)

Register a request for use by the pipeline handler.

Parameters
[in]requestThe request to register

This function is called when the request is created, and allows the pipeline handler to perform any one-time initialization it requries for the request.

◆ release()

void libcamera::PipelineHandler::release ( Camera camera)

Release exclusive access to the pipeline handler.

Parameters
[in]cameraThe camera for which to release data

This function releases access to the pipeline handler previously acquired by a call to acquire(). Every release() call shall match a previous successful acquire() call. Calling this function on a pipeline handler that hasn't been acquired results in undefined behaviour.

Pipeline handlers shall not call this function directly as the Camera class handles access internally.

Thread Safety:
This function is thread-safe.
See also
acquire()

◆ releaseDevice()

void libcamera::PipelineHandler::releaseDevice ( Camera camera)
protectedvirtual

Release resources associated with this camera.

Parameters
[in]cameraThe camera for which to release resources

Pipeline handlers may override this in order to perform cleanup operations when a camera is released, such as freeing memory.

◆ start()

libcamera::PipelineHandler::start ( Camera camera,
const ControlList controls 
)
pure virtual

Start capturing from a group of streams.

Parameters
[in]cameraThe camera to start
[in]controlsControls to be applied before starting the Camera

Start the group of streams that have been configured for capture by configure(). The intended caller of this function is the Camera class which will in turn be called from the application to indicate that it has configured the streams and is ready to capture.

Thread Safety:
This function is called from the CameraManager thread.
Returns
0 on success or a negative error code otherwise

◆ stop()

void libcamera::PipelineHandler::stop ( Camera camera)

Stop capturing from all running streams and cancel pending requests.

Parameters
[in]cameraThe camera to stop

This function stops capturing and processing requests immediately. All pending requests are cancelled and complete immediately in an error state.

Thread Safety:
This function is called from the CameraManager thread.

◆ stopDevice()

libcamera::PipelineHandler::stopDevice ( Camera camera)
protectedpure virtual

Stop capturing from all running streams.

Parameters
[in]cameraThe camera to stop

This function stops capturing and processing requests immediately. All pending requests are cancelled and complete immediately in an error state.

Member Data Documentation

◆ manager_

libcamera::PipelineHandler::manager_
protected

The Camera manager associated with the pipeline handler.

The camera manager pointer is stored in the pipeline handler for the convenience of pipeline handler implementations. It remains valid and constant for the whole lifetime of the pipeline handler.


The documentation for this class was generated from the following files: