pipewire/src/modules/module-mixer.c

187 lines
4.5 KiB
C
Raw Normal View History

2017-05-23 19:15:33 +02:00
/* PipeWire
* Copyright (C) 2017 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <dlfcn.h>
#include "config.h"
2017-07-11 15:57:20 +02:00
#include "pipewire/core.h"
#include "pipewire/module.h"
#include "modules/spa/spa-node.h"
#define AUDIOMIXER_LIB "audiomixer/libspa-audiomixer"
2017-05-23 19:15:33 +02:00
struct impl {
2017-05-26 08:05:01 +02:00
struct pw_core *core;
struct pw_module *module;
2017-05-26 08:05:01 +02:00
struct pw_properties *properties;
2017-05-26 08:05:01 +02:00
void *hnd;
const struct spa_handle_factory *factory;
2017-05-23 19:15:33 +02:00
};
2017-05-26 08:05:01 +02:00
static const struct spa_handle_factory *find_factory(struct impl *impl)
{
2017-05-26 08:05:01 +02:00
spa_handle_factory_enum_func_t enum_func;
uint32_t index;
const struct spa_handle_factory *factory = NULL;
int res;
char *filename;
const char *dir;
2017-05-26 08:05:01 +02:00
if ((dir = getenv("SPA_PLUGIN_DIR")) == NULL)
dir = PLUGINDIR;
asprintf(&filename, "%s/%s.so", dir, AUDIOMIXER_LIB);
if ((impl->hnd = dlopen(filename, RTLD_NOW)) == NULL) {
2017-05-26 08:05:01 +02:00
pw_log_error("can't load %s: %s", AUDIOMIXER_LIB, dlerror());
goto open_failed;
2017-05-26 08:05:01 +02:00
}
if ((enum_func = dlsym(impl->hnd, SPA_HANDLE_FACTORY_ENUM_FUNC_NAME)) == NULL) {
pw_log_error("can't find enum function");
goto no_symbol;
}
for (index = 0;; index++) {
if ((res = enum_func(&factory, index)) < 0) {
if (res != SPA_RESULT_ENUM_END)
pw_log_error("can't enumerate factories: %d", res);
goto enum_failed;
}
if (strcmp(factory->name, "audiomixer") == 0)
break;
}
free(filename);
2017-05-26 08:05:01 +02:00
return factory;
enum_failed:
no_symbol:
dlclose(impl->hnd);
impl->hnd = NULL;
open_failed:
free(filename);
2017-05-26 08:05:01 +02:00
return NULL;
}
2017-05-26 08:05:01 +02:00
static struct pw_node *make_node(struct impl *impl)
{
2017-05-26 08:05:01 +02:00
struct spa_handle *handle;
int res;
void *iface;
struct spa_node *spa_node;
struct spa_clock *spa_clock;
struct pw_node *node;
handle = calloc(1, impl->factory->size);
if ((res = spa_handle_factory_init(impl->factory,
handle,
NULL, impl->core->support, impl->core->n_support)) < 0) {
pw_log_error("can't make factory instance: %d", res);
goto init_failed;
}
if ((res = spa_handle_get_interface(handle, impl->core->type.spa_node, &iface)) < 0) {
pw_log_error("can't get interface %d", res);
goto interface_failed;
}
spa_node = iface;
if ((res = spa_handle_get_interface(handle, impl->core->type.spa_clock, &iface)) < 0) {
iface = NULL;
}
spa_clock = iface;
node = pw_spa_node_new(impl->core, NULL, impl->module->global,
"audiomixer", false, spa_node, spa_clock, NULL);
2017-05-26 08:05:01 +02:00
return node;
interface_failed:
spa_handle_clear(handle);
init_failed:
free(handle);
return NULL;
}
static bool module_init(struct pw_module *module, struct pw_properties *properties)
{
struct pw_core *core = module->core;
2017-05-26 08:05:01 +02:00
struct impl *impl;
struct pw_node *n;
2017-05-26 08:05:01 +02:00
impl = calloc(1, sizeof(struct impl));
pw_log_debug("module %p: new", impl);
2017-05-26 08:05:01 +02:00
impl->core = core;
impl->module = module;
2017-05-26 08:05:01 +02:00
impl->properties = properties;
2017-05-26 08:05:01 +02:00
impl->factory = find_factory(impl);
2017-05-26 08:05:01 +02:00
spa_list_for_each(n, &core->node_list, link) {
const char *str;
char *error;
struct pw_node *node;
struct pw_port *ip, *op;
2017-05-26 08:05:01 +02:00
if (n->global == NULL)
continue;
2017-05-26 08:05:01 +02:00
if (n->properties == NULL)
continue;
2017-05-26 08:05:01 +02:00
if ((str = pw_properties_get(n->properties, "media.class")) == NULL)
continue;
2017-05-26 08:05:01 +02:00
if (strcmp(str, "Audio/Sink") != 0)
continue;
2017-05-26 08:05:01 +02:00
if ((ip = pw_node_get_free_port(n, PW_DIRECTION_INPUT)) == NULL)
continue;
2017-05-26 08:05:01 +02:00
node = make_node(impl);
op = pw_node_get_free_port(node, PW_DIRECTION_OUTPUT);
if (op == NULL)
continue;
n->idle_used_input_links++;
node->idle_used_output_links++;
pw_link_new(core, module->global, op, ip, NULL, NULL, &error);
2017-05-26 08:05:01 +02:00
}
return impl;
}
#if 0
2017-05-26 08:05:01 +02:00
static void module_destroy(struct impl *impl)
{
2017-05-26 08:05:01 +02:00
pw_log_debug("module %p: destroy", impl);
2017-05-26 08:05:01 +02:00
free(impl);
}
#endif
2017-05-26 08:05:01 +02:00
bool pipewire__module_init(struct pw_module *module, const char *args)
{
return module_init(module, NULL);
}