JLChen
2021-08-19 f798871425ebfbb7dc4fdf17b22943e4ea2a73e2
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
package com.hdl.hdllinphonesdk.core.linphone;
 
import android.content.Context;
 
import com.hdl.hdllinphonesdk.core.service.HDLLinphoneService;
 
import org.linphone.core.AccountCreator;
import org.linphone.core.Address;
import org.linphone.core.AudioDevice;
import org.linphone.core.AuthInfo;
import org.linphone.core.Call;
import org.linphone.core.CallParams;
import org.linphone.core.Core;
import org.linphone.core.CoreException;
import org.linphone.core.Factory;
import org.linphone.core.ProxyConfig;
import org.linphone.core.TransportType;
import org.linphone.core.tools.Log;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
 
/**
 * 语音通话工具类
 */
 
public class LinphoneUtils {
 
    private static final String TAG = "LinphoneUtils";
    private static volatile LinphoneUtils sLinphoneUtils;
 
    public static LinphoneUtils getInstance() {
        if (sLinphoneUtils == null) {
            synchronized (LinphoneUtils.class) {
                if (sLinphoneUtils == null) {
                    sLinphoneUtils = new LinphoneUtils();
                }
            }
        }
        return sLinphoneUtils;
    }
 
    private LinphoneUtils() {
 
    }
 
    /**
     * 注册到服务器
     *
     * @param name
     * @param password
     * @param host
     * @throws CoreException
     */
    public void registerUserAuth(String name, String password, String host) throws CoreException {
        Core mLinphoneCore = HDLLinphoneService.getCore();
        if(mLinphoneCore == null) return;
 
        AuthInfo authInfo = Factory.instance().createAuthInfo(name, null, password,
                null, null, host);
 
        AccountCreator mAccountCreator = mLinphoneCore.createAccountCreator(null);
        mAccountCreator.setUsername(name);
        mAccountCreator.setDomain(host);
        mAccountCreator.setPassword(password);
        mAccountCreator.setTransport(TransportType.Udp);
        ProxyConfig prxCfg = mAccountCreator.createProxyConfig();
 
        prxCfg.enableQualityReporting(false);
        prxCfg.setQualityReportingCollector(null);
        prxCfg.setQualityReportingInterval(0);
        prxCfg.enableRegister(true);
////        mLinphoneCore.enableDnsSrv(true);
        mLinphoneCore.addProxyConfig(prxCfg);
        mLinphoneCore.addAuthInfo(authInfo);
        mLinphoneCore.setDefaultProxyConfig(prxCfg);
    }
 
