1
0

obs-module.c 27 KB

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