wangzhengquan
2020-06-18 5ee033b3f16717e3a437aa4cc207cb0d760ac93d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "usg_common.h"
#include <stdio.h>
#include <iostream>
#include "HCNetSDK.h"
using namespace std;
int main()
{
    int i=0;
    BYTE byIPID,byIPIDHigh;
    int iDevInfoIndex, iGroupNO, iIPCh;
    DWORD dwReturned = 0;
 
 
    char *libPath = "../hclib/";
    NET_DVR_LOCAL_SDK_PATH struComPath = {0};
    sprintf(struComPath.sPath, "%s", libPath); //HCNetSDKCom文件夹所在的路径
    NET_DVR_SetSDKInitCfg(NET_SDK_INIT_CFG_SDK_PATH, &struComPath);
    //---------------------------------------
    // 初始化
    NET_DVR_Init();
    //设置连接时间与重连时间
    NET_DVR_SetConnectTime(2000, 1);
    NET_DVR_SetReconnect(10000, true);
 
    //---------------------------------------
    // 注册设备
    LONG lUserID;
 
    //登录参数,包括设备地址、登录用户、密码等
    NET_DVR_USER_LOGIN_INFO struLoginInfo = {0};
    struLoginInfo.bUseAsynLogin = 0; //同步登录方式
    strcpy(struLoginInfo.sDeviceAddress, "192.168.20.11"); //设备IP地址
    struLoginInfo.wPort = 8000; //设备服务端口
    strcpy(struLoginInfo.sUserName, "admin"); //设备登录用户名
    strcpy(struLoginInfo.sPassword, "a1234567"); //设备登录密码
  
    //设备信息, 输出参数
    NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = {0};
 
    lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40);
    if (lUserID < 0)
    {
        printf("Login failed, error code: %d\n", NET_DVR_GetLastError());
        NET_DVR_Cleanup();
        return 0;
    }
    printf("模拟通道起始通道号: %d\n",struDeviceInfoV40.struDeviceV30.byStartChan); 
    printf("数字通道起始通道号: %d\n",struDeviceInfoV40.struDeviceV30.byStartDChan); 
    printf("The max number of analog channels: %d\n",struDeviceInfoV40.struDeviceV30.byChanNum); //模拟通道个数
    printf("The max number of IP channels: %d\n", struDeviceInfoV40.struDeviceV30.byIPChanNum + struDeviceInfoV40.struDeviceV30.byHighDChanNum * 256);//IP通道个数
 
    //获取IP通道参数信息
    NET_DVR_IPPARACFG_V40 IPAccessCfgV40;
    memset(&IPAccessCfgV40, 0, sizeof(NET_DVR_IPPARACFG));
    iGroupNO=0;
    if (!NET_DVR_GetDVRConfig(lUserID, NET_DVR_GET_IPPARACFG_V40, iGroupNO, &IPAccessCfgV40, sizeof(NET_DVR_IPPARACFG_V40), &dwReturned))
    {
        printf("NET_DVR_GET_IPPARACFG_V40 error, %d\n", NET_DVR_GetLastError());
        NET_DVR_Logout(lUserID);
        NET_DVR_Cleanup();
        return 0;
    }
    else
    {
        //包括模拟通道是否禁用(byAnalogChanEnable)、IP通道个数(dwDChanNum)、IP通道起始通道号(dwStartDChan)、
        
        printf("IP通道起始通道号: %d\n", IPAccessCfgV40.dwStartDChan);
        printf("IP通道个数: %d\n", IPAccessCfgV40.dwDChanNum);
        for (i=0;i<IPAccessCfgV40.dwDChanNum;i++)
        {
            switch(IPAccessCfgV40.struStreamMode[i].byGetStreamType)
            {
            case 0: //直接从设备取流
                if (IPAccessCfgV40.struStreamMode[i].uGetStream.struChanInfo.byEnable)
                {
                    byIPID=IPAccessCfgV40.struStreamMode[i].uGetStream.struChanInfo.byIPID;
                    byIPIDHigh=IPAccessCfgV40.struStreamMode[i].uGetStream.struChanInfo.byIPIDHigh;
                    iDevInfoIndex=byIPIDHigh*256 + byIPID-1-iGroupNO*64;
                    printf("IP channel no.%d is online, IP: %s\n", i+1, IPAccessCfgV40.struIPDevInfo[iDevInfoIndex].struIP.sIpV4);
                }
                break;
            case 1: //从流媒体取流
                if (IPAccessCfgV40.struStreamMode[i].uGetStream.struPUStream.struStreamMediaSvrCfg.byValid)
                {
                    printf("IP channel %d connected with the IP device by stream server.\n", i+1);
                    printf("IP of stream server: %s, IP of IP device: %s\n",IPAccessCfgV40.struStreamMode[i].uGetStream.\
                    struPUStream.struStreamMediaSvrCfg.struDevIP.sIpV4, IPAccessCfgV40.struStreamMode[i].uGetStream.\
                    struPUStream.struDevChanInfo.struIP.sIpV4);
                }
                break;
            default:
                break;
            }
        }
    }
    
    //配置IP通道5;
    iIPCh=34;
 
    //支持自定义协议
    NET_DVR_CUSTOM_PROTOCAL struCustomPro;
    if (!NET_DVR_GetDVRConfig(lUserID, NET_DVR_GET_CUSTOM_PRO_CFG, 1, &struCustomPro, sizeof(NET_DVR_CUSTOM_PROTOCAL), &dwReturned))
    //获取自定义协议1
    {
        printf("NET_DVR_GET_CUSTOM_PRO_CFG error, %d\n", NET_DVR_GetLastError());
        NET_DVR_Logout(lUserID);
        NET_DVR_Cleanup();
        return 0;
    }
    struCustomPro.dwEnabled=1;   //启用主码流
    struCustomPro.dwEnableSubStream=1; //启用子码流
    strcpy((char *)struCustomPro.sProtocalName,"Protocal_RTSP");  //自定义协议名称:Protocal_RTSP,最大16字节
    struCustomPro.byMainProType=1;    //主码流协议类型: 1- RTSP
    struCustomPro.byMainTransType=2;  //主码流传输协议: 0- Auto, 1- udp, 2- rtp over rtsp
    struCustomPro.wMainPort=554;     //主码流取流端口
    strcpy((char *)struCustomPro.sMainPath,"rtsp://192.168.1.65/h264/ch1/main/av_stream");//主码流取流URL
    struCustomPro.bySubProType=1;    //子码流协议类型: 1- RTSP
    struCustomPro.bySubTransType=2;  //子码流传输协议: 0- Auto, 1- udp, 2- rtp over rtsp
    struCustomPro.wSubPort=554;     //子码流取流端口
    strcpy((char *)struCustomPro.sSubPath,"rtsp://192.168.1.65/h264/ch1/sub/av_stream");//子码流取流URL
 
    if (!NET_DVR_SetDVRConfig(lUserID, NET_DVR_SET_CUSTOM_PRO_CFG, 1, &struCustomPro, sizeof(NET_DVR_CUSTOM_PROTOCAL)))
    //设置自定义协议1
    {
        printf("NET_DVR_SET_CUSTOM_PRO_CFG error, %d\n", NET_DVR_GetLastError());
        NET_DVR_Logout(lUserID);
        NET_DVR_Cleanup();
        return 0;
    }
    
    printf("Set the custom protocol: %s\n", "Protocal_RTSP");
    NET_DVR_IPC_PROTO_LIST m_struProtoList;
    if (!NET_DVR_GetIPCProtoList(lUserID, &m_struProtoList)) //获取设备支持的前端协议
    {
        printf("NET_DVR_GetIPCProtoList error, %d\n", NET_DVR_GetLastError());
        NET_DVR_Logout(lUserID);
        NET_DVR_Cleanup();
        return 0;
    }
 
    IPAccessCfgV40.struIPDevInfo[iIPCh].byEnable=1;     //启用
    for (i = 0; i<m_struProtoList.dwProtoNum; i++)
    {
        if(strcmp((char *)struCustomPro.sProtocalName,(char *)m_struProtoList.struProto[i].byDescribe)==0)
        {
            IPAccessCfgV40.struIPDevInfo[iIPCh].byProType=m_struProtoList.struProto[i].dwType; //选择自定义协议
            break;
        }
    }
    
    //IPAccessCfgV40.struIPDevInfo[iIPCh].byProType=0;  //厂家私有协议
    strcpy((char *)IPAccessCfgV40.struIPDevInfo[iIPCh].struIP.sIpV4,"192.168.20.11"); //前端IP设备的IP地址
    IPAccessCfgV40.struIPDevInfo[iIPCh].wDVRPort=8000;  //前端IP设备服务端口
    strcpy((char *)IPAccessCfgV40.struIPDevInfo[iIPCh].sUserName,"admin");  //前端IP设备登录用户名
    strcpy((char *)IPAccessCfgV40.struIPDevInfo[iIPCh].sPassword,"a1234567");  //前端IP设备登录密码
 
    IPAccessCfgV40.struStreamMode[iIPCh].byGetStreamType=0;
    IPAccessCfgV40.struStreamMode[iIPCh].uGetStream.struChanInfo.byChannel=1;
    IPAccessCfgV40.struStreamMode[iIPCh].uGetStream.struChanInfo.byIPID=(iIPCh)%256;
    IPAccessCfgV40.struStreamMode[iIPCh].uGetStream.struChanInfo.byIPIDHigh=(iIPCh)/256;
    
    //IP通道配置,包括添加、删除、修改IP通道等
    if (!NET_DVR_SetDVRConfig(lUserID, NET_DVR_SET_IPPARACFG_V40, iGroupNO, &IPAccessCfgV40, sizeof(NET_DVR_IPPARACFG_V40)))
    {
        printf("NET_DVR_SET_IPPARACFG_V40 error, %d\n", NET_DVR_GetLastError());
        NET_DVR_Logout(lUserID);
        NET_DVR_Cleanup();
        return 0;
    }
    else
    {
        printf("Set IP channel no.%d, IP: %s\n", iIPCh, IPAccessCfgV40.struIPDevInfo[iIPCh].struIP.sIpV4);
    }
    
    //注销用户
    NET_DVR_Logout(lUserID);
 
    //释放SDK资源
    NET_DVR_Cleanup();
    return 0;
}