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(); } } }