2021-11-02 17:24:19 +01:00
|
|
|
/* Spa libcamera Device
|
2020-04-20 12:26:50 +05:30
|
|
|
*
|
|
|
|
|
* Copyright (C) 2020, Collabora Ltd.
|
|
|
|
|
* Author: Raghavendra Rao Sidlagatta <raghavendra.rao@collabora.com>
|
2021-11-02 17:24:19 +01:00
|
|
|
* Copyright (C) 2021 Wim Taymans <wim.taymans@gmail.com>
|
2020-04-20 12:26:50 +05:30
|
|
|
*
|
2021-11-02 17:24:19 +01:00
|
|
|
* libcamera-device.cpp
|
2020-04-20 12:26:50 +05:30
|
|
|
*
|
|
|
|
|
* 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 <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
|
|
#include <spa/support/plugin.h>
|
|
|
|
|
#include <spa/support/log.h>
|
|
|
|
|
#include <spa/support/loop.h>
|
|
|
|
|
#include <spa/utils/keys.h>
|
2021-11-02 17:24:19 +01:00
|
|
|
#include <spa/utils/result.h>
|
2020-04-20 12:26:50 +05:30
|
|
|
#include <spa/utils/names.h>
|
2021-05-18 11:36:13 +10:00
|
|
|
#include <spa/utils/string.h>
|
2020-04-20 12:26:50 +05:30
|
|
|
#include <spa/node/node.h>
|
|
|
|
|
#include <spa/pod/builder.h>
|
|
|
|
|
#include <spa/monitor/device.h>
|
|
|
|
|
#include <spa/monitor/utils.h>
|
2023-01-18 13:00:06 +01:00
|
|
|
#include <spa/param/param.h>
|
2020-04-20 12:26:50 +05:30
|
|
|
|
|
|
|
|
#include "libcamera.h"
|
2021-11-02 17:24:19 +01:00
|
|
|
#include "libcamera-manager.hpp"
|
2020-04-20 12:26:50 +05:30
|
|
|
|
2021-11-02 17:24:19 +01:00
|
|
|
#include <libcamera/camera.h>
|
|
|
|
|
#include <libcamera/property_ids.h>
|
|
|
|
|
|
|
|
|
|
using namespace libcamera;
|
2020-04-20 12:26:50 +05:30
|
|
|
|
spa: libcamera: properly construct/deconstruct libcamera device impl
Previously, the "impl" object was never properly constructed or
destructed, but it more or less worked out since the memory was initialized
to zero bytes and each member had trivial constructors. Except std::shared_ptr,
but an all zero storage happened to be equivalent to a default constructed
shared_ptr.
However, there was the still the problem that the shared_ptr was never
destructed, so it kept the referenced `Camera` object alive, which lead
to memory leaks.
An additional, somewhat unrelated change is that the "props" struct
is removed, and the device identifier is now stored in an `std::string`.
The reason is that `CameraManager::get()` already takes a const std::string reference,
so an std::string must be constructed in any case, so we might as well
take advantage of that and use `std::string` in the "impl" object as well.
Furthermore, wrap the `impl` struct in an anonymous namespace
to avoid name resolution problems.
2022-09-03 19:55:49 +02:00
|
|
|
namespace {
|
2020-04-20 12:26:50 +05:30
|
|
|
|
|
|
|
|
struct impl {
|
|
|
|
|
struct spa_handle handle;
|
spa: libcamera: properly construct/deconstruct libcamera device impl
Previously, the "impl" object was never properly constructed or
destructed, but it more or less worked out since the memory was initialized
to zero bytes and each member had trivial constructors. Except std::shared_ptr,
but an all zero storage happened to be equivalent to a default constructed
shared_ptr.
However, there was the still the problem that the shared_ptr was never
destructed, so it kept the referenced `Camera` object alive, which lead
to memory leaks.
An additional, somewhat unrelated change is that the "props" struct
is removed, and the device identifier is now stored in an `std::string`.
The reason is that `CameraManager::get()` already takes a const std::string reference,
so an std::string must be constructed in any case, so we might as well
take advantage of that and use `std::string` in the "impl" object as well.
Furthermore, wrap the `impl` struct in an anonymous namespace
to avoid name resolution problems.
2022-09-03 19:55:49 +02:00
|
|
|
struct spa_device device = {};
|
2020-04-20 12:26:50 +05:30
|
|
|
|
|
|
|
|
struct spa_log *log;
|
|
|
|
|
|
spa: libcamera: properly construct/deconstruct libcamera device impl
Previously, the "impl" object was never properly constructed or
destructed, but it more or less worked out since the memory was initialized
to zero bytes and each member had trivial constructors. Except std::shared_ptr,
but an all zero storage happened to be equivalent to a default constructed
shared_ptr.
However, there was the still the problem that the shared_ptr was never
destructed, so it kept the referenced `Camera` object alive, which lead
to memory leaks.
An additional, somewhat unrelated change is that the "props" struct
is removed, and the device identifier is now stored in an `std::string`.
The reason is that `CameraManager::get()` already takes a const std::string reference,
so an std::string must be constructed in any case, so we might as well
take advantage of that and use `std::string` in the "impl" object as well.
Furthermore, wrap the `impl` struct in an anonymous namespace
to avoid name resolution problems.
2022-09-03 19:55:49 +02:00
|
|
|
std::string device_id;
|
2020-04-20 12:26:50 +05:30
|
|
|
|
|
|
|
|
struct spa_hook_list hooks;
|
|
|
|
|
|
2022-09-03 22:10:50 +02:00
|
|
|
std::shared_ptr<CameraManager> manager;
|
2021-11-02 17:24:19 +01:00
|
|
|
std::shared_ptr<Camera> camera;
|
spa: libcamera: properly construct/deconstruct libcamera device impl
Previously, the "impl" object was never properly constructed or
destructed, but it more or less worked out since the memory was initialized
to zero bytes and each member had trivial constructors. Except std::shared_ptr,
but an all zero storage happened to be equivalent to a default constructed
shared_ptr.
However, there was the still the problem that the shared_ptr was never
destructed, so it kept the referenced `Camera` object alive, which lead
to memory leaks.
An additional, somewhat unrelated change is that the "props" struct
is removed, and the device identifier is now stored in an `std::string`.
The reason is that `CameraManager::get()` already takes a const std::string reference,
so an std::string must be constructed in any case, so we might as well
take advantage of that and use `std::string` in the "impl" object as well.
Furthermore, wrap the `impl` struct in an anonymous namespace
to avoid name resolution problems.
2022-09-03 19:55:49 +02:00
|
|
|
|
|
|
|
|
impl(spa_log *log,
|
2022-09-03 22:10:50 +02:00
|
|
|
std::shared_ptr<CameraManager> manager,
|
spa: libcamera: properly construct/deconstruct libcamera device impl
Previously, the "impl" object was never properly constructed or
destructed, but it more or less worked out since the memory was initialized
to zero bytes and each member had trivial constructors. Except std::shared_ptr,
but an all zero storage happened to be equivalent to a default constructed
shared_ptr.
However, there was the still the problem that the shared_ptr was never
destructed, so it kept the referenced `Camera` object alive, which lead
to memory leaks.
An additional, somewhat unrelated change is that the "props" struct
is removed, and the device identifier is now stored in an `std::string`.
The reason is that `CameraManager::get()` already takes a const std::string reference,
so an std::string must be constructed in any case, so we might as well
take advantage of that and use `std::string` in the "impl" object as well.
Furthermore, wrap the `impl` struct in an anonymous namespace
to avoid name resolution problems.
2022-09-03 19:55:49 +02:00
|
|
|
std::shared_ptr<Camera> camera,
|
|
|
|
|
std::string device_id);
|
2020-04-20 12:26:50 +05:30
|
|
|
};
|
|
|
|
|
|
spa: libcamera: properly construct/deconstruct libcamera device impl
Previously, the "impl" object was never properly constructed or
destructed, but it more or less worked out since the memory was initialized
to zero bytes and each member had trivial constructors. Except std::shared_ptr,
but an all zero storage happened to be equivalent to a default constructed
shared_ptr.
However, there was the still the problem that the shared_ptr was never
destructed, so it kept the referenced `Camera` object alive, which lead
to memory leaks.
An additional, somewhat unrelated change is that the "props" struct
is removed, and the device identifier is now stored in an `std::string`.
The reason is that `CameraManager::get()` already takes a const std::string reference,
so an std::string must be constructed in any case, so we might as well
take advantage of that and use `std::string` in the "impl" object as well.
Furthermore, wrap the `impl` struct in an anonymous namespace
to avoid name resolution problems.
2022-09-03 19:55:49 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-20 03:14:20 +02:00
|
|
|
static std::string cameraModel(const Camera *camera)
|
2021-11-02 17:24:19 +01:00
|
|
|
{
|
|
|
|
|
const ControlList &props = camera->properties();
|
2022-07-20 03:11:58 +02:00
|
|
|
|
|
|
|
|
if (auto model = props.get(properties::Model))
|
|
|
|
|
return std::move(model.value());
|
|
|
|
|
|
|
|
|
|
return camera->id();
|
2021-11-02 17:24:19 +01:00
|
|
|
}
|
|
|
|
|
|
2022-07-20 03:15:54 +02:00
|
|
|
static const char *cameraLoc(const Camera *camera)
|
2022-05-26 13:37:37 +03:00
|
|
|
{
|
|
|
|
|
const ControlList &props = camera->properties();
|
2022-07-20 03:11:58 +02:00
|
|
|
|
|
|
|
|
if (auto location = props.get(properties::Location)) {
|
|
|
|
|
switch (location.value()) {
|
2022-05-26 13:37:37 +03:00
|
|
|
case properties::CameraLocationFront:
|
2022-07-20 03:11:58 +02:00
|
|
|
return "front";
|
2022-05-26 13:37:37 +03:00
|
|
|
case properties::CameraLocationBack:
|
2022-07-20 03:11:58 +02:00
|
|
|
return "back";
|
2022-05-26 13:37:37 +03:00
|
|
|
case properties::CameraLocationExternal:
|
2022-07-20 03:11:58 +02:00
|
|
|
return "external";
|
2022-05-26 13:37:37 +03:00
|
|
|
}
|
|
|
|
|
}
|
2022-07-20 03:11:58 +02:00
|
|
|
|
2022-07-20 03:19:11 +02:00
|
|
|
return nullptr;
|
2022-05-26 13:37:37 +03:00
|
|
|
}
|
2021-11-02 17:24:19 +01:00
|
|
|
|
|
|
|
|
static int emit_info(struct impl *impl, bool full)
|
2020-04-20 12:26:50 +05:30
|
|
|
{
|
|
|
|
|
struct spa_dict_item items[10];
|
2021-11-02 17:24:19 +01:00
|
|
|
struct spa_dict dict;
|
2020-04-20 12:26:50 +05:30
|
|
|
uint32_t n_items = 0;
|
|
|
|
|
struct spa_device_info info;
|
|
|
|
|
struct spa_param_info params[2];
|
2022-07-20 03:19:11 +02:00
|
|
|
char path[256], model[256], name[256];
|
2020-04-20 12:26:50 +05:30
|
|
|
|
|
|
|
|
info = SPA_DEVICE_INFO_INIT();
|
|
|
|
|
|
|
|
|
|
info.change_mask = SPA_DEVICE_CHANGE_MASK_PROPS;
|
|
|
|
|
|
|
|
|
|
#define ADD_ITEM(key, value) items[n_items++] = SPA_DICT_ITEM_INIT(key, value)
|
spa: libcamera: properly construct/deconstruct libcamera device impl
Previously, the "impl" object was never properly constructed or
destructed, but it more or less worked out since the memory was initialized
to zero bytes and each member had trivial constructors. Except std::shared_ptr,
but an all zero storage happened to be equivalent to a default constructed
shared_ptr.
However, there was the still the problem that the shared_ptr was never
destructed, so it kept the referenced `Camera` object alive, which lead
to memory leaks.
An additional, somewhat unrelated change is that the "props" struct
is removed, and the device identifier is now stored in an `std::string`.
The reason is that `CameraManager::get()` already takes a const std::string reference,
so an std::string must be constructed in any case, so we might as well
take advantage of that and use `std::string` in the "impl" object as well.
Furthermore, wrap the `impl` struct in an anonymous namespace
to avoid name resolution problems.
2022-09-03 19:55:49 +02:00
|
|
|
snprintf(path, sizeof(path), "libcamera:%s", impl->device_id.c_str());
|
2020-04-20 12:26:50 +05:30
|
|
|
ADD_ITEM(SPA_KEY_OBJECT_PATH, path);
|
|
|
|
|
ADD_ITEM(SPA_KEY_DEVICE_API, "libcamera");
|
|
|
|
|
ADD_ITEM(SPA_KEY_MEDIA_CLASS, "Video/Device");
|
spa: libcamera: properly construct/deconstruct libcamera device impl
Previously, the "impl" object was never properly constructed or
destructed, but it more or less worked out since the memory was initialized
to zero bytes and each member had trivial constructors. Except std::shared_ptr,
but an all zero storage happened to be equivalent to a default constructed
shared_ptr.
However, there was the still the problem that the shared_ptr was never
destructed, so it kept the referenced `Camera` object alive, which lead
to memory leaks.
An additional, somewhat unrelated change is that the "props" struct
is removed, and the device identifier is now stored in an `std::string`.
The reason is that `CameraManager::get()` already takes a const std::string reference,
so an std::string must be constructed in any case, so we might as well
take advantage of that and use `std::string` in the "impl" object as well.
Furthermore, wrap the `impl` struct in an anonymous namespace
to avoid name resolution problems.
2022-09-03 19:55:49 +02:00
|
|
|
ADD_ITEM(SPA_KEY_API_LIBCAMERA_PATH, impl->device_id.c_str());
|
2022-07-20 03:19:11 +02:00
|
|
|
|
|
|
|
|
if (auto location = cameraLoc(impl->camera.get()))
|
|
|
|
|
ADD_ITEM(SPA_KEY_API_LIBCAMERA_LOCATION, location);
|
|
|
|
|
|
2022-05-26 19:18:35 +03:00
|
|
|
snprintf(model, sizeof(model), "%s", cameraModel(impl->camera.get()).c_str());
|
|
|
|
|
ADD_ITEM(SPA_KEY_DEVICE_PRODUCT_NAME, model);
|
|
|
|
|
ADD_ITEM(SPA_KEY_DEVICE_DESCRIPTION, model);
|
spa: libcamera: properly construct/deconstruct libcamera device impl
Previously, the "impl" object was never properly constructed or
destructed, but it more or less worked out since the memory was initialized
to zero bytes and each member had trivial constructors. Except std::shared_ptr,
but an all zero storage happened to be equivalent to a default constructed
shared_ptr.
However, there was the still the problem that the shared_ptr was never
destructed, so it kept the referenced `Camera` object alive, which lead
to memory leaks.
An additional, somewhat unrelated change is that the "props" struct
is removed, and the device identifier is now stored in an `std::string`.
The reason is that `CameraManager::get()` already takes a const std::string reference,
so an std::string must be constructed in any case, so we might as well
take advantage of that and use `std::string` in the "impl" object as well.
Furthermore, wrap the `impl` struct in an anonymous namespace
to avoid name resolution problems.
2022-09-03 19:55:49 +02:00
|
|
|
snprintf(name, sizeof(name), "libcamera_device.%s", impl->device_id.c_str());
|
2021-11-02 17:24:19 +01:00
|
|
|
ADD_ITEM(SPA_KEY_DEVICE_NAME, name);
|
2020-04-20 12:26:50 +05:30
|
|
|
#undef ADD_ITEM
|
2021-11-02 17:24:19 +01:00
|
|
|
|
|
|
|
|
dict = SPA_DICT_INIT(items, n_items);
|
|
|
|
|
info.props = &dict;
|
2020-04-20 12:26:50 +05:30
|
|
|
|
|
|
|
|
info.change_mask |= SPA_DEVICE_CHANGE_MASK_PARAMS;
|
|
|
|
|
params[0] = SPA_PARAM_INFO(SPA_PARAM_EnumProfile, SPA_PARAM_INFO_READ);
|
|
|
|
|
params[1] = SPA_PARAM_INFO(SPA_PARAM_Profile, SPA_PARAM_INFO_WRITE);
|
|
|
|
|
info.n_params = SPA_N_ELEMENTS(params);
|
|
|
|
|
info.params = params;
|
|
|
|
|
|
2021-11-02 17:24:19 +01:00
|
|
|
spa_device_emit_info(&impl->hooks, &info);
|
2020-04-20 12:26:50 +05:30
|
|
|
|
2021-11-02 17:24:19 +01:00
|
|
|
if (true) {
|
2020-04-20 12:26:50 +05:30
|
|
|
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;
|
2021-11-02 17:24:19 +01:00
|
|
|
oinfo.props = &dict;
|
2020-04-20 12:26:50 +05:30
|
|
|
|
2021-11-02 17:24:19 +01:00
|
|
|
spa_device_emit_object_info(&impl->hooks, 0, &oinfo);
|
2020-04-20 12:26:50 +05:30
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int impl_add_listener(void *object,
|
|
|
|
|
struct spa_hook *listener,
|
|
|
|
|
const struct spa_device_events *events,
|
|
|
|
|
void *data)
|
|
|
|
|
{
|
2021-11-02 17:24:19 +01:00
|
|
|
struct impl *impl = (struct impl*)object;
|
2020-04-20 12:26:50 +05:30
|
|
|
struct spa_hook_list save;
|
|
|
|
|
int res = 0;
|
|
|
|
|
|
2021-11-02 17:24:19 +01:00
|
|
|
spa_return_val_if_fail(impl != NULL, -EINVAL);
|
2020-04-20 12:26:50 +05:30
|
|
|
spa_return_val_if_fail(events != NULL, -EINVAL);
|
|
|
|
|
|
2021-11-02 17:24:19 +01:00
|
|
|
spa_hook_list_isolate(&impl->hooks, &save, listener, events, data);
|
2020-04-20 12:26:50 +05:30
|
|
|
|
|
|
|
|
if (events->info || events->object_info)
|
2021-11-02 17:24:19 +01:00
|
|
|
res = emit_info(impl, true);
|
2020-04-20 12:26:50 +05:30
|
|
|
|
2021-11-02 17:24:19 +01:00
|
|
|
spa_hook_list_join(&impl->hooks, &save);
|
2020-04-20 12:26:50 +05:30
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int impl_sync(void *object, int seq)
|
|
|
|
|
{
|
2021-11-02 17:24:19 +01:00
|
|
|
struct impl *impl = (struct impl*) object;
|
2020-04-20 12:26:50 +05:30
|
|
|
|
2021-11-02 17:24:19 +01:00
|
|
|
spa_return_val_if_fail(impl != NULL, -EINVAL);
|
2020-04-20 12:26:50 +05:30
|
|
|
|
2021-11-02 17:24:19 +01:00
|
|
|
spa_device_emit_result(&impl->hooks, seq, 0, 0, NULL);
|
2020-04-20 12:26:50 +05:30
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int impl_enum_params(void *object, int seq,
|
|
|
|
|
uint32_t id, uint32_t start, uint32_t num,
|
|
|
|
|
const struct spa_pod *filter)
|
|
|
|
|
{
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int impl_set_param(void *object,
|
|
|
|
|
uint32_t id, uint32_t flags,
|
|
|
|
|
const struct spa_pod *param)
|
|
|
|
|
{
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct spa_device_methods impl_device = {
|
|
|
|
|
SPA_VERSION_DEVICE_METHODS,
|
|
|
|
|
.add_listener = impl_add_listener,
|
|
|
|
|
.sync = impl_sync,
|
|
|
|
|
.enum_params = impl_enum_params,
|
|
|
|
|
.set_param = impl_set_param,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int impl_get_interface(struct spa_handle *handle, const char *type, void **interface)
|
|
|
|
|
{
|
2021-11-02 17:24:19 +01:00
|
|
|
struct impl *impl;
|
2020-04-20 12:26:50 +05:30
|
|
|
|
|
|
|
|
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
|
|
|
|
spa_return_val_if_fail(interface != NULL, -EINVAL);
|
|
|
|
|
|
2021-11-02 17:24:19 +01:00
|
|
|
impl = (struct impl *) handle;
|
2020-04-20 12:26:50 +05:30
|
|
|
|
2021-05-18 11:36:13 +10:00
|
|
|
if (spa_streq(type, SPA_TYPE_INTERFACE_Device))
|
2021-11-02 17:24:19 +01:00
|
|
|
*interface = &impl->device;
|
2020-04-20 12:26:50 +05:30
|
|
|
else
|
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int impl_clear(struct spa_handle *handle)
|
|
|
|
|
{
|
2022-09-03 22:10:50 +02:00
|
|
|
std::destroy_at(reinterpret_cast<impl *>(handle));
|
2020-04-20 12:26:50 +05:30
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-03 22:10:50 +02:00
|
|
|
impl::impl(spa_log *log,
|
|
|
|
|
std::shared_ptr<CameraManager> manager,
|
|
|
|
|
std::shared_ptr<Camera> camera,
|
|
|
|
|
std::string device_id)
|
spa: libcamera: properly construct/deconstruct libcamera device impl
Previously, the "impl" object was never properly constructed or
destructed, but it more or less worked out since the memory was initialized
to zero bytes and each member had trivial constructors. Except std::shared_ptr,
but an all zero storage happened to be equivalent to a default constructed
shared_ptr.
However, there was the still the problem that the shared_ptr was never
destructed, so it kept the referenced `Camera` object alive, which lead
to memory leaks.
An additional, somewhat unrelated change is that the "props" struct
is removed, and the device identifier is now stored in an `std::string`.
The reason is that `CameraManager::get()` already takes a const std::string reference,
so an std::string must be constructed in any case, so we might as well
take advantage of that and use `std::string` in the "impl" object as well.
Furthermore, wrap the `impl` struct in an anonymous namespace
to avoid name resolution problems.
2022-09-03 19:55:49 +02:00
|
|
|
: handle({ SPA_VERSION_HANDLE, impl_get_interface, impl_clear }),
|
|
|
|
|
log(log),
|
|
|
|
|
device_id(std::move(device_id)),
|
2022-09-03 22:10:50 +02:00
|
|
|
manager(std::move(manager)),
|
spa: libcamera: properly construct/deconstruct libcamera device impl
Previously, the "impl" object was never properly constructed or
destructed, but it more or less worked out since the memory was initialized
to zero bytes and each member had trivial constructors. Except std::shared_ptr,
but an all zero storage happened to be equivalent to a default constructed
shared_ptr.
However, there was the still the problem that the shared_ptr was never
destructed, so it kept the referenced `Camera` object alive, which lead
to memory leaks.
An additional, somewhat unrelated change is that the "props" struct
is removed, and the device identifier is now stored in an `std::string`.
The reason is that `CameraManager::get()` already takes a const std::string reference,
so an std::string must be constructed in any case, so we might as well
take advantage of that and use `std::string` in the "impl" object as well.
Furthermore, wrap the `impl` struct in an anonymous namespace
to avoid name resolution problems.
2022-09-03 19:55:49 +02:00
|
|
|
camera(std::move(camera))
|
|
|
|
|
{
|
|
|
|
|
libcamera_log_topic_init(log);
|
|
|
|
|
|
|
|
|
|
spa_hook_list_init(&hooks);
|
|
|
|
|
|
|
|
|
|
device.iface = SPA_INTERFACE_INIT(
|
|
|
|
|
SPA_TYPE_INTERFACE_Device,
|
|
|
|
|
SPA_VERSION_DEVICE,
|
|
|
|
|
&impl_device, this);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 12:26:50 +05:30
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
const char *str;
|
2021-11-02 17:24:19 +01:00
|
|
|
int res;
|
2020-04-20 12:26:50 +05:30
|
|
|
|
|
|
|
|
spa_return_val_if_fail(factory != NULL, -EINVAL);
|
|
|
|
|
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
|
|
|
|
|
spa: libcamera: properly construct/deconstruct libcamera device impl
Previously, the "impl" object was never properly constructed or
destructed, but it more or less worked out since the memory was initialized
to zero bytes and each member had trivial constructors. Except std::shared_ptr,
but an all zero storage happened to be equivalent to a default constructed
shared_ptr.
However, there was the still the problem that the shared_ptr was never
destructed, so it kept the referenced `Camera` object alive, which lead
to memory leaks.
An additional, somewhat unrelated change is that the "props" struct
is removed, and the device identifier is now stored in an `std::string`.
The reason is that `CameraManager::get()` already takes a const std::string reference,
so an std::string must be constructed in any case, so we might as well
take advantage of that and use `std::string` in the "impl" object as well.
Furthermore, wrap the `impl` struct in an anonymous namespace
to avoid name resolution problems.
2022-09-03 19:55:49 +02:00
|
|
|
auto log = static_cast<spa_log *>(spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Log));
|
2020-04-20 12:26:50 +05:30
|
|
|
|
2022-09-03 22:10:50 +02:00
|
|
|
auto manager = libcamera_manager_acquire(res);
|
spa: libcamera: properly construct/deconstruct libcamera device impl
Previously, the "impl" object was never properly constructed or
destructed, but it more or less worked out since the memory was initialized
to zero bytes and each member had trivial constructors. Except std::shared_ptr,
but an all zero storage happened to be equivalent to a default constructed
shared_ptr.
However, there was the still the problem that the shared_ptr was never
destructed, so it kept the referenced `Camera` object alive, which lead
to memory leaks.
An additional, somewhat unrelated change is that the "props" struct
is removed, and the device identifier is now stored in an `std::string`.
The reason is that `CameraManager::get()` already takes a const std::string reference,
so an std::string must be constructed in any case, so we might as well
take advantage of that and use `std::string` in the "impl" object as well.
Furthermore, wrap the `impl` struct in an anonymous namespace
to avoid name resolution problems.
2022-09-03 19:55:49 +02:00
|
|
|
if (!manager) {
|
|
|
|
|
spa_log_error(log, "can't start camera manager: %s", spa_strerror(res));
|
2022-09-03 19:03:11 +02:00
|
|
|
return res;
|
2021-11-02 17:24:19 +01:00
|
|
|
}
|
|
|
|
|
|
spa: libcamera: properly construct/deconstruct libcamera device impl
Previously, the "impl" object was never properly constructed or
destructed, but it more or less worked out since the memory was initialized
to zero bytes and each member had trivial constructors. Except std::shared_ptr,
but an all zero storage happened to be equivalent to a default constructed
shared_ptr.
However, there was the still the problem that the shared_ptr was never
destructed, so it kept the referenced `Camera` object alive, which lead
to memory leaks.
An additional, somewhat unrelated change is that the "props" struct
is removed, and the device identifier is now stored in an `std::string`.
The reason is that `CameraManager::get()` already takes a const std::string reference,
so an std::string must be constructed in any case, so we might as well
take advantage of that and use `std::string` in the "impl" object as well.
Furthermore, wrap the `impl` struct in an anonymous namespace
to avoid name resolution problems.
2022-09-03 19:55:49 +02:00
|
|
|
std::string device_id;
|
|
|
|
|
if (info && (str = spa_dict_lookup(info, SPA_KEY_API_LIBCAMERA_PATH)))
|
|
|
|
|
device_id = str;
|
|
|
|
|
|
|
|
|
|
auto camera = manager->get(device_id);
|
|
|
|
|
if (!camera) {
|
|
|
|
|
spa_log_error(log, "unknown camera id %s", device_id.c_str());
|
2021-11-02 17:24:19 +01:00
|
|
|
return -ENOENT;
|
|
|
|
|
}
|
spa: libcamera: properly construct/deconstruct libcamera device impl
Previously, the "impl" object was never properly constructed or
destructed, but it more or less worked out since the memory was initialized
to zero bytes and each member had trivial constructors. Except std::shared_ptr,
but an all zero storage happened to be equivalent to a default constructed
shared_ptr.
However, there was the still the problem that the shared_ptr was never
destructed, so it kept the referenced `Camera` object alive, which lead
to memory leaks.
An additional, somewhat unrelated change is that the "props" struct
is removed, and the device identifier is now stored in an `std::string`.
The reason is that `CameraManager::get()` already takes a const std::string reference,
so an std::string must be constructed in any case, so we might as well
take advantage of that and use `std::string` in the "impl" object as well.
Furthermore, wrap the `impl` struct in an anonymous namespace
to avoid name resolution problems.
2022-09-03 19:55:49 +02:00
|
|
|
|
2022-09-03 22:10:50 +02:00
|
|
|
new (handle) impl(log, std::move(manager), std::move(camera), std::move(device_id));
|
spa: libcamera: properly construct/deconstruct libcamera device impl
Previously, the "impl" object was never properly constructed or
destructed, but it more or less worked out since the memory was initialized
to zero bytes and each member had trivial constructors. Except std::shared_ptr,
but an all zero storage happened to be equivalent to a default constructed
shared_ptr.
However, there was the still the problem that the shared_ptr was never
destructed, so it kept the referenced `Camera` object alive, which lead
to memory leaks.
An additional, somewhat unrelated change is that the "props" struct
is removed, and the device identifier is now stored in an `std::string`.
The reason is that `CameraManager::get()` already takes a const std::string reference,
so an std::string must be constructed in any case, so we might as well
take advantage of that and use `std::string` in the "impl" object as well.
Furthermore, wrap the `impl` struct in an anonymous namespace
to avoid name resolution problems.
2022-09-03 19:55:49 +02:00
|
|
|
|
2020-04-20 12:26:50 +05:30
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-02 17:24:19 +01:00
|
|
|
extern "C" {
|
2020-04-20 12:26:50 +05:30
|
|
|
const struct spa_handle_factory spa_libcamera_device_factory = {
|
|
|
|
|
SPA_VERSION_HANDLE_FACTORY,
|
|
|
|
|
SPA_NAME_API_LIBCAMERA_DEVICE,
|
|
|
|
|
NULL,
|
|
|
|
|
impl_get_size,
|
|
|
|
|
impl_init,
|
|
|
|
|
impl_enum_interface_info,
|
|
|
|
|
};
|
2021-11-02 17:24:19 +01:00
|
|
|
}
|