libcamera  v0.3.0
Supporting cameras in Linux since 2019
Classes | Public Member Functions | Static Public Member Functions | Public Attributes | Friends | List of all members
libcamera::Camera Class Referencefinal

Camera device. More...

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

Classes

class  Private
 Base class for camera private data. More...
 

Public Member Functions

const std::string & id () const
 Retrieve the ID of the camera. More...
 
int acquire ()
 Acquire the camera device for exclusive access. More...
 
int release ()
 Release exclusive access to the camera device. More...
 
const ControlInfoMapcontrols () const
 Retrieve the list of controls supported by the camera. More...
 
const ControlListproperties () const
 Retrieve the list of properties of the camera. More...
 
const std::set< Stream * > & streams () const
 Retrieve all the camera's stream information. More...
 
std::unique_ptr< CameraConfigurationgenerateConfiguration (Span< const StreamRole > roles={})
 Generate a default camera configuration according to stream roles. More...
 
std::unique_ptr< CameraConfigurationgenerateConfiguration (std::initializer_list< StreamRole > roles)
 
int configure (CameraConfiguration *config)
 Configure the camera prior to capture. More...
 
std::unique_ptr< RequestcreateRequest (uint64_t cookie=0)
 Create a request object for the camera. More...
 
int queueRequest (Request *request)
 Queue a request to the camera. More...
 
int start (const ControlList *controls=nullptr)
 Start capture from camera. More...
 
int stop ()
 Stop capture from camera. 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...
 
- Public Member Functions inherited from libcamera::Extensible
 Extensible (std::unique_ptr< Private > d)
 Construct an instance of an Extensible class. More...
 

Static Public Member Functions

static std::shared_ptr< Cameracreate (std::unique_ptr< Private > d, const std::string &id, const std::set< Stream *> &streams)
 Create a camera instance. More...
 

Public Attributes

Signal< Request *, FrameBuffer * > bufferCompleted
 Signal emitted when a buffer for a request queued to the camera has completed.
 
Signal< Request * > requestCompleted
 Signal emitted when a request queued to the camera has completed.
 
Signal disconnected
 Signal emitted when the camera is disconnected from the system. More...
 

Friends

class PipelineHandler
 
class FrameBufferAllocator
 

Additional Inherited Members

- 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 Member Functions inherited from libcamera::Extensible
template<typename T >
const T * _d () const
 Retrieve the private data instance. More...
 
template<typename T >
T * _d ()
 Retrieve the private data instance. More...
 

Detailed Description

Camera device.

Todo:
Add documentation for camera start timings. What exactly does the camera expect the pipeline handler to do when start() is called?

The Camera class models a camera capable of producing one or more image streams from a single image source. It provides the main interface to configuring and controlling the device, and capturing image streams. It is the central object exposed by libcamera.

To support the central nature of Camera objects, libcamera manages the lifetime of camera instances with std::shared_ptr<>. Instances shall be created with the create() function which returns a shared pointer. The Camera constructors and destructor are private, to prevent instances from being constructed and destroyed manually.

Operating the Camera

An application needs to perform a sequence of operations on a camera before it is ready to process requests. The camera needs to be acquired and configured to prepare the camera for capture. Once started the camera can process requests until it is stopped. When an application is done with a camera, the camera needs to be released.

An application may start and stop a camera multiple times as long as it is not released. The camera may also be reconfigured.

Functions that affect the camera state as defined below are generally not synchronized with each other by the Camera class. The caller is responsible for ensuring their synchronization if necessary.

States

To help manage the sequence of operations needed to control the camera a set of states are defined. Each state describes which operations may be performed on the camera. Performing an operation not allowed in the camera state results in undefined behaviour. Operations not listed at all in the state diagram are allowed in all states.

dot_inline_dotgraph_1.png

Available

The base state of a camera, an application can inspect the properties of the camera to determine if it wishes to use it. If an application wishes to use a camera it should acquire() it to proceed to the Acquired state.

Acquired

In the acquired state an application has exclusive access to the camera and may modify the camera's parameters to configure it and proceed to the Configured state.

Configured

The camera is configured and ready to be started. The application may release() the camera and to get back to the Available state or start() it to progress to the Running state.

Stopping

The camera has been asked to stop. Pending requests are being completed or cancelled, and no new requests are permitted to be queued. The camera will transition to the Configured state when all queued requests have been returned to the application.

