obs-module.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 const char *get_module_extension(void);
  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_MISSING_EXPORTS;
  26. }
  27. static int load_module_exports(struct obs_module *mod, const char *path)
  28. {
  29. mod->load = os_dlsym(mod->module, "obs_module_load");
  30. if (!mod->load)
  31. return req_func_not_found("obs_module_load", path);
  32. mod->set_pointer = os_dlsym(mod->module, "obs_module_set_pointer");
  33. if (!mod->set_pointer)
  34. return req_func_not_found("obs_module_set_pointer", path);
  35. mod->ver = os_dlsym(mod->module, "obs_module_ver");
  36. if (!mod->ver)
  37. return req_func_not_found("obs_module_ver", path);
  38. /* optional exports */
  39. mod->unload = os_dlsym(mod->module, "obs_module_unload");
  40. mod->set_locale = os_dlsym(mod->module, "obs_module_set_locale");
  41. mod->free_locale = os_dlsym(mod->module, "obs_module_free_locale");
  42. mod->name = os_dlsym(mod->module, "obs_module_name");
  43. mod->description = os_dlsym(mod->module, "obs_module_description");
  44. mod->author = os_dlsym(mod->module, "obs_module_author");
  45. return MODULE_SUCCESS;
  46. }
  47. int obs_open_module(obs_module_t **module, const char *path,
  48. const char *data_path)
  49. {
  50. struct obs_module mod = {0};
  51. int errorcode;
  52. if (!module || !path || !obs)
  53. return MODULE_ERROR;
  54. mod.module = os_dlopen(path);
  55. if (!mod.module) {
  56. blog(LOG_WARNING, "Module '%s' not found", path);
  57. return MODULE_FILE_NOT_FOUND;
  58. }
  59. errorcode = load_module_exports(&mod, path);
  60. if (errorcode != MODULE_SUCCESS)
  61. return errorcode;
  62. mod.bin_path = bstrdup(path);
  63. mod.file = strrchr(mod.bin_path, '/');
  64. mod.file = (!mod.file) ? mod.bin_path : (mod.file + 1);
  65. mod.data_path = bstrdup(data_path);
  66. mod.next = obs->first_module;
  67. if (mod.file) {
  68. blog(LOG_INFO, "---------------------------------\n"
  69. "Loading module: %s", mod.file);
  70. }
  71. *module = bmemdup(&mod, sizeof(mod));
  72. obs->first_module = (*module);
  73. mod.set_pointer(*module);
  74. if (mod.set_locale)
  75. mod.set_locale(obs->locale);
  76. return MODULE_SUCCESS;
  77. }
  78. bool obs_init_module(obs_module_t *module)
  79. {
  80. if (!module || !obs)
  81. return false;
  82. if (module->loaded)
  83. return true;
  84. module->loaded = module->load();
  85. if (!module->loaded)
  86. blog(LOG_WARNING, "Failed to initialize module '%s'",
  87. module->file);
  88. return module->loaded;
  89. }
  90. const char *obs_get_module_file_name(obs_module_t *module)
  91. {
  92. return module ? module->file : NULL;
  93. }
  94. const char *obs_get_module_name(obs_module_t *module)
  95. {
  96. return (module && module->name) ? module->name() : NULL;
  97. }
  98. const char *obs_get_module_author(obs_module_t *module)
  99. {
  100. return (module && module->author) ? module->author() : NULL;
  101. }
  102. const char *obs_get_module_description(obs_module_t *module)
  103. {
  104. return (module && module->description) ? module->description() : NULL;
  105. }
  106. const char *obs_get_module_binary_path(obs_module_t *module)
  107. {
  108. return module ? module->bin_path : NULL;
  109. }
  110. const char *obs_get_module_data_path(obs_module_t *module)
  111. {
  112. return module ? module->data_path : NULL;
  113. }
  114. char *obs_find_module_file(obs_module_t *module, const char *file)
  115. {
  116. struct dstr output = {0};
  117. if (!module)
  118. return NULL;
  119. dstr_copy(&output, module->data_path);
  120. if (!dstr_is_empty(&output) && dstr_end(&output) != '/')
  121. dstr_cat_ch(&output, '/');
  122. dstr_cat(&output, file);
  123. if (!os_file_exists(output.array))
  124. dstr_free(&output);
  125. return output.array;
  126. }
  127. void obs_add_module_path(const char *bin, const char *data)
  128. {
  129. struct obs_module_path omp;
  130. if (!obs || !bin || !data) return;
  131. omp.bin = bstrdup(bin);
  132. omp.data = bstrdup(data);
  133. da_push_back(obs->module_paths, &omp);
  134. }
  135. static void load_all_callback(void *param, const struct obs_module_info *info)
  136. {
  137. obs_module_t *module;
  138. int code = obs_open_module(&module, info->bin_path, info->data_path);
  139. if (code != MODULE_SUCCESS) {
  140. blog(LOG_DEBUG, "Failed to load module file '%s': %d",
  141. info->bin_path, code);
  142. return;
  143. }
  144. obs_init_module(module);
  145. UNUSED_PARAMETER(param);
  146. }
  147. void obs_load_all_modules(void)
  148. {
  149. obs_find_modules(load_all_callback, NULL);
  150. }
  151. static inline void make_data_dir(struct dstr *parsed_data_dir,
  152. const char *data_dir, const char *name)
  153. {
  154. dstr_copy(parsed_data_dir, data_dir);
  155. dstr_replace(parsed_data_dir, "%module%", name);
  156. if (dstr_end(parsed_data_dir) == '/')
  157. dstr_resize(parsed_data_dir, parsed_data_dir->len - 1);
  158. }
  159. static char *make_data_directory(const char *module_name, const char *data_dir)
  160. {
  161. struct dstr parsed_data_dir = {0};
  162. bool found = false;
  163. make_data_dir(&parsed_data_dir, data_dir, module_name);
  164. found = os_file_exists(parsed_data_dir.array);
  165. if (!found && astrcmpi_n(module_name, "lib", 3) == 0)
  166. make_data_dir(&parsed_data_dir, data_dir, module_name + 3);
  167. return parsed_data_dir.array;
  168. }
  169. static bool parse_binary_from_directory(struct dstr *parsed_bin_path,
  170. const char *bin_path, const char *file)
  171. {
  172. struct dstr directory = {0};
  173. bool found = true;
  174. dstr_copy(&directory, bin_path);
  175. dstr_replace(&directory, "%module%", file);
  176. if (dstr_end(&directory) != '/')
  177. dstr_cat_ch(&directory, '/');
  178. dstr_copy_dstr(parsed_bin_path, &directory);
  179. dstr_cat(parsed_bin_path, file);
  180. dstr_cat(parsed_bin_path, get_module_extension());
  181. if (!os_file_exists(parsed_bin_path->array)) {
  182. /* if the file doesn't exist, check with 'lib' prefix */
  183. dstr_copy_dstr(parsed_bin_path, &directory);
  184. dstr_cat(parsed_bin_path, "lib");
  185. dstr_cat(parsed_bin_path, file);
  186. dstr_cat(parsed_bin_path, get_module_extension());
  187. /* if neither exist, don't include this as a library */
  188. if (!os_file_exists(parsed_bin_path->array)) {
  189. dstr_free(parsed_bin_path);
  190. found = false;
  191. }
  192. }
  193. dstr_free(&directory);
  194. return found;
  195. }
  196. static void process_found_module(struct obs_module_path *omp,
  197. const char *path, bool directory,
  198. obs_find_module_callback_t callback, void *param)
  199. {
  200. struct obs_module_info info;
  201. struct dstr name = {0};
  202. struct dstr parsed_bin_path = {0};
  203. const char *file;
  204. char *parsed_data_dir;
  205. bool bin_found = true;
  206. file = strrchr(path, '/');
  207. file = file ? (file + 1) : path;
  208. if (strcmp(file, ".") == 0 || strcmp(file, "..") == 0)
  209. return;
  210. dstr_copy(&name, file);
  211. if (!directory) {
  212. char *ext = strrchr(name.array, '.');
  213. if (ext)
  214. dstr_resize(&name, ext - name.array);
  215. dstr_copy(&parsed_bin_path, path);
  216. } else {
  217. bin_found = parse_binary_from_directory(&parsed_bin_path,
  218. omp->bin, file);
  219. }
  220. parsed_data_dir = make_data_directory(name.array, omp->data);
  221. if (parsed_data_dir && bin_found) {
  222. info.bin_path = parsed_bin_path.array;
  223. info.data_path = parsed_data_dir;
  224. callback(param, &info);
  225. }
  226. bfree(parsed_data_dir);
  227. dstr_free(&name);
  228. dstr_free(&parsed_bin_path);
  229. }
  230. static void find_modules_in_path(struct obs_module_path *omp,
  231. obs_find_module_callback_t callback, void *param)
  232. {
  233. struct dstr search_path = {0};
  234. char *module_start;
  235. bool search_directories = false;
  236. os_glob_t *gi;
  237. dstr_copy(&search_path, omp->bin);
  238. module_start = strstr(search_path.array, "%module%");
  239. if (module_start) {
  240. dstr_resize(&search_path, module_start - search_path.array);
  241. search_directories = true;
  242. }
  243. if (!dstr_is_empty(&search_path) && dstr_end(&search_path) != '/')
  244. dstr_cat_ch(&search_path, '/');
  245. dstr_cat_ch(&search_path, '*');
  246. if (!search_directories)
  247. dstr_cat(&search_path, get_module_extension());
  248. if (os_glob(search_path.array, 0, &gi) == 0) {
  249. for (size_t i = 0; i < gi->gl_pathc; i++) {
  250. if (search_directories == gi->gl_pathv[i].directory)
  251. process_found_module(omp,
  252. gi->gl_pathv[i].path,
  253. search_directories,
  254. callback, param);
  255. }
  256. os_globfree(gi);
  257. }
  258. dstr_free(&search_path);
  259. }
  260. void obs_find_modules(obs_find_module_callback_t callback, void *param)
  261. {
  262. if (!obs)
  263. return;
  264. for (size_t i = 0; i < obs->module_paths.num; i++) {
  265. struct obs_module_path *omp = obs->module_paths.array + i;
  266. find_modules_in_path(omp, callback, param);
  267. }
  268. }
  269. void obs_enum_modules(obs_enum_module_callback_t callback, void *param)
  270. {
  271. struct obs_module *module;
  272. if (!obs)
  273. return;
  274. module = obs->first_module;
  275. while (module) {
  276. callback(param, module);
  277. module = module->next;
  278. }
  279. }
  280. void free_module(struct obs_module *mod)
  281. {
  282. if (!mod)
  283. return;
  284. if (mod->module) {
  285. if (mod->free_locale)
  286. mod->free_locale();
  287. if (mod->loaded && mod->unload)
  288. mod->unload();
  289. /* there is no real reason to close the dynamic libraries,
  290. * and sometimes this can cause issues. */
  291. /* os_dlclose(mod->module); */
  292. }
  293. bfree(mod->bin_path);
  294. bfree(mod->data_path);
  295. bfree(mod);
  296. }
  297. lookup_t *obs_module_load_locale(obs_module_t *module,
  298. const char *default_locale, const char *locale)
  299. {
  300. struct dstr str = {0};
  301. lookup_t *lookup = NULL;
  302. if (!module || !default_locale || !locale) {
  303. blog(LOG_WARNING, "obs_module_load_locale: Invalid parameters");
  304. return NULL;
  305. }
  306. dstr_copy(&str, "locale/");
  307. dstr_cat(&str, default_locale);
  308. dstr_cat(&str, ".ini");
  309. char *file = obs_find_module_file(module, str.array);
  310. if (file)
  311. lookup = text_lookup_create(file);
  312. bfree(file);
  313. if (!lookup) {
  314. blog(LOG_WARNING, "Failed to load '%s' text for module: '%s'",
  315. default_locale, module->file);
  316. goto cleanup;
  317. }
  318. if (astrcmpi(locale, default_locale) == 0)
  319. goto cleanup;
  320. dstr_copy(&str, "/locale/");
  321. dstr_cat(&str, locale);
  322. dstr_cat(&str, ".ini");
  323. file = obs_find_module_file(module, str.array);
  324. if (!text_lookup_add(lookup, file))
  325. blog(LOG_WARNING, "Failed to load '%s' text for module: '%s'",
  326. locale, module->file);
  327. bfree(file);
  328. cleanup:
  329. dstr_free(&str);
  330. return lookup;
  331. }
  332. #define REGISTER_OBS_DEF(size_var, structure, dest, info) \
  333. do { \
  334. struct structure data = {0}; \
  335. if (!size_var) { \
  336. blog(LOG_ERROR, "Tried to register " #structure \
  337. " outside of obs_module_load"); \
  338. return; \
  339. } \
  340. \
  341. memcpy(&data, info, size_var); \
  342. da_push_back(dest, &data); \
  343. } while (false)
  344. #define CHECK_REQUIRED_VAL(info, val, func) \
  345. do { \
  346. if (!info->val) {\
  347. blog(LOG_ERROR, "Required value '" #val " for" \
  348. "'%s' not found. " #func \
  349. " failed.", info->id); \
  350. return; \
  351. } \
  352. } while (false)
  353. void obs_register_source_s(const struct obs_source_info *info, size_t size)
  354. {
  355. struct obs_source_info data = {0};
  356. struct darray *array;
  357. if (info->type == OBS_SOURCE_TYPE_INPUT) {
  358. array = &obs->input_types.da;
  359. } else if (info->type == OBS_SOURCE_TYPE_FILTER) {
  360. array = &obs->filter_types.da;
  361. } else if (info->type == OBS_SOURCE_TYPE_TRANSITION) {
  362. array = &obs->transition_types.da;
  363. } else {
  364. blog(LOG_ERROR, "Tried to register unknown source type: %u",
  365. info->type);
  366. return;
  367. }
  368. if (find_source(array, info->id)) {
  369. blog(LOG_WARNING, "Source d '%s' already exists! "
  370. "Duplicate library?", info->id);
  371. return;
  372. }
  373. CHECK_REQUIRED_VAL(info, get_name, obs_register_source);
  374. CHECK_REQUIRED_VAL(info, create, obs_register_source);
  375. CHECK_REQUIRED_VAL(info, destroy, obs_register_source);
  376. if (info->type == OBS_SOURCE_TYPE_INPUT &&
  377. (info->output_flags & OBS_SOURCE_VIDEO) != 0 &&
  378. (info->output_flags & OBS_SOURCE_ASYNC) == 0) {
  379. CHECK_REQUIRED_VAL(info, get_width, obs_register_source);
  380. CHECK_REQUIRED_VAL(info, get_height, obs_register_source);
  381. }
  382. memcpy(&data, info, size);
  383. /* mark audio-only filters as an async filter categorically */
  384. if (data.type == OBS_SOURCE_TYPE_FILTER) {
  385. if ((data.output_flags & OBS_SOURCE_VIDEO) == 0)
  386. data.output_flags |= OBS_SOURCE_ASYNC;
  387. }
  388. darray_push_back(sizeof(struct obs_source_info), array, &data);
  389. }
  390. void obs_register_output_s(const struct obs_output_info *info, size_t size)
  391. {
  392. if (find_output(info->id)) {
  393. blog(LOG_WARNING, "Output id '%s' already exists! "
  394. "Duplicate library?", info->id);
  395. return;
  396. }
  397. CHECK_REQUIRED_VAL(info, get_name, obs_register_output);
  398. CHECK_REQUIRED_VAL(info, create, obs_register_output);
  399. CHECK_REQUIRED_VAL(info, destroy, obs_register_output);
  400. CHECK_REQUIRED_VAL(info, start, obs_register_output);
  401. CHECK_REQUIRED_VAL(info, stop, obs_register_output);
  402. if (info->flags & OBS_OUTPUT_ENCODED) {
  403. CHECK_REQUIRED_VAL(info, encoded_packet, obs_register_output);
  404. } else {
  405. if (info->flags & OBS_OUTPUT_VIDEO)
  406. CHECK_REQUIRED_VAL(info, raw_video,
  407. obs_register_output);
  408. if (info->flags & OBS_OUTPUT_AUDIO)
  409. CHECK_REQUIRED_VAL(info, raw_audio,
  410. obs_register_output);
  411. }
  412. REGISTER_OBS_DEF(size, obs_output_info, obs->output_types, info);
  413. }
  414. void obs_register_encoder_s(const struct obs_encoder_info *info, size_t size)
  415. {
  416. if (find_encoder(info->id)) {
  417. blog(LOG_WARNING, "Encoder id '%s' already exists! "
  418. "Duplicate library?", info->id);
  419. return;
  420. }
  421. CHECK_REQUIRED_VAL(info, get_name, obs_register_encoder);
  422. CHECK_REQUIRED_VAL(info, create, obs_register_encoder);
  423. CHECK_REQUIRED_VAL(info, destroy, obs_register_encoder);
  424. CHECK_REQUIRED_VAL(info, encode, obs_register_encoder);
  425. if (info->type == OBS_ENCODER_AUDIO)
  426. CHECK_REQUIRED_VAL(info, get_frame_size, obs_register_encoder);
  427. REGISTER_OBS_DEF(size, obs_encoder_info, obs->encoder_types, info);
  428. }
  429. void obs_register_service_s(const struct obs_service_info *info, size_t size)
  430. {
  431. if (find_service(info->id)) {
  432. blog(LOG_WARNING, "Service id '%s' already exists! "
  433. "Duplicate library?", info->id);
  434. return;
  435. }
  436. CHECK_REQUIRED_VAL(info, get_name, obs_register_service);
  437. CHECK_REQUIRED_VAL(info, create, obs_register_service);
  438. CHECK_REQUIRED_VAL(info, destroy, obs_register_service);
  439. REGISTER_OBS_DEF(size, obs_service_info, obs->service_types, info);
  440. }
  441. void obs_regsiter_modal_ui_s(const struct obs_modal_ui *info, size_t size)
  442. {
  443. CHECK_REQUIRED_VAL(info, task, obs_regsiter_modal_ui);
  444. CHECK_REQUIRED_VAL(info, target, obs_regsiter_modal_ui);
  445. CHECK_REQUIRED_VAL(info, exec, obs_regsiter_modal_ui);
  446. REGISTER_OBS_DEF(size, obs_modal_ui, obs->modal_ui_callbacks, info);
  447. }
  448. void obs_regsiter_modeless_ui_s(const struct obs_modeless_ui *info, size_t size)
  449. {
  450. CHECK_REQUIRED_VAL(info, task, obs_regsiter_modeless_ui);
  451. CHECK_REQUIRED_VAL(info, target, obs_regsiter_modeless_ui);
  452. CHECK_REQUIRED_VAL(info, create, obs_regsiter_modeless_ui);
  453. REGISTER_OBS_DEF(size, obs_modeless_ui, obs->modeless_ui_callbacks,
  454. info);
  455. }