obs-module.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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-data.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. extern char *find_plugin(const char *plugin);
  63. /* checks API version of module and calls module_load if it exists.
  64. * if the API version used by the module is incompatible, fails. */
  65. static int call_module_load(void *module, const char *path)
  66. {
  67. uint32_t (*module_version)(uint32_t obs_ver) = NULL;
  68. bool (*module_load)(void) = NULL;
  69. uint32_t version, major, minor;
  70. module_load = os_dlsym(module, "module_load");
  71. module_version = os_dlsym(module, "module_version");
  72. if (!module_version) {
  73. blog(LOG_WARNING, "Module '%s' failed to load: "
  74. "module_version not found.", path);
  75. return MODULE_FUNCTIONNOTFOUND;
  76. }
  77. version = module_version(LIBOBS_API_VER);
  78. major = (version >> 16);
  79. minor = (version & 0xFF);
  80. if (major != LIBOBS_API_MAJOR_VER) {
  81. blog(LOG_WARNING, "Module '%s' failed to load: "
  82. "incompatible major version "
  83. "(current API: %u.%u, module version: %u.%u)",
  84. path,
  85. LIBOBS_API_MAJOR_VER, LIBOBS_API_MINOR_VER,
  86. major, minor);
  87. return MODULE_INCOMPATIBLE_VER;
  88. }
  89. if (minor > LIBOBS_API_MINOR_VER) {
  90. blog(LOG_WARNING, "Module '%s' failed to load: "
  91. "incompatible minor version "
  92. "(current API: %u.%u, module version: %u.%u)",
  93. path,
  94. LIBOBS_API_MAJOR_VER, LIBOBS_API_MINOR_VER,
  95. major, minor);
  96. return MODULE_INCOMPATIBLE_VER;
  97. }
  98. if (module_load && !module_load()) {
  99. blog(LOG_WARNING, "Module '%s' failed to load: "
  100. "module_load failed", path);
  101. return MODULE_ERROR;
  102. }
  103. return MODULE_SUCCESS;
  104. }
  105. int obs_load_module(const char *path)
  106. {
  107. struct obs_module mod;
  108. char *plugin_path = find_plugin(path);
  109. int errorcode;
  110. mod.module = os_dlopen(plugin_path);
  111. bfree(plugin_path);
  112. if (!mod.module)
  113. return MODULE_FILENOTFOUND;
  114. errorcode = call_module_load(mod.module, path);
  115. if (errorcode != MODULE_SUCCESS) {
  116. os_dlclose(mod.module);
  117. return errorcode;
  118. }
  119. mod.name = bstrdup(path);
  120. module_load_exports(&mod, &obs->input_types.da, "inputs",
  121. sizeof(struct source_info), load_source_info);
  122. module_load_exports(&mod, &obs->filter_types.da, "filters",
  123. sizeof(struct source_info), load_source_info);
  124. module_load_exports(&mod, &obs->transition_types.da, "transitions",
  125. sizeof(struct source_info), load_source_info);
  126. module_load_exports(&mod, &obs->output_types.da, "outputs",
  127. sizeof(struct output_info), load_output_info);
  128. module_load_exports(&mod, &obs->encoder_types.da, "encoders",
  129. sizeof(struct encoder_info), load_encoder_info);
  130. da_push_back(obs->modules, &mod);
  131. return MODULE_SUCCESS;
  132. }
  133. void free_module(struct obs_module *mod)
  134. {
  135. if (!mod)
  136. return;
  137. if (mod->module) {
  138. void (*module_unload)(void);
  139. module_unload = os_dlsym(mod->module,
  140. "module_unload");
  141. if (module_unload)
  142. module_unload();
  143. os_dlclose(mod->module);
  144. }
  145. bfree(mod->name);
  146. }