Android · 2015年2月11日 0

基于SIP协议的VOIP

基于SIP协议的VOIP
SIP(Session Initiation Protocol,会话发起协议)是一个应用层协议,用用建立、修改和终止包括视频、语音、即时通信、在线游戏和虚拟现实等多种多媒体元素在内的交互式用户会话。

在开发视频会议、即时消息等应用程序的时候可能用户到Android SIP API

Android SIP应用程序必要条件
1.必须运行在Android2.3以上系统
2.SIP是通过无线数据连接来运行的,所以设备必须有一个数据连接(wifi或者移动网络)
3.必须有一个SIP账户。
SIP服务器搭建
高效的SIP服务器有很多,这里介绍Brekeke SIP Server下载地址:(国内打不开请翻墙)http://www.brekeke.com/downloads/sip-server.php
更多服务器参考http://www.officesip.com/、http://www.kamailio.org/
安装配置大家自己安装不同服务器配置不同,自行配置。(http://wenku.baidu.com/link?url=hUh_ICcJ6C_S0ru9ygE5jzt9CJb9Xs3NHfz2kWsRdunB3ArX3abB-w4YlsIuxWDayM3ZQq8oPqudP4Lk9CoAK6W5wuqK87AQCPgavgCODre)

SIP程序设置
主要类android.net.sip包中,其中最重要的是SipManager类

android SIP API中类和接口
1.SipAudioCall 通过SIP处理网络音频电话
2.SipAudioCall.Listener 关于SIP电话的事件监听器,比如接到一个电话或者呼出一个电话的时候
3.SipErrorCode 定义在SIP活动中返回的错误代码
4.SipManager 为SIP任务提供API,比如初始化一个SIP连接,提供相关SIP服务的访问
5.SipProfile 定义了SIP相关属性,包括SIP账户、域名和服务器信息
6.SipProfile.Builder 创建SipProfile的帮助类
7.SipSession 代表一个SIP会话,跟SIP对话或者一个没有对话框的独立事务相关联
8.SipSession.Listener 关于SIP会话的事件监听器,比如注册一个会话或者呼出一个电话的时候
9.SipSession.State 定义SIP会话的声明,比如 注册、呼出电话、打入电话
10.SipRegisterationListener 一个关于SIP注册事件监听器的接口

Android权限列表
//访问网络
<uses-permission android:name=”android.permission.INTERNET”/>
//使用SIP连接
<uses-permission android:name=”android.permission.USE_SIP”/>
//访问WIFI状态
<uses-permission android:name=”android.permission.ACCESS_WIFI_STATE”/>
//允许程序在手机屏幕关闭后后天进程仍然运行
<uses-permission android:name=”android.permission.WAKE_LOCK”/>
//允许通过手机或者耳机的麦克录制声音
<uses-permission android:name=”android.permission.RECORD_AUDIO”/>

不是所有的设备支持SIP,确保APP只安装在支持SIP的设备上,程序中使用下面代码检测
//检测当前设备是否支持VOIP通话
Boolean voipSupported=SipManager.isVoipSupported(this);
//检测当前设备是否支持SIP的API
Boolean apiSupport=SipManager.isApiSupported(this);

添加<uses-feature android:name=”android.hardware.sip.voip” android:required=”true”/>在Manifest中,不支持SIP的设备在市场过滤掉(Google play)

<uses-feature android:name=”android.hardware.wifi” android:required=”true”/>
<uses-feature android:name=”android.hardware.microphone” android:required=”true”/>

在应用Manifest中定义一耳光广播接收器,接收呼叫
<receiver android:name=”.IncomingCallReceiver” android:lable=”Call Receiver”/>
SIP初始化通话
//创建空的SipManager对象
public SipManager mSipManager=null;
//检测当前SipManager是否为空
if(mSipManager==null){
mSipManager=SipManager.newInstance(this);
}
创建SipProfile
//创建SipProfile对象
public SipProfile mSipProfile=null;
//使用用户名和服务器地址做为参数
SipProfile.Builder builder=new SipProfile.Builder(username,domain);
//给出密码
builder.setPassword(password);
//实例化SipProfile对象
mSipProfile=builder.build();

接下来设置一个标签为android.SipDemo.INCOMING_CALL的意图,这个意图会被一个意图过滤器使用
Intent intent=new Intent();
intent.setAction(“android.SipDemo.INCOMING_CALL”);
//使用PendingIntent对象延后调用
PendingIntent pendingIntent=PendingIntent.getBroadcast(this,0,intent,Intent.Fill_IN_DATA);
//调用方法处理意图
mSipManager.open(mSipProfile,pendingintent,null);

在SipManager上设置 一个SipRegisterationListener监听器,这个监听器会跟踪SipProfile,来确定是否成功地注册到SIP服务器
mSipManager.setRegistrationListener(mSipProfile.getUriString(),new SipRegistrationListener(){
public void onRegistrationListener(String localProfileUri){
//正在和SIP服务器通信
updateStatus(“Registering with SIP Server…”);
}

public void onRegistrationDone(String localProfileUri,long expiryTime){
//在SIP服务器上注册成功
updateStatus(“Ready”);
}

public void onRefistrationFailed(String localProfileUri,int errorCode,String errorMessage){
//在SIP服务器上注册失败
updateStatus(“Registration failed,Please check settings.”);
}
});
//注销当前设备
public void closeLocalProfile(){
//检测SipManager对象是否为空
if(mSipManager==null){//如果为空,则不进行热乎操作,直接返回
return;
}else{
try{
//不为空的情况
if(mSipProfile!=null){
//关闭相关对象
mSipManager.close(mSipProfile.getUriString());
}
}catch(Exception e){

}
}
}

监听SIP通话
大部分客户与SIP堆栈的交互都是通过监听器来完成的,所以在拨打SIP语音大户的时候,需要建立一个SipAudioCall.Listener监听器

//创建一个新的SipAudioCall.Listener监听器
SipAudioCall.Listener listener=new SipAudioCall.Listener(){
public void onCallEstablished(SipAudioCall call){
//对已经建立的呼叫启动音频
call.startAudio();
//设备设置为扬声器模式
call.setSpeakerMode(true);
//静音切换
call.toggleMute();
}

//会话终止时候调用
public void onCallEnded(SipAudioCall call){

}
}

拨打电话
一旦创建了SipAudioCall.Listener监听器,就可以拨打电话了,需要用到SipAudioCall的makeAudioCall方法

//拨打电话
makeAudioCall(SipProfile localProfile,SipProfile peerProfile,SipAudioCall.Listener listener,int timeout)
参数localProfileUri代表本地SIP配置文件(呼叫方),peerProfile为相应的Sip配置文件(被呼叫方);listener用来监听SipAudioCall发财的呼叫事件,这个参数可以为null,timeout是超时时间,以秒为单位。
call=mSipManger.makeAudioCall(mSipProfile.getUriString(),sipAddress,listener,30);

接收呼叫
为了接收呼叫,SIP应用程序必须包含一个BroadcastReceiver的子类,这个子类有能力响应一个表明有来电的Intent
当Android系统接收到一个呼叫的时候,会处理这个SIP呼叫,然后广播一个来的的Intent。

(详细内容参考:深入理解Android网络编程 技术详解与最佳实践)

待续。。。

 

转载注明:http://www.etongwl.com/?p=648

Share this: