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