spa examples: Load plugins from SPA_PLUGIN_DIR or installed path

This lets them be used installed or uninstalled, like pipewire itself.

Signed-off-by: Simon McVittie <smcv@debian.org>
This commit is contained in:
Simon McVittie 2020-06-05 13:19:17 +01:00 committed by Wim Taymans
parent cecbe8ff17
commit 8b97d2ae74
5 changed files with 99 additions and 32 deletions

View file

@ -22,6 +22,9 @@
* DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#include <math.h>
#include <string.h>
#include <stdio.h>
@ -59,6 +62,7 @@ struct buffer {
};
struct data {
const char *plugin_dir;
struct spa_log *log;
struct spa_system *system;
struct spa_loop *loop;
@ -95,11 +99,17 @@ static int load_handle(struct data *data, struct spa_handle **handle, const char
void *hnd;
spa_handle_factory_enum_func_t enum_func;
uint32_t i;
char *path;
if ((hnd = dlopen(lib, RTLD_NOW)) == NULL) {
if ((path = spa_aprintf("%s/%s", data->plugin_dir, lib)) == NULL) {
return -ENOMEM;
}
if ((hnd = dlopen(path, RTLD_NOW)) == NULL) {
printf("can't load %s: %s\n", lib, dlerror());
free(path);
return -ENOENT;
}
free(path);
if ((enum_func = dlsym(hnd, SPA_HANDLE_FACTORY_ENUM_FUNC_NAME)) == NULL) {
printf("can't find enum function\n");
return -ENOENT;
@ -137,6 +147,10 @@ int init_data(struct data *data)
struct spa_handle *handle = NULL;
void *iface;
if ((str = getenv("SPA_PLUGIN_DIR")) == NULL)
str = PLUGINDIR;
data->plugin_dir = str;
/* init the graph */
spa_graph_init(&data->graph, &data->graph_state);
@ -146,7 +160,7 @@ int init_data(struct data *data)
/* load and set support system */
if ((res = load_handle(data, &handle,
"build/spa/plugins/support/libspa-support.so",
"support/libspa-support.so",
SPA_NAME_SUPPORT_SYSTEM)) < 0)
return res;
if ((res = spa_handle_get_interface(handle, SPA_TYPE_INTERFACE_System, &iface)) < 0) {
@ -159,7 +173,7 @@ int init_data(struct data *data)
/* load and set support loop and loop control */
if ((res = load_handle(data, &handle,
"build/spa/plugins/support/libspa-support.so",
"support/libspa-support.so",
SPA_NAME_SUPPORT_LOOP)) < 0)
return res;
@ -190,11 +204,17 @@ static int make_node(struct data *data, struct spa_node **node, const char *lib,
void *hnd;
spa_handle_factory_enum_func_t enum_func;
uint32_t i;
char *path;
if ((hnd = dlopen(lib, RTLD_NOW)) == NULL) {
if ((path = spa_aprintf("%s/%s", data->plugin_dir, lib)) == NULL) {
return -ENOMEM;
}
if ((hnd = dlopen(path, RTLD_NOW)) == NULL) {
printf("can't load %s: %s\n", lib, dlerror());
free(path);
return -ENOENT;
}
free(path);
if ((enum_func = dlsym(hnd, SPA_HANDLE_FACTORY_ENUM_FUNC_NAME)) == NULL) {
printf("can't find enum function\n");
return -ENOENT;
@ -269,7 +289,7 @@ static int make_nodes(struct data *data, const char *device)
/* make the source node (audiotestsrc) */
if ((res = make_node(data, &data->source_follower_node,
"build/spa/plugins/audiotestsrc/libspa-audiotestsrc.so",
"audiotestsrc/libspa-audiotestsrc.so",
"audiotestsrc",
NULL)) < 0) {
printf("can't create source follower node (audiotestsrc): %d\n", res);
@ -294,7 +314,7 @@ static int make_nodes(struct data *data, const char *device)
snprintf(value, sizeof(value), "pointer:%p", data->source_follower_node);
items[0] = SPA_DICT_ITEM_INIT("audio.adapt.follower", value);
if ((res = make_node(data, &data->source_node,
"build/spa/plugins/audioconvert/libspa-audioconvert.so",
"audioconvert/libspa-audioconvert.so",
SPA_NAME_AUDIO_ADAPT,
&SPA_DICT_INIT(items, 1))) < 0) {
printf("can't create source adapter node: %d\n", res);
@ -333,7 +353,7 @@ static int make_nodes(struct data *data, const char *device)
/* make the sink follower node (alsa-pcm-sink) */
if ((res = make_node(data, &data->sink_follower_node,
"build/spa/plugins/alsa/libspa-alsa.so",
"alsa/libspa-alsa.so",
SPA_NAME_API_ALSA_PCM_SINK,
NULL)) < 0) {
printf("can't create sink follower node (alsa-pcm-sink): %d\n", res);
@ -344,7 +364,7 @@ static int make_nodes(struct data *data, const char *device)
snprintf(value, sizeof(value), "pointer:%p", data->sink_follower_node);
items[0] = SPA_DICT_ITEM_INIT("audio.adapt.follower", value);
if ((res = make_node(data, &data->sink_node,
"build/spa/plugins/audioconvert/libspa-audioconvert.so",
"audioconvert/libspa-audioconvert.so",
SPA_NAME_AUDIO_ADAPT,
&SPA_DICT_INIT(items, 1))) < 0) {
printf("can't create sink adapter node: %d\n", res);