背景
好多开发者在对接大牛直播SDK(官方)的Windows平台RTMP推送时,不熟悉摄像头调用,实际上,摄像头调用逻辑并不复杂,以下是大概流程:
- 首先调用我们sdk接口获取摄像头个数,调用接口是:GetVideoCaptureDeviceNumber()
- 接着需要获取每个摄像头的id和name,调用接口是:GetVideoCaptureDeviceInfo()
- 接下来针对某个指定的摄像头,需要获取它的分辨率列表个数, 调用接口是:GetVideoCaptureDeviceCapabilityNumber()
- 根据摄像头id和分辨率的index,获取实际的分辨率,调用接口:GetVideoCaptureDeviceCapability()
对的,比如一个摄像头有四个分辨率 (1920*1080,1080*720,640*480, 320*240), 调用GetVideoCaptureDeviceCapabilityNumber 拿到的值就是4,想拿第一个index的分辨率,就传入GetVideoCaptureDeviceCapability(id, 0, capability)。
接口对接
以C#设计为例,接口设计如下:
/** 获取摄像头数量* * pNumer: 返回设备数量* * 成功返回 NT_ERC_OK*/[DllImport(@"SmartPublisherSDK.dll")]public static extern UInt32 NT_PB_GetVideoCaptureDeviceNumber(ref Int32 pNumer);/** 返回摄像头设备信息* * device_index: 设备索引* device_name_utf8: 设备名称,传NULL将不返回名称,如果不是NULL的话, device_name_length必须大于等于256, 返回utf8编码的设备名称* device_name_length: 设备名称缓冲大小,如果device_name_utf8是NULL,则传入0, 否则必须大于等于256* device_unique_id_utf8: 设备唯一ID, 传NULL将不返回ID,如果不传NULL的话,device_unique_id_length必须大于等于1024,返回utf8编码的设备ID* device_unique_id_length: 设备唯一ID缓冲代销, 如果是device_unique_id_utf8NULL, 则传入0,否则必须大于等于1024* * 成功返回 NT_ERC_OK*/[DllImport(@"SmartPublisherSDK.dll")]public static extern UInt32 NT_PB_GetVideoCaptureDeviceInfo(Int32 device_index,[MarshalAs(UnmanagedType.LPStr)] StringBuilder device_name_utf8,UInt32 device_name_length,[MarshalAs(UnmanagedType.LPStr)] StringBuilder device_unique_id_utf8,UInt32 device_unique_id_length);/** 返回摄像头能力数* * device_unique_id_utf8: 设备唯一id* capability_number: 返回的设备能力数* * 成功返回 NT_ERC_OK*/[DllImport(@"SmartPublisherSDK.dll")]public static extern UInt32 NT_PB_GetVideoCaptureDeviceCapabilityNumber([MarshalAs(UnmanagedType.LPStr)] String device_unique_id_utf8,ref Int32 capability_number);/** 返回摄像头能力* * device_unique_id_utf8: 设备唯一id* device_capability_index: 设备能力索引* capability: 设备能力* * 成功返回 NT_ERC_OK*/[DllImport(@"SmartPublisherSDK.dll", EntryPoint = "NT_PB_GetVideoCaptureDeviceCapability", CallingConvention = CallingConvention.StdCall)]public static extern UInt32 NT_PB_GetVideoCaptureDeviceCapability([MarshalAs(UnmanagedType.LPStr)] String device_unique_id_utf8,Int32 device_capability_index,ref NT_PB_VideoCaptureCapability capability);
调用逻辑如下:
private void FillCameraInfo(){int device_number = 0;if (NTBaseCodeDefine.NT_ERC_OK != NTSmartPublisherSDK.NT_PB_GetVideoCaptureDeviceNumber(ref device_number)){return;}if (device_number < 1){return;}cameras_.Clear();for (int i = 0; i < device_number; ++i){CameraInfo info = new CameraInfo();info.capabilities_ = new List<NT_PB_VideoCaptureCapability>();StringBuilder name = new StringBuilder(256);StringBuilder id = new StringBuilder(1024);if (NTBaseCodeDefine.NT_ERC_OK != NTSmartPublisherSDK.NT_PB_GetVideoCaptureDeviceInfo(i,name, 256,id, 1024)){continue;}info.name_ = name.ToString();info.id_ = id.ToString();int capability_number = 0;if (NTBaseCodeDefine.NT_ERC_OK != NTSmartPublisherSDK.NT_PB_GetVideoCaptureDeviceCapabilityNumber(id.ToString(), ref capability_number)){continue;}bool is_failed = false;for (int j = 0; j < capability_number; ++j){NT_PB_VideoCaptureCapability capability = new NT_PB_VideoCaptureCapability();if (NTBaseCodeDefine.NT_ERC_OK != NTSmartPublisherSDK.NT_PB_GetVideoCaptureDeviceCapability(id.ToString(), j, ref capability)){is_failed = true;break;}info.capabilities_.Add(capability);}if (!is_failed){cameras_.Add(info);}}}
在此之前,需要设置是采集摄像头,还是屏幕或者窗口:
/*定义Video源选项*/public enum NT_PB_E_VIDEO_OPTION : uint{NT_PB_E_VIDEO_OPTION_NO_VIDEO = 0x0,NT_PB_E_VIDEO_OPTION_SCREEN = 0x1, // 采集屏幕NT_PB_E_VIDEO_OPTION_CAMERA = 0x2, // 摄像头采集NT_PB_E_VIDEO_OPTION_LAYER = 0x3, // 视频合并,比如桌面叠加摄像头等NT_PB_E_VIDEO_OPTION_ENCODED_DATA = 0x4, // 已经编码的视频数据,目前支持H264NT_PB_E_VIDEO_OPTION_WINDOW = 0x5, // 采集窗口}
更详细的集成参考,参见:Windows平台RTMP直播推送集成简要说明_乐学吧-CSDN博客