OBS源碼學習(四)-插件模塊加載流程

一、OBS Studio在架構上採用的是微內核+插件的形式開發的,至於微內核的介紹請自行百度。OBS開源社區這樣寫的目的是爲了提高項目的可維護性,也讓新功能的擴展變得更加簡單,OBS內部開發了一些常用的插件如下圖:在這裏插入圖片描述
在OBS啓動初始化void OBSBasic::OBSInit()時,會調用AddExtraModulePaths函數,這個函數的目的是設置插件的路徑

static void AddExtraModulePaths()
{
   
   
	char base_module_dir[512];
#if defined(_WIN32) || defined(__APPLE__)
	int ret = GetProgramDataPath(base_module_dir, sizeof(base_module_dir),
				     "obs-studio/plugins/%module%");
#else
	int ret = GetConfigPath(base_module_dir, sizeof(base_module_dir),
				"obs-studio/plugins/%module%");
#endif

	if (ret <= 0)
		return;

	string path = base_module_dir;
#if defined(__APPLE__)
	obs_add_module_path((path + "/bin").c_str(), (path + "/data").c_str());

	BPtr<char> config_bin =
		os_get_config_path_ptr("obs-studio/plugins/%module%/bin");
	BPtr<char> config_data =
		os_get_config_path_ptr("obs-studio/plugins/%module%/data");
	obs_add_module_path(config_bin, config_data);

#elif ARCH_BITS == 64
	obs_add_module_path((path + "/bin/64bit").c_str(),
			    (path + "/data").c_str());
#else
	obs_add_module_path((path + "/bin/32bit").c_str(),
			    (path + "/data").c_str());
#endif
}

會將設置的路徑給到obs->module_paths

void obs_add_module_path(const char *bin, const char *data)
{
   
   
	struct obs_module_path omp;

	if (!obs || !bin || !data)
		return;

	omp.bin = bstrdup(bin);
	omp.data = bstrdup(data);
	da_push_back(obs->module_paths, &omp);
}

設置完路徑之後會接着調用obs_load_all_modules函數

void obs_load_all_modules(void)
{
   
   
	profile_start(obs_load_all_modules_name);
	obs_find_modules(load_all_callback, NULL);
#ifdef _WIN32
	profile_start(reset_win32_symbol_paths_name);
	reset_win32_symbol_paths();
	profile_end(reset_win32_symbol_paths_name);
#endif
	profile_end(obs_load_all_modules_name);
}

然後會在設置的路徑下分別查找並處理每一個插件,

void obs_find_modules(obs_find_module_callback_t callback, void *param)
{
   
   
	if (!obs)
		return;

	for (size_t i = 0; i < obs->module_paths.num; i++) {
   
   
		struct obs_module_path *omp = obs->module_paths.array + i;
		//遍歷查找並分別處理
		find_modules_in_path(omp, callback, param);
	}
}
static void find_modules_in_path(struct obs_module_path *omp,
				 obs_find_module_callback_t callback,
				 void *param)
{
   
   
	struct dstr search_path = {
   
   0};
	char *module_start;
	bool search_directories = false;
	os_glob_t *gi;

	dstr_copy(&search_path, omp->bin);

	module_start = strstr(search_path.array, "%module%");
	if (module_start) {
   
   
		dstr_resize(&search_path, module_start - search_path.array);
		search_directories = true;
	}

	if (!dstr_is_empty(&search_path) && dstr_end(&search_path) != '/')
		dstr_cat_ch(&search_path, '/');

	dstr_cat_ch(&search_path, '*');
	if (!search_directories)
		dstr_cat(&search_path, get_module_extension());

	if (os_glob(search_path.array, 0, &gi) == 0) {
   
   
		for (size_t i = 0; i < gi->gl_pathc; i++) {
   
   
			if (search_directories == gi->gl_pathv[i].directory)
			
				process_found_module(omp, gi->gl_pathv[i].path,
						     search_directories,
						     callback, param);
		}

		os_globfree(gi);
	}

	dstr_free(&search_path);
}
static void process_found_module(struct obs_module_path *omp, const char *path,
				 bool directory,
				 obs_find_module_callback_t callback,
				 void *param)
{
   
   
	struct obs_module_info info;
	struct dstr name = {
   
   0};
	struct dstr parsed_bin_path = {
   
   0};
	const char *file;
	char *parsed_data_dir;
	bool bin_found = true;

	file = strrchr(path, '/');
	file = file ? (file + 1) : path;

	if (strcmp(file, ".") == 0 || strcmp(file, "..") == 0)
		return;

	dstr_copy(&name, file);
	if (!directory) {
   
   
		char *ext = strrchr(name.array, '.');
		if (ext)
			dstr_resize(&name, ext - name.array);

		dstr_copy(&parsed_bin_path, path);
	} else {
   
   
		bin_found = parse_binary_from_directory(&parsed_bin_path,
							omp->bin, file);
	}

	parsed_data_dir = make_data_directory(name.array, omp->data);

	if (parsed_data_dir && bin_found) {
   
   
		info.bin_path = parsed_bin_path.array;
		info.data_path = parsed_data_dir;
		//得到每個文件的詳細路徑,並調用回調函數
		callback(param, &info);
	}

	bfree(parsed_data_dir);
	dstr_free(&name);
	dstr_free(&parsed_bin_path);
}

