summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2019-07-10 14:47:30 +0300
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2019-07-11 11:57:37 +0300
commitcc3ae13d9edf36473fb0c4c78b9490c355ce0096 (patch)
tree5216f22dd8eda2975bc41fc6ea9d504892a62f3f /include
parent01b930964acdd9475d46044c459396f8c3cf8a79 (diff)
libcamera: signal: Support cross-thread signals
Allow signals to cross thread boundaries by posting them to the recipient through messages instead of calling the slot directly when the recipient lives in a different thread. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Diffstat (limited to 'include')
-rw-r--r--include/libcamera/signal.h67
1 files changed, 59 insertions, 8 deletions
diff --git a/include/libcamera/signal.h b/include/libcamera/signal.h
index c8f3243e..11ffb857 100644
--- a/include/libcamera/signal.h
+++ b/include/libcamera/signal.h
@@ -8,6 +8,8 @@
#define __LIBCAMERA_SIGNAL_H__
#include <list>
+#include <tuple>
+#include <type_traits>
#include <vector>
#include <libcamera/object.h>
@@ -27,6 +29,9 @@ public:
void *obj() { return obj_; }
bool isObject() const { return isObject_; }
+ void activatePack(void *pack);
+ virtual void invokePack(void *pack) = 0;
+
protected:
void *obj_;
bool isObject_;
@@ -35,24 +40,70 @@ protected:
template<typename... Args>
class SlotArgs : public SlotBase
{
+private:
+#ifndef __DOXYGEN__
+ /*
+ * This is a cheap partial implementation of std::integer_sequence<>
+ * from C++14.
+ */
+ template<int...>
+ struct sequence {
+ };
+
+ template<int N, int... S>
+ struct generator : generator<N-1, N-1, S...> {
+ };
+
+ template<int... S>
+ struct generator<0, S...> {
+ typedef sequence<S...> type;
+ };
+#endif
+
+ using PackType = std::tuple<typename std::remove_reference<Args>::type...>;
+
+ template<int... S>
+ void invokePack(void *pack, sequence<S...>)
+ {
+ PackType *args = static_cast<PackType *>(pack);
+ invoke(std::get<S>(*args)...);
+ delete args;
+ }
+
public:
SlotArgs(void *obj, bool isObject)
: SlotBase(obj, isObject) {}
- virtual void invoke(Args... args) = 0;
+ void invokePack(void *pack) override
+ {
+ invokePack(pack, typename generator<sizeof...(Args)>::type());
+ }
-protected:
- friend class Signal<Args...>;
+ virtual void activate(Args... args) = 0;
+ virtual void invoke(Args... args) = 0;
};
template<typename T, typename... Args>
class SlotMember : public SlotArgs<Args...>
{
public:
+ using PackType = std::tuple<typename std::remove_reference<Args>::type...>;
+
SlotMember(T *obj, bool isObject, void (T::*func)(Args...))
: SlotArgs<Args...>(obj, isObject), func_(func) {}
- void invoke(Args... args) { (static_cast<T *>(this->obj_)->*func_)(args...); }
+ void activate(Args... args)
+ {
+ if (this->isObject_)
+ SlotBase::activatePack(new PackType{ args... });
+ else
+ (static_cast<T *>(this->obj_)->*func_)(args...);
+ }
+
+ void invoke(Args... args)
+ {
+ (static_cast<T *>(this->obj_)->*func_)(args...);
+ }
private:
friend class Signal<Args...>;
@@ -66,7 +117,8 @@ public:
SlotStatic(void (*func)(Args...))
: SlotArgs<Args...>(nullptr, false), func_(func) {}
- void invoke(Args... args) { (*func_)(args...); }
+ void activate(Args... args) { (*func_)(args...); }
+ void invoke(Args... args) {}
private:
friend class Signal<Args...>;
@@ -186,9 +238,8 @@ public:
* disconnect operation, invalidating the iterator.
*/
std::vector<SlotBase *> slots{ slots_.begin(), slots_.end() };
- for (SlotBase *slot : slots) {
- static_cast<SlotArgs<Args...> *>(slot)->invoke(args...);
- }
+ for (SlotBase *slot : slots)
+ static_cast<SlotArgs<Args...> *>(slot)->activate(args...);
}
};
ef='#n289'>289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
#!/usr/bin/env python3

