obs-module.c 27 KB

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