obs-module.c 7.0 KB

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