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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package com.netsdk.demo.frame;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
 
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
 
import com.sun.jna.Pointer;
 
import com.netsdk.common.*;
import com.netsdk.demo.module.*;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.NetSDKLib.LLong;
import com.netsdk.lib.ToolKits;
 
class RealPlayFrame extends JFrame{
    private static final long serialVersionUID = 1L;
    
    private Vector<String> chnlist = new Vector<String>(); 
 
    private boolean isrealplayOne = false;
    private boolean isrealplayTwo = false;
 
    // 设备断线通知回调
    private static DisConnect disConnect       = new DisConnect(); 
    
    // 网络连接恢复
    private static HaveReConnect haveReConnect = new HaveReConnect(); 
    
    // 预览句柄
    public static LLong m_hPlayHandleOne = new LLong(0);
    
    public static LLong m_hPlayHandleTwo = new LLong(0);
    
    // 获取界面窗口
    private static JFrame frame = new JFrame();   
    
    public RealPlayFrame() {
        setTitle(Res.string().getRealplay());
        setLayout(new BorderLayout());
        pack();
        setSize(800, 560);
        setResizable(false);
        setLocationRelativeTo(null);
        LoginModule.init(disConnect, haveReConnect);   // 打开工程,初始化
        
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        } 
     
        loginPanel = new LoginPanel();
        realPanel = new JPanel();
        
        add(loginPanel, BorderLayout.NORTH);
        add(realPanel, BorderLayout.CENTER);
        
        // 预览面板
        realPanelOne = new RealPanelOne();
        realPanelTwo = new RealPanelTwo();
        
        realPanel.setLayout(new GridLayout(1,  2));
        realPanel.add(realPanelOne);
        realPanel.add(realPanelTwo);
        
