/* SPDX-License-Identifier: LGPL-2.1-or-later */ /* * Copyright (C) 2020, Collabora Ltd. * Author: Nicolas Dufresne * * gstlibcamerapool.cpp - GStreamer Buffer Pool */ #include "gstlibcamerapool.h" #include #include "gstlibcamera-utils.h" using namespace libcamera; enum { SIGNAL_BUFFER_NOTIFY, N_SIGNALS }; static guint signals[N_SIGNALS]; struct _GstLibcameraPool { GstBufferPool parent; GstAtomicQueue *queue; GstLibcameraAllocator *allocator; Stream *stream; }; G_DEFINE_TYPE(GstLibcameraPool, gst_libcamera_pool, GST_TYPE_BUFFER_POOL) static GstFlowReturn gst_libcamera_pool_acquire_buffer(GstBufferPool *pool, GstBuffer **buffer, [[maybe_unused]] GstBufferPoolAcquireParams *params) { GstLibcameraPool *self = GST_LIBCAMERA_POOL(pool); GstBuffer *buf = GST_BUFFER(gst_atomic_queue_pop(self->queue)); if (!buf) return GST_FLOW_ERROR; if (!gst_libcamera_allocator_prepare_buffer(self->allocator, self->stream, buf)) return GST_FLOW_ERROR; *buffer = buf; return GST_FLOW_OK; } static void gst_libcamera_pool_reset_buffer(GstBufferPool *pool, GstBuffer *buffer) { GstBufferPoolClass *klass = GST_BUFFER_POOL_CLASS(gst_libcamera_pool_parent_class); /* Clears all the memories and only pool the GstBuffer objects */ gst_buffer_remove_all_memory(buffer); klass->reset_buffer(pool, buffer); GST_BUFFER_FLAGS(buffer) = 0; } static void gst_libcamera_pool_release_buffer(GstBufferPool *pool, GstBuffer *buffer) { GstLibcameraPool *self = GST_LIBCAMERA_POOL(pool); bool do_notify = gst_atomic_queue_length(self->queue) == 0; gst_atomic_queue_push(self->queue, buffer); if (do_notify) g_signal_emit(self, signals[SIGNAL_BUFFER_NOTIFY], 0); } static void gst_libcamera_pool_init(GstLibcameraPool *self) { self->queue = gst_atomic_queue_new(4); } static void gst_libcamera_pool_finalize(GObject *object) { GstLibcameraPool *self = GST_LIBCAMERA_POOL(object); GstBuffer *buf; while ((buf = GST_BUFFER(gst_atomic_queue_pop(self->queue)))) gst_buffer_unref(buf); gst_atomic_queue_unref(self->queue); g_object_unref(self->allocator); G_OBJECT_CLASS(gst_libcamera_pool_parent_class)->finalize(object); } static void gst_libcamera_pool_class_init(GstLibcameraPoolClass *klass) { auto *object_class = G_OBJECT_CLASS(klass); auto *pool_class = GST_BUFFER_POOL_CLASS(klass); object_class->finalize = gst_libcamera_pool_finalize; pool_class->start = nullptr; pool_class->acquire_buffer = gst_libcamera_pool_acquire_buffer; pool_class->reset_buffer = gst_libcamera_pool_reset_buffer; pool_class->release_buffer = gst_libcamera_pool_release_buffer; signals[SIGNAL_BUFFER_NOTIFY] = g_signal_new("buffer-notify", G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_LAST, 0, nullptr, nullptr, nullptr, G_TYPE_NONE, 0); } GstLibcameraPool * gst_libcamera_pool_new(GstLibcameraAllocator *allocator, Stream *stream) { auto *pool = GST_LIBCAMERA_POOL(g_object_new(GST_TYPE_LIBCAMERA_POOL, nullptr)); pool->allocator = GST_LIBCAMERA_ALLOCATOR(g_object_ref(allocator)); pool->stream = stream; gsize pool_size = gst_libcamera_allocator_get_pool_size(allocator, stream); for (gsize i = 0; i < pool_size; i++) { GstBuffer *buffer = gst_buffer_new(); gst_atomic_queue_push(pool->queue, buffer); } return pool; } Stream * gst_libcamera_pool_get_stream(GstLibcameraPool *self) { return self->stream; } Stream * gst_libcamera_buffer_get_stream(GstBuffer *buffer) { auto *self = (GstLibcameraPool *)buffer->pool; return self->stream; } FrameBuffer * gst_libcamera_buffer_get_frame_buffer(GstBuffer *buffer) { GstMemory *mem = gst_buffer_peek_memory(buffer, 0); return gst_libcamera_memory_get_frame_buffer(mem); } ='n28' href='#n28'>28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
.. section-begin-libcamera

===========
 libcamera
===========

**A complex camera support library for Linux, Android, and ChromeOS**

Cameras are complex devices that need heavy hardware image processing
operations. Control of the processing is based on advanced algorithms that must
run on a programmable processor. This has traditionally been implemented in a
dedicated MCU in the camera, but in embedded devices algorithms have been moved
to the main CPU to save cost. Blurring the boundary between camera devices and
Linux often left the user with no other option than a vendor-specific
closed-source solution.

To address this problem the Linux media community has very recently started
collaboration with the industry to develop a camera stack that will be
open-source-friendly while still protecting vendor core IP. libcamera was born
out of that collaboration and will offer modern camera support to Linux-based
systems, including traditional Linux distributions, ChromeOS and Android.

.. section-end-libcamera
.. section-begin-getting-started

Getting Started
---------------

To fetch the sources, build and install:

::

  git clone git://linuxtv.org/libcamera.git
  cd libcamera
  meson build
  ninja -C build install

Dependencies
~~~~~~~~~~~~

The following Debian/Ubuntu packages are required for building libcamera.
Other distributions may have differing package names:

A C++ toolchain: [required]
	Either {g++, clang}

for libcamera: [required]
	meson ninja-build python3-yaml

for device hotplug enumeration: [optional]
	pkg-config libudev-dev

for qcam: [optional]
	qtbase5-dev libqt5core5a libqt5gui5 libqt5widgets5

for documentation: [optional]
	python3-sphinx doxygen

.. section-end-getting-started