PipeWire: export-spa-device.c

PipeWire 0.3.69

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

Exporting and loading a SPA device, using Core API.

/* PipeWire */
/* SPDX-FileCopyrightText: Copyright © 2019 Wim Taymans */
/* SPDX-License-Identifier: MIT */
/*
[title]
Exporting and loading a SPA device, 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 pw_impl_device *device;
const char *library;
const char *factory;
const char *path;
};
static int make_device(struct data *data)
{
struct pw_impl_factory *factory;
struct pw_properties *props;
factory = pw_context_find_factory(data->context, "spa-device-factory");
if (factory == NULL)
return -1;
props = pw_properties_new(SPA_KEY_LIBRARY_NAME, data->library,
SPA_KEY_FACTORY_NAME, data->factory, NULL);
data->device = pw_impl_factory_create_object(factory,
NULL,
PW_TYPE_INTERFACE_Device,
PW_VERSION_DEVICE,
props, SPA_ID_INVALID);
pw_core_export(data->core, SPA_TYPE_INTERFACE_Device, NULL,
pw_impl_device_get_implementation(data->device), 0);
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>\n\n"
"\texample: %s v4l2/libspa-v4l2 api.v4l2.device\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];
pw_context_load_module(data.context, "libpipewire-module-spa-device-factory", NULL, NULL);
data.core = pw_context_connect(data.context, NULL, 0);
if (data.core == NULL) {
pw_log_error("can't connect %m");
return -1;
}
pw_core_add_listener(data.core, &data.core_listener, &core_events, &data);
if (make_device(&data) < 0) {
pw_log_error("can't make device");
return -1;
}
pw_main_loop_run(data.loop);
pw_context_destroy(data.context);
pw_main_loop_destroy(data.loop);
return 0;
}
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:380
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:168
PW_ID_CORE
#define PW_ID_CORE
default ID for the core object after connect
Definition: core.h:59
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:392
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:265
pw_core_add_listener
#define pw_core_add_listener(c,...)
Definition: core.h:347
PW_VERSION_CORE_EVENTS
#define PW_VERSION_CORE_EVENTS
Definition: core.h:125
PW_TYPE_INTERFACE_Device
#define PW_TYPE_INTERFACE_Device
Definition: src/pipewire/device.h:31
PW_VERSION_DEVICE
#define PW_VERSION_DEVICE
Definition: src/pipewire/device.h:34
pw_impl_device_get_implementation
struct spa_device * pw_impl_device_get_implementation(struct pw_impl_device *device)
Get the device implementation.
Definition: impl-device.c:877
pw_impl_factory_create_object
void * pw_impl_factory_create_object(struct pw_impl_factory *factory, struct pw_resource *resource, const char *type, uint32_t version, struct pw_properties *properties, uint32_t new_id)
Definition: impl-factory.c:245
pw_context_find_factory
struct pw_impl_factory * pw_context_find_factory(struct pw_context *context, const char *name)
Find a factory by name.
Definition: impl-factory.c:271
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:142
pw_log_error
#define pw_log_error(...)
Definition: src/pipewire/log.h:142
pw_loop_add_signal
#define pw_loop_add_signal(l,...)
Definition: src/pipewire/loop.h:63
pw_main_loop_quit
int pw_main_loop_quit(struct pw_main_loop *loop)
Quit a main loop.
Definition: main-loop.c:105
pw_main_loop_destroy
void pw_main_loop_destroy(struct pw_main_loop *loop)
Destroy a loop.
Definition: main-loop.c:70
pw_main_loop_run
int pw_main_loop_run(struct pw_main_loop *loop)
Run a main loop.
Definition: main-loop.c:119
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:60
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:93
pw_init
void pw_init(int *argc, char **argv[])
Initialize PipeWire.
Definition: pipewire.c:558
pw_properties_new
struct pw_properties * pw_properties_new(const char *key,...) 1
Make a new properties object.
Definition: properties.c:82
SPA_TYPE_INTERFACE_Device
#define SPA_TYPE_INTERFACE_Device
Definition: spa/include/spa/monitor/device.h:36
SPA_KEY_FACTORY_NAME
#define SPA_KEY_FACTORY_NAME
the name of a factory
Definition: plugin.h:202
SPA_KEY_LIBRARY_NAME
#define SPA_KEY_LIBRARY_NAME
the name of a library.
Definition: plugin.h:211
spa_strerror
#define spa_strerror(err)
Definition: result.h:49
SPA_ID_INVALID
#define SPA_ID_INVALID
Definition: defs.h:228
impl.h
pipewire/impl.h
props.h
result.h
spa/utils/result.h
pw_context
pw_core_events
Core events.
Definition: core.h:123
pw_core
pw_impl_device
pw_impl_factory
pw_loop
Definition: src/pipewire/loop.h:31
pw_main_loop
A main loop object.
pw_properties
Definition: properties.h:33
spa_hook
A hook, contains the structure with functions and the data passed to the functions.
Definition: hook.h:331