package com.hdl.hdllinphonesdk; import android.content.Context; import android.content.Intent; import android.util.Log; import android.view.SurfaceView; import android.view.TextureView; import android.widget.Toast; import com.hdl.hdllinphonesdk.activity.HDLLinphoneIntercomActivity; import com.hdl.hdllinphonesdk.callback.OnHDLLinphoneCallListener; import com.hdl.hdllinphonesdk.callback.OnLPOpenDoorCallBack; import com.hdl.hdllinphonesdk.core.callback.PhoneCallback; import com.hdl.hdllinphonesdk.core.callback.RegistrationCallback; import com.hdl.hdllinphonesdk.core.linphone.LinphoneUtils; import com.hdl.hdllinphonesdk.core.service.HDLLinphoneService; import com.hdl.hdllinphonesdk.utils.HDLLog; import org.linphone.core.Address; import org.linphone.core.Call; import org.linphone.core.CallParams; import org.linphone.core.CallStats; import org.linphone.core.Core; import org.linphone.core.CoreException; import org.linphone.core.ProxyConfig; import static java.lang.Thread.sleep; /** * Created by jlchen on 2021/8/4. */ public class HDLLinphoneKit { public static final String KEY_TITLE_NAME = "lpTitleName"; public static final String HDLLinphoneKitNAME = "HDLLinphoneKit"; private volatile static HDLLinphoneKit instance; private Context viewContext; private ServiceWaitThread mServiceWaitThread; private String mUsername, mPassword, mServerIP; private TextureView mRenderingView, mPreviewView; private boolean isAutoJumpCallView;//是否自动跳转呼叫页面 private HDLLinphoneKit() { } public static synchronized HDLLinphoneKit getInstance() { if (instance == null) { synchronized (HDLLinphoneKit.class) { if (instance == null) { instance = new HDLLinphoneKit(); } } } return instance; } /** * 初始化Linphone */ public void initLinphone(Context context) { viewContext = context.getApplicationContext(); if (!HDLLinphoneService.isReady()) { startService(context); addCallback(new RegistrationCallback() { @Override public void registrationOk() { super.registrationOk(); Log.e(HDLLinphoneKitNAME, "registrationOk: "); callBackRegistrationOk(viewContext); } @Override public void registrationFailed() { super.registrationFailed(); Log.e(HDLLinphoneKitNAME, "registrationFailed: "); // Toast.makeText(LoginActivity.this, "登录失败!", Toast.LENGTH_SHORT).show(); } }, null); } else { } } /** * 注册成功后,注册呼叫监听 */ void callBackRegistrationOk(Context context) { addCallback(null, new PhoneCallback() { @Override public void incomingCall(Call linphoneCall) { super.incomingCall(linphoneCall); String callUsername = linphoneCall.getRemoteAddress().getUsername(); HDLLog.E("来电号码:" + callUsername); if (isAutoJumpCallView) { //是否需要自动打开呼叫页面 gotoHDLLinphoneIntercomActivity(); } } @Override public void outgoingInit() { super.outgoingInit(); } @Override public void callConnected() { super.callConnected(); // 视频通话默认免提,语音通话默认非免提 toggleSpeaker(true); // 所有通话默认非静音 toggleMicro(false); } @Override public void callEnd() { super.callEnd(); context.sendBroadcast(new Intent(HDLLinphoneIntercomActivity.RECEIVE_FINISH_VIDEO_ACTIVITY)); } }); } /** * 跳转打开呼叫页面 */ public void gotoHDLLinphoneIntercomActivity() { Intent intent = new Intent(viewContext, HDLLinphoneIntercomActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); viewContext.startActivity(intent); } /** * showToast */ private void showToast(String text) { Toast.makeText(viewContext, text, Toast.LENGTH_SHORT).show(); } /** * 开启服务 * * @param context 上下文 */ public void startService(Context context) { if (!HDLLinphoneService.isReady()) { Intent intent = new Intent(Intent.ACTION_MAIN); intent.setClass(context, HDLLinphoneService.class); context.startService(intent); } } // /** // * 设置 sip 账户信息 // * // * @param username sip 账户 // * @param password 密码 // * @param serverIP sip 服务器 // */ // public void setAccount(String username, String password, String serverIP) { // mUsername = username; // mPassword = password; // mServerIP = serverIP; // } /** * 设置 sip 账户信息 * * @param username sip 账户 * @param password 密码 * @param serverIP sip 服务器 */ public void setAccountAndLogin(String username, String password, String serverIP) { mUsername = username; mPassword = password; mServerIP = serverIP; login(); } /** * 添加注册状态、通话状态回调 * * @param phoneCallback 通话回调 * @param registrationCallback 注册状态回调 */ public void addCallback(RegistrationCallback registrationCallback, PhoneCallback phoneCallback) { if (HDLLinphoneService.isReady()) { HDLLinphoneService.addRegistrationCallback(registrationCallback); HDLLinphoneService.addPhoneCallback(phoneCallback); } else { mServiceWaitThread = new ServiceWaitThread(registrationCallback, phoneCallback); mServiceWaitThread.start(); } } /** * 登录到 SIP 服务器 */ public void login() { new Thread(new Runnable() { @Override public void run() { while (!HDLLinphoneService.isReady()) { try { sleep(80); } catch (InterruptedException e) { e.printStackTrace(); } } loginToServer(); } }).start(); } /** * 退出清空账号 */ public void logout() { try { Core core = HDLLinphoneService.getCore(); if (core != null) { ProxyConfig[] configs = core.getProxyConfigList(); for (ProxyConfig config : configs) { if (config != null) { config.edit(); config.enableRegister(false); config.done(); } } core.clearAllAuthInfo(); core.clearProxyConfig(); } } catch (Exception e) { e.printStackTrace(); } } /** * 呼叫指定号码 * * @param num 呼叫号码 */ public void callTo(String num, boolean isVideoCall) { LinphoneUtils.getInstance().callTo(num, isVideoCall); } /** * 接听来电 */ public void acceptCall() { LinphoneUtils.getInstance().acceptCall(); } // /** // * 接听来电 // */ // public void acceptCallWithVideo(boolean enableVideo) { // LinphoneUtils.getInstance().acceptCallWithVideo(enableVideo); // } /** * 挂断当前通话 */ public void hangUp() { LinphoneUtils.getInstance().hangUp(); } /** * 切换静音 * * @param isMicMuted 是否静音 */ public void toggleMicro(boolean isMicMuted) { LinphoneUtils.getInstance().toggleMicro(isMicMuted); } /** * 切换免提 * * @param isSpeakerEnabled 是否免提 */ public void toggleSpeaker(boolean isSpeakerEnabled) { LinphoneUtils.getInstance().toggleSpeaker(isSpeakerEnabled); } private class ServiceWaitThread extends Thread { private PhoneCallback mPhoneCallback; private RegistrationCallback mRegistrationCallback; ServiceWaitThread(RegistrationCallback registrationCallback, PhoneCallback phoneCallback) { mRegistrationCallback = registrationCallback; mPhoneCallback = phoneCallback; } @Override public void run() { super.run(); while (!HDLLinphoneService.isReady()) { try { sleep(80); } catch (InterruptedException e) { throw new RuntimeException("waiting thread sleep() has been interrupted"); } } HDLLinphoneService.addPhoneCallback(mPhoneCallback); HDLLinphoneService.addRegistrationCallback(mRegistrationCallback); mServiceWaitThread = null; } } /** * 登录 SIP 服务器 */ private void loginToServer() { try { if (mUsername == null || mPassword == null || mServerIP == null) { throw new RuntimeException("The sip account is not configured."); } LinphoneUtils.getInstance().registerUserAuth(mUsername, mPassword, mServerIP); } catch (CoreException e) { e.printStackTrace(); } } // public static boolean getVideoEnabled() { // CallParams remoteParams = HDLLinphoneService.getCore().getCurrentCall().getRemoteParams(); // return remoteParams != null && remoteParams.videoEnabled(); // } /** * 设置 SurfaceView * * @param renderingView 远程 TextureView * @param previewView 本地 TextureView */ public void setAndroidVideoWindow(final TextureView renderingView, final TextureView previewView) { mRenderingView = renderingView; mPreviewView = previewView; setVideoWindow(renderingView); setPreviewWindow(previewView); } /** * onResume */ public void onResume() { // if (mRenderingView != null) { // ((TextureView) mRenderingView).onResume(); // } // if (mAndroidVideoWindow != null) { // synchronized (mAndroidVideoWindow) { // LinphoneManager.getLc().setNativeVideoWindowId(mAndroidVideoWindow); // } // } } /** * onPause */ public void onPause() { // if (mAndroidVideoWindow != null) { // synchronized (mAndroidVideoWindow) { // LinphoneManager.getLc().setNativeVideoWindowId(null); // } // } // // if (mRenderingView != null) { // ((GLSurfaceView) mRenderingView).onPause(); // } } /** * onDestroy */ public void onDestroy() { mPreviewView = null; mRenderingView = null; } // private void fixZOrder(SurfaceView rendering, SurfaceView preview) { // rendering.setZOrderOnTop(false); // preview.setZOrderOnTop(true); // preview.setZOrderMediaOverlay(true); // Needed to be able to display control layout over // } private void setVideoWindow(Object o) { Core linphoneCore = HDLLinphoneService.getCore(); if (linphoneCore != null) { linphoneCore.setNativeVideoWindowId(o); } } private void removeVideoWindow() { Core linphoneCore = HDLLinphoneService.getCore(); if (linphoneCore != null) { linphoneCore.setNativeVideoWindowId(null); } } private void setPreviewWindow(Object o) { Core linphoneCore = HDLLinphoneService.getCore(); if (linphoneCore != null) { linphoneCore.setNativePreviewWindowId(o); } } private void removePreviewWindow() { Core linphoneCore = HDLLinphoneService.getCore(); if (linphoneCore != null) { linphoneCore.setNativePreviewWindowId(null); } } // /** // * 获取 LinphoneCore // * // * @return LinphoneCore // */ // public Core getLC() { // return HDLLinphoneService.getCore(); // } /** * 获取当前通话状态 */ public Call.State getCallState() { return HDLLinphoneService.getInstance().getCurrentCallState(); } /** * 获取当前通话状态是否为来电中 */ public boolean isIncomingReceivedCallState() { return HDLLinphoneService.getInstance().getCurrentCallState() == Call.State.IncomingReceived; } /** * 是否自动跳转呼叫页面方案 */ public boolean isAutoJumpCallView() { return isAutoJumpCallView; } public void setAutoJumpCallView(boolean autoJumpCallView) { isAutoJumpCallView = autoJumpCallView; } /** * 开门回调接口 */ private OnLPOpenDoorCallBack openOpenDoorCallBack; /** * setOpenOpenDoorCallBack */ public void setOpenOpenDoorCallBack(OnLPOpenDoorCallBack openOpenDoorCallBack) { this.openOpenDoorCallBack = openOpenDoorCallBack; } /** * 通知开门成功 */ public void onOpenSuccess() { if (openOpenDoorCallBack != null) { openOpenDoorCallBack.onOpenSuccess(); } } /** * 通知开门失败 */ public void onOpenError(String mes) { if (openOpenDoorCallBack != null) { openOpenDoorCallBack.onOpenError(mes); } } /** * 呼叫页面相关操作回调接口 */ private OnHDLLinphoneCallListener mOnHDLLinphoneCallListener; public OnHDLLinphoneCallListener getOnHDLLinphoneCallListener() { return mOnHDLLinphoneCallListener; } public void setOnHDLLinphoneCallListener(OnHDLLinphoneCallListener onHDLLinphoneCallListener) { mOnHDLLinphoneCallListener = onHDLLinphoneCallListener; } }