初始化流程 | 源码解读
!!!注意:不同版本的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, "---------------------------------")