1
0

obs-module.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 3 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_data *obs, 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. int obs_load_module(struct obs_data *obs, const char *path)
  63. {
  64. struct obs_module mod;
  65. bool (*module_load)(void) = NULL;
  66. mod.module = os_dlopen(path);
  67. if (!mod.module)
  68. return MODULE_FILENOTFOUND;
  69. module_load = os_dlsym(mod.module, "module_load");
  70. if (module_load) {
  71. if (!module_load()) {
  72. os_dlclose(mod.module);
  73. return MODULE_ERROR;
  74. }
  75. }
  76. mod.name = bstrdup(path);
  77. module_load_exports(obs, &mod, &obs->input_types.da, "inputs",
  78. sizeof(struct source_info), get_source_info);
  79. module_load_exports(obs, &mod, &obs->filter_types.da, "filters",
  80. sizeof(struct source_info), get_source_info);
  81. module_load_exports(obs, &mod, &obs->transition_types.da, "transitions",
  82. sizeof(struct source_info), get_source_info);
  83. module_load_exports(obs, &mod, &obs->output_types.da, "outputs",
  84. sizeof(struct output_info), get_output_info);
  85. da_push_back(obs->modules, &mod);
  86. return MODULE_SUCCESS;
  87. }
  88. void free_module(struct obs_module *mod)
  89. {
  90. if (!mod)
  91. return;
  92. if (mod->module) {
  93. void (*module_unload)(void);
  94. module_unload = os_dlsym(mod->module,
  95. "module_unload");
  96. if (module_unload)
  97. module_unload();
  98. os_dlclose(mod->module);
  99. }
  100. bfree(mod->name);
  101. }