| | |
| | | package com.hdl.hdllinphonesdk; |
| | | |
| | | import static java.lang.Thread.sleep; |
| | | |
| | | import android.content.ComponentName; |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.content.ServiceConnection; |
| | | import android.os.IBinder; |
| | | import android.util.Log; |
| | | import android.view.TextureView; |
| | | import android.widget.Toast; |
| | | |
| | | import com.hdl.hdllinphonesdk.activity.HDLLinphoneIntercomActivity; |
| | | import com.hdl.hdllinphonesdk.activity.HDLLinphoneReverseCallActivity; |
| | | import com.hdl.hdllinphonesdk.callback.OnHDLLinphoneCallListener; |
| | | import com.hdl.hdllinphonesdk.callback.OnLPOpenDoorCallBack; |
| | | import com.hdl.hdllinphonesdk.core.callback.PhoneCallback; |
| | |
| | | import org.linphone.core.AuthInfo; |
| | | import org.linphone.core.Call; |
| | | import org.linphone.core.CallParams; |
| | | import org.linphone.core.ChatMessage; |
| | | import org.linphone.core.ChatRoom; |
| | | 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 java.io.FileOutputStream; |
| | | import java.io.InputStream; |
| | | |
| | | import static java.lang.Thread.sleep; |
| | | |
| | | /** |
| | | * Created by jlchen on 2021/8/4. |
| | |
| | | public static final String KEY_TITLE_NAME = "lpTitleName"; |
| | | public static final String KEY_SIP_ACCOUNT = "lpSipAccount"; |
| | | public static final String HDLLinphoneKitNAME = "HDLLinphoneKit"; |
| | | public static final String KEY_CALL_ALL_REJECTION = "callAllRejection"; |
| | | private volatile static HDLLinphoneKit instance; |
| | | private Context viewContext; |
| | | private ServiceWaitThread mServiceWaitThread; |
| | | private String mUsername, mPassword, mServerIP; |
| | | // private TextureView mRenderingView, mPreviewView; |
| | | private boolean isAutoJumpCallView;//是否自动跳转呼叫页面 |
| | | private String intercomeType; |
| | | |
| | | private HDLLinphoneKit() { |
| | | |
| | |
| | | @Override |
| | | public void incomingCall(Call linphoneCall) { |
| | | super.incomingCall(linphoneCall); |
| | | Log.d("HDLLinphoneKit","------incomingCall------"); |
| | | Log.d("HDLLinphoneKit", "------incomingCall------"); |
| | | if (linphoneCall != null) { |
| | | String callUsername = linphoneCall.getRemoteAddress().getUsername(); |
| | | HDLLog.I("来电号码:" + callUsername); |
| | |
| | | @Override |
| | | public void outgoingInit() { |
| | | super.outgoingInit(); |
| | | Log.d("HDLLinphoneKit","------outgoingInit------"); |
| | | Log.d("HDLLinphoneKit", "------outgoingInit------"); |
| | | } |
| | | |
| | | @Override |
| | | public void callConnected() { |
| | | super.callConnected(); |
| | | // 视频通话默认免提,语音通话默认非免提 |
| | | Log.d("HDLLinphoneKit","------callConnected------"); |
| | | Log.d("HDLLinphoneKit", "------callConnected------"); |
| | | toggleSpeaker(true); |
| | | } |
| | | |
| | | @Override |
| | | public void callEnd() { |
| | | super.callEnd(); |
| | | Log.d("HDLLinphoneKit","------callEnd------"); |
| | | Log.d("HDLLinphoneKit", "------callEnd------"); |
| | | // context.sendBroadcast(new Intent(HDLLinphoneIntercomActivity.RECEIVE_FINISH_VIDEO_ACTIVITY)); |
| | | } |
| | | }); |
| | |
| | | } |
| | | } |
| | | |
| | | private HDLLinphoneService mService; |
| | | private final ServiceConnection connection = new ServiceConnection() { |
| | | |
| | | @Override |
| | | public void onServiceConnected(ComponentName name, IBinder ibinder) { |
| | | HDLLinphoneService.LinphoneBinder binder = (HDLLinphoneService.LinphoneBinder) ibinder; |
| | | mService = binder.getService(); |
| | | //初始化 |
| | | mService.initCore(); |
| | | } |
| | | |
| | | @Override |
| | | public void onServiceDisconnected(ComponentName name) { |
| | | mService = null; |
| | | } |
| | | |
| | | }; |
| | | |
| | | /** |
| | | * 开启服务 |
| | | * |
| | |
| | | if (!HDLLinphoneService.isReady()) { |
| | | Intent intent = new Intent(Intent.ACTION_MAIN); |
| | | intent.setClass(context, HDLLinphoneService.class); |
| | | context.startService(intent); |
| | | // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| | | // context.startForegroundService(intent);//解决android8.0以上无法启动服务的问题 |
| | | // } else { |
| | | // context.startService(intent); |
| | | // } |
| | | context.bindService(intent, connection, Context.BIND_AUTO_CREATE); |
| | | |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | |
| | | mUsername = username; |
| | | mPassword = password; |
| | | mServerIP = serverIP; |
| | | intercomeType = inter_type; |
| | | login(inter_type); |
| | | } |
| | | |
| | | public String getIntercomeType() { |
| | | return intercomeType; |
| | | } |
| | | |
| | | /** |
| | |
| | | * 登录 SIP 服务器 |
| | | */ |
| | | private void loginToServer(String inter_type) { |
| | | Log.e("==========", "loginToServer: "); |
| | | |
| | | try { |
| | | if (mUsername == null || mPassword == null || mServerIP == null) { |
| | | throw new RuntimeException("The sip account is not configured."); |
| | |
| | | Core mLinphoneCore = HDLLinphoneService.getCore(); |
| | | if (mLinphoneCore == null) return; |
| | | |
| | | AuthInfo authInfo = Factory.instance().createAuthInfo(name, null, password, null, null, host); |
| | | AuthInfo authInfo = Factory.instance().createAuthInfo(name, name, password, null, null, host); |
| | | AccountCreator mAccountCreator = mLinphoneCore.createAccountCreator(null); |
| | | mAccountCreator.setUsername(name); |
| | | mAccountCreator.setDomain(host); |
| | | mAccountCreator.setPassword(password); |
| | | mAccountCreator.setTransport(TransportType.Udp); |
| | | mAccountCreator.setTransport(TransportType.Tcp); |
| | | ProxyConfig prxCfg = mAccountCreator.createProxyConfig(); |
| | | |
| | | prxCfg.enableQualityReporting(false); |
| | |
| | | prxCfg.setQualityReportingCollector(null); |
| | | prxCfg.setQualityReportingInterval(0); |
| | | prxCfg.enableRegister(true); |
| | | prxCfg.setServerAddr("sip:139.159.157.75:46000;transport=udp"); |
| | | //全视通需要设置固定代理服务器ip |
| | | prxCfg.setServerAddr("sip:" + domain + ";transport=udp"); |
| | | |
| | | mLinphoneCore.addProxyConfig(prxCfg);//添加代理配置。如果启用了注册,这将开始在代理上注册。 |
| | | mLinphoneCore.addAuthInfo(authInfo);//添加认证信息到,该信息片段将在所有需要的SIP事务中使用身份验证 |
| | |
| | | |
| | | if (addressToCall != null) { |
| | | call = core.inviteAddressWithParams(addressToCall, params); |
| | | // 设置外放 |
| | | for (AudioDevice audioDevice : HDLLinphoneService.getCore().getAudioDevices()) { |
| | | if (audioDevice.getType() == AudioDevice.Type.Speaker) { |
| | | call.setOutputAudioDevice(audioDevice); |
| | | if (call != null) { |
| | | // 设置外放 |
| | | for (AudioDevice audioDevice : HDLLinphoneService.getCore().getAudioDevices()) { |
| | | if (audioDevice.getType() == AudioDevice.Type.Speaker) { |
| | | call.setOutputAudioDevice(audioDevice); |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | params.enableLowBandwidth(false); |
| | | params.setAudioBandwidthLimit(0); // disable limitation |
| | | currentCall.acceptWithParams(params); |
| | | Log.d("HDLLinphoneKit","---acceptCall= "); |
| | | Log.d("HDLLinphoneKit", "---acceptCall= "); |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | |
| | | } |
| | | } |
| | | |
| | | public void sendSipMessage(String sipAccount, String sipAddress, String message) { |
| | | try { |
| | | Core mLinphoneCore = HDLLinphoneService.getCore(); |
| | | if (mLinphoneCore == null) return; |
| | | |
| | | //对方的sip地址,sipAddress:"sip:对方账号@服务器地址" |
| | | Address address = mLinphoneCore.interpretUrl("sip:" + sipAccount + "@" + sipAddress); |
| | | |
| | | //建立对话 |
| | | ChatRoom chatRoom = mLinphoneCore.getChatRoom(address); |
| | | |
| | | //创建消息 |
| | | ChatMessage chatMessage = chatRoom.createMessageFromUtf8(message); |
| | | |
| | | //发送消息 |
| | | chatMessage.send(); |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 是否静音 |
| | | * |
| | |
| | | * 设置外放设备 |
| | | */ |
| | | private void routeAudioToSpeakerHelper(boolean speakerOn) { |
| | | Log.d("HDLLinphoneKit","设置外放设备[Audio Manager] Routing audio to " + (speakerOn ? "speaker" : "earpiece")); |
| | | Log.d("HDLLinphoneKit", "设置外放设备[Audio Manager] Routing audio to " + (speakerOn ? "speaker" : "earpiece")); |
| | | try { |
| | | if (HDLLinphoneService.getCore() == null) return; |
| | | if (HDLLinphoneService.getCore().getCallsNb() == 0) return; |
| | |
| | | if (currentCall == null) return; |
| | | |
| | | for (AudioDevice audioDevice : HDLLinphoneService.getCore().getAudioDevices()) { |
| | | Log.d("HDLLinphoneKit","audioDevice.getType()= " + audioDevice.getType()); |
| | | Log.d("HDLLinphoneKit", "audioDevice.getType()= " + audioDevice.getType()); |
| | | } |
| | | |
| | | for (AudioDevice audioDevice : HDLLinphoneService.getCore().getAudioDevices()) { |
| | | Log.d("HDLLinphoneKit","设置外放设备speakerOn=" + speakerOn + " audioDevice.getType()=" + audioDevice.getType()); |
| | | Log.d("HDLLinphoneKit", "设置外放设备speakerOn=" + speakerOn + " audioDevice.getType()=" + audioDevice.getType()); |
| | | if (speakerOn && audioDevice.getType() == AudioDevice.Type.Speaker) { |
| | | currentCall.setOutputAudioDevice(audioDevice); |
| | | Log.d("HDLLinphoneKit","AudioDevice.Type.Speaker"); |
| | | Log.d("HDLLinphoneKit", "AudioDevice.Type.Speaker"); |
| | | return; |
| | | } else if (!speakerOn && audioDevice.getType() == AudioDevice.Type.Earpiece) { |
| | | currentCall.setOutputAudioDevice(audioDevice); |