mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-10-29 05:40:27 -04:00
Remove the done and error callbacks. The error callback is in an error message. The done callback is replace with spa_pending. Make enum_params take a callback and data for the results. This allows us to push the results one after another to the app and avoids ownership issues of the passed data. We can then extend this to handle the async case by doing a _wait call with a spa_pending+callback+data that will be called when the _enum_params returns and async result. Add a sync method. All methods can now return SPA_RESULT_IS_ASYNC return values and you can use spa_node_wait() to register a callback when they complete with optional extra parameters. This makes it easier to sync and handle the reply. Make helper methods to simulate the sync enum_params behaviour for sync nodes. Let the transport generate the sequence number for pw_resource_sync() and pw_proxy_sync(). That way we don't need to keep track of numbers ourselves and we can match the reply to the request easily.
304 lines
7.7 KiB
C
304 lines
7.7 KiB
C
/* Simple Plugin API
|
|
*
|
|
* Copyright © 2018 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 <error.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <dlfcn.h>
|
|
|
|
#include <spa/support/log-impl.h>
|
|
#include <spa/support/loop.h>
|
|
#include <spa/node/node.h>
|
|
#include <spa/node/utils.h>
|
|
#include <spa/pod/parser.h>
|
|
#include <spa/param/param.h>
|
|
#include <spa/param/format.h>
|
|
#include <spa/debug/dict.h>
|
|
#include <spa/debug/pod.h>
|
|
#include <spa/debug/format.h>
|
|
#include <spa/debug/types.h>
|
|
|
|
static SPA_LOG_IMPL(default_log);
|
|
|
|
struct data {
|
|
struct spa_support support[4];
|
|
uint32_t n_support;
|
|
struct spa_log *log;
|
|
struct spa_loop loop;
|
|
struct spa_node *node;
|
|
};
|
|
|
|
static void
|
|
inspect_node_params(struct data *data, struct spa_node *node)
|
|
{
|
|
int res;
|
|
uint32_t idx1, idx2;
|
|
|
|
for (idx1 = 0;;) {
|
|
uint32_t buffer[4096];
|
|
struct spa_pod_builder b = { 0 };
|
|
struct spa_pod *param;
|
|
uint32_t id;
|
|
|
|
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
|
if ((res = spa_node_enum_params_sync(node,
|
|
SPA_PARAM_List, &idx1,
|
|
NULL, ¶m, &b)) != 1) {
|
|
if (res != 0)
|
|
error(0, -res, "enum_params");
|
|
break;
|
|
}
|
|
|
|
spa_pod_parse_object(param,
|
|
SPA_TYPE_OBJECT_ParamList, NULL,
|
|
SPA_PARAM_LIST_id, SPA_POD_Id(&id));
|
|
|
|
printf("enumerating: %s:\n", spa_debug_type_find_name(spa_type_param, id));
|
|
for (idx2 = 0;;) {
|
|
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
|
if ((res = spa_node_enum_params_sync(node,
|
|
id, &idx2,
|
|
NULL, ¶m, &b)) != 1) {
|
|
if (res != 0)
|
|
error(0, -res, "enum_params %d", id);
|
|
break;
|
|
}
|
|
spa_debug_pod(0, NULL, param);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void
|
|
inspect_port_params(struct data *data, struct spa_node *node,
|
|
enum spa_direction direction, uint32_t port_id)
|
|
{
|
|
int res;
|
|
uint32_t idx1, idx2;
|
|
|
|
for (idx1 = 0;;) {
|
|
uint32_t buffer[4096];
|
|
struct spa_pod_builder b = { 0 };
|
|
struct spa_pod *param;
|
|
uint32_t id;
|
|
|
|
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
|
if ((res = spa_node_port_enum_params_sync(node,
|
|
direction, port_id,
|
|
SPA_PARAM_List, &idx1,
|
|
NULL, ¶m, &b)) != 1) {
|
|
if (res != 0)
|
|
error(0, -res, "port_enum_params");
|
|
break;
|
|
}
|
|
spa_pod_parse_object(param,
|
|
SPA_TYPE_OBJECT_ParamList, NULL,
|
|
SPA_PARAM_LIST_id, SPA_POD_Id(&id));
|
|
|
|
printf("enumerating: %s:\n", spa_debug_type_find_name(spa_type_param, id));
|
|
for (idx2 = 0;;) {
|
|
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
|
if ((res = spa_node_port_enum_params_sync(node,
|
|
direction, port_id,
|
|
id, &idx2,
|
|
NULL, ¶m, &b)) != 1) {
|
|
if (res != 0)
|
|
error(0, -res, "port_enum_params");
|
|
break;
|
|
}
|
|
|
|
if (spa_pod_is_object_type(param, SPA_TYPE_OBJECT_Format))
|
|
spa_debug_format(0, NULL, param);
|
|
else
|
|
spa_debug_pod(0, NULL, param);
|
|
}
|
|
}
|
|
}
|
|
|
|
static int node_info(void *_data, const struct spa_node_info *info)
|
|
{
|
|
struct data *data = _data;
|
|
|
|
printf("max input ports: %u\n", info->max_input_ports);
|
|
printf("max output ports: %u\n", info->max_output_ports);
|
|
|
|
if (info->change_mask & SPA_NODE_CHANGE_MASK_PROPS) {
|
|
printf("node properties:\n");
|
|
spa_debug_dict(2, info->props);
|
|
}
|
|
inspect_node_params(data, data->node);
|
|
return 0;
|
|
}
|
|
|
|
static int node_port_info(void *_data, enum spa_direction direction, uint32_t id,
|
|
const struct spa_port_info *info)
|
|
{
|
|
struct data *data = _data;
|
|
|
|
if (info == NULL) {
|
|
printf("port %d removed", id);
|
|
}
|
|
else {
|
|
printf(" %s port: %08x\n",
|
|
direction == SPA_DIRECTION_INPUT ? "input" : "output",
|
|
id);
|
|
inspect_port_params(data, data->node, direction, id);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static const struct spa_node_callbacks node_callbacks =
|
|
{
|
|
SPA_VERSION_NODE_CALLBACKS,
|
|
.info = node_info,
|
|
.port_info = node_port_info,
|
|
};
|
|
|
|
static void inspect_node(struct data *data, struct spa_node *node)
|
|
{
|
|
printf("node info:\n");
|
|
data->node = node;
|
|
spa_node_set_callbacks(node, &node_callbacks, data);
|
|
}
|
|
|
|
static void inspect_factory(struct data *data, const struct spa_handle_factory *factory)
|
|
{
|
|
int res;
|
|
struct spa_handle *handle;
|
|
void *interface;
|
|
const struct spa_interface_info *info;
|
|
uint32_t index;
|
|
|
|
printf("factory name:\t\t'%s'\n", factory->name);
|
|
printf("factory info:\n");
|
|
if (factory->info)
|
|
spa_debug_dict(2, factory->info);
|
|
else
|
|
printf(" none\n");
|
|
|
|
printf("factory interfaces:\n");
|
|
for (index = 0;;) {
|
|
if ((res = spa_handle_factory_enum_interface_info(factory, &info, &index)) <= 0) {
|
|
if (res == 0)
|
|
break;
|
|
else
|
|
error(0, -res, "spa_handle_factory_enum_interface_info");
|
|
}
|
|
printf(" interface: '%d'\n", info->type);
|
|
}
|
|
|
|
handle = calloc(1, spa_handle_factory_get_size(factory, NULL));
|
|
if ((res =
|
|
spa_handle_factory_init(factory, handle, NULL, data->support, data->n_support)) < 0) {
|
|
printf("can't make factory instance: %d\n", res);
|
|
return;
|
|
}
|
|
|
|
printf("factory instance:\n");
|
|
|
|
for (index = 0;;) {
|
|
if ((res = spa_handle_factory_enum_interface_info(factory, &info, &index)) <= 0) {
|
|
if (res == 0)
|
|
break;
|
|
else
|
|
error(0, -res, "spa_handle_factory_enum_interface_info");
|
|
}
|
|
printf(" interface: '%d'\n", info->type);
|
|
|
|
if ((res = spa_handle_get_interface(handle, info->type, &interface)) < 0) {
|
|
printf("can't get interface: %d %d\n", info->type, res);
|
|
continue;
|
|
}
|
|
|
|
if (info->type == SPA_TYPE_INTERFACE_Node)
|
|
inspect_node(data, interface);
|
|
else
|
|
printf("skipping unknown interface\n");
|
|
}
|
|
}
|
|
|
|
static int do_add_source(struct spa_loop *loop, struct spa_source *source)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
static int do_update_source(struct spa_source *source)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
static void do_remove_source(struct spa_source *source)
|
|
{
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
struct data data = { 0 };
|
|
int res;
|
|
void *handle;
|
|
spa_handle_factory_enum_func_t enum_func;
|
|
uint32_t index;
|
|
const char *str;
|
|
|
|
if (argc < 2) {
|
|
printf("usage: %s <plugin.so>\n", argv[0]);
|
|
return -1;
|
|
}
|
|
|
|
data.log = &default_log.log;
|
|
data.loop.version = SPA_VERSION_LOOP;
|
|
data.loop.add_source = do_add_source;
|
|
data.loop.update_source = do_update_source;
|
|
data.loop.remove_source = do_remove_source;
|
|
|
|
if ((str = getenv("SPA_DEBUG")))
|
|
data.log->level = atoi(str);
|
|
|
|
data.support[0] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_Log, data.log);
|
|
data.support[1] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_MainLoop, &data.loop);
|
|
data.support[2] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_DataLoop, &data.loop);
|
|
data.n_support = 3;
|
|
|
|
if ((handle = dlopen(argv[1], RTLD_NOW)) == NULL) {
|
|
printf("can't load %s\n", argv[1]);
|
|
return -1;
|
|
}
|
|
if ((enum_func = dlsym(handle, SPA_HANDLE_FACTORY_ENUM_FUNC_NAME)) == NULL) {
|
|
printf("can't find function\n");
|
|
return -1;
|
|
}
|
|
|
|
for (index = 0;;) {
|
|
const struct spa_handle_factory *factory;
|
|
|
|
if ((res = enum_func(&factory, &index)) <= 0) {
|
|
if (res != 0)
|
|
error(0, -res, "enum_func");
|
|
break;
|
|
}
|
|
inspect_factory(&data, factory);
|
|
}
|
|
return 0;
|
|
}
|