CSipsimple的核心在PJSIP(JNI中),而Java代码只是实现了PJSIP的管理和界面的展示。
本篇将对CSipsimple进行重新封装,把项目封装成jar,简单的说是重新封装了如何使用PJSIP,方便其它项目直接使用。
先看效果图:
支持多个帐户,效果图如上。
通话效果如图,支持多个帐户、多个电话。
支持媒体设置,效果上图。
测试项目如图:
主要测试源码介绍
InCallMediaControl 类是媒体设置对话框(直接引用CSipsimple) MainActivity 主界面 SessionManager 会话管理 SipCallStatus 通话界面 SipSession 会话
MainActivity 中主要就是注册了,如下:
public SipProfile buildAccount() {//
Log.d("", "buildAccount");
SipProfile account = new SipProfile();
account.setDisplayName("1001");
String[] serverParts = "wx-s.net: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;
}如果其它项目直接使用,只要实现注册,实现该接口即可实现会话了,是不是很方便?
CSipsimple的重新封装告一段落,以后其它项目可直接应用Jar包便可实现Sip的功能。
原文:http://blog.csdn.net/banketree/article/details/21543253