From 5df5afd0a9c0612644aa2fcfb8c1d142589b37f6 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Tue, 23 Jan 2018 15:25:48 +0100 Subject: [PATCH] module: load and register in one go Load and register the module in one go because we need to have the global set when calling the init function so that factories and nodes can use this as the parent global. Set the name in the global properties. --- src/examples/export-spa.c | 2 +- src/examples/local-v4l2.c | 2 +- src/pipewire/command.c | 3 +- src/pipewire/module.c | 61 +++++++++++++++++++++------------------ src/pipewire/module.h | 19 ++++++------ src/pipewire/remote.c | 4 +-- src/tools/pipewire-cli.c | 4 +-- 7 files changed, 50 insertions(+), 45 deletions(-) diff --git a/src/examples/export-spa.c b/src/examples/export-spa.c index a6eccfb88..6fb11913c 100644 --- a/src/examples/export-spa.c +++ b/src/examples/export-spa.c @@ -138,7 +138,7 @@ int main(int argc, char *argv[]) if (argc > 3) data.path = argv[3]; - pw_module_load(data.core, "libpipewire-module-spa-node-factory", NULL); + pw_module_load(data.core, "libpipewire-module-spa-node-factory", NULL, NULL, NULL, NULL); spa_debug_set_type_map(data.t->map); diff --git a/src/examples/local-v4l2.c b/src/examples/local-v4l2.c index 410dc66fe..0e91f0a51 100644 --- a/src/examples/local-v4l2.c +++ b/src/examples/local-v4l2.c @@ -514,7 +514,7 @@ int main(int argc, char *argv[]) data.core = pw_core_new(pw_main_loop_get_loop(data.loop), NULL); data.t = pw_core_get_type(data.core); - pw_module_load(data.core, "libpipewire-module-spa-node-factory", NULL); + pw_module_load(data.core, "libpipewire-module-spa-node-factory", NULL, NULL, NULL, NULL); init_type(&data.type, data.t->map); diff --git a/src/pipewire/command.c b/src/pipewire/command.c index 41908cbf1..1578829bb 100644 --- a/src/pipewire/command.c +++ b/src/pipewire/command.c @@ -92,12 +92,11 @@ execute_command_module_load(struct pw_command *command, struct pw_core *core, ch { struct pw_module *module; - module = pw_module_load(core, command->args[1], command->args[2]); + module = pw_module_load(core, command->args[1], command->args[2], NULL, NULL, NULL); if (module == NULL) { asprintf(err, "could not load module \"%s\"", command->args[1]); return -ENOMEM; } - pw_module_register(module, NULL, NULL, NULL); return 0; } diff --git a/src/pipewire/module.c b/src/pipewire/module.c index a1a347f2b..13e5d5adc 100644 --- a/src/pipewire/module.c +++ b/src/pipewire/module.c @@ -158,7 +158,12 @@ struct pw_module * pw_core_find_module(struct pw_core *core, const char *filenam * * \memberof pw_module */ -struct pw_module *pw_module_load(struct pw_core *core, const char *name, const char *args) +struct pw_module * +pw_module_load(struct pw_core *core, + const char *name, const char *args, + struct pw_client *owner, + struct pw_global *parent, + struct pw_properties *properties) { struct pw_module *this; struct impl *impl; @@ -205,6 +210,11 @@ struct pw_module *pw_module_load(struct pw_core *core, const char *name, const c if (impl == NULL) goto no_mem; + if (properties == NULL) + properties = pw_properties_new(NULL, NULL); + if (properties == NULL) + goto no_mem; + impl->hnd = hnd; this = &impl->this; @@ -218,6 +228,21 @@ struct pw_module *pw_module_load(struct pw_core *core, const char *name, const c this->info.args = args ? strdup(args) : NULL; this->info.props = NULL; + pw_properties_set(properties, PW_MODULE_PROP_NAME, name); + + spa_list_append(&core->module_list, &this->link); + + this->global = pw_global_new(core, + core->type.module, PW_VERSION_MODULE, + properties, + module_bind_func, this); + + if (this->global == NULL) + goto no_global; + + pw_global_register(this->global, owner, parent); + this->info.id = this->global->id; + if ((res = init_func(this, args)) < 0) goto init_failed; @@ -234,38 +259,18 @@ struct pw_module *pw_module_load(struct pw_core *core, const char *name, const c return NULL; no_mem: no_pw_module: - pw_log_error("\"%s\" is not a pipewire module", filename); + pw_log_error("\"%s\": is not a pipewire module", filename); dlclose(hnd); free(filename); return NULL; - init_failed: - pw_log_error("\"%s\" failed to initialize: %s", filename, spa_strerror(res)); + no_global: + pw_log_error("\"%s\": failed to create global", filename); + pw_module_destroy(this); + return NULL; + init_failed: + pw_log_error("\"%s\": failed to initialize: %s", filename, spa_strerror(res)); pw_module_destroy(this); return NULL; -} - -int pw_module_register(struct pw_module *module, - struct pw_client *owner, - struct pw_global *parent, - struct pw_properties *properties) -{ - struct pw_core *core = module->core; - - spa_list_append(&core->module_list, &module->link); - - module->global = pw_global_new(core, - core->type.module, PW_VERSION_MODULE, - properties, - module_bind_func, module); - - if (module->global == NULL) - return -ENOMEM; - - pw_global_register(module->global, owner, parent); - - module->info.id = module->global->id; - - return 0; } /** Destroy a module diff --git a/src/pipewire/module.h b/src/pipewire/module.h index 784b094d1..78c91a549 100644 --- a/src/pipewire/module.h +++ b/src/pipewire/module.h @@ -58,19 +58,20 @@ struct pw_module_events { #define PW_VERSION_MODULE_EVENTS 0 uint32_t version; - /** The module id destroyed */ + /** The module is destroyed */ void (*destroy) (void *data); }; -/** Load a module by name and arguments and register it */ -struct pw_module * -pw_module_load(struct pw_core *core, const char *name, const char *args); +/** The name of the module */ +#define PW_MODULE_PROP_NAME "pipewire.module.name" -/** Finish module configuration and register */ -int pw_module_register(struct pw_module *module, /**< the module to register */ - struct pw_client *owner, /**< optional owner */ - struct pw_global *parent, /**< parent global */ - struct pw_properties *properties /**< extra global properties */); +struct pw_module * +pw_module_load(struct pw_core *core, + const char *name, /**< name of the module */ + const char *args /**< arguments of the module */, + struct pw_client *owner, /**< optional owner */ + struct pw_global *parent, /**< parent global */ + struct pw_properties *properties /**< extra global properties */); /** Get the core of a module */ struct pw_core * pw_module_get_core(struct pw_module *module); diff --git a/src/pipewire/remote.c b/src/pipewire/remote.c index 4f72166a1..2cf6c16d2 100644 --- a/src/pipewire/remote.c +++ b/src/pipewire/remote.c @@ -251,7 +251,7 @@ struct pw_remote *pw_remote_new(struct pw_core *core, spa_hook_list_init(&this->listener_list); if ((protocol_name = pw_properties_get(properties, PW_REMOTE_PROP_PROTOCOL)) == NULL) { - if (!pw_module_load(core, "libpipewire-module-protocol-native", NULL)) + if (!pw_module_load(core, "libpipewire-module-protocol-native", NULL, NULL, NULL, NULL)) goto no_protocol; protocol_name = PW_TYPE_PROTOCOL__Native; @@ -265,7 +265,7 @@ struct pw_remote *pw_remote_new(struct pw_core *core, if (this->conn == NULL) goto no_connection; - pw_module_load(core, "libpipewire-module-client-node", NULL); + pw_module_load(core, "libpipewire-module-client-node", NULL, NULL, NULL, NULL); spa_list_append(&core->remote_list, &this->link); diff --git a/src/tools/pipewire-cli.c b/src/tools/pipewire-cli.c index 0cbd8e43f..83a4ad67b 100644 --- a/src/tools/pipewire-cli.c +++ b/src/tools/pipewire-cli.c @@ -218,7 +218,7 @@ static bool do_load_module(struct data *data, const char *cmd, char *args, char return false; } - module = pw_module_load(data->core, a[0], n == 2 ? a[1] : NULL); + module = pw_module_load(data->core, a[0], n == 2 ? a[1] : NULL, NULL, NULL, NULL); if (module == NULL) { asprintf(error, "Could not load module"); return false; @@ -1116,7 +1116,7 @@ int main(int argc, char *argv[]) data.t = pw_core_get_type(data.core); info = pw_core_get_info(data.core); - pw_module_load(data.core, "libpipewire-module-link-factory", NULL); + pw_module_load(data.core, "libpipewire-module-link-factory", NULL, NULL, NULL, NULL); pw_loop_add_io(l, STDIN_FILENO, SPA_IO_IN|SPA_IO_HUP, false, do_input, &data);