CSipSimple是运行在android设备上的一个开源的sip协议应用程序,本文其中的拨打电话机制进行大致分析。
项目中,拨打电话利用了AIDL方法来实现。aidl是 Android Interface definition language的缩写,它是一种android内部进程通信接口的描述语言,通过它来定义进程间的通信接口,完成IPC(Inter-Process Communication,进程间通信)。
创建.aidl文件
ISipService.aidl内容如下:
- /**
- * Copyright (C) 2010-2012 Regis Montoya (aka r3gis - www.r3gis.fr)
- * This file is part of CSipSimple.
- *
- * CSipSimple is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- * If you own a pjsip commercial license you can also redistribute it
- * and/or modify it under the terms of the GNU Lesser General Public License
- * as an android library.
- *
- * CSipSimple is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with CSipSimple. If not, see <http://www.gnu.org/licenses/>.
- *
- * This file and this file only is also released under Apache license as an API file
- */
- package com.csipsimple.api;
- import com.csipsimple.api.SipProfileState;
- import com.csipsimple.api.SipCallSession;
- import com.csipsimple.api.MediaState;
- interface ISipService{
- /**
- * Get the current API version
- * @return version number. 1000 x major version + minor version
- * Each major version must be compatible with all versions of the same major version
- */
- .........
- void makeCallWithOptions(in String callee, int accountId, in Bundle options);
- }
ISipService.aidl中定义了包含makeCallWithOptions
方法的接口ISipService。
自动编译生成java文件
eclipse中的ADT插件会自动在aidl文件中声明的包名目录下生成java文件,如下图所示:
- package com.csipsimple.api;
- public interface ISipService extends android.os.IInterface
- {
- ……
- //Place a call
- ublic void makeCallWithOptions(java.lang.String callee, int accountId, android.os.Bundle options) throws android.os.RemoteException;
- }
接下来就是实现ISipService.aidl中定义的接口,提供接口的实例供客户端调用
IPC实现
项目中拨打电话
void com.csipsimple.api.ISipService.makeCallWithOptions(String msg, String toNumber, long accountId)
结合代码一层层看调用
目录:src\com\csipsimple\ui\dialpad
DialerFragment.java
- private ISipService service;
- private ServiceConnection connection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName arg0, IBinder arg1) {
- service = ISipService.Stub.asInterface(arg1);
- ........
- }
- @Override
- public void onServiceDisconnected(ComponentName arg0) {
- service = null;
- }
- };
- <span style="color:#333333;"> @Override
- public void placeCall() {
- placeCallWithOption(null);
- }
- private void placeCallWithOption(Bundle b) {
- if (service == null) {
- return;
- }
- String toCall = "";
- Long accountToUse = SipProfile.INVALID_ID;
- // Find account to use
- SipProfile acc = accountChooserButton.getSelectedAccount();
- if (acc != null) {
- accountToUse = acc.id;
- }
- // Find number to dial
- if(isDigit) {
- toCall = PhoneNumberUtils.stripSeparators(digits.getText().toString());
- }else {
- toCall = digits.getText().toString();
- }
- if (TextUtils.isEmpty(toCall)) {
- return;
- }
- // Well we have now the fields, clear theses fields
- digits.getText().clear();
- // -- MAKE THE CALL --//
- if (accountToUse >= 0) {
- // It is a SIP account, try to call service for that
- try {
- </span><span style="color:#ff0000;"> service.makeCallWithOptions(toCall, accountToUse.intValue(), b);</span><span style="color:#333333;">
- } catch (RemoteException e) {
- Log.e(THIS_FILE, "Service can't be called to make the call");
- }
- } else if (accountToUse != SipProfile.INVALID_ID) {
- // It's an external account, find correct external account
- CallHandlerPlugin ch = new CallHandlerPlugin(getActivity());
- ch.loadFrom(accountToUse, toCall, new OnLoadListener() {
- @Override
- public void onLoad(CallHandlerPlugin ch) {
- placePluginCall(ch);
- }
- });
- }
- }
- </span>
- 2.服务端
- SipService.java
- /**
- * 继承 Service发布服务
- */
- public class SipService extends Service {
- ...
- // 为服务实现公共接口, Stub类继承了Binder
- private final ISipService.Stub binder = new ISipService.Stub() {
- ...
- @Override
- public void makeCallWithOptions(final String callee, final int accountId, final Bundle options)
- throws RemoteException {
- SipService.this.enforceCallingOrSelfPermission(SipManager.PERMISSION_USE_SIP, null);
- //We have to ensure service is properly started and not just binded
- SipService.this.startService(new Intent(SipService.this, SipService.class));
- if(pjService == null) {
- Log.e(THIS_FILE, "Can't place call if service not started");
- // TODO - we should return a failing status here
- return;
- }
- if(!supportMultipleCalls) {
- // Check if there is no ongoing calls if so drop this request by alerting user
- SipCallSession activeCall = pjService.getActiveCallInProgress();
- if(activeCall != null) {
- if(!CustomDistribution.forceNoMultipleCalls()) {
- notifyUserOfMessage(R.string.not_configured_multiple_calls);
- }
- return;
- }
- }
- getExecutor().execute(new SipRunnable() {
- @Override
- protected void doRun() throws SameThreadException {
- <span style="color:#ff0000;"> pjService.makeCall(callee, accountId, options);
- }</span>
- });
- }
/**
* 返回一个实现了接口的类对象,给客户端接收
*/
@Override
public IBinder onBind(Intent intent) {
String serviceName = intent.getAction();
Log.d(THIS_FILE, "Action is " + serviceName );
if (serviceName == null || serviceName.equalsIgnoreCase(SipManager.INTENT_SIP_SERVICE )) {
Log.d(THIS_FILE, "Service returned");
return binder ;
} else if (serviceName. equalsIgnoreCase(SipManager.INTENT_SIP_CONFIGURATION )) {
Log.d(THIS_FILE, "Conf returned");
return binderConfiguration ;
}
Log.d(THIS_FILE, "Default service (SipService) returned");
return binder;
}
...
}
就可以被其他Activity访问调用了。
- <span style="color:#ff0000;">service.makeCallWithOptions(toCall, accountToUse.intValue(), b);</span><span style="font-family: Verdana, Arial, Helvetica, sans-serif;">调用接口中的方法,完成IPC方法。</span>
- <span style="color:#333333;">public int makeCall(String callee, int accountId, Bundle b) throws SameThreadException {
- if (!created) {
- return -1;
- }
- final ToCall toCall = sanitizeSipUri(callee, accountId);
- if (toCall != null) {
- pj_str_t uri = pjsua.pj_str_copy(toCall.getCallee());
- // Nothing to do with this values
- byte[] userData = new byte[1];
- int[] callId = new int[1];
- pjsua_call_setting cs = new pjsua_call_setting();
- pjsua_msg_data msgData = new pjsua_msg_data();
- int pjsuaAccId = toCall.getPjsipAccountId();
- // Call settings to add video
- pjsua.call_setting_default(cs);
- cs.setAud_cnt(1);
- cs.setVid_cnt(0);
- if(b != null && b.getBoolean(SipCallSession.OPT_CALL_VIDEO, false)) {
- cs.setVid_cnt(1);
- }
- cs.setFlag(0);
- pj_pool_t pool = pjsua.pool_create("call_tmp", 512, 512);
- // Msg data to add headers
- pjsua.msg_data_init(msgData);
- pjsua.csipsimple_init_acc_msg_data(pool, pjsuaAccId, msgData);
- if(b != null) {
- Bundle extraHeaders = b.getBundle(SipCallSession.OPT_CALL_EXTRA_HEADERS);
- if(extraHeaders != null) {
- for(String key : extraHeaders.keySet()) {
- try {
- String value = extraHeaders.getString(key);
- if(!TextUtils.isEmpty(value)) {
- int res = pjsua.csipsimple_msg_data_add_string_hdr(pool, msgData, pjsua.pj_str_copy(key), pjsua.pj_str_copy(value));
- if(res == pjsuaConstants.PJ_SUCCESS) {
- Log.e(THIS_FILE, "Failed to add Xtra hdr (" + key + " : " + value + ") probably not X- header");
- }
- }
- }catch(Exception e) {
- Log.e(THIS_FILE, "Invalid header value for key : " + key);
- }
- }
- }
- }
- </span><span style="color:#ff0000;">int status = pjsua.call_make_call(pjsuaAccId, uri, cs, userData, msgData, callId);</span><span style="color:#333333;">
- if(status == pjsuaConstants.PJ_SUCCESS) {
- dtmfToAutoSend.put(callId[0], toCall.getDtmf());
- Log.d(THIS_FILE, "DTMF - Store for " + callId[0] + " - "+toCall.getDtmf());
- }
- pjsua.pj_pool_release(pool);
- return status;
- } else {
- service.notifyUserOfMessage(service.getString(R.string.invalid_sip_uri) + " : "
- + callee);
- }
- return -1;
- }</span>
- package org.pjsip.pjsua;
- public class pjsua implements pjsuaConstants {
- public synchronized static int call_make_call(int acc_id, pj_str_t dst_uri, pjsua_call_setting opt, byte[] user_data, pjsua_msg_data msg_data, int[] p_call_id) {
- <span style="color:#ff0000;"> return pjsuaJNI.call_make_call</span>(acc_id, pj_str_t.getCPtr(dst_uri), dst_uri, pjsua_call_setting.getCPtr(opt), opt, user_data, pjsua_msg_data.getCPtr(msg_data), msg_data, p_call_id);
- }
- ..........
- }
* This file was automatically generated by SWIG (http://www.swig.org).
* Version 2.0.4
*
* Do not make changes to this file unless you know what you are doing--modify
* the SWIG interface file instead.
* ----------------------------------------------------------------------------- */
package org.pjsip.pjsua;
public class pjsuaJNI {
...
public final static native int call_make_call(int jarg1, long jarg2, pj_str_t jarg2_, long jarg3, pjsua_call_setting jarg3_, byte[] jarg4, long jarg5, pjsua_msg_data jarg5_, int[] jarg6);
...
}
我们看到了native方法call_make_call,它调用的是封装在库libpjsipjni.so中的函数pjsua_call_make_call,进一步可以在jni目录下找到C代码。
- PJ_DEF(pj_status_t) pjsua_call_make_call(pjsua_acc_id acc_id,
- const pj_str_t *dest_uri,
- const pjsua_call_setting *opt,
- void *user_data,
- const pjsua_msg_data *msg_data,
- pjsua_call_id *p_call_id)
- {
- pj_pool_t *tmp_pool = NULL;
- pjsip_dialog *dlg = NULL;
- pjsua_acc *acc;
- pjsua_call *call;
- int call_id = -1;
- pj_str_t contact;
- pj_status_t status;
- /* Check that account is valid */
- PJ_ASSERT_RETURN(acc_id>=0 || acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
- PJ_EINVAL);
- /* Check arguments */
- PJ_ASSERT_RETURN(dest_uri, PJ_EINVAL);
- PJ_LOG(4,(THIS_FILE, "Making call with acc #%d to %.*s", acc_id,
- (int)dest_uri->slen, dest_uri->ptr));
- pj_log_push_indent();
- PJSUA_LOCK();
- /* Create sound port if none is instantiated, to check if sound device
- * can be used. But only do this with the conference bridge, as with
- * audio switchboard (i.e. APS-Direct), we can only open the sound
- * device once the correct format has been known
- */
- if (!pjsua_var.is_mswitch && pjsua_var.snd_port==NULL &&
- pjsua_var.null_snd==NULL && !pjsua_var.no_snd)
- {
- status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
- if (status != PJ_SUCCESS)
- goto on_error;
- }
- acc = &pjsua_var.acc[acc_id];
- if (!acc->valid) {
- pjsua_perror(THIS_FILE, "Unable to make call because account "
- "is not valid", PJ_EINVALIDOP);
- status = PJ_EINVALIDOP;
- goto on_error;
- }
- /* Find free call slot. */
- call_id = alloc_call_id();
- if (call_id == PJSUA_INVALID_ID) {
- pjsua_perror(THIS_FILE, "Error making call", PJ_ETOOMANY);
- status = PJ_ETOOMANY;
- goto on_error;
- }
- call = &pjsua_var.calls[call_id];
- /* Associate session with account */
- call->acc_id = acc_id;
- call->call_hold_type = acc->cfg.call_hold_type;
- /* Apply call setting */
- status = apply_call_setting(call, opt, NULL);
- if (status != PJ_SUCCESS) {
- pjsua_perror(THIS_FILE, "Failed to apply call setting", status);
- goto on_error;
- }
- /* Create temporary pool */
- tmp_pool = pjsua_pool_create("tmpcall10", 512, 256);
- /* Verify that destination URI is valid before calling
- * pjsua_acc_create_uac_contact, or otherwise there
- * a misleading "Invalid Contact URI" error will be printed
- * when pjsua_acc_create_uac_contact() fails.
- */
- if (1) {
- pjsip_uri *uri;
- pj_str_t dup;
- pj_strdup_with_null(tmp_pool, &dup, dest_uri);
- uri = pjsip_parse_uri(tmp_pool, dup.ptr, dup.slen, 0);
- if (uri == NULL) {
- pjsua_perror(THIS_FILE, "Unable to make call",
- PJSIP_EINVALIDREQURI);
- status = PJSIP_EINVALIDREQURI;
- goto on_error;
- }
- }
- /* Mark call start time. */
- pj_gettimeofday(&call->start_time);
- /* Reset first response time */
- call->res_time.sec = 0;
- /* Create suitable Contact header unless a Contact header has been
- * set in the account.
- */
- if (acc->contact.slen) {
- contact = acc->contact;
- } else {
- status = pjsua_acc_create_uac_contact(tmp_pool, &contact,
- acc_id, dest_uri);
- if (status != PJ_SUCCESS) {
- pjsua_perror(THIS_FILE, "Unable to generate Contact header",
- status);
- goto on_error;
- }
- }
- /* Create outgoing dialog: */
- status = pjsip_dlg_create_uac( pjsip_ua_instance(),
- &acc->cfg.id, &contact,
- dest_uri, dest_uri, &dlg);
- if (status != PJ_SUCCESS) {
- pjsua_perror(THIS_FILE, "Dialog creation failed", status);
- goto on_error;
- }
- /* Increment the dialog's lock otherwise when invite session creation
- * fails the dialog will be destroyed prematurely.
- <span style="white-space:pre"> </span>*/
- pjsip_dlg_inc_lock(dlg);
- if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0)
- pjsip_dlg_set_via_sent_by(dlg, &acc->via_addr, acc->via_tp);
- /* Calculate call's secure level */
- call->secure_level = get_secure_level(acc_id, dest_uri);
- /* Attach user data */
- call->user_data = user_data;
- /* Store variables required for the callback after the async
- * media transport creation is completed.
- */
- if (msg_data) {
- call->async_call.call_var.out_call.msg_data = pjsua_msg_data_clone(
- dlg->pool, msg_data);
- }
- call->async_call.dlg = dlg;
- /* Temporarily increment dialog session. Without this, dialog will be
- * prematurely destroyed if dec_lock() is called on the dialog before
- * the invite session is created.
- */
- pjsip_dlg_inc_session(dlg, &pjsua_var.mod);
- /* Init media channel */
- status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC,
- call->secure_level, dlg->pool,
- NULL, NULL, PJ_TRUE,
- &on_make_call_med_tp_complete);
- if (status == PJ_SUCCESS) {
- status = on_make_call_med_tp_complete(call->index, NULL);
- if (status != PJ_SUCCESS)
- goto on_error;
- } else if (status != PJ_EPENDING) {
- pjsua_perror(THIS_FILE, "Error initializing media channel", status);
- pjsip_dlg_dec_session(dlg, &pjsua_var.mod);
- goto on_error;
- }
- /* Done. */
- if (p_call_id)
- *p_call_id = call_id;
- pjsip_dlg_dec_lock(dlg);
- pj_pool_release(tmp_pool);
- PJSUA_UNLOCK();
- pj_log_pop_indent();
- return PJ_SUCCESS;
- on_error:
- if (dlg) {
- /* This may destroy the dialog */
- pjsip_dlg_dec_lock(dlg);
- }
- if (call_id != -1) {
- reset_call(call_id);
- pjsua_media_channel_deinit(call_id);
- }
- if (tmp_pool)
- pj_pool_release(tmp_pool);
- PJSUA_UNLOCK();
- pj_log_pop_indent();
- return status;
- }
- 通过本文的研究分析,<span style="font-family: Consolas; color: rgb(51, 51, 51);">我们了解到CSipSimple通过aidl方法实现进程间通信,从而实现了拨打电话功能。</span>
相关推荐
支持视频通话的csipsimple android网络电话
csipsimple 好用的安卓软电话,和电话簿完美结合,支持流量监控,支持G729编码,跟bria有一拼
开发者可以通过分析这些文件来深入理解CSipSimple的工作原理。 总的来说,CSipSimple可编译版本为开发者提供了一个实践VoIP技术的绝佳平台,无论是对于新手学习基础知识,还是对于经验丰富的开发者探索高级特性,都...
CSipSimple是一款开源的Android应用,专为移动设备设计,实现了VoIP(Voice over Internet Protocol)功能,即通过互联网进行语音通信。它基于SIP(Session Initiation Protocol)协议,这是一种用于控制多媒体通信...
csipsimple 是开方式比较好用的ip电话软件。兼容标准ip协议。速度快。
7. **源代码分析**:CsipSimple的源代码对于学习SIP通信和Android应用开发非常有价值。开发者可以通过阅读代码理解如何集成PJSIP库,处理网络和多媒体数据流,以及如何在Android环境中实现SIP会话的各个阶段。 总之...
3. **视频通话**:除了语音,CSipSimple还支持视频通信,利用设备的前置或后置摄像头进行视频流传输。 4. **联系人管理**:用户可以导入、编辑和管理联系人,方便拨号。 5. **呼叫历史记录**:查看过去的通话记录,...
最近在研究视频通信,决定使用CSipSimple做二次开发,网上的资源版本都是只支持语音的,自己捣鼓了一下弄出来一个支持视频通信的。 文件解压之后将两个工程直接导入eclipse就可以使用了。使用视频通信需要在设置里面...
CSipSimple是一款基于Android操作系统的SIP软电话应用,它允许用户使用VoIP(Voice over Internet Protocol)服务进行语音通话。安装文档提供了在CentOS6.5 32位系统上安装CSipSimple所需步骤的详细说明。文档指出了...
"csipsimple-master" 是一个开源的VoIP(Voice over Internet Protocol)应用程序项目,主要针对Android平台。这个项目的核心是实现SIP(Session Initiation Protocol)通信,它允许用户通过互联网进行语音、视频...
CSIPSimple是一款针对Android平台的开源VoIP(Voice over IP)软件,它的主要功能是提供一个用户友好的界面,让用户可以使用互联网进行语音通话、视频通话以及即时消息交流。这款应用基于SIP(Session Initiation ...
Csipsimple是一款开源的SIP(Session Initiation Protocol)客户端,用于实现VoIP(Voice over IP)功能,支持语音通话、视频通话以及即时消息等通信方式。 在这款Demo中,开发人员已经对原版的Csipsimple进行了...
从CSipSimple官网svn上下载的源码。需要用lz工具解压。
android 上sip 软件件,是一个开源的软件,这是人个2012年10年4月更新的一个软件
定位功能,轻松定位,能很详细的展示对方所在街道名称和地段。
SIP相关安装包整合:1、CSipSimple客户端安装包 2、SIP服务端:yate安装包,可以在win7上安装 (附上部分自己研究SIP源码过程中 一步步走来看过的资料,以及一些分析,希望能帮到你 o.0)
包含CSipsimple(最新版)所有可用的.so文件 libpj_aac_codec.so libpj_codec2_codec.so libpj_g726_codec.so libpj_g729_codec.so libpj_g7221_codec.so libpj_opensl_dev.so libpj_opus_codec.so libpj_silk_codec...
voipdoup_CSipSimple是一款可以在android设备包括手机、平板电脑上使用的支持sip的网络电话软件,可以在上面设置很多支持sip协议的网络电话使用。 手机网络如3g,edg,gprs最好选择使用gsm编码,带宽占用少,流量少。...