Running

The camera is running and ready to process requests queued by the application. The camera remains in this state until it is stopped and moved to the Configured state.

Member Function Documentation

◆ acquire()

int libcamera::Camera::acquire ( )

Acquire the camera device for exclusive access.

After opening the device with open(), exclusive access must be obtained before performing operations that change the device state. This function is not blocking, if the device has already been acquired (by the same or another process) the -EBUSY error code is returned.

Acquiring a camera may limit usage of any other camera(s) provided by the same pipeline handler to the same instance of libcamera. The limit is in effect until all cameras from the pipeline handler are released. Other instances of libcamera can still list and examine the cameras but will fail if they attempt to acquire() any of them.

Once exclusive access isn't needed anymore, the device should be released with a call to the release() function.

Thread Safety:
This function is thread-safe. It may only be called when the camera is in the Available state as defined in Operating the Camera.
Returns
0 on success or a negative error code otherwise
Return values
-ENODEVThe camera has been disconnected from the system
-EBUSYThe camera is not free and can't be acquired by the caller

◆ configure()

int libcamera::Camera::configure ( CameraConfiguration config)

Configure the camera prior to capture.

Parameters
[in]configThe camera configurations to setup

Prior to starting capture, the camera must be configured to select a group of streams to be involved in the capture and their configuration. The caller specifies which streams are to be involved and their configuration by populating config.

The configuration is created by generateConfiguration(), and adjusted by the caller with CameraConfiguration::validate(). This function only accepts fully valid configurations and returns an error if config is not valid.

Exclusive access to the camera shall be ensured by a call to acquire() prior to calling this function, otherwise an -EACCES error will be returned.

Thread Safety:
This function may only be called when the camera is in the Acquired or Configured state as defined in Operating the Camera, and shall be synchronized by the caller with other functions that affect the camera state.

Upon return the StreamConfiguration entries in config are associated with Stream instances which can be retrieved with StreamConfiguration::stream().

Returns
0 on success or a negative error code otherwise
Return values
-ENODEVThe camera has been disconnected from the system
-EACCESThe camera is not in a state where it can be configured
-EINVALThe configuration is not valid

◆ controls()

const ControlInfoMap & libcamera::Camera::controls ( ) const

Retrieve the list of controls supported by the camera.

The list of controls supported by the camera and their associated constraints remain constant through the lifetime of the Camera object.

Thread Safety:
This function is thread-safe.
Returns
A ControlInfoMap listing the controls supported by the camera

◆ create()

std::shared_ptr< Camera > libcamera::Camera::create ( std::unique_ptr< Private d,
const std::string &  id,
const std::set< Stream *> &  streams 
)
static

Create a camera instance.

Parameters
[in]dCamera private data
[in]idThe ID of the camera device
[in]streamsArray of streams the camera provides

The caller is responsible for guaranteeing a stable and unique camera ID matching the constraints described by Camera::id(). Parameters that are allocated dynamically at system startup, such as bus numbers that may be enumerated differently, are therefore not suitable to use in the ID.

Pipeline handlers that use a CameraSensor may use the CameraSensor::id() to generate an ID that satisfies the criteria of a stable and unique camera ID.

Returns
A shared pointer to the newly created camera object

◆ createRequest()

std::unique_ptr< Request > libcamera::Camera::createRequest ( uint64_t  cookie = 0)

Create a request object for the camera.

Parameters
[in]cookieOpaque cookie for application use

This function creates an empty request for the application to fill with buffers and parameters, and queue for capture.

The cookie is stored in the request and is accessible through the Request::cookie() function at any time. It is typically used by applications to map the request to an external resource in the request completion handler, and is completely opaque to libcamera.

The ownership of the returned request is passed to the caller, which is responsible for deleting it. The request may be deleted in the completion handler, or reused after resetting its state with Request::reuse().

Thread Safety:
This function is thread-safe. It may only be called when the camera is in the Configured or Running state as defined in Operating the Camera.
Returns
A pointer to the newly created request, or nullptr on error

◆ generateConfiguration() [1/2]

std::unique_ptr< CameraConfiguration > libcamera::Camera::generateConfiguration ( Span< const StreamRole roles = {})

Generate a default camera configuration according to stream roles.

Parameters
[in]rolesA list of stream roles

Generate a camera configuration for a set of desired stream roles. The caller specifies a list of stream roles and the camera returns a configuration containing suitable streams and their suggested default configurations. An empty list of roles is valid, and will generate an empty configuration that can be filled by the caller.

Thread Safety:
This function is thread-safe.
Returns
A CameraConfiguration if the requested roles can be satisfied, or a null pointer otherwise.

◆ generateConfiguration() [2/2]

std::unique_ptr< CameraConfiguration > libcamera::Camera::generateConfiguration ( std::initializer_list< StreamRole roles)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ id()

const std::string & libcamera::Camera::id ( ) const

Retrieve the ID of the camera.

The camera ID is a free-form string that identifies a camera in the system. IDs are guaranteed to be unique and stable: the same camera, when connected to the system in the same way (e.g. in the same USB port), will have the same ID across both unplug/replug and system reboots.

Applications may store the camera ID and use it later to acquire the same camera. They shall treat the ID as an opaque identifier, without interpreting its value.

Camera IDs may change when the system hardware or firmware is modified, for instance when replacing a PCI USB controller or moving it to another PCI slot, or updating the ACPI tables or Device Tree.

Thread Safety:
This function is thread-safe.
Returns
ID of the camera device

◆ properties()

const ControlList & libcamera::Camera::properties ( ) const

Retrieve the list of properties of the camera.

Camera properties are static information that describe the capabilities of the camera. They remain constant through the lifetime of the Camera object.

Returns
A ControlList of properties supported by the camera

◆ queueRequest()

int libcamera::Camera::queueRequest ( Request request)

Queue a request to the camera.

Parameters
[in]requestThe request to queue to the camera

This function queues a request to the camera for capture.

After allocating the request with createRequest(), the application shall fill it with at least one capture buffer before queuing it. Requests that contain no buffers are invalid and are rejected without being queued.

Once the request has been queued, the camera will notify its completion through the requestCompleted signal.

Thread Safety:
This function is thread-safe. It may only be called when the camera is in the Running state as defined in Operating the Camera.
Returns
0 on success or a negative error code otherwise
Return values
-ENODEVThe camera has been disconnected from the system
-EACCESThe camera is not running so requests can't be queued
-EXDEVThe request does not belong to this camera
-EINVALThe request is invalid
-ENOMEMNo buffer memory was available to handle the request

◆ release()

int libcamera::Camera::release ( )

Release exclusive access to the camera device.

Releasing the camera device allows other users to acquire exclusive access with the acquire() function.

Thread Safety:
This function may only be called when the camera is in the Available or Configured state as defined in Operating the Camera, and shall be synchronized by the caller with other functions that affect the camera state.
Returns
0 on success or a negative error code otherwise
Return values
-EBUSYThe camera is running and can't be released

◆ start()

int libcamera::Camera::start ( const ControlList controls = nullptr)

Start capture from camera.

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

Start the camera capture session, optionally providing a list of controls to apply before starting. Once the camera is started the application can queue requests to the camera to process and return to the application until the capture session is terminated with stop().

Thread Safety:
This function may only be called when the camera is in the Configured state as defined in Operating the Camera, and shall be synchronized by the caller with other functions that affect the camera state.
Returns
0 on success or a negative error code otherwise
Return values
-ENODEVThe camera has been disconnected from the system
-EACCESThe camera is not in a state where it can be started

◆ stop()

int libcamera::Camera::stop ( )

Stop capture from camera.

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

Thread Safety:
This function may be called in any camera state as defined in Operating the Camera, and shall be synchronized by the caller with other functions that affect the camera state. If called when the camera isn't running, it is a no-op.
Returns
0 on success or a negative error code otherwise
Return values
-ENODEVThe camera has been disconnected from the system
-EACCESThe camera is not running so can't be stopped

◆ streams()

const std::set< Stream * > & libcamera::Camera::streams ( ) const

Retrieve all the camera's stream information.

Retrieve all of the camera's static stream information. The static information describes among other things how many streams the camera supports and the capabilities of each stream.

Thread Safety:
This function is thread-safe.
Returns
An array of all the camera's streams

Member Data Documentation

◆ disconnected

libcamera::Camera::disconnected

Signal emitted when the camera is disconnected from the system.

This signal is emitted when libcamera detects that the camera has been removed from the system. For hot-pluggable devices this is usually caused by physical device disconnection. The media device is passed as a parameter.

As soon as this signal is emitted the camera instance will refuse all new application API calls by returning errors immediately.


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