PipeWire: export-spa.c

PipeWire 1.6.7

Loading...
Searching...
No Matches
export-spa.c

Exporting and loading a SPA node, using Core API.

Exporting and loading a SPA node, using Core API.

/* PipeWire */
/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
/* SPDX-License-Identifier: MIT */
/*
[title]
Exporting and loading a SPA node, using \ref api_pw_core.
[title]
*/
#include <stdio.h>
#include <sys/mman.h>
#include <signal.h>
#include <spa/utils/result.h>
#include <spa/param/video/format-utils.h>
#include <spa/param/props.h>
#include <pipewire/impl.h>
struct data {
struct pw_main_loop *loop;
struct pw_context *context;
struct pw_core *core;
struct spa_hook core_listener;
struct spa_node *node;
const char *library;
const char *factory;
const char *path;
struct pw_proxy *proxy;
struct spa_hook proxy_listener;
uint32_t id;
};
static void proxy_event_bound_props(void *_data, uint32_t global_id, const struct spa_dict *props)
{
struct data *data = _data;
if (data->id != global_id) {
printf("node id: %u\n", global_id);
data->id = global_id;
}
}
static const struct pw_proxy_events proxy_events = {
PW_VERSION_PROXY_EVENTS,
.bound_props = proxy_event_bound_props,
};
static int make_node(struct data *data)
{
struct pw_properties *props;
struct spa_handle *hndl;
void *iface;
int res;
props = pw_properties_new(SPA_KEY_LIBRARY_NAME, data->library,
SPA_KEY_FACTORY_NAME, data->factory,
NULL);
hndl = pw_context_load_spa_handle(data->context, data->factory, &props->dict);
if (hndl == NULL)
return -errno;
if ((res = spa_handle_get_interface(hndl, SPA_TYPE_INTERFACE_Node, &iface)) < 0)
return res;
data->node = iface;
if (data->path) {
pw_properties_set(props, PW_KEY_NODE_AUTOCONNECT, "true");
pw_properties_set(props, PW_KEY_TARGET_OBJECT, data->path);
}
data->proxy = pw_core_export(data->core,
SPA_TYPE_INTERFACE_Node, &props->dict,
data->node, 0);
pw_properties_free(props);
if (data->proxy == NULL)
return -errno;
pw_proxy_add_listener(data->proxy,
&data->proxy_listener, &proxy_events, data);
return 0;
}
static void on_core_error(void *data, uint32_t id, int seq, int res, const char *message)
{
struct data *d = data;
pw_log_error("error id:%u seq:%d res:%d (%s): %s",
id, seq, res, spa_strerror(res), message);
if (id == PW_ID_CORE)
pw_main_loop_quit(d->loop);
}
static const struct pw_core_events core_events = {
PW_VERSION_CORE_EVENTS,
.error = on_core_error,
};
static void do_quit(void *data, int signal_number)
{
struct data *d = data;
pw_main_loop_quit(d->loop);
}
int main(int argc, char *argv[])
{
struct data data = { 0, };
struct pw_loop *l;
pw_init(&argc, &argv);
if (argc < 3) {
fprintf(stderr, "usage: %s <library> <factory> [path]\n\n"
"\texample: %s v4l2/libspa-v4l2 api.v4l2.source\n\n",
argv[0], argv[0]);
return -1;
}
data.loop = pw_main_loop_new(NULL);
l = pw_main_loop_get_loop(data.loop);
pw_loop_add_signal(l, SIGINT, do_quit, &data);
pw_loop_add_signal(l, SIGTERM, do_quit, &data);
data.context = pw_context_new(l, NULL, 0);
data.library = argv[1];
data.factory = argv[2];
if (argc > 3)
data.path = argv[3];
pw_context_load_module(data.context, "libpipewire-module-spa-node-factory", NULL, NULL);
data.core = pw_context_connect(data.context, NULL, 0);
if (data.core == NULL) {
printf("can't connect: %m\n");
return -1;
}
pw_core_add_listener(data.core,
&data.core_listener,
&core_events, &data);
if (make_node(&data) < 0) {
pw_log_error("can't make node");
return -1;
}
pw_main_loop_run(data.loop);
pw_proxy_destroy(data.proxy);
pw_core_disconnect(data.core);
pw_context_destroy(data.context);
pw_main_loop_destroy(data.loop);
return 0;
}
pw_context_load_spa_handle
struct spa_handle * pw_context_load_spa_handle(struct pw_context *context, const char *factory_name, const struct spa_dict *info)
Load a SPA handle from a context.
Definition context.c:1883
pw_context_destroy
void pw_context_destroy(struct pw_context *context)
destroy a context object, all resources except the main_loop will be destroyed
Definition context.c:608
pw_context_new
struct pw_context * pw_context_new(struct pw_loop *main_loop, struct pw_properties *props, size_t user_data_size)
Make a new context object for a given main_loop.
Definition context.c:395
PW_ID_CORE
#define PW_ID_CORE
default ID for the core object after connect
Definition core.h:73
pw_context_connect
struct pw_core * pw_context_connect(struct pw_context *context, struct pw_properties *properties, size_t user_data_size)
Connect to a PipeWire instance.
Definition core.c:410
pw_core_add_listener
PW_API_CORE_IMPL int pw_core_add_listener(struct pw_core *object, struct spa_hook *listener, const struct pw_core_events *events, void *data)
Definition core.h:389
seq
uint32_t int seq
Definition core.h:432
pw_core_export
struct pw_proxy * pw_core_export(struct pw_core *core, const char *type, const struct spa_dict *props, void *object, size_t user_data_size)
Export an object into the PipeWire instance associated with core.
Definition core.c:285
pw_core_disconnect
int pw_core_disconnect(struct pw_core *core)
disconnect and destroy a core
Definition core.c:506
id
uint32_t id
Definition core.h:432
message
uint32_t int int const char * message
Definition core.h:433
res
uint32_t int int res
Definition core.h:433
PW_VERSION_CORE_EVENTS
#define PW_VERSION_CORE_EVENTS
Definition core.h:141
pw_context_load_module
struct pw_impl_module * pw_context_load_module(struct pw_context *context, const char *name, const char *args, struct pw_properties *properties)
Load a module.
Definition impl-module.c:139
PW_KEY_NODE_AUTOCONNECT
#define PW_KEY_NODE_AUTOCONNECT
node wants to be automatically connected to a compatible node
Definition keys.h:238
PW_KEY_TARGET_OBJECT
#define PW_KEY_TARGET_OBJECT
a target object to link to.
Definition keys.h:579
pw_log_error
#define pw_log_error(...)
Definition log.h:178
pw_loop_add_signal
PW_API_LOOP_IMPL struct spa_source * pw_loop_add_signal(struct pw_loop *object, int signal_number, spa_source_signal_func_t func, void *data)
Definition loop.h:177
pw_main_loop_quit
int pw_main_loop_quit(struct pw_main_loop *loop)
Quit a main loop.
Definition main-loop.c:108
pw_main_loop_destroy
void pw_main_loop_destroy(struct pw_main_loop *loop)
Destroy a loop.
Definition main-loop.c:73
pw_main_loop_run
int pw_main_loop_run(struct pw_main_loop *loop)
Run a main loop.
Definition main-loop.c:122
pw_main_loop_new
struct pw_main_loop * pw_main_loop_new(const struct spa_dict *props)
Create a new main loop.
Definition main-loop.c:63
pw_main_loop_get_loop
struct pw_loop * pw_main_loop_get_loop(struct pw_main_loop *loop)
Get the loop implementation.
Definition main-loop.c:96
pw_init
void pw_init(int *argc, char **argv[])
Initialize PipeWire.
Definition pipewire.c:499
pw_properties_free
void pw_properties_free(struct pw_properties *properties)
Free a properties object.
Definition properties.c:559
pw_properties_new
struct pw_properties * pw_properties_new(const char *key,...)
Make a new properties object.
Definition properties.c:97
pw_properties_set
int pw_properties_set(struct pw_properties *properties, const char *key, const char *value)
Set a property value.
Definition properties.c:586
pw_proxy_add_listener
void pw_proxy_add_listener(struct pw_proxy *proxy, struct spa_hook *listener, const struct pw_proxy_events *events, void *data)
Add an event listener to proxy.
Definition proxy.c:172
PW_VERSION_PROXY_EVENTS
#define PW_VERSION_PROXY_EVENTS
Definition proxy.h:100
pw_proxy_destroy
void pw_proxy_destroy(struct pw_proxy *proxy)
destroy a proxy
Definition proxy.c:209
SPA_KEY_FACTORY_NAME
#define SPA_KEY_FACTORY_NAME
the name of a factory
Definition plugin.h:242
spa_handle_get_interface
SPA_API_PLUGIN int spa_handle_get_interface(struct spa_handle *object, const char *type, void **iface)
Definition plugin.h:74
SPA_KEY_LIBRARY_NAME
#define SPA_KEY_LIBRARY_NAME
the name of a library.
Definition plugin.h:251
SPA_TYPE_INTERFACE_Node
#define SPA_TYPE_INTERFACE_Node
Definition node.h:47
impl.h
pipewire/impl.h
props.h
result.h
spa/utils/result.h
pw_context
pw_core_events
Core events.
Definition core.h:139
pw_core
pw_loop
Definition loop.h:33
pw_main_loop
A main loop object.
pw_properties
Definition properties.h:39
pw_properties::dict
struct spa_dict dict
dictionary of key/values
Definition properties.h:40
pw_proxy_events
Proxy events, use pw_proxy_add_listener.
Definition proxy.h:98
pw_proxy
spa_dict
Definition dict.h:51
spa_handle
Definition plugin.h:43
spa_hook
A hook, contains the structure with functions and the data passed to the functions.
Definition hook.h:427
spa_node
Definition node.h:51