本文主要是介绍OBS 源码分析- 采集方案之二 (摄像头采集),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 1、OBS采集方案
- 2、OBS 摄像头采集
- 2.1 DirectShow 基础
- 2.2 OBS 的实现
1、OBS采集方案
obs的视屏录制主要分3种:
窗口采集:采集应用程序窗口
显示器采集:也叫全屏采集,可以采集整个屏幕,当有多个显示器时,可以设置采集其中一个显示器
游戏采集:可以采集游戏窗口
extern struct obs_source_info duplicator_capture_info;
extern struct obs_source_info monitor_capture_info;
extern struct obs_source_info window_capture_info;
extern struct obs_source_info game_capture_info;
2、OBS 摄像头采集
obs 的方案采用 directshow 的方案,如果需要查看源码需要下载submodule, 因为在另外一个仓库。
2.1 DirectShow 基础
DirectShow 其主要设计目标是通过将应用程序与数据传输的复杂性、硬件差异和同步隔离,简化在 Windows 平台上创建数字媒体应用程序的任务 ,DirectShow简化媒体播放、格式转换和捕获任务
下图显示了应用程序、DirectShow组件以及应用程序支持的一些硬件和软件DirectShow之间的关系。
在这里插入图片描述
Filter是DirectShow技术体系中最基本的概念。如上图所示,DirectShow中的Filter分成三大类:Source Filter、Transform Filter、Render Filter。Source Filter就是提供数据源的Filter,所有的数据都是从Source Filter流出去的。不管是多媒体文件还是多媒体设备,Source Filter都进行了封装统一了接口,在使用方式上保持了一致。Transform Filter则是对数据进行操作处理的Filter,所有的图像操作都应该在这里进行。而Render Filter则是用来渲染图像的Filter,不管是保存到文件还是输出到其他地方,都由这个Render Filter来实现。Windows系统本身提供了非常多的Filter,我们在开发的时候可以直接使用。
下面是UI 界面配置directshow 软件(graphstudionext)的示意图,通过拖动就可以配置,filter 的概念。而整个图就 Filter Graph。如果需要就向 Filter Graph 添加filter , 并且链接 pin(引脚)
2.2 OBS 的实现
由于OBS 封装了dshow ,如果比较了解dshow ,那obs也是比较简单的,大家可以看下我整理的类图,帮助理解
类图
这边只说下,回调的数据接口在什么位置吧。回调接口是通过配置中callback 传进入的。 回调接口是DShowInput::OnVideoData
bool DShowInput::UpdateVideoConfig(obs_data_t *settings) {
videoConfig.name = id.name;videoConfig.path = id.path;videoConfig.useDefaultConfig = resType == ResType_Preferred;videoConfig.cx = cx;videoConfig.cy_abs = abs(cy);videoConfig.cy_flip = cy < 0;videoConfig.frameInterval = interval;videoConfig.internalFormat = format;deviceHasAudio = dev.audioAttached;deviceHasSeparateAudioFilter = dev.separateAudioFilter;//设置回调函数videoConfig.callback = std::bind(&DShowInput::OnVideoData, this,placeholders::_1, placeholders::_2,placeholders::_3, placeholders::_4,placeholders::_5, placeholders::_6);videoConfig.format = videoConfig.internalFormat;if (!device.SetVideoConfig(&videoConfig)) {blog(LOG_WARNING, "%s: device.SetVideoConfig failed",obs_source_get_name(source));return false;}}
最终数据存放到source->async_frames,
static void
obs_source_output_video_internal(obs_source_t *source,const struct obs_source_frame *frame)
{if (!obs_source_valid(source, "obs_source_output_video"))return;if (!frame) {source->async_active = false;return;}struct obs_source_frame *output = cache_video(source, frame);/* ------------------------------------------- */pthread_mutex_lock(&source->async_mutex);if (output) {if (os_atomic_dec_long(&output->refs) == 0) {obs_source_frame_destroy(output);output = NULL;} else {da_push_back(source->async_frames, &output);source->async_active = true;}}pthread_mutex_unlock(&source->async_mutex);
}
在 tick_sources 中 循环调用所有source , obs_source_video_tick中获取视频frame,等待渲染使用
tick_sources ->obs_source_video_tick ->async_tick
static void async_tick(obs_source_t *source)
{uint64_t sys_time = obs->video.video_time;pthread_mutex_lock(&source->async_mutex);if (deinterlacing_enabled(source)) {deinterlace_process_last_frame(source, sys_time);} else {if (source->cur_async_frame) {remove_async_frame(source, source->cur_async_frame);source->cur_async_frame = NULL;}source->cur_async_frame = get_closest_frame(source, sys_time);}source->last_sys_timestamp = sys_time;pthread_mutex_unlock(&source->async_mutex);if (source->cur_async_frame)source->async_update_texture =set_async_texture_size(source, source->cur_async_frame);
}
这篇关于OBS 源码分析- 采集方案之二 (摄像头采集)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!