Jay
2024-11-01 03e8aca3ad6201c0d74e00d4c8d7367cdaaa54f9
提交 | 用户 | 时间
ce910c 1 package com.netsdk.demo.frame;
H 2
3 import java.io.File;
4
5 import com.netsdk.common.CaseMenu;
6 import com.netsdk.lib.NetSDKLib;
7 import com.netsdk.lib.NetSDKLib.LLong;
8 import com.netsdk.lib.NetSDKLib.NET_DEVICEINFO_Ex;
9 import java.util.Scanner;
10 import com.sun.jna.Pointer;
11 import com.sun.jna.ptr.IntByReference;
12
13 public class RealplayEx {
14     public static final NetSDKLib netSdk = NetSDKLib.NETSDK_INSTANCE;
15     
16     // 登陆句柄
17     private LLong loginHandle = new LLong(0);
18     
19     // 预览预览句柄
20     private static LLong lRealHandle = new LLong(0);
21     
22     
23     // 设备信息扩展
24     private NET_DEVICEINFO_Ex deviceInfo = new NET_DEVICEINFO_Ex();
25     
26     //private NET_TIME m_startTime = new NET_TIME(); // 开始时间
27     //private NET_TIME m_stopTime = new NET_TIME(); // 结束时间
28     
29     public void InitTest(){
30         // 初始化SDK库
31         netSdk.CLIENT_Init(DisConnectCallBack.getInstance(), null);
32
33         // 设置断线重连成功回调函数
34         netSdk.CLIENT_SetAutoReconnect(HaveReConnectCallBack.getInstance(), null);
35         
36         //打开日志,可选
37         NetSDKLib.LOG_SET_PRINT_INFO setLog = new NetSDKLib.LOG_SET_PRINT_INFO();
38         String logPath = new File(".").getAbsoluteFile().getParent() + File.separator + "sdk_log" + File.separator + "sdk.log";
39         setLog.bSetFilePath = 1;
40         System.arraycopy(logPath.getBytes(), 0, setLog.szLogFilePath, 0, logPath.getBytes().length);
41         setLog.bSetPrintStrategy = 1;
42         setLog.nPrintStrategy = 0;
43         if (!netSdk.CLIENT_LogOpen(setLog)){
44             System.err.println("Open SDK Log Failed!!!");
45         }        
46
47     }
48     
49     public void Login(String m_strIp,int m_nPort,String m_strUser,String m_strPassword ){
50
51          // 登陆设备
52         int nSpecCap = NetSDKLib.EM_LOGIN_SPAC_CAP_TYPE.EM_LOGIN_SPEC_CAP_TCP;    // TCP登入
53         IntByReference nError = new IntByReference(0);
54         loginHandle = netSdk.CLIENT_LoginEx2(m_strIp, m_nPort, m_strUser,
55                 m_strPassword ,nSpecCap, null, deviceInfo, nError);
56         if (loginHandle.longValue() != 0) {
57             System.out.printf("Login Device[%s] Success!\n", m_strIp);             
58         }
59         else {
60             System.err.printf("Login Device[%s] Fail.Error[0x%x]\n", m_strIp, netSdk.CLIENT_GetLastError()); 
61             LoginOut();
62         }
63     }
64     public void realplay(){
65         lRealHandle= netSdk.CLIENT_RealPlayEx(loginHandle, 0, null, 0);
66         if(lRealHandle.longValue()!=0){
67             System.out.println("realplay success");
68             netSdk.CLIENT_SetRealDataCallBackEx(lRealHandle, CbfRealDataCallBackEx.getInstance(),null, 31);
69         }
70     }
71     
72     public void StopRealPlay(){
73         if(netSdk.CLIENT_StopRealPlayEx(lRealHandle)){
74             System.out.println("StopRealPlay success");
75         }
76     }
77     public void LoginOut(){
78         System.out.println("End Test");
79         
80         if( loginHandle.longValue() != 0)
81         {
82             netSdk.CLIENT_Logout(loginHandle);
83         }
84         System.out.println("See You...");
85         
86         netSdk.CLIENT_Cleanup();
87         System.exit(0);
88     }
89     
90      public void RunTest(){
91             CaseMenu menu=new CaseMenu();
92             menu.addItem((new CaseMenu.Item(this , "realplay" , "realplay")));
93             menu.addItem((new CaseMenu.Item(this , "StopRealPlay" , "StopRealPlay")));
94             menu.run();
95         }
96      /**
97      * 设备断线回调
98      */
99     private static class DisConnectCallBack implements NetSDKLib.fDisConnect {
100
101         private DisConnectCallBack() {
102         }
103
104         private static class CallBackHolder {
105             private static DisConnectCallBack instance = new DisConnectCallBack();
106         }
107
108         public static DisConnectCallBack getInstance() {
109             return CallBackHolder.instance;
110         }
111
112         public void invoke(NetSDKLib.LLong lLoginID, String pchDVRIP, int nDVRPort, Pointer dwUser) {
113             System.out.printf("Device[%s] Port[%d] DisConnect!\n", pchDVRIP, nDVRPort);
114         }
115     }
116     
117     /**
118      * 设备重连回调
119      */
120     private static class HaveReConnectCallBack implements NetSDKLib.fHaveReConnect {
121         private HaveReConnectCallBack() {
122         }
123
124         private static class CallBackHolder {
125             private static HaveReConnectCallBack instance = new HaveReConnectCallBack();
126         }
127
128         public static HaveReConnectCallBack getInstance() {
129             return CallBackHolder.instance;
130         }
131
132         public void invoke(NetSDKLib.LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
133             System.out.printf("ReConnect Device[%s] Port[%d]\n", pchDVRIP, nDVRPort);
134
135         }
136     }
137     
138     /**
139      * 实时预览数据回调函数--扩展(pBuffer内存由SDK内部申请释放)
140      */
141     private static class CbfRealDataCallBackEx implements NetSDKLib.fRealDataCallBackEx {
142         private CbfRealDataCallBackEx() {
143         }
144
145         private static class CallBackHolder {
146             private static CbfRealDataCallBackEx instance = new CbfRealDataCallBackEx();
147         }
148
149         public static CbfRealDataCallBackEx getInstance() {
150             return CallBackHolder.instance;
151         }
152
153         @Override
154         public void invoke(LLong lRealHandle, int dwDataType, Pointer pBuffer,
155                 int dwBufSize, int param, Pointer dwUser) {
156             int bInput=0;
157             if(0 != lRealHandle.longValue())
158             {
159                 switch(dwDataType) {
160                 case 0:
161                     System.out.println("码流大小为" + dwBufSize + "\n" + "码流类型为原始音视频混合数据");                    
162                     break;
163                 case 1:
164                     //标准视频数据
165                     
166                     break;
167                 case 2:
168                     //yuv 数据
169                     
170                     break;
171                 case 3:
172                     //pcm 音频数据
173                     
174                     break;
175                 case 4:
176                     //原始音频数据
177                     
178                     break;
179                 default:
180                     break;
181                 }    
182             }
183         }
184     }
185     
186     public static void main(String []args){
187         RealplayEx XM=new RealplayEx();
188         String ip = "172.23.12.231";
189         int port = 37777;
190         String username = "admin";
191         String password = "admin123";
192         Scanner scanner = new Scanner(System.in);
193         String defaultConfig = "ip:%s,port:%d,username:%s,password:%s,需要修改吗?(y/n)";
194         defaultConfig = String.format(defaultConfig, ip, port, username, password);
195         System.out.println(defaultConfig);
196         String answer = "";
197         do {
198             answer = scanner.nextLine();
199             if ("y".equalsIgnoreCase(answer) || "yes".equalsIgnoreCase(answer)) {
200                 System.out.println("please input ip");
201                 ip = scanner.nextLine().trim();
202                 System.out.println("please input port:");
203                 port = Integer.parseInt(scanner.nextLine());
204                 System.out.println("please input username:");
205                 username = scanner.nextLine().trim();
206                 System.out.println("please input password:");
207                 password = scanner.nextLine().trim();
208                 break;
209             } else if ("n".equalsIgnoreCase(answer) || "no".equalsIgnoreCase(answer)) {
210                 break;
211             }
212             System.out.println("please input the right word.y/yes/n/no,try again.");
213         } while (!(answer.equalsIgnoreCase("y")
214                 || answer.equalsIgnoreCase("yes")
215                 || answer.equalsIgnoreCase("no")
216                 || answer.equalsIgnoreCase("n")));
217
218         XM.InitTest();
219         XM.Login(ip,port,username,password);
220         XM.RunTest();
221         XM.LoginOut();
222     }
223 }