Android VOIP拨打电话机制分析

本文详细分析了Android应用CSipSimple如何利用AIDL进行进程间通信(IPC)来实现VOIP拨打电话功能。从创建ISipService.aidl接口开始,介绍了自动编译生成的Java接口以及DialerFragment如何调用服务端的makeCallWithOptions方法。接着,展示了SipService中的实现,包括如何通过接口调用最终到达native层的pjsua_call_make_call函数,从而完成电话拨打过程。
摘要由CSDN通过智能技术生成

【转载自】http://blog.csdn.net/jindegegesun/article/details/8673514

CSipSimple拨打电话机制分析

CSipSimple是运行在android设备上的一个开源的sip协议应用程序,本文其中的拨打电话机制进行大致分析。
项目中,拨打电话利用了AIDL方法来实现。aidl是 Android Interface definition language的缩写,它是一种android内部进程通信接口的描述语言,通过它来定义进程间的通信接口,完成IPC(Inter-Process Communication,进程间通信)。

创建.aidl文件


ISipService.aidl内容如下:

[java]  view plain copy print ?
  1. /** 
  2.  * Copyright (C) 2010-2012 Regis Montoya (aka r3gis - www.r3gis.fr) 
  3.  * This file is part of CSipSimple. 
  4.  * 
  5.  *  CSipSimple is free software: you can redistribute it and/or modify 
  6.  *  it under the terms of the GNU General Public License as published by 
  7.  *  the Free Software Foundation, either version 3 of the License, or 
  8.  *  (at your option) any later version. 
  9.  *  If you own a pjsip commercial license you can also redistribute it 
  10.  *  and/or modify it under the terms of the GNU Lesser General Public License 
  11.  *  as an android library. 
  12.  * 
  13.  *  CSipSimple is distributed in the hope that it will be useful, 
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  16.  *  GNU General Public License for more details. 
  17.  * 
  18.  *  You should have received a copy of the GNU General Public License 
  19.  *  along with CSipSimple.  If not, see <http://www.gnu.org/licenses/>. 
  20.  *   
  21.  *  This file and this file only is also released under Apache license as an API file 
  22.  */  
  23. package com.csipsimple.api;  
  24. import com.csipsimple.api.SipProfileState;  
  25. import com.csipsimple.api.SipCallSession;  
  26. import com.csipsimple.api.MediaState;  
  27.   
  28. interface ISipService{  
  29.     /** 
  30.     * Get the current API version 
  31.     * @return version number. 1000 x major version + minor version 
  32.     * Each major version must be compatible with all versions of the same major version 
  33.     */  
  34.   
  35. .........  
  36. void makeCallWithOptions(in String callee, int accountId, in Bundle options);  
  37. }  
ISipService.aidl中定义了包含makeCallWithOptions
方法的接口ISipService。

自动编译生成java文件

eclipse中的ADT插件会自动在aidl文件中声明的包名目录下生成java文件,如下图所示:


ISipService.java

[java]  view plain copy print ?
  1. package com.csipsimple.api;  
  2. public interface ISipService extends android.os.IInterface  
  3. {  
  4. ……  
  5. //Place a call  
  6.   
  7. ublic void makeCallWithOptions(java.lang.String callee, int accountId, android.os.Bundle options) throws android.os.RemoteException;  
  8. }  
接下来就是 实现 ISipService.aidl中定义的接口,提供接口的实例供客户端调用

IPC实现

项目中拨打电话  

void com.csipsimple.api.ISipService.makeCallWithOptions(String msg, String toNumber, long accountId)

结合代码一层层看调用

目录:src\com\csipsimple\ui\dialpad

DialerFragment.java

[java]  view plain copy print ?
  1. private ISipService service;  
  2. private ServiceConnection connection = new ServiceConnection() {  
  3.   
  4.     @Override  
  5.     public void onServiceConnected(ComponentName arg0, IBinder arg1) {  
  6.         service = ISipService.Stub.asInterface(arg1);  
  7.      ........  
  8.     }  
  9.   
  10.     @Override  
  11.     public void onServiceDisconnected(ComponentName arg0) {  
  12.         service = null;  
  13.     }  
  14. };  


[java]  view plain copy print ?
  1. <span style="color:#333333;">   @Override  
  2.     public void placeCall() {  
  3.         placeCallWithOption(null);  
  4.     }  
  5.   
  6. private void placeCallWithOption(Bundle b) {  
  7.         if (service == null) {  
  8.             return;  
  9.         }  
  10.         String toCall = "";  
  11.         Long accountToUse = SipProfile.INVALID_ID;  
AndroidVoIP客户端App源代码,用以实现经典的语音及视频通话功能,开发环境的配置下载下来SipDroid源代码后,不能直接导入到eclipse中,否则是无法使用和编译的。   首先需要通过command line参照工程目录下的BUILT.txt文件对工程进行update。   如:android update project -p . -t 2(这里 -p指的是 --path -t指的是 --target)   由于是开源软件,需要版本控制的服务器,可以安装Subclipse(可以参见官网)   这样就可以将工程import到eclipse中了。   整理一些Android视频通话技术开发的资料:   SipDroid是一款开源的SIP/Voip客户端软件,针对Android手机开发平台。它是通过SIP provider来提供电话通信服务的,在它的最新的版本1.5.5beta中也提供了视频通话的服务。   它运行在标准的SIP协议之上,由于Android内核本身没有提供SIP协议栈,所以SipDroid软件中通过JNI直接实现了SIP协议栈。它能够在如下一些网络中提供VoiP服务:WLAN、3G、EDGE网络。甚至能够和Skype、Google Talk, MSN, Yahoo, AIM, and ICQ等用户进行通话。但前提必须到一些SIP provider注册账号(有免费的,也有收费的),SipDroid强烈建议使用PBXes这个sip provider来得到服务。   对于SipDroid实现视频通话的原理,目前的理解是:发送方是将是将视频数据传输到视频流媒体服务器,然后再传输给接收方的。具体细节目前不是很清楚,因为涉及到许多通信协议。   本项目共250 个目标文件,还有jni文件下c 文件需要编译。 感兴趣的同学必须得好好研究研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值