obs-module.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  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. char *source_id = bstrdup(info->id);
  806. da_push_back(loadingModule->sources, &source_id);
  807. }
  808. memcpy(&data, info, size);
  809. /* mark audio-only filters as an async filter categorically */
  810. if (data.type == OBS_SOURCE_TYPE_FILTER) {
  811. if ((data.output_flags & OBS_SOURCE_VIDEO) == 0)
  812. data.output_flags |= OBS_SOURCE_ASYNC;
  813. }
  814. if (data.type == OBS_SOURCE_TYPE_TRANSITION) {
  815. if (data.get_width)
  816. source_warn("get_width ignored registering "
  817. "transition '%s'",
  818. data.id);
  819. if (data.get_height)
  820. source_warn("get_height ignored registering "
  821. "transition '%s'",
  822. data.id);
  823. data.output_flags |= OBS_SOURCE_COMPOSITE | OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW;
  824. }
  825. if ((data.output_flags & OBS_SOURCE_COMPOSITE) != 0) {
  826. if ((data.output_flags & OBS_SOURCE_AUDIO) != 0) {
  827. source_warn("Source '%s': Composite sources "
  828. "cannot be audio sources",
  829. info->id);
  830. goto error;
  831. }
  832. if ((data.output_flags & OBS_SOURCE_ASYNC) != 0) {
  833. source_warn("Source '%s': Composite sources "
  834. "cannot be async sources",
  835. info->id);
  836. goto error;
  837. }
  838. }
  839. #define CHECK_REQUIRED_VAL_(info, val, func) CHECK_REQUIRED_VAL(struct obs_source_info, info, val, func)
  840. CHECK_REQUIRED_VAL_(info, get_name, obs_register_source);
  841. if (info->type != OBS_SOURCE_TYPE_FILTER && info->type != OBS_SOURCE_TYPE_TRANSITION &&
  842. (info->output_flags & OBS_SOURCE_VIDEO) != 0 && (info->output_flags & OBS_SOURCE_ASYNC) == 0) {
  843. CHECK_REQUIRED_VAL_(info, get_width, obs_register_source);
  844. CHECK_REQUIRED_VAL_(info, get_height, obs_register_source);
  845. }
  846. if ((data.output_flags & OBS_SOURCE_COMPOSITE) != 0) {
  847. CHECK_REQUIRED_VAL_(info, audio_render, obs_register_source);
  848. }
  849. #undef CHECK_REQUIRED_VAL_
  850. /* version-related stuff */
  851. data.unversioned_id = data.id;
  852. if (data.version) {
  853. struct dstr versioned_id = {0};
  854. dstr_printf(&versioned_id, "%s_v%d", data.id, (int)data.version);
  855. data.id = versioned_id.array;
  856. } else {
  857. data.id = bstrdup(data.id);
  858. }
  859. if (array)
  860. da_push_back(*array, &data);
  861. da_push_back(obs->source_types, &data);
  862. return;
  863. error:
  864. HANDLE_ERROR(size, obs_source_info, info);
  865. }
  866. void obs_register_output_s(const struct obs_output_info *info, size_t size)
  867. {
  868. if (find_output(info->id)) {
  869. output_warn("Output id '%s' already exists! "
  870. "Duplicate library?",
  871. info->id);
  872. goto error;
  873. }
  874. #define CHECK_REQUIRED_VAL_(info, val, func) CHECK_REQUIRED_VAL(struct obs_output_info, info, val, func)
  875. CHECK_REQUIRED_VAL_(info, get_name, obs_register_output);
  876. CHECK_REQUIRED_VAL_(info, create, obs_register_output);
  877. CHECK_REQUIRED_VAL_(info, destroy, obs_register_output);
  878. CHECK_REQUIRED_VAL_(info, start, obs_register_output);
  879. CHECK_REQUIRED_VAL_(info, stop, obs_register_output);
  880. if (info->flags & OBS_OUTPUT_SERVICE)
  881. CHECK_REQUIRED_VAL_(info, protocols, obs_register_output);
  882. if (info->flags & OBS_OUTPUT_ENCODED) {
  883. CHECK_REQUIRED_VAL_(info, encoded_packet, obs_register_output);
  884. } else {
  885. if (info->flags & OBS_OUTPUT_VIDEO)
  886. CHECK_REQUIRED_VAL_(info, raw_video, obs_register_output);
  887. if (info->flags & OBS_OUTPUT_AUDIO) {
  888. if (info->flags & OBS_OUTPUT_MULTI_TRACK) {
  889. CHECK_REQUIRED_VAL_(info, raw_audio2, obs_register_output);
  890. } else {
  891. CHECK_REQUIRED_VAL_(info, raw_audio, obs_register_output);
  892. }
  893. }
  894. }
  895. #undef CHECK_REQUIRED_VAL_
  896. REGISTER_OBS_DEF(size, obs_output_info, obs->output_types, info);
  897. if (info->flags & OBS_OUTPUT_SERVICE) {
  898. char **protocols = strlist_split(info->protocols, ';', false);
  899. for (char **protocol = protocols; *protocol; ++protocol) {
  900. bool skip = false;
  901. for (size_t i = 0; i < obs->data.protocols.num; i++) {
  902. if (strcmp(*protocol, obs->data.protocols.array[i]) == 0)
  903. skip = true;
  904. }
  905. if (skip)
  906. continue;
  907. char *new_prtcl = bstrdup(*protocol);
  908. da_push_back(obs->data.protocols, &new_prtcl);
  909. }
  910. strlist_free(protocols);
  911. }
  912. return;
  913. error:
  914. HANDLE_ERROR(size, obs_output_info, info);
  915. }
  916. void obs_register_encoder_s(const struct obs_encoder_info *info, size_t size)
  917. {
  918. if (find_encoder(info->id)) {
  919. encoder_warn("Encoder id '%s' already exists! "
  920. "Duplicate library?",
  921. info->id);
  922. goto error;
  923. }
  924. if (((info->caps & OBS_ENCODER_CAP_PASS_TEXTURE) != 0 && info->caps & OBS_ENCODER_CAP_SCALING) != 0) {
  925. encoder_warn("Texture encoders cannot self-scale. Encoder id '%s' not registered.", info->id);
  926. goto error;
  927. }
  928. #define CHECK_REQUIRED_VAL_(info, val, func) CHECK_REQUIRED_VAL(struct obs_encoder_info, info, val, func)
  929. CHECK_REQUIRED_VAL_(info, get_name, obs_register_encoder);
  930. CHECK_REQUIRED_VAL_(info, create, obs_register_encoder);
  931. CHECK_REQUIRED_VAL_(info, destroy, obs_register_encoder);
  932. if ((info->caps & OBS_ENCODER_CAP_PASS_TEXTURE) != 0)
  933. CHECK_REQUIRED_VAL_EITHER(struct obs_encoder_info, info, encode_texture, encode_texture2,
  934. obs_register_encoder);
  935. else
  936. CHECK_REQUIRED_VAL_(info, encode, obs_register_encoder);
  937. if (info->type == OBS_ENCODER_AUDIO)
  938. CHECK_REQUIRED_VAL_(info, get_frame_size, obs_register_encoder);
  939. #undef CHECK_REQUIRED_VAL_
  940. REGISTER_OBS_DEF(size, obs_encoder_info, obs->encoder_types, info);
  941. if (loadingModule) {
  942. char *encoder_id = bstrdup(info->id);
  943. da_push_back(loadingModule->encoders, &encoder_id);
  944. }
  945. return;
  946. error:
  947. HANDLE_ERROR(size, obs_encoder_info, info);
  948. }
  949. void obs_register_service_s(const struct obs_service_info *info, size_t size)
  950. {
  951. if (find_service(info->id)) {
  952. service_warn("Service id '%s' already exists! "
  953. "Duplicate library?",
  954. info->id);
  955. goto error;
  956. }
  957. #define CHECK_REQUIRED_VAL_(info, val, func) CHECK_REQUIRED_VAL(struct obs_service_info, info, val, func)
  958. CHECK_REQUIRED_VAL_(info, get_name, obs_register_service);
  959. CHECK_REQUIRED_VAL_(info, create, obs_register_service);
  960. CHECK_REQUIRED_VAL_(info, destroy, obs_register_service);
  961. CHECK_REQUIRED_VAL_(info, get_protocol, obs_register_service);
  962. #undef CHECK_REQUIRED_VAL_
  963. REGISTER_OBS_DEF(size, obs_service_info, obs->service_types, info);
  964. if (loadingModule) {
  965. char *service_id = bstrdup(info->id);
  966. da_push_back(loadingModule->services, &service_id);
  967. }
  968. return;
  969. error:
  970. HANDLE_ERROR(size, obs_service_info, info);
  971. }