2 * Copyright (c) 2012, Intel Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of Intel Corporation nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <murphy/common/mm.h>
36 #include "srs/daemon/context.h"
37 #include "srs/daemon/plugin.h"
39 static srs_plugin_t *find_plugin(srs_context_t *srs, const char *name)
42 mrp_list_hook_t *p, *n;
44 mrp_list_foreach(&srs->plugins, p, n) {
45 plugin = mrp_list_entry(p, typeof(*plugin), hook);
47 if (!strcmp(plugin->name, name))
55 srs_plugin_t *srs_create_plugin(srs_context_t *srs, const char *name)
57 srs_plugin_query_t query;
58 const char *plugin_name, *description, *authors, *version;
60 srs_plugin_api_t *api;
65 if (find_plugin(srs, name) != NULL) {
66 mrp_log_error("Plugin '%s' already exists.", name);
71 if (snprintf(path, sizeof(path), "%s/plugin-%s.so",
72 srs->plugin_dir, name) >= (int)sizeof(path)) {
77 mrp_log_info("Loading plugin '%s' (%s)...", name, path);
80 h = dlopen(path, RTLD_LAZY | RTLD_LOCAL);
83 const char *error = dlerror();
86 mrp_log_error("Failed to load plugin '%s' (error: %s).", name,
89 mrp_log_error("Failed to load plugin '%s'.", name);
94 plugin = mrp_allocz(sizeof(*plugin));
99 mrp_list_init(&plugin->hook);
101 query = dlsym(h, SRS_DESCRIBE_PLUGIN_FUNC);
104 mrp_log_error("Invalid plugin %s (does not export symbol '%s').",
105 path, SRS_DESCRIBE_PLUGIN_FUNC);
109 api = query(&plugin_name, &description, &authors, &version, &srs_version);
112 mrp_log_error("Invalid plugin %s (provided NULL API).", path);
116 mrp_log_info("Plugin query gave:");
117 mrp_log_info(" name: %s", plugin_name);
118 mrp_log_info(" description: %s", description);
119 mrp_log_info(" authors: %s", authors);
120 mrp_log_info(" version: %s", version);
122 if (srs_version != SRS_PLUGIN_API_VERSION) {
123 mrp_log_error("Plugin %s uses incompatible API version (0x%x != 0x%x).",
124 name, srs_version, SRS_PLUGIN_API_VERSION);
129 plugin->name = mrp_strdup(name);
130 plugin->description = description;
131 plugin->authors = authors;
134 if (!plugin->api->create(plugin)) {
135 mrp_log_error("Failed to create plugin '%s'.", name);
139 mrp_list_append(&srs->plugins, &plugin->hook);
145 srs_destroy_plugin(plugin);
154 int srs_configure_plugin(srs_plugin_t *plugin, srs_cfg_t *settings)
156 if (plugin != NULL) {
157 mrp_log_info("Configuring plugin '%s'.", plugin->name);
158 return plugin->api->config(plugin, settings);
165 int srs_start_plugin(srs_plugin_t *plugin)
167 if (plugin != NULL) {
168 mrp_log_info("Starting plugin '%s'.", plugin->name);
169 return plugin->api->start(plugin);
176 void srs_stop_plugin(srs_plugin_t *plugin)
178 if (plugin != NULL) {
179 mrp_log_info("Stopping plugin '%s'.", plugin->name);
180 plugin->api->stop(plugin);
185 void srs_destroy_plugin(srs_plugin_t *plugin)
187 if (plugin != NULL) {
188 mrp_log_info("Destroying plugin '%s'.", plugin->name);
190 mrp_list_delete(&plugin->hook);
191 plugin->api->destroy(plugin);
193 mrp_free(plugin->name);
199 int srs_configure_plugins(srs_context_t *srs)
201 srs_plugin_t *plugin;
202 mrp_list_hook_t *p, *n;
204 mrp_list_foreach(&srs->plugins, p, n) {
205 plugin = mrp_list_entry(p, typeof(*plugin), hook);
207 if (!srs_configure_plugin(plugin, srs->settings))
215 int srs_start_plugins(srs_context_t *srs)
217 srs_plugin_t *plugin;
218 mrp_list_hook_t *p, *n;
220 mrp_list_foreach(&srs->plugins, p, n) {
221 plugin = mrp_list_entry(p, typeof(*plugin), hook);
223 if (!srs_start_plugin(plugin))
231 void srs_stop_plugins(srs_context_t *srs)
233 srs_plugin_t *plugin;
234 mrp_list_hook_t *p, *n;
236 mrp_list_foreach(&srs->plugins, p, n) {
237 plugin = mrp_list_entry(p, typeof(*plugin), hook);
239 srs_stop_plugin(plugin);
244 void srs_destroy_plugins(srs_context_t *srs)
246 srs_plugin_t *plugin;
247 mrp_list_hook_t *p, *n;
249 mrp_list_foreach(&srs->plugins, p, n) {
250 plugin = mrp_list_entry(p, typeof(*plugin), hook);
252 srs_destroy_plugin(plugin);