Windows平台RTMP推送摄像头对接介绍

背景

好多开发者在对接大牛直播SDK(官方)的Windows平台RTMP推送时,不熟悉摄像头调用,实际上,摄像头调用逻辑并不复杂,以下是大概流程:

  1. 首先调用我们sdk接口获取摄像头个数,调用接口是:GetVideoCaptureDeviceNumber()
  2. 接着需要获取每个摄像头的id和name,调用接口是:GetVideoCaptureDeviceInfo()
  3. 接下来针对某个指定的摄像头,需要获取它的分辨率列表个数, 调用接口是:GetVideoCaptureDeviceCapabilityNumber()
  4. 根据摄像头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, // 已经编码的视频数据,目前支持H264
            NT_PB_E_VIDEO_OPTION_WINDOW = 0x5, // 采集窗口
        }

更详细的集成参考,参见:Windows平台RTMP直播推送集成简要说明_乐学吧-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值