obs-module.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /******************************************************************************
  2. Copyright (C) 2013-2014 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "util/platform.h"
  15. #include "util/dstr.h"
  16. #include "obs-defs.h"
  17. #include "obs-internal.h"
  18. #include "obs-module.h"
  19. extern const char *get_module_extension(void);
  20. static inline int req_func_not_found(const char *name, const char *path)
  21. {
  22. blog(LOG_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") != NULL &&
  93. astrstri(path, "obs-browser") != NULL) {
  94. blog(LOG_WARNING, "Ignoring old obs-browser.so version");
  95. return MODULE_ERROR;
  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. char *obs_find_module_file(obs_module_t *module, const char *file)
  182. {
  183. struct dstr output = {0};
  184. if (!file)
  185. file = "";
  186. if (!module)
  187. return NULL;
  188. dstr_copy(&output, module->data_path);
  189. if (!dstr_is_empty(&output) && dstr_end(&output) != '/' && *file)
  190. dstr_cat_ch(&output, '/');
  191. dstr_cat(&output, file);
  192. if (!os_file_exists(output.array))
  193. dstr_free(&output);
  194. return output.array;
  195. }
  196. char *obs_module_get_config_path(obs_module_t *module, const char *file)
  197. {
  198. struct dstr output = {0};
  199. dstr_copy(&output, obs->module_config_path);
  200. if (!dstr_is_empty(&output) && dstr_end(&output) != '/')
  201. dstr_cat_ch(&output, '/');
  202. dstr_cat(&output, module->mod_name);
  203. dstr_cat_ch(&output, '/');
  204. dstr_cat(&output, file);
  205. return output.array;
  206. }
  207. void obs_add_module_path(const char *bin, const char *data)
  208. {
  209. struct obs_module_path omp;
  210. if (!obs || !bin || !data)
  211. return;
  212. omp.bin = bstrdup(bin);
  213. omp.data = bstrdup(data);
  214. da_push_back(obs->module_paths, &omp);
  215. }
  216. static void load_all_callback(void *param, const struct obs_module_info *info)
  217. {
  218. obs_module_t *module;
  219. int code = obs_open_module(&module, info->bin_path, info->data_path);
  220. if (code != MODULE_SUCCESS) {
  221. blog(LOG_DEBUG, "Failed to load module file '%s': %d",
  222. info->bin_path, code);
  223. return;
  224. }
  225. obs_init_module(module);
  226. UNUSED_PARAMETER(param);
  227. }
  228. static const char *obs_load_all_modules_name = "obs_load_all_modules";
  229. #ifdef _WIN32
  230. static const char *reset_win32_symbol_paths_name = "reset_win32_symbol_paths";
  231. #endif
  232. void obs_load_all_modules(void)
  233. {
  234. profile_start(obs_load_all_modules_name);
  235. obs_find_modules(load_all_callback, NULL);
  236. #ifdef _WIN32
  237. profile_start(reset_win32_symbol_paths_name);
  238. reset_win32_symbol_paths();
  239. profile_end(reset_win32_symbol_paths_name);
  240. #endif
  241. profile_end(obs_load_all_modules_name);
  242. }
  243. void obs_post_load_modules(void)
  244. {
  245. for (obs_module_t *mod = obs->first_module; !!mod; mod = mod->next)
  246. if (mod->post_load)
  247. mod->post_load();
  248. }
  249. static inline void make_data_dir(struct dstr *parsed_data_dir,
  250. const char *data_dir, const char *name)
  251. {
  252. dstr_copy(parsed_data_dir, data_dir);
  253. dstr_replace(parsed_data_dir, "%module%", name);
  254. if (dstr_end(parsed_data_dir) == '/')
  255. dstr_resize(parsed_data_dir, parsed_data_dir->len - 1);
  256. }
  257. static char *make_data_directory(const char *module_name, const char *data_dir)
  258. {
  259. struct dstr parsed_data_dir = {0};
  260. bool found = false;
  261. make_data_dir(&parsed_data_dir, data_dir, module_name);
  262. found = os_file_exists(parsed_data_dir.array);
  263. if (!found && astrcmpi_n(module_name, "lib", 3) == 0)
  264. make_data_dir(&parsed_data_dir, data_dir, module_name + 3);
  265. return parsed_data_dir.array;
  266. }
  267. static bool parse_binary_from_directory(struct dstr *parsed_bin_path,
  268. const char *bin_path, const char *file)
  269. {
  270. struct dstr directory = {0};
  271. bool found = true;
  272. dstr_copy(&directory, bin_path);
  273. dstr_replace(&directory, "%module%", file);
  274. if (dstr_end(&directory) != '/')
  275. dstr_cat_ch(&directory, '/');
  276. dstr_copy_dstr(parsed_bin_path, &directory);
  277. dstr_cat(parsed_bin_path, file);
  278. dstr_cat(parsed_bin_path, get_module_extension());
  279. if (!os_file_exists(parsed_bin_path->array)) {
  280. /* if the file doesn't exist, check with 'lib' prefix */
  281. dstr_copy_dstr(parsed_bin_path, &directory);
  282. dstr_cat(parsed_bin_path, "lib");
  283. dstr_cat(parsed_bin_path, file);
  284. dstr_cat(parsed_bin_path, get_module_extension());
  285. /* if neither exist, don't include this as a library */
  286. if (!os_file_exists(parsed_bin_path->array)) {
  287. dstr_free(parsed_bin_path);
  288. found = false;
  289. }
  290. }
  291. dstr_free(&directory);
  292. return found;
  293. }
  294. static void process_found_module(struct obs_module_path *omp, const char *path,
  295. bool directory,
  296. obs_find_module_callback_t callback,
  297. void *param)
  298. {
  299. struct obs_module_info info;
  300. struct dstr name = {0};
  301. struct dstr parsed_bin_path = {0};
  302. const char *file;
  303. char *parsed_data_dir;
  304. bool bin_found = true;
  305. file = strrchr(path, '/');
  306. file = file ? (file + 1) : path;
  307. if (strcmp(file, ".") == 0 || strcmp(file, "..") == 0)
  308. return;
  309. dstr_copy(&name, file);
  310. if (!directory) {
  311. char *ext = strrchr(name.array, '.');
  312. if (ext)
  313. dstr_resize(&name, ext - name.array);
  314. dstr_copy(&parsed_bin_path, path);
  315. } else {
  316. bin_found = parse_binary_from_directory(&parsed_bin_path,
  317. omp->bin, file);
  318. }
  319. parsed_data_dir = make_data_directory(name.array, omp->data);
  320. if (parsed_data_dir && bin_found) {
  321. info.bin_path = parsed_bin_path.array;
  322. info.data_path = parsed_data_dir;
  323. callback(param, &info);
  324. }
  325. bfree(parsed_data_dir);
  326. dstr_free(&name);
  327. dstr_free(&parsed_bin_path);
  328. }
  329. static void find_modules_in_path(struct obs_module_path *omp,
  330. obs_find_module_callback_t callback,
  331. void *param)
  332. {
  333. struct dstr search_path = {0};
  334. char *module_start;
  335. bool search_directories = false;
  336. os_glob_t *gi;
  337. dstr_copy(&search_path, omp->bin);
  338. module_start = strstr(search_path.array, "%module%");
  339. if (module_start) {
  340. dstr_resize(&search_path, module_start - search_path.array);
  341. search_directories = true;
  342. }
  343. if (!dstr_is_empty(&search_path) && dstr_end(&search_path) != '/')
  344. dstr_cat_ch(&search_path, '/');
  345. dstr_cat_ch(&search_path, '*');
  346. if (!search_directories)
  347. dstr_cat(&search_path, get_module_extension());
  348. if (os_glob(search_path.array, 0, &gi) == 0) {
  349. for (size_t i = 0; i < gi->gl_pathc; i++) {
  350. if (search_directories == gi->gl_pathv[i].directory)
  351. process_found_module(omp, gi->gl_pathv[i].path,
  352. search_directories,
  353. callback, param);
  354. }
  355. os_globfree(gi);
  356. }
  357. dstr_free(&search_path);
  358. }
  359. void obs_find_modules(obs_find_module_callback_t callback, void *param)
  360. {
  361. if (!obs)
  362. return;
  363. for (size_t i = 0; i < obs->module_paths.num; i++) {
  364. struct obs_module_path *omp = obs->module_paths.array + i;
  365. find_modules_in_path(omp, callback, param);
  366. }
  367. }
  368. void obs_enum_modules(obs_enum_module_callback_t callback, void *param)
  369. {
  370. struct obs_module *module;
  371. if (!obs)
  372. return;
  373. module = obs->first_module;
  374. while (module) {
  375. callback(param, module);
  376. module = module->next;
  377. }
  378. }
  379. void free_module(struct obs_module *mod)
  380. {
  381. if (!mod)
  382. return;
  383. if (mod->module) {
  384. if (mod->free_locale)
  385. mod->free_locale();
  386. if (mod->loaded && mod->unload)
  387. mod->unload();
  388. /* there is no real reason to close the dynamic libraries,
  389. * and sometimes this can cause issues. */
  390. /* os_dlclose(mod->module); */
  391. }
  392. bfree(mod->mod_name);
  393. bfree(mod->bin_path);
  394. bfree(mod->data_path);
  395. bfree(mod);
  396. }
  397. lookup_t *obs_module_load_locale(obs_module_t *module,
  398. const char *default_locale, const char *locale)
  399. {
  400. struct dstr str = {0};
  401. lookup_t *lookup = NULL;
  402. if (!module || !default_locale || !locale) {
  403. blog(LOG_WARNING, "obs_module_load_locale: Invalid parameters");
  404. return NULL;
  405. }
  406. dstr_copy(&str, "locale/");
  407. dstr_cat(&str, default_locale);
  408. dstr_cat(&str, ".ini");
  409. char *file = obs_find_module_file(module, str.array);
  410. if (file)
  411. lookup = text_lookup_create(file);
  412. bfree(file);
  413. if (!lookup) {
  414. blog(LOG_WARNING, "Failed to load '%s' text for module: '%s'",
  415. default_locale, module->file);
  416. goto cleanup;
  417. }
  418. if (astrcmpi(locale, default_locale) == 0)
  419. goto cleanup;
  420. dstr_copy(&str, "/locale/");
  421. dstr_cat(&str, locale);
  422. dstr_cat(&str, ".ini");
  423. file = obs_find_module_file(module, str.array);
  424. if (!text_lookup_add(lookup, file))
  425. blog(LOG_WARNING, "Failed to load '%s' text for module: '%s'",
  426. locale, module->file);
  427. bfree(file);
  428. cleanup:
  429. dstr_free(&str);
  430. return lookup;
  431. }
  432. #define REGISTER_OBS_DEF(size_var, structure, dest, info) \
  433. do { \
  434. struct structure data = {0}; \
  435. if (!size_var) { \
  436. blog(LOG_ERROR, "Tried to register " #structure \
  437. " outside of obs_module_load"); \
  438. return; \
  439. } \
  440. \
  441. if (size_var > sizeof(data)) { \
  442. blog(LOG_ERROR, \
  443. "Tried to register " #structure \
  444. " with size %llu which is more " \
  445. "than libobs currently supports " \
  446. "(%llu)", \
  447. (long long unsigned)size_var, \
  448. (long long unsigned)sizeof(data)); \
  449. goto error; \
  450. } \
  451. \
  452. memcpy(&data, info, size_var); \
  453. da_push_back(dest, &data); \
  454. } while (false)
  455. #define CHECK_REQUIRED_VAL(type, info, val, func) \
  456. do { \
  457. if ((offsetof(type, val) + sizeof(info->val) > size) || \
  458. !info->val) { \
  459. blog(LOG_ERROR, \
  460. "Required value '" #val "' for " \
  461. "'%s' not found. " #func " failed.", \
  462. info->id); \
  463. goto error; \
  464. } \
  465. } while (false)
  466. #define HANDLE_ERROR(size_var, structure, info) \
  467. do { \
  468. struct structure data = {0}; \
  469. if (!size_var) \
  470. return; \
  471. \
  472. memcpy(&data, info, \
  473. sizeof(data) < size_var ? sizeof(data) : size_var); \
  474. \
  475. if (info->type_data && info->free_type_data) \
  476. info->free_type_data(info->type_data); \
  477. } while (false)
  478. #define source_warn(format, ...) \
  479. blog(LOG_WARNING, "obs_register_source: " format, ##__VA_ARGS__)
  480. #define output_warn(format, ...) \
  481. blog(LOG_WARNING, "obs_register_output: " format, ##__VA_ARGS__)
  482. #define encoder_warn(format, ...) \
  483. blog(LOG_WARNING, "obs_register_encoder: " format, ##__VA_ARGS__)
  484. #define service_warn(format, ...) \
  485. blog(LOG_WARNING, "obs_register_service: " format, ##__VA_ARGS__)
  486. void obs_register_source_s(const struct obs_source_info *info, size_t size)
  487. {
  488. struct obs_source_info data = {0};
  489. struct darray *array = NULL;
  490. if (info->type == OBS_SOURCE_TYPE_INPUT) {
  491. array = &obs->input_types.da;
  492. } else if (info->type == OBS_SOURCE_TYPE_FILTER) {
  493. array = &obs->filter_types.da;
  494. } else if (info->type == OBS_SOURCE_TYPE_TRANSITION) {
  495. array = &obs->transition_types.da;
  496. } else if (info->type != OBS_SOURCE_TYPE_SCENE) {
  497. source_warn("Tried to register unknown source type: %u",
  498. info->type);
  499. goto error;
  500. }
  501. if (get_source_info2(info->id, info->version)) {
  502. source_warn("Source '%s' already exists! "
  503. "Duplicate library?",
  504. info->id);
  505. goto error;
  506. }
  507. memcpy(&data, info, size);
  508. /* mark audio-only filters as an async filter categorically */
  509. if (data.type == OBS_SOURCE_TYPE_FILTER) {
  510. if ((data.output_flags & OBS_SOURCE_VIDEO) == 0)
  511. data.output_flags |= OBS_SOURCE_ASYNC;
  512. }
  513. if (data.type == OBS_SOURCE_TYPE_TRANSITION) {
  514. if (data.get_width)
  515. source_warn("get_width ignored registering "
  516. "transition '%s'",
  517. data.id);
  518. if (data.get_height)
  519. source_warn("get_height ignored registering "
  520. "transition '%s'",
  521. data.id);
  522. data.output_flags |= OBS_SOURCE_COMPOSITE | OBS_SOURCE_VIDEO |
  523. OBS_SOURCE_CUSTOM_DRAW;
  524. }
  525. if ((data.output_flags & OBS_SOURCE_COMPOSITE) != 0) {
  526. if ((data.output_flags & OBS_SOURCE_AUDIO) != 0) {
  527. source_warn("Source '%s': Composite sources "
  528. "cannot be audio sources",
  529. info->id);
  530. goto error;
  531. }
  532. if ((data.output_flags & OBS_SOURCE_ASYNC) != 0) {
  533. source_warn("Source '%s': Composite sources "
  534. "cannot be async sources",
  535. info->id);
  536. goto error;
  537. }
  538. }
  539. #define CHECK_REQUIRED_VAL_(info, val, func) \
  540. CHECK_REQUIRED_VAL(struct obs_source_info, info, val, func)
  541. CHECK_REQUIRED_VAL_(info, get_name, obs_register_source);
  542. if (info->type != OBS_SOURCE_TYPE_FILTER &&
  543. info->type != OBS_SOURCE_TYPE_TRANSITION &&
  544. (info->output_flags & OBS_SOURCE_VIDEO) != 0 &&
  545. (info->output_flags & OBS_SOURCE_ASYNC) == 0) {
  546. CHECK_REQUIRED_VAL_(info, get_width, obs_register_source);
  547. CHECK_REQUIRED_VAL_(info, get_height, obs_register_source);
  548. }
  549. if ((data.output_flags & OBS_SOURCE_COMPOSITE) != 0) {
  550. CHECK_REQUIRED_VAL_(info, audio_render, obs_register_source);
  551. }
  552. #undef CHECK_REQUIRED_VAL_
  553. if (size > sizeof(data)) {
  554. source_warn("Tried to register obs_source_info with size "
  555. "%llu which is more than libobs currently "
  556. "supports (%llu)",
  557. (long long unsigned)size,
  558. (long long unsigned)sizeof(data));
  559. goto error;
  560. }
  561. /* version-related stuff */
  562. data.unversioned_id = data.id;
  563. if (data.version) {
  564. struct dstr versioned_id = {0};
  565. dstr_printf(&versioned_id, "%s_v%d", data.id,
  566. (int)data.version);
  567. data.id = versioned_id.array;
  568. } else {
  569. data.id = bstrdup(data.id);
  570. }
  571. if (array)
  572. darray_push_back(sizeof(struct obs_source_info), array, &data);
  573. da_push_back(obs->source_types, &data);
  574. return;
  575. error:
  576. HANDLE_ERROR(size, obs_source_info, info);
  577. }
  578. void obs_register_output_s(const struct obs_output_info *info, size_t size)
  579. {
  580. if (find_output(info->id)) {
  581. output_warn("Output id '%s' already exists! "
  582. "Duplicate library?",
  583. info->id);
  584. goto error;
  585. }
  586. #define CHECK_REQUIRED_VAL_(info, val, func) \
  587. CHECK_REQUIRED_VAL(struct obs_output_info, info, val, func)
  588. CHECK_REQUIRED_VAL_(info, get_name, obs_register_output);
  589. CHECK_REQUIRED_VAL_(info, create, obs_register_output);
  590. CHECK_REQUIRED_VAL_(info, destroy, obs_register_output);
  591. CHECK_REQUIRED_VAL_(info, start, obs_register_output);
  592. CHECK_REQUIRED_VAL_(info, stop, obs_register_output);
  593. if (info->flags & OBS_OUTPUT_ENCODED) {
  594. CHECK_REQUIRED_VAL_(info, encoded_packet, obs_register_output);
  595. } else {
  596. if (info->flags & OBS_OUTPUT_VIDEO)
  597. CHECK_REQUIRED_VAL_(info, raw_video,
  598. obs_register_output);
  599. if (info->flags & OBS_OUTPUT_AUDIO) {
  600. if (info->flags & OBS_OUTPUT_MULTI_TRACK) {
  601. CHECK_REQUIRED_VAL_(info, raw_audio2,
  602. obs_register_output);
  603. } else {
  604. CHECK_REQUIRED_VAL_(info, raw_audio,
  605. obs_register_output);
  606. }
  607. }
  608. }
  609. #undef CHECK_REQUIRED_VAL_
  610. REGISTER_OBS_DEF(size, obs_output_info, obs->output_types, info);
  611. return;
  612. error:
  613. HANDLE_ERROR(size, obs_output_info, info);
  614. }
  615. void obs_register_encoder_s(const struct obs_encoder_info *info, size_t size)
  616. {
  617. if (find_encoder(info->id)) {
  618. encoder_warn("Encoder id '%s' already exists! "
  619. "Duplicate library?",
  620. info->id);
  621. goto error;
  622. }
  623. #define CHECK_REQUIRED_VAL_(info, val, func) \
  624. CHECK_REQUIRED_VAL(struct obs_encoder_info, info, val, func)
  625. CHECK_REQUIRED_VAL_(info, get_name, obs_register_encoder);
  626. CHECK_REQUIRED_VAL_(info, create, obs_register_encoder);
  627. CHECK_REQUIRED_VAL_(info, destroy, obs_register_encoder);
  628. if ((info->caps & OBS_ENCODER_CAP_PASS_TEXTURE) != 0)
  629. CHECK_REQUIRED_VAL_(info, encode_texture, obs_register_encoder);
  630. else
  631. CHECK_REQUIRED_VAL_(info, encode, obs_register_encoder);
  632. if (info->type == OBS_ENCODER_AUDIO)
  633. CHECK_REQUIRED_VAL_(info, get_frame_size, obs_register_encoder);
  634. #undef CHECK_REQUIRED_VAL_
  635. REGISTER_OBS_DEF(size, obs_encoder_info, obs->encoder_types, info);
  636. return;
  637. error:
  638. HANDLE_ERROR(size, obs_encoder_info, info);
  639. }
  640. void obs_register_service_s(const struct obs_service_info *info, size_t size)
  641. {
  642. if (find_service(info->id)) {
  643. service_warn("Service id '%s' already exists! "
  644. "Duplicate library?",
  645. info->id);
  646. goto error;
  647. }
  648. #define CHECK_REQUIRED_VAL_(info, val, func) \
  649. CHECK_REQUIRED_VAL(struct obs_service_info, info, val, func)
  650. CHECK_REQUIRED_VAL_(info, get_name, obs_register_service);
  651. CHECK_REQUIRED_VAL_(info, create, obs_register_service);
  652. CHECK_REQUIRED_VAL_(info, destroy, obs_register_service);
  653. #undef CHECK_REQUIRED_VAL_
  654. REGISTER_OBS_DEF(size, obs_service_info, obs->service_types, info);
  655. return;
  656. error:
  657. HANDLE_ERROR(size, obs_service_info, info);
  658. }
  659. void obs_register_modal_ui_s(const struct obs_modal_ui *info, size_t size)
  660. {
  661. #define CHECK_REQUIRED_VAL_(info, val, func) \
  662. CHECK_REQUIRED_VAL(struct obs_modal_ui, info, val, func)
  663. CHECK_REQUIRED_VAL_(info, task, obs_register_modal_ui);
  664. CHECK_REQUIRED_VAL_(info, target, obs_register_modal_ui);
  665. CHECK_REQUIRED_VAL_(info, exec, obs_register_modal_ui);
  666. #undef CHECK_REQUIRED_VAL_
  667. REGISTER_OBS_DEF(size, obs_modal_ui, obs->modal_ui_callbacks, info);
  668. return;
  669. error:
  670. HANDLE_ERROR(size, obs_modal_ui, info);
  671. }
  672. void obs_register_modeless_ui_s(const struct obs_modeless_ui *info, size_t size)
  673. {
  674. #define CHECK_REQUIRED_VAL_(info, val, func) \
  675. CHECK_REQUIRED_VAL(struct obs_modeless_ui, info, val, func)
  676. CHECK_REQUIRED_VAL_(info, task, obs_register_modeless_ui);
  677. CHECK_REQUIRED_VAL_(info, target, obs_register_modeless_ui);
  678. CHECK_REQUIRED_VAL_(info, create, obs_register_modeless_ui);
  679. #undef CHECK_REQUIRED_VAL_
  680. REGISTER_OBS_DEF(size, obs_modeless_ui, obs->modeless_ui_callbacks,
  681. info);
  682. return;
  683. error:
  684. HANDLE_ERROR(size, obs_modeless_ui, info);
  685. }