CSipsimple的核心在PJSIP(JNI中),而Java代码只是实现了PJSIP的管理和界面的展示。
本篇将对CSipsimple进行重新封装,把项目封装成jar,简单的说是重新封装了如何使用PJSIP,方便其它项目直接使用。
先看效果图:
支持多个帐户,效果图如上。
通话效果如图,支持多个帐户、多个电话。
支持媒体设置,效果上图。
G729 编码测试
测试项目如图:
主要测试源码介绍
InCallMediaControl 类是媒体设置对话框(直接引用CSipsimple) MainActivity 主界面 SessionManager 会话管理 SipCallStatus 通话界面 SipSession 会话
MainActivity 中主要就是注册了,如下:
public SipProfile buildAccount() {// Log.d("", "buildAccount"); SipProfile account = new SipProfile(); account.setDisplayName("1001"); String[] serverParts = "baidu.com:5060".split(":"); account.setAccId("<sip:" + SipUri.encodeUser("1001") + "@" + serverParts[0].trim() + ">"); String regUri = "sip:" + "baidu:5060"; account.setRegUri(regUri); account.setProxies(new String[] { regUri }); account.setRealm("*"); account.setUsername("1001"); account.setData("1001"); account.setScheme(SipProfile.CRED_SCHEME_DIGEST); account.setDatatype(SipProfile.CRED_DATA_PLAIN_PASSWD); // By default auto transport account.setTransport(SipProfile.TRANSPORT_UDP); return account; }
SipSession 一个会话单元
SessionManager 管理会话,会话分为四个步骤:连接Sip,连接对方,连接成功,连接结束(一个会话的流程)
其中会话里面有一个接口,监听会话信息,如下:
@Override public boolean onSipSessionState(int state, SipCallSessionImpl sipCallSessionImpl, String accId) { if (mLastSessionState == state) return false; mLastSessionState = state; String number = sipCallSessionImpl.getRemoteContact(); number = SipUri.getDisplayedSimpleContact(number); SipSession sipSession = hasSipSessionByNumber(number); if (state != UAStateReceiver.UA_STATE_RINGRING) { SipManager.getInstance().getMediaManager().stopRing(); } if (state == UAStateReceiver.UA_STATE_INCOMING_CALL) { if (sipSession != null && sipSession.getCallStatus() == CallStatus.ConnectSuccess) return true; if (sipSession != null) delSipSession(sipSession); sipSession = new SipSession(number, CallType.IncomeCall); sipSession.setRingTime(System.currentTimeMillis()); sipSession.setCallStatus(CallStatus.ConnectOther); getSipSessions().add(sipSession); ActivityHelp.jumpToNewActivity(mContext, SipCallStatus.class, number); SipManager.getInstance().getMediaManager() .startRing(sipCallSessionImpl.getRemoteContact()); getWakeLock().acquire(); } else if (state == UAStateReceiver.UA_STATE_OUTGOING_CALL) { } else if (state == UAStateReceiver.UA_STATE_RINGRING) { if (sipSession == null) return false; sipSession.setRingTime(System.currentTimeMillis()); sipSession.setCallStatus(CallStatus.ConnectSuccess);// 响铃 } else if (state == UAStateReceiver.UA_STATE_INCALL) { if (getWakeLock().isHeld()) getWakeLock().release(); if (sipSession == null) return false; if (sipSession.getInCallTime() == 0) sipSession.setInCallTime(System.currentTimeMillis()); sipSession.setCallStatus(CallStatus.ConnectSuccess);// } else if (state == UAStateReceiver.UA_STATE_IDLE || state == UAStateReceiver.UA_STATE_IDLE_BUSY) { if (getWakeLock().isHeld()) getWakeLock().release(); if (sipSession == null) return false; sipSession.setCallStatus(CallStatus.ConnectOver);// 结束 sipSession.setSessionOver(true); } else if (state == UAStateReceiver.UA_STATE_IDLE_NOTFOUND) { } else if (state == UAStateReceiver.UA_STATE_HOLD) { } if (sipSession != null) { sipSession.setSipCallSessionImpl(sipCallSessionImpl); sipSession.onISipSessionStatusListener(); if (state == UAStateReceiver.UA_STATE_IDLE || state == UAStateReceiver.UA_STATE_IDLE_BUSY) { // 更改通知 cancelCallNotification(); } else { showCallNotification(sipSession); } } return false; }
该接口是根据PJSIP触发。
使用G729 编码,只需要设置其优先级即可,代码如下:
SipManager.getInstance().setCodecPriority("G729/8000/1", SipManager.CODEC_NB, "240"); SipManager.getInstance().setCodecPriority("G729/8000/1", SipManager.CODEC_WB, "240");设置好后,重新启动sip便可使用了。经测试语音良好。
最新版新增:
优化注册流程;新增获取当前使用的编码;新增编解码的限制。
限制编码使用如下:
// [speex/16000/1, speex/8000/1, speex/32000/1, GSM/8000/1, PCMU/8000/1, // PCMA/8000/1, G722/16000/1, AMR/8000/1, AMR-WB/16000/1, ISAC/16000/1, // ILBC/8000/1, G729/8000/1, SILK/8000/1, SILK/12000/1, SILK/16000/1, // SILK/24000/1] SipManager.getInstance().addAudioCodesForOnlyUse("G729/8000/1");// 谨慎使用,如果对方无此编码,对方不会接收会话
如果其它项目直接使用,只要实现注册,实现该接口即可实现会话了,是不是很方便?
CSipsimple的重新封装告一段落,以后其它项目可直接应用Jar包便可实现Sip的功能。
项目下载(已添加资源)
项目下载(最新版,优化注册流程;新增获取当前使用的编码;新增编解码的限制)
项目下载(最新稳定版,解决部分bug,解决通话异常情况)