mirror of
https://gitlab.freedesktop.org/pipewire/pipewire
synced 2024-09-19 16:01:45 +00:00
libcamera: work on rewrite
Use manager to hotplug devices Use StreamConfig to enumerate formats
This commit is contained in:
parent
b0e3e43c42
commit
b2c38a2b3b
|
@ -128,6 +128,7 @@ extern "C" {
|
|||
|
||||
/** keys for libcamera factory names */
|
||||
#define SPA_NAME_API_LIBCAMERA_ENUM_CLIENT "api.libcamera.enum.client" /**< a libcamera client Device interface */
|
||||
#define SPA_NAME_API_LIBCAMERA_ENUM_MANAGER "api.libcamera.enum.manager" /**< a libcamera manager Device interface */
|
||||
#define SPA_NAME_API_LIBCAMERA_DEVICE "api.libcamera.device" /**< a libcamera Device interface */
|
||||
#define SPA_NAME_API_LIBCAMERA_SOURCE "api.libcamera.source" /**< a libcamera Node interface for
|
||||
* capturing */
|
||||
|
|
|
@ -57,7 +57,6 @@ struct impl {
|
|||
struct spa_device_info info;
|
||||
|
||||
struct spa_source source;
|
||||
struct spa_libcamera_device dev;
|
||||
};
|
||||
|
||||
static int emit_object_info(struct impl *this, uint32_t id)
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
/* Spa libcamera Source
|
||||
/* Spa libcamera Device
|
||||
*
|
||||
* Copyright (C) 2020, Collabora Ltd.
|
||||
* Author: Raghavendra Rao Sidlagatta <raghavendra.rao@collabora.com>
|
||||
* Copyright (C) 2021 Wim Taymans <wim.taymans@gmail.com>
|
||||
*
|
||||
* libcamera-device.c
|
||||
* libcamera-device.cpp
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
|
@ -36,6 +37,7 @@
|
|||
#include <spa/support/log.h>
|
||||
#include <spa/support/loop.h>
|
||||
#include <spa/utils/keys.h>
|
||||
#include <spa/utils/result.h>
|
||||
#include <spa/utils/names.h>
|
||||
#include <spa/utils/string.h>
|
||||
#include <spa/node/node.h>
|
||||
|
@ -45,18 +47,21 @@
|
|||
#include <spa/debug/pod.h>
|
||||
|
||||
#include "libcamera.h"
|
||||
#include "libcamera-manager.hpp"
|
||||
|
||||
static const char default_device[] = "/dev/media0";
|
||||
#include <libcamera/camera.h>
|
||||
#include <libcamera/property_ids.h>
|
||||
|
||||
using namespace libcamera;
|
||||
|
||||
struct props {
|
||||
char device[64];
|
||||
char device[128];
|
||||
char device_name[128];
|
||||
int device_fd;
|
||||
};
|
||||
|
||||
static void reset_props(struct props *props)
|
||||
{
|
||||
strncpy(props->device, default_device, 64);
|
||||
spa_zero(*props);
|
||||
}
|
||||
|
||||
struct impl {
|
||||
|
@ -69,49 +74,68 @@ struct impl {
|
|||
|
||||
struct spa_hook_list hooks;
|
||||
|
||||
struct spa_libcamera_device dev;
|
||||
CameraManager *manager;
|
||||
std::shared_ptr<Camera> camera;
|
||||
};
|
||||
|
||||
static int emit_info(struct impl *this, bool full)
|
||||
std::string cameraDesc(const Camera *camera)
|
||||
{
|
||||
const ControlList &props = camera->properties();
|
||||
bool addModel = true;
|
||||
std::string name;
|
||||
|
||||
if (props.contains(properties::Location)) {
|
||||
switch (props.get(properties::Location)) {
|
||||
case properties::CameraLocationFront:
|
||||
addModel = false;
|
||||
name = "Internal front camera ";
|
||||
break;
|
||||
case properties::CameraLocationBack:
|
||||
addModel = false;
|
||||
name = "Internal back camera ";
|
||||
break;
|
||||
case properties::CameraLocationExternal:
|
||||
name = "External camera ";
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (addModel) {
|
||||
if (props.contains(properties::Model))
|
||||
name = "" + props.get(properties::Model);
|
||||
else
|
||||
name = "" + camera->id();
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
static int emit_info(struct impl *impl, bool full)
|
||||
{
|
||||
int res, err;
|
||||
struct spa_dict_item items[10];
|
||||
struct spa_dict dict;
|
||||
uint32_t n_items = 0;
|
||||
struct spa_device_info info;
|
||||
struct spa_param_info params[2];
|
||||
char path[128], version[16];
|
||||
|
||||
if ((res = spa_libcamera_open(&this->dev)) < 0)
|
||||
return res;
|
||||
char path[256], desc[256], name[256];
|
||||
|
||||
info = SPA_DEVICE_INFO_INIT();
|
||||
|
||||
info.change_mask = SPA_DEVICE_CHANGE_MASK_PROPS;
|
||||
|
||||
do {
|
||||
err = ioctl(this->dev.fd, MEDIA_IOC_DEVICE_INFO, &this->dev.dev_info);
|
||||
} while (err == -1 && errno == EINTR);
|
||||
|
||||
if(err < 0) {
|
||||
spa_log_error(this->log, "%s:: Failed to query MEDIA_IOC_DEVICE_INFO on fd %d", __FUNCTION__, this->dev.fd);
|
||||
}
|
||||
|
||||
#define ADD_ITEM(key, value) items[n_items++] = SPA_DICT_ITEM_INIT(key, value)
|
||||
snprintf(path, sizeof(path), "libcamera:%s", this->props.device);
|
||||
snprintf(path, sizeof(path), "libcamera:%s", impl->props.device);
|
||||
ADD_ITEM(SPA_KEY_OBJECT_PATH, path);
|
||||
ADD_ITEM(SPA_KEY_DEVICE_API, "libcamera");
|
||||
ADD_ITEM(SPA_KEY_MEDIA_CLASS, "Video/Device");
|
||||
ADD_ITEM(SPA_KEY_API_LIBCAMERA_PATH, (char *)this->props.device);
|
||||
ADD_ITEM(SPA_KEY_API_LIBCAMERA_CAP_DRIVER, (char *)this->dev.dev_info.driver);
|
||||
ADD_ITEM(SPA_KEY_API_LIBCAMERA_CAP_CARD, (char *)this->dev.dev_info.model);
|
||||
ADD_ITEM(SPA_KEY_API_LIBCAMERA_CAP_BUS_INFO, (char *)this->dev.dev_info.bus_info);
|
||||
snprintf(version, sizeof(version), "%u.%u.%u",
|
||||
(this->dev.dev_info.media_version >> 16) & 0xFF,
|
||||
(this->dev.dev_info.media_version >> 8) & 0xFF,
|
||||
(this->dev.dev_info.media_version) & 0xFF);
|
||||
ADD_ITEM(SPA_KEY_API_LIBCAMERA_CAP_VERSION, version);
|
||||
ADD_ITEM(SPA_KEY_API_LIBCAMERA_PATH, (char *)impl->props.device);
|
||||
snprintf(desc, sizeof(desc), "%s", cameraDesc(impl->camera.get()).c_str());
|
||||
ADD_ITEM(SPA_KEY_DEVICE_DESCRIPTION, desc);
|
||||
snprintf(name, sizeof(name), "libcamera_device.%s", impl->props.device);
|
||||
ADD_ITEM(SPA_KEY_DEVICE_NAME, name);
|
||||
#undef ADD_ITEM
|
||||
info.props = &SPA_DICT_INIT(items, n_items);
|
||||
|
||||
dict = SPA_DICT_INIT(items, n_items);
|
||||
info.props = &dict;
|
||||
|
||||
info.change_mask |= SPA_DEVICE_CHANGE_MASK_PARAMS;
|
||||
params[0] = SPA_PARAM_INFO(SPA_PARAM_EnumProfile, SPA_PARAM_INFO_READ);
|
||||
|
@ -119,22 +143,19 @@ static int emit_info(struct impl *this, bool full)
|
|||
info.n_params = SPA_N_ELEMENTS(params);
|
||||
info.params = params;
|
||||
|
||||
spa_device_emit_info(&this->hooks, &info);
|
||||
spa_device_emit_info(&impl->hooks, &info);
|
||||
|
||||
if (spa_libcamera_is_capture(&this->dev)) {
|
||||
if (true) {
|
||||
struct spa_device_object_info oinfo;
|
||||
|
||||
oinfo = SPA_DEVICE_OBJECT_INFO_INIT();
|
||||
oinfo.type = SPA_TYPE_INTERFACE_Node;
|
||||
oinfo.factory_name = SPA_NAME_API_LIBCAMERA_SOURCE;
|
||||
oinfo.change_mask = SPA_DEVICE_OBJECT_CHANGE_MASK_PROPS;
|
||||
oinfo.props = &SPA_DICT_INIT(items, n_items);
|
||||
oinfo.props = &dict;
|
||||
|
||||
spa_device_emit_object_info(&this->hooks, 0, &oinfo);
|
||||
spa_device_emit_object_info(&impl->hooks, 0, &oinfo);
|
||||
}
|
||||
|
||||
spa_libcamera_close(&this->dev);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -143,30 +164,30 @@ static int impl_add_listener(void *object,
|
|||
const struct spa_device_events *events,
|
||||
void *data)
|
||||
{
|
||||
struct impl *this = object;
|
||||
struct impl *impl = (struct impl*)object;
|
||||
struct spa_hook_list save;
|
||||
int res = 0;
|
||||
|
||||
spa_return_val_if_fail(this != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(impl != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(events != NULL, -EINVAL);
|
||||
|
||||
spa_hook_list_isolate(&this->hooks, &save, listener, events, data);
|
||||
spa_hook_list_isolate(&impl->hooks, &save, listener, events, data);
|
||||
|
||||
if (events->info || events->object_info)
|
||||
res = emit_info(this, true);
|
||||
res = emit_info(impl, true);
|
||||
|
||||
spa_hook_list_join(&this->hooks, &save);
|
||||
spa_hook_list_join(&impl->hooks, &save);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static int impl_sync(void *object, int seq)
|
||||
{
|
||||
struct impl *this = object;
|
||||
struct impl *impl = (struct impl*) object;
|
||||
|
||||
spa_return_val_if_fail(this != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(impl != NULL, -EINVAL);
|
||||
|
||||
spa_device_emit_result(&this->hooks, seq, 0, 0, NULL);
|
||||
spa_device_emit_result(&impl->hooks, seq, 0, 0, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -195,15 +216,15 @@ static const struct spa_device_methods impl_device = {
|
|||
|
||||
static int impl_get_interface(struct spa_handle *handle, const char *type, void **interface)
|
||||
{
|
||||
struct impl *this;
|
||||
struct impl *impl;
|
||||
|
||||
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(interface != NULL, -EINVAL);
|
||||
|
||||
this = (struct impl *) handle;
|
||||
impl = (struct impl *) handle;
|
||||
|
||||
if (spa_streq(type, SPA_TYPE_INTERFACE_Device))
|
||||
*interface = &this->device;
|
||||
*interface = &impl->device;
|
||||
else
|
||||
return -ENOENT;
|
||||
|
||||
|
@ -212,6 +233,10 @@ static int impl_get_interface(struct spa_handle *handle, const char *type, void
|
|||
|
||||
static int impl_clear(struct spa_handle *handle)
|
||||
{
|
||||
struct impl *impl = (struct impl *) handle;
|
||||
if (impl->manager)
|
||||
libcamera_manager_release(impl->manager);
|
||||
impl->manager = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -229,32 +254,44 @@ impl_init(const struct spa_handle_factory *factory,
|
|||
const struct spa_support *support,
|
||||
uint32_t n_support)
|
||||
{
|
||||
struct impl *this;
|
||||
struct impl *impl;
|
||||
const char *str;
|
||||
int res;
|
||||
|
||||
spa_return_val_if_fail(factory != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
||||
|
||||
handle->get_interface = impl_get_interface;
|
||||
handle->clear = impl_clear, this = (struct impl *) handle;
|
||||
handle->clear = impl_clear, impl = (struct impl *) handle;
|
||||
|
||||
this->log = spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Log);
|
||||
libcamera_log_topic_init(this->log);
|
||||
impl->log = (struct spa_log*) spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Log);
|
||||
libcamera_log_topic_init(impl->log);
|
||||
|
||||
spa_hook_list_init(&this->hooks);
|
||||
spa_hook_list_init(&impl->hooks);
|
||||
|
||||
this->device.iface = SPA_INTERFACE_INIT(
|
||||
impl->device.iface = SPA_INTERFACE_INIT(
|
||||
SPA_TYPE_INTERFACE_Device,
|
||||
SPA_VERSION_DEVICE,
|
||||
&impl_device, this);
|
||||
this->dev.log = this->log;
|
||||
this->dev.fd = -1;
|
||||
&impl_device, impl);
|
||||
|
||||
reset_props(&this->props);
|
||||
reset_props(&impl->props);
|
||||
|
||||
if (info && (str = spa_dict_lookup(info, SPA_KEY_API_LIBCAMERA_PATH)))
|
||||
strncpy(this->props.device, str, 63);
|
||||
strncpy(impl->props.device, str, sizeof(impl->props.device));
|
||||
|
||||
impl->manager = libcamera_manager_acquire();
|
||||
if (impl->manager == NULL) {
|
||||
res = -errno;
|
||||
spa_log_error(impl->log, "can't start camera manager: %s", spa_strerror(res));
|
||||
return res;
|
||||
}
|
||||
|
||||
impl->camera = impl->manager->get(impl->props.device);
|
||||
if (impl->camera == NULL) {
|
||||
spa_log_error(impl->log, "unknown camera id %s", impl->props.device);
|
||||
libcamera_manager_release(impl->manager);
|
||||
return -ENOENT;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -277,6 +314,7 @@ static int impl_enum_interface_info(const struct spa_handle_factory *factory,
|
|||
return 1;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
const struct spa_handle_factory spa_libcamera_device_factory = {
|
||||
SPA_VERSION_HANDLE_FACTORY,
|
||||
SPA_NAME_API_LIBCAMERA_DEVICE,
|
||||
|
@ -285,3 +323,4 @@ const struct spa_handle_factory spa_libcamera_device_factory = {
|
|||
impl_init,
|
||||
impl_enum_interface_info,
|
||||
};
|
||||
}
|
412
spa/plugins/libcamera/libcamera-manager.cpp
Normal file
412
spa/plugins/libcamera/libcamera-manager.cpp
Normal file
|
@ -0,0 +1,412 @@
|
|||
/* Spa libcamera manager
|
||||
*
|
||||
* Copyright © 2021 Wim Taymans
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <limits.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
|
||||
#include <libcamera/camera.h>
|
||||
#include <libcamera/camera_manager.h>
|
||||
|
||||
#include <linux/videodev2.h>
|
||||
|
||||
using namespace libcamera;
|
||||
|
||||
#include <spa/support/log.h>
|
||||
#include <spa/utils/type.h>
|
||||
#include <spa/utils/keys.h>
|
||||
#include <spa/utils/names.h>
|
||||
#include <spa/utils/result.h>
|
||||
#include <spa/utils/string.h>
|
||||
#include <spa/support/loop.h>
|
||||
#include <spa/support/plugin.h>
|
||||
#include <spa/monitor/device.h>
|
||||
#include <spa/monitor/utils.h>
|
||||
|
||||
#include "libcamera.h"
|
||||
#include "libcamera-manager.hpp"
|
||||
|
||||
#define MAX_DEVICES 64
|
||||
|
||||
struct global {
|
||||
int ref;
|
||||
CameraManager *manager;
|
||||
};
|
||||
|
||||
static struct global global;
|
||||
|
||||
struct device {
|
||||
uint32_t id;
|
||||
std::shared_ptr<Camera> camera;
|
||||
};
|
||||
|
||||
typedef struct impl {
|
||||
struct spa_handle handle;
|
||||
struct spa_device device;
|
||||
|
||||
struct spa_log *log;
|
||||
struct spa_loop *main_loop;
|
||||
|
||||
struct spa_hook_list hooks;
|
||||
|
||||
uint64_t info_all;
|
||||
struct spa_device_info info;
|
||||
|
||||
CameraManager *manager;
|
||||
void addCamera(std::shared_ptr<libcamera::Camera> camera);
|
||||
void removeCamera(std::shared_ptr<libcamera::Camera> camera);
|
||||
|
||||
struct device devices[MAX_DEVICES];
|
||||
uint32_t n_devices;
|
||||
} Impl;
|
||||
|
||||
int libcamera_manager_release(CameraManager *manager)
|
||||
{
|
||||
if (global.manager != manager)
|
||||
return -EINVAL;
|
||||
|
||||
if (--global.ref == 0) {
|
||||
global.manager->stop();
|
||||
delete global.manager;
|
||||
global.manager = NULL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
CameraManager *libcamera_manager_acquire(void)
|
||||
{
|
||||
int res;
|
||||
|
||||
if (global.ref++ == 0) {
|
||||
global.manager = new CameraManager();
|
||||
if (global.manager == NULL)
|
||||
return NULL;
|
||||
|
||||
if ((res = global.manager->start()) < 0) {
|
||||
libcamera_manager_release(global.manager);
|
||||
errno = -res;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return global.manager;
|
||||
}
|
||||
|
||||
static struct device *add_device(struct impl *impl, std::shared_ptr<Camera> camera)
|
||||
{
|
||||
struct device *device;
|
||||
uint32_t id;
|
||||
|
||||
if (impl->n_devices >= MAX_DEVICES)
|
||||
return NULL;
|
||||
id = impl->n_devices++;
|
||||
device = &impl->devices[id];
|
||||
device->id = id;
|
||||
device->camera = camera;
|
||||
return device;
|
||||
}
|
||||
|
||||
static struct device *find_device(struct impl *impl, std::shared_ptr<Camera> camera)
|
||||
{
|
||||
uint32_t i;
|
||||
for (i = 0; i < impl->n_devices; i++) {
|
||||
if (impl->devices[i].camera == camera)
|
||||
return &impl->devices[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void remove_device(struct impl *impl, struct device *device)
|
||||
{
|
||||
*device = impl->devices[--impl->n_devices];
|
||||
}
|
||||
|
||||
static void clear_devices(struct impl *impl)
|
||||
{
|
||||
impl->n_devices = 0;
|
||||
}
|
||||
|
||||
static int emit_object_info(struct impl *impl, struct device *device)
|
||||
{
|
||||
struct spa_device_object_info info;
|
||||
uint32_t id = device->id;
|
||||
struct spa_dict_item items[20];
|
||||
struct spa_dict dict;
|
||||
uint32_t n_items = 0;
|
||||
char path[256];
|
||||
|
||||
info = SPA_DEVICE_OBJECT_INFO_INIT();
|
||||
|
||||
info.type = SPA_TYPE_INTERFACE_Device;
|
||||
info.factory_name = SPA_NAME_API_LIBCAMERA_DEVICE;
|
||||
info.change_mask = SPA_DEVICE_OBJECT_CHANGE_MASK_FLAGS |
|
||||
SPA_DEVICE_OBJECT_CHANGE_MASK_PROPS;
|
||||
info.flags = 0;
|
||||
|
||||
#define ADD_ITEM(key, value) items[n_items++] = SPA_DICT_ITEM_INIT(key, value)
|
||||
ADD_ITEM(SPA_KEY_DEVICE_ENUM_API,"libcamera.manager");
|
||||
ADD_ITEM(SPA_KEY_DEVICE_API, "libcamera");
|
||||
ADD_ITEM(SPA_KEY_MEDIA_CLASS, "Video/Device");
|
||||
snprintf(path, sizeof(path), "%s", device->camera->id().c_str());
|
||||
ADD_ITEM(SPA_KEY_API_LIBCAMERA_PATH, path);
|
||||
#undef ADD_ITEM
|
||||
|
||||
dict = SPA_DICT_INIT(items, n_items);
|
||||
info.props = &dict;
|
||||
spa_device_emit_object_info(&impl->hooks, id, &info);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void Impl::addCamera(std::shared_ptr<Camera> camera)
|
||||
{
|
||||
struct impl *impl = this;
|
||||
struct device *device;
|
||||
|
||||
spa_log_info(impl->log, "new camera");
|
||||
|
||||
if ((device = find_device(impl, camera)) != NULL)
|
||||
return;
|
||||
|
||||
if ((device = add_device(impl, camera)) == NULL)
|
||||
return;
|
||||
|
||||
emit_object_info(impl, device);
|
||||
}
|
||||
|
||||
void Impl::removeCamera(std::shared_ptr<Camera> camera)
|
||||
{
|
||||
struct impl *impl = this;
|
||||
struct device *device;
|
||||
|
||||
spa_log_info(impl->log, "camera removed");
|
||||
if ((device = find_device(impl, camera)) == NULL)
|
||||
return;
|
||||
|
||||
remove_device(impl, device);
|
||||
}
|
||||
|
||||
static int start_monitor(struct impl *impl)
|
||||
{
|
||||
impl->manager->cameraAdded.connect(impl, &Impl::addCamera);
|
||||
impl->manager->cameraRemoved.connect(impl, &Impl::removeCamera);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int stop_monitor(struct impl *impl)
|
||||
{
|
||||
if (impl->manager != NULL) {
|
||||
impl->manager->cameraAdded.disconnect(impl, &Impl::addCamera);
|
||||
impl->manager->cameraRemoved.disconnect(impl, &Impl::removeCamera);
|
||||
}
|
||||
clear_devices (impl);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int enum_devices(struct impl *impl)
|
||||
{
|
||||
auto cameras = impl->manager->cameras();
|
||||
|
||||
for (const std::shared_ptr<Camera> &cam : cameras) {
|
||||
impl->addCamera(cam);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct spa_dict_item device_info_items[] = {
|
||||
{ SPA_KEY_DEVICE_API, "libcamera" },
|
||||
{ SPA_KEY_DEVICE_NICK, "libcamera-manager" },
|
||||
};
|
||||
|
||||
static void emit_device_info(struct impl *impl, bool full)
|
||||
{
|
||||
uint64_t old = full ? impl->info.change_mask : 0;
|
||||
if (full)
|
||||
impl->info.change_mask = impl->info_all;
|
||||
if (impl->info.change_mask) {
|
||||
struct spa_dict dict;
|
||||
dict = SPA_DICT_INIT_ARRAY(device_info_items);
|
||||
impl->info.props = &dict;
|
||||
spa_device_emit_info(&impl->hooks, &impl->info);
|
||||
impl->info.change_mask = old;
|
||||
}
|
||||
}
|
||||
|
||||
static void impl_hook_removed(struct spa_hook *hook)
|
||||
{
|
||||
struct impl *impl = (struct impl*)hook->priv;
|
||||
if (spa_hook_list_is_empty(&impl->hooks)) {
|
||||
stop_monitor(impl);
|
||||
if (impl->manager)
|
||||
libcamera_manager_release(impl->manager);
|
||||
impl->manager = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
impl_device_add_listener(void *object, struct spa_hook *listener,
|
||||
const struct spa_device_events *events, void *data)
|
||||
{
|
||||
int res;
|
||||
struct impl *impl = (struct impl*) object;
|
||||
struct spa_hook_list save;
|
||||
|
||||
spa_return_val_if_fail(impl != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(events != NULL, -EINVAL);
|
||||
|
||||
impl->manager = libcamera_manager_acquire();
|
||||
if (impl->manager == NULL)
|
||||
return -errno;
|
||||
|
||||
spa_hook_list_isolate(&impl->hooks, &save, listener, events, data);
|
||||
|
||||
emit_device_info(impl, true);
|
||||
|
||||
if ((res = enum_devices(impl)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = start_monitor(impl)) < 0)
|
||||
return res;
|
||||
|
||||
spa_hook_list_join(&impl->hooks, &save);
|
||||
|
||||
listener->removed = impl_hook_removed;
|
||||
listener->priv = impl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct spa_device_methods impl_device = {
|
||||
SPA_VERSION_DEVICE_METHODS,
|
||||
.add_listener = impl_device_add_listener,
|
||||
};
|
||||
|
||||
static int impl_get_interface(struct spa_handle *handle, const char *type, void **interface)
|
||||
{
|
||||
struct impl *impl;
|
||||
|
||||
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(interface != NULL, -EINVAL);
|
||||
|
||||
impl = (struct impl *) handle;
|
||||
|
||||
if (spa_streq(type, SPA_TYPE_INTERFACE_Device))
|
||||
*interface = &impl->device;
|
||||
else
|
||||
return -ENOENT;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int impl_clear(struct spa_handle *handle)
|
||||
{
|
||||
struct impl *impl = (struct impl *) handle;
|
||||
stop_monitor(impl);
|
||||
if (impl->manager)
|
||||
libcamera_manager_release(impl->manager);
|
||||
impl->manager = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static size_t
|
||||
impl_get_size(const struct spa_handle_factory *factory,
|
||||
const struct spa_dict *params)
|
||||
{
|
||||
return sizeof(struct impl);
|
||||
}
|
||||
|
||||
static int
|
||||
impl_init(const struct spa_handle_factory *factory,
|
||||
struct spa_handle *handle,
|
||||
const struct spa_dict *info,
|
||||
const struct spa_support *support,
|
||||
uint32_t n_support)
|
||||
{
|
||||
struct impl *impl;
|
||||
|
||||
spa_return_val_if_fail(factory != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
||||
|
||||
handle->get_interface = impl_get_interface;
|
||||
handle->clear = impl_clear;
|
||||
|
||||
impl = (struct impl *) handle;
|
||||
|
||||
impl->log = (struct spa_log*)spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Log);
|
||||
libcamera_log_topic_init(impl->log);
|
||||
|
||||
impl->main_loop = (struct spa_loop*)spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Loop);
|
||||
if (impl->main_loop == NULL) {
|
||||
spa_log_error(impl->log, "a main-loop is needed");
|
||||
return -EINVAL;
|
||||
}
|
||||
spa_hook_list_init(&impl->hooks);
|
||||
|
||||
impl->device.iface = SPA_INTERFACE_INIT(
|
||||
SPA_TYPE_INTERFACE_Device,
|
||||
SPA_VERSION_DEVICE,
|
||||
&impl_device, impl);
|
||||
|
||||
impl->info = SPA_DEVICE_INFO_INIT();
|
||||
impl->info_all = SPA_DEVICE_CHANGE_MASK_FLAGS |
|
||||
SPA_DEVICE_CHANGE_MASK_PROPS;
|
||||
impl->info.flags = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct spa_interface_info impl_interfaces[] = {
|
||||
{SPA_TYPE_INTERFACE_Device,},
|
||||
};
|
||||
|
||||
static int
|
||||
impl_enum_interface_info(const struct spa_handle_factory *factory,
|
||||
const struct spa_interface_info **info,
|
||||
uint32_t *index)
|
||||
{
|
||||
spa_return_val_if_fail(factory != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(info != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(index != NULL, -EINVAL);
|
||||
|
||||
if (*index >= SPA_N_ELEMENTS(impl_interfaces))
|
||||
return 0;
|
||||
|
||||
*info = &impl_interfaces[(*index)++];
|
||||
return 1;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
const struct spa_handle_factory spa_libcamera_manager_factory = {
|
||||
SPA_VERSION_HANDLE_FACTORY,
|
||||
SPA_NAME_API_LIBCAMERA_ENUM_MANAGER,
|
||||
NULL,
|
||||
impl_get_size,
|
||||
impl_init,
|
||||
impl_enum_interface_info,
|
||||
};
|
||||
}
|
32
spa/plugins/libcamera/libcamera-manager.hpp
Normal file
32
spa/plugins/libcamera/libcamera-manager.hpp
Normal file
|
@ -0,0 +1,32 @@
|
|||
/* Spa libcamera support
|
||||
*
|
||||
* Copyright © 2021 wim Taymans <wim.taymans@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <libcamera/camera_manager.h>
|
||||
|
||||
#include <linux/media.h>
|
||||
|
||||
using namespace libcamera;
|
||||
|
||||
CameraManager *libcamera_manager_acquire(void);
|
||||
int libcamera_manager_release(CameraManager *manager);
|
|
@ -2,8 +2,9 @@
|
|||
*
|
||||
* Copyright (C) 2020, Collabora Ltd.
|
||||
* Author: Raghavendra Rao Sidlagatta <raghavendra.rao@collabora.com>
|
||||
* Copyright (C) 2021 Wim Taymans <wim.taymans@gmail.com>
|
||||
*
|
||||
* libcamera-source.c
|
||||
* libcamera-source.cpp
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
|
@ -36,6 +37,7 @@
|
|||
#include <spa/utils/list.h>
|
||||
#include <spa/utils/keys.h>
|
||||
#include <spa/utils/names.h>
|
||||
#include <spa/utils/result.h>
|
||||
#include <spa/utils/string.h>
|
||||
#include <spa/monitor/device.h>
|
||||
#include <spa/node/node.h>
|
||||
|
@ -47,19 +49,21 @@
|
|||
#include <spa/pod/filter.h>
|
||||
#include <spa/debug/pod.h>
|
||||
|
||||
#include "libcamera.h"
|
||||
#include <libcamera/camera.h>
|
||||
#include <libcamera/stream.h>
|
||||
#include <libcamera/formats.h>
|
||||
|
||||
static const char default_device[] = "/dev/media0";
|
||||
#include "libcamera.h"
|
||||
#include "libcamera-manager.hpp"
|
||||
|
||||
struct props {
|
||||
char device[64];
|
||||
char device[128];
|
||||
char device_name[128];
|
||||
int device_fd;
|
||||
};
|
||||
|
||||
static void reset_props(struct props *props)
|
||||
{
|
||||
strncpy(props->device, default_device, 64);
|
||||
spa_zero(*props);
|
||||
}
|
||||
|
||||
#define MAX_BUFFERS 32
|
||||
|
@ -85,33 +89,13 @@ struct control {
|
|||
double value;
|
||||
};
|
||||
|
||||
struct camera_fmt {
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
uint32_t pixelformat;
|
||||
uint32_t bytesperline;
|
||||
uint32_t numerator;
|
||||
uint32_t denominator;
|
||||
uint32_t sizeimage;
|
||||
};
|
||||
|
||||
struct port {
|
||||
struct impl *impl;
|
||||
|
||||
bool export_buf;
|
||||
|
||||
bool next_fmtdesc;
|
||||
uint32_t fmtdesc_index;
|
||||
bool next_frmsize;
|
||||
|
||||
bool have_format;
|
||||
struct spa_video_info current_format;
|
||||
struct spa_fraction rate;
|
||||
|
||||
struct spa_libcamera_device dev;
|
||||
|
||||
bool have_query_ext_ctrl;
|
||||
struct camera_fmt fmt;
|
||||
uint32_t memtype;
|
||||
|
||||
struct control controls[MAX_CONTROLS];
|
||||
|
@ -128,6 +112,12 @@ struct port {
|
|||
struct spa_io_buffers *io;
|
||||
struct spa_io_sequence *control;
|
||||
struct spa_param_info params[8];
|
||||
|
||||
uint32_t fmt_index;
|
||||
bool next_fmt;
|
||||
PixelFormat enum_fmt;
|
||||
uint32_t size_index;
|
||||
bool next_size;
|
||||
};
|
||||
|
||||
struct impl {
|
||||
|
@ -151,27 +141,36 @@ struct impl {
|
|||
|
||||
struct spa_io_position *position;
|
||||
struct spa_io_clock *clock;
|
||||
|
||||
CameraManager *manager;
|
||||
std::shared_ptr<Camera> camera;
|
||||
|
||||
unsigned int have_config;
|
||||
std::unique_ptr<CameraConfiguration> config;
|
||||
|
||||
unsigned int active:1;
|
||||
unsigned int acquired:1;
|
||||
};
|
||||
|
||||
#define CHECK_PORT(this,direction,port_id) ((direction) == SPA_DIRECTION_OUTPUT && (port_id) == 0)
|
||||
#define CHECK_PORT(impl,direction,port_id) ((direction) == SPA_DIRECTION_OUTPUT && (port_id) == 0)
|
||||
|
||||
#define GET_OUT_PORT(this,p) (&this->out_ports[p])
|
||||
#define GET_PORT(this,d,p) GET_OUT_PORT(this,p)
|
||||
#define GET_OUT_PORT(impl,p) (&impl->out_ports[p])
|
||||
#define GET_PORT(impl,d,p) GET_OUT_PORT(impl,p)
|
||||
|
||||
#include "libcamera-utils.c"
|
||||
#include "libcamera-utils.cpp"
|
||||
|
||||
static int impl_node_enum_params(void *object, int seq,
|
||||
uint32_t id, uint32_t start, uint32_t num,
|
||||
const struct spa_pod *filter)
|
||||
{
|
||||
struct impl *this = object;
|
||||
struct impl *impl = (struct impl*)object;
|
||||
struct spa_pod *param;
|
||||
struct spa_pod_builder b = { 0 };
|
||||
uint8_t buffer[1024];
|
||||
struct spa_result_node_params result;
|
||||
uint32_t count = 0;
|
||||
|
||||
spa_return_val_if_fail(this != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(impl != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(num != 0, -EINVAL);
|
||||
|
||||
result.id = id;
|
||||
|
@ -184,30 +183,23 @@ next:
|
|||
switch (id) {
|
||||
case SPA_PARAM_PropInfo:
|
||||
{
|
||||
struct props *p = &this->props;
|
||||
struct props *p = &impl->props;
|
||||
|
||||
switch (result.index) {
|
||||
case 0:
|
||||
param = spa_pod_builder_add_object(&b,
|
||||
param = (struct spa_pod*)spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_PropInfo, id,
|
||||
SPA_PROP_INFO_id, SPA_POD_Id(SPA_PROP_device),
|
||||
SPA_PROP_INFO_name, SPA_POD_String("The libcamera device"),
|
||||
SPA_PROP_INFO_type, SPA_POD_String(p->device));
|
||||
break;
|
||||
case 1:
|
||||
param = spa_pod_builder_add_object(&b,
|
||||
param = (struct spa_pod*)spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_PropInfo, id,
|
||||
SPA_PROP_INFO_id, SPA_POD_Id(SPA_PROP_deviceName),
|
||||
SPA_PROP_INFO_name, SPA_POD_String("The libcamera device name"),
|
||||
SPA_PROP_INFO_type, SPA_POD_String(p->device_name));
|
||||
break;
|
||||
case 2:
|
||||
param = spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_PropInfo, id,
|
||||
SPA_PROP_INFO_id, SPA_POD_Id(SPA_PROP_deviceFd),
|
||||
SPA_PROP_INFO_name, SPA_POD_String("The libcamera fd"),
|
||||
SPA_PROP_INFO_type, SPA_POD_Int(p->device_fd));
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
@ -215,15 +207,14 @@ next:
|
|||
}
|
||||
case SPA_PARAM_Props:
|
||||
{
|
||||
struct props *p = &this->props;
|
||||
struct props *p = &impl->props;
|
||||
|
||||
switch (result.index) {
|
||||
case 0:
|
||||
param = spa_pod_builder_add_object(&b,
|
||||
param = (struct spa_pod*)spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_Props, id,
|
||||
SPA_PROP_device, SPA_POD_String(p->device),
|
||||
SPA_PROP_deviceName, SPA_POD_String(p->device_name),
|
||||
SPA_PROP_deviceFd, SPA_POD_Int(p->device_fd));
|
||||
SPA_PROP_deviceName, SPA_POD_String(p->device_name));
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
|
@ -237,7 +228,7 @@ next:
|
|||
if (spa_pod_filter(&b, &result.param, param, filter) < 0)
|
||||
goto next;
|
||||
|
||||
spa_node_emit_result(&this->hooks, seq, 0, SPA_RESULT_TYPE_NODE_PARAMS, &result);
|
||||
spa_node_emit_result(&impl->hooks, seq, 0, SPA_RESULT_TYPE_NODE_PARAMS, &result);
|
||||
|
||||
if (++count != num)
|
||||
goto next;
|
||||
|
@ -249,14 +240,14 @@ static int impl_node_set_param(void *object,
|
|||
uint32_t id, uint32_t flags,
|
||||
const struct spa_pod *param)
|
||||
{
|
||||
struct impl *this = object;
|
||||
struct impl *impl = (struct impl*)object;
|
||||
|
||||
spa_return_val_if_fail(this != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(impl != NULL, -EINVAL);
|
||||
|
||||
switch (id) {
|
||||
case SPA_PARAM_Props:
|
||||
{
|
||||
struct props *p = &this->props;
|
||||
struct props *p = &impl->props;
|
||||
|
||||
if (param == NULL) {
|
||||
reset_props(p);
|
||||
|
@ -275,16 +266,16 @@ static int impl_node_set_param(void *object,
|
|||
|
||||
static int impl_node_set_io(void *object, uint32_t id, void *data, size_t size)
|
||||
{
|
||||
struct impl *this = object;
|
||||
struct impl *impl = (struct impl*)object;
|
||||
|
||||
spa_return_val_if_fail(this != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(impl != NULL, -EINVAL);
|
||||
|
||||
switch (id) {
|
||||
case SPA_IO_Clock:
|
||||
this->clock = data;
|
||||
impl->clock = (struct spa_io_clock*)data;
|
||||
break;
|
||||
case SPA_IO_Position:
|
||||
this->position = data;
|
||||
impl->position = (struct spa_io_position*)data;
|
||||
break;
|
||||
default:
|
||||
return -ENOENT;
|
||||
|
@ -294,29 +285,29 @@ static int impl_node_set_io(void *object, uint32_t id, void *data, size_t size)
|
|||
|
||||
static int impl_node_send_command(void *object, const struct spa_command *command)
|
||||
{
|
||||
struct impl *this = object;
|
||||
struct impl *impl = (struct impl*)object;
|
||||
int res;
|
||||
|
||||
spa_return_val_if_fail(this != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(impl != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(command != NULL, -EINVAL);
|
||||
|
||||
switch (SPA_NODE_COMMAND_ID(command)) {
|
||||
case SPA_NODE_COMMAND_Start:
|
||||
{
|
||||
struct port *port = GET_OUT_PORT(this, 0);
|
||||
struct port *port = GET_OUT_PORT(impl, 0);
|
||||
|
||||
if (!port->have_format)
|
||||
return -EIO;
|
||||
if (port->n_buffers == 0)
|
||||
return -EIO;
|
||||
|
||||
if ((res = spa_libcamera_stream_on(this)) < 0)
|
||||
if ((res = spa_libcamera_stream_on(impl)) < 0)
|
||||
return res;
|
||||
break;
|
||||
}
|
||||
case SPA_NODE_COMMAND_Pause:
|
||||
case SPA_NODE_COMMAND_Suspend:
|
||||
if ((res = spa_libcamera_stream_off(this)) < 0)
|
||||
if ((res = spa_libcamera_stream_off(impl)) < 0)
|
||||
return res;
|
||||
break;
|
||||
default:
|
||||
|
@ -334,25 +325,26 @@ static const struct spa_dict_item info_items[] = {
|
|||
{ SPA_KEY_NODE_DRIVER, "true" },
|
||||
};
|
||||
|
||||
static void emit_node_info(struct impl *this, bool full)
|
||||
static void emit_node_info(struct impl *impl, bool full)
|
||||
{
|
||||
uint64_t old = full ? this->info.change_mask : 0;
|
||||
uint64_t old = full ? impl->info.change_mask : 0;
|
||||
if (full)
|
||||
this->info.change_mask = this->info_all;
|
||||
if (this->info.change_mask) {
|
||||
this->info.props = &SPA_DICT_INIT_ARRAY(info_items);
|
||||
spa_node_emit_info(&this->hooks, &this->info);
|
||||
this->info.change_mask = old;
|
||||
impl->info.change_mask = impl->info_all;
|
||||
if (impl->info.change_mask) {
|
||||
struct spa_dict dict = SPA_DICT_INIT_ARRAY(info_items);
|
||||
impl->info.props = &dict;
|
||||
spa_node_emit_info(&impl->hooks, &impl->info);
|
||||
impl->info.change_mask = old;
|
||||
}
|
||||
}
|
||||
|
||||
static void emit_port_info(struct impl *this, struct port *port, bool full)
|
||||
static void emit_port_info(struct impl *impl, struct port *port, bool full)
|
||||
{
|
||||
uint64_t old = full ? port->info.change_mask : 0;
|
||||
if (full)
|
||||
port->info.change_mask = port->info_all;
|
||||
if (port->info.change_mask) {
|
||||
spa_node_emit_port_info(&this->hooks,
|
||||
spa_node_emit_port_info(&impl->hooks,
|
||||
SPA_DIRECTION_OUTPUT, 0, &port->info);
|
||||
port->info.change_mask = old;
|
||||
}
|
||||
|
@ -364,17 +356,17 @@ impl_node_add_listener(void *object,
|
|||
const struct spa_node_events *events,
|
||||
void *data)
|
||||
{
|
||||
struct impl *this = object;
|
||||
struct impl *impl = (struct impl*)object;
|
||||
struct spa_hook_list save;
|
||||
|
||||
spa_return_val_if_fail(this != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(impl != NULL, -EINVAL);
|
||||
|
||||
spa_hook_list_isolate(&this->hooks, &save, listener, events, data);
|
||||
spa_hook_list_isolate(&impl->hooks, &save, listener, events, data);
|
||||
|
||||
emit_node_info(this, true);
|
||||
emit_port_info(this, GET_OUT_PORT(this, 0), true);
|
||||
emit_node_info(impl, true);
|
||||
emit_port_info(impl, GET_OUT_PORT(impl, 0), true);
|
||||
|
||||
spa_hook_list_join(&this->hooks, &save);
|
||||
spa_hook_list_join(&impl->hooks, &save);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -383,22 +375,22 @@ static int impl_node_set_callbacks(void *object,
|
|||
const struct spa_node_callbacks *callbacks,
|
||||
void *data)
|
||||
{
|
||||
struct impl *this = object;
|
||||
struct impl *impl = (struct impl*)object;
|
||||
|
||||
spa_return_val_if_fail(this != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(impl != NULL, -EINVAL);
|
||||
|
||||
this->callbacks = SPA_CALLBACKS_INIT(callbacks, data);
|
||||
impl->callbacks = SPA_CALLBACKS_INIT(callbacks, data);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int impl_node_sync(void *object, int seq)
|
||||
{
|
||||
struct impl *this = object;
|
||||
struct impl *impl = (struct impl*)object;
|
||||
|
||||
spa_return_val_if_fail(this != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(impl != NULL, -EINVAL);
|
||||
|
||||
spa_node_emit_result(&this->hooks, seq, 0, 0, NULL);
|
||||
spa_node_emit_result(&impl->hooks, seq, 0, 0, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -424,8 +416,8 @@ static int port_get_format(void *object,
|
|||
struct spa_pod **param,
|
||||
struct spa_pod_builder *builder)
|
||||
{
|
||||
struct impl *this = object;
|
||||
struct port *port = GET_PORT(this, direction, port_id);
|
||||
struct impl *impl = (struct impl*)object;
|
||||
struct port *port = GET_PORT(impl, direction, port_id);
|
||||
struct spa_pod_frame f;
|
||||
|
||||
if (!port->have_format)
|
||||
|
@ -464,7 +456,7 @@ static int port_get_format(void *object,
|
|||
return -EIO;
|
||||
}
|
||||
|
||||
*param = spa_pod_builder_pop(builder, &f);
|
||||
*param = (struct spa_pod*)spa_pod_builder_pop(builder, &f);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
@ -476,7 +468,7 @@ static int impl_node_port_enum_params(void *object, int seq,
|
|||
const struct spa_pod *filter)
|
||||
{
|
||||
|
||||
struct impl *this = object;
|
||||
struct impl *impl = (struct impl*)object;
|
||||
struct port *port;
|
||||
struct spa_pod *param;
|
||||
struct spa_pod_builder b = { 0 };
|
||||
|
@ -485,11 +477,11 @@ static int impl_node_port_enum_params(void *object, int seq,
|
|||
uint32_t count = 0;
|
||||
int res;
|
||||
|
||||
spa_return_val_if_fail(this != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(impl != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(num != 0, -EINVAL);
|
||||
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
|
||||
spa_return_val_if_fail(CHECK_PORT(impl, direction, port_id), -EINVAL);
|
||||
|
||||
port = GET_PORT(this, direction, port_id);
|
||||
port = GET_PORT(impl, direction, port_id);
|
||||
|
||||
result.id = id;
|
||||
result.next = start;
|
||||
|
@ -500,17 +492,18 @@ next:
|
|||
|
||||
switch (id) {
|
||||
case SPA_PARAM_PropInfo:
|
||||
return spa_libcamera_enum_controls(this, seq, start, num, filter);
|
||||
return spa_libcamera_enum_controls(impl, seq, start, num, filter);
|
||||
|
||||
case SPA_PARAM_EnumFormat:
|
||||
return spa_libcamera_enum_format(this, seq, start, num, filter);
|
||||
return spa_libcamera_enum_format(impl, seq, start, num, filter);
|
||||
|
||||
case SPA_PARAM_Format:
|
||||
if((res = port_get_format(this, direction, port_id,
|
||||
if((res = port_get_format(impl, direction, port_id,
|
||||
result.index, filter, ¶m, &b)) <= 0)
|
||||
return res;
|
||||
break;
|
||||
case SPA_PARAM_Buffers:
|
||||
{
|
||||
if (!port->have_format)
|
||||
return -EIO;
|
||||
if (result.index > 0)
|
||||
|
@ -519,21 +512,22 @@ next:
|
|||
/* Get the number of buffers to be used from libcamera and send the same to pipewire
|
||||
* so that exact number of buffers are allocated
|
||||
*/
|
||||
uint32_t n_buffers = libcamera_get_nbuffers(port->dev.camera);
|
||||
// uint32_t n_buffers = libcamera_get_nbuffers(port->dev.camera);
|
||||
uint32_t n_buffers = 0;
|
||||
|
||||
param = spa_pod_builder_add_object(&b,
|
||||
param = (struct spa_pod*)spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_ParamBuffers, id,
|
||||
SPA_PARAM_BUFFERS_buffers, SPA_POD_CHOICE_RANGE_Int(n_buffers, n_buffers, n_buffers),
|
||||
SPA_PARAM_BUFFERS_blocks, SPA_POD_Int(1),
|
||||
SPA_PARAM_BUFFERS_size, SPA_POD_Int(port->fmt.sizeimage),
|
||||
SPA_PARAM_BUFFERS_stride, SPA_POD_Int(port->fmt.bytesperline),
|
||||
SPA_PARAM_BUFFERS_size, SPA_POD_Int(0),
|
||||
SPA_PARAM_BUFFERS_stride, SPA_POD_Int(0),
|
||||
SPA_PARAM_BUFFERS_align, SPA_POD_Int(16));
|
||||
break;
|
||||
|
||||
}
|
||||
case SPA_PARAM_Meta:
|
||||
switch (result.index) {
|
||||
case 0:
|
||||
param = spa_pod_builder_add_object(&b,
|
||||
param = (struct spa_pod*)spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_ParamMeta, id,
|
||||
SPA_PARAM_META_type, SPA_POD_Id(SPA_META_Header),
|
||||
SPA_PARAM_META_size, SPA_POD_Int(sizeof(struct spa_meta_header)));
|
||||
|
@ -545,19 +539,19 @@ next:
|
|||
case SPA_PARAM_IO:
|
||||
switch (result.index) {
|
||||
case 0:
|
||||
param = spa_pod_builder_add_object(&b,
|
||||
param = (struct spa_pod*)spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_ParamIO, id,
|
||||
SPA_PARAM_IO_id, SPA_POD_Id(SPA_IO_Buffers),
|
||||
SPA_PARAM_IO_size, SPA_POD_Int(sizeof(struct spa_io_buffers)));
|
||||
break;
|
||||
case 1:
|
||||
param = spa_pod_builder_add_object(&b,
|
||||
param = (struct spa_pod*)spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_ParamIO, id,
|
||||
SPA_PARAM_IO_id, SPA_POD_Id(SPA_IO_Clock),
|
||||
SPA_PARAM_IO_size, SPA_POD_Int(sizeof(struct spa_io_clock)));
|
||||
break;
|
||||
case 2:
|
||||
param = spa_pod_builder_add_object(&b,
|
||||
param = (struct spa_pod*)spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_ParamIO, id,
|
||||
SPA_PARAM_IO_id, SPA_POD_Id(SPA_IO_Control),
|
||||
SPA_PARAM_IO_size, SPA_POD_Int(sizeof(struct spa_io_sequence)));
|
||||
|
@ -573,7 +567,7 @@ next:
|
|||
if (spa_pod_filter(&b, &result.param, param, filter) < 0)
|
||||
goto next;
|
||||
|
||||
spa_node_emit_result(&this->hooks, seq, 0, SPA_RESULT_TYPE_NODE_PARAMS, &result);
|
||||
spa_node_emit_result(&impl->hooks, seq, 0, SPA_RESULT_TYPE_NODE_PARAMS, &result);
|
||||
|
||||
if (++count != num)
|
||||
goto next;
|
||||
|
@ -586,35 +580,34 @@ static int port_set_format(void *object,
|
|||
uint32_t flags,
|
||||
const struct spa_pod *format)
|
||||
{
|
||||
struct impl *this = object;
|
||||
struct impl *impl = (struct impl*)object;
|
||||
struct spa_video_info info;
|
||||
struct port *port = GET_PORT(this, direction, port_id);
|
||||
struct port *port = GET_PORT(impl, direction, port_id);
|
||||
int res;
|
||||
|
||||
if (format == NULL) {
|
||||
if (!port->have_format)
|
||||
return 0;
|
||||
|
||||
spa_libcamera_stream_off(this);
|
||||
spa_libcamera_clear_buffers(this);
|
||||
spa_libcamera_stream_off(impl);
|
||||
spa_libcamera_clear_buffers(impl);
|
||||
port->have_format = false;
|
||||
port->dev.have_format = false;
|
||||
|
||||
spa_libcamera_close(&port->dev);
|
||||
spa_libcamera_close(impl);
|
||||
goto done;
|
||||
} else {
|
||||
if ((res = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
|
||||
return res;
|
||||
|
||||
if (info.media_type != SPA_MEDIA_TYPE_video) {
|
||||
spa_log_error(this->log, "media type must be video");
|
||||
spa_log_error(impl->log, "media type must be video");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
switch (info.media_subtype) {
|
||||
case SPA_MEDIA_SUBTYPE_raw:
|
||||
if (spa_format_video_raw_parse(format, &info.info.raw) < 0) {
|
||||
spa_log_error(this->log, "can't parse video raw");
|
||||
spa_log_error(impl->log, "can't parse video raw");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
|
@ -651,11 +644,11 @@ static int port_set_format(void *object,
|
|||
}
|
||||
|
||||
if (port->have_format && !(flags & SPA_NODE_PARAM_FLAG_TEST_ONLY)) {
|
||||
spa_libcamera_use_buffers(this, NULL, 0);
|
||||
spa_libcamera_use_buffers(impl, NULL, 0);
|
||||
port->have_format = false;
|
||||
}
|
||||
|
||||
if (spa_libcamera_set_format(this, &info, flags & SPA_NODE_PARAM_FLAG_TEST_ONLY) < 0)
|
||||
if (spa_libcamera_set_format(impl, &info, flags & SPA_NODE_PARAM_FLAG_TEST_ONLY) < 0)
|
||||
return -EINVAL;
|
||||
|
||||
if (!(flags & SPA_NODE_PARAM_FLAG_TEST_ONLY)) {
|
||||
|
@ -672,7 +665,7 @@ static int port_set_format(void *object,
|
|||
port->params[4] = SPA_PARAM_INFO(SPA_PARAM_Format, SPA_PARAM_INFO_WRITE);
|
||||
port->params[5] = SPA_PARAM_INFO(SPA_PARAM_Buffers, 0);
|
||||
}
|
||||
emit_port_info(this, port, false);
|
||||
emit_port_info(impl, port, false);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -700,30 +693,30 @@ static int impl_node_port_use_buffers(void *object,
|
|||
struct spa_buffer **buffers,
|
||||
uint32_t n_buffers)
|
||||
{
|
||||
struct impl *this = object;
|
||||
struct impl *impl = (struct impl*)object;
|
||||
struct port *port;
|
||||
int res;
|
||||
|
||||
spa_return_val_if_fail(this != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
|
||||
spa_return_val_if_fail(impl != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(CHECK_PORT(impl, direction, port_id), -EINVAL);
|
||||
|
||||
port = GET_PORT(this, direction, port_id);
|
||||
port = GET_PORT(impl, direction, port_id);
|
||||
|
||||
if (!port->have_format)
|
||||
return -EIO;
|
||||
|
||||
if (port->n_buffers) {
|
||||
spa_libcamera_stream_off(this);
|
||||
if ((res = spa_libcamera_clear_buffers(this)) < 0)
|
||||
spa_libcamera_stream_off(impl);
|
||||
if ((res = spa_libcamera_clear_buffers(impl)) < 0)
|
||||
return res;
|
||||
}
|
||||
if (buffers == NULL)
|
||||
return 0;
|
||||
|
||||
if (flags & SPA_NODE_BUFFERS_FLAG_ALLOC) {
|
||||
res = spa_libcamera_alloc_buffers(this, buffers, n_buffers);
|
||||
res = spa_libcamera_alloc_buffers(impl, buffers, n_buffers);
|
||||
} else {
|
||||
res = spa_libcamera_use_buffers(this, buffers, n_buffers);
|
||||
res = spa_libcamera_use_buffers(impl, buffers, n_buffers);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
@ -734,20 +727,20 @@ static int impl_node_port_set_io(void *object,
|
|||
uint32_t id,
|
||||
void *data, size_t size)
|
||||
{
|
||||
struct impl *this = object;
|
||||
struct impl *impl = (struct impl*)object;
|
||||
struct port *port;
|
||||
|
||||
spa_return_val_if_fail(this != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
|
||||
spa_return_val_if_fail(impl != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(CHECK_PORT(impl, direction, port_id), -EINVAL);
|
||||
|
||||
port = GET_PORT(this, direction, port_id);
|
||||
port = GET_PORT(impl, direction, port_id);
|
||||
|
||||
switch (id) {
|
||||
case SPA_IO_Buffers:
|
||||
port->io = data;
|
||||
port->io = (struct spa_io_buffers*)data;
|
||||
break;
|
||||
case SPA_IO_Control:
|
||||
port->control = data;
|
||||
port->control = (struct spa_io_sequence*)data;
|
||||
break;
|
||||
default:
|
||||
return -ENOENT;
|
||||
|
@ -759,30 +752,30 @@ static int impl_node_port_reuse_buffer(void *object,
|
|||
uint32_t port_id,
|
||||
uint32_t buffer_id)
|
||||
{
|
||||
struct impl *this = object;
|
||||
struct impl *impl = (struct impl*)object;
|
||||
struct port *port;
|
||||
int res;
|
||||
|
||||
spa_return_val_if_fail(this != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(impl != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(port_id == 0, -EINVAL);
|
||||
|
||||
port = GET_OUT_PORT(this, port_id);
|
||||
port = GET_OUT_PORT(impl, port_id);
|
||||
|
||||
spa_return_val_if_fail(buffer_id < port->n_buffers, -EINVAL);
|
||||
|
||||
res = spa_libcamera_buffer_recycle(this, buffer_id);
|
||||
res = spa_libcamera_buffer_recycle(impl, buffer_id);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static void set_control(struct impl *this, struct port *port, uint32_t control_id, float value)
|
||||
static void set_control(struct impl *impl, struct port *port, uint32_t control_id, float value)
|
||||
{
|
||||
if(libcamera_set_control(port->dev.camera, control_id, value) < 0) {
|
||||
spa_log_error(this->log, "Failed to set control");
|
||||
}
|
||||
// if(libcamera_set_control(port->dev.camera, control_id, value) < 0) {
|
||||
spa_log_error(impl->log, "Failed to set control");
|
||||
// }
|
||||
}
|
||||
|
||||
static int process_control(struct impl *this, struct spa_pod_sequence *control)
|
||||
static int process_control(struct impl *impl, struct spa_pod_sequence *control)
|
||||
{
|
||||
struct spa_pod_control *c;
|
||||
struct port *port;
|
||||
|
@ -795,8 +788,8 @@ static int process_control(struct impl *this, struct spa_pod_sequence *control)
|
|||
struct spa_pod_object *obj = (struct spa_pod_object *) &c->value;
|
||||
|
||||
SPA_POD_OBJECT_FOREACH(obj, prop) {
|
||||
port = GET_OUT_PORT(this, 0);
|
||||
set_control(this, port, prop->key,
|
||||
port = GET_OUT_PORT(impl, 0);
|
||||
set_control(impl, port, prop->key,
|
||||
SPA_POD_VALUE(struct spa_pod_float, &prop->value));
|
||||
}
|
||||
break;
|
||||
|
@ -810,31 +803,30 @@ static int process_control(struct impl *this, struct spa_pod_sequence *control)
|
|||
|
||||
static int impl_node_process(void *object)
|
||||
{
|
||||
struct impl *this = object;
|
||||
struct impl *impl = (struct impl*)object;
|
||||
int res;
|
||||
struct spa_io_buffers *io;
|
||||
struct port *port;
|
||||
struct buffer *b;
|
||||
|
||||
spa_return_val_if_fail(this != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(impl != NULL, -EINVAL);
|
||||
|
||||
port = GET_OUT_PORT(this, 0);
|
||||
port = GET_OUT_PORT(impl, 0);
|
||||
io = port->io;
|
||||
spa_return_val_if_fail(io != NULL, -EIO);
|
||||
|
||||
if (port->control)
|
||||
process_control(this, &port->control->sequence);
|
||||
process_control(impl, &port->control->sequence);
|
||||
|
||||
spa_log_trace(this->log, "%p; status %d", this, io->status);
|
||||
spa_log_trace(impl->log, "%p; status %d", impl, io->status);
|
||||
|
||||
if (io->status == SPA_STATUS_HAVE_DATA) {
|
||||
return SPA_STATUS_HAVE_DATA;
|
||||
}
|
||||
|
||||
if (io->buffer_id < port->n_buffers) {
|
||||
if ((res = spa_libcamera_buffer_recycle(this, io->buffer_id)) < 0) {
|
||||
if ((res = spa_libcamera_buffer_recycle(impl, io->buffer_id)) < 0)
|
||||
return res;
|
||||
}
|
||||
|
||||
io->buffer_id = SPA_ID_INVALID;
|
||||
}
|
||||
|
@ -847,7 +839,7 @@ static int impl_node_process(void *object)
|
|||
spa_list_remove(&b->link);
|
||||
SPA_FLAG_SET(b->flags, BUFFER_FLAG_OUTSTANDING);
|
||||
|
||||
spa_log_trace(this->log, "%p: dequeue buffer %d", this, b->id);
|
||||
spa_log_trace(impl->log, "%p: dequeue buffer %d", impl, b->id);
|
||||
|
||||
io->buffer_id = b->id;
|
||||
io->status = SPA_STATUS_HAVE_DATA;
|
||||
|
@ -876,15 +868,15 @@ static const struct spa_node_methods impl_node = {
|
|||
|
||||
static int impl_get_interface(struct spa_handle *handle, const char *type, void **interface)
|
||||
{
|
||||
struct impl *this;
|
||||
struct impl *impl;
|
||||
|
||||
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(interface != NULL, -EINVAL);
|
||||
|
||||
this = (struct impl *) handle;
|
||||
impl = (struct impl *) handle;
|
||||
|
||||
if (spa_streq(type, SPA_TYPE_INTERFACE_Node))
|
||||
*interface = &this->node;
|
||||
*interface = &impl->node;
|
||||
else
|
||||
return -ENOENT;
|
||||
|
||||
|
@ -893,19 +885,15 @@ static int impl_get_interface(struct spa_handle *handle, const char *type, void
|
|||
|
||||
static int impl_clear(struct spa_handle *handle)
|
||||
{
|
||||
struct impl *this;
|
||||
struct impl *impl;
|
||||
struct port *port;
|
||||
|
||||
this = (struct impl *) handle;
|
||||
port = GET_OUT_PORT(this, 0);
|
||||
impl = (struct impl *) handle;
|
||||
port = GET_OUT_PORT(impl, 0);
|
||||
|
||||
if(port->dev.camera) {
|
||||
deleteLibCamera(port->dev.camera);
|
||||
port->dev.camera = NULL;
|
||||
}
|
||||
if(this->have_source) {
|
||||
spa_system_close(this->system, port->source.fd);
|
||||
this->have_source = false;
|
||||
if(impl->have_source) {
|
||||
spa_system_close(impl->system, port->source.fd);
|
||||
impl->have_source = false;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -924,7 +912,7 @@ impl_init(const struct spa_handle_factory *factory,
|
|||
const struct spa_support *support,
|
||||
uint32_t n_support)
|
||||
{
|
||||
struct impl *this;
|
||||
struct impl *impl;
|
||||
const char *str;
|
||||
struct port *port;
|
||||
int res;
|
||||
|
@ -935,44 +923,44 @@ impl_init(const struct spa_handle_factory *factory,
|
|||
handle->get_interface = impl_get_interface;
|
||||
handle->clear = impl_clear;
|
||||
|
||||
this = (struct impl *) handle;
|
||||
impl = (struct impl *) handle;
|
||||
|
||||
this->log = spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Log);
|
||||
libcamera_log_topic_init(this->log);
|
||||
impl->log = (struct spa_log*)spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Log);
|
||||
libcamera_log_topic_init(impl->log);
|
||||
|
||||
this->data_loop = spa_support_find(support, n_support, SPA_TYPE_INTERFACE_DataLoop);
|
||||
this->system = spa_support_find(support, n_support, SPA_TYPE_INTERFACE_System);
|
||||
impl->data_loop = (struct spa_loop*)spa_support_find(support, n_support, SPA_TYPE_INTERFACE_DataLoop);
|
||||
impl->system = (struct spa_system*)spa_support_find(support, n_support, SPA_TYPE_INTERFACE_System);
|
||||
|
||||
if (this->data_loop == NULL) {
|
||||
spa_log_error(this->log, "a data_loop is needed");
|
||||
if (impl->data_loop == NULL) {
|
||||
spa_log_error(impl->log, "a data_loop is needed");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (this->system == NULL) {
|
||||
spa_log_error(this->log, "a system is needed");
|
||||
if (impl->system == NULL) {
|
||||
spa_log_error(impl->log, "a system is needed");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
this->node.iface = SPA_INTERFACE_INIT(
|
||||
impl->node.iface = SPA_INTERFACE_INIT(
|
||||
SPA_TYPE_INTERFACE_Node,
|
||||
SPA_VERSION_NODE,
|
||||
&impl_node, this);
|
||||
spa_hook_list_init(&this->hooks);
|
||||
&impl_node, impl);
|
||||
spa_hook_list_init(&impl->hooks);
|
||||
|
||||
this->info_all = SPA_NODE_CHANGE_MASK_FLAGS |
|
||||
impl->info_all = SPA_NODE_CHANGE_MASK_FLAGS |
|
||||
SPA_NODE_CHANGE_MASK_PROPS |
|
||||
SPA_NODE_CHANGE_MASK_PARAMS;
|
||||
this->info = SPA_NODE_INFO_INIT();
|
||||
this->info.max_output_ports = 1;
|
||||
this->info.flags = SPA_NODE_FLAG_RT;
|
||||
this->params[0] = SPA_PARAM_INFO(SPA_PARAM_PropInfo, SPA_PARAM_INFO_READ);
|
||||
this->params[1] = SPA_PARAM_INFO(SPA_PARAM_Props, SPA_PARAM_INFO_READWRITE);
|
||||
this->info.params = this->params;
|
||||
this->info.n_params = 2;
|
||||
reset_props(&this->props);
|
||||
impl->info = SPA_NODE_INFO_INIT();
|
||||
impl->info.max_output_ports = 1;
|
||||
impl->info.flags = SPA_NODE_FLAG_RT;
|
||||
impl->params[0] = SPA_PARAM_INFO(SPA_PARAM_PropInfo, SPA_PARAM_INFO_READ);
|
||||
impl->params[1] = SPA_PARAM_INFO(SPA_PARAM_Props, SPA_PARAM_INFO_READWRITE);
|
||||
impl->info.params = impl->params;
|
||||
impl->info.n_params = 2;
|
||||
reset_props(&impl->props);
|
||||
|
||||
port = GET_OUT_PORT(this, 0);
|
||||
port->impl = this;
|
||||
port = GET_OUT_PORT(impl, 0);
|
||||
port->impl = impl;
|
||||
spa_list_init(&port->queue);
|
||||
port->info_all = SPA_PORT_CHANGE_MASK_FLAGS |
|
||||
SPA_PORT_CHANGE_MASK_PARAMS;
|
||||
|
@ -989,23 +977,22 @@ impl_init(const struct spa_handle_factory *factory,
|
|||
port->info.params = port->params;
|
||||
port->info.n_params = 6;
|
||||
|
||||
port->export_buf = true;
|
||||
port->have_query_ext_ctrl = true;
|
||||
port->dev.log = this->log;
|
||||
port->dev.fd = -1;
|
||||
if (info && (str = spa_dict_lookup(info, SPA_KEY_API_LIBCAMERA_PATH)))
|
||||
strncpy(impl->props.device, str, sizeof(impl->props.device));
|
||||
|
||||
if(port->dev.camera == NULL)
|
||||
port->dev.camera = (LibCamera*)newLibCamera();
|
||||
if(port->dev.camera != NULL)
|
||||
libcamera_set_log(port->dev.camera, port->dev.log);
|
||||
|
||||
if (info && (str = spa_dict_lookup(info, SPA_KEY_API_LIBCAMERA_PATH))) {
|
||||
strncpy(this->props.device, str, 63);
|
||||
if ((res = spa_libcamera_open(&port->dev)) < 0)
|
||||
return res;
|
||||
spa_libcamera_close(&port->dev);
|
||||
impl->manager = libcamera_manager_acquire();
|
||||
if (impl->manager == NULL) {
|
||||
res = -errno;
|
||||
spa_log_error(impl->log, "can't start camera manager: %s", spa_strerror(res));
|
||||
return res;
|
||||
}
|
||||
|
||||
impl->camera = impl->manager->get(impl->props.device);
|
||||
if (impl->camera == NULL) {
|
||||
spa_log_error(impl->log, "unknown camera id %s", impl->props.device);
|
||||
libcamera_manager_release(impl->manager);
|
||||
return -ENOENT;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1028,6 +1015,7 @@ static int impl_enum_interface_info(const struct spa_handle_factory *factory,
|
|||
return 1;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
const struct spa_handle_factory spa_libcamera_source_factory = {
|
||||
SPA_VERSION_HANDLE_FACTORY,
|
||||
SPA_NAME_API_LIBCAMERA_SOURCE,
|
||||
|
@ -1036,3 +1024,4 @@ const struct spa_handle_factory spa_libcamera_source_factory = {
|
|||
impl_init,
|
||||
impl_enum_interface_info,
|
||||
};
|
||||
}
|
|
@ -1,930 +0,0 @@
|
|||
/* Spa
|
||||
*
|
||||
* Copyright (C) 2020, Collabora Ltd.
|
||||
* Author: Raghavendra Rao Sidlagatta <raghavendra.rao@collabora.com>
|
||||
*
|
||||
* libcamera-utils.c
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sched.h>
|
||||
#include <errno.h>
|
||||
#include <sys/mman.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <linux/media.h>
|
||||
|
||||
static void libcamera_on_fd_events(struct spa_source *source);
|
||||
|
||||
int get_dev_fd(struct spa_libcamera_device *dev) {
|
||||
if(dev->fd == -1) {
|
||||
int fd = open("/dev/media0", O_RDONLY | O_NONBLOCK, 0);
|
||||
return fd;
|
||||
} else {
|
||||
return dev->fd;
|
||||
}
|
||||
}
|
||||
|
||||
int spa_libcamera_open(struct spa_libcamera_device *dev)
|
||||
{
|
||||
if(!dev) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
dev->fd = get_dev_fd(dev);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int spa_libcamera_is_capture(struct spa_libcamera_device *dev)
|
||||
{
|
||||
if(!dev) {
|
||||
spa_log_error(dev->log, "Invalid argument");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int spa_libcamera_close(struct spa_libcamera_device *dev)
|
||||
{
|
||||
if(!dev) {
|
||||
spa_log_error(dev->log, "Invalid argument");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (dev->fd == -1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (dev->active || dev->have_format) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (close(dev->fd)) {
|
||||
spa_log_warn(dev->log, "close: %m");
|
||||
}
|
||||
|
||||
dev->fd = -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int spa_libcamera_buffer_recycle(struct impl *this, uint32_t buffer_id)
|
||||
{
|
||||
struct port *port = &this->out_ports[0];
|
||||
struct buffer *b = &port->buffers[buffer_id];
|
||||
|
||||
if (!SPA_FLAG_IS_SET(b->flags, BUFFER_FLAG_OUTSTANDING))
|
||||
return 0;
|
||||
|
||||
SPA_FLAG_CLEAR(b->flags, BUFFER_FLAG_OUTSTANDING);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int spa_libcamera_clear_buffers(struct impl *this)
|
||||
{
|
||||
struct port *port = &this->out_ports[0];
|
||||
uint32_t i;
|
||||
|
||||
if (port->n_buffers == 0)
|
||||
return 0;
|
||||
|
||||
for (i = 0; i < port->n_buffers; i++) {
|
||||
struct buffer *b;
|
||||
struct spa_data *d;
|
||||
|
||||
b = &port->buffers[i];
|
||||
d = b->outbuf->datas;
|
||||
|
||||
if (SPA_FLAG_IS_SET(b->flags, BUFFER_FLAG_OUTSTANDING)) {
|
||||
spa_log_debug(this->log, "queueing outstanding buffer %p", b);
|
||||
spa_libcamera_buffer_recycle(this, i);
|
||||
}
|
||||
if (SPA_FLAG_IS_SET(b->flags, BUFFER_FLAG_MAPPED)) {
|
||||
munmap(SPA_PTROFF(b->ptr, -d[0].mapoffset, void),
|
||||
d[0].maxsize - d[0].mapoffset);
|
||||
}
|
||||
if (SPA_FLAG_IS_SET(b->flags, BUFFER_FLAG_ALLOCATED)) {
|
||||
close(d[0].fd);
|
||||
}
|
||||
d[0].type = SPA_ID_INVALID;
|
||||
}
|
||||
|
||||
port->n_buffers = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct format_info {
|
||||
char fourcc[32];
|
||||
uint32_t format;
|
||||
uint32_t media_type;
|
||||
uint32_t media_subtype;
|
||||
};
|
||||
|
||||
#define VIDEO SPA_MEDIA_TYPE_video
|
||||
#define IMAGE SPA_MEDIA_TYPE_image
|
||||
|
||||
#define RAW SPA_MEDIA_SUBTYPE_raw
|
||||
|
||||
#define BAYER SPA_MEDIA_SUBTYPE_bayer
|
||||
#define MJPG SPA_MEDIA_SUBTYPE_mjpg
|
||||
#define JPEG SPA_MEDIA_SUBTYPE_jpeg
|
||||
#define DV SPA_MEDIA_SUBTYPE_dv
|
||||
#define MPEGTS SPA_MEDIA_SUBTYPE_mpegts
|
||||
#define H264 SPA_MEDIA_SUBTYPE_h264
|
||||
#define H263 SPA_MEDIA_SUBTYPE_h263
|
||||
#define MPEG1 SPA_MEDIA_SUBTYPE_mpeg1
|
||||
#define MPEG2 SPA_MEDIA_SUBTYPE_mpeg2
|
||||
#define MPEG4 SPA_MEDIA_SUBTYPE_mpeg4
|
||||
#define XVID SPA_MEDIA_SUBTYPE_xvid
|
||||
#define VC1 SPA_MEDIA_SUBTYPE_vc1
|
||||
#define VP8 SPA_MEDIA_SUBTYPE_vp8
|
||||
|
||||
#define FORMAT_UNKNOWN SPA_VIDEO_FORMAT_UNKNOWN
|
||||
#define FORMAT_ENCODED SPA_VIDEO_FORMAT_ENCODED
|
||||
#define FORMAT_RGB15 SPA_VIDEO_FORMAT_RGB15
|
||||
#define FORMAT_BGR15 SPA_VIDEO_FORMAT_BGR15
|
||||
#define FORMAT_RGB16 SPA_VIDEO_FORMAT_RGB16
|
||||
#define FORMAT_BGR SPA_VIDEO_FORMAT_BGR
|
||||
#define FORMAT_RGB SPA_VIDEO_FORMAT_RGB
|
||||
#define FORMAT_BGRA SPA_VIDEO_FORMAT_BGRA
|
||||
#define FORMAT_BGRx SPA_VIDEO_FORMAT_BGRx
|
||||
#define FORMAT_ARGB SPA_VIDEO_FORMAT_ARGB
|
||||
#define FORMAT_xRGB SPA_VIDEO_FORMAT_xRGB
|
||||
#define FORMAT_GRAY8 SPA_VIDEO_FORMAT_GRAY8
|
||||
#define FORMAT_GRAY16_LE SPA_VIDEO_FORMAT_GRAY16_LE
|
||||
#define FORMAT_GRAY16_BE SPA_VIDEO_FORMAT_GRAY16_BE
|
||||
#define FORMAT_YVU9 SPA_VIDEO_FORMAT_YVU9
|
||||
#define FORMAT_YV12 SPA_VIDEO_FORMAT_YV12
|
||||
#define FORMAT_YUY2 SPA_VIDEO_FORMAT_YUY2
|
||||
#define FORMAT_YVYU SPA_VIDEO_FORMAT_YVYU
|
||||
#define FORMAT_UYVY SPA_VIDEO_FORMAT_UYVY
|
||||
#define FORMAT_Y42B SPA_VIDEO_FORMAT_Y42B
|
||||
#define FORMAT_Y41B SPA_VIDEO_FORMAT_Y41B
|
||||
#define FORMAT_YUV9 SPA_VIDEO_FORMAT_YUV9
|
||||
#define FORMAT_I420 SPA_VIDEO_FORMAT_I420
|
||||
#define FORMAT_NV12 SPA_VIDEO_FORMAT_NV12
|
||||
#define FORMAT_NV12_64Z32 SPA_VIDEO_FORMAT_NV12_64Z32
|
||||
#define FORMAT_NV21 SPA_VIDEO_FORMAT_NV21
|
||||
#define FORMAT_NV16 SPA_VIDEO_FORMAT_NV16
|
||||
#define FORMAT_NV61 SPA_VIDEO_FORMAT_NV61
|
||||
#define FORMAT_NV24 SPA_VIDEO_FORMAT_NV24
|
||||
|
||||
static const struct format_info format_info[] = {
|
||||
/* RGB formats */
|
||||
{{"RGB332"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"ARGB555"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"XRGB555"}, FORMAT_RGB15, VIDEO, RAW},
|
||||
{{"ARGB555X"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"XRGB555X"}, FORMAT_BGR15, VIDEO, RAW},
|
||||
{{"RGB565"}, FORMAT_RGB16, VIDEO, RAW},
|
||||
{{"RGB565X"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"BGR666"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"BGR24"}, FORMAT_BGR, VIDEO, RAW},
|
||||
{{"RGB24"}, FORMAT_RGB, VIDEO, RAW},
|
||||
{{"ABGR32"}, FORMAT_BGRA, VIDEO, RAW},
|
||||
{{"XBGR32"}, FORMAT_BGRx, VIDEO, RAW},
|
||||
{{"ARGB32"}, FORMAT_ARGB, VIDEO, RAW},
|
||||
{{"XRGB32"}, FORMAT_xRGB, VIDEO, RAW},
|
||||
|
||||
/* Deprecated Packed RGB Image Formats (alpha ambiguity) */
|
||||
{{"RGB444"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"RGB555"}, FORMAT_RGB15, VIDEO, RAW},
|
||||
{{"RGB555X"}, FORMAT_BGR15, VIDEO, RAW},
|
||||
{{"BGR32"}, FORMAT_BGRx, VIDEO, RAW},
|
||||
{{"RGB32"}, FORMAT_xRGB, VIDEO, RAW},
|
||||
|
||||
/* Grey formats */
|
||||
{{"GREY"}, FORMAT_GRAY8, VIDEO, RAW},
|
||||
{{"Y4"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"Y6"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"Y10"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"Y12"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"Y16"}, FORMAT_GRAY16_LE, VIDEO, RAW},
|
||||
{{"Y16_BE"}, FORMAT_GRAY16_BE, VIDEO, RAW},
|
||||
{{"Y10BPACK"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
|
||||
/* Palette formats */
|
||||
{{"PAL8"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
|
||||
/* Chrominance formats */
|
||||
{{"UV8"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
|
||||
/* Luminance+Chrominance formats */
|
||||
{{"YVU410"}, FORMAT_YVU9, VIDEO, RAW},
|
||||
{{"YVU420"}, FORMAT_YV12, VIDEO, RAW},
|
||||
{{"YVU420M"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"YUYV"}, FORMAT_YUY2, VIDEO, RAW},
|
||||
{{"YYUV"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"YVYU"}, FORMAT_YVYU, VIDEO, RAW},
|
||||
{{"UYVY"}, FORMAT_UYVY, VIDEO, RAW},
|
||||
{{"VYUY"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"YUV422P"}, FORMAT_Y42B, VIDEO, RAW},
|
||||
{{"YUV411P"}, FORMAT_Y41B, VIDEO, RAW},
|
||||
{{"Y41P"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"YUV444"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"YUV555"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"YUV565"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"YUV32"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"YUV410"}, FORMAT_YUV9, VIDEO, RAW},
|
||||
{{"YUV420"}, FORMAT_I420, VIDEO, RAW},
|
||||
{{"YUV420M"}, FORMAT_I420, VIDEO, RAW},
|
||||
{{"HI240"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"HM12"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"M420"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
|
||||
/* two planes -- one Y, one Cr + Cb interleaved */
|
||||
{{"NV12"}, FORMAT_NV12, VIDEO, RAW},
|
||||
{{"NV12M"}, FORMAT_NV12, VIDEO, RAW},
|
||||
{{"NV12MT"}, FORMAT_NV12_64Z32, VIDEO, RAW},
|
||||
{{"NV12MT_16X16"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"NV21"}, FORMAT_NV21, VIDEO, RAW},
|
||||
{{"NV21M"}, FORMAT_NV21, VIDEO, RAW},
|
||||
{{"NV16"}, FORMAT_NV16, VIDEO, RAW},
|
||||
{{"NV16M"}, FORMAT_NV16, VIDEO, RAW},
|
||||
{{"NV61"}, FORMAT_NV61, VIDEO, RAW},
|
||||
{{"NV61M"}, FORMAT_NV61, VIDEO, RAW},
|
||||
{{"NV24"}, FORMAT_NV24, VIDEO, RAW},
|
||||
{{"NV42"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
|
||||
/* Bayer formats - see http://www.siliconimaging.com/RGB%20Bayer.htm */
|
||||
{{"SBGGR8"}, FORMAT_UNKNOWN, VIDEO, BAYER},
|
||||
{{"SGBRG8"}, FORMAT_UNKNOWN, VIDEO, BAYER},
|
||||
{{"SGRBG8"}, FORMAT_UNKNOWN, VIDEO, BAYER},
|
||||
{{"SRGGB8"}, FORMAT_UNKNOWN, VIDEO, BAYER},
|
||||
|
||||
/* compressed formats */
|
||||
{{"MJPEG"}, FORMAT_ENCODED, VIDEO, MJPG},
|
||||
{{"JPEG"}, FORMAT_ENCODED, VIDEO, MJPG},
|
||||
{{"PJPG"}, FORMAT_ENCODED, VIDEO, MJPG},
|
||||
{{"DV"}, FORMAT_ENCODED, VIDEO, DV},
|
||||
{{"MPEG"}, FORMAT_ENCODED, VIDEO, MPEGTS},
|
||||
{{"H264"}, FORMAT_ENCODED, VIDEO, H264},
|
||||
{{"H264_NO_SC"}, FORMAT_ENCODED, VIDEO, H264},
|
||||
{{"H264_MVC"}, FORMAT_ENCODED, VIDEO, H264},
|
||||
{{"H263"}, FORMAT_ENCODED, VIDEO, H263},
|
||||
{{"MPEG1"}, FORMAT_ENCODED, VIDEO, MPEG1},
|
||||
{{"MPEG2"}, FORMAT_ENCODED, VIDEO, MPEG2},
|
||||
{{"MPEG4"}, FORMAT_ENCODED, VIDEO, MPEG4},
|
||||
{{"XVID"}, FORMAT_ENCODED, VIDEO, XVID},
|
||||
{{"VC1_ANNEX_G"}, FORMAT_ENCODED, VIDEO, VC1},
|
||||
{{"VC1_ANNEX_L"}, FORMAT_ENCODED, VIDEO, VC1},
|
||||
{{"VP8"}, FORMAT_ENCODED, VIDEO, VP8},
|
||||
|
||||
/* Vendor-specific formats */
|
||||
{{"WNVA"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"SN9C10X"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"PWC1"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
{{"PWC2"}, FORMAT_UNKNOWN, VIDEO, RAW},
|
||||
};
|
||||
|
||||
static const struct format_info *video_format_to_info(uint32_t fmt) {
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < SPA_N_ELEMENTS(format_info); i++) {
|
||||
if (format_info[i].format == fmt)
|
||||
return &format_info[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const struct format_info *find_format_info_by_media_type(uint32_t type,
|
||||
uint32_t subtype,
|
||||
uint32_t format,
|
||||
int startidx)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = startidx; i < SPA_N_ELEMENTS(format_info); i++) {
|
||||
if ((format_info[i].media_type == type) &&
|
||||
(format_info[i].media_subtype == subtype) &&
|
||||
(format == 0 || format_info[i].format == format))
|
||||
return &format_info[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#define FOURCC_ARGS(f) (f)&0x7f,((f)>>8)&0x7f,((f)>>16)&0x7f,((f)>>24)&0x7f
|
||||
|
||||
static int
|
||||
spa_libcamera_enum_format(struct impl *this, int seq,
|
||||
uint32_t start, uint32_t num,
|
||||
const struct spa_pod *filter)
|
||||
{
|
||||
struct port *port = &this->out_ports[0];
|
||||
int res;
|
||||
const struct format_info *info;
|
||||
uint32_t video_format;
|
||||
struct spa_libcamera_device *dev = &port->dev;
|
||||
uint8_t buffer[1024];
|
||||
struct spa_pod_builder b = { 0 };
|
||||
struct spa_pod_frame f[2];
|
||||
struct spa_result_node_params result;
|
||||
uint32_t width = 0, height = 0;
|
||||
|
||||
if ((res = spa_libcamera_open(dev)) < 0) {
|
||||
spa_log_error(dev->log, "failed to open libcamera device");
|
||||
return res;
|
||||
}
|
||||
|
||||
result.id = SPA_PARAM_EnumFormat;
|
||||
result.next = start;
|
||||
|
||||
if (result.next == 0) {
|
||||
port->fmtdesc_index = 0;
|
||||
spa_zero(port->fmt);
|
||||
}
|
||||
|
||||
next_fmtdesc:
|
||||
port->fmtdesc_index++;
|
||||
|
||||
result.index = result.next++;
|
||||
|
||||
/* Enumerate all the video formats supported by libcamera */
|
||||
video_format = libcamera_drm_to_video_format(
|
||||
libcamera_enum_streamcfgpixel_format(dev->camera, port->fmtdesc_index));
|
||||
if(UINT32_MAX == video_format) {
|
||||
goto enum_end;
|
||||
}
|
||||
port->fmt.pixelformat = video_format;
|
||||
port->fmt.width = libcamera_get_streamcfg_width(dev->camera);
|
||||
port->fmt.height = libcamera_get_streamcfg_height(dev->camera);
|
||||
|
||||
if (!(info = video_format_to_info(video_format))) {
|
||||
goto next_fmtdesc;
|
||||
}
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
spa_pod_builder_push_object(&b, &f[0], SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat);
|
||||
spa_pod_builder_add(&b,
|
||||
SPA_FORMAT_mediaType, SPA_POD_Id(info->media_type),
|
||||
SPA_FORMAT_mediaSubtype, SPA_POD_Id(info->media_subtype),
|
||||
0);
|
||||
|
||||
if (info->media_subtype == SPA_MEDIA_SUBTYPE_raw) {
|
||||
spa_pod_builder_prop(&b, SPA_FORMAT_VIDEO_format, 0);
|
||||
spa_pod_builder_id(&b, info->format);
|
||||
}
|
||||
|
||||
spa_log_info(this->log, "%s:: In have_size: Got width = %u height = %u", __FUNCTION__, width, height);
|
||||
|
||||
spa_pod_builder_prop(&b, SPA_FORMAT_VIDEO_size, 0);
|
||||
spa_pod_builder_rectangle(&b, port->fmt.width, port->fmt.height);
|
||||
|
||||
spa_pod_builder_prop(&b, SPA_FORMAT_VIDEO_framerate, 0);
|
||||
spa_pod_builder_push_choice(&b, &f[1], SPA_CHOICE_None, 0);
|
||||
|
||||
/* Below framerates are hardcoded until framerates are queried from libcamera */
|
||||
port->fmt.denominator = 30;
|
||||
port->fmt.numerator = 1;
|
||||
|
||||
spa_pod_builder_fraction(&b,
|
||||
port->fmt.denominator,
|
||||
port->fmt.numerator);
|
||||
|
||||
spa_pod_builder_pop(&b, &f[1]);
|
||||
result.param = spa_pod_builder_pop(&b, &f[0]);
|
||||
|
||||
spa_node_emit_result(&this->hooks, seq, 0, SPA_RESULT_TYPE_NODE_PARAMS, &result);
|
||||
|
||||
goto next_fmtdesc;
|
||||
|
||||
enum_end:
|
||||
res = 0;
|
||||
|
||||
spa_libcamera_close(dev);
|
||||
return res;
|
||||
}
|
||||
|
||||
static int spa_libcamera_set_format(struct impl *this, struct spa_video_info *format, bool try_only)
|
||||
{
|
||||
struct port *port = &this->out_ports[0];
|
||||
struct spa_libcamera_device *dev = &port->dev;
|
||||
int res;
|
||||
struct camera_fmt fmt;
|
||||
const struct format_info *info = NULL;
|
||||
uint32_t video_format;
|
||||
struct spa_rectangle *size = NULL;
|
||||
struct spa_fraction *framerate = NULL;
|
||||
|
||||
spa_zero(fmt);
|
||||
|
||||
switch (format->media_subtype) {
|
||||
case SPA_MEDIA_SUBTYPE_raw:
|
||||
video_format = format->info.raw.format;
|
||||
size = &format->info.raw.size;
|
||||
framerate = &format->info.raw.framerate;
|
||||
break;
|
||||
case SPA_MEDIA_SUBTYPE_mjpg:
|
||||
case SPA_MEDIA_SUBTYPE_jpeg:
|
||||
video_format = SPA_VIDEO_FORMAT_ENCODED;
|
||||
size = &format->info.mjpg.size;
|
||||
framerate = &format->info.mjpg.framerate;
|
||||
break;
|
||||
case SPA_MEDIA_SUBTYPE_h264:
|
||||
video_format = SPA_VIDEO_FORMAT_ENCODED;
|
||||
size = &format->info.h264.size;
|
||||
framerate = &format->info.h264.framerate;
|
||||
break;
|
||||
default:
|
||||
video_format = SPA_VIDEO_FORMAT_ENCODED;
|
||||
break;
|
||||
}
|
||||
|
||||
info = find_format_info_by_media_type(format->media_type,
|
||||
format->media_subtype, video_format, 0);
|
||||
if (info == NULL || size == NULL || framerate == NULL) {
|
||||
spa_log_error(this->log, "unknown media type %d %d %d", format->media_type,
|
||||
format->media_subtype, video_format);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
fmt.pixelformat = video_format;
|
||||
fmt.width = size->width;
|
||||
fmt.height = size->height;
|
||||
fmt.sizeimage = libcamera_get_max_size(dev->camera);
|
||||
fmt.bytesperline = libcamera_get_stride(dev->camera);
|
||||
fmt.numerator = framerate->denom;
|
||||
fmt.denominator = framerate->num;
|
||||
|
||||
if ((res = spa_libcamera_open(dev)) < 0)
|
||||
return res;
|
||||
|
||||
spa_log_info(dev->log, "set %s %dx%d %d/%d", (char *)&info->fourcc,
|
||||
fmt.width, fmt.height,
|
||||
fmt.denominator, fmt.numerator);
|
||||
|
||||
libcamera_set_streamcfgpixel_format(dev->camera, libcamera_video_format_to_drm(video_format));
|
||||
libcamera_set_streamcfg_width(dev->camera, size->width);
|
||||
libcamera_set_streamcfg_height(dev->camera, size->height);
|
||||
|
||||
if(!libcamera_set_config(dev->camera)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
dev->have_format = true;
|
||||
size->width = libcamera_get_streamcfg_width(dev->camera);
|
||||
size->height = libcamera_get_streamcfg_height(dev->camera);
|
||||
port->rate.denom = framerate->num = fmt.denominator;
|
||||
port->rate.num = framerate->denom = fmt.numerator;
|
||||
|
||||
port->fmt = fmt;
|
||||
port->info.change_mask |= SPA_PORT_CHANGE_MASK_FLAGS | SPA_PORT_CHANGE_MASK_RATE;
|
||||
port->info.flags = (port->export_buf ? SPA_PORT_FLAG_CAN_ALLOC_BUFFERS : 0) |
|
||||
SPA_PORT_FLAG_LIVE |
|
||||
SPA_PORT_FLAG_PHYSICAL |
|
||||
SPA_PORT_FLAG_TERMINAL;
|
||||
port->info.rate = SPA_FRACTION(port->rate.num, port->rate.denom);
|
||||
|
||||
spa_log_info(dev->log, " got format. width = %d height = %d and fmt = %s. bytesperline = %u sizeimage = %u",
|
||||
fmt.width, fmt.height,
|
||||
(char *)&info->fourcc, fmt.bytesperline, fmt.sizeimage);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
spa_libcamera_enum_controls(struct impl *this, int seq,
|
||||
uint32_t start, uint32_t num,
|
||||
const struct spa_pod *filter)
|
||||
{
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
static int mmap_read(struct impl *this)
|
||||
{
|
||||
struct port *port = &this->out_ports[0];
|
||||
struct spa_libcamera_device *dev = &port->dev;
|
||||
struct buffer *b = NULL;
|
||||
struct spa_data *d = NULL;
|
||||
unsigned int sequence = 0;
|
||||
struct timeval timestamp;
|
||||
int64_t pts;
|
||||
struct OutBuf *pOut = NULL;
|
||||
struct CamData *pDatas = NULL;
|
||||
uint32_t bytesused = 0;
|
||||
|
||||
timestamp.tv_sec = 0;
|
||||
timestamp.tv_usec = 0;
|
||||
|
||||
if(dev->camera) {
|
||||
pOut = (struct OutBuf *)libcamera_get_ring_buffer_data(dev->camera);
|
||||
if(!pOut) {
|
||||
spa_log_debug(this->log, "Exiting %s as pOut is NULL", __FUNCTION__);
|
||||
return -1;
|
||||
}
|
||||
/* update the read index of the ring buffer */
|
||||
libcamera_ringbuffer_read_update(dev->camera);
|
||||
|
||||
pDatas = pOut->datas;
|
||||
if(NULL == pDatas) {
|
||||
spa_log_debug(this->log, "Exiting %s on NULL pointer", __FUNCTION__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
b = &port->buffers[pOut->bufIdx];
|
||||
b->outbuf->n_datas = pOut->n_datas;
|
||||
|
||||
if(NULL == b->outbuf->datas) {
|
||||
spa_log_debug(this->log, "Exiting %s as b->outbuf->datas is NULL", __FUNCTION__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
for(unsigned int i = 0; i < pOut->n_datas; ++i) {
|
||||
struct CamData *pData = &pDatas[i];
|
||||
if(NULL == pData) {
|
||||
spa_log_debug(this->log, "Exiting %s on NULL pointer", __FUNCTION__);
|
||||
goto end;
|
||||
}
|
||||
b->outbuf->datas[i].flags = SPA_DATA_FLAG_READABLE;
|
||||
if(port->memtype == SPA_DATA_DmaBuf) {
|
||||
b->outbuf->datas[i].fd = pData->fd;
|
||||
}
|
||||
bytesused = b->outbuf->datas[i].chunk->size = pData->size;
|
||||
timestamp = pData->timestamp;
|
||||
sequence = pData->sequence;
|
||||
|
||||
b->outbuf->datas[i].mapoffset = 0;
|
||||
b->outbuf->datas[i].chunk->offset = 0;
|
||||
b->outbuf->datas[i].chunk->flags = 0;
|
||||
//b->outbuf->datas[i].chunk->stride = pData->sstride; /* FIXME:: This needs to be appropriately filled */
|
||||
b->outbuf->datas[i].maxsize = pData->maxsize;
|
||||
|
||||
spa_log_trace(this->log,"Spa libcamera Source::%s:: got bufIdx = %d and ndatas = %d",
|
||||
__FUNCTION__, pOut->bufIdx, pOut->n_datas);
|
||||
spa_log_trace(this->log," data[%d] --> fd = %ld bytesused = %d sequence = %d",
|
||||
i, b->outbuf->datas[i].fd, bytesused, sequence);
|
||||
}
|
||||
}
|
||||
|
||||
pts = SPA_TIMEVAL_TO_NSEC(×tamp);
|
||||
|
||||
if (this->clock) {
|
||||
this->clock->nsec = pts;
|
||||
this->clock->rate = port->rate;
|
||||
this->clock->position = sequence;
|
||||
this->clock->duration = 1;
|
||||
this->clock->delay = 0;
|
||||
this->clock->rate_diff = 1.0;
|
||||
this->clock->next_nsec = pts + 1000000000LL / port->rate.denom;
|
||||
}
|
||||
|
||||
if (b->h) {
|
||||
b->h->flags = 0;
|
||||
b->h->offset = 0;
|
||||
b->h->seq = sequence;
|
||||
b->h->pts = pts;
|
||||
b->h->dts_offset = 0;
|
||||
}
|
||||
|
||||
d = b->outbuf->datas;
|
||||
d[0].chunk->offset = 0;
|
||||
d[0].chunk->size = bytesused;
|
||||
d[0].chunk->flags = 0;
|
||||
d[0].data = b->ptr;
|
||||
spa_log_trace(this->log,"%s:: b->ptr = %p d[0].data = %p",
|
||||
__FUNCTION__, b->ptr, d[0].data);
|
||||
spa_list_append(&port->queue, &b->link);
|
||||
end:
|
||||
libcamera_free_CamData(dev->camera, pDatas);
|
||||
libcamera_free_OutBuf(dev->camera, pOut);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void libcamera_on_fd_events(struct spa_source *source)
|
||||
{
|
||||
struct impl *this = source->data;
|
||||
struct spa_io_buffers *io;
|
||||
struct port *port = &this->out_ports[0];
|
||||
struct buffer *b;
|
||||
uint64_t cnt;
|
||||
|
||||
if (source->rmask & SPA_IO_ERR) {
|
||||
struct port *port = &this->out_ports[0];
|
||||
spa_log_error(this->log, "libcamera %p: error %08x", this, source->rmask);
|
||||
if (port->source.loop)
|
||||
spa_loop_remove_source(this->data_loop, &port->source);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(source->rmask & SPA_IO_IN)) {
|
||||
spa_log_warn(this->log, "libcamera %p: spurious wakeup %d", this, source->rmask);
|
||||
return;
|
||||
}
|
||||
|
||||
if (spa_system_eventfd_read(this->system, port->source.fd, &cnt) < 0) {
|
||||
spa_log_error(this->log, "Failed to read on event fd");
|
||||
return;
|
||||
}
|
||||
|
||||
if (mmap_read(this) < 0) {
|
||||
spa_log_debug(this->log, "%s:: mmap_read failure", __FUNCTION__);
|
||||
return;
|
||||
}
|
||||
|
||||
if (spa_list_is_empty(&port->queue)) {
|
||||
spa_log_debug(this->log, "Exiting %s as spa list is empty", __FUNCTION__);
|
||||
return;
|
||||
}
|
||||
|
||||
io = port->io;
|
||||
if (io != NULL && io->status != SPA_STATUS_HAVE_DATA) {
|
||||
if (io->buffer_id < port->n_buffers)
|
||||
spa_libcamera_buffer_recycle(this, io->buffer_id);
|
||||
|
||||
b = spa_list_first(&port->queue, struct buffer, link);
|
||||
spa_list_remove(&b->link);
|
||||
SPA_FLAG_SET(b->flags, BUFFER_FLAG_OUTSTANDING);
|
||||
|
||||
io->buffer_id = b->id;
|
||||
io->status = SPA_STATUS_HAVE_DATA;
|
||||
spa_log_trace(this->log, "libcamera %p: now queued %d", this, b->id);
|
||||
}
|
||||
spa_node_call_ready(&this->callbacks, SPA_STATUS_HAVE_DATA);
|
||||
}
|
||||
|
||||
static int spa_libcamera_use_buffers(struct impl *this, struct spa_buffer **buffers, uint32_t n_buffers)
|
||||
{
|
||||
struct port *port = &this->out_ports[0];
|
||||
unsigned int i, j;
|
||||
struct spa_data *d;
|
||||
|
||||
n_buffers = libcamera_get_nbuffers(port->dev.camera);
|
||||
if (n_buffers > 0) {
|
||||
d = buffers[0]->datas;
|
||||
|
||||
if (d[0].type == SPA_DATA_MemFd ||
|
||||
(d[0].type == SPA_DATA_MemPtr && d[0].data != NULL)) {
|
||||
port->memtype = SPA_DATA_MemPtr;
|
||||
} else if (d[0].type == SPA_DATA_DmaBuf) {
|
||||
port->memtype = SPA_DATA_DmaBuf;
|
||||
} else {
|
||||
spa_log_error(this->log, "v4l2: can't use buffers of type %d", d[0].type);
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < n_buffers; i++) {
|
||||
struct buffer *b;
|
||||
|
||||
b = &port->buffers[i];
|
||||
b->id = i;
|
||||
b->outbuf = buffers[i];
|
||||
b->flags = BUFFER_FLAG_OUTSTANDING;
|
||||
b->h = spa_buffer_find_meta_data(buffers[i], SPA_META_Header, sizeof(*b->h));
|
||||
|
||||
spa_log_debug(this->log, "import buffer %p", buffers[i]);
|
||||
|
||||
if (buffers[i]->n_datas < 1) {
|
||||
spa_log_error(this->log, "invalid memory on buffer %p", buffers[i]);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
d = buffers[i]->datas;
|
||||
for(j = 0; j < buffers[i]->n_datas; ++j) {
|
||||
d[j].mapoffset = 0;
|
||||
d[j].maxsize = libcamera_get_max_size(port->dev.camera);
|
||||
|
||||
if (port->memtype == SPA_DATA_MemPtr) {
|
||||
if (d[j].data == NULL) {
|
||||
d[j].fd = -1;
|
||||
d[j].data = mmap(NULL,
|
||||
d[j].maxsize + d[j].mapoffset,
|
||||
PROT_READ, MAP_SHARED,
|
||||
libcamera_get_fd(port->dev.camera, i, j),
|
||||
0);
|
||||
if (d[j].data == MAP_FAILED) {
|
||||
return -errno;
|
||||
}
|
||||
|
||||
b->ptr = d[j].data;
|
||||
spa_log_debug(this->log, "In spa_libcamera_use_buffers(). mmap ptr:%p for fd = %ld buffer: #%d",
|
||||
d[j].data, d[j].fd, i);
|
||||
SPA_FLAG_SET(b->flags, BUFFER_FLAG_MAPPED);
|
||||
} else {
|
||||
b->ptr = d[j].data;
|
||||
spa_log_debug(this->log, "In spa_libcamera_use_buffers(). b->ptr = %p d[j].maxsize = %d for buffer: #%d",
|
||||
d[j].data, d[j].maxsize, i);
|
||||
}
|
||||
spa_log_debug(this->log, "In spa_libcamera_use_buffers(). setting b->ptr = %p for buffer: #%d on libcamera",
|
||||
b->ptr, i);
|
||||
}
|
||||
else if (port->memtype == SPA_DATA_DmaBuf) {
|
||||
d[j].fd = libcamera_get_fd(port->dev.camera, i, j);
|
||||
spa_log_debug(this->log, "Got fd = %ld for buffer: #%d", d[j].fd, i);
|
||||
}
|
||||
else {
|
||||
spa_log_error(this->log, "Exiting spa_libcamera_use_buffers() with -EIO");
|
||||
return -EIO;
|
||||
}
|
||||
}
|
||||
|
||||
spa_libcamera_buffer_recycle(this, i);
|
||||
}
|
||||
port->n_buffers = n_buffers;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
mmap_init(struct impl *this,
|
||||
struct spa_buffer **buffers, uint32_t n_buffers)
|
||||
{
|
||||
struct port *port = &this->out_ports[0];
|
||||
unsigned int i, j;
|
||||
struct spa_data *d;
|
||||
|
||||
spa_log_info(this->log, "In mmap_init()");
|
||||
|
||||
if (n_buffers > 0) {
|
||||
d = buffers[0]->datas;
|
||||
|
||||
if (d[0].type != SPA_ID_INVALID &&
|
||||
d[0].type & (1u << SPA_DATA_DmaBuf)) {
|
||||
port->memtype = SPA_DATA_DmaBuf;
|
||||
} else if (d[0].type != SPA_ID_INVALID &&
|
||||
d[0].type & (1u << SPA_DATA_MemFd)) {
|
||||
port->memtype = SPA_DATA_MemFd;
|
||||
} else if (d[0].type & (1u << SPA_DATA_MemPtr)) {
|
||||
port->memtype = SPA_DATA_MemPtr;
|
||||
} else {
|
||||
spa_log_error(this->log, "v4l2: can't use buffers of type %d", d[0].type);
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
/* get n_buffers from libcamera */
|
||||
uint32_t libcamera_nbuffers = libcamera_get_nbuffers(port->dev.camera);
|
||||
|
||||
for (i = 0; i < libcamera_nbuffers; i++) {
|
||||
struct buffer *b;
|
||||
|
||||
if (buffers[i]->n_datas < 1) {
|
||||
spa_log_error(this->log, "invalid buffer data");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
b = &port->buffers[i];
|
||||
b->id = i;
|
||||
b->outbuf = buffers[i];
|
||||
b->flags = BUFFER_FLAG_OUTSTANDING;
|
||||
b->h = spa_buffer_find_meta_data(buffers[i], SPA_META_Header, sizeof(*b->h));
|
||||
|
||||
d = buffers[i]->datas;
|
||||
for(j = 0; j < buffers[i]->n_datas; ++j) {
|
||||
d[j].type = port->memtype;
|
||||
d[j].flags = SPA_DATA_FLAG_READABLE;
|
||||
d[j].mapoffset = 0;
|
||||
d[j].maxsize = libcamera_get_max_size(port->dev.camera);
|
||||
d[j].chunk->offset = 0;
|
||||
d[j].chunk->size = 0;
|
||||
d[j].chunk->stride = port->fmt.bytesperline; /* FIXME:: This needs to be appropriately filled */
|
||||
d[j].chunk->flags = 0;
|
||||
|
||||
if (port->memtype == SPA_DATA_DmaBuf ||
|
||||
port->memtype == SPA_DATA_MemFd) {
|
||||
d[j].fd = libcamera_get_fd(port->dev.camera, i, j);
|
||||
spa_log_info(this->log, "Got fd = %ld for buffer: #%d", d[j].fd, i);
|
||||
d[j].data = NULL;
|
||||
SPA_FLAG_SET(b->flags, BUFFER_FLAG_ALLOCATED);
|
||||
}
|
||||
else if(port->memtype == SPA_DATA_MemPtr) {
|
||||
d[j].fd = -1;
|
||||
d[j].data = mmap(NULL,
|
||||
d[j].maxsize + d[j].mapoffset,
|
||||
PROT_READ, MAP_SHARED,
|
||||
libcamera_get_fd(port->dev.camera, i, j),
|
||||
0);
|
||||
if (d[j].data == MAP_FAILED) {
|
||||
spa_log_error(this->log, "mmap: %m");
|
||||
continue;
|
||||
}
|
||||
b->ptr = d[j].data;
|
||||
SPA_FLAG_SET(b->flags, BUFFER_FLAG_MAPPED);
|
||||
spa_log_info(this->log, "mmap ptr:%p", d[j].data);
|
||||
} else {
|
||||
spa_log_error(this->log, "invalid buffer type");
|
||||
return -EIO;
|
||||
}
|
||||
}
|
||||
|
||||
spa_libcamera_buffer_recycle(this, i);
|
||||
}
|
||||
port->n_buffers = libcamera_nbuffers;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
spa_libcamera_alloc_buffers(struct impl *this,
|
||||
struct spa_buffer **buffers,
|
||||
uint32_t n_buffers)
|
||||
{
|
||||
int res;
|
||||
struct port *port = &this->out_ports[0];
|
||||
|
||||
if (port->n_buffers > 0)
|
||||
return -EIO;
|
||||
|
||||
if ((res = mmap_init(this, buffers, n_buffers)) < 0) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int spa_libcamera_stream_on(struct impl *this)
|
||||
{
|
||||
struct port *port = &this->out_ports[0];
|
||||
struct spa_libcamera_device *dev = &port->dev;
|
||||
|
||||
if (!dev->have_format) {
|
||||
spa_log_error(this->log, "Exting %s with -EIO", __FUNCTION__);
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
if (dev->active) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
spa_log_info(this->log, "connecting camera");
|
||||
|
||||
libcamera_connect(dev->camera);
|
||||
|
||||
libcamera_start_capture(dev->camera);
|
||||
|
||||
port->source.func = libcamera_on_fd_events;
|
||||
port->source.data = this;
|
||||
port->source.fd = spa_system_eventfd_create(this->system, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
|
||||
port->source.mask = SPA_IO_IN | SPA_IO_ERR;
|
||||
port->source.rmask = 0;
|
||||
if (port->source.fd < 0) {
|
||||
spa_log_error(this->log, "Failed to create eventfd. Exting %s with -EIO", __FUNCTION__);
|
||||
} else {
|
||||
spa_loop_add_source(this->data_loop, &port->source);
|
||||
this->have_source = true;
|
||||
|
||||
libcamera_set_spa_system(dev->camera, this->system);
|
||||
libcamera_set_eventfd(dev->camera, port->source.fd);
|
||||
}
|
||||
|
||||
dev->active = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_remove_source(struct spa_loop *loop,
|
||||
bool async,
|
||||
uint32_t seq,
|
||||
const void *data,
|
||||
size_t size,
|
||||
void *user_data)
|
||||
{
|
||||
struct port *port = user_data;
|
||||
if (port->source.loop)
|
||||
spa_loop_remove_source(loop, &port->source);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int spa_libcamera_stream_off(struct impl *this)
|
||||
{
|
||||
struct port *port = &this->out_ports[0];
|
||||
struct spa_libcamera_device *dev = &port->dev;
|
||||
|
||||
if (!dev->active)
|
||||
return 0;
|
||||
|
||||
spa_log_info(this->log, "stopping camera");
|
||||
|
||||
libcamera_stop_capture(dev->camera);
|
||||
|
||||
spa_log_info(this->log, "disconnecting camera");
|
||||
|
||||
libcamera_disconnect(dev->camera);
|
||||
|
||||
spa_loop_invoke(this->data_loop, do_remove_source, 0, NULL, 0, true, port);
|
||||
|
||||
spa_list_init(&port->queue);
|
||||
dev->active = false;
|
||||
|
||||
return 0;
|
||||
}
|
797
spa/plugins/libcamera/libcamera-utils.cpp
Normal file
797
spa/plugins/libcamera/libcamera-utils.cpp
Normal file
|
@ -0,0 +1,797 @@
|
|||
/* Spa
|
||||
*
|
||||
* Copyright (C) 2020, Collabora Ltd.
|
||||
* Author: Raghavendra Rao Sidlagatta <raghavendra.rao@collabora.com>
|
||||
* Copyright (C) 2021 Wim Taymans <wim.taymans@gmail.com>
|
||||
*
|
||||
* libcamera-utils.cpp
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sched.h>
|
||||
#include <errno.h>
|
||||
#include <sys/mman.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <linux/media.h>
|
||||
|
||||
int spa_libcamera_open(struct impl *impl)
|
||||
{
|
||||
if (impl->acquired)
|
||||
return 0;
|
||||
impl->camera->acquire();
|
||||
impl->acquired = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int spa_libcamera_close(struct impl *impl)
|
||||
{
|
||||
struct port *port = &impl->out_ports[0];
|
||||
if (!impl->acquired)
|
||||
return 0;
|
||||
if (impl->active || port->have_format)
|
||||
return 0;
|
||||
impl->camera->release();
|
||||
impl->acquired = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void spa_libcamera_get_config(struct impl *impl)
|
||||
{
|
||||
if (impl->have_config)
|
||||
return;
|
||||
|
||||
StreamRoles roles;
|
||||
roles.push_back(VideoRecording);
|
||||
impl->config = impl->camera->generateConfiguration(roles);
|
||||
impl->have_config = true;
|
||||
}
|
||||
|
||||
static int spa_libcamera_buffer_recycle(struct impl *impl, uint32_t buffer_id)
|
||||
{
|
||||
struct port *port = &impl->out_ports[0];
|
||||
struct buffer *b = &port->buffers[buffer_id];
|
||||
|
||||
if (!SPA_FLAG_IS_SET(b->flags, BUFFER_FLAG_OUTSTANDING))
|
||||
return 0;
|
||||
|
||||
SPA_FLAG_CLEAR(b->flags, BUFFER_FLAG_OUTSTANDING);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int spa_libcamera_clear_buffers(struct impl *impl)
|
||||
{
|
||||
struct port *port = &impl->out_ports[0];
|
||||
uint32_t i;
|
||||
|
||||
if (port->n_buffers == 0)
|
||||
return 0;
|
||||
|
||||
for (i = 0; i < port->n_buffers; i++) {
|
||||
struct buffer *b;
|
||||
struct spa_data *d;
|
||||
|
||||
b = &port->buffers[i];
|
||||
d = b->outbuf->datas;
|
||||
|
||||
if (SPA_FLAG_IS_SET(b->flags, BUFFER_FLAG_OUTSTANDING)) {
|
||||
spa_log_debug(impl->log, "queueing outstanding buffer %p", b);
|
||||
spa_libcamera_buffer_recycle(impl, i);
|
||||
}
|
||||
if (SPA_FLAG_IS_SET(b->flags, BUFFER_FLAG_MAPPED)) {
|
||||
munmap(SPA_PTROFF(b->ptr, -d[0].mapoffset, void),
|
||||
d[0].maxsize - d[0].mapoffset);
|
||||
}
|
||||
if (SPA_FLAG_IS_SET(b->flags, BUFFER_FLAG_ALLOCATED)) {
|
||||
close(d[0].fd);
|
||||
}
|
||||
d[0].type = SPA_ID_INVALID;
|
||||
}
|
||||
|
||||
port->n_buffers = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct format_info {
|
||||
PixelFormat pix;
|
||||
uint32_t format;
|
||||
uint32_t media_type;
|
||||
uint32_t media_subtype;
|
||||
};
|
||||
|
||||
#define MAKE_FMT(pix,fmt,mt,mst) { pix, SPA_VIDEO_FORMAT_ ##fmt, SPA_MEDIA_TYPE_ ##mt, SPA_MEDIA_SUBTYPE_ ##mst }
|
||||
static const struct format_info format_info[] = {
|
||||
/* RGB formats */
|
||||
MAKE_FMT(formats::RGB565, RGB16, video, raw),
|
||||
MAKE_FMT(formats::RGB565_BE, RGB16, video, raw),
|
||||
MAKE_FMT(formats::RGB888, RGB, video, raw),
|
||||
MAKE_FMT(formats::BGR888, BGR, video, raw),
|
||||
MAKE_FMT(formats::XRGB8888, xRGB, video, raw),
|
||||
MAKE_FMT(formats::XBGR8888, xBGR, video, raw),
|
||||
MAKE_FMT(formats::RGBX8888, RGBx, video, raw),
|
||||
MAKE_FMT(formats::BGRX8888, BGRx, video, raw),
|
||||
MAKE_FMT(formats::ARGB8888, ARGB, video, raw),
|
||||
MAKE_FMT(formats::ABGR8888, ABGR, video, raw),
|
||||
MAKE_FMT(formats::RGBA8888, RGBA, video, raw),
|
||||
MAKE_FMT(formats::BGRA8888, BGRA, video, raw),
|
||||
|
||||
MAKE_FMT(formats::YUYV, YUY2, video, raw),
|
||||
MAKE_FMT(formats::YVYU, YVYU, video, raw),
|
||||
MAKE_FMT(formats::UYVY, UYVY, video, raw),
|
||||
MAKE_FMT(formats::VYUY, VYUY, video, raw),
|
||||
|
||||
MAKE_FMT(formats::NV12, NV12, video, raw),
|
||||
MAKE_FMT(formats::NV21, NV21, video, raw),
|
||||
MAKE_FMT(formats::NV16, NV16, video, raw),
|
||||
MAKE_FMT(formats::NV61, NV61, video, raw),
|
||||
MAKE_FMT(formats::NV24, NV24, video, raw),
|
||||
|
||||
MAKE_FMT(formats::YUV420, I420, video, raw),
|
||||
MAKE_FMT(formats::YVU420, YV12, video, raw),
|
||||
MAKE_FMT(formats::YUV422, Y42B, video, raw),
|
||||
|
||||
MAKE_FMT(formats::MJPEG, ENCODED, video, mjpg),
|
||||
#undef MAKE_FMT
|
||||
};
|
||||
|
||||
static const struct format_info *video_format_to_info(const PixelFormat &pix) {
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < SPA_N_ELEMENTS(format_info); i++) {
|
||||
if (format_info[i].pix == pix)
|
||||
return &format_info[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const struct format_info *find_format_info_by_media_type(uint32_t type,
|
||||
uint32_t subtype,
|
||||
uint32_t format,
|
||||
int startidx)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = startidx; i < SPA_N_ELEMENTS(format_info); i++) {
|
||||
if ((format_info[i].media_type == type) &&
|
||||
(format_info[i].media_subtype == subtype) &&
|
||||
(format == 0 || format_info[i].format == format))
|
||||
return &format_info[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int
|
||||
spa_libcamera_enum_format(struct impl *impl, int seq,
|
||||
uint32_t start, uint32_t num,
|
||||
const struct spa_pod *filter)
|
||||
{
|
||||
struct port *port = &impl->out_ports[0];
|
||||
int res;
|
||||
const struct format_info *info;
|
||||
uint8_t buffer[1024];
|
||||
struct spa_pod_builder b = { 0 };
|
||||
struct spa_pod_frame f[2];
|
||||
struct spa_result_node_params result;
|
||||
struct spa_pod *fmt;
|
||||
uint32_t count = 0;
|
||||
PixelFormat format;
|
||||
Size frameSize;
|
||||
SizeRange sizeRange = SizeRange();
|
||||
|
||||
spa_libcamera_get_config(impl);
|
||||
|
||||
const StreamConfiguration& streamConfig = impl->config->at(0);
|
||||
const StreamFormats &formats = streamConfig.formats();
|
||||
|
||||
result.id = SPA_PARAM_EnumFormat;
|
||||
result.next = start;
|
||||
|
||||
if (result.next == 0) {
|
||||
port->fmt_index = 0;
|
||||
port->size_index = 0;
|
||||
}
|
||||
next:
|
||||
result.index = result.next++;
|
||||
|
||||
next_fmt:
|
||||
if (port->fmt_index >= formats.pixelformats().size())
|
||||
goto enum_end;
|
||||
|
||||
format = formats.pixelformats()[port->fmt_index];
|
||||
|
||||
spa_log_debug(impl->log, "format: %s", format.toString().c_str());
|
||||
|
||||
info = video_format_to_info(format);
|
||||
if (info == NULL) {
|
||||
spa_log_debug(impl->log, "unknown format");
|
||||
port->fmt_index++;
|
||||
goto next_fmt;
|
||||
}
|
||||
|
||||
if (port->size_index < formats.sizes(format).size()) {
|
||||
frameSize = formats.sizes(format)[port->size_index];
|
||||
} else if (port->size_index < 1) {
|
||||
sizeRange = formats.range(format);
|
||||
if (sizeRange.hStep == 0 || sizeRange.vStep == 0) {
|
||||
port->size_index = 0;
|
||||
port->fmt_index++;
|
||||
goto next_fmt;
|
||||
}
|
||||
} else {
|
||||
port->size_index = 0;
|
||||
port->fmt_index++;
|
||||
goto next_fmt;
|
||||
}
|
||||
port->size_index++;
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
spa_pod_builder_push_object(&b, &f[0], SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat);
|
||||
spa_pod_builder_add(&b,
|
||||
SPA_FORMAT_mediaType, SPA_POD_Id(info->media_type),
|
||||
SPA_FORMAT_mediaSubtype, SPA_POD_Id(info->media_subtype),
|
||||
0);
|
||||
|
||||
if (info->media_subtype == SPA_MEDIA_SUBTYPE_raw) {
|
||||
spa_pod_builder_prop(&b, SPA_FORMAT_VIDEO_format, 0);
|
||||
spa_pod_builder_id(&b, info->format);
|
||||
}
|
||||
if (info->pix.modifier()) {
|
||||
spa_pod_builder_prop(&b, SPA_FORMAT_VIDEO_modifier, 0);
|
||||
spa_pod_builder_long(&b, info->pix.modifier());
|
||||
}
|
||||
spa_pod_builder_prop(&b, SPA_FORMAT_VIDEO_size, 0);
|
||||
|
||||
if (sizeRange.hStep != 0 && sizeRange.vStep != 0) {
|
||||
spa_pod_builder_push_choice(&b, &f[1], SPA_CHOICE_Step, 0);
|
||||
spa_pod_builder_frame(&b, &f[1]);
|
||||
spa_pod_builder_rectangle(&b,
|
||||
sizeRange.min.width,
|
||||
sizeRange.min.height);
|
||||
spa_pod_builder_rectangle(&b,
|
||||
sizeRange.min.width,
|
||||
sizeRange.min.height);
|
||||
spa_pod_builder_rectangle(&b,
|
||||
sizeRange.max.width,
|
||||
sizeRange.max.height);
|
||||
spa_pod_builder_rectangle(&b,
|
||||
sizeRange.hStep,
|
||||
sizeRange.vStep);
|
||||
spa_pod_builder_pop(&b, &f[1]);
|
||||
|
||||
} else {
|
||||
spa_pod_builder_rectangle(&b, frameSize.width, frameSize.height);
|
||||
}
|
||||
|
||||
fmt = (struct spa_pod*) spa_pod_builder_pop(&b, &f[0]);
|
||||
|
||||
if (spa_pod_filter(&b, &result.param, fmt, filter) < 0)
|
||||
goto next;
|
||||
|
||||
spa_node_emit_result(&impl->hooks, seq, 0, SPA_RESULT_TYPE_NODE_PARAMS, &result);
|
||||
|
||||
if (++count != num)
|
||||
goto next;
|
||||
|
||||
enum_end:
|
||||
res = 0;
|
||||
return res;
|
||||
}
|
||||
|
||||
static int spa_libcamera_set_format(struct impl *impl, struct spa_video_info *format, bool try_only)
|
||||
{
|
||||
struct port *port = &impl->out_ports[0];
|
||||
const struct format_info *info = NULL;
|
||||
uint32_t video_format;
|
||||
struct spa_rectangle *size = NULL;
|
||||
struct spa_fraction *framerate = NULL;
|
||||
int res;
|
||||
|
||||
switch (format->media_subtype) {
|
||||
case SPA_MEDIA_SUBTYPE_raw:
|
||||
video_format = format->info.raw.format;
|
||||
size = &format->info.raw.size;
|
||||
framerate = &format->info.raw.framerate;
|
||||
break;
|
||||
case SPA_MEDIA_SUBTYPE_mjpg:
|
||||
case SPA_MEDIA_SUBTYPE_jpeg:
|
||||
video_format = SPA_VIDEO_FORMAT_ENCODED;
|
||||
size = &format->info.mjpg.size;
|
||||
framerate = &format->info.mjpg.framerate;
|
||||
break;
|
||||
case SPA_MEDIA_SUBTYPE_h264:
|
||||
video_format = SPA_VIDEO_FORMAT_ENCODED;
|
||||
size = &format->info.h264.size;
|
||||
framerate = &format->info.h264.framerate;
|
||||
break;
|
||||
default:
|
||||
video_format = SPA_VIDEO_FORMAT_ENCODED;
|
||||
break;
|
||||
}
|
||||
|
||||
info = find_format_info_by_media_type(format->media_type,
|
||||
format->media_subtype, video_format, 0);
|
||||
if (info == NULL || size == NULL || framerate == NULL) {
|
||||
spa_log_error(impl->log, "unknown media type %d %d %d", format->media_type,
|
||||
format->media_subtype, video_format);
|
||||
return -EINVAL;
|
||||
}
|
||||
StreamConfiguration& streamConfig = impl->config->at(0);
|
||||
|
||||
streamConfig.pixelFormat = info->pix;
|
||||
streamConfig.size.width = size->width;
|
||||
streamConfig.size.height = size->height;
|
||||
|
||||
if (impl->config->validate() == CameraConfiguration::Invalid)
|
||||
return -EINVAL;
|
||||
|
||||
if ((res = spa_libcamera_open(impl)) < 0)
|
||||
return res;
|
||||
|
||||
res = impl->camera->configure(impl->config.get());
|
||||
if (res != 0)
|
||||
goto error;
|
||||
|
||||
port->have_format = true;
|
||||
|
||||
port->info.change_mask |= SPA_PORT_CHANGE_MASK_FLAGS | SPA_PORT_CHANGE_MASK_RATE;
|
||||
port->info.flags = SPA_PORT_FLAG_CAN_ALLOC_BUFFERS |
|
||||
SPA_PORT_FLAG_LIVE |
|
||||
SPA_PORT_FLAG_PHYSICAL |
|
||||
SPA_PORT_FLAG_TERMINAL;
|
||||
port->info.rate = SPA_FRACTION(port->rate.num, port->rate.denom);
|
||||
|
||||
return 0;
|
||||
error:
|
||||
spa_libcamera_close(impl);
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
static int
|
||||
spa_libcamera_enum_controls(struct impl *impl, int seq,
|
||||
uint32_t start, uint32_t num,
|
||||
const struct spa_pod *filter)
|
||||
{
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
static int mmap_read(struct impl *impl)
|
||||
{
|
||||
#if 0
|
||||
struct port *port = &impl->out_ports[0];
|
||||
struct buffer *b = NULL;
|
||||
struct spa_data *d = NULL;
|
||||
unsigned int sequence = 0;
|
||||
struct timeval timestamp;
|
||||
int64_t pts;
|
||||
struct OutBuf *pOut = NULL;
|
||||
struct CamData *pDatas = NULL;
|
||||
uint32_t bytesused = 0;
|
||||
|
||||
timestamp.tv_sec = 0;
|
||||
timestamp.tv_usec = 0;
|
||||
|
||||
if (impl->camera) {
|
||||
// pOut = (struct OutBuf *)libcamera_get_ring_buffer_data(dev->camera);
|
||||
if(!pOut) {
|
||||
spa_log_debug(impl->log, "Exiting %s as pOut is NULL", __FUNCTION__);
|
||||
return -1;
|
||||
}
|
||||
/* update the read index of the ring buffer */
|
||||
// libcamera_ringbuffer_read_update(dev->camera);
|
||||
|
||||
pDatas = pOut->datas;
|
||||
if(NULL == pDatas) {
|
||||
spa_log_debug(impl->log, "Exiting %s on NULL pointer", __FUNCTION__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
b = &port->buffers[pOut->bufIdx];
|
||||
b->outbuf->n_datas = pOut->n_datas;
|
||||
|
||||
if(NULL == b->outbuf->datas) {
|
||||
spa_log_debug(impl->log, "Exiting %s as b->outbuf->datas is NULL", __FUNCTION__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
for(unsigned int i = 0; i < pOut->n_datas; ++i) {
|
||||
struct CamData *pData = &pDatas[i];
|
||||
if(NULL == pData) {
|
||||
spa_log_debug(impl->log, "Exiting %s on NULL pointer", __FUNCTION__);
|
||||
goto end;
|
||||
}
|
||||
b->outbuf->datas[i].flags = SPA_DATA_FLAG_READABLE;
|
||||
if(port->memtype == SPA_DATA_DmaBuf) {
|
||||
b->outbuf->datas[i].fd = pData->fd;
|
||||
}
|
||||
bytesused = b->outbuf->datas[i].chunk->size = pData->size;
|
||||
timestamp = pData->timestamp;
|
||||
sequence = pData->sequence;
|
||||
|
||||
b->outbuf->datas[i].mapoffset = 0;
|
||||
b->outbuf->datas[i].chunk->offset = 0;
|
||||
b->outbuf->datas[i].chunk->flags = 0;
|
||||
//b->outbuf->datas[i].chunk->stride = pData->sstride; /* FIXME:: This needs to be appropriately filled */
|
||||
b->outbuf->datas[i].maxsize = pData->maxsize;
|
||||
|
||||
spa_log_trace(impl->log,"Spa libcamera Source::%s:: got bufIdx = %d and ndatas = %d",
|
||||
__FUNCTION__, pOut->bufIdx, pOut->n_datas);
|
||||
spa_log_trace(impl->log," data[%d] --> fd = %ld bytesused = %d sequence = %d",
|
||||
i, b->outbuf->datas[i].fd, bytesused, sequence);
|
||||
}
|
||||
}
|
||||
|
||||
pts = SPA_TIMEVAL_TO_NSEC(×tamp);
|
||||
|
||||
if (impl->clock) {
|
||||
impl->clock->nsec = pts;
|
||||
impl->clock->rate = port->rate;
|
||||
impl->clock->position = sequence;
|
||||
impl->clock->duration = 1;
|
||||
impl->clock->delay = 0;
|
||||
impl->clock->rate_diff = 1.0;
|
||||
impl->clock->next_nsec = pts + 1000000000LL / port->rate.denom;
|
||||
}
|
||||
|
||||
if (b->h) {
|
||||
b->h->flags = 0;
|
||||
b->h->offset = 0;
|
||||
b->h->seq = sequence;
|
||||
b->h->pts = pts;
|
||||
b->h->dts_offset = 0;
|
||||
}
|
||||
|
||||
d = b->outbuf->datas;
|
||||
d[0].chunk->offset = 0;
|
||||
d[0].chunk->size = bytesused;
|
||||
d[0].chunk->flags = 0;
|
||||
d[0].data = b->ptr;
|
||||
spa_log_trace(impl->log,"%s:: b->ptr = %p d[0].data = %p",
|
||||
__FUNCTION__, b->ptr, d[0].data);
|
||||
spa_list_append(&port->queue, &b->link);
|
||||
end:
|
||||
// libcamera_free_CamData(dev->camera, pDatas);
|
||||
// libcamera_free_OutBuf(dev->camera, pOut);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void libcamera_on_fd_events(struct spa_source *source)
|
||||
{
|
||||
struct impl *impl = (struct impl*) source->data;
|
||||
struct spa_io_buffers *io;
|
||||
struct port *port = &impl->out_ports[0];
|
||||
struct buffer *b;
|
||||
uint64_t cnt;
|
||||
|
||||
if (source->rmask & SPA_IO_ERR) {
|
||||
struct port *port = &impl->out_ports[0];
|
||||
spa_log_error(impl->log, "libcamera %p: error %08x", impl, source->rmask);
|
||||
if (port->source.loop)
|
||||
spa_loop_remove_source(impl->data_loop, &port->source);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(source->rmask & SPA_IO_IN)) {
|
||||
spa_log_warn(impl->log, "libcamera %p: spurious wakeup %d", impl, source->rmask);
|
||||
return;
|
||||
}
|
||||
|
||||
if (spa_system_eventfd_read(impl->system, port->source.fd, &cnt) < 0) {
|
||||
spa_log_error(impl->log, "Failed to read on event fd");
|
||||
return;
|
||||
}
|
||||
|
||||
if (mmap_read(impl) < 0) {
|
||||
spa_log_debug(impl->log, "%s:: mmap_read failure", __FUNCTION__);
|
||||
return;
|
||||
}
|
||||
|
||||
if (spa_list_is_empty(&port->queue)) {
|
||||
spa_log_debug(impl->log, "Exiting %s as spa list is empty", __FUNCTION__);
|
||||
return;
|
||||
}
|
||||
|
||||
io = port->io;
|
||||
if (io != NULL && io->status != SPA_STATUS_HAVE_DATA) {
|
||||
if (io->buffer_id < port->n_buffers)
|
||||
spa_libcamera_buffer_recycle(impl, io->buffer_id);
|
||||
|
||||
b = spa_list_first(&port->queue, struct buffer, link);
|
||||
spa_list_remove(&b->link);
|
||||
SPA_FLAG_SET(b->flags, BUFFER_FLAG_OUTSTANDING);
|
||||
|
||||
io->buffer_id = b->id;
|
||||
io->status = SPA_STATUS_HAVE_DATA;
|
||||
spa_log_trace(impl->log, "libcamera %p: now queued %d", impl, b->id);
|
||||
}
|
||||
spa_node_call_ready(&impl->callbacks, SPA_STATUS_HAVE_DATA);
|
||||
}
|
||||
|
||||
static int spa_libcamera_use_buffers(struct impl *impl, struct spa_buffer **buffers, uint32_t n_buffers)
|
||||
{
|
||||
#if 0
|
||||
struct port *port = &impl->out_ports[0];
|
||||
unsigned int i, j;
|
||||
struct spa_data *d;
|
||||
|
||||
n_buffers = libcamera_get_nbuffers(port->dev.camera);
|
||||
if (n_buffers > 0) {
|
||||
d = buffers[0]->datas;
|
||||
|
||||
if (d[0].type == SPA_DATA_MemFd ||
|
||||
(d[0].type == SPA_DATA_MemPtr && d[0].data != NULL)) {
|
||||
port->memtype = SPA_DATA_MemPtr;
|
||||
} else if (d[0].type == SPA_DATA_DmaBuf) {
|
||||
port->memtype = SPA_DATA_DmaBuf;
|
||||
} else {
|
||||
spa_log_error(impl->log, "v4l2: can't use buffers of type %d", d[0].type);
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < n_buffers; i++) {
|
||||
struct buffer *b;
|
||||
|
||||
b = &port->buffers[i];
|
||||
b->id = i;
|
||||
b->outbuf = buffers[i];
|
||||
b->flags = BUFFER_FLAG_OUTSTANDING;
|
||||
b->h = spa_buffer_find_meta_data(buffers[i], SPA_META_Header, sizeof(*b->h));
|
||||
|
||||
spa_log_debug(impl->log, "import buffer %p", buffers[i]);
|
||||
|
||||
if (buffers[i]->n_datas < 1) {
|
||||
spa_log_error(impl->log, "invalid memory on buffer %p", buffers[i]);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
d = buffers[i]->datas;
|
||||
for(j = 0; j < buffers[i]->n_datas; ++j) {
|
||||
d[j].mapoffset = 0;
|
||||
d[j].maxsize = libcamera_get_max_size(port->dev.camera);
|
||||
|
||||
if (port->memtype == SPA_DATA_MemPtr) {
|
||||
if (d[j].data == NULL) {
|
||||
d[j].fd = -1;
|
||||
d[j].data = mmap(NULL,
|
||||
d[j].maxsize + d[j].mapoffset,
|
||||
PROT_READ, MAP_SHARED,
|
||||
libcamera_get_fd(port->dev.camera, i, j),
|
||||
0);
|
||||
if (d[j].data == MAP_FAILED) {
|
||||
return -errno;
|
||||
}
|
||||
|
||||
b->ptr = d[j].data;
|
||||
spa_log_debug(impl->log, "In spa_libcamera_use_buffers(). mmap ptr:%p for fd = %ld buffer: #%d",
|
||||
d[j].data, d[j].fd, i);
|
||||
SPA_FLAG_SET(b->flags, BUFFER_FLAG_MAPPED);
|
||||
} else {
|
||||
b->ptr = d[j].data;
|
||||
spa_log_debug(impl->log, "In spa_libcamera_use_buffers(). b->ptr = %p d[j].maxsize = %d for buffer: #%d",
|
||||
d[j].data, d[j].maxsize, i);
|
||||
}
|
||||
spa_log_debug(impl->log, "In spa_libcamera_use_buffers(). setting b->ptr = %p for buffer: #%d on libcamera",
|
||||
b->ptr, i);
|
||||
}
|
||||
else if (port->memtype == SPA_DATA_DmaBuf) {
|
||||
d[j].fd = libcamera_get_fd(port->dev.camera, i, j);
|
||||
spa_log_debug(impl->log, "Got fd = %ld for buffer: #%d", d[j].fd, i);
|
||||
}
|
||||
else {
|
||||
spa_log_error(impl->log, "Exiting spa_libcamera_use_buffers() with -EIO");
|
||||
return -EIO;
|
||||
}
|
||||
}
|
||||
|
||||
spa_libcamera_buffer_recycle(impl, i);
|
||||
}
|
||||
port->n_buffers = n_buffers;
|
||||
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
mmap_init(struct impl *impl,
|
||||
struct spa_buffer **buffers, uint32_t n_buffers)
|
||||
{
|
||||
#if 0
|
||||
struct port *port = &impl->out_ports[0];
|
||||
unsigned int i, j;
|
||||
struct spa_data *d;
|
||||
|
||||
spa_log_info(impl->log, "In mmap_init()");
|
||||
|
||||
if (n_buffers > 0) {
|
||||
d = buffers[0]->datas;
|
||||
|
||||
if (d[0].type != SPA_ID_INVALID &&
|
||||
d[0].type & (1u << SPA_DATA_DmaBuf)) {
|
||||
port->memtype = SPA_DATA_DmaBuf;
|
||||
} else if (d[0].type != SPA_ID_INVALID &&
|
||||
d[0].type & (1u << SPA_DATA_MemFd)) {
|
||||
port->memtype = SPA_DATA_MemFd;
|
||||
} else if (d[0].type & (1u << SPA_DATA_MemPtr)) {
|
||||
port->memtype = SPA_DATA_MemPtr;
|
||||
} else {
|
||||
spa_log_error(impl->log, "v4l2: can't use buffers of type %d", d[0].type);
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
/* get n_buffers from libcamera */
|
||||
uint32_t libcamera_nbuffers = libcamera_get_nbuffers(port->dev.camera);
|
||||
|
||||
for (i = 0; i < libcamera_nbuffers; i++) {
|
||||
struct buffer *b;
|
||||
|
||||
if (buffers[i]->n_datas < 1) {
|
||||
spa_log_error(impl->log, "invalid buffer data");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
b = &port->buffers[i];
|
||||
b->id = i;
|
||||
b->outbuf = buffers[i];
|
||||
b->flags = BUFFER_FLAG_OUTSTANDING;
|
||||
b->h = spa_buffer_find_meta_data(buffers[i], SPA_META_Header, sizeof(*b->h));
|
||||
|
||||
d = buffers[i]->datas;
|
||||
for(j = 0; j < buffers[i]->n_datas; ++j) {
|
||||
d[j].type = port->memtype;
|
||||
d[j].flags = SPA_DATA_FLAG_READABLE;
|
||||
d[j].mapoffset = 0;
|
||||
d[j].maxsize = libcamera_get_max_size(port->dev.camera);
|
||||
d[j].chunk->offset = 0;
|
||||
d[j].chunk->size = 0;
|
||||
d[j].chunk->stride = port->fmt.bytesperline; /* FIXME:: This needs to be appropriately filled */
|
||||
d[j].chunk->flags = 0;
|
||||
|
||||
if (port->memtype == SPA_DATA_DmaBuf ||
|
||||
port->memtype == SPA_DATA_MemFd) {
|
||||
d[j].fd = libcamera_get_fd(port->dev.camera, i, j);
|
||||
spa_log_info(impl->log, "Got fd = %ld for buffer: #%d", d[j].fd, i);
|
||||
d[j].data = NULL;
|
||||
SPA_FLAG_SET(b->flags, BUFFER_FLAG_ALLOCATED);
|
||||
}
|
||||
else if(port->memtype == SPA_DATA_MemPtr) {
|
||||
d[j].fd = -1;
|
||||
d[j].data = mmap(NULL,
|
||||
d[j].maxsize + d[j].mapoffset,
|
||||
PROT_READ, MAP_SHARED,
|
||||
libcamera_get_fd(port->dev.camera, i, j),
|
||||
0);
|
||||
if (d[j].data == MAP_FAILED) {
|
||||
spa_log_error(impl->log, "mmap: %m");
|
||||
continue;
|
||||
}
|
||||
b->ptr = d[j].data;
|
||||
SPA_FLAG_SET(b->flags, BUFFER_FLAG_MAPPED);
|
||||
spa_log_info(impl->log, "mmap ptr:%p", d[j].data);
|
||||
} else {
|
||||
spa_log_error(impl->log, "invalid buffer type");
|
||||
return -EIO;
|
||||
}
|
||||
}
|
||||
|
||||
spa_libcamera_buffer_recycle(impl, i);
|
||||
}
|
||||
port->n_buffers = libcamera_nbuffers;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
spa_libcamera_alloc_buffers(struct impl *impl,
|
||||
struct spa_buffer **buffers,
|
||||
uint32_t n_buffers)
|
||||
{
|
||||
int res;
|
||||
struct port *port = &impl->out_ports[0];
|
||||
|
||||
if (port->n_buffers > 0)
|
||||
return -EIO;
|
||||
|
||||
if ((res = mmap_init(impl, buffers, n_buffers)) < 0) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int spa_libcamera_stream_on(struct impl *impl)
|
||||
{
|
||||
struct port *port = &impl->out_ports[0];
|
||||
|
||||
if (!port->have_format) {
|
||||
spa_log_error(impl->log, "Exting %s with -EIO", __FUNCTION__);
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
if (impl->active)
|
||||
return 0;
|
||||
|
||||
spa_log_info(impl->log, "connecting camera");
|
||||
|
||||
// libcamera_connect(dev->camera);
|
||||
|
||||
// libcamera_start_capture(dev->camera);
|
||||
|
||||
port->source.func = libcamera_on_fd_events;
|
||||
port->source.data = impl;
|
||||
port->source.fd = spa_system_eventfd_create(impl->system, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
|
||||
port->source.mask = SPA_IO_IN | SPA_IO_ERR;
|
||||
port->source.rmask = 0;
|
||||
if (port->source.fd < 0) {
|
||||
spa_log_error(impl->log, "Failed to create eventfd. Exting %s with -EIO", __FUNCTION__);
|
||||
} else {
|
||||
spa_loop_add_source(impl->data_loop, &port->source);
|
||||
impl->have_source = true;
|
||||
|
||||
// libcamera_set_spa_system(dev->camera, impl->system);
|
||||
// libcamera_set_eventfd(dev->camera, port->source.fd);
|
||||
}
|
||||
|
||||
impl->active = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_remove_source(struct spa_loop *loop,
|
||||
bool async,
|
||||
uint32_t seq,
|
||||
const void *data,
|
||||
size_t size,
|
||||
void *user_data)
|
||||
{
|
||||
struct port *port = (struct port *)user_data;
|
||||
if (port->source.loop)
|
||||
spa_loop_remove_source(loop, &port->source);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int spa_libcamera_stream_off(struct impl *impl)
|
||||
{
|
||||
struct port *port = &impl->out_ports[0];
|
||||
|
||||
if (!impl->active)
|
||||
return 0;
|
||||
|
||||
spa_log_info(impl->log, "stopping camera");
|
||||
|
||||
// libcamera_stop_capture(dev->camera);
|
||||
|
||||
spa_log_info(impl->log, "disconnecting camera");
|
||||
|
||||
// libcamera_disconnect(dev->camera);
|
||||
|
||||
spa_loop_invoke(impl->data_loop, do_remove_source, 0, NULL, 0, true, port);
|
||||
|
||||
spa_list_init(&port->queue);
|
||||
impl->active = false;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -27,9 +27,7 @@
|
|||
#include <spa/support/plugin.h>
|
||||
#include <spa/support/log.h>
|
||||
|
||||
extern const struct spa_handle_factory spa_libcamera_source_factory;
|
||||
extern const struct spa_handle_factory spa_libcamera_client_factory;
|
||||
extern const struct spa_handle_factory spa_libcamera_device_factory;
|
||||
#include "libcamera.h"
|
||||
|
||||
struct spa_log_topic log_topic = SPA_LOG_TOPIC(0, "spa.libcamera");
|
||||
struct spa_log_topic *libcamera_log_topic = &log_topic;
|
||||
|
@ -43,13 +41,13 @@ int spa_handle_factory_enum(const struct spa_handle_factory **factory,
|
|||
|
||||
switch (*index) {
|
||||
case 0:
|
||||
*factory = &spa_libcamera_source_factory;
|
||||
*factory = &spa_libcamera_manager_factory;
|
||||
break;
|
||||
case 1:
|
||||
*factory = &spa_libcamera_client_factory;
|
||||
*factory = &spa_libcamera_device_factory;
|
||||
break;
|
||||
case 2:
|
||||
*factory = &spa_libcamera_device_factory;
|
||||
*factory = &spa_libcamera_source_factory;
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
|
|
|
@ -27,6 +27,15 @@
|
|||
#include <linux/media.h>
|
||||
|
||||
#include <spa/support/log.h>
|
||||
#include <spa/support/system.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
extern const struct spa_handle_factory spa_libcamera_source_factory;
|
||||
extern const struct spa_handle_factory spa_libcamera_manager_factory;
|
||||
extern const struct spa_handle_factory spa_libcamera_device_factory;
|
||||
|
||||
#undef SPA_LOG_TOPIC_DEFAULT
|
||||
#define SPA_LOG_TOPIC_DEFAULT libcamera_log_topic
|
||||
|
@ -37,18 +46,6 @@ static inline void libcamera_log_topic_init(struct spa_log *log)
|
|||
spa_log_topic_init(log, libcamera_log_topic);
|
||||
}
|
||||
|
||||
#include "libcamera_wrapper.h"
|
||||
|
||||
struct spa_libcamera_device {
|
||||
struct spa_log *log;
|
||||
int fd;
|
||||
struct media_device_info dev_info;
|
||||
unsigned int active:1;
|
||||
unsigned int have_format:1;
|
||||
LibCamera *camera;
|
||||
};
|
||||
|
||||
int spa_libcamera_open(struct spa_libcamera_device *dev);
|
||||
int spa_libcamera_close(struct spa_libcamera_device *dev);
|
||||
int spa_libcamera_is_capture(struct spa_libcamera_device *dev);
|
||||
int get_dev_fd(struct spa_libcamera_device *dev);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
libcamera_sources = [
|
||||
'libcamera.c',
|
||||
'libcamera-device.c',
|
||||
'libcamera-client.c',
|
||||
'libcamera-source.c',
|
||||
'libcamera_wrapper.cpp'
|
||||
'libcamera-manager.cpp',
|
||||
'libcamera-device.cpp',
|
||||
'libcamera-source.cpp'
|
||||
]
|
||||
|
||||
libdrm_dep = dependency('libdrm', version : '>= 2.4.98',
|
||||
|
|
Loading…
Reference in a new issue