調用回調函數load_all_callback ,打開插件並初始化

static void load_all_callback(void *param, const struct obs_module_info *info)
{
   
   
	obs_module_t *module;

	int code = obs_open_module(&module, info->bin_path, info->data_path);
	if (code != MODULE_SUCCESS) {
   
   
		blog(LOG_DEBUG, "Failed to load module file '%s': %d",
		     info->bin_path, code);
		return;
	}

	obs_init_module(module);

	UNUSED_PARAMETER(param);
}

調用obs_open_module()函數利用os_dlopen(path)加載插件,返回HMODULE指針

int obs_open_module(obs_module_t **module, const char *path,
		    const char *data_path)
{
   
   
	struct obs_module mod = {
   
   0};
	int errorcode;

	if (!module || !path || !obs)
		return MODULE_ERROR;

#ifdef __APPLE__
	/* HACK: Do not load obsolete obs-browser build on macOS; the
	 * obs-browser plugin used to live in the Application Support
	 * directory. */
	if (astrstri(path, "Library/Application Support") != NULL &&
	    astrstri(path, "obs-browser") != NULL) {
   
   
		blog(LOG_WARNING, "Ignoring old obs-browser.so version");
		return MODULE_ERROR;
	}
#endif

	blog(LOG_DEBUG, "---------------------------------");

	mod.module = os_dlopen(path);
	if (!mod.module) {
   
   
		blog(LOG_WARNING, "Module '%s' not loaded", path);
		return MODULE_FILE_NOT_FOUND;
	}

	errorcode = load_module_exports(&mod, path);
	if (errorcode != MODULE_SUCCESS)
		return errorcode;

	mod.bin_path = bstrdup(path);
	mod.file = strrchr(mod.bin_path, '/');
	mod.file = (!mod.file) ? mod.bin_path : (mod.file + 1);
	mod.mod_name = get_module_name(mod.file);
	mod.data_path = bstrdup(data_path);
	mod.next = obs->first_module;

	if (mod.file) {
   
   
		blog(LOG_DEBUG, "Loading module: %s", mod.file);
	}

	*module = bmemdup(&mod, sizeof(mod));
	obs->first_module = (*module);
	mod.set_pointer(*module);

	if (mod.set_locale)
		mod.set_locale(obs->locale);

	return MODULE_SUCCESS;
}

最後調用obs_init_module對模塊進行初始化,並且會調用每個模塊的load指針函數對應的obs_module_load函數

bool obs_init_module(obs_module_t *module)
{
   
   
	if (!module || !obs)
		return false;
	if (module->loaded)
		return true;

	const char *profile_name =
		profile_store_name(obs_get_profiler_name_store(),
				   "obs_init_module(%s)", module->file);
	profile_start(profile_name);

	module->loaded = module->load();
	if (!module->loaded)
		blog(LOG_WARNING, "Failed to initialize module '%s'",
		     module->file);

	profile_end(profile_name);
	return module->loaded;
}

以調用X264模塊爲例,調用obs_module_load函數

#include <obs-module.h>

OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("obs-x264", "en-US")
MODULE_EXPORT const char *obs_module_description(void)
{
   
   
	return "x264 based encoder";
}

extern struct obs_encoder_info obs_x264_encoder;

bool obs_module_load(void)
{
   
   
	obs_register_encoder(&obs_x264_encoder);
	return true;
}

到這裏就結束了,每個模塊的調用方式都是一樣的,大家可以看看!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章