提交 | 用户 | 时间
|
ce910c
|
1 |
package com.netsdk.demo.module; |
H |
2 |
|
|
3 |
import java.awt.Panel; |
|
4 |
|
|
5 |
import com.netsdk.lib.NetSDKLib.CFG_DVRIP_INFO; |
|
6 |
import com.netsdk.lib.NetSDKLib.LLong; |
|
7 |
import com.netsdk.lib.NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY; |
|
8 |
import com.netsdk.lib.NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY; |
|
9 |
import com.netsdk.lib.NetSDKLib.fServiceCallBack; |
|
10 |
import com.netsdk.lib.NetSDKLib; |
|
11 |
import com.netsdk.lib.ToolKits; |
|
12 |
|
|
13 |
import com.sun.jna.Native; |
|
14 |
import com.sun.jna.Pointer; |
|
15 |
import com.sun.jna.ptr.IntByReference; |
|
16 |
|
|
17 |
public class AutoRegisterModule { |
|
18 |
// 监听服务句柄 |
|
19 |
public static LLong mServerHandler = new LLong(0); |
|
20 |
|
|
21 |
// 设备信息 |
|
22 |
public static NetSDKLib.NET_DEVICEINFO_Ex m_stDeviceInfo = new NetSDKLib.NET_DEVICEINFO_Ex(); |
|
23 |
|
|
24 |
// 语音对讲句柄 |
|
25 |
public static LLong m_hTalkHandle = new LLong(0); |
|
26 |
private static boolean m_bRecordStatus = false; |
|
27 |
|
|
28 |
/** |
|
29 |
* 开启服务 |
|
30 |
* @param address 本地IP地址 |
|
31 |
* @param port 本地端口, 可以任意 |
|
32 |
* @param callback 回调函数 |
|
33 |
*/ |
|
34 |
public static boolean startServer(String address, int port, fServiceCallBack callback) { |
|
35 |
|
|
36 |
mServerHandler = LoginModule.netsdk.CLIENT_ListenServer(address, port, 1000, callback, null); |
|
37 |
if (0 == mServerHandler.longValue()) { |
|
38 |
System.err.println("Failed to start server." + ToolKits.getErrorCodePrint()); |
|
39 |
} else { |
|
40 |
System.out.printf("Start server, [Server address %s][Server port %d]\n", address, port); |
|
41 |
} |
|
42 |
return mServerHandler.longValue() != 0; |
|
43 |
} |
|
44 |
|
|
45 |
/** |
|
46 |
* 结束服务 |
|
47 |
*/ |
|
48 |
public static boolean stopServer() { |
|
49 |
boolean bRet = false; |
|
50 |
|
|
51 |
if(mServerHandler.longValue() != 0) { |
|
52 |
bRet = LoginModule.netsdk.CLIENT_StopListenServer(mServerHandler); |
|
53 |
mServerHandler.setValue(0); |
|
54 |
System.out.println("Stop server!"); |
|
55 |
} |
|
56 |
|
|
57 |
return bRet; |
|
58 |
} |
|
59 |
|
|
60 |
/** |
|
61 |
* 登录设备(主动注册登陆接口) |
|
62 |
* @param m_strIp 设备IP |
|
63 |
* @param m_nPort 设备端口号 |
|
64 |
* @param m_strUser 设备用户名 |
|
65 |
* @param m_strPassword 设备密码 |
|
66 |
* @param deviceId 设备ID |
|
67 |
* @return |
|
68 |
*/ |
|
69 |
public static LLong login(String m_strIp, int m_nPort, String m_strUser, String m_strPassword, String deviceIds) { |
|
70 |
Pointer deviceId = ToolKits.GetGBKStringToPointer(deviceIds); |
|
71 |
//IntByReference nError = new IntByReference(0); |
|
72 |
//入参 |
|
73 |
NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY pstInParam=new NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY(); |
|
74 |
pstInParam.nPort=m_nPort; |
|
75 |
pstInParam.szIP=m_strIp.getBytes(); |
|
76 |
pstInParam.szPassword=m_strPassword.getBytes(); |
|
77 |
pstInParam.szUserName=m_strUser.getBytes(); |
|
78 |
pstInParam.emSpecCap = 2;// 主动注册方式 |
|
79 |
pstInParam.pCapParam=deviceId; |
|
80 |
//出参 |
|
81 |
NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY pstOutParam=new NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY(); |
|
82 |
pstOutParam.stuDeviceInfo=m_stDeviceInfo; |
|
83 |
/*LLong m_hLoginHandle = LoginModule.netsdk.CLIENT_LoginEx2(m_strIp, m_nPort, m_strUser, m_strPassword, |
|
84 |
tcpSpecCap, deviceId, m_stDeviceInfo, nError);*/ |
|
85 |
LLong m_hLoginHandle=LoginModule.netsdk.CLIENT_LoginWithHighLevelSecurity(pstInParam, pstOutParam); |
|
86 |
return m_hLoginHandle; |
|
87 |
} |
|
88 |
|
|
89 |
/** |
|
90 |
* 登出设备 |
|
91 |
* @param m_hLoginHandle 登陆句柄 |
|
92 |
* @return |
|
93 |
*/ |
|
94 |
public static boolean logout(LLong m_hLoginHandle) { |
|
95 |
boolean bRet = false; |
|
96 |
if(m_hLoginHandle.longValue() != 0) { |
|
97 |
bRet = LoginModule.netsdk.CLIENT_Logout(m_hLoginHandle); |
|
98 |
m_hLoginHandle.setValue(0); |
|
99 |
} |
|
100 |
|
|
101 |
return bRet; |
|
102 |
} |
|
103 |
|
|
104 |
/** |
|
105 |
* 开始预览 |
|
106 |
* @param m_hLoginHandle 登陆句柄 |
|
107 |
* @param channel 通道号 |
|
108 |
* @param stream 码流类型 |
|
109 |
* @param realPlayWindow 拉流窗口 |
|
110 |
* @return |
|
111 |
*/ |
|
112 |
public static LLong startRealPlay(LLong m_hLoginHandle, int channel, int stream, Panel realPlayWindow) { |
|
113 |
LLong m_hPlayHandle = LoginModule.netsdk.CLIENT_RealPlayEx(m_hLoginHandle, channel, Native.getComponentPointer(realPlayWindow), stream); |
|
114 |
|
|
115 |
if(m_hPlayHandle.longValue() == 0) { |
|
116 |
System.err.println("Failed to start realplay." + ToolKits.getErrorCodePrint()); |
|
117 |
} else { |
|
118 |
System.out.println("Success to start realplay"); |
|
119 |
} |
|
120 |
|
|
121 |
return m_hPlayHandle; |
|
122 |
} |
|
123 |
|
|
124 |
/** |
|
125 |
* 停止预览 |
|
126 |
* @param m_hPlayHandle 实时预览句柄 |
|
127 |
* @return |
|
128 |
*/ |
|
129 |
public static boolean stopRealPlay(LLong m_hPlayHandle) { |
|
130 |
boolean bRet = false; |
|
131 |
if(m_hPlayHandle.longValue() != 0) { |
|
132 |
bRet = LoginModule.netsdk.CLIENT_StopRealPlayEx(m_hPlayHandle); |
|
133 |
m_hPlayHandle.setValue(0); |
|
134 |
} |
|
135 |
|
|
136 |
return bRet; |
|
137 |
} |
|
138 |
|
|
139 |
/** |
|
140 |
* 远程抓图 |
|
141 |
* @param m_hLoginHandle 登陆句柄 |
|
142 |
* @param chn 通道号 |
|
143 |
* @return |
|
144 |
*/ |
|
145 |
public static boolean snapPicture(LLong m_hLoginHandle, int chn) { |
|
146 |
// 发送抓图命令给前端设备,抓图的信息 |
|
147 |
NetSDKLib.SNAP_PARAMS msg = new NetSDKLib.SNAP_PARAMS(); |
|
148 |
msg.Channel = chn; // 抓图通道 |
|
149 |
msg.mode = 0; // 抓图模式 |
|
150 |
msg.Quality = 3; // 画质 |
|
151 |
msg.InterSnap = 0; // 定时抓图时间间隔 |
|
152 |
msg.CmdSerial = 0; // 请求序列号,有效值范围 0~65535,超过范围会被截断为 |
|
153 |
|
|
154 |
IntByReference reserved = new IntByReference(0); |
|
155 |
|
|
156 |
if (!LoginModule.netsdk.CLIENT_SnapPictureEx(m_hLoginHandle, msg, reserved)) { |
|
157 |
System.err.printf("SnapPictureEx Failed!" + ToolKits.getErrorCodePrint()); |
|
158 |
return false; |
|
159 |
} else { |
|
160 |
System.out.println("SnapPictureEx success"); |
|
161 |
} |
|
162 |
return true; |
|
163 |
} |
|
164 |
|
|
165 |
/** |
|
166 |
*设置抓图回调函数, 图片主要在m_SnapReceiveCB中返回 |
|
167 |
* @param m_SnapReceiveCB |
|
168 |
*/ |
|
169 |
public static void setSnapRevCallBack(NetSDKLib.fSnapRev m_SnapReceiveCB){ |
|
170 |
LoginModule.netsdk.CLIENT_SetSnapRevCallBack(m_SnapReceiveCB, null); |
|
171 |
} |
|
172 |
|
|
173 |
/** |
|
174 |
* 获取网络协议 |
|
175 |
* @param m_hLoginHandle 登录句柄 |
|
176 |
* @return |
|
177 |
*/ |
|
178 |
public static CFG_DVRIP_INFO getDVRIPConfig(LLong m_hLoginHandle) { |
|
179 |
CFG_DVRIP_INFO msg = new CFG_DVRIP_INFO(); |
|
180 |
|
|
181 |
if(!ToolKits.GetDevConfig(m_hLoginHandle, -1, NetSDKLib.CFG_CMD_DVRIP, msg)) { |
|
182 |
return null; |
|
183 |
} |
|
184 |
|
|
185 |
return msg; |
|
186 |
} |
|
187 |
|
|
188 |
/** |
|
189 |
* 网络协议配置 |
|
190 |
* @param m_hLoginHandle 登陆句柄 |
|
191 |
* @param enable 使能 |
|
192 |
* @param address 服务器地址 |
|
193 |
* @param nPort 服务器端口号 |
|
194 |
* @param deviceId 设备ID |
|
195 |
* @param info 获取到的网络协议配置 |
|
196 |
* @return |
|
197 |
*/ |
|
198 |
public static boolean setDVRIPConfig(LLong m_hLoginHandle, boolean enable, String address, int nPort, byte[] deviceId, CFG_DVRIP_INFO info) { |
|
199 |
CFG_DVRIP_INFO msg = info; |
|
200 |
// 主动注册配置个数 |
|
201 |
msg.nRegistersNum = 1; |
|
202 |
|
|
203 |
// 主动注册使能 |
|
204 |
msg.stuRegisters[0].bEnable = enable? 1:0; |
|
205 |
|
|
206 |
// 服务器个数 |
|
207 |
msg.stuRegisters[0].nServersNum = 1; |
|
208 |
|
|
209 |
// 服务器地址 |
|
210 |
ToolKits.StringToByteArray(address, msg.stuRegisters[0].stuServers[0].szAddress); |
|
211 |
|
|
212 |
// 服务器端口号 |
|
213 |
msg.stuRegisters[0].stuServers[0].nPort = nPort; |
|
214 |
|
|
215 |
// 设备ID |
|
216 |
ToolKits.ByteArrayToByteArray(deviceId, msg.stuRegisters[0].szDeviceID); |
|
217 |
|
|
218 |
return ToolKits.SetDevConfig(m_hLoginHandle, -1, NetSDKLib.CFG_CMD_DVRIP, msg); |
|
219 |
} |
|
220 |
|
|
221 |
/** |
|
222 |
* \if ENGLISH_LANG |
|
223 |
* Start Talk |
|
224 |
* \else |
|
225 |
* 开始通话 |
|
226 |
* \endif |
|
227 |
*/ |
|
228 |
public static boolean startTalk(LLong m_hLoginHandle) { |
|
229 |
|
|
230 |
// 设置语音对讲编码格式 |
|
231 |
NetSDKLib.NETDEV_TALKDECODE_INFO talkEncode = new NetSDKLib.NETDEV_TALKDECODE_INFO(); |
|
232 |
talkEncode.encodeType = NetSDKLib.NET_TALK_CODING_TYPE.NET_TALK_PCM; |
|
233 |
talkEncode.dwSampleRate = 8000; |
|
234 |
talkEncode.nAudioBit = 16; |
|
235 |
talkEncode.nPacketPeriod = 25; |
|
236 |
talkEncode.write(); |
|
237 |
if(LoginModule.netsdk.CLIENT_SetDeviceMode(m_hLoginHandle, NetSDKLib.EM_USEDEV_MODE.NET_TALK_ENCODE_TYPE, talkEncode.getPointer())) { |
|
238 |
System.out.println("Set Talk Encode Type Succeed!"); |
|
239 |
} else { |
|
240 |
System.err.println("Set Talk Encode Type Failed!" + ToolKits.getErrorCodePrint()); |
|
241 |
return false; |
|
242 |
} |
|
243 |
|
|
244 |
// 设置对讲模式 |
|
245 |
NetSDKLib.NET_SPEAK_PARAM speak = new NetSDKLib.NET_SPEAK_PARAM(); |
|
246 |
speak.nMode = 0; |
|
247 |
speak.bEnableWait = false; |
|
248 |
speak.nSpeakerChannel = 0; |
|
249 |
speak.write(); |
|
250 |
|
|
251 |
if (LoginModule.netsdk.CLIENT_SetDeviceMode(m_hLoginHandle, NetSDKLib.EM_USEDEV_MODE.NET_TALK_SPEAK_PARAM, speak.getPointer())) { |
|
252 |
System.out.println("Set Talk Speak Mode Succeed!"); |
|
253 |
} else { |
|
254 |
System.err.println("Set Talk Speak Mode Failed!" + ToolKits.getErrorCodePrint()); |
|
255 |
return false; |
|
256 |
} |
|
257 |
|
|
258 |
// 设置语音对讲是否为转发模式 |
|
259 |
NetSDKLib.NET_TALK_TRANSFER_PARAM talkTransfer = new NetSDKLib.NET_TALK_TRANSFER_PARAM(); |
|
260 |
talkTransfer.bTransfer = 0; // 是否开启语音对讲转发模式, 1-true; 0-false |
|
261 |
talkTransfer.write(); |
|
262 |
if(LoginModule.netsdk.CLIENT_SetDeviceMode(m_hLoginHandle, NetSDKLib.EM_USEDEV_MODE.NET_TALK_TRANSFER_MODE, talkTransfer.getPointer())) { |
|
263 |
System.out.println("Set Talk Transfer Mode Succeed!"); |
|
264 |
} else { |
|
265 |
System.err.println("Set Talk Transfer Mode Failed!" + ToolKits.getErrorCodePrint()); |
|
266 |
return false; |
|
267 |
} |
|
268 |
|
|
269 |
m_hTalkHandle = LoginModule.netsdk.CLIENT_StartTalkEx(m_hLoginHandle, AudioDataCB.getInstance(), null); |
|
270 |
|
|
271 |
if(m_hTalkHandle.longValue() == 0) { |
|
272 |
System.err.println("Start Talk Failed!" + ToolKits.getErrorCodePrint()); |
|
273 |
return false; |
|
274 |
} else { |
|
275 |
System.out.println("Start Talk Success"); |
|
276 |
if(LoginModule.netsdk.CLIENT_RecordStart()){ |
|
277 |
System.out.println("Start Record Success"); |
|
278 |
m_bRecordStatus = true; |
|
279 |
} else { |
|
280 |
System.err.println("Start Local Record Failed!" + ToolKits.getErrorCodePrint()); |
|
281 |
stopTalk(m_hTalkHandle); |
|
282 |
return false; |
|
283 |
} |
|
284 |
} |
|
285 |
|
|
286 |
return true; |
|
287 |
} |
|
288 |
|
|
289 |
/** |
|
290 |
* \if ENGLISH_LANG |
|
291 |
* Stop Talk |
|
292 |
* \else |
|
293 |
* 结束通话 |
|
294 |
* \endif |
|
295 |
*/ |
|
296 |
public static void stopTalk(LLong m_hTalkHandle) { |
|
297 |
if(m_hTalkHandle.longValue() == 0) { |
|
298 |
return; |
|
299 |
} |
|
300 |
|
|
301 |
if (m_bRecordStatus){ |
|
302 |
LoginModule.netsdk.CLIENT_RecordStop(); |
|
303 |
m_bRecordStatus = false; |
|
304 |
} |
|
305 |
|
|
306 |
if(!LoginModule.netsdk.CLIENT_StopTalkEx(m_hTalkHandle)) { |
|
307 |
System.err.println("Stop Talk Failed!" + ToolKits.getErrorCodePrint()); |
|
308 |
} else { |
|
309 |
m_hTalkHandle.setValue(0); |
|
310 |
} |
|
311 |
} |
|
312 |
|
|
313 |
/** |
|
314 |
* \if ENGLISH_LANG |
|
315 |
* Audio Data Callback |
|
316 |
* \else |
|
317 |
* 语音对讲的数据回调 |
|
318 |
* \endif |
|
319 |
*/ |
|
320 |
private static class AudioDataCB implements NetSDKLib.pfAudioDataCallBack { |
|
321 |
|
|
322 |
private AudioDataCB() {} |
|
323 |
private static AudioDataCB audioCallBack = new AudioDataCB(); |
|
324 |
|
|
325 |
public static AudioDataCB getInstance() { |
|
326 |
return audioCallBack; |
|
327 |
} |
|
328 |
|
|
329 |
public void invoke(LLong lTalkHandle, Pointer pDataBuf, int dwBufSize, byte byAudioFlag, Pointer dwUser){ |
|
330 |
|
|
331 |
if(lTalkHandle.longValue() != m_hTalkHandle.longValue()) { |
|
332 |
return; |
|
333 |
} |
|
334 |
|
|
335 |
if (byAudioFlag == 0) { // 将收到的本地PC端检测到的声卡数据发送给设备端 |
|
336 |
|
|
337 |
LLong lSendSize = LoginModule.netsdk.CLIENT_TalkSendData(m_hTalkHandle, pDataBuf, dwBufSize); |
|
338 |
if(lSendSize.longValue() != (long)dwBufSize) { |
|
339 |
System.err.println("send incomplete" + lSendSize.longValue() + ":" + dwBufSize); |
|
340 |
} |
|
341 |
}else if (byAudioFlag == 1) { // 将收到的设备端发送过来的语音数据传给SDK解码播放 |
|
342 |
LoginModule.netsdk.CLIENT_AudioDecEx(m_hTalkHandle, pDataBuf, dwBufSize); |
|
343 |
} |
|
344 |
} |
|
345 |
} |
|
346 |
} |