houzhongjian
2024-10-30 a28ca3f36d0ace05966a8c0fac1e4b5fe921f882
提交 | 用户 | 时间
ce910c 1 package com.netsdk.demo.module;
H 2
3 import com.netsdk.lib.NetSDKLib;
4 import com.netsdk.lib.NetSDKLib.LLong;
5 import com.netsdk.lib.ToolKits;
6
7 import com.sun.jna.Pointer;
8 import com.sun.jna.ptr.IntByReference;
9
10 /**
11  * \if ENGLISH_LANG
12  * Talk Interface
13  * contains:start talk、stop talk and audio data callback implement class  
14  * \else
15  * 语音对讲接口实现
16  * 包含: 开始通话、结束通话、语音对讲的数据回调实现类
17  * \endif
18  */
19 public class TalkModule {
20
21     public static LLong m_hTalkHandle = new LLong(0); // 语音对讲句柄 
22     
23     private static boolean m_bRecordStatus    = false;             // 是否正在录音
24     
25     /**
26      * \if ENGLISH_LANG
27      * Start Talk
28      * \else
29      * 开始通话
30      * \endif
31      */
32     public static boolean startTalk(int transferType, int chn) {
33     
34         // 设置语音对讲编码格式
35         NetSDKLib.NETDEV_TALKDECODE_INFO talkEncode = new NetSDKLib.NETDEV_TALKDECODE_INFO();
36         talkEncode.encodeType = NetSDKLib.NET_TALK_CODING_TYPE.NET_TALK_PCM;
37         talkEncode.dwSampleRate = 8000;
38         talkEncode.nAudioBit = 16;
39         talkEncode.nPacketPeriod = 25;
40         talkEncode.write();
41         if(LoginModule.netsdk.CLIENT_SetDeviceMode(LoginModule.m_hLoginHandle, NetSDKLib.EM_USEDEV_MODE.NET_TALK_ENCODE_TYPE, talkEncode.getPointer())) {
42             System.out.println("Set Talk Encode Type Succeed!");
43         } else {
44             System.err.println("Set Talk Encode Type Failed!" + ToolKits.getErrorCodePrint());
45             return false;
46         }
47         
48         // 设置语音对讲喊话参数
49         NetSDKLib.NET_SPEAK_PARAM speak = new NetSDKLib.NET_SPEAK_PARAM();
50         speak.nMode = 0;
51         speak.bEnableWait = false;
52         speak.nSpeakerChannel = 0;
53         speak.write();
54         
55         if (LoginModule.netsdk.CLIENT_SetDeviceMode(LoginModule.m_hLoginHandle, NetSDKLib.EM_USEDEV_MODE.NET_TALK_SPEAK_PARAM, speak.getPointer())) {
56             System.out.println("Set Talk Speak Mode Succeed!");
57         } else {
58             System.err.println("Set Talk Speak Mode Failed!" + ToolKits.getErrorCodePrint());
59             return false;
60         }
61         
62         // 设置语音对讲是否为转发模式
63         NetSDKLib.NET_TALK_TRANSFER_PARAM talkTransfer = new NetSDKLib.NET_TALK_TRANSFER_PARAM();
64         talkTransfer.bTransfer = transferType;
65         talkTransfer.write();
66         if(LoginModule.netsdk.CLIENT_SetDeviceMode(LoginModule.m_hLoginHandle, NetSDKLib.EM_USEDEV_MODE.NET_TALK_TRANSFER_MODE, talkTransfer.getPointer())) {
67             System.out.println("Set Talk Transfer Mode Succeed!");
68         } else {
69             System.err.println("Set Talk Transfer Mode Failed!" + ToolKits.getErrorCodePrint());
70             return false;
71         }
72         
73         if (talkTransfer.bTransfer == 1) {  // 转发模式设置转发通道
74             
75             IntByReference nChn = new IntByReference(chn);
76             if(LoginModule.netsdk.CLIENT_SetDeviceMode(LoginModule.m_hLoginHandle, NetSDKLib.EM_USEDEV_MODE.NET_TALK_TALK_CHANNEL, nChn.getPointer())) {
77                 System.out.println("Set Talk Channel Succeed!");
78             } else {
79                 System.err.println("Set Talk Channel Failed!" + ToolKits.getErrorCodePrint());
80                 return false;
81             }
82         }
83         
84         
85         m_hTalkHandle = LoginModule.netsdk.CLIENT_StartTalkEx(LoginModule.m_hLoginHandle, AudioDataCB.getInstance(), null);
86         
87         if(m_hTalkHandle.longValue() == 0) {
88               System.err.println("Start Talk Failed!" + ToolKits.getErrorCodePrint());
89               return false;
90         } else {
91               System.out.println("Start Talk Success");
92             if(LoginModule.netsdk.CLIENT_RecordStart()){
93                 System.out.println("Start Record Success");
94                 m_bRecordStatus = true;
95             } else {
96                 System.err.println("Start Local Record Failed!" + ToolKits.getErrorCodePrint());
97                 stopTalk();
98                 return false;
99             }
100         }
101         
102         return true;
103     }
104     
105     /**
106      * \if ENGLISH_LANG
107      * Stop Talk
108      * \else
109      * 结束通话
110      * \endif
111      */
112     public static void stopTalk() {
113         if(m_hTalkHandle.longValue() == 0) {
114             return;
115         }
116         
117         if (m_bRecordStatus){
118             LoginModule.netsdk.CLIENT_RecordStop();
119             m_bRecordStatus = false;
120         }
121         
122         if(LoginModule.netsdk.CLIENT_StopTalkEx(m_hTalkHandle)) {
123             m_hTalkHandle.setValue(0);
124         }else {
125             System.err.println("Stop Talk Failed!" + ToolKits.getErrorCodePrint());
126         }
127     }
128     
129     /**
130      * \if ENGLISH_LANG
131      * Audio Data Callback
132      * \else
133      * 语音对讲的数据回调
134      * \endif
135      */
136     private static class AudioDataCB implements NetSDKLib.pfAudioDataCallBack {
137         
138         private AudioDataCB() {}
139         private static AudioDataCB audioCallBack = new AudioDataCB();
140         
141         public static AudioDataCB getInstance() {
142             return audioCallBack;
143         }
144         
145         public void invoke(LLong lTalkHandle, Pointer pDataBuf, int dwBufSize, byte byAudioFlag, Pointer dwUser){
146             
147             if(lTalkHandle.longValue() != m_hTalkHandle.longValue()) {
148                 return;
149             }
150             
151             if (byAudioFlag == 0) { // 将收到的本地PC端检测到的声卡数据发送给设备端
152                 
153                 LLong lSendSize = LoginModule.netsdk.CLIENT_TalkSendData(m_hTalkHandle, pDataBuf, dwBufSize);
154                 if(lSendSize.longValue() != (long)dwBufSize) {
155                     System.err.println("send incomplete" + lSendSize.longValue() + ":" + dwBufSize);
156                 } 
157             }else if (byAudioFlag == 1) { // 将收到的设备端发送过来的语音数据传给SDK解码播放
158                 LoginModule.netsdk.CLIENT_AudioDecEx(m_hTalkHandle, pDataBuf, dwBufSize);
159             }
160         }
161     }
162 }