obs-module.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. da_push_back(obs->modules, &mod);
  62. return MODULE_SUCCESS;
  63. }
  64. void free_module(struct obs_module *mod)
  65. {
  66. if (!mod)
  67. return;
  68. if (mod->module) {
  69. void (*module_unload)(void);
  70. module_unload = os_dlsym(mod->module, "module_unload");
  71. if (module_unload)
  72. module_unload();
  73. os_dlclose(mod->module);
  74. }
  75. bfree(mod->name);
  76. }
  77. #define REGISTER_OBS_DEF(size_var, structure, dest, info) \
  78. do { \
  79. struct structure data = {0}; \
  80. if (!size_var) { \
  81. blog(LOG_ERROR, "Tried to register " #structure \
  82. " outside of obs_module_load"); \
  83. return; \
  84. } \
  85. \
  86. memcpy(&data, info, size_var); \
  87. da_push_back(dest, &data); \
  88. } while (false)
  89. #define CHECK_REQUIRED_VAL(info, val, func) \
  90. do { \
  91. if (!info->val) {\
  92. blog(LOG_ERROR, "Required value '" #val " for" \
  93. "'%s' not found. " #func \
  94. " failed.", info->id); \
  95. return; \
  96. } \
  97. } while (false)
  98. void obs_register_source_s(const struct obs_source_info *info, size_t size)
  99. {
  100. struct obs_source_info data = {0};
  101. struct darray *array;
  102. CHECK_REQUIRED_VAL(info, getname, obs_register_source);
  103. CHECK_REQUIRED_VAL(info, create, obs_register_source);
  104. CHECK_REQUIRED_VAL(info, destroy, obs_register_source);
  105. if (info->type == OBS_SOURCE_TYPE_INPUT &&
  106. (info->output_flags & OBS_SOURCE_VIDEO) != 0 &&
  107. (info->output_flags & OBS_SOURCE_ASYNC) == 0) {
  108. CHECK_REQUIRED_VAL(info, getwidth, obs_register_source);
  109. CHECK_REQUIRED_VAL(info, getheight, obs_register_source);
  110. }
  111. memcpy(&data, info, size);
  112. if (info->type == OBS_SOURCE_TYPE_INPUT) {
  113. array = &obs->input_types.da;
  114. } else if (info->type == OBS_SOURCE_TYPE_FILTER) {
  115. array = &obs->filter_types.da;
  116. } else if (info->type == OBS_SOURCE_TYPE_TRANSITION) {
  117. array = &obs->transition_types.da;
  118. } else {
  119. blog(LOG_ERROR, "Tried to register unknown source type: %u",
  120. info->type);
  121. return;
  122. }
  123. darray_push_back(sizeof(struct obs_source_info), array, &data);
  124. }
  125. void obs_register_output_s(const struct obs_output_info *info, size_t size)
  126. {
  127. CHECK_REQUIRED_VAL(info, getname, obs_register_output);
  128. CHECK_REQUIRED_VAL(info, create, obs_register_output);
  129. CHECK_REQUIRED_VAL(info, destroy, obs_register_output);
  130. CHECK_REQUIRED_VAL(info, start, obs_register_output);
  131. CHECK_REQUIRED_VAL(info, stop, obs_register_output);
  132. if (info->flags & OBS_OUTPUT_ENCODED) {
  133. CHECK_REQUIRED_VAL(info, encoded_packet, obs_register_output);
  134. } else {
  135. if (info->flags & OBS_OUTPUT_VIDEO)
  136. CHECK_REQUIRED_VAL(info, raw_video,
  137. obs_register_output);
  138. if (info->flags & OBS_OUTPUT_AUDIO)
  139. CHECK_REQUIRED_VAL(info, raw_audio,
  140. obs_register_output);
  141. }
  142. REGISTER_OBS_DEF(size, obs_output_info, obs->output_types, info);
  143. }
  144. void obs_register_encoder_s(const struct obs_encoder_info *info, size_t size)
  145. {
  146. CHECK_REQUIRED_VAL(info, getname, obs_register_encoder);
  147. CHECK_REQUIRED_VAL(info, create, obs_register_encoder);
  148. CHECK_REQUIRED_VAL(info, destroy, obs_register_encoder);
  149. CHECK_REQUIRED_VAL(info, encode, obs_register_encoder);
  150. if (info->type == OBS_ENCODER_AUDIO)
  151. CHECK_REQUIRED_VAL(info, frame_size, obs_register_encoder);
  152. REGISTER_OBS_DEF(size, obs_encoder_info, obs->encoder_types, info);
  153. }
  154. void obs_register_service_s(const struct obs_service_info *info, size_t size)
  155. {
  156. CHECK_REQUIRED_VAL(info, getname, obs_register_service);
  157. CHECK_REQUIRED_VAL(info, create, obs_register_service);
  158. CHECK_REQUIRED_VAL(info, destroy, obs_register_service);
  159. REGISTER_OBS_DEF(size, obs_service_info, obs->service_types, info);
  160. }
  161. void obs_regsiter_modal_ui_s(const struct obs_modal_ui *info, size_t size)
  162. {
  163. CHECK_REQUIRED_VAL(info, task, obs_regsiter_modal_ui);
  164. CHECK_REQUIRED_VAL(info, target, obs_regsiter_modal_ui);
  165. CHECK_REQUIRED_VAL(info, exec, obs_regsiter_modal_ui);
  166. REGISTER_OBS_DEF(size, obs_modal_ui, obs->modal_ui_callbacks, info);
  167. }
  168. void obs_regsiter_modeless_ui_s(const struct obs_modeless_ui *info, size_t size)
  169. {
  170. CHECK_REQUIRED_VAL(info, task, obs_regsiter_modeless_ui);
  171. CHECK_REQUIRED_VAL(info, target, obs_regsiter_modeless_ui);
  172. CHECK_REQUIRED_VAL(info, create, obs_regsiter_modeless_ui);
  173. REGISTER_OBS_DEF(size, obs_modeless_ui, obs->modeless_ui_callbacks,
  174. info);
  175. }