        loginPanel.addLoginBtnActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {               
                if(loginPanel.checkLoginText()) {
                    if(login()) {
                        frame = ToolKits.getFrame(e);
                        frame.setTitle(Res.string().getRealplay() + " : " + Res.string().getOnline());
                    }
                }
            }
        });
        
        loginPanel.addLogoutBtnActionListener(new ActionListener() {        
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setTitle(Res.string().getRealplay());
                logout();    
            }
        });
        
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                RealPlayModule.stopRealPlay(m_hPlayHandleOne);
                RealPlayModule.stopRealPlay(m_hPlayHandleTwo);
                LoginModule.logout();
                LoginModule.cleanup();   // 关闭工程,释放资源
                dispose();    
                
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        FunctionList demo = new FunctionList();
                        demo.setVisible(true);
                    }
                });
            }
        });
    }
    
    /////////////////面板///////////////////
    // 设备断线回调: 通过 CLIENT_Init 设置该回调函数,当设备出现断线时,SDK会调用该函数
    private static class DisConnect implements NetSDKLib.fDisConnect {
        public void invoke(LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
            System.out.printf("Device[%s] Port[%d] DisConnect!\n", pchDVRIP, nDVRPort);
            // 断线提示
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    frame.setTitle(Res.string().getRealplay() + " : " + Res.string().getDisConnectReconnecting());
                }
            });
        }
    }
    
    // 网络连接恢复,设备重连成功回调
    // 通过 CLIENT_SetAutoReconnect 设置该回调函数,当已断线的设备重连成功时,SDK会调用该函数
    private static class HaveReConnect implements NetSDKLib.fHaveReConnect {
        public void invoke(LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
            System.out.printf("ReConnect Device[%s] Port[%d]\n", pchDVRIP, nDVRPort);
            
            // 重连提示
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    frame.setTitle(Res.string().getRealplay() + " : " + Res.string().getOnline());
                }
            });
        }
    }
    
    // 登录
    public boolean login() {
        if(LoginModule.login(loginPanel.ipTextArea.getText(), 
                        Integer.parseInt(loginPanel.portTextArea.getText()), 
                        loginPanel.nameTextArea.getText(), 
                        new String(loginPanel.passwordTextArea.getPassword()))) {
    
            loginPanel.setButtonEnable(true);
            setButtonEnable(true);
            
            for(int i = 1; i < LoginModule.m_stDeviceInfo.byChanNum + 1; i++) {
                chnlist.add(Res.string().getChannel() + " " + String.valueOf(i));
            }
            
            // 登陆成功,将通道添加到控件
            chnComboBoxOne.setModel(new DefaultComboBoxModel(chnlist));
            chnComboBoxTwo.setModel(new DefaultComboBoxModel(chnlist));
            
        } else {
            JOptionPane.showMessageDialog(null, Res.string().getLoginFailed() + ", " + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }
    
    // 登出
    public void logout() {
        RealPlayModule.stopRealPlay(m_hPlayHandleOne);
        RealPlayModule.stopRealPlay(m_hPlayHandleTwo);
        LoginModule.logout();
        
        loginPanel.setButtonEnable(false);
        setButtonEnable(false);
        realPlayWindowOne.repaint();    
        realPlayWindowTwo.repaint();   
 
        isrealplayOne = false;
        realplayBtnOne.setText(Res.string().getStartRealPlay());
        
        isrealplayTwo = false;
        realplayBtnTwo.setText(Res.string().getStartRealPlay());
        
        for(int i = 0; i < LoginModule.m_stDeviceInfo.byChanNum; i++) {
            chnlist.clear();
        }
        
        chnComboBoxOne.setModel(new DefaultComboBoxModel());
        chnComboBoxTwo.setModel(new DefaultComboBoxModel());
    }
    
    /*
     * 预览界面通道、码流设置  以及抓图面板
     */
    private class RealPanelOne extends JPanel {
        private static final long serialVersionUID = 1L;
        
        public RealPanelOne() {
            BorderEx.set(this, Res.string().getRealplay(), 2);
            setLayout(new BorderLayout());
            
            channelPanelOne = new Panel();
            realplayPanelOne = new JPanel();
            
            add(channelPanelOne, BorderLayout.NORTH);
            add(realplayPanelOne, BorderLayout.CENTER);
            
            /************ 预览面板 **************/
            realplayPanelOne.setLayout(new BorderLayout());
            realplayPanelOne.setBorder(new EmptyBorder(5, 5, 5, 5));
            realPlayWindowOne = new Panel();
            realPlayWindowOne.setBackground(Color.GRAY);
            realplayPanelOne.add(realPlayWindowOne, BorderLayout.CENTER);
            
            /************ 通道、码流面板 **************/
            chnlabelOne = new JLabel(Res.string().getChn());
            chnComboBoxOne = new JComboBox();                 
 
            streamLabelOne = new JLabel(Res.string().getStreamType());
            String[] stream = {Res.string().getMasterStream(), Res.string().getSubStream()};
            streamComboBoxOne = new JComboBox(stream);     
            
            realplayBtnOne = new JButton(Res.string().getStartRealPlay());
            
            channelPanelOne.setLayout(new FlowLayout());            
            channelPanelOne.add(chnlabelOne);
            channelPanelOne.add(chnComboBoxOne);
            channelPanelOne.add(streamLabelOne);
            channelPanelOne.add(streamComboBoxOne);
            channelPanelOne.add(realplayBtnOne);
            
            chnComboBoxOne.setPreferredSize(new Dimension(80, 20)); 
            streamComboBoxOne.setPreferredSize(new Dimension(95, 20)); 
            realplayBtnOne.setPreferredSize(new Dimension(115, 20)); 
            
            realPlayWindowOne.setEnabled(false);
             chnComboBoxOne.setEnabled(false);
            streamComboBoxOne.setEnabled(false);
            realplayBtnOne.setEnabled(false);
            
            realplayBtnOne.addActionListener(new ActionListener() {            
                @Override
                public void actionPerformed(ActionEvent e) {
                    realplayOne();
                }
            });
        }
    }
    
    // 预览
    public void realplayOne() {
        if(!isrealplayOne) {
            m_hPlayHandleOne = RealPlayModule.startRealPlay(chnComboBoxOne.getSelectedIndex(), 
                    streamComboBoxOne.getSelectedIndex()==0? 0:3,
                    realPlayWindowOne);
            if(m_hPlayHandleOne.longValue() != 0) {
                realPlayWindowOne.repaint();
                isrealplayOne = true;
                chnComboBoxOne.setEnabled(false);
                streamComboBoxOne.setEnabled(false);
                realplayBtnOne.setText(Res.string().getStopRealPlay());
            } else {
                JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
            }
        } else {
            RealPlayModule.stopRealPlay(m_hPlayHandleOne);
            realPlayWindowOne.repaint();
            isrealplayOne = false;
            chnComboBoxOne.setEnabled(true);
            streamComboBoxOne.setEnabled(true);
            realplayBtnOne.setText(Res.string().getStartRealPlay());
        }    
    }
    
    
    private class RealPanelTwo extends JPanel {
        private static final long serialVersionUID = 1L;
        
        public RealPanelTwo() {
            BorderEx.set(this, Res.string().getRealplay(), 2);
            setLayout(new BorderLayout());
            
            channelPanelTwo = new Panel();
            realplayPanelTwo = new JPanel();
            
            add(channelPanelTwo, BorderLayout.NORTH);
            add(realplayPanelTwo, BorderLayout.CENTER);
            
            /************ 预览面板 **************/
            realplayPanelTwo.setLayout(new BorderLayout());
            realplayPanelTwo.setBorder(new EmptyBorder(5, 5, 5, 5));
            realPlayWindowTwo = new Panel();
            realPlayWindowTwo.setBackground(Color.GRAY);
            realplayPanelTwo.add(realPlayWindowTwo, BorderLayout.CENTER);
            
            /************ 通道、码流面板 **************/
            chnlabelTwo = new JLabel(Res.string().getChn());
            chnComboBoxTwo = new JComboBox();                
 
            streamLabelTwo = new JLabel(Res.string().getStreamType());
            String[] stream = {Res.string().getMasterStream(), Res.string().getSubStream()};
            streamComboBoxTwo = new JComboBox(stream);    
            
            realplayBtnTwo = new JButton(Res.string().getStartRealPlay());
            
            channelPanelTwo.setLayout(new FlowLayout());            
            channelPanelTwo.add(chnlabelTwo);
            channelPanelTwo.add(chnComboBoxTwo);
            channelPanelTwo.add(streamLabelTwo);
            channelPanelTwo.add(streamComboBoxTwo);
            channelPanelTwo.add(realplayBtnTwo);
            
            chnComboBoxTwo.setPreferredSize(new Dimension(80, 20)); 
            streamComboBoxTwo.setPreferredSize(new Dimension(95, 20)); 
            realplayBtnTwo.setPreferredSize(new Dimension(115, 20)); 
            
            realPlayWindowTwo.setEnabled(false);
             chnComboBoxTwo.setEnabled(false);
            streamComboBoxTwo.setEnabled(false);
            realplayBtnTwo.setEnabled(false);
            
            realplayBtnTwo.addActionListener(new ActionListener() {            
                @Override
                public void actionPerformed(ActionEvent e) {
                    realplayTwo();
                }
            });
        }
    }
    
    // 预览
    public void realplayTwo() {
        if(!isrealplayTwo) {
            m_hPlayHandleTwo = RealPlayModule.startRealPlay(chnComboBoxTwo.getSelectedIndex(), 
                    streamComboBoxTwo.getSelectedIndex()==0? 0:3,
                    realPlayWindowTwo);
            if(m_hPlayHandleTwo.longValue() != 0) {
                realPlayWindowTwo.repaint();
                isrealplayTwo = true;
                chnComboBoxTwo.setEnabled(false);
                streamComboBoxTwo.setEnabled(false);
                realplayBtnTwo.setText(Res.string().getStopRealPlay());
            } else {
                JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
            }
        } else {
            RealPlayModule.stopRealPlay(m_hPlayHandleTwo);
            realPlayWindowTwo.repaint();
            isrealplayTwo = false;
            chnComboBoxTwo.setEnabled(true);
            streamComboBoxTwo.setEnabled(true);
            realplayBtnTwo.setText(Res.string().getStartRealPlay());
        }    
    }
    private void setButtonEnable(boolean bln) {
        realPlayWindowOne.setEnabled(bln);
        chnComboBoxOne.setEnabled(bln);
        streamComboBoxOne.setEnabled(bln);
        realplayBtnOne.setEnabled(bln);
        
        realPlayWindowTwo.setEnabled(bln);
        chnComboBoxTwo.setEnabled(bln);
        streamComboBoxTwo.setEnabled(bln);
        realplayBtnTwo.setEnabled(bln);
    }
    
    /*
     * 登录
     */
    private LoginPanel loginPanel;    
    
    private JPanel realPanel;
    
    /*
     * 预览
     */
    private RealPanelOne realPanelOne;
    private JPanel realplayPanelOne;
    private Panel realPlayWindowOne;
    private Panel channelPanelOne;
    
    private JLabel chnlabelOne;
    private JComboBox chnComboBoxOne;    
    private JLabel streamLabelOne;
    private JComboBox streamComboBoxOne;
    private JButton realplayBtnOne;
 
    // 
    private RealPanelTwo realPanelTwo;
    private JPanel realplayPanelTwo;
    private Panel realPlayWindowTwo;
    private Panel channelPanelTwo;
    
    private JLabel chnlabelTwo;
    private JComboBox chnComboBoxTwo;    
    private JLabel streamLabelTwo;
    private JComboBox streamComboBoxTwo;
    private JButton realplayBtnTwo;
}
 
public class RealPlay {
    public static void main(String[] args) {    
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                RealPlayFrame demo = new RealPlayFrame();    
                demo.setVisible(true);
            }
        });        
    }
}