OBS二次开发 | 源码解读(一) | 初始化流程

本文详细介绍了OBS28.1.0版本的初始化过程,包括判断是否已初始化、音频和视频的基本配置、设置监听设备、模块路径指定、加载模块、推流服务初始化、输出创建、场景构建及采集方式设定。每个步骤都包含了函数声明和调用,为OBS二次开发提供了参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

!!!注意:不同版本的OBS源码,函数、参数等有细节性差别…

前期回顾:
OBS二次开发 | 构建、编译

OBS28.1.0源码

一、判断是否已经初始化OBS

1.函数声明/定义

/** @return true if the main OBS context has been initialized */
EXPORT bool obs_initialized(void);

bool obs_initialized(void)
{
   
	return obs != NULL;
}

2.函数调用

在进行OBS初始化之前,先判断是否已经完成了初始化。若已完成初始化,则不需再进行初始化;若未进行初始化,再进行初始化。

if (obs_initialized())
	return false;

二、初始化OBS

1.函数声明/定义

/**
 * Initializes OBS
 * @param  locale              The locale to use for modules
 * @param  module_config_path  Path to module config storage directory (or NULL if none)
 * @param  store               The profiler name store for OBS to use or NULL
 */
EXPORT bool obs_startup(const char *locale, const char *module_config_path, profiler_name_store_t *store);

//核心:obs_init(locale, module_config_path, store);
static const char *obs_startup_name = "obs_startup";
bool obs_startup(const char *locale, const char *module_config_path, profiler_name_store_t *store)
{
   
	bool success;
	profile_start(obs_startup_name);
	if (obs) {
   
		blog(LOG_WARNING, "Tried to call obs_startup more than once");
		return false;
	}
#ifdef _WIN32
	com_initialized = initialize_com();
#endif
	/*
	其中obs_init(locale, module_config_path, store);对struct obs_core *obs;进行初始化设置。成功则返回true;
	static bool obs_init(const char *locale, const char *module_config_path, profiler_name_store_t *store)
	*/
	success = obs_init(locale, module_config_path, store);
	profile_end(obs_startup_name);
	if (!success) //失败则调用obs_shutdown();将释放obs并置空obs = NULL;。
		obs_shutdown();
	return success;
}

2.函数调用

这里对struct obs_core *obs;进行初始化设置。

if (!obs_startup("zh-CN", nullptr, nullptr))
	return false;

三、初始化音频基本配置

1.函数定义/声明

设置基本音频输出格式、通道、样本等…
如果输出当前处于活动状态,则无法重置基本音频。

EXPORT bool obs_reset_audio2(const struct obs_audio_info2 *oai);
//核心:obs_init_audio(&ai);
bool obs_reset_audio2(const struct obs_audio_info2 *oai)
{
   
	struct obs_core_audio *audio = &obs->audio;
	struct audio_output_info ai;
	/* don't allow changing of audio settings if active. */
	if (!obs || (audio->audio && audio_output_active(audio->audio)))
		return false;
	obs_free_audio();
	if (!oai)
		return true;
	if (oai->max_buffering_ms) {
   
		uint32_t max_frames = oai->max_buffering_ms *
				      oai->samples_per_sec / SEC_TO_MSEC;
		max_frames += (AUDIO_OUTPUT_FRAMES - 1);
		audio->max_buffering_ticks = max_frames / AUDIO_OUTPUT_FRAMES;
	} else {
   
		audio->max_buffering_ticks = 45;
	}
	audio->fixed_buffer = oai->fixed_buffering;
	int max_buffering_ms = audio->max_buffering_ticks *
			       AUDIO_OUTPUT_FRAMES * SEC_TO_MSEC /
			       (int)oai->samples_per_sec;

	ai.name = "Audio";
	ai.samples_per_sec = oai->samples_per_sec;
	ai.format = AUDIO_FORMAT_FLOAT_PLANAR;
	ai.speakers = oai->speakers;
	ai.input_callback = audio_callback;
	blog(LOG_INFO, "---------------------------------");
	blog(LOG_INFO,
	     "audio settings reset:\n"
	     "\tsamples per sec: %d\n"
	     "\tspeakers:        %d\n"
	     "\tmax buffering:   %d milliseconds\n"
	     "\tbuffering type:  %s",
	     (int)ai.samples_per_sec, (int)ai.speakers, max_buffering_ms,
	     oai->fixed_buffering ? "fixed" : "dynamically increasing");
	//其中audio_output_open(&audio->audio, ai);对struct audio_output *out;进行初始化设置。成功则返回true;失败则调用相关函数释放out。
	return obs_init_audio(&ai);
}

2.函数调用

设置音频采样率和声道。可根据自身需求设置更多参数…

bool ResetAudio(uint32_t samples_per_sec = 48000); //采样率默认48kHZ
bool 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值