Implement per client config files

Move the daemon config file loading to a new conf.c file used by
the context to load the configuration. This replaces the module
profiles and some hacks to move properties around.

If there is nothing other specified with $PIPEWIRE_CONFIG_NAME or
a property, the client.conf file is loaded as a fallback.

Update the session manager config file to load the modules via the
config now. Rename the session modules section to another name.

Update pipewire-pulse to also load a specific pulse property file.
This then makes it pssible to assign specific RT priorities for the
pipewire-pulse process.
This commit is contained in:
Wim Taymans 2021-02-10 19:57:35 +01:00
parent 0d56f717bf
commit c605672d43
17 changed files with 619 additions and 416 deletions

53
src/daemon/client.conf.in Normal file
View file

@ -0,0 +1,53 @@
# Daemon config file for PipeWire clients version @VERSION@ #
properties = {
## Configure properties in the system.
#mem.warn-mlock = false
#mem.allow-mlock = true
#mem.mlock-all = false
#log.level = 2
}
spa-libs = {
## <factory-name regex> = <library-name>
#
# Used to find spa factory names. It maps an spa factory name
# regular expression to a library name that should contain
# that factory.
#
audio.convert* = audioconvert/libspa-audioconvert
support.* = support/libspa-support
}
modules = {
## <module-name> = { [args = { <key>=<value> ... }]
# [flags = [ [ifexists] [nofail] ]}
#
# Loads a module with the given parameters.
# If ifexists is given, the module is ignored when it is not found.
# If nofail is given, module initialization failures are ignored.
#
# The native communication protocol.
libpipewire-module-protocol-native = null
# Allows creating nodes that run in the context of the
# client. Is used by all clients that want to provide
# data to PipeWire.
libpipewire-module-client-node = null
# Allows creating devices that run in the context of the
# client. Is used by the session manager.
libpipewire-module-client-device = null
# Makes a factory for wrapping nodes in an adapter with a
# converter and resampler.
libpipewire-module-adapter = null
# Allows applications to create metadata objects. It creates
# a factory for Metadata objects.
libpipewire-module-metadata = null
# Provides factories to make session manager objects.
libpipewire-module-session-manager = null
}

View file

