From 589e3d977cc0deac1c513f903300965e92cbacb7 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Wed, 19 Jul 2017 12:44:10 +0200 Subject: [PATCH] module: fix registration of modules Don't fail when protocol-native is already registered. --- src/examples/local-v4l2.c | 3 +-- src/modules/module-protocol-native.c | 3 +++ src/pipewire/command.c | 6 +++++- src/pipewire/core.c | 12 +---------- src/pipewire/core.h | 4 +--- src/pipewire/module.c | 31 ++++++++++++++++++---------- src/pipewire/module.h | 6 ++++-- src/pipewire/protocol.c | 13 +++++++++--- src/pipewire/protocol.h | 2 ++ src/pipewire/remote.c | 6 +++--- 10 files changed, 50 insertions(+), 36 deletions(-) diff --git a/src/examples/local-v4l2.c b/src/examples/local-v4l2.c index 9c7d5d855..652bee41e 100644 --- a/src/examples/local-v4l2.c +++ b/src/examples/local-v4l2.c @@ -492,7 +492,6 @@ static void make_nodes(struct data *data) int main(int argc, char *argv[]) { struct data data = { 0, }; - char *err; pw_init(&argc, &argv); @@ -501,7 +500,7 @@ int main(int argc, char *argv[]) data.core = pw_core_new(data.loop, NULL); data.path = argc > 1 ? argv[1] : NULL; - pw_module_load(data.core, "libpipewire-module-spa-node-factory", NULL, &err); + pw_module_load(data.core, "libpipewire-module-spa-node-factory", NULL); init_type(&data.type, data.core->type.map); diff --git a/src/modules/module-protocol-native.c b/src/modules/module-protocol-native.c index a1bbe746f..b045d9bb6 100644 --- a/src/modules/module-protocol-native.c +++ b/src/modules/module-protocol-native.c @@ -715,6 +715,9 @@ static bool module_init(struct pw_module *module, struct pw_properties *properti const char *val; struct protocol_data *d; + if (pw_core_find_protocol(core, PW_TYPE_PROTOCOL__Native) != NULL) + return true; + this = pw_protocol_new(core, PW_TYPE_PROTOCOL__Native, sizeof(struct protocol_data)); if (this == NULL) return false; diff --git a/src/pipewire/command.c b/src/pipewire/command.c index 2422ea822..edda2351b 100644 --- a/src/pipewire/command.c +++ b/src/pipewire/command.c @@ -90,7 +90,11 @@ execute_command_module_load(struct pw_command *command, struct pw_core *core, ch { struct impl *impl = SPA_CONTAINER_OF(command, struct impl, this); - return pw_module_load(core, impl->args[1], impl->args[2], err) != NULL; + if (pw_module_load(core, impl->args[1], impl->args[2]) == NULL) { + asprintf(err, "could not load module \"%s\"", impl->args[1]); + return false; + } + return true; } /** Free command diff --git a/src/pipewire/core.c b/src/pipewire/core.c index 75c10c64d..2a0301479 100644 --- a/src/pipewire/core.c +++ b/src/pipewire/core.c @@ -349,6 +349,7 @@ struct pw_core *pw_core_new(struct pw_loop *main_loop, struct pw_properties *pro spa_list_init(&this->resource_list); spa_list_init(&this->registry_resource_list); spa_list_init(&this->global_list); + spa_list_init(&this->module_list); spa_list_init(&this->client_list); spa_list_init(&this->node_list); spa_list_init(&this->node_factory_list); @@ -770,14 +771,3 @@ struct pw_node_factory *pw_core_find_node_factory(struct pw_core *core, const ch } return NULL; } - -struct pw_protocol *pw_core_find_protocol(struct pw_core *core, const char *name) -{ - struct pw_protocol *protocol; - - spa_list_for_each(protocol, &core->protocol_list, link) { - if (strcmp(protocol->name, name) == 0) - return protocol; - } - return NULL; -} diff --git a/src/pipewire/core.h b/src/pipewire/core.h index c934c0481..4edb0f5ab 100644 --- a/src/pipewire/core.h +++ b/src/pipewire/core.h @@ -168,6 +168,7 @@ struct pw_core { struct spa_list remote_list; /**< list of remote connections */ struct spa_list resource_list; /**< list of core resources */ struct spa_list registry_resource_list; /**< list of registry resources */ + struct spa_list module_list; /**< list of modules */ struct spa_list global_list; /**< list of globals */ struct spa_list client_list; /**< list of clients */ struct spa_list node_list; /**< list of nodes */ @@ -244,9 +245,6 @@ pw_core_find_port(struct pw_core *core, struct pw_node_factory * pw_core_find_node_factory(struct pw_core *core, const char *name); -struct pw_protocol * -pw_core_find_protocol(struct pw_core *core, const char *name); - #ifdef __cplusplus } #endif diff --git a/src/pipewire/module.c b/src/pipewire/module.c index 45b1a06db..d212f7fac 100644 --- a/src/pipewire/module.c +++ b/src/pipewire/module.c @@ -116,18 +116,27 @@ module_bind_func(struct pw_global *global, return SPA_RESULT_NO_MEMORY; } +struct pw_module * pw_core_find_module(struct pw_core *core, const char *filename) +{ + struct pw_module *module; + spa_list_for_each(module, &core->module_list, link) { + if (strcmp(module->info.filename, filename) == 0) + return module; + } + return NULL; +} + /** Load a module * * \param core a \ref pw_core * \param name name of the module to load * \param args A string with arguments for the module - * \param[out] err Return location for an error string, or NULL + * \param[out] error Return location for an error string, or NULL * \return A \ref pw_module if the module could be loaded, or NULL on failure. * * \memberof pw_module */ -struct pw_module *pw_module_load(struct pw_core *core, - const char *name, const char *args, char **err) +struct pw_module *pw_module_load(struct pw_core *core, const char *name, const char *args) { struct pw_module *this; struct impl *impl; @@ -185,6 +194,7 @@ struct pw_module *pw_module_load(struct pw_core *core, this->info.args = args ? strdup(args) : NULL; this->info.props = NULL; + spa_list_insert(core->module_list.prev, &this->link); this->global = pw_core_add_global(core, NULL, core->global, core->type.module, PW_VERSION_MODULE, module_bind_func, this); @@ -197,24 +207,20 @@ struct pw_module *pw_module_load(struct pw_core *core, return this; not_found: - if (err) - asprintf(err, "No module \"%s\" was found", name); + pw_log_error("No module \"%s\" was found", name); return NULL; open_failed: - if (err) - asprintf(err, "Failed to open module: \"%s\" %s", filename, dlerror()); + pw_log_error("Failed to open module: \"%s\" %s", filename, dlerror()); free(filename); return NULL; no_mem: no_pw_module: - if (err) - asprintf(err, "\"%s\" is not a pipewire module", name); + pw_log_error("\"%s\" is not a pipewire module", filename); dlclose(hnd); free(filename); return NULL; init_failed: - if (err) - asprintf(err, "\"%s\" failed to initialize", name); + pw_log_error("\"%s\" failed to initialize", filename); pw_module_destroy(this); return NULL; } @@ -235,6 +241,9 @@ void pw_module_destroy(struct pw_module *module) free((char *) module->info.filename); if (module->info.args) free((char *) module->info.args); + + spa_list_remove(&module->link); + pw_global_destroy(module->global); dlclose(impl->hnd); free(impl); } diff --git a/src/pipewire/module.h b/src/pipewire/module.h index 7dab7b4ca..ded805673 100644 --- a/src/pipewire/module.h +++ b/src/pipewire/module.h @@ -63,12 +63,14 @@ struct pw_module { typedef bool (*pw_module_init_func_t) (struct pw_module *module, char *args); struct pw_module * -pw_module_load(struct pw_core *core, - const char *name, const char *args, char **err); +pw_module_load(struct pw_core *core, const char *name, const char *args); void pw_module_destroy(struct pw_module *module); +struct pw_module * +pw_core_find_module(struct pw_core *core, const char *filename); + #ifdef __cplusplus } #endif diff --git a/src/pipewire/protocol.c b/src/pipewire/protocol.c index 02df317ba..a9b572ab0 100644 --- a/src/pipewire/protocol.c +++ b/src/pipewire/protocol.c @@ -35,9 +35,6 @@ struct pw_protocol *pw_protocol_new(struct pw_core *core, { struct pw_protocol *protocol; - if (pw_core_find_protocol(core, name) != NULL) - return NULL; - protocol = calloc(1, sizeof(struct impl) + user_data_size); protocol->core = core; protocol->name = strdup(name); @@ -116,3 +113,13 @@ pw_protocol_get_marshal(struct pw_protocol *protocol, uint32_t type) } return NULL; } + +struct pw_protocol *pw_core_find_protocol(struct pw_core *core, const char *name) +{ + struct pw_protocol *protocol; + spa_list_for_each(protocol, &core->protocol_list, link) { + if (strcmp(protocol->name, name) == 0) + return protocol; + } + return NULL; +} diff --git a/src/pipewire/protocol.h b/src/pipewire/protocol.h index 56f49059b..e87c8ea4f 100644 --- a/src/pipewire/protocol.h +++ b/src/pipewire/protocol.h @@ -123,6 +123,8 @@ void pw_protocol_add_marshal(struct pw_protocol *protocol, const struct pw_protocol_marshal * pw_protocol_get_marshal(struct pw_protocol *protocol, uint32_t type); +struct pw_protocol * pw_core_find_protocol(struct pw_core *core, const char *name); + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/src/pipewire/remote.c b/src/pipewire/remote.c index b05d6050b..b2ba4bc92 100644 --- a/src/pipewire/remote.c +++ b/src/pipewire/remote.c @@ -187,7 +187,7 @@ struct pw_remote *pw_remote_new(struct pw_core *core, pw_signal_init(&this->destroy_signal); if ((protocol_name = pw_properties_get(properties, "pipewire.protocol")) == NULL) { - if (!pw_module_load(core, "libpipewire-module-protocol-native", NULL, NULL)) + if (!pw_module_load(core, "libpipewire-module-protocol-native", NULL)) goto no_protocol; protocol_name = PW_TYPE_PROTOCOL__Native; @@ -201,7 +201,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, NULL); + pw_module_load(core, "libpipewire-module-client-node", NULL); spa_list_insert(core->remote_list.prev, &this->link); @@ -245,7 +245,7 @@ static int do_connect(struct pw_remote *remote) if (remote->core_proxy == NULL) goto no_proxy; - pw_proxy_add_listener((struct pw_proxy*)remote->core_proxy, remote, &core_events); + pw_proxy_add_listener(&remote->core_proxy->proxy, remote, &core_events); pw_core_proxy_client_update(remote->core_proxy, &remote->properties->dict); pw_core_proxy_sync(remote->core_proxy, 0);