# SPDX-License-Identifier: BSD-3-Clause
# Copyright (C) 2022, Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>

# A simple libcamera capture example
#
# This is a python version of simple-cam from:
# https://git.libcamera.org/libcamera/simple-cam.git
#
# \todo Move to simple-cam repository when the Python API has stabilized more

import libcamera as libcam
import selectors
import sys
import time

TIMEOUT_SEC = 3


def handle_camera_event(cm):
    # cm.get_ready_requests() returns the ready requests, which in our case
    # should almost always return a single Request, but in some cases there
    # could be multiple or none.

    reqs = cm.get_ready_requests()

    # Process the captured frames

    for req in reqs:
        process_request(req)


def process_request(request):
    global camera

    print()

    print(f'Request completed: {request}')

    # When a request has completed, it is populated with a metadata control
    # list that allows an application to determine various properties of
    # the completed request. This can include the timestamp of the Sensor
    # capture, or its gain and exposure values, or properties from the IPA
    # such as the state of the 3A algorithms.
    #
    # To examine each request, print all the metadata for inspection. A custom
    # application can parse each of these items and process them according to
    # its needs.

    requestMetadata = request.metadata
    for id, value in requestMetadata.items():
        print(f'\t{id.name} = {value}')

    # Each buffer has its own FrameMetadata to describe its state, or the
    # usage of each buffer. While in our simple capture we only provide one
    # buffer per request, a request can have a buffer for each stream that
    # is established when configuring the camera.
    #
    # This allows a viewfinder and a still image to be processed at the
    # same time, or to allow obtaining the RAW capture buffer from the
    # sensor along with the image as processed by the ISP.

    buffers = request.buffers
    for _, buffer in buffers.items():
        metadata = buffer.metadata

        # Print some information about the buffer which has completed.
        print(f' seq: {metadata.sequence:06} timestamp: {metadata.timestamp} bytesused: ' +
              '/'.join([str(p.bytes_used) for p in metadata.planes]))

        # Image data can be accessed here, but the FrameBuffer
        # must be mapped by the application

    # Re-queue the Request to the camera.
    request.reuse()
    camera.queue_request(request)


# ----------------------------------------------------------------------------
# Camera Naming.
#
# Applications are responsible for deciding how to name cameras, and present
# that information to the users. Every camera has a unique identifier, though
# this string is not designed to be friendly for a human reader.
#
# To support human consumable names, libcamera provides camera properties
# that allow an application to determine a naming scheme based on its needs.
#
# In this example, we focus on the location property, but also detail the
# model string for external cameras, as this is more likely to be visible
# information to the user of an externally connected device.
#
# The unique camera ID is appended for informative purposes.
#
def camera_name(camera):
    props = camera.properties
    location = props.get(libcam.properties.Location, None)

    if location == libcam.properties.LocationEnum.Front:
        name = 'Internal front camera'
    elif location == libcam.properties.LocationEnum.Back:
        name = 'Internal back camera'
    elif location == libcam.properties.LocationEnum.External:
        name = 'External camera'
        if libcam.properties.Model in props:
            name += f' "{props[libcam.properties.Model]}"'
    else:
        name = 'Undefined location'

    name += f' ({camera.id})'

    return name


def main():
    global camera

    # --------------------------------------------------------------------
    # Get the Camera Manager.
    #
    # The Camera Manager is responsible for enumerating all the Camera
    # in the system, by associating Pipeline Handlers with media entities
    # registered in the system.
    #
    # The CameraManager provides a list of available Cameras that
    # applications can operate on.
    #
    # There can only be a single CameraManager within any process space.

    cm = libcam.CameraManager.singleton()

    # Just as a test, generate names of the Cameras registered in the
    # system, and list them.

    for camera in cm.cameras:
        print(f' - {camera_name(camera)}')

    # --------------------------------------------------------------------
    # Camera
    #
    # Camera are entities created by pipeline handlers, inspecting the
    # entities registered in the system and reported to applications
    # by the CameraManager.
    #
    # In general terms, a Camera corresponds to a single image source
    # available in the system, such as an image sensor.
    #
    # Application lock usage of Camera by 'acquiring' them.
    # Once done with it, application shall similarly 'release' the Camera.
    #
    # As an example, use the first available camera in the system after
    # making sure that at least one camera is available.
    #
    # Cameras can be obtained by their ID or their index, to demonstrate