sip协议的核心是SipManager,我在前篇博客中已经讲述了sip通话的代码建立过程,点击打开链接
会创建一个SipManager实例,它的构造代码如下:
public static SipManager newInstance(Context context) {
return (isApiSupported(context) ? new SipManager(context) : null);
}
private SipManager(Context context) {
mContext = context;
createSipService();
}
private void createSipService() {
IBinder b = ServiceManager.getService(Context.SIP_SERVICE); //获取sip服务的代理类,返回一个BinderProxy对象.
mSipService = ISipService.Stub.asInterface(b);
}
从上面代码可以看出,SipManager的构造过程中会去获取sip服务(实际就是SipService)的代理类对象,SipManager的很多方法最终是调用到这个服务端的相应方法来实现的.
那么下面我们先看看SipService是如何注册到ServiceManager中去的.
它的注册与一般服务的注册不同,绝大多数Service都是直接在SysetmServer进程中进行注册,而SipService是在TeleService apk中注册,也就是在phone进程中注册的(部分phone相关的服务也是在phone进程中注册的),虽然最终都是注册到ServiceManager中,但这个服务是运行在phone进程中的.
具体实现代码在PhoneGlobals.java中.
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
PhoneConstants.State phoneState;
switch (msg.what) {
// Starts the SIP service. It's a no-op if SIP API is not supported
// on the deivce.
// TODO: Having the phone process host the SIP service is only
// temporary. Will move it to a persistent communication process
// later.
case EVENT_START_SIP_SERVICE:
SipService.start(getApplicationContext());
break;
..//
}
在PhoneGlobals的onCreate()中
if (mCM == null) {
....//
mHandler.sendEmptyMessage(EVENT_START_SIP_SERVICE);
}
SipService的start方法如下:
public static void start(Context context) {
if (SipManager.isApiSupported(context)) {
ServiceManager.addService("sip", new SipService(context));
context.sendBroadcast(new Intent(SipManager.ACTION_SIP_SERVICE_UP));
if (DBG) slog("start:");
}
}
这样就将一个SipService注册到ServiceManager中了.
我在前面博客已分析过,拨打一个网络电话是调用SipManager的makeAudioCall()方法实现的.
下面我以拨打一个网络电话为例进行说明具体实现流程:
public SipAudioCall makeAudioCall(SipProfile localProfile,