obs-module.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "util/platform.h"
  15. #include "util/dstr.h"
  16. #include "obs-defs.h"
  17. #include "obs-internal.h"
  18. #include "obs-module.h"
  19. void *load_module_subfunc(void *module, const char *module_name,
  20. const char *name, const char *func, bool required)
  21. {
  22. struct dstr func_name;
  23. void *func_addr = NULL;
  24. dstr_init_copy(&func_name, name);
  25. dstr_cat(&func_name, "_");
  26. dstr_cat(&func_name, func);
  27. func_addr = os_dlsym(module, func_name.array);
  28. if (required && !func_addr)
  29. blog(LOG_ERROR, "Could not load function '%s' from module '%s'",
  30. func_name.array, module_name);
  31. dstr_free(&func_name);
  32. return func_addr;
  33. }
  34. static void module_load_exports(struct obs_module *mod,
  35. struct darray *output_array, const char *type,
  36. const size_t data_size, void *callback_ptr)
  37. {
  38. bool (*enum_func)(size_t idx, const char **name);
  39. bool (*callback)(void*, const char*, const char*, void*);
  40. struct dstr enum_name;
  41. const char *name;
  42. size_t i = 0;
  43. callback = callback_ptr;
  44. dstr_init_copy(&enum_name, "enum_");
  45. dstr_cat(&enum_name, type);
  46. enum_func = os_dlsym(mod->module, enum_name.array);
  47. if (!enum_func)
  48. goto complete;
  49. while (enum_func(i++, &name)) {
  50. void *info = bmalloc(data_size);
  51. if (!callback(mod->module, mod->name, name, info))
  52. blog(LOG_ERROR, "Couldn't load '%s' because it "
  53. "was missing required functions",
  54. name);
  55. else
  56. darray_push_back(data_size, output_array, info);
  57. bfree(info);
  58. }
  59. complete:
  60. dstr_free(&enum_name);
  61. }
  62. struct generic_ui_callback {
  63. struct obs_ui_info ui_info;
  64. void *callback;
  65. };
  66. static void module_load_ui_exports(struct obs_module *mod,
  67. const char *main_export, struct darray *array)
  68. {
  69. bool (*enum_func)(size_t idx, struct obs_ui_info *info);
  70. struct obs_ui_info ui_info;
  71. size_t i = 0;
  72. enum_func = os_dlsym(mod->module, main_export);
  73. if (!enum_func)
  74. return;
  75. while (enum_func(i++, &ui_info)) {
  76. struct generic_ui_callback callback;
  77. struct dstr name;
  78. dstr_init_copy(&name, ui_info.name);
  79. dstr_cat(&name, "_");
  80. dstr_cat(&name, ui_info.task);
  81. dstr_cat(&name, "_");
  82. dstr_cat(&name, ui_info.target);
  83. callback.ui_info = ui_info;
  84. callback.callback = os_dlsym(mod->module, name.array);
  85. if (!callback.callback) {
  86. blog(LOG_WARNING, "Module '%s' enumerated UI callback "
  87. "'%s', but the function was not "
  88. "found", mod->name, name.array);
  89. } else {
  90. darray_push_back(sizeof(struct generic_ui_callback),
  91. array, &callback);
  92. }
  93. dstr_free(&name);
  94. }
  95. }
  96. extern char *find_plugin(const char *plugin);
  97. /* checks API version of module and calls module_load if it exists.
  98. * if the API version used by the module is incompatible, fails. */
  99. static int call_module_load(void *module, const char *path)
  100. {
  101. uint32_t (*module_version)(uint32_t obs_ver) = NULL;
  102. bool (*module_load)(void) = NULL;
  103. uint32_t version, major, minor;
  104. module_load = os_dlsym(module, "module_load");
  105. module_version = os_dlsym(module, "module_version");
  106. if (!module_version) {
  107. blog(LOG_WARNING, "Module '%s' failed to load: "
  108. "module_version not found.", path);
  109. return MODULE_FUNCTIONNOTFOUND;
  110. }
  111. version = module_version(LIBOBS_API_VER);
  112. major = (version >> 16);
  113. minor = (version & 0xFF);
  114. if (major != LIBOBS_API_MAJOR_VER) {
  115. blog(LOG_WARNING, "Module '%s' failed to load: "
  116. "incompatible major version "
  117. "(current API: %u.%u, module version: %u.%u)",
  118. path,
  119. LIBOBS_API_MAJOR_VER, LIBOBS_API_MINOR_VER,
  120. major, minor);
  121. return MODULE_INCOMPATIBLE_VER;
  122. }
  123. if (minor > LIBOBS_API_MINOR_VER) {
  124. blog(LOG_WARNING, "Module '%s' failed to load: "
  125. "incompatible minor version "
  126. "(current API: %u.%u, module version: %u.%u)",
  127. path,
  128. LIBOBS_API_MAJOR_VER, LIBOBS_API_MINOR_VER,
  129. major, minor);
  130. return MODULE_INCOMPATIBLE_VER;
  131. }
  132. if (module_load && !module_load()) {
  133. blog(LOG_WARNING, "Module '%s' failed to load: "
  134. "module_load failed", path);
  135. return MODULE_ERROR;
  136. }
  137. return MODULE_SUCCESS;
  138. }
  139. int obs_load_module(const char *path)
  140. {
  141. struct obs_module mod;
  142. char *plugin_path = find_plugin(path);
  143. int errorcode;
  144. mod.module = os_dlopen(plugin_path);
  145. bfree(plugin_path);
  146. if (!mod.module)
  147. return MODULE_FILENOTFOUND;
  148. errorcode = call_module_load(mod.module, path);
  149. if (errorcode != MODULE_SUCCESS) {
  150. os_dlclose(mod.module);
  151. return errorcode;
  152. }
  153. mod.name = bstrdup(path);
  154. module_load_exports(&mod, &obs->input_types.da, "inputs",
  155. sizeof(struct source_info), load_source_info);
  156. module_load_exports(&mod, &obs->filter_types.da, "filters",
  157. sizeof(struct source_info), load_source_info);
  158. module_load_exports(&mod, &obs->transition_types.da, "transitions",
  159. sizeof(struct source_info), load_source_info);
  160. module_load_exports(&mod, &obs->output_types.da, "outputs",
  161. sizeof(struct output_info), load_output_info);
  162. module_load_exports(&mod, &obs->encoder_types.da, "encoders",
  163. sizeof(struct encoder_info), load_encoder_info);
  164. module_load_ui_exports(&mod, "enum_ui", &obs->ui_callbacks.da);
  165. module_load_ui_exports(&mod, "enum_modeless_ui",
  166. &obs->ui_modeless_callbacks.da);
  167. da_push_back(obs->modules, &mod);
  168. return MODULE_SUCCESS;
  169. }
  170. void free_module(struct obs_module *mod)
  171. {
  172. if (!mod)
  173. return;
  174. if (mod->module) {
  175. void (*module_unload)(void);
  176. module_unload = os_dlsym(mod->module,
  177. "module_unload");
  178. if (module_unload)
  179. module_unload();
  180. os_dlclose(mod->module);
  181. }
  182. bfree(mod->name);
  183. }