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
package com.netsdk.demo.module;
 
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.NetSDKLib.LLong;
import com.netsdk.lib.ToolKits;
 
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
 
/**
 * \if ENGLISH_LANG
 * Talk Interface
 * contains:start talk、stop talk and audio data callback implement class  
 * \else
 * 语音对讲接口实现
 * 包含: 开始通话、结束通话、语音对讲的数据回调实现类
 * \endif
 */
public class TalkModule {
 
    public static LLong m_hTalkHandle = new LLong(0); // 语音对讲句柄 
    
    private static boolean m_bRecordStatus    = false;             // 是否正在录音
    
    /**
     * \if ENGLISH_LANG
     * Start Talk
     * \else
     * 开始通话
     * \endif
     */
    public static boolean startTalk(int transferType, int chn) {
    
        // 设置语音对讲编码格式
        NetSDKLib.NETDEV_TALKDECODE_INFO talkEncode = new NetSDKLib.NETDEV_TALKDECODE_INFO();
        talkEncode.encodeType = NetSDKLib.NET_TALK_CODING_TYPE.NET_TALK_PCM;
        talkEncode.dwSampleRate = 8000;
        talkEncode.nAudioBit = 16;
        talkEncode.nPacketPeriod = 25;
        talkEncode.write();
        if(LoginModule.netsdk.CLIENT_SetDeviceMode(LoginModule.m_hLoginHandle, NetSDKLib.EM_USEDEV_MODE.NET_TALK_ENCODE_TYPE, talkEncode.getPointer())) {
            System.out.println("Set Talk Encode Type Succeed!");
        } else {
            System.err.println("Set Talk Encode Type Failed!" + ToolKits.getErrorCodePrint());
            return false;
        }
        
        // 设置语音对讲喊话参数
        NetSDKLib.NET_SPEAK_PARAM speak = new NetSDKLib.NET_SPEAK_PARAM();
        speak.nMode = 0;
        speak.bEnableWait = false;
        speak.nSpeakerChannel = 0;
        speak.write();
        
        if (LoginModule.netsdk.CLIENT_SetDeviceMode(LoginModule.m_hLoginHandle, NetSDKLib.EM_USEDEV_MODE.NET_TALK_SPEAK_PARAM, speak.getPointer())) {
            System.out.println("Set Talk Speak Mode Succeed!");
        } else {
            System.err.println("Set Talk Speak Mode Failed!" + ToolKits.getErrorCodePrint());
            return false;
        }
        
        // 设置语音对讲是否为转发模式
        NetSDKLib.NET_TALK_TRANSFER_PARAM talkTransfer = new NetSDKLib.NET_TALK_TRANSFER_PARAM();
        talkTransfer.bTransfer = transferType;
        talkTransfer.write();
        if(LoginModule.netsdk.CLIENT_SetDeviceMode(LoginModule.m_hLoginHandle, NetSDKLib.EM_USEDEV_MODE.NET_TALK_TRANSFER_MODE, talkTransfer.getPointer())) {
            System.out.println("Set Talk Transfer Mode Succeed!");
        } else {
            System.err.println("Set Talk Transfer Mode Failed!" + ToolKits.getErrorCodePrint());
            return false;
        }
        
        if (talkTransfer.bTransfer == 1) {  // 转发模式设置转发通道
            
            IntByReference nChn = new IntByReference(chn);
            if(LoginModule.netsdk.CLIENT_SetDeviceMode(LoginModule.m_hLoginHandle, NetSDKLib.EM_USEDEV_MODE.NET_TALK_TALK_CHANNEL, nChn.getPointer())) {
                System.out.println("Set Talk Channel Succeed!");
            } else {
                System.err.println("Set Talk Channel Failed!" + ToolKits.getErrorCodePrint());
                return false;
            }
        }
        
        
        m_hTalkHandle = LoginModule.netsdk.CLIENT_StartTalkEx(LoginModule.m_hLoginHandle, AudioDataCB.getInstance(), null);
        
        if(m_hTalkHandle.longValue() == 0) {
              System.err.println("Start Talk Failed!" + ToolKits.getErrorCodePrint());
              return false;
        } else {
              System.out.println("Start Talk Success");
            if(LoginModule.netsdk.CLIENT_RecordStart()){
                System.out.println("Start Record Success");
                m_bRecordStatus = true;
            } else {
                System.err.println("Start Local Record Failed!" + ToolKits.getErrorCodePrint());
                stopTalk();
                return false;
            }
        }
        
        return true;
    }
    
    /**
     * \if ENGLISH_LANG
     * Stop Talk
     * \else
     * 结束通话
     * \endif
     */
    public static void stopTalk() {
        if(m_hTalkHandle.longValue() == 0) {
            return;
        }
        
        if (m_bRecordStatus){
            LoginModule.netsdk.CLIENT_RecordStop();
            m_bRecordStatus = false;
        }
        
        if(LoginModule.netsdk.CLIENT_StopTalkEx(m_hTalkHandle)) {
            m_hTalkHandle.setValue(0);
        }else {
            System.err.println("Stop Talk Failed!" + ToolKits.getErrorCodePrint());
        }
    }
    
    /**
     * \if ENGLISH_LANG
     * Audio Data Callback
     * \else
     * 语音对讲的数据回调
     * \endif
     */
    private static class AudioDataCB implements NetSDKLib.pfAudioDataCallBack {
        
        private AudioDataCB() {}
        private static AudioDataCB audioCallBack = new AudioDataCB();
        
        public static AudioDataCB getInstance() {
            return audioCallBack;
        }
        
        public void invoke(LLong lTalkHandle, Pointer pDataBuf, int dwBufSize, byte byAudioFlag, Pointer dwUser){
            
            if(lTalkHandle.longValue() != m_hTalkHandle.longValue()) {
                return;
            }
            
            if (byAudioFlag == 0) { // 将收到的本地PC端检测到的声卡数据发送给设备端
                
                LLong lSendSize = LoginModule.netsdk.CLIENT_TalkSendData(m_hTalkHandle, pDataBuf, dwBufSize);
                if(lSendSize.longValue() != (long)dwBufSize) {
                    System.err.println("send incomplete" + lSendSize.longValue() + ":" + dwBufSize);
                } 
            }else if (byAudioFlag == 1) { // 将收到的设备端发送过来的语音数据传给SDK解码播放
                LoginModule.netsdk.CLIENT_AudioDecEx(m_hTalkHandle, pDataBuf, dwBufSize);
            }
        }
    }
}