PageRenderTime 35ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 1ms

/CSipSimple/src/com/csipsimple/pjsip/PjSipAccount.java

https://bitbucket.org/bohlooli/csipsimple2
Java | 366 lines | 263 code | 47 blank | 56 comment | 73 complexity | a5b9eade46536f8ff6b55d635c64175a MD5 | raw file
  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. package com.csipsimple.pjsip;
  22. import android.content.Context;
  23. import android.text.TextUtils;
  24. import com.csipsimple.api.SipConfigManager;
  25. import com.csipsimple.api.SipProfile;
  26. import com.csipsimple.api.SipUri;
  27. import com.csipsimple.api.SipUri.ParsedSipContactInfos;
  28. import com.csipsimple.utils.Log;
  29. import com.csipsimple.utils.PreferencesProviderWrapper;
  30. import org.pjsip.pjsua.SWIGTYPE_p_pj_stun_auth_cred;
  31. import org.pjsip.pjsua.csipsimple_acc_config;
  32. import org.pjsip.pjsua.pj_qos_params;
  33. import org.pjsip.pjsua.pj_qos_type;
  34. import org.pjsip.pjsua.pj_str_t;
  35. import org.pjsip.pjsua.pjmedia_srtp_use;
  36. import org.pjsip.pjsua.pjsip_auth_clt_pref;
  37. import org.pjsip.pjsua.pjsip_cred_info;
  38. import org.pjsip.pjsua.pjsua;
  39. import org.pjsip.pjsua.pjsuaConstants;
  40. import org.pjsip.pjsua.pjsua_acc_config;
  41. import org.pjsip.pjsua.pjsua_ice_config;
  42. import org.pjsip.pjsua.pjsua_ice_config_use;
  43. import org.pjsip.pjsua.pjsua_ipv6_use;
  44. import org.pjsip.pjsua.pjsua_stun_use;
  45. import org.pjsip.pjsua.pjsua_transport_config;
  46. import org.pjsip.pjsua.pjsua_turn_config;
  47. import org.pjsip.pjsua.pjsua_turn_config_use;
  48. public class PjSipAccount {
  49. //private static final String THIS_FILE = "PjSipAcc";
  50. private String displayName;
  51. // For now everything is public, easiest to manage
  52. public String wizard;
  53. public boolean active;
  54. public pjsua_acc_config cfg;
  55. public csipsimple_acc_config css_cfg;
  56. public Long id;
  57. public Integer transport = 0;
  58. private int profile_vid_auto_show = -1;
  59. private int profile_vid_auto_transmit = -1;
  60. private int profile_enable_qos;
  61. private int profile_qos_dscp;
  62. private boolean profile_default_rtp_port = true;
  63. //private boolean hasZrtpValue = false;
  64. public PjSipAccount() {
  65. cfg = new pjsua_acc_config();
  66. pjsua.acc_config_default(cfg);
  67. css_cfg = new csipsimple_acc_config();
  68. pjsua.csipsimple_acc_config_default(css_cfg);
  69. }
  70. /**
  71. * Initialize from a SipProfile (public api) object
  72. * @param profile the sip profile to use
  73. */
  74. public PjSipAccount(SipProfile profile) {
  75. this();
  76. if(profile.id != SipProfile.INVALID_ID) {
  77. id = profile.id;
  78. }
  79. displayName = profile.display_name;
  80. wizard = profile.wizard;
  81. transport = profile.transport;
  82. active = profile.active;
  83. transport = profile.transport;
  84. cfg.setPriority(profile.priority);
  85. if(profile.acc_id != null) {
  86. cfg.setId(pjsua.pj_str_copy(profile.acc_id));
  87. }
  88. if(profile.reg_uri != null) {
  89. cfg.setReg_uri(pjsua.pj_str_copy(profile.reg_uri));
  90. }
  91. if(profile.publish_enabled != -1) {
  92. cfg.setPublish_enabled(profile.publish_enabled);
  93. }
  94. if(profile.reg_timeout != -1) {
  95. cfg.setReg_timeout(profile.reg_timeout);
  96. }
  97. if(profile.reg_delay_before_refresh != -1) {
  98. cfg.setReg_delay_before_refresh(profile.reg_delay_before_refresh);
  99. }
  100. if(profile.ka_interval != -1) {
  101. cfg.setKa_interval(profile.ka_interval);
  102. }
  103. if(profile.pidf_tuple_id != null) {
  104. cfg.setPidf_tuple_id(pjsua.pj_str_copy(profile.pidf_tuple_id));
  105. }
  106. if(profile.force_contact != null) {
  107. cfg.setForce_contact(pjsua.pj_str_copy(profile.force_contact));
  108. }
  109. cfg.setAllow_contact_rewrite(profile.allow_contact_rewrite ? pjsuaConstants.PJ_TRUE : pjsuaConstants.PJ_FALSE);
  110. cfg.setContact_rewrite_method(profile.contact_rewrite_method);
  111. cfg.setAllow_via_rewrite(profile.allow_via_rewrite ? pjsuaConstants.PJ_TRUE : pjsuaConstants.PJ_FALSE);
  112. if(profile.use_srtp != -1) {
  113. cfg.setUse_srtp(pjmedia_srtp_use.swigToEnum(profile.use_srtp));
  114. cfg.setSrtp_secure_signaling(0);
  115. }
  116. css_cfg.setUse_zrtp(profile.use_zrtp);
  117. if(profile.proxies != null) {
  118. Log.d("PjSipAccount", "Create proxy "+profile.proxies.length);
  119. cfg.setProxy_cnt(profile.proxies.length);
  120. pj_str_t[] proxies = cfg.getProxy();
  121. int i = 0;
  122. for(String proxy : profile.proxies) {
  123. Log.d("PjSipAccount", "Add proxy "+proxy);
  124. proxies[i] = pjsua.pj_str_copy(proxy);
  125. i += 1;
  126. }
  127. cfg.setProxy(proxies);
  128. }else {
  129. cfg.setProxy_cnt(0);
  130. }
  131. cfg.setReg_use_proxy(profile.reg_use_proxy);
  132. if(profile.username != null || profile.data != null) {
  133. cfg.setCred_count(1);
  134. pjsip_cred_info cred_info = cfg.getCred_info();
  135. if(profile.realm != null) {
  136. cred_info.setRealm(pjsua.pj_str_copy(profile.realm));
  137. }
  138. if(profile.username != null) {
  139. cred_info.setUsername(pjsua.pj_str_copy(profile.username));
  140. }
  141. if(profile.datatype != -1) {
  142. cred_info.setData_type(profile.datatype);
  143. }
  144. if(profile.data != null) {
  145. cred_info.setData(pjsua.pj_str_copy(profile.data));
  146. }
  147. }else {
  148. cfg.setCred_count(0);
  149. }
  150. // Auth prefs
  151. {
  152. pjsip_auth_clt_pref authPref = cfg.getAuth_pref();
  153. authPref.setInitial_auth(profile.initial_auth ? pjsuaConstants.PJ_TRUE : pjsuaConstants.PJ_FALSE);
  154. if(!TextUtils.isEmpty(profile.auth_algo)) {
  155. authPref.setAlgorithm(pjsua.pj_str_copy(profile.auth_algo));
  156. }
  157. cfg.setAuth_pref(authPref);
  158. }
  159. cfg.setMwi_enabled(profile.mwi_enabled ? pjsuaConstants.PJ_TRUE : pjsuaConstants.PJ_FALSE);
  160. cfg.setIpv6_media_use(profile.ipv6_media_use == 1 ? pjsua_ipv6_use.PJSUA_IPV6_ENABLED
  161. : pjsua_ipv6_use.PJSUA_IPV6_DISABLED);
  162. // RFC5626
  163. cfg.setUse_rfc5626(profile.use_rfc5626? pjsuaConstants.PJ_TRUE : pjsuaConstants.PJ_FALSE);
  164. if(!TextUtils.isEmpty(profile.rfc5626_instance_id)) {
  165. cfg.setRfc5626_instance_id(pjsua.pj_str_copy(profile.rfc5626_instance_id));
  166. }
  167. if(!TextUtils.isEmpty(profile.rfc5626_reg_id)) {
  168. cfg.setRfc5626_reg_id(pjsua.pj_str_copy(profile.rfc5626_reg_id));
  169. }
  170. // Video
  171. profile_vid_auto_show = profile.vid_in_auto_show;
  172. profile_vid_auto_transmit = profile.vid_out_auto_transmit;
  173. // Rtp cfg
  174. pjsua_transport_config rtpCfg = cfg.getRtp_cfg();
  175. if(profile.rtp_port >= 0) {
  176. rtpCfg.setPort(profile.rtp_port);
  177. profile_default_rtp_port = false;
  178. }
  179. if(!TextUtils.isEmpty(profile.rtp_public_addr)) {
  180. rtpCfg.setPublic_addr(pjsua.pj_str_copy(profile.rtp_public_addr));
  181. }
  182. if(!TextUtils.isEmpty(profile.rtp_bound_addr)) {
  183. rtpCfg.setBound_addr(pjsua.pj_str_copy(profile.rtp_bound_addr));
  184. }
  185. profile_enable_qos = profile.rtp_enable_qos;
  186. profile_qos_dscp = profile.rtp_qos_dscp;
  187. cfg.setSip_stun_use(profile.sip_stun_use == 0 ? pjsua_stun_use.PJSUA_STUN_USE_DISABLED : pjsua_stun_use.PJSUA_STUN_USE_DEFAULT);
  188. cfg.setMedia_stun_use(profile.media_stun_use == 0 ? pjsua_stun_use.PJSUA_STUN_USE_DISABLED : pjsua_stun_use.PJSUA_STUN_USE_DEFAULT);
  189. if(profile.ice_cfg_use == 1) {
  190. cfg.setIce_cfg_use(pjsua_ice_config_use.PJSUA_ICE_CONFIG_USE_CUSTOM);
  191. pjsua_ice_config iceCfg = cfg.getIce_cfg();
  192. iceCfg.setEnable_ice( (profile.ice_cfg_enable == 1 )? pjsuaConstants.PJ_TRUE : pjsuaConstants.PJ_FALSE);
  193. }else {
  194. cfg.setIce_cfg_use(pjsua_ice_config_use.PJSUA_ICE_CONFIG_USE_DEFAULT);
  195. }
  196. if(profile.turn_cfg_use == 1) {
  197. cfg.setTurn_cfg_use(pjsua_turn_config_use.PJSUA_TURN_CONFIG_USE_CUSTOM);
  198. pjsua_turn_config turnCfg = cfg.getTurn_cfg();
  199. SWIGTYPE_p_pj_stun_auth_cred creds = turnCfg.getTurn_auth_cred();
  200. turnCfg.setEnable_turn( (profile.turn_cfg_enable == 1) ? pjsuaConstants.PJ_TRUE : pjsuaConstants.PJ_FALSE);
  201. turnCfg.setTurn_server( pjsua.pj_str_copy(profile.turn_cfg_server) );
  202. pjsua.set_turn_credentials(
  203. pjsua.pj_str_copy(profile.turn_cfg_user),
  204. pjsua.pj_str_copy(profile.turn_cfg_password),
  205. pjsua.pj_str_copy("*"),
  206. creds);
  207. // Normally this step is useless as manipulating a pointer in C memory at this point, but in case this changes reassign
  208. turnCfg.setTurn_auth_cred(creds);
  209. }else {
  210. cfg.setTurn_cfg_use(pjsua_turn_config_use.PJSUA_TURN_CONFIG_USE_DEFAULT);
  211. }
  212. }
  213. /**
  214. * Automatically apply csipsimple specific parameters to the account
  215. * @param ctxt
  216. */
  217. public void applyExtraParams(Context ctxt) {
  218. // Transport
  219. String regUri = "";
  220. String argument = "";
  221. switch (transport) {
  222. case SipProfile.TRANSPORT_UDP:
  223. argument = ";transport=udp;lr";
  224. break;
  225. case SipProfile.TRANSPORT_TCP:
  226. argument = ";transport=tcp;lr";
  227. break;
  228. case SipProfile.TRANSPORT_TLS:
  229. //TODO : differentiate ssl/tls ?
  230. argument = ";transport=tls;lr";
  231. break;
  232. default:
  233. break;
  234. }
  235. if (!TextUtils.isEmpty(argument)) {
  236. regUri = PjSipService.pjStrToString(cfg.getReg_uri());
  237. if(!TextUtils.isEmpty(regUri)) {
  238. long initialProxyCnt = cfg.getProxy_cnt();
  239. pj_str_t[] proxies = cfg.getProxy();
  240. //TODO : remove lr and transport from uri
  241. // cfg.setReg_uri(pjsua.pj_str_copy(proposed_server));
  242. String firstProxy = PjSipService.pjStrToString(proxies[0]);
  243. if (initialProxyCnt == 0 || TextUtils.isEmpty(firstProxy)) {
  244. cfg.setReg_uri(pjsua.pj_str_copy(regUri + argument));
  245. cfg.setProxy_cnt(0);
  246. } else {
  247. proxies[0] = pjsua.pj_str_copy(firstProxy + argument);
  248. cfg.setProxy(proxies);
  249. }
  250. // } else {
  251. // proxies[0] = pjsua.pj_str_copy(proxies[0].getPtr() + argument);
  252. // cfg.setProxy(proxies);
  253. // }
  254. }
  255. }
  256. //Caller id
  257. PreferencesProviderWrapper prefs = new PreferencesProviderWrapper(ctxt);
  258. String defaultCallerid = prefs.getPreferenceStringValue(SipConfigManager.DEFAULT_CALLER_ID);
  259. // If one default caller is set
  260. if (!TextUtils.isEmpty(defaultCallerid)) {
  261. String accId = PjSipService.pjStrToString(cfg.getId());
  262. ParsedSipContactInfos parsedInfos = SipUri.parseSipContact(accId);
  263. if (TextUtils.isEmpty(parsedInfos.displayName)) {
  264. // Apply new display name
  265. parsedInfos.displayName = defaultCallerid;
  266. cfg.setId(pjsua.pj_str_copy(parsedInfos.toString()));
  267. }
  268. }
  269. // Keep alive
  270. cfg.setKa_interval(prefs.getUdpKeepAliveInterval());
  271. // Video
  272. if(profile_vid_auto_show >= 0) {
  273. cfg.setVid_in_auto_show((profile_vid_auto_show == 1) ? pjsuaConstants.PJ_TRUE : pjsuaConstants.PJ_FALSE);
  274. }else {
  275. cfg.setVid_in_auto_show(pjsuaConstants.PJ_TRUE);
  276. }
  277. if(profile_vid_auto_transmit >= 0) {
  278. cfg.setVid_out_auto_transmit((profile_vid_auto_transmit == 1) ? pjsuaConstants.PJ_TRUE : pjsuaConstants.PJ_FALSE);
  279. }else {
  280. cfg.setVid_out_auto_transmit(pjsuaConstants.PJ_TRUE);
  281. }
  282. // RTP cfg
  283. pjsua_transport_config rtpCfg = cfg.getRtp_cfg();
  284. if(profile_default_rtp_port) {
  285. rtpCfg.setPort(prefs.getRTPPort());
  286. }
  287. boolean hasQos = prefs.getPreferenceBooleanValue(SipConfigManager.ENABLE_QOS);
  288. if(profile_enable_qos >= 0) {
  289. hasQos = (profile_enable_qos == 1);
  290. }
  291. if(hasQos) {
  292. // TODO - video?
  293. rtpCfg.setQos_type(pj_qos_type.PJ_QOS_TYPE_VOICE);
  294. pj_qos_params qosParam = rtpCfg.getQos_params();
  295. // Default for RTP layer is different than default for SIP layer.
  296. short dscpVal = (short) prefs.getPreferenceIntegerValue(SipConfigManager.DSCP_RTP_VAL);
  297. if(profile_qos_dscp >= 0) {
  298. // If not set, we don't need to change dscp value
  299. dscpVal = (short) profile_qos_dscp;
  300. qosParam.setDscp_val(dscpVal);
  301. qosParam.setFlags((short) 1); // DSCP
  302. }
  303. }
  304. }
  305. /**
  306. * @return the displayName
  307. */
  308. public String getDisplayName() {
  309. return displayName;
  310. }
  311. @Override
  312. public boolean equals(Object o) {
  313. if(o != null && o.getClass() == PjSipAccount.class) {
  314. PjSipAccount oAccount = (PjSipAccount) o;
  315. return oAccount.id == id;
  316. }
  317. return super.equals(o);
  318. }
  319. }