2023-02-08 18:12:00 +01:00
|
|
|
/* PipeWire */
|
|
|
|
|
/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
|
|
|
|
|
/* SPDX-License-Identifier: MIT */
|
2019-07-02 17:34:26 +02:00
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
2019-07-10 18:04:59 +02:00
|
|
|
#include <math.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
2019-07-02 17:34:26 +02:00
|
|
|
|
|
|
|
|
#include <spa/node/node.h>
|
2019-07-25 12:19:41 +02:00
|
|
|
#include <spa/node/utils.h>
|
2019-07-10 18:04:59 +02:00
|
|
|
#include <spa/utils/hook.h>
|
2020-05-08 16:18:10 +02:00
|
|
|
#include <spa/utils/result.h>
|
2019-07-02 17:34:26 +02:00
|
|
|
#include <spa/utils/names.h>
|
2021-05-18 11:36:13 +10:00
|
|
|
#include <spa/utils/string.h>
|
2019-07-10 18:04:59 +02:00
|
|
|
#include <spa/utils/type-info.h>
|
2019-07-25 12:19:41 +02:00
|
|
|
#include <spa/param/format.h>
|
2022-01-20 18:06:19 +01:00
|
|
|
#include <spa/param/audio/format.h>
|
|
|
|
|
#include <spa/param/audio/format-utils.h>
|
2019-07-25 12:19:41 +02:00
|
|
|
#include <spa/param/format-utils.h>
|
|
|
|
|
#include <spa/debug/types.h>
|
2022-01-18 10:38:34 +01:00
|
|
|
#include <spa/utils/json-pod.h>
|
2019-07-02 17:34:26 +02:00
|
|
|
|
|
|
|
|
#include "pipewire/pipewire.h"
|
|
|
|
|
|
|
|
|
|
#include "modules/spa/spa-node.h"
|
|
|
|
|
|
2019-07-25 12:19:41 +02:00
|
|
|
#define NAME "adapter"
|
2019-07-02 17:34:26 +02:00
|
|
|
|
2021-09-16 15:35:10 +10:00
|
|
|
PW_LOG_TOPIC_EXTERN(mod_topic);
|
|
|
|
|
#define PW_LOG_TOPIC_DEFAULT mod_topic
|
|
|
|
|
|
2019-07-02 17:34:26 +02:00
|
|
|
struct buffer {
|
|
|
|
|
struct spa_buffer buf;
|
|
|
|
|
struct spa_data datas[1];
|
|
|
|
|
struct spa_chunk chunk[1];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct node {
|
2019-12-10 18:19:56 +01:00
|
|
|
struct pw_context *context;
|
2019-07-02 17:34:26 +02:00
|
|
|
|
2019-12-11 12:41:45 +01:00
|
|
|
struct pw_impl_node *node;
|
2019-07-02 17:34:26 +02:00
|
|
|
struct spa_hook node_listener;
|
|
|
|
|
|
2022-05-11 12:28:26 +02:00
|
|
|
struct spa_node *follower;
|
2019-07-12 09:57:31 +02:00
|
|
|
|
2019-07-10 18:04:59 +02:00
|
|
|
void *user_data;
|
|
|
|
|
enum pw_direction direction;
|
|
|
|
|
struct pw_properties *props;
|
2019-07-02 17:34:26 +02:00
|
|
|
|
2019-07-25 12:19:41 +02:00
|
|
|
uint32_t media_type;
|
|
|
|
|
uint32_t media_subtype;
|
2019-07-02 17:34:26 +02:00
|
|
|
|
|
|
|
|
struct spa_list ports;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** \endcond */
|
2019-07-23 17:37:48 +02:00
|
|
|
static void node_free(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct node *n = data;
|
2020-11-06 15:53:32 +01:00
|
|
|
spa_hook_remove(&n->node_listener);
|
2019-07-12 09:57:31 +02:00
|
|
|
pw_properties_free(n->props);
|
2019-07-10 18:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
2019-12-11 12:41:45 +01:00
|
|
|
static const struct pw_impl_node_events node_events = {
|
|
|
|
|
PW_VERSION_IMPL_NODE_EVENTS,
|
2019-07-23 17:37:48 +02:00
|
|
|
.free = node_free,
|
2019-07-02 17:34:26 +02:00
|
|
|
};
|
|
|
|
|
|
2022-05-11 12:28:26 +02:00
|
|
|
static int find_format(struct spa_node *node, enum pw_direction direction,
|
2019-07-25 12:19:41 +02:00
|
|
|
uint32_t *media_type, uint32_t *media_subtype)
|
|
|
|
|
{
|
|
|
|
|
uint32_t state = 0;
|
|
|
|
|
uint8_t buffer[4096];
|
|
|
|
|
struct spa_pod_builder b;
|
|
|
|
|
int res;
|
|
|
|
|
struct spa_pod *format;
|
|
|
|
|
|
|
|
|
|
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
2022-05-11 12:28:26 +02:00
|
|
|
if ((res = spa_node_port_enum_params_sync(node,
|
2019-07-25 12:19:41 +02:00
|
|
|
direction == PW_DIRECTION_INPUT ?
|
|
|
|
|
SPA_DIRECTION_INPUT :
|
|
|
|
|
SPA_DIRECTION_OUTPUT, 0,
|
|
|
|
|
SPA_PARAM_EnumFormat, &state,
|
|
|
|
|
NULL, &format, &b)) != 1) {
|
2021-02-26 20:45:25 +01:00
|
|
|
res = res < 0 ? res : -ENOENT;
|
2021-09-16 15:35:10 +10:00
|
|
|
pw_log_warn("%p: can't get format: %s", node, spa_strerror(res));
|
2021-02-26 20:45:25 +01:00
|
|
|
return res;
|
2019-07-25 12:19:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((res = spa_format_parse(format, media_type, media_subtype)) < 0)
|
|
|
|
|
return res;
|
|
|
|
|
|
2021-09-16 15:35:10 +10:00
|
|
|
pw_log_debug("%p: %s/%s", node,
|
2019-07-25 12:19:41 +02:00
|
|
|
spa_debug_type_find_name(spa_type_media_type, *media_type),
|
|
|
|
|
spa_debug_type_find_name(spa_type_media_subtype, *media_subtype));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-11 12:28:26 +02:00
|
|
|
struct info_data {
|
|
|
|
|
struct spa_hook listener;
|
|
|
|
|
struct spa_node *node;
|
|
|
|
|
struct pw_properties *props;
|
|
|
|
|
uint32_t n_input_ports;
|
|
|
|
|
uint32_t max_input_ports;
|
|
|
|
|
uint32_t n_output_ports;
|
|
|
|
|
uint32_t max_output_ports;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void info_event(void *data, const struct spa_node_info *info)
|
|
|
|
|
{
|
|
|
|
|
struct info_data *d = data;
|
|
|
|
|
|
|
|
|
|
pw_properties_update(d->props, info->props);
|
|
|
|
|
|
|
|
|
|
d->max_input_ports = info->max_input_ports;
|
|
|
|
|
d->max_output_ports = info->max_output_ports;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void port_info_event(void *data, enum spa_direction direction, uint32_t port,
|
|
|
|
|
const struct spa_port_info *info)
|
|
|
|
|
{
|
|
|
|
|
struct info_data *d = data;
|
|
|
|
|
|
|
|
|
|
if (direction == SPA_DIRECTION_OUTPUT)
|
|
|
|
|
d->n_output_ports++;
|
|
|
|
|
else if (direction == SPA_DIRECTION_INPUT)
|
|
|
|
|
d->n_input_ports++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct spa_node_events node_info_events = {
|
|
|
|
|
.version = SPA_VERSION_NODE_EVENTS,
|
|
|
|
|
.info = info_event,
|
|
|
|
|
.port_info = port_info_event,
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-11 12:41:45 +01:00
|
|
|
struct pw_impl_node *pw_adapter_new(struct pw_context *context,
|
2022-05-11 12:28:26 +02:00
|
|
|
struct spa_node *follower,
|
2019-07-10 18:04:59 +02:00
|
|
|
struct pw_properties *props,
|
|
|
|
|
size_t user_data_size)
|
2019-07-02 17:34:26 +02:00
|
|
|
{
|
2019-12-11 12:41:45 +01:00
|
|
|
struct pw_impl_node *node;
|
2019-07-10 18:04:59 +02:00
|
|
|
struct node *n;
|
2019-08-12 14:47:16 +02:00
|
|
|
const char *str, *factory_name;
|
2019-07-10 18:04:59 +02:00
|
|
|
enum pw_direction direction;
|
2019-07-25 12:19:41 +02:00
|
|
|
int res;
|
|
|
|
|
uint32_t media_type, media_subtype;
|
2022-05-11 12:28:26 +02:00
|
|
|
struct info_data info;
|
2019-07-02 17:34:26 +02:00
|
|
|
|
2022-05-11 12:28:26 +02:00
|
|
|
spa_zero(info);
|
|
|
|
|
info.node = follower;
|
|
|
|
|
info.props = props;
|
|
|
|
|
|
|
|
|
|
res = spa_node_add_listener(info.node, &info.listener, &node_info_events, &info);
|
|
|
|
|
if (res < 0)
|
2019-07-25 12:19:41 +02:00
|
|
|
goto error;
|
2019-07-02 17:34:26 +02:00
|
|
|
|
2022-05-11 12:28:26 +02:00
|
|
|
spa_hook_remove(&info.listener);
|
2019-07-25 12:19:41 +02:00
|
|
|
|
2022-05-11 12:28:26 +02:00
|
|
|
pw_log_debug("%p: in %d/%d out %d/%d", info.node,
|
|
|
|
|
info.n_input_ports, info.max_input_ports,
|
|
|
|
|
info.n_output_ports, info.max_output_ports);
|
2019-07-15 17:12:51 +02:00
|
|
|
|
2022-05-11 12:28:26 +02:00
|
|
|
if (info.n_output_ports > 0) {
|
2019-07-10 18:04:59 +02:00
|
|
|
direction = PW_DIRECTION_OUTPUT;
|
2022-05-11 12:28:26 +02:00
|
|
|
} else if (info.n_input_ports > 0) {
|
2019-07-25 12:19:41 +02:00
|
|
|
direction = PW_DIRECTION_INPUT;
|
|
|
|
|
} else {
|
|
|
|
|
res = -EINVAL;
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
2019-07-02 17:34:26 +02:00
|
|
|
|
2019-07-10 18:04:59 +02:00
|
|
|
if ((str = pw_properties_get(props, PW_KEY_NODE_ID)) != NULL)
|
|
|
|
|
pw_properties_set(props, PW_KEY_NODE_SESSION, str);
|
2019-07-02 17:34:26 +02:00
|
|
|
|
2020-05-20 13:52:10 +02:00
|
|
|
if (pw_properties_get(props, "factory.mode") == NULL) {
|
|
|
|
|
if (direction == PW_DIRECTION_INPUT)
|
2019-07-12 15:11:03 +02:00
|
|
|
str = "merge";
|
2020-05-20 13:52:10 +02:00
|
|
|
else
|
2019-07-12 15:11:03 +02:00
|
|
|
str = "split";
|
|
|
|
|
pw_properties_set(props, "factory.mode", str);
|
2019-07-10 18:04:59 +02:00
|
|
|
}
|
2019-07-02 17:34:26 +02:00
|
|
|
|
2020-02-21 10:47:32 +01:00
|
|
|
if ((res = find_format(follower, direction, &media_type, &media_subtype)) < 0)
|
2019-07-25 12:19:41 +02:00
|
|
|
goto error;
|
2019-07-02 17:34:26 +02:00
|
|
|
|
2019-07-25 12:19:41 +02:00
|
|
|
if (media_type == SPA_MEDIA_TYPE_audio) {
|
2022-05-11 12:28:26 +02:00
|
|
|
pw_properties_setf(props, "audio.adapt.follower", "pointer:%p", follower);
|
2019-07-25 12:19:41 +02:00
|
|
|
pw_properties_set(props, SPA_KEY_LIBRARY_NAME, "audioconvert/libspa-audioconvert");
|
|
|
|
|
if (pw_properties_get(props, PW_KEY_MEDIA_CLASS) == NULL)
|
|
|
|
|
pw_properties_setf(props, PW_KEY_MEDIA_CLASS, "Audio/%s",
|
|
|
|
|
direction == PW_DIRECTION_INPUT ? "Sink" : "Source");
|
|
|
|
|
factory_name = SPA_NAME_AUDIO_ADAPT;
|
|
|
|
|
}
|
|
|
|
|
else if (media_type == SPA_MEDIA_TYPE_video) {
|
2022-05-11 12:28:26 +02:00
|
|
|
pw_properties_setf(props, "video.adapt.follower", "pointer:%p", follower);
|
2019-07-25 12:19:41 +02:00
|
|
|
pw_properties_set(props, SPA_KEY_LIBRARY_NAME, "videoconvert/libspa-videoconvert");
|
|
|
|
|
if (pw_properties_get(props, PW_KEY_MEDIA_CLASS) == NULL)
|
|
|
|
|
pw_properties_setf(props, PW_KEY_MEDIA_CLASS, "Video/%s",
|
|
|
|
|
direction == PW_DIRECTION_INPUT ? "Sink" : "Source");
|
|
|
|
|
factory_name = SPA_NAME_VIDEO_ADAPT;
|
|
|
|
|
} else {
|
|
|
|
|
res = -ENOTSUP;
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
2019-07-02 17:34:26 +02:00
|
|
|
|
2019-12-10 18:19:56 +01:00
|
|
|
node = pw_spa_node_load(context,
|
2019-07-25 12:19:41 +02:00
|
|
|
factory_name,
|
|
|
|
|
PW_SPA_NODE_FLAG_ACTIVATE | PW_SPA_NODE_FLAG_NO_REGISTER,
|
2024-02-13 16:01:10 +01:00
|
|
|
pw_properties_copy(props), sizeof(struct node) + user_data_size);
|
2019-07-10 18:04:59 +02:00
|
|
|
if (node == NULL) {
|
2019-07-02 17:34:26 +02:00
|
|
|
res = -errno;
|
2019-07-10 18:04:59 +02:00
|
|
|
pw_log_error("can't load spa node: %m");
|
|
|
|
|
goto error;
|
2019-07-02 17:34:26 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-10 18:04:59 +02:00
|
|
|
n = pw_spa_node_get_user_data(node);
|
2019-12-10 18:19:56 +01:00
|
|
|
n->context = context;
|
2019-07-10 18:04:59 +02:00
|
|
|
n->node = node;
|
2020-02-21 10:47:32 +01:00
|
|
|
n->follower = follower;
|
2019-07-10 18:04:59 +02:00
|
|
|
n->direction = direction;
|
|
|
|
|
n->props = props;
|
2019-07-25 12:19:41 +02:00
|
|
|
n->media_type = media_type;
|
|
|
|
|
n->media_subtype = media_subtype;
|
2019-07-10 18:04:59 +02:00
|
|
|
spa_list_init(&n->ports);
|
|
|
|
|
|
|
|
|
|
if (user_data_size > 0)
|
2021-05-06 13:41:44 +10:00
|
|
|
n->user_data = SPA_PTROFF(n, sizeof(struct node), void);
|
2019-07-02 17:34:26 +02:00
|
|
|
|
2019-12-11 12:41:45 +01:00
|
|
|
pw_impl_node_add_listener(node, &n->node_listener, &node_events, n);
|
2019-07-02 17:34:26 +02:00
|
|
|
|
2019-07-10 18:04:59 +02:00
|
|
|
return node;
|
2019-07-02 17:34:26 +02:00
|
|
|
|
2019-07-10 18:04:59 +02:00
|
|
|
error:
|
2021-06-01 11:21:17 +10:00
|
|
|
pw_properties_free(props);
|
2019-07-02 17:34:26 +02:00
|
|
|
errno = -res;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 12:41:45 +01:00
|
|
|
void *pw_adapter_get_user_data(struct pw_impl_node *node)
|
2019-07-02 17:34:26 +02:00
|
|
|
{
|
2019-07-10 18:04:59 +02:00
|
|
|
struct node *n = pw_spa_node_get_user_data(node);
|
|
|
|
|
return n->user_data;
|
2019-07-02 17:34:26 +02:00
|
|
|
}
|