@ -48,311 +48,8 @@ struct data {
struct pw_main_loop *loop;
const char *daemon_name;
struct pw_properties *conf;
};
static int load_config(struct data *d)
{
const char *path;
char filename[PATH_MAX], *data;
struct stat sbuf;
int fd;
path = getenv("PIPEWIRE_CONFIG_FILE");
if (path == NULL) {
const char *dir;
dir = getenv("PIPEWIRE_CONFIG_DIR");
if (dir == NULL)
dir = PIPEWIRE_CONFIG_DIR;
if (dir == NULL)
return -ENOENT;
snprintf(filename, sizeof(filename), "%s/%s",
dir, DEFAULT_CONFIG_FILE);
path = filename;
}
if ((fd = open(path, O_CLOEXEC | O_RDONLY)) < 0) {
pw_log_warn(NAME" %p: error loading config '%s': %m", d, path);
return -errno;
}
pw_log_info(NAME" %p: loading config '%s'", d, path);
if (fstat(fd, &sbuf) < 0)
goto error_close;
if ((data = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED)
goto error_close;
close(fd);
pw_properties_update_string(d->conf, data, sbuf.st_size);
munmap(data, sbuf.st_size);
return 0;
error_close:
close(fd);
return -errno;
}
static int parse_spa_libs(struct data *d, const char *str)
{
struct spa_json it[2];
char key[512], value[512];
spa_json_init(&it[0], str, strlen(str));
if (spa_json_enter_object(&it[0], &it[1]) < 0)
return -EINVAL;
while (spa_json_get_string(&it[1], key, sizeof(key)-1) > 0) {
const char *val;
if (key[0] == '#') {
if (spa_json_next(&it[1], &val) <= 0)
break;
}
else if (spa_json_get_string(&it[1], value, sizeof(value)-1) > 0) {
pw_context_add_spa_lib(d->context, key, value);
}
}
return 0;
}
static int load_module(struct data *d, const char *key, const char *args, const char *flags)
{
if (pw_context_load_module(d->context, key, args, NULL) == NULL) {
if (errno == ENOENT && flags && strstr(flags, "ifexists") != NULL) {
pw_log_debug(NAME" %p: skipping unavailable module %s",
d, key);
} else if (flags == NULL || strstr(flags, "nofail") == NULL) {
pw_log_error(NAME" %p: could not load module \"%s\": %m",
d, key);
return -errno;
} else {
pw_log_info(NAME" %p: could not load module \"%s\": %m",
d, key);
}
}
return 0;
}
static int parse_modules(struct data *d, const char *str)
{
struct spa_json it[3];
char key[512];
int res = 0;
spa_json_init(&it[0], str, strlen(str));
if (spa_json_enter_object(&it[0], &it[1]) < 0)
return -EINVAL;
while (spa_json_get_string(&it[1], key, sizeof(key)-1) > 0) {
const char *val, *aval;
char *args = NULL, *flags = NULL;
int len, alen;
if ((len = spa_json_next(&it[1], &val)) <= 0)
break;
if (key[0] == '#')
continue;
if (spa_json_is_object(val, len)) {
char arg[512];
spa_json_enter(&it[1], &it[2]);
while (spa_json_get_string(&it[2], arg, sizeof(arg)-1) > 0) {
if ((alen = spa_json_next(&it[2], &aval)) <= 0)
break;
if (strcmp(arg, "args") == 0) {
if (spa_json_is_container(aval, alen))
alen = spa_json_container_len(&it[2], aval, alen);
args = malloc(alen + 1);
spa_json_parse_string(aval, alen, args);
} else if (strcmp(arg, "flags") == 0) {
if (spa_json_is_container(aval, alen))
alen = spa_json_container_len(&it[2], aval, alen);
flags = malloc(alen + 1);
spa_json_parse_string(aval, alen, flags);
}
}
}
else if (!spa_json_is_null(val, len))
break;
res = load_module(d, key, args, flags);
free(args);
free(flags);
if (res < 0)
break;
}
return res;
}
static int create_object(struct data *d, const char *key, const char *args, const char *flags)
{
struct pw_impl_factory *factory;
void *obj;
pw_log_debug("find factory %s", key);
factory = pw_context_find_factory(d->context, key);
if (factory == NULL) {
if (flags && strstr(flags, "nofail") != NULL)
return 0;
pw_log_error("can't find factory %s", key);
return -ENOENT;
}
pw_log_debug("create object with args %s", args);
obj = pw_impl_factory_create_object(factory,
NULL, NULL, 0,
args ? pw_properties_new_string(args) : NULL,
SPA_ID_INVALID);
if (obj == NULL) {
if (flags && strstr(flags, "nofail") != NULL)
return 0;
pw_log_error("can't create object from factory %s: %m", key);
return -errno;
}
return 0;
}
static int parse_objects(struct data *d, const char *str)
{
struct spa_json it[3];
char key[512];
int res = 0;
spa_json_init(&it[0], str, strlen(str));
if (spa_json_enter_object(&it[0], &it[1]) < 0)
return -EINVAL;
while (spa_json_get_string(&it[1], key, sizeof(key)-1) > 0) {
const char *val, *aval;
char *args = NULL, *flags = NULL;
int len, alen;
if ((len = spa_json_next(&it[1], &val)) <= 0)
break;
if (key[0] == '#')
continue;
if (spa_json_is_object(val, len)) {
char arg[512];
spa_json_enter(&it[1], &it[2]);
while (spa_json_get_string(&it[2], arg, sizeof(arg)-1) > 0) {
if ((alen = spa_json_next(&it[2], &aval)) <= 0)
break;
if (strcmp(arg, "args") == 0) {
if (spa_json_is_container(aval, alen))
alen = spa_json_container_len(&it[2], aval, alen);
args = malloc(alen + 1);
spa_json_parse_string(aval, alen, args);
} else if (strcmp(arg, "flags") == 0) {
flags = strndup(aval, alen);
}
}
}
else if (!spa_json_is_null(val, len))
break;
res = create_object(d, key, args, flags);
free(args);
free(flags);
if (res < 0)
break;
}
return res;
}
static int do_exec(struct data *d, const char *key, const char *args)
{
int pid, res, n_args;
pid = fork();
if (pid == 0) {
char *cmd, **argv;
cmd = spa_aprintf("%s %s", key, args ? args : "");
argv = pw_split_strv(cmd, " \t", INT_MAX, &n_args);
free(cmd);
pw_log_info("exec %s '%s'", key, args);
res = execvp(key, argv);
pw_free_strv(argv);
if (res == -1) {
res = -errno;
pw_log_error("execvp error '%s': %m", key);
return res;
}
}
else {
int status;
res = waitpid(pid, &status, WNOHANG);
pw_log_info("exec got pid %d res:%d status:%d", pid, res, status);
}
return 0;
}
static int parse_exec(struct data *d, const char *str)
{
struct spa_json it[3];
char key[512];
int res = 0;
spa_json_init(&it[0], str, strlen(str));
if (spa_json_enter_object(&it[0], &it[1]) < 0)
return -EINVAL;
while (spa_json_get_string(&it[1], key, sizeof(key)-1) > 0) {
const char *val;
char *args = NULL;
int len;
if ((len = spa_json_next(&it[1], &val)) <= 0)
break;
if (key[0] == '#')
continue;
if (spa_json_is_object(val, len)) {
char arg[512], aval[1024];
spa_json_enter(&it[1], &it[2]);
while (spa_json_get_string(&it[2], arg, sizeof(arg)-1) > 0) {
if (spa_json_get_string(&it[2], aval, sizeof(aval)-1) <= 0)
break;
if (strcmp(arg, "args") == 0)
args = strdup(aval);
}
}
else if (!spa_json_is_null(val, len))
break;
res = do_exec(d, key, args);
free(args);
if (res < 0)
break;
}
return res;
}
static void do_quit(void *data, int signal_number)
{
struct data *d = data;
@ -373,7 +70,6 @@ int main(int argc, char *argv[])
{
struct data d;
struct pw_properties *properties;
const char *str;
static const struct option long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
@ -381,7 +77,7 @@ int main(int argc, char *argv[])
{ NULL, 0, NULL, 0}
};
int c, res;
int c;
if (setenv("PIPEWIRE_INTERNAL", "1", 1) < 0)
fprintf(stderr, "can't set PIPEWIRE_INTERNAL env: %m");
@ -389,16 +85,6 @@ int main(int argc, char *argv[])
spa_zero(d);
pw_init(&argc, &argv);
if ((d.conf = pw_properties_new(NULL, NULL)) == NULL) {
pw_log_error("failed to create config: %m");
return -1;
}
if ((res = load_config(&d)) < 0) {
pw_log_error("failed to load config: %s", spa_strerror(res));
return -1;
}
d.daemon_name = getenv("PIPEWIRE_CORE");
if (d.daemon_name == NULL)
d.daemon_name = PW_DEFAULT_REMOTE;
@ -427,13 +113,9 @@ int main(int argc, char *argv[])
properties = pw_properties_new(
PW_KEY_CORE_NAME, d.daemon_name,
PW_KEY_CONTEXT_PROFILE_MODULES, "none",
PW_KEY_CONFIG_NAME, "pipewire-uninstalled.conf",
PW_KEY_CORE_DAEMON, "true", NULL);
/* parse configuration */
if ((str = pw_properties_get(d.conf, "properties")) != NULL)
pw_properties_update_string(properties, str, strlen(str));
d.loop = pw_main_loop_new(&properties->dict);
if (d.loop == NULL) {
pw_log_error("failed to create main-loop: %m");
@ -449,32 +131,10 @@ int main(int argc, char *argv[])
return -1;
}
if ((str = pw_properties_get(d.conf, "spa-libs")) != NULL)
parse_spa_libs(&d, str);
if ((str = pw_properties_get(d.conf, "modules")) != NULL) {
if ((res = parse_modules(&d, str)) < 0) {
pw_log_error("failed to load modules: %s", spa_strerror(res));
return -1;
}
}
if ((str = pw_properties_get(d.conf, "objects")) != NULL) {
if ((res = parse_objects(&d, str)) < 0) {
pw_log_error("failed to load objects: %s", spa_strerror(res));
return -1;
}
}
if ((str = pw_properties_get(d.conf, "exec")) != NULL) {
if ((res = parse_exec(&d, str)) < 0) {
pw_log_error("failed to exec: %s", spa_strerror(res));
return -1;
}
}
pw_log_info("start main loop");
pw_main_loop_run(d.loop);
pw_log_info("leave main loop");
pw_properties_free(d.conf);
pw_context_destroy(d.context);
pw_main_loop_destroy(d.loop);
pw_deinit();

View file

@ -17,6 +17,49 @@ spa-libs = {
}
modules = {
## <module-name> = { [args = { <key>=<value> ... }]
# [flags = [ [ifexists] [nofail] ]}
#
# Loads a module with the given parameters.
# If ifexists is given, the module is ignored when it is not found.
# If nofail is given, module initialization failures are ignored.
#
# Uses RTKit to boost the data thread priority.
libpipewire-module-rtkit = {
args = {
#nice.level = -11
#rt.prio = 20
#rt.time.soft = 200000
#rt.time.hard = 200000
}
flags = [ ifexists nofail ]
}
# The native communication protocol.
libpipewire-module-protocol-native = null
# Allows creating nodes that run in the context of the
# client. Is used by all clients that want to provide
# data to PipeWire.
libpipewire-module-client-node = null
# Allows creating devices that run in the context of the
# client. Is used by the session manager.
libpipewire-module-client-device = null
# Makes a factory for wrapping nodes in an adapter with a
# converter and resampler.
libpipewire-module-adapter = null
# Allows applications to create metadata objects. It creates
# a factory for Metadata objects.
libpipewire-module-metadata = null
# Provides factories to make session manager objects.
libpipewire-module-session-manager = null
}
session.modules = {
# These are the modules that are enabled when a file with
# the key name is found in the media-session.d config directory.
# the default bundle is always enabled.

View file

@ -35,6 +35,16 @@ configure_file(input : 'pipewire.conf.in',
configuration : conf_config,
install_dir : conf_install_dir)
configure_file(input : 'client.conf.in',
output : 'client.conf',
configuration : conf_config,
install_dir : conf_install_dir)
configure_file(input : 'pipewire-pulse.conf.in',
output : 'pipewire-pulse.conf',
configuration : conf_config,
install_dir : conf_install_dir)
configure_file(input : 'pipewire.conf.in',
output : 'pipewire-uninstalled.conf',
configuration : conf_config_uninstalled)

View file

@ -93,7 +93,7 @@ int main(int argc, char *argv[])
}
properties = pw_properties_new(
PW_KEY_CONTEXT_PROFILE_MODULES, "default,rtkit",
PW_KEY_CONFIG_NAME, "pipewire-pulse.conf",
NULL);
loop = pw_main_loop_new(&properties->dict);

View file

@ -0,0 +1,56 @@
# Daemon config file for PipeWire pulse version @VERSION@ #
properties = {
## Configure properties in the system.
#mem.warn-mlock = false
#mem.allow-mlock = true
#mem.mlock-all = false
#log.level = 2
}
spa-libs = {
## <factory-name regex> = <library-name>
#
# Used to find spa factory names. It maps an spa factory name
# regular expression to a library name that should contain
# that factory.
#
audio.convert* = audioconvert/libspa-audioconvert
support.* = support/libspa-support
}
modules = {
## <module-name> = { [args = { <key>=<value> ... }]
# [flags = [ [ifexists] [nofail] ]}
#
# Loads a module with the given parameters.
# If ifexists is given, the module is ignored when it is not found.
# If nofail is given, module initialization failures are ignored.
#
# Uses RTKit to boost the data thread priority.
libpipewire-module-rtkit = {
args = {
nice.level = -14
#rt.prio = 20
#rt.time.soft = 200000
#rt.time.hard = 200000
}
flags = [ ifexists nofail ]
}
# The native communication protocol.
libpipewire-module-protocol-native = null
# Allows creating nodes that run in the context of the
# client. Is used by all clients that want to provide
# data to PipeWire.
libpipewire-module-client-node = null
# Makes a factory for wrapping nodes in an adapter with a
# converter and resampler.
libpipewire-module-adapter = null
# Allows applications to create metadata objects. It creates
# a factory for Metadata objects.
libpipewire-module-metadata = null
}