    public Call startSingleCallingTo(PhoneBean bean, boolean isVideoCall) {
        Core mLinphoneCore = HDLLinphoneService.getCore();
        if(mLinphoneCore == null) return null;
        Address address;
        Call call = null;
        try {
            address = mLinphoneCore.interpretUrl(bean.getUserName() + "@" + bean.getHost());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        address.setDisplayName(bean.getDisplayName());
        CallParams params = mLinphoneCore.createCallParams(null);
        if (isVideoCall) {
            params.enableVideo(true);
            params.enableLowBandwidth(false);
        } else {
            params.enableVideo(false);
        }
        try {
            call = mLinphoneCore.inviteAddressWithParams(address, params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return call;
    }
 
    /**
     * 呼叫
     */
    public Call startSingleCallingTo(String userName, boolean isVideoCall) {
        Core mLinphoneCore = HDLLinphoneService.getCore();
        if(mLinphoneCore == null) return null;
 
        Call call = null;
        Core core = HDLLinphoneService.getCore();
        if(core != null) {
            Address addressToCall = core.interpretUrl(userName);
            CallParams params = core.createCallParams(null);
 
            if (isVideoCall) {
                params.enableVideo(true);
                params.enableLowBandwidth(false);
            } else {
                params.enableVideo(false);
            }
 
            if (addressToCall != null) {
                call = core.inviteAddressWithParams(addressToCall, params);
            }
        }
        return call;
    }
 
    /**
     * 呼叫指定号码
     *
     * @param num 呼叫号码
     */
    public void callTo(String num, boolean isVideoCall) {
        if (!HDLLinphoneService.isReady() || !HDLLinphoneService.getCoreIsNotNull()) {
            return;
        }
        if (!num.equals("")) {
           startSingleCallingTo(num, isVideoCall);
        }
    }
 
    /**
     * 接听来电
     */
    public void acceptCall() {
        try {
            if(HDLLinphoneService.getCore() == null) return;
            Call currentCall = HDLLinphoneService.getCore().getCurrentCall();
            CallParams params = HDLLinphoneService.getCore().createCallParams(currentCall);
            params.enableVideo(true);
            currentCall.acceptWithParams(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 接听来电
     */
    public void acceptCallWithVideo(boolean enableVideo) {
        try {
            if(HDLLinphoneService.getCore() == null) return;
            Call currentCall = HDLLinphoneService.getCore().getCurrentCall();
            CallParams params = HDLLinphoneService.getCore().createCallParams(currentCall);
            params.enableVideo(enableVideo);
            currentCall.acceptWithParams(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 挂断电话
     */
    public void hangUp() {
        Core mLinphoneCore = HDLLinphoneService.getCore();
        if(mLinphoneCore == null) return;
        Call currentCall = mLinphoneCore.getCurrentCall();
        if (currentCall != null) {
//            mLinphoneCore.terminateCall(currentCall);
            mLinphoneCore.terminateAllCalls();
        } else if (mLinphoneCore.isInConference()) {
            mLinphoneCore.terminateConference();
        } else {
            mLinphoneCore.terminateAllCalls();
        }
    }
 
    /**
     * 是否静音
     *
     * @param isMicMuted
     */
    public void toggleMicro(boolean isMicMuted) {
        Core mLinphoneCore = HDLLinphoneService.getCore();
        if(mLinphoneCore == null) return;
        mLinphoneCore.enableMic(!isMicMuted);
    }
 
    /**
     * 是否外放
     *
     * @param isSpeakerEnabled
     */
    public void toggleSpeaker(boolean isSpeakerEnabled) {
        routeAudioToSpeakerHelper(isSpeakerEnabled);
    }
 
    private void routeAudioToSpeakerHelper(boolean speakerOn) {
        Log.w("[Audio Manager] Routing audio to " + (speakerOn ? "speaker" : "earpiece"));
        try {
            if(HDLLinphoneService.getCore() == null) return;
            if (HDLLinphoneService.getCore().getCallsNb() == 0) return;
            Call currentCall = HDLLinphoneService.getCore().getCurrentCall();
            if (currentCall == null) currentCall = HDLLinphoneService.getCore().getCalls()[0];
            if (currentCall == null) return;
 
            for (AudioDevice audioDevice : HDLLinphoneService.getCore().getAudioDevices()) {
                if (speakerOn && audioDevice.getType() == AudioDevice.Type.Speaker) {
                    currentCall.setOutputAudioDevice(audioDevice);
                    return;
                } else if (!speakerOn && audioDevice.getType() == AudioDevice.Type.Earpiece) {
                    currentCall.setOutputAudioDevice(audioDevice);
                    return;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static void copyIfNotExist(Context context, int resourceId, String target) throws IOException {
        File fileToCopy = new File(target);
        if (!fileToCopy.exists()) {
            copyFromPackage(context, resourceId, fileToCopy.getName());
        }
    }
 
    public static void copyFromPackage(Context context, int resourceId, String target) throws IOException {
        FileOutputStream outputStream = context.openFileOutput(target, 0);
        InputStream inputStream = context.getResources().openRawResource(resourceId);
        int readByte;
        byte[] buff = new byte[8048];
        while ((readByte = inputStream.read(buff)) != -1) {
            outputStream.write(buff, 0, readByte);
        }
        outputStream.flush();
        outputStream.close();
        inputStream.close();
    }
 
    public static void sleep(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
 
}