obs-module.c 5.2 KB
Newer Older
J
jp9000 已提交
1 2 3 4 5
/******************************************************************************
    Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
6
    the Free Software Foundation, either version 2 of the License, or
J
jp9000 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "util/platform.h"
#include "util/dstr.h"

#include "obs-defs.h"
J
jp9000 已提交
22
#include "obs-internal.h"
J
jp9000 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include "obs-module.h"

void *load_module_subfunc(void *module, const char *module_name,
		const char *name, const char *func, bool required)
{
	struct dstr func_name;
	void *func_addr = NULL;

	dstr_init_copy(&func_name, name);
	dstr_cat(&func_name, "_");
	dstr_cat(&func_name, func);

	func_addr = os_dlsym(module, func_name.array);
	if (required && !func_addr)
		blog(LOG_ERROR, "Could not load function '%s' from module '%s'",
				func_name.array, module_name);

	dstr_free(&func_name);
	return func_addr;
}

J
jp9000 已提交
44
static void module_load_exports(struct obs_module *mod,
J
jp9000 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
		struct darray *output_array, const char *type,
		const size_t data_size, void *callback_ptr)
{
	bool (*enum_func)(size_t idx, const char **name);
	bool (*callback)(void*, const char*, const char*, void*);
	struct dstr enum_name;
	const char *name;
	size_t i = 0;

	callback = callback_ptr;

	dstr_init_copy(&enum_name, "enum_");
	dstr_cat(&enum_name, type);

	enum_func = os_dlsym(mod->module, enum_name.array);
	if (!enum_func)
		goto complete;

	while (enum_func(i++, &name)) {
		void *info = bmalloc(data_size);
		if (!callback(mod->module, mod->name, name, info))
			blog(LOG_ERROR, "Couldn't load '%s' because it "
			                "was missing required functions",
			                name);
		else
			darray_push_back(data_size, output_array, info);

		bfree(info);
	}

complete:
	dstr_free(&enum_name);
}

J
jp9000 已提交
79 80
extern char *find_plugin(const char *plugin);

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
/* checks API version of module and calls module_load if it exists.
 * if the API version used by the module is incompatible, fails. */
static int call_module_load(void *module, const char *path)
{
	uint32_t (*module_version)(uint32_t obs_ver) = NULL;
	bool (*module_load)(void) = NULL;
	uint32_t version, major, minor;

	module_load = os_dlsym(module, "module_load");

	module_version = os_dlsym(module, "module_version");
	if (!module_version) {
		blog(LOG_WARNING, "Module '%s' failed to load: "
		                  "module_version not found.", path);
		return MODULE_FUNCTIONNOTFOUND;
	}

	version = module_version(LIBOBS_API_VER);
	major = (version >> 16);
	minor = (version & 0xFF);

	if (major != LIBOBS_API_MAJOR_VER) {
		blog(LOG_WARNING, "Module '%s' failed to load: "
		                  "incompatible major version "
		                  "(current API: %u.%u, module version: %u.%u)",
		                  path,
		                  LIBOBS_API_MAJOR_VER, LIBOBS_API_MINOR_VER,
		                  major, minor);
		return MODULE_INCOMPATIBLE_VER;
	}

	if (minor > LIBOBS_API_MINOR_VER) {
		blog(LOG_WARNING, "Module '%s' failed to load: "
		                  "incompatible minor version "
		                  "(current API: %u.%u, module version: %u.%u)",
		                  path,
		                  LIBOBS_API_MAJOR_VER, LIBOBS_API_MINOR_VER,
		                  major, minor);
		return MODULE_INCOMPATIBLE_VER;
	}

	if (module_load && !module_load()) {
		blog(LOG_WARNING, "Module '%s' failed to load: "
		                  "module_load failed", path);
		return MODULE_ERROR;
	}

	return MODULE_SUCCESS;
}

131
int obs_load_module(const char *path)
J
jp9000 已提交
132 133
{
	struct obs_module mod;
J
jp9000 已提交
134
	char *plugin_path = find_plugin(path);
135
	int errorcode;
J
jp9000 已提交
136

137
	mod.module = os_dlopen(plugin_path);
J
jp9000 已提交
138
	bfree(plugin_path);
J
jp9000 已提交
139 140 141
	if (!mod.module)
		return MODULE_FILENOTFOUND;

142 143 144 145
	errorcode = call_module_load(mod.module, path);
	if (errorcode != MODULE_SUCCESS) {
		os_dlclose(mod.module);
		return errorcode;
J
jp9000 已提交
146 147 148
	}

	mod.name = bstrdup(path);
J
jp9000 已提交
149
	module_load_exports(&mod, &obs->input_types.da, "inputs",
150
			sizeof(struct source_info), load_source_info);
J
jp9000 已提交
151
	module_load_exports(&mod, &obs->filter_types.da, "filters",
152
			sizeof(struct source_info), load_source_info);
J
jp9000 已提交
153
	module_load_exports(&mod, &obs->transition_types.da, "transitions",
154
			sizeof(struct source_info), load_source_info);
J
jp9000 已提交
155
	module_load_exports(&mod, &obs->output_types.da, "outputs",
156
			sizeof(struct output_info), load_output_info);
157 158
	module_load_exports(&mod, &obs->encoder_types.da, "encoders",
			sizeof(struct encoder_info), load_encoder_info);
J
jp9000 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

	da_push_back(obs->modules, &mod);
	return MODULE_SUCCESS;
}

void free_module(struct obs_module *mod)
{
	if (!mod)
		return;

	if (mod->module) {
		void (*module_unload)(void);

		module_unload = os_dlsym(mod->module,
				"module_unload");
		if (module_unload)
			module_unload();

		os_dlclose(mod->module);
	}

	bfree(mod->name);
}