obs-module.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /******************************************************************************
  2. Copyright (C) 2013-2014 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. extern char *find_plugin(const char *plugin);
  20. extern const char *get_module_extension(void);
  21. static inline int req_func_not_found(const char *name, const char *path)
  22. {
  23. blog(LOG_ERROR, "Required module function '%s' in module '%s' not "
  24. "found, loading of module failed",
  25. name, path);
  26. return MODULE_MISSING_EXPORTS;
  27. }
  28. #define LOAD_REQ_SIZE_FUNC(func, module, path) \
  29. func = os_dlsym(module, #func); \
  30. if (!func) \
  31. return req_func_not_found(#func, path)
  32. static int call_module_load(void *module, const char *path)
  33. {
  34. bool (*obs_module_load)(uint32_t obs_ver) = NULL;
  35. obs_module_load = os_dlsym(module, "obs_module_load");
  36. if (!obs_module_load)
  37. return req_func_not_found("obs_module_load", path);
  38. if (!obs_module_load(LIBOBS_API_VER)) {
  39. blog(LOG_ERROR, "Module '%s' failed to load: "
  40. "obs_module_load failed", path);
  41. return MODULE_ERROR;
  42. }
  43. return MODULE_SUCCESS;
  44. }
  45. int obs_load_module(const char *path)
  46. {
  47. struct obs_module mod;
  48. char *plugin_path = find_plugin(path);
  49. int errorcode;
  50. mod.module = os_dlopen(plugin_path);
  51. bfree(plugin_path);
  52. if (!mod.module) {
  53. blog(LOG_WARNING, "Module '%s' not found", path);
  54. return MODULE_FILE_NOT_FOUND;
  55. }
  56. errorcode = call_module_load(mod.module, path);
  57. if (errorcode != MODULE_SUCCESS) {
  58. os_dlclose(mod.module);
  59. return errorcode;
  60. }
  61. mod.name = bstrdup(path);
  62. mod.set_locale = os_dlsym(mod.module, "obs_module_set_locale");
  63. if (mod.set_locale)
  64. mod.set_locale(obs->locale);
  65. da_push_back(obs->modules, &mod);
  66. return MODULE_SUCCESS;
  67. }
  68. void free_module(struct obs_module *mod)
  69. {
  70. if (!mod)
  71. return;
  72. if (mod->module) {
  73. void (*module_unload)(void);
  74. module_unload = os_dlsym(mod->module, "obs_module_unload");
  75. if (module_unload)
  76. module_unload();
  77. os_dlclose(mod->module);
  78. }
  79. bfree(mod->name);
  80. }
  81. lookup_t obs_module_load_locale(const char *module, const char *default_locale,
  82. const char *locale)
  83. {
  84. struct dstr str = {0};
  85. lookup_t lookup = NULL;
  86. if (!module || !default_locale || !locale) {
  87. blog(LOG_WARNING, "obs_module_load_locale: Invalid parameters");
  88. return NULL;
  89. }
  90. dstr_copy(&str, module);
  91. dstr_cat(&str, "/locale/");
  92. dstr_cat(&str, default_locale);
  93. dstr_cat(&str, ".ini");
  94. char *file = obs_find_plugin_file(str.array);
  95. if (file)
  96. lookup = text_lookup_create(file);
  97. bfree(file);
  98. if (!lookup) {
  99. blog(LOG_WARNING, "Failed to load '%s' text for module: '%s'",
  100. default_locale, module);
  101. goto cleanup;
  102. }
  103. if (astrcmpi(locale, default_locale) == 0)
  104. goto cleanup;
  105. dstr_copy(&str, module);
  106. dstr_cat(&str, "/locale/");
  107. dstr_cat(&str, locale);
  108. dstr_cat(&str, ".ini");
  109. file = obs_find_plugin_file(str.array);
  110. if (!text_lookup_add(lookup, file))
  111. blog(LOG_WARNING, "Failed to load '%s' text for module: '%s'",
  112. locale, module);
  113. bfree(file);
  114. cleanup:
  115. dstr_free(&str);
  116. return lookup;
  117. }
  118. #define REGISTER_OBS_DEF(size_var, structure, dest, info) \
  119. do { \
  120. struct structure data = {0}; \
  121. if (!size_var) { \
  122. blog(LOG_ERROR, "Tried to register " #structure \
  123. " outside of obs_module_load"); \
  124. return; \
  125. } \
  126. \
  127. memcpy(&data, info, size_var); \
  128. da_push_back(dest, &data); \
  129. } while (false)
  130. #define CHECK_REQUIRED_VAL(info, val, func) \
  131. do { \
  132. if (!info->val) {\
  133. blog(LOG_ERROR, "Required value '" #val " for" \
  134. "'%s' not found. " #func \
  135. " failed.", info->id); \
  136. return; \
  137. } \
  138. } while (false)
  139. void obs_register_source_s(const struct obs_source_info *info, size_t size)
  140. {
  141. struct obs_source_info data = {0};
  142. struct darray *array;
  143. CHECK_REQUIRED_VAL(info, getname, obs_register_source);
  144. CHECK_REQUIRED_VAL(info, create, obs_register_source);
  145. CHECK_REQUIRED_VAL(info, destroy, obs_register_source);
  146. if (info->type == OBS_SOURCE_TYPE_INPUT &&
  147. (info->output_flags & OBS_SOURCE_VIDEO) != 0 &&
  148. (info->output_flags & OBS_SOURCE_ASYNC) == 0) {
  149. CHECK_REQUIRED_VAL(info, getwidth, obs_register_source);
  150. CHECK_REQUIRED_VAL(info, getheight, obs_register_source);
  151. }
  152. memcpy(&data, info, size);
  153. if (info->type == OBS_SOURCE_TYPE_INPUT) {
  154. array = &obs->input_types.da;
  155. } else if (info->type == OBS_SOURCE_TYPE_FILTER) {
  156. array = &obs->filter_types.da;
  157. } else if (info->type == OBS_SOURCE_TYPE_TRANSITION) {
  158. array = &obs->transition_types.da;
  159. } else {
  160. blog(LOG_ERROR, "Tried to register unknown source type: %u",
  161. info->type);
  162. return;
  163. }
  164. darray_push_back(sizeof(struct obs_source_info), array, &data);
  165. }
  166. void obs_register_output_s(const struct obs_output_info *info, size_t size)
  167. {
  168. CHECK_REQUIRED_VAL(info, getname, obs_register_output);
  169. CHECK_REQUIRED_VAL(info, create, obs_register_output);
  170. CHECK_REQUIRED_VAL(info, destroy, obs_register_output);
  171. CHECK_REQUIRED_VAL(info, start, obs_register_output);
  172. CHECK_REQUIRED_VAL(info, stop, obs_register_output);
  173. if (info->flags & OBS_OUTPUT_ENCODED) {
  174. CHECK_REQUIRED_VAL(info, encoded_packet, obs_register_output);
  175. } else {
  176. if (info->flags & OBS_OUTPUT_VIDEO)
  177. CHECK_REQUIRED_VAL(info, raw_video,
  178. obs_register_output);
  179. if (info->flags & OBS_OUTPUT_AUDIO)
  180. CHECK_REQUIRED_VAL(info, raw_audio,
  181. obs_register_output);
  182. }
  183. REGISTER_OBS_DEF(size, obs_output_info, obs->output_types, info);
  184. }
  185. void obs_register_encoder_s(const struct obs_encoder_info *info, size_t size)
  186. {
  187. CHECK_REQUIRED_VAL(info, getname, obs_register_encoder);
  188. CHECK_REQUIRED_VAL(info, create, obs_register_encoder);
  189. CHECK_REQUIRED_VAL(info, destroy, obs_register_encoder);
  190. CHECK_REQUIRED_VAL(info, encode, obs_register_encoder);
  191. if (info->type == OBS_ENCODER_AUDIO)
  192. CHECK_REQUIRED_VAL(info, frame_size, obs_register_encoder);
  193. REGISTER_OBS_DEF(size, obs_encoder_info, obs->encoder_types, info);
  194. }
  195. void obs_register_service_s(const struct obs_service_info *info, size_t size)
  196. {
  197. CHECK_REQUIRED_VAL(info, getname, obs_register_service);
  198. CHECK_REQUIRED_VAL(info, create, obs_register_service);
  199. CHECK_REQUIRED_VAL(info, destroy, obs_register_service);
  200. REGISTER_OBS_DEF(size, obs_service_info, obs->service_types, info);
  201. }
  202. void obs_regsiter_modal_ui_s(const struct obs_modal_ui *info, size_t size)
  203. {
  204. CHECK_REQUIRED_VAL(info, task, obs_regsiter_modal_ui);
  205. CHECK_REQUIRED_VAL(info, target, obs_regsiter_modal_ui);
  206. CHECK_REQUIRED_VAL(info, exec, obs_regsiter_modal_ui);
  207. REGISTER_OBS_DEF(size, obs_modal_ui, obs->modal_ui_callbacks, info);
  208. }
  209. void obs_regsiter_modeless_ui_s(const struct obs_modeless_ui *info, size_t size)
  210. {
  211. CHECK_REQUIRED_VAL(info, task, obs_regsiter_modeless_ui);
  212. CHECK_REQUIRED_VAL(info, target, obs_regsiter_modeless_ui);
  213. CHECK_REQUIRED_VAL(info, create, obs_regsiter_modeless_ui);
  214. REGISTER_OBS_DEF(size, obs_modeless_ui, obs->modeless_ui_callbacks,
  215. info);
  216. }