obs-module.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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. obs_module_t *loadingModule = NULL;
  21. static inline int req_func_not_found(const char *name, const char *path)
  22. {
  23. blog(LOG_DEBUG,
  24. "Required module function '%s' in module '%s' not "
  25. "found, loading of module failed",
  26. name, path);
  27. return MODULE_MISSING_EXPORTS;
  28. }
  29. static int load_module_exports(struct obs_module *mod, const char *path)
  30. {
  31. mod->load = os_dlsym(mod->module, "obs_module_load");
  32. if (!mod->load)
  33. return req_func_not_found("obs_module_load", path);
  34. mod->set_pointer = os_dlsym(mod->module, "obs_module_set_pointer");
  35. if (!mod->set_pointer)
  36. return req_func_not_found("obs_module_set_pointer", path);
  37. mod->ver = os_dlsym(mod->module, "obs_module_ver");
  38. if (!mod->ver)
  39. return req_func_not_found("obs_module_ver", path);
  40. /* optional exports */
  41. mod->unload = os_dlsym(mod->module, "obs_module_unload");
  42. mod->post_load = os_dlsym(mod->module, "obs_module_post_load");
  43. mod->set_locale = os_dlsym(mod->module, "obs_module_set_locale");
  44. mod->free_locale = os_dlsym(mod->module, "obs_module_free_locale");
  45. mod->name = os_dlsym(mod->module, "obs_module_name");
  46. mod->description = os_dlsym(mod->module, "obs_module_description");
  47. mod->author = os_dlsym(mod->module, "obs_module_author");
  48. mod->get_string = os_dlsym(mod->module, "obs_module_get_string");
  49. return MODULE_SUCCESS;
  50. }
  51. bool obs_module_get_locale_string(const obs_module_t *mod, const char *lookup_string, const char **translated_string)
  52. {
  53. if (mod->get_string) {
  54. return mod->get_string(lookup_string, translated_string);
  55. }
  56. return false;
  57. }
  58. const char *obs_module_get_locale_text(const obs_module_t *mod, const char *text)
  59. {
  60. const char *str = text;
  61. obs_module_get_locale_string(mod, text, &str);
  62. return str;
  63. }
  64. static inline char *get_module_name(const char *file)
  65. {
  66. static size_t ext_len = 0;
  67. struct dstr name = {0};
  68. if (ext_len == 0) {
  69. const char *ext = get_module_extension();
  70. ext_len = strlen(ext);
  71. }
  72. dstr_copy(&name, file);
  73. dstr_resize(&name, name.len - ext_len);
  74. return name.array;
  75. }
  76. #ifdef _WIN32
  77. extern void reset_win32_symbol_paths(void);
  78. #endif
  79. int obs_module_load_metadata(struct obs_module *mod)
  80. {
  81. struct obs_module_metadata *md = NULL;
  82. /* Check if the metadata file exists */
  83. struct dstr path = {0};
  84. dstr_copy(&path, mod->data_path);
  85. if (!dstr_is_empty(&path) && dstr_end(&path) != '/') {
  86. dstr_cat_ch(&path, '/');
  87. }
  88. dstr_cat(&path, "manifest.json");
  89. if (os_file_exists(path.array)) {
  90. /* If we find a metadata file, allocate a new metadata. */
  91. md = bmalloc(sizeof(obs_module_metadata_t));
  92. obs_data_t *metadata = obs_data_create_from_json_file(path.array);
  93. md->display_name = bstrdup(obs_data_get_string(metadata, "display_name"));
  94. md->id = bstrdup(obs_data_get_string(metadata, "id"));
  95. md->version = bstrdup(obs_data_get_string(metadata, "version"));
  96. md->os_arch = bstrdup(obs_data_get_string(metadata, "os_arch"));
  97. md->name = bstrdup(obs_data_get_string(metadata, "name"));
  98. md->description = bstrdup(obs_data_get_string(metadata, "description"));
  99. md->long_description = bstrdup(obs_data_get_string(metadata, "long_description"));
  100. obs_data_t *urls = obs_data_get_obj(metadata, "urls");
  101. md->repository_url = bstrdup(obs_data_get_string(urls, "repository"));
  102. md->website_url = bstrdup(obs_data_get_string(urls, "website"));
  103. md->support_url = bstrdup(obs_data_get_string(urls, "support"));
  104. obs_data_release(urls);
  105. md->has_banner = obs_data_get_bool(metadata, "has_banner");
  106. md->has_icon = obs_data_get_bool(metadata, "has_icon");
  107. obs_data_release(metadata);
  108. }
  109. dstr_free(&path);
  110. mod->metadata = md;
  111. return MODULE_SUCCESS;
  112. }
  113. int obs_open_module(obs_module_t **module, const char *path, const char *data_path)
  114. {
  115. struct obs_module mod = {0};
  116. int errorcode;
  117. if (!module || !path || !obs)
  118. return MODULE_ERROR;
  119. #ifdef __APPLE__
  120. /* HACK: Do not load obsolete obs-browser build on macOS; the
  121. * obs-browser plugin used to live in the Application Support
  122. * directory. */
  123. if (astrstri(path, "Library/Application Support/obs-studio") != NULL && astrstri(path, "obs-browser") != NULL) {
  124. blog(LOG_WARNING, "Ignoring old obs-browser.so version");
  125. return MODULE_HARDCODED_SKIP;
  126. }
  127. #endif
  128. blog(LOG_DEBUG, "---------------------------------");
  129. mod.module = os_dlopen(path);
  130. if (!mod.module) {
  131. blog(LOG_WARNING, "Module '%s' not loaded", path);
  132. return MODULE_FILE_NOT_FOUND;
  133. }
  134. errorcode = load_module_exports(&mod, path);
  135. if (errorcode != MODULE_SUCCESS)
  136. return errorcode;
  137. /* Reject plugins compiled with a newer libobs. Patch version (lower 16-bit) is ignored. */
  138. uint32_t ver = mod.ver ? mod.ver() & 0xFFFF0000 : 0;
  139. if (ver > LIBOBS_API_VER) {
  140. blog(LOG_WARNING, "Module '%s' compiled with newer libobs %d.%d", path, (ver >> 24) & 0xFF,
  141. (ver >> 16) & 0xFF);
  142. return MODULE_INCOMPATIBLE_VER;
  143. }
  144. mod.bin_path = bstrdup(path);
  145. mod.file = strrchr(mod.bin_path, '/');
  146. mod.file = (!mod.file) ? mod.bin_path : (mod.file + 1);
  147. mod.mod_name = get_module_name(mod.file);
  148. mod.data_path = bstrdup(data_path);
  149. mod.next = obs->first_module;
  150. mod.load_state = OBS_MODULE_ENABLED;
  151. da_init(mod.sources);
  152. da_init(mod.outputs);
  153. da_init(mod.encoders);
  154. da_init(mod.services);
  155. if (mod.file) {
  156. blog(LOG_DEBUG, "Loading module: %s", mod.file);
  157. }
  158. obs_module_load_metadata(&mod);
  159. *module = bmemdup(&mod, sizeof(mod));
  160. obs->first_module = (*module);
  161. mod.set_pointer(*module);
  162. if (mod.set_locale)
  163. mod.set_locale(obs->locale);
  164. return MODULE_SUCCESS;
  165. }
  166. bool obs_create_disabled_module(obs_module_t **module, const char *path, const char *data_path,
  167. enum obs_module_load_state state)
  168. {
  169. struct obs_module mod = {0};
  170. mod.bin_path = bstrdup(path);
  171. mod.file = strrchr(mod.bin_path, '/');
  172. mod.file = (!mod.file) ? mod.bin_path : (mod.file + 1);
  173. mod.mod_name = get_module_name(mod.file);
  174. mod.data_path = bstrdup(data_path);
  175. mod.next = obs->first_disabled_module;
  176. mod.load_state = state;
  177. da_init(mod.sources);
  178. da_init(mod.outputs);
  179. da_init(mod.encoders);
  180. da_init(mod.services);
  181. obs_module_load_metadata(&mod);
  182. *module = bmemdup(&mod, sizeof(mod));
  183. obs->first_disabled_module = (*module);
  184. return true;
  185. }
  186. bool obs_init_module(obs_module_t *module)
  187. {
  188. if (!module || !obs)
  189. return false;
  190. if (module->loaded)
  191. return true;
  192. const char *profile_name =
  193. profile_store_name(obs_get_profiler_name_store(), "obs_init_module(%s)", module->file);
  194. profile_start(profile_name);
  195. loadingModule = module;
  196. module->loaded = module->load();
  197. loadingModule = NULL;
  198. if (!module->loaded)
  199. blog(LOG_WARNING, "Failed to initialize module '%s'", module->file);
  200. profile_end(profile_name);
  201. return module->loaded;
  202. }
  203. void obs_log_loaded_modules(void)
  204. {
  205. blog(LOG_INFO, " Loaded Modules:");
  206. for (obs_module_t *mod = obs->first_module; !!mod; mod = mod->next)
  207. blog(LOG_INFO, " %s", mod->file);
  208. }
  209. const char *obs_get_module_file_name(obs_module_t *module)
  210. {
  211. return module ? module->file : NULL;
  212. }
  213. const char *obs_get_module_name(obs_module_t *module)
  214. {
  215. if (module && module->metadata && module->metadata->display_name) {
  216. return module->metadata->display_name;
  217. }
  218. return (module && module->name) ? module->name() : NULL;
  219. }
  220. const char *obs_get_module_author(obs_module_t *module)
  221. {
  222. return (module && module->author) ? module->author() : NULL;
  223. }
  224. const char *obs_get_module_description(obs_module_t *module)
  225. {
  226. return (module && module->description) ? module->description() : NULL;
  227. }
  228. const char *obs_get_module_binary_path(obs_module_t *module)
  229. {
  230. return module ? module->bin_path : NULL;
  231. }
  232. const char *obs_get_module_data_path(obs_module_t *module)
  233. {
  234. return module ? module->data_path : NULL;
  235. }
  236. const char *obs_get_module_id(obs_module_t *module)
  237. {
  238. return module && module->metadata ? module->metadata->id : NULL;
  239. }
  240. const char *obs_get_module_version(obs_module_t *module)
  241. {
  242. return module && module->metadata ? module->metadata->version : NULL;
  243. }
  244. void obs_module_add_source(obs_module_t *module, const char *id)
  245. {
  246. char *source_id = bstrdup(id);
  247. if (module) {
  248. da_push_back(module->sources, &source_id);
  249. }
  250. }
  251. void obs_module_add_output(obs_module_t *module, const char *id)
  252. {
  253. char *output_id = bstrdup(id);
  254. if (module) {
  255. da_push_back(module->outputs, &output_id);
  256. }
  257. }
  258. void obs_module_add_encoder(obs_module_t *module, const char *id)
  259. {
  260. char *encoder_id = bstrdup(id);
  261. if (module) {
  262. da_push_back(module->encoders, &encoder_id);
  263. }
  264. }
  265. void obs_module_add_service(obs_module_t *module, const char *id)
  266. {
  267. char *service_id = bstrdup(id);
  268. if (module) {
  269. da_push_back(module->services, &service_id);
  270. }
  271. }
  272. obs_module_t *obs_get_module(const char *name)
  273. {
  274. obs_module_t *module = obs->first_module;
  275. while (module) {
  276. if (strcmp(module->mod_name, name) == 0) {
  277. return module;
  278. }
  279. module = module->next;
  280. }
  281. return NULL;
  282. }
  283. obs_module_t *obs_get_disabled_module(const char *name)
  284. {
  285. obs_module_t *module = obs->first_disabled_module;
  286. while (module) {
  287. if (strcmp(module->mod_name, name) == 0) {
  288. return module;
  289. }
  290. module = module->next;
  291. }
  292. return NULL;
  293. }
  294. void *obs_get_module_lib(obs_module_t *module)
  295. {
  296. return module ? module->module : NULL;
  297. }
  298. char *obs_find_module_file(obs_module_t *module, const char *file)
  299. {
  300. struct dstr output = {0};
  301. if (!file)
  302. file = "";
  303. if (!module)
  304. return NULL;
  305. dstr_copy(&output, module->data_path);
  306. if (!dstr_is_empty(&output) && dstr_end(&output) != '/' && *file)
  307. dstr_cat_ch(&output, '/');
  308. dstr_cat(&output, file);
  309. if (!os_file_exists(output.array))
  310. dstr_free(&output);
  311. return output.array;
  312. }
  313. char *obs_module_get_config_path(obs_module_t *module, const char *file)
  314. {
  315. struct dstr output = {0};
  316. dstr_copy(&output, obs->module_config_path);
  317. if (!dstr_is_empty(&output) && dstr_end(&output) != '/')
  318. dstr_cat_ch(&output, '/');
  319. dstr_cat(&output, module->mod_name);
  320. dstr_cat_ch(&output, '/');
  321. dstr_cat(&output, file);
  322. return output.array;
  323. }
  324. void obs_add_module_path(const char *bin, const char *data)
  325. {
  326. struct obs_module_path omp;
  327. if (!obs || !bin || !data)
  328. return;
  329. omp.bin = bstrdup(bin);
  330. omp.data = bstrdup(data);
  331. da_push_back(obs->module_paths, &omp);
  332. }
  333. void obs_add_safe_module(const char *name)
  334. {
  335. if (!obs || !name)
  336. return;
  337. char *item = bstrdup(name);
  338. da_push_back(obs->safe_modules, &item);
  339. }
  340. void obs_add_core_module(const char *name)
  341. {
  342. if (!obs || !name)
  343. return;
  344. char *item = bstrdup(name);
  345. da_push_back(obs->core_modules, &item);
  346. }
  347. void obs_add_disabled_module(const char *name)
  348. {
  349. if (!obs || !name)
  350. return;
  351. char *item = bstrdup(name);
  352. da_push_back(obs->disabled_modules, &item);
  353. }
  354. extern void get_plugin_info(const char *path, bool *is_obs_plugin, bool *can_load);
  355. struct fail_info {
  356. struct dstr fail_modules;
  357. size_t fail_count;
  358. };
  359. static bool is_safe_module(const char *name)
  360. {
  361. if (!obs->safe_modules.num)
  362. return true;
  363. for (size_t i = 0; i < obs->safe_modules.num; i++) {
  364. if (strcmp(name, obs->safe_modules.array[i]) == 0)
  365. return true;
  366. }
  367. return false;
  368. }
  369. static bool is_core_module(const char *name)
  370. {
  371. for (size_t i = 0; i < obs->core_modules.num; i++) {
  372. if (strcmp(name, obs->core_modules.array[i]) == 0)
  373. return true;
  374. }
  375. return false;
  376. }
  377. static bool is_disabled_module(const char *name)
  378. {
  379. if (obs->disabled_modules.num == 0)
  380. return false;
  381. for (size_t i = 0; i < obs->disabled_modules.num; i++) {
  382. if (strcmp(name, obs->disabled_modules.array[i]) == 0)
  383. return true;
  384. }
  385. return false;
  386. }
  387. bool obs_get_module_allow_disable(const char *name)
  388. {
  389. return !is_core_module(name);
  390. }
  391. static void load_all_callback(void *param, const struct obs_module_info2 *info)
  392. {
  393. struct fail_info *fail_info = param;
  394. obs_module_t *module;
  395. obs_module_t *disabled_module;
  396. bool is_obs_plugin;
  397. bool can_load_obs_plugin;
  398. get_plugin_info(info->bin_path, &is_obs_plugin, &can_load_obs_plugin);
  399. if (!is_obs_plugin) {
  400. blog(LOG_WARNING, "Skipping module '%s', not an OBS plugin", info->bin_path);
  401. return;
  402. }
  403. if (!is_safe_module(info->name)) {
  404. obs_create_disabled_module(&disabled_module, info->bin_path, info->data_path, OBS_MODULE_DISABLED_SAFE);
  405. blog(LOG_WARNING, "Skipping module '%s', not on safe list", info->name);
  406. return;
  407. }
  408. if (is_disabled_module(info->name)) {
  409. obs_create_disabled_module(&disabled_module, info->bin_path, info->data_path, OBS_MODULE_DISABLED);
  410. blog(LOG_WARNING, "Skipping module '%s', is disabled", info->name);
  411. return;
  412. }
  413. if (!can_load_obs_plugin) {
  414. blog(LOG_WARNING,
  415. "Skipping module '%s' due to possible "
  416. "import conflicts",
  417. info->bin_path);
  418. goto load_failure;
  419. }
  420. int code = obs_open_module(&module, info->bin_path, info->data_path);
  421. switch (code) {
  422. case MODULE_MISSING_EXPORTS:
  423. blog(LOG_DEBUG, "Failed to load module file '%s', not an OBS plugin", info->bin_path);
  424. return;
  425. case MODULE_FILE_NOT_FOUND:
  426. blog(LOG_DEBUG, "Failed to load module file '%s', file not found", info->bin_path);
  427. return;
  428. case MODULE_ERROR:
  429. blog(LOG_DEBUG, "Failed to load module file '%s'", info->bin_path);
  430. goto load_failure;
  431. case MODULE_INCOMPATIBLE_VER:
  432. blog(LOG_DEBUG, "Failed to load module file '%s', incompatible version", info->bin_path);
  433. goto load_failure;
  434. case MODULE_HARDCODED_SKIP:
  435. return;
  436. }
  437. if (!obs_init_module(module)) {
  438. free_module(module);
  439. obs_create_disabled_module(&disabled_module, info->bin_path, info->data_path, OBS_MODULE_ERROR);
  440. }
  441. UNUSED_PARAMETER(param);
  442. return;
  443. load_failure:
  444. if (fail_info) {
  445. dstr_cat(&fail_info->fail_modules, info->name);
  446. dstr_cat(&fail_info->fail_modules, ";");
  447. fail_info->fail_count++;
  448. }
  449. }
  450. static const char *obs_load_all_modules_name = "obs_load_all_modules";
  451. #ifdef _WIN32
  452. static const char *reset_win32_symbol_paths_name = "reset_win32_symbol_paths";
  453. #endif
  454. void obs_load_all_modules(void)
  455. {
  456. profile_start(obs_load_all_modules_name);
  457. obs_find_modules2(load_all_callback, NULL);
  458. #ifdef _WIN32
  459. profile_start(reset_win32_symbol_paths_name);
  460. reset_win32_symbol_paths();
  461. profile_end(reset_win32_symbol_paths_name);
  462. #endif
  463. profile_end(obs_load_all_modules_name);
  464. }
  465. static const char *obs_load_all_modules2_name = "obs_load_all_modules2";
  466. void obs_load_all_modules2(struct obs_module_failure_info *mfi)
  467. {
  468. struct fail_info fail_info = {0};
  469. memset(mfi, 0, sizeof(*mfi));
  470. profile_start(obs_load_all_modules2_name);
  471. obs_find_modules2(load_all_callback, &fail_info);
  472. #ifdef _WIN32
  473. profile_start(reset_win32_symbol_paths_name);
  474. reset_win32_symbol_paths();
  475. profile_end(reset_win32_symbol_paths_name);
  476. #endif
  477. profile_end(obs_load_all_modules2_name);
  478. mfi->count = fail_info.fail_count;
  479. mfi->failed_modules = strlist_split(fail_info.fail_modules.array, ';', false);
  480. dstr_free(&fail_info.fail_modules);
  481. }
  482. void obs_module_failure_info_free(struct obs_module_failure_info *mfi)
  483. {
  484. if (mfi->failed_modules) {
  485. bfree(mfi->failed_modules);
  486. mfi->failed_modules = NULL;
  487. }
  488. }
  489. void obs_post_load_modules(void)
  490. {
  491. for (obs_module_t *mod = obs->first_module; !!mod; mod = mod->next)
  492. if (mod->post_load)
  493. mod->post_load();
  494. }
  495. static inline void make_data_dir(struct dstr *parsed_data_dir, const char *data_dir, const char *name)
  496. {
  497. dstr_copy(parsed_data_dir, data_dir);
  498. dstr_replace(parsed_data_dir, "%module%", name);
  499. if (dstr_end(parsed_data_dir) == '/')
  500. dstr_resize(parsed_data_dir, parsed_data_dir->len - 1);
  501. }
  502. static char *make_data_directory(const char *module_name, const char *data_dir)
  503. {
  504. struct dstr parsed_data_dir = {0};
  505. bool found = false;
  506. make_data_dir(&parsed_data_dir, data_dir, module_name);
  507. found = os_file_exists(parsed_data_dir.array);
  508. if (!found && astrcmpi_n(module_name, "lib", 3) == 0)
  509. make_data_dir(&parsed_data_dir, data_dir, module_name + 3);
  510. return parsed_data_dir.array;
  511. }
  512. static bool parse_binary_from_directory(struct dstr *parsed_bin_path, const char *bin_path, const char *file)
  513. {
  514. struct dstr directory = {0};
  515. bool found = true;
  516. dstr_copy(&directory, bin_path);
  517. dstr_replace(&directory, "%module%", file);
  518. if (dstr_end(&directory) != '/')
  519. dstr_cat_ch(&directory, '/');
  520. dstr_copy_dstr(parsed_bin_path, &directory);
  521. dstr_cat(parsed_bin_path, file);
  522. #ifdef __APPLE__
  523. if (!os_file_exists(parsed_bin_path->array)) {
  524. dstr_cat(parsed_bin_path, ".so");
  525. }
  526. #else
  527. dstr_cat(parsed_bin_path, get_module_extension());
  528. #endif
  529. if (!os_file_exists(parsed_bin_path->array)) {
  530. /* Legacy fallback: Check for plugin with .so suffix*/
  531. dstr_cat(parsed_bin_path, ".so");
  532. /* if the file doesn't exist, check with 'lib' prefix */
  533. dstr_copy_dstr(parsed_bin_path, &directory);
  534. dstr_cat(parsed_bin_path, "lib");
  535. dstr_cat(parsed_bin_path, file);
  536. dstr_cat(parsed_bin_path, get_module_extension());
  537. /* if neither exist, don't include this as a library */
  538. if (!os_file_exists(parsed_bin_path->array)) {
  539. dstr_free(parsed_bin_path);
  540. found = false;
  541. }
  542. }
  543. dstr_free(&directory);
  544. return found;
  545. }
  546. static void process_found_module(struct obs_module_path *omp, const char *path, bool directory,
  547. obs_find_module_callback2_t callback, void *param)
  548. {
  549. struct obs_module_info2 info;
  550. struct dstr name = {0};
  551. struct dstr parsed_bin_path = {0};
  552. const char *file;
  553. char *parsed_data_dir;
  554. bool bin_found = true;
  555. file = strrchr(path, '/');
  556. file = file ? (file + 1) : path;
  557. if (strcmp(file, ".") == 0 || strcmp(file, "..") == 0)
  558. return;
  559. dstr_copy(&name, file);
  560. char *ext = strrchr(name.array, '.');
  561. if (ext)
  562. dstr_resize(&name, ext - name.array);
  563. if (!directory) {
  564. dstr_copy(&parsed_bin_path, path);
  565. } else {
  566. bin_found = parse_binary_from_directory(&parsed_bin_path, omp->bin, name.array);
  567. }
  568. parsed_data_dir = make_data_directory(name.array, omp->data);
  569. if (parsed_data_dir && bin_found) {
  570. info.bin_path = parsed_bin_path.array;
  571. info.data_path = parsed_data_dir;
  572. info.name = name.array;
  573. callback(param, &info);
  574. }
  575. bfree(parsed_data_dir);
  576. dstr_free(&name);
  577. dstr_free(&parsed_bin_path);
  578. }
  579. static void find_modules_in_path(struct obs_module_path *omp, obs_find_module_callback2_t callback, void *param)
  580. {
  581. struct dstr search_path = {0};
  582. char *module_start;
  583. bool search_directories = false;
  584. os_glob_t *gi;
  585. dstr_copy(&search_path, omp->bin);
  586. module_start = strstr(search_path.array, "%module%");
  587. if (module_start) {
  588. dstr_resize(&search_path, module_start - search_path.array);
  589. search_directories = true;
  590. }
  591. if (!dstr_is_empty(&search_path) && dstr_end(&search_path) != '/')
  592. dstr_cat_ch(&search_path, '/');
  593. dstr_cat_ch(&search_path, '*');
  594. if (!search_directories)
  595. dstr_cat(&search_path, get_module_extension());
  596. if (os_glob(search_path.array, 0, &gi) == 0) {
  597. for (size_t i = 0; i < gi->gl_pathc; i++) {
  598. if (search_directories == gi->gl_pathv[i].directory)
  599. process_found_module(omp, gi->gl_pathv[i].path, search_directories, callback, param);
  600. }
  601. os_globfree(gi);
  602. }
  603. dstr_free(&search_path);
  604. }
  605. void obs_find_modules2(obs_find_module_callback2_t callback, void *param)
  606. {
  607. if (!obs)
  608. return;
  609. for (size_t i = 0; i < obs->module_paths.num; i++) {
  610. struct obs_module_path *omp = obs->module_paths.array + i;
  611. find_modules_in_path(omp, callback, param);
  612. }
  613. }
  614. void obs_find_modules(obs_find_module_callback_t callback, void *param)
  615. {
  616. /* the structure is ABI compatible so we can just cast the callback */
  617. obs_find_modules2((obs_find_module_callback2_t)callback, param);
  618. }
  619. void obs_enum_modules(obs_enum_module_callback_t callback, void *param)
  620. {
  621. struct obs_module *module;
  622. if (!obs)
  623. return;
  624. module = obs->first_module;
  625. while (module) {
  626. callback(param, module);
  627. module = module->next;
  628. }
  629. }
  630. void free_module(struct obs_module *mod)
  631. {
  632. if (!mod)
  633. return;
  634. if (mod->module) {
  635. if (mod->free_locale)
  636. mod->free_locale();
  637. if (mod->loaded && mod->unload)
  638. mod->unload();
  639. /* there is no real reason to close the dynamic libraries,
  640. * and sometimes this can cause issues. */
  641. /* os_dlclose(mod->module); */
  642. }
  643. /* Is this module an active / loaded module, or a disabled module? */
  644. if (mod->load_state == OBS_MODULE_ENABLED) {
  645. for (obs_module_t *m = obs->first_module; !!m; m = m->next) {
  646. if (m->next == mod) {
  647. m->next = mod->next;
  648. break;
  649. }
  650. }
  651. if (obs->first_module == mod)
  652. obs->first_module = mod->next;
  653. } else {
  654. for (obs_module_t *m = obs->first_disabled_module; !!m; m = m->next) {
  655. if (m->next == mod) {
  656. m->next = mod->next;
  657. break;
  658. }
  659. }
  660. if (obs->first_disabled_module == mod)
  661. obs->first_disabled_module = mod->next;
  662. }
  663. bfree(mod->mod_name);
  664. bfree(mod->bin_path);
  665. bfree(mod->data_path);
  666. for (size_t i = 0; i < mod->sources.num; i++) {
  667. bfree(mod->sources.array[i]);
  668. }
  669. da_free(mod->sources);
  670. for (size_t i = 0; i < mod->outputs.num; i++) {
  671. bfree(mod->outputs.array[i]);
  672. }
  673. da_free(mod->outputs);
  674. for (size_t i = 0; i < mod->encoders.num; i++) {
  675. bfree(mod->encoders.array[i]);
  676. }
  677. da_free(mod->encoders);
  678. for (size_t i = 0; i < mod->services.num; i++) {
  679. bfree(mod->services.array[i]);
  680. }
  681. da_free(mod->services);
  682. if (mod->metadata) {
  683. free_module_metadata(mod->metadata);
  684. bfree(mod->metadata);
  685. }
  686. bfree(mod);
  687. }
  688. lookup_t *obs_module_load_locale(obs_module_t *module, const char *default_locale, const char *locale)
  689. {
  690. struct dstr str = {0};
  691. lookup_t *lookup = NULL;
  692. if (!module || !default_locale || !locale) {
  693. blog(LOG_WARNING, "obs_module_load_locale: Invalid parameters");
  694. return NULL;
  695. }
  696. dstr_copy(&str, "locale/");
  697. dstr_cat(&str, default_locale);
  698. dstr_cat(&str, ".ini");
  699. char *file = obs_find_module_file(module, str.array);
  700. if (file)
  701. lookup = text_lookup_create(file);
  702. bfree(file);
  703. if (!lookup) {
  704. blog(LOG_WARNING, "Failed to load '%s' text for module: '%s'", default_locale, module->file);
  705. goto cleanup;
  706. }
  707. if (astrcmpi(locale, default_locale) == 0)
  708. goto cleanup;
  709. dstr_copy(&str, "/locale/");
  710. dstr_cat(&str, locale);
  711. dstr_cat(&str, ".ini");
  712. file = obs_find_module_file(module, str.array);
  713. if (!text_lookup_add(lookup, file))
  714. blog(LOG_WARNING, "Failed to load '%s' text for module: '%s'", locale, module->file);
  715. bfree(file);
  716. cleanup:
  717. dstr_free(&str);
  718. return lookup;
  719. }
  720. #define REGISTER_OBS_DEF(size_var, structure, dest, info) \
  721. do { \
  722. struct structure data = {0}; \
  723. if (!size_var) { \
  724. blog(LOG_ERROR, "Tried to register " #structure " outside of obs_module_load"); \
  725. return; \
  726. } \
  727. \
  728. if (size_var > sizeof(data)) { \
  729. blog(LOG_ERROR, \
  730. "Tried to register " #structure " with size %llu which is more " \
  731. "than libobs currently supports " \
  732. "(%llu)", \
  733. (long long unsigned)size_var, (long long unsigned)sizeof(data)); \
  734. goto error; \
  735. } \
  736. \
  737. memcpy(&data, info, size_var); \
  738. da_push_back(dest, &data); \
  739. } while (false)
  740. #define HAS_VAL(type, info, val) ((offsetof(type, val) + sizeof(info->val) <= size) && info->val)
  741. #define CHECK_REQUIRED_VAL(type, info, val, func) \
  742. do { \
  743. if (!HAS_VAL(type, info, val)) { \
  744. blog(LOG_ERROR, \
  745. "Required value '" #val "' for " \
  746. "'%s' not found. " #func " failed.", \
  747. info->id); \
  748. goto error; \
  749. } \
  750. } while (false)
  751. #define CHECK_REQUIRED_VAL_EITHER(type, info, val1, val2, func) \
  752. do { \
  753. if (!HAS_VAL(type, info, val1) && !HAS_VAL(type, info, val2)) { \
  754. blog(LOG_ERROR, \
  755. "Neither '" #val1 "' nor '" #val2 "' " \
  756. "for '%s' found. " #func " failed.", \
  757. info->id); \
  758. goto error; \
  759. } \
  760. } while (false)
  761. #define HANDLE_ERROR(size_var, structure, info) \
  762. do { \
  763. struct structure data = {0}; \
  764. if (!size_var) \
  765. return; \
  766. \
  767. memcpy(&data, info, sizeof(data) < size_var ? sizeof(data) : size_var); \
  768. \
  769. if (data.type_data && data.free_type_data) \
  770. data.free_type_data(data.type_data); \
  771. } while (false)
  772. #define source_warn(format, ...) blog(LOG_WARNING, "obs_register_source: " format, ##__VA_ARGS__)
  773. #define output_warn(format, ...) blog(LOG_WARNING, "obs_register_output: " format, ##__VA_ARGS__)
  774. #define encoder_warn(format, ...) blog(LOG_WARNING, "obs_register_encoder: " format, ##__VA_ARGS__)
  775. #define service_warn(format, ...) blog(LOG_WARNING, "obs_register_service: " format, ##__VA_ARGS__)
  776. void obs_register_source_s(const struct obs_source_info *info, size_t size)
  777. {
  778. struct obs_source_info data = {0};
  779. obs_source_info_array_t *array = NULL;
  780. if (info->type == OBS_SOURCE_TYPE_INPUT) {
  781. array = &obs->input_types;
  782. } else if (info->type == OBS_SOURCE_TYPE_FILTER) {
  783. array = &obs->filter_types;
  784. } else if (info->type == OBS_SOURCE_TYPE_TRANSITION) {
  785. array = &obs->transition_types;
  786. } else if (info->type != OBS_SOURCE_TYPE_SCENE) {
  787. source_warn("Tried to register unknown source type: %u", info->type);
  788. goto error;
  789. }
  790. if (get_source_info2(info->id, info->version)) {
  791. source_warn("Source '%s' already exists! "
  792. "Duplicate library?",
  793. info->id);
  794. goto error;
  795. }
  796. if (size > sizeof(data)) {
  797. source_warn("Tried to register obs_source_info with size "
  798. "%llu which is more than libobs currently "
  799. "supports (%llu)",
  800. (long long unsigned)size, (long long unsigned)sizeof(data));
  801. goto error;
  802. }
  803. /* NOTE: The assignment of data.module must occur before memcpy! */
  804. if (loadingModule) {
  805. data.module = loadingModule;
  806. char *source_id = bstrdup(info->id);
  807. da_push_back(loadingModule->sources, &source_id);
  808. }
  809. memcpy(&data, info, size);
  810. /* mark audio-only filters as an async filter categorically */
  811. if (data.type == OBS_SOURCE_TYPE_FILTER) {
  812. if ((data.output_flags & OBS_SOURCE_VIDEO) == 0)
  813. data.output_flags |= OBS_SOURCE_ASYNC;
  814. }
  815. if (data.type == OBS_SOURCE_TYPE_TRANSITION) {
  816. if (data.get_width)
  817. source_warn("get_width ignored registering "
  818. "transition '%s'",
  819. data.id);
  820. if (data.get_height)
  821. source_warn("get_height ignored registering "
  822. "transition '%s'",
  823. data.id);
  824. data.output_flags |= OBS_SOURCE_COMPOSITE | OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW;
  825. }
  826. if ((data.output_flags & OBS_SOURCE_COMPOSITE) != 0) {
  827. if ((data.output_flags & OBS_SOURCE_AUDIO) != 0) {
  828. source_warn("Source '%s': Composite sources "
  829. "cannot be audio sources",
  830. info->id);
  831. goto error;
  832. }
  833. if ((data.output_flags & OBS_SOURCE_ASYNC) != 0) {
  834. source_warn("Source '%s': Composite sources "
  835. "cannot be async sources",
  836. info->id);
  837. goto error;
  838. }
  839. }
  840. #define CHECK_REQUIRED_VAL_(info, val, func) CHECK_REQUIRED_VAL(struct obs_source_info, info, val, func)
  841. CHECK_REQUIRED_VAL_(info, get_name, obs_register_source);
  842. if (info->type != OBS_SOURCE_TYPE_FILTER && info->type != OBS_SOURCE_TYPE_TRANSITION &&
  843. (info->output_flags & OBS_SOURCE_VIDEO) != 0 && (info->output_flags & OBS_SOURCE_ASYNC) == 0) {
  844. CHECK_REQUIRED_VAL_(info, get_width, obs_register_source);
  845. CHECK_REQUIRED_VAL_(info, get_height, obs_register_source);
  846. }
  847. if ((data.output_flags & OBS_SOURCE_COMPOSITE) != 0) {
  848. CHECK_REQUIRED_VAL_(info, audio_render, obs_register_source);
  849. }
  850. #undef CHECK_REQUIRED_VAL_
  851. /* version-related stuff */
  852. data.unversioned_id = data.id;
  853. if (data.version) {
  854. struct dstr versioned_id = {0};
  855. dstr_printf(&versioned_id, "%s_v%d", data.id, (int)data.version);
  856. data.id = versioned_id.array;
  857. } else {
  858. data.id = bstrdup(data.id);
  859. }
  860. if (array)
  861. da_push_back(*array, &data);
  862. da_push_back(obs->source_types, &data);
  863. return;
  864. error:
  865. HANDLE_ERROR(size, obs_source_info, info);
  866. }
  867. void obs_register_output_s(const struct obs_output_info *info, size_t size)
  868. {
  869. if (find_output(info->id)) {
  870. output_warn("Output id '%s' already exists! "
  871. "Duplicate library?",
  872. info->id);
  873. goto error;
  874. }
  875. #define CHECK_REQUIRED_VAL_(info, val, func) CHECK_REQUIRED_VAL(struct obs_output_info, info, val, func)
  876. CHECK_REQUIRED_VAL_(info, get_name, obs_register_output);
  877. CHECK_REQUIRED_VAL_(info, create, obs_register_output);
  878. CHECK_REQUIRED_VAL_(info, destroy, obs_register_output);
  879. CHECK_REQUIRED_VAL_(info, start, obs_register_output);
  880. CHECK_REQUIRED_VAL_(info, stop, obs_register_output);
  881. if (info->flags & OBS_OUTPUT_SERVICE)
  882. CHECK_REQUIRED_VAL_(info, protocols, obs_register_output);
  883. if (info->flags & OBS_OUTPUT_ENCODED) {
  884. CHECK_REQUIRED_VAL_(info, encoded_packet, obs_register_output);
  885. } else {
  886. if (info->flags & OBS_OUTPUT_VIDEO)
  887. CHECK_REQUIRED_VAL_(info, raw_video, obs_register_output);
  888. if (info->flags & OBS_OUTPUT_AUDIO) {
  889. if (info->flags & OBS_OUTPUT_MULTI_TRACK) {
  890. CHECK_REQUIRED_VAL_(info, raw_audio2, obs_register_output);
  891. } else {
  892. CHECK_REQUIRED_VAL_(info, raw_audio, obs_register_output);
  893. }
  894. }
  895. }
  896. #undef CHECK_REQUIRED_VAL_
  897. REGISTER_OBS_DEF(size, obs_output_info, obs->output_types, info);
  898. if (info->flags & OBS_OUTPUT_SERVICE) {
  899. char **protocols = strlist_split(info->protocols, ';', false);
  900. for (char **protocol = protocols; *protocol; ++protocol) {
  901. bool skip = false;
  902. for (size_t i = 0; i < obs->data.protocols.num; i++) {
  903. if (strcmp(*protocol, obs->data.protocols.array[i]) == 0)
  904. skip = true;
  905. }
  906. if (skip)
  907. continue;
  908. char *new_prtcl = bstrdup(*protocol);
  909. da_push_back(obs->data.protocols, &new_prtcl);
  910. }
  911. strlist_free(protocols);
  912. }
  913. return;
  914. error:
  915. HANDLE_ERROR(size, obs_output_info, info);
  916. }
  917. void obs_register_encoder_s(const struct obs_encoder_info *info, size_t size)
  918. {
  919. if (find_encoder(info->id)) {
  920. encoder_warn("Encoder id '%s' already exists! "
  921. "Duplicate library?",
  922. info->id);
  923. goto error;
  924. }
  925. if (((info->caps & OBS_ENCODER_CAP_PASS_TEXTURE) != 0 && info->caps & OBS_ENCODER_CAP_SCALING) != 0) {
  926. encoder_warn("Texture encoders cannot self-scale. Encoder id '%s' not registered.", info->id);
  927. goto error;
  928. }
  929. #define CHECK_REQUIRED_VAL_(info, val, func) CHECK_REQUIRED_VAL(struct obs_encoder_info, info, val, func)
  930. CHECK_REQUIRED_VAL_(info, get_name, obs_register_encoder);
  931. CHECK_REQUIRED_VAL_(info, create, obs_register_encoder);
  932. CHECK_REQUIRED_VAL_(info, destroy, obs_register_encoder);
  933. if ((info->caps & OBS_ENCODER_CAP_PASS_TEXTURE) != 0)
  934. CHECK_REQUIRED_VAL_EITHER(struct obs_encoder_info, info, encode_texture, encode_texture2,
  935. obs_register_encoder);
  936. else
  937. CHECK_REQUIRED_VAL_(info, encode, obs_register_encoder);
  938. if (info->type == OBS_ENCODER_AUDIO)
  939. CHECK_REQUIRED_VAL_(info, get_frame_size, obs_register_encoder);
  940. #undef CHECK_REQUIRED_VAL_
  941. REGISTER_OBS_DEF(size, obs_encoder_info, obs->encoder_types, info);
  942. if (loadingModule) {
  943. char *encoder_id = bstrdup(info->id);
  944. da_push_back(loadingModule->encoders, &encoder_id);
  945. }
  946. return;
  947. error:
  948. HANDLE_ERROR(size, obs_encoder_info, info);
  949. }
  950. void obs_register_service_s(const struct obs_service_info *info, size_t size)
  951. {
  952. if (find_service(info->id)) {
  953. service_warn("Service id '%s' already exists! "
  954. "Duplicate library?",
  955. info->id);
  956. goto error;
  957. }
  958. #define CHECK_REQUIRED_VAL_(info, val, func) CHECK_REQUIRED_VAL(struct obs_service_info, info, val, func)
  959. CHECK_REQUIRED_VAL_(info, get_name, obs_register_service);
  960. CHECK_REQUIRED_VAL_(info, create, obs_register_service);
  961. CHECK_REQUIRED_VAL_(info, destroy, obs_register_service);
  962. CHECK_REQUIRED_VAL_(info, get_protocol, obs_register_service);
  963. #undef CHECK_REQUIRED_VAL_
  964. REGISTER_OBS_DEF(size, obs_service_info, obs->service_types, info);
  965. if (loadingModule) {
  966. char *service_id = bstrdup(info->id);
  967. da_push_back(loadingModule->services, &service_id);
  968. }
  969. return;
  970. error:
  971. HANDLE_ERROR(size, obs_service_info, info);
  972. }