From 66db2477a4855f5452c45f5e5baeae2d8c3e8c05 Mon Sep 17 00:00:00 2001 From: wxr <wxr@hdlchina.com.cn> Date: 星期四, 14 十一月 2024 14:15:55 +0800 Subject: [PATCH] 增加sip message发送 --- HDLLinphoneSDKDemoNew/HDLLinphoneSDK/src/main/java/com/hdl/hdllinphonesdk/HDLLinphoneKit.java | 283 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 257 insertions(+), 26 deletions(-) diff --git a/HDLLinphoneSDKDemoNew/HDLLinphoneSDK/src/main/java/com/hdl/hdllinphonesdk/HDLLinphoneKit.java b/HDLLinphoneSDKDemoNew/HDLLinphoneSDK/src/main/java/com/hdl/hdllinphonesdk/HDLLinphoneKit.java index 37ef91b..9c240f4 100644 --- a/HDLLinphoneSDKDemoNew/HDLLinphoneSDK/src/main/java/com/hdl/hdllinphonesdk/HDLLinphoneKit.java +++ b/HDLLinphoneSDKDemoNew/HDLLinphoneSDK/src/main/java/com/hdl/hdllinphonesdk/HDLLinphoneKit.java @@ -1,7 +1,13 @@ package com.hdl.hdllinphonesdk; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.ServiceConnection; +import android.media.AudioManager; +import android.media.MediaPlayer; +import android.os.Build; +import android.os.IBinder; import android.util.Log; import android.view.TextureView; import android.widget.Toast; @@ -19,11 +25,17 @@ import org.linphone.core.Address; import org.linphone.core.AudioDevice; import org.linphone.core.AuthInfo; +import org.linphone.core.Buffer; import org.linphone.core.Call; import org.linphone.core.CallParams; +import org.linphone.core.ChatMessage; +import org.linphone.core.ChatMessageListener; +import org.linphone.core.ChatRoom; +import org.linphone.core.Content; import org.linphone.core.Core; import org.linphone.core.CoreException; import org.linphone.core.Factory; +import org.linphone.core.ParticipantImdnState; import org.linphone.core.ProxyConfig; import org.linphone.core.TransportType; @@ -63,12 +75,91 @@ private volatile static HDLLinphoneKit instance; private Context viewContext; private ServiceWaitThread mServiceWaitThread; - private String mUsername, mPassword, mServerIP; + private String mUsername, mPassword, mServerIP,mTransportType; // private TextureView mRenderingView, mPreviewView; private boolean isAutoJumpCallView;//鏄惁鑷姩璺宠浆鍛煎彨椤甸潰 + private String intercomeType; + + /** + * 閾冨0鎾斁鍣� + */ + private MediaPlayer mediaPlayer; + + /** + * 鏈帴浜嬩欢瑙﹀彂鏃堕棿 + */ + public int missedCallsTime = 30; + + public AudioManager audioManager; + private HDLLinphoneKit() { + } + + private void InitMediaPlayer(Context context) { + if (mediaPlayer == null) { + try { + mediaPlayer = MediaPlayer.create(context, R.raw.oldphone_mono); + } catch (Exception ex) { + + } + } + } + + private void initAudioManager(Context context){ + if(audioManager == null){ + try{ + audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + }catch (Exception exception){ + + } + } + } + + public int getRingerMode() + { + return audioManager.getRingerMode(); + } + + public boolean getMediaPlayerPlayingStatus(){ + if (mediaPlayer != null){ + return mediaPlayer.isPlaying(); + } + return false; + } + + + // 鎾斁閾冨0 + public void playRingtone() { + if (mediaPlayer != null && !mediaPlayer.isPlaying()) { + mediaPlayer.start(); // 寮�濮嬫挱鏀� + } + } + + /** + * 鏄惁寰幆鎾斁閾冨0 + * @param isLooping + */ + public void setMediaPlayerLooping(boolean isLooping) { + mediaPlayer.setLooping(isLooping); + } + + // 鍋滄閾冨0 + public void stopRingtone() { + if (mediaPlayer != null && mediaPlayer.isPlaying()) { + mediaPlayer.stop(); // 鍋滄鎾斁 +// mediaPlayer.reset(); // 閲嶇疆 MediaPlayer +// mediaPlayer.release(); // 閲婃斁璧勬簮 +// mediaPlayer = null; + } + } + + public void releaseRingtone(){ + if (mediaPlayer != null ) { + mediaPlayer.release(); // 閲婃斁璧勬簮 + mediaPlayer = null; + } } /** @@ -110,6 +201,8 @@ viewContext = context.getApplicationContext(); if (!HDLLinphoneService.isReady()) { CheckIfNeedstartService(); + InitMediaPlayer(context); + initAudioManager(context); } else { } } @@ -144,7 +237,7 @@ @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); @@ -163,21 +256,21 @@ @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)); } }); @@ -196,6 +289,24 @@ } } + 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; + } + + }; + /** * 寮�鍚湇鍔� * @@ -206,12 +317,8 @@ 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);//瑙e喅android8.0浠ヤ笂鏃犳硶鍚姩鏈嶅姟鐨勯棶棰� -// } else { -// context.startService(intent); -// } + context.bindService(intent, connection, Context.BIND_AUTO_CREATE); + } } catch (Exception e) { e.printStackTrace(); @@ -251,7 +358,22 @@ mUsername = username; mPassword = password; mServerIP = serverIP; + intercomeType = inter_type; + mTransportType = "UDP"; login(inter_type); + } + + public void setAccountAndLogin(String username, String password, String serverIP, String inter_type, String transportType) { + mUsername = username; + mPassword = password; + mServerIP = serverIP; + mTransportType = transportType; + intercomeType = inter_type; + login(inter_type); + } + + public String getIntercomeType() { + return intercomeType; } /** @@ -352,16 +474,18 @@ * 鐧诲綍 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."); } if (inter_type.equals(INTER_PHONE_TYPE_FREEVIEW)) { // 鍏ㄨ閫氬彲瑙嗗璁叉敞鍐� - freeViewRegisterUserAuth(mUsername, mPassword, mServerIP); + freeViewRegisterUserAuth(mUsername, mPassword, mServerIP,mTransportType); } else { // 鍏朵粬鍙瀵硅娉ㄥ唽 - registerUserAuth(mUsername, mPassword, mServerIP); + registerUserAuth(mUsername, mPassword, mServerIP,mTransportType); } } catch (CoreException e) { e.printStackTrace(); @@ -376,16 +500,29 @@ * @param host * @throws CoreException */ - public void registerUserAuth(String name, String password, String host) throws CoreException { + public void registerUserAuth(String name, String password, String host,String transportType) throws CoreException { 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); + switch (transportType.toUpperCase()){ + case "UDP": + mAccountCreator.setTransport(TransportType.Udp); + Log.i("linphone", "setting : TransportType.Udp"); + break; + case "TCP": + mAccountCreator.setTransport(TransportType.Tcp); + Log.i("linphone", "setting : TransportType.Tcp"); + break; + default: + mAccountCreator.setTransport(TransportType.Udp); + Log.i("linphone", "default : TransportType.Udp"); + break; + } ProxyConfig prxCfg = mAccountCreator.createProxyConfig(); prxCfg.enableQualityReporting(false); @@ -395,14 +532,89 @@ mLinphoneCore.addProxyConfig(prxCfg);//娣诲姞浠g悊閰嶇疆銆傚鏋滃惎鐢ㄤ簡娉ㄥ唽锛岃繖灏嗗紑濮嬪湪浠g悊涓婃敞鍐屻�� mLinphoneCore.addAuthInfo(authInfo);//娣诲姞璁よ瘉淇℃伅鍒�,璇ヤ俊鎭墖娈靛皢鍦ㄦ墍鏈夐渶瑕佺殑SIP浜嬪姟涓娇鐢ㄨ韩浠介獙璇� mLinphoneCore.setDefaultProxyConfig(prxCfg);//璁剧疆榛樿浠g悊銆� + } + + public void sendSipMessage(String sipAddress, String message) { + try { + Core mLinphoneCore = HDLLinphoneService.getCore(); + if (mLinphoneCore == null) { + Log.e("Linphone", "Linphone Core 鏈垵濮嬪寲"); + return; + } + + // 瑙f瀽瀵规柟鐨凷IP鍦板潃 + Address address = mLinphoneCore.interpretUrl(sipAddress); + + // 寤虹珛瀵硅瘽 + ChatRoom chatRoom = mLinphoneCore.getChatRoom(address); + + // 鍒涘缓娑堟伅 + ChatMessage chatMessage = chatRoom.createMessageFromUtf8(message); + + // 鐩戝惉娑堟伅鐘舵�� + chatMessage.addListener(new ChatMessageListener() { + @Override + public void onFileTransferSendChunk(ChatMessage message, Content content, int offset, int size, Buffer buffer) { + // 鏂囦欢浼犺緭鐨勫垎鍧楀彂閫佸洖璋冿紝鏈娇鐢� + } + + @Override + public void onMsgStateChanged(ChatMessage message, ChatMessage.State state) { + if (state == ChatMessage.State.Delivered) { + Log.d("Linphone", "娑堟伅宸叉垚鍔熷彂閫�"); + } else if (state == ChatMessage.State.NotDelivered) { + Log.d("Linphone", "娑堟伅鍙戦�佸け璐�"); + } + } + + @Override + public Buffer onFileTransferSend(ChatMessage message, Content content, int offset, int size) { + return null; // 鏂囦欢浼犺緭鍥炶皟锛屾湭浣跨敤 + } + + @Override + public void onEphemeralMessageDeleted(ChatMessage message) { + // 涓存椂娑堟伅鍒犻櫎鍥炶皟锛屾湭浣跨敤 + } + + @Override + public void onEphemeralMessageTimerStarted(ChatMessage message) { + // 涓存椂娑堟伅璁℃椂鍣ㄥ紑濮嬪洖璋冿紝鏈娇鐢� + } + + @Override + public void onParticipantImdnStateChanged(ChatMessage message, ParticipantImdnState state) { + // 鍙備笌鑰呮秷鎭�氱煡鐘舵�佹敼鍙樺洖璋冿紝鏈娇鐢� + } + + @Override + public void onFileTransferRecv(ChatMessage message, Content content, Buffer buffer) { + // 鏂囦欢浼犺緭鎺ユ敹鍥炶皟锛屾湭浣跨敤 + } + + @Override + public void onFileTransferProgressIndication(ChatMessage message, Content content, int offset, int total) { + // 鏂囦欢浼犺緭杩涘害鍥炶皟锛屾湭浣跨敤 + } + }); + // 鍙戦�佹秷鎭� + chatMessage.send(); + + } catch (Exception e) { + Log.e("Linphone", "娑堟伅鍙戦�佸け璐�", e); + e.printStackTrace(); + } + } + + /** * 鎻愪氦璐﹀彿鍜屽瘑鐮佹敞鍐屽埌鍏ㄨ閫氭湇鍔″櫒 * * @throws CoreException */ - public void freeViewRegisterUserAuth(String userName, String password, String domain) throws CoreException { + public void freeViewRegisterUserAuth(String userName, String password, String domain,String transportType) throws CoreException { Core mLinphoneCore = HDLLinphoneService.getCore(); if (mLinphoneCore == null) return; @@ -412,13 +624,28 @@ mAccountCreator.setUsername(userName); mAccountCreator.setDomain("yun"); mAccountCreator.setPassword(password); - mAccountCreator.setTransport(TransportType.Udp); + + switch (transportType.toUpperCase()){ + case "UDP": + mAccountCreator.setTransport(TransportType.Udp); + Log.i("linphone", "setting : TransportType.Udp"); + break; + case "TCP": + mAccountCreator.setTransport(TransportType.Tcp); + Log.i("linphone", "setting : TransportType.Tcp"); + break; + default: + mAccountCreator.setTransport(TransportType.Udp); + Log.i("linphone", "default : TransportType.Udp"); + break; + } ProxyConfig prxCfg = mAccountCreator.createProxyConfig(); prxCfg.enableQualityReporting(false); prxCfg.setQualityReportingCollector(null); prxCfg.setQualityReportingInterval(0); prxCfg.enableRegister(true); + //鍏ㄨ閫氶渶瑕佽缃浐瀹氫唬鐞嗘湇鍔″櫒ip prxCfg.setServerAddr("sip:139.159.157.75:46000;transport=udp"); mLinphoneCore.addProxyConfig(prxCfg);//娣诲姞浠g悊閰嶇疆銆傚鏋滃惎鐢ㄤ簡娉ㄥ唽锛岃繖灏嗗紑濮嬪湪浠g悊涓婃敞鍐屻�� @@ -452,10 +679,14 @@ if (addressToCall != null) { call = core.inviteAddressWithParams(addressToCall, params); // 璁剧疆澶栨斁 - for (AudioDevice audioDevice : HDLLinphoneService.getCore().getAudioDevices()) { - if (audioDevice.getType() == AudioDevice.Type.Speaker) { - call.setOutputAudioDevice(audioDevice); + try { + for (AudioDevice audioDevice : HDLLinphoneService.getCore().getAudioDevices()) { + if (audioDevice.getType() == AudioDevice.Type.Speaker) { + call.setOutputAudioDevice(audioDevice); + } } + }catch (Exception exception){ + Log.e("LinphoneError",exception.getMessage()); } } } @@ -488,7 +719,7 @@ params.enableLowBandwidth(false); params.setAudioBandwidthLimit(0); // disable limitation currentCall.acceptWithParams(params); - Log.d("HDLLinphoneKit","---acceptCall= "); + Log.d("HDLLinphoneKit", "---acceptCall= "); } catch (Exception e) { e.printStackTrace(); @@ -552,7 +783,7 @@ * 璁剧疆澶栨斁璁惧 */ 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; @@ -561,14 +792,14 @@ 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); -- Gitblit v1.8.0