obs-module.c 18 KB

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