/pjsip_android/apps/pjsip/project/pjsip/src/pjsua-lib/pjsua_call.c

http://csipsimple.googlecode.com/ · C · 2394 lines · 1526 code · 447 blank · 421 comment · 363 complexity · 929fb932131e7c328e17a12e1aa42097 MD5 · raw file

Large files are truncated click here to view the full file

  1. /* $Id: pjsua_call.c 3771 2011-09-22 08:13:15Z bennylp $ */
  2. /*
  3. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  4. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <pjsua-lib/pjsua.h>
  21. #include <pjsua-lib/pjsua_internal.h>
  22. #define THIS_FILE "pjsua_call.c"
  23. #if USE_CSIPSIMPLE_HACKS
  24. pj_bool_t pjsua_no_update = PJ_FALSE;
  25. #endif
  26. /* Retry interval of sending re-INVITE for locking a codec when remote
  27. * SDP answer contains multiple codec, in milliseconds.
  28. */
  29. #define LOCK_CODEC_RETRY_INTERVAL 200
  30. /*
  31. * Max UPDATE/re-INVITE retry to lock codec
  32. */
  33. #define LOCK_CODEC_MAX_RETRY 5
  34. /* This callback receives notification from invite session when the
  35. * session state has changed.
  36. */
  37. static void pjsua_call_on_state_changed(pjsip_inv_session *inv,
  38. pjsip_event *e);
  39. /* This callback is called by invite session framework when UAC session
  40. * has forked.
  41. */
  42. static void pjsua_call_on_forked( pjsip_inv_session *inv,
  43. pjsip_event *e);
  44. /*
  45. * Callback to be called when SDP offer/answer negotiation has just completed
  46. * in the session. This function will start/update media if negotiation
  47. * has succeeded.
  48. */
  49. static void pjsua_call_on_media_update(pjsip_inv_session *inv,
  50. pj_status_t status);
  51. /*
  52. * Called when session received new offer.
  53. */
  54. static void pjsua_call_on_rx_offer(pjsip_inv_session *inv,
  55. const pjmedia_sdp_session *offer);
  56. /*
  57. * Called to generate new offer.
  58. */
  59. static void pjsua_call_on_create_offer(pjsip_inv_session *inv,
  60. pjmedia_sdp_session **offer);
  61. /*
  62. * This callback is called when transaction state has changed in INVITE
  63. * session. We use this to trap:
  64. * - incoming REFER request.
  65. * - incoming MESSAGE request.
  66. */
  67. static void pjsua_call_on_tsx_state_changed(pjsip_inv_session *inv,
  68. pjsip_transaction *tsx,
  69. pjsip_event *e);
  70. /*
  71. * Redirection handler.
  72. */
  73. static pjsip_redirect_op pjsua_call_on_redirected(pjsip_inv_session *inv,
  74. const pjsip_uri *target,
  75. const pjsip_event *e);
  76. /* Create SDP for call hold. */
  77. static pj_status_t create_sdp_of_call_hold(pjsua_call *call,
  78. pjmedia_sdp_session **p_sdp);
  79. /*
  80. * Callback called by event framework when the xfer subscription state
  81. * has changed.
  82. */
  83. static void xfer_client_on_evsub_state( pjsip_evsub *sub, pjsip_event *event);
  84. static void xfer_server_on_evsub_state( pjsip_evsub *sub, pjsip_event *event);
  85. /*
  86. * Reset call descriptor.
  87. */
  88. static void reset_call(pjsua_call_id id)
  89. {
  90. pjsua_call *call = &pjsua_var.calls[id];
  91. call->index = id;
  92. call->inv = NULL;
  93. call->user_data = NULL;
  94. call->session = NULL;
  95. call->audio_idx = -1;
  96. call->ssrc = pj_rand();
  97. call->rtp_tx_seq = 0;
  98. call->rtp_tx_ts = 0;
  99. call->rtp_tx_seq_ts_set = 0;
  100. call->xfer_sub = NULL;
  101. call->last_code = (pjsip_status_code) 0;
  102. call->conf_slot = PJSUA_INVALID_ID;
  103. call->last_text.ptr = call->last_text_buf_;
  104. call->last_text.slen = 0;
  105. call->conn_time.sec = 0;
  106. call->conn_time.msec = 0;
  107. call->res_time.sec = 0;
  108. call->res_time.msec = 0;
  109. call->rem_nat_type = PJ_STUN_NAT_TYPE_UNKNOWN;
  110. call->rem_srtp_use = PJMEDIA_SRTP_DISABLED;
  111. call->local_hold = PJ_FALSE;
  112. pj_bzero(&call->lock_codec, sizeof(call->lock_codec));
  113. }
  114. /*
  115. * Init call subsystem.
  116. */
  117. pj_status_t pjsua_call_subsys_init(const pjsua_config *cfg)
  118. {
  119. pjsip_inv_callback inv_cb;
  120. unsigned i;
  121. const pj_str_t str_norefersub = { "norefersub", 10 };
  122. pj_status_t status;
  123. /* Init calls array. */
  124. for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.calls); ++i)
  125. reset_call(i);
  126. /* Copy config */
  127. pjsua_config_dup(pjsua_var.pool, &pjsua_var.ua_cfg, cfg);
  128. /* Verify settings */
  129. if (pjsua_var.ua_cfg.max_calls >= PJSUA_MAX_CALLS) {
  130. pjsua_var.ua_cfg.max_calls = PJSUA_MAX_CALLS;
  131. }
  132. /* Check the route URI's and force loose route if required */
  133. for (i=0; i<pjsua_var.ua_cfg.outbound_proxy_cnt; ++i) {
  134. status = normalize_route_uri(pjsua_var.pool,
  135. &pjsua_var.ua_cfg.outbound_proxy[i]);
  136. if (status != PJ_SUCCESS)
  137. return status;
  138. }
  139. /* Initialize invite session callback. */
  140. pj_bzero(&inv_cb, sizeof(inv_cb));
  141. inv_cb.on_state_changed = &pjsua_call_on_state_changed;
  142. inv_cb.on_new_session = &pjsua_call_on_forked;
  143. inv_cb.on_media_update = &pjsua_call_on_media_update;
  144. inv_cb.on_rx_offer = &pjsua_call_on_rx_offer;
  145. inv_cb.on_create_offer = &pjsua_call_on_create_offer;
  146. inv_cb.on_tsx_state_changed = &pjsua_call_on_tsx_state_changed;
  147. inv_cb.on_redirected = &pjsua_call_on_redirected;
  148. /* Initialize invite session module: */
  149. status = pjsip_inv_usage_init(pjsua_var.endpt, &inv_cb);
  150. PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
  151. /* Add "norefersub" in Supported header */
  152. pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_SUPPORTED,
  153. NULL, 1, &str_norefersub);
  154. return status;
  155. }
  156. /*
  157. * Start call subsystem.
  158. */
  159. pj_status_t pjsua_call_subsys_start(void)
  160. {
  161. /* Nothing to do */
  162. return PJ_SUCCESS;
  163. }
  164. /*
  165. * Get maximum number of calls configured in pjsua.
  166. */
  167. PJ_DEF(unsigned) pjsua_call_get_max_count(void)
  168. {
  169. return pjsua_var.ua_cfg.max_calls;
  170. }
  171. /*
  172. * Get number of currently active calls.
  173. */
  174. PJ_DEF(unsigned) pjsua_call_get_count(void)
  175. {
  176. return pjsua_var.call_cnt;
  177. }
  178. /*
  179. * Enum calls.
  180. */
  181. PJ_DEF(pj_status_t) pjsua_enum_calls( pjsua_call_id ids[],
  182. unsigned *count)
  183. {
  184. unsigned i, c;
  185. PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
  186. PJSUA_LOCK();
  187. for (i=0, c=0; c<*count && i<pjsua_var.ua_cfg.max_calls; ++i) {
  188. if (!pjsua_var.calls[i].inv)
  189. continue;
  190. ids[c] = i;
  191. ++c;
  192. }
  193. *count = c;
  194. PJSUA_UNLOCK();
  195. return PJ_SUCCESS;
  196. }
  197. /* Allocate one call id */
  198. static pjsua_call_id alloc_call_id(void)
  199. {
  200. pjsua_call_id cid;
  201. #if 1
  202. /* New algorithm: round-robin */
  203. if (pjsua_var.next_call_id >= (int)pjsua_var.ua_cfg.max_calls ||
  204. pjsua_var.next_call_id < 0)
  205. {
  206. pjsua_var.next_call_id = 0;
  207. }
  208. for (cid=pjsua_var.next_call_id;
  209. cid<(int)pjsua_var.ua_cfg.max_calls;
  210. ++cid)
  211. {
  212. if (pjsua_var.calls[cid].inv == NULL) {
  213. ++pjsua_var.next_call_id;
  214. return cid;
  215. }
  216. }
  217. for (cid=0; cid < pjsua_var.next_call_id; ++cid) {
  218. if (pjsua_var.calls[cid].inv == NULL) {
  219. ++pjsua_var.next_call_id;
  220. return cid;
  221. }
  222. }
  223. #else
  224. /* Old algorithm */
  225. for (cid=0; cid<(int)pjsua_var.ua_cfg.max_calls; ++cid) {
  226. if (pjsua_var.calls[cid].inv == NULL)
  227. return cid;
  228. }
  229. #endif
  230. return PJSUA_INVALID_ID;
  231. }
  232. /* Get signaling secure level.
  233. * Return:
  234. * 0: if signaling is not secure
  235. * 1: if TLS transport is used for immediate hop
  236. * 2: if end-to-end signaling is secure.
  237. */
  238. static int get_secure_level(pjsua_acc_id acc_id, const pj_str_t *dst_uri)
  239. {
  240. const pj_str_t tls = pj_str(";transport=tls");
  241. const pj_str_t sips = pj_str("sips:");
  242. pjsua_acc *acc = &pjsua_var.acc[acc_id];
  243. if (pj_stristr(dst_uri, &sips))
  244. return 2;
  245. if (!pj_list_empty(&acc->route_set)) {
  246. pjsip_route_hdr *r = acc->route_set.next;
  247. pjsip_uri *uri = r->name_addr.uri;
  248. pjsip_sip_uri *sip_uri;
  249. sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
  250. if (pj_stricmp2(&sip_uri->transport_param, "tls")==0)
  251. return 1;
  252. } else {
  253. if (pj_stristr(dst_uri, &tls))
  254. return 1;
  255. }
  256. return 0;
  257. }
  258. /*
  259. static int call_get_secure_level(pjsua_call *call)
  260. {
  261. if (call->inv->dlg->secure)
  262. return 2;
  263. if (!pj_list_empty(&call->inv->dlg->route_set)) {
  264. pjsip_route_hdr *r = call->inv->dlg->route_set.next;
  265. pjsip_uri *uri = r->name_addr.uri;
  266. pjsip_sip_uri *sip_uri;
  267. sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
  268. if (pj_stricmp2(&sip_uri->transport_param, "tls")==0)
  269. return 1;
  270. } else {
  271. pjsip_sip_uri *sip_uri;
  272. if (PJSIP_URI_SCHEME_IS_SIPS(call->inv->dlg->target))
  273. return 2;
  274. if (!PJSIP_URI_SCHEME_IS_SIP(call->inv->dlg->target))
  275. return 0;
  276. sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(call->inv->dlg->target);
  277. if (pj_stricmp2(&sip_uri->transport_param, "tls")==0)
  278. return 1;
  279. }
  280. return 0;
  281. }
  282. */
  283. /*
  284. * Make outgoing call to the specified URI using the specified account.
  285. */
  286. PJ_DEF(pj_status_t) pjsua_call_make_call( pjsua_acc_id acc_id,
  287. const pj_str_t *dest_uri,
  288. unsigned options,
  289. void *user_data,
  290. const pjsua_msg_data *msg_data,
  291. pjsua_call_id *p_call_id)
  292. {
  293. pj_pool_t *tmp_pool;
  294. pjsip_dialog *dlg = NULL;
  295. pjmedia_sdp_session *offer;
  296. pjsip_inv_session *inv = NULL;
  297. pjsua_acc *acc;
  298. pjsua_call *call;
  299. int call_id = -1;
  300. pj_str_t contact;
  301. pjsip_tx_data *tdata;
  302. pj_status_t status;
  303. /* Check that account is valid */
  304. PJ_ASSERT_RETURN(acc_id>=0 || acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
  305. PJ_EINVAL);
  306. /* Check arguments */
  307. PJ_ASSERT_RETURN(dest_uri, PJ_EINVAL);
  308. PJSUA_LOCK();
  309. /* Create sound port if none is instantiated, to check if sound device
  310. * can be used. But only do this with the conference bridge, as with
  311. * audio switchboard (i.e. APS-Direct), we can only open the sound
  312. * device once the correct format has been known
  313. */
  314. if (!pjsua_var.is_mswitch && pjsua_var.snd_port==NULL &&
  315. pjsua_var.null_snd==NULL && !pjsua_var.no_snd)
  316. {
  317. pj_status_t status;
  318. status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
  319. if (status != PJ_SUCCESS) {
  320. PJSUA_UNLOCK();
  321. return status;
  322. }
  323. }
  324. acc = &pjsua_var.acc[acc_id];
  325. if (!acc->valid) {
  326. pjsua_perror(THIS_FILE, "Unable to make call because account "
  327. "is not valid", PJ_EINVALIDOP);
  328. PJSUA_UNLOCK();
  329. return PJ_EINVALIDOP;
  330. }
  331. /* Find free call slot. */
  332. call_id = alloc_call_id();
  333. if (call_id == PJSUA_INVALID_ID) {
  334. pjsua_perror(THIS_FILE, "Error making call", PJ_ETOOMANY);
  335. PJSUA_UNLOCK();
  336. return PJ_ETOOMANY;
  337. }
  338. call = &pjsua_var.calls[call_id];
  339. /* Associate session with account */
  340. call->acc_id = acc_id;
  341. call->call_hold_type = acc->cfg.call_hold_type;
  342. /* Create temporary pool */
  343. tmp_pool = pjsua_pool_create("tmpcall10", 512, 256);
  344. /* Verify that destination URI is valid before calling
  345. * pjsua_acc_create_uac_contact, or otherwise there
  346. * a misleading "Invalid Contact URI" error will be printed
  347. * when pjsua_acc_create_uac_contact() fails.
  348. */
  349. if (1) {
  350. pjsip_uri *uri;
  351. pj_str_t dup;
  352. pj_strdup_with_null(tmp_pool, &dup, dest_uri);
  353. uri = pjsip_parse_uri(tmp_pool, dup.ptr, dup.slen, 0);
  354. if (uri == NULL) {
  355. pjsua_perror(THIS_FILE, "Unable to make call",
  356. PJSIP_EINVALIDREQURI);
  357. pj_pool_release(tmp_pool);
  358. PJSUA_UNLOCK();
  359. return PJSIP_EINVALIDREQURI;
  360. }
  361. }
  362. PJ_LOG(4,(THIS_FILE, "Making call with acc #%d to %.*s", acc_id,
  363. (int)dest_uri->slen, dest_uri->ptr));
  364. /* Mark call start time. */
  365. pj_gettimeofday(&call->start_time);
  366. /* Reset first response time */
  367. call->res_time.sec = 0;
  368. /* Create suitable Contact header unless a Contact header has been
  369. * set in the account.
  370. */
  371. if (acc->contact.slen) {
  372. contact = acc->contact;
  373. } else {
  374. status = pjsua_acc_create_uac_contact(tmp_pool, &contact,
  375. acc_id, dest_uri);
  376. if (status != PJ_SUCCESS) {
  377. pjsua_perror(THIS_FILE, "Unable to generate Contact header",
  378. status);
  379. pj_pool_release(tmp_pool);
  380. PJSUA_UNLOCK();
  381. return status;
  382. }
  383. }
  384. /* Create outgoing dialog: */
  385. status = pjsip_dlg_create_uac( pjsip_ua_instance(),
  386. &acc->cfg.id, &contact,
  387. dest_uri, dest_uri, &dlg);
  388. if (status != PJ_SUCCESS) {
  389. pjsua_perror(THIS_FILE, "Dialog creation failed", status);
  390. pj_pool_release(tmp_pool);
  391. PJSUA_UNLOCK();
  392. return status;
  393. }
  394. /* Increment the dialog's lock otherwise when invite session creation
  395. * fails the dialog will be destroyed prematurely.
  396. */
  397. pjsip_dlg_inc_lock(dlg);
  398. /* Calculate call's secure level */
  399. call->secure_level = get_secure_level(acc_id, dest_uri);
  400. /* Init media channel */
  401. status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC,
  402. call->secure_level, dlg->pool,
  403. NULL, NULL);
  404. if (status != PJ_SUCCESS) {
  405. pjsua_perror(THIS_FILE, "Error initializing media channel", status);
  406. goto on_error;
  407. }
  408. /* Create offer */
  409. status = pjsua_media_channel_create_sdp(call->index, dlg->pool, NULL,
  410. &offer, NULL);
  411. if (status != PJ_SUCCESS) {
  412. pjsua_perror(THIS_FILE, "Error initializing media channel", status);
  413. goto on_error;
  414. }
  415. /* Create the INVITE session: */
  416. options |= PJSIP_INV_SUPPORT_100REL;
  417. if (acc->cfg.require_100rel == PJSUA_100REL_MANDATORY)
  418. options |= PJSIP_INV_REQUIRE_100REL;
  419. if (acc->cfg.use_timer != PJSUA_SIP_TIMER_INACTIVE) {
  420. options |= PJSIP_INV_SUPPORT_TIMER;
  421. if (acc->cfg.use_timer == PJSUA_SIP_TIMER_REQUIRED)
  422. options |= PJSIP_INV_REQUIRE_TIMER;
  423. else if (acc->cfg.use_timer == PJSUA_SIP_TIMER_ALWAYS)
  424. options |= PJSIP_INV_ALWAYS_USE_TIMER;
  425. }
  426. status = pjsip_inv_create_uac( dlg, offer, options, &inv);
  427. if (status != PJ_SUCCESS) {
  428. pjsua_perror(THIS_FILE, "Invite session creation failed", status);
  429. goto on_error;
  430. }
  431. /* Init Session Timers */
  432. status = pjsip_timer_init_session(inv, &acc->cfg.timer_setting);
  433. if (status != PJ_SUCCESS) {
  434. pjsua_perror(THIS_FILE, "Session Timer init failed", status);
  435. goto on_error;
  436. }
  437. /* Create and associate our data in the session. */
  438. call->inv = inv;
  439. dlg->mod_data[pjsua_var.mod.id] = call;
  440. inv->mod_data[pjsua_var.mod.id] = call;
  441. /* Attach user data */
  442. call->user_data = user_data;
  443. /* If account is locked to specific transport, then lock dialog
  444. * to this transport too.
  445. */
  446. if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
  447. pjsip_tpselector tp_sel;
  448. pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
  449. pjsip_dlg_set_transport(dlg, &tp_sel);
  450. }
  451. /* Set dialog Route-Set: */
  452. if (!pj_list_empty(&acc->route_set))
  453. pjsip_dlg_set_route_set(dlg, &acc->route_set);
  454. /* Set credentials: */
  455. if (acc->cred_cnt) {
  456. pjsip_auth_clt_set_credentials( &dlg->auth_sess,
  457. acc->cred_cnt, acc->cred);
  458. }
  459. /* Set authentication preference */
  460. pjsip_auth_clt_set_prefs(&dlg->auth_sess, &acc->cfg.auth_pref);
  461. /* Create initial INVITE: */
  462. status = pjsip_inv_invite(inv, &tdata);
  463. if (status != PJ_SUCCESS) {
  464. pjsua_perror(THIS_FILE, "Unable to create initial INVITE request",
  465. status);
  466. goto on_error;
  467. }
  468. /* Add additional headers etc */
  469. pjsua_process_msg_data( tdata, msg_data);
  470. /* Must increment call counter now */
  471. ++pjsua_var.call_cnt;
  472. /* Send initial INVITE: */
  473. status = pjsip_inv_send_msg(inv, tdata);
  474. if (status != PJ_SUCCESS) {
  475. pjsua_perror(THIS_FILE, "Unable to send initial INVITE request",
  476. status);
  477. /* Upon failure to send first request, the invite
  478. * session would have been cleared.
  479. */
  480. inv = NULL;
  481. goto on_error;
  482. }
  483. /* Done. */
  484. if (p_call_id)
  485. *p_call_id = call_id;
  486. pjsip_dlg_dec_lock(dlg);
  487. pj_pool_release(tmp_pool);
  488. PJSUA_UNLOCK();
  489. return PJ_SUCCESS;
  490. on_error:
  491. if (dlg) {
  492. /* This may destroy the dialog */
  493. pjsip_dlg_dec_lock(dlg);
  494. }
  495. if (inv != NULL) {
  496. pjsip_inv_terminate(inv, PJSIP_SC_OK, PJ_FALSE);
  497. }
  498. if (call_id != -1) {
  499. reset_call(call_id);
  500. pjsua_media_channel_deinit(call_id);
  501. }
  502. pj_pool_release(tmp_pool);
  503. PJSUA_UNLOCK();
  504. return status;
  505. }
  506. /* Get the NAT type information in remote's SDP */
  507. static void update_remote_nat_type(pjsua_call *call,
  508. const pjmedia_sdp_session *sdp)
  509. {
  510. const pjmedia_sdp_attr *xnat;
  511. xnat = pjmedia_sdp_attr_find2(sdp->attr_count, sdp->attr, "X-nat", NULL);
  512. if (xnat) {
  513. call->rem_nat_type = (pj_stun_nat_type) (xnat->value.ptr[0] - '0');
  514. } else {
  515. call->rem_nat_type = PJ_STUN_NAT_TYPE_UNKNOWN;
  516. }
  517. PJ_LOG(5,(THIS_FILE, "Call %d: remote NAT type is %d (%s)", call->index,
  518. call->rem_nat_type, pj_stun_get_nat_name(call->rem_nat_type)));
  519. }
  520. /**
  521. * Handle incoming INVITE request.
  522. * Called by pjsua_core.c
  523. */
  524. pj_bool_t pjsua_call_on_incoming(pjsip_rx_data *rdata)
  525. {
  526. pj_str_t contact;
  527. pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata);
  528. pjsip_dialog *replaced_dlg = NULL;
  529. pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
  530. pjsip_msg *msg = rdata->msg_info.msg;
  531. pjsip_tx_data *response = NULL;
  532. unsigned options = 0;
  533. pjsip_inv_session *inv = NULL;
  534. int acc_id;
  535. pjsua_call *call;
  536. int call_id = -1;
  537. int sip_err_code;
  538. pjmedia_sdp_session *offer=NULL, *answer;
  539. pj_status_t status;
  540. /* Don't want to handle anything but INVITE */
  541. if (msg->line.req.method.id != PJSIP_INVITE_METHOD)
  542. return PJ_FALSE;
  543. /* Don't want to handle anything that's already associated with
  544. * existing dialog or transaction.
  545. */
  546. if (dlg || tsx)
  547. return PJ_FALSE;
  548. /* Don't want to accept the call if shutdown is in progress */
  549. if (pjsua_var.thread_quit_flag) {
  550. pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata,
  551. PJSIP_SC_TEMPORARILY_UNAVAILABLE, NULL,
  552. NULL, NULL);
  553. return PJ_TRUE;
  554. }
  555. PJSUA_LOCK();
  556. /* Find free call slot. */
  557. call_id = alloc_call_id();
  558. if (call_id == PJSUA_INVALID_ID) {
  559. pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata,
  560. PJSIP_SC_BUSY_HERE, NULL,
  561. NULL, NULL);
  562. PJ_LOG(2,(THIS_FILE,
  563. "Unable to accept incoming call (too many calls)"));
  564. PJSUA_UNLOCK();
  565. return PJ_TRUE;
  566. }
  567. /* Clear call descriptor */
  568. reset_call(call_id);
  569. call = &pjsua_var.calls[call_id];
  570. /* Mark call start time. */
  571. pj_gettimeofday(&call->start_time);
  572. /* Check INVITE request for Replaces header. If Replaces header is
  573. * present, the function will make sure that we can handle the request.
  574. */
  575. status = pjsip_replaces_verify_request(rdata, &replaced_dlg, PJ_FALSE,
  576. &response);
  577. if (status != PJ_SUCCESS) {
  578. /*
  579. * Something wrong with the Replaces header.
  580. */
  581. if (response) {
  582. pjsip_response_addr res_addr;
  583. pjsip_get_response_addr(response->pool, rdata, &res_addr);
  584. pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, response,
  585. NULL, NULL);
  586. } else {
  587. /* Respond with 500 (Internal Server Error) */
  588. pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL,
  589. NULL, NULL);
  590. }
  591. PJSUA_UNLOCK();
  592. return PJ_TRUE;
  593. }
  594. /* If this INVITE request contains Replaces header, notify application
  595. * about the request so that application can do subsequent checking
  596. * if it wants to.
  597. */
  598. if (replaced_dlg != NULL && pjsua_var.ua_cfg.cb.on_call_replace_request) {
  599. pjsua_call *replaced_call;
  600. int st_code = 200;
  601. pj_str_t st_text = { "OK", 2 };
  602. /* Get the replaced call instance */
  603. replaced_call = (pjsua_call*) replaced_dlg->mod_data[pjsua_var.mod.id];
  604. /* Notify application */
  605. pjsua_var.ua_cfg.cb.on_call_replace_request(replaced_call->index,
  606. rdata, &st_code, &st_text);
  607. /* Must specify final response */
  608. PJ_ASSERT_ON_FAIL(st_code >= 200, st_code = 200);
  609. /* Check if application rejects this request. */
  610. if (st_code >= 300) {
  611. if (st_text.slen == 2)
  612. st_text = *pjsip_get_status_text(st_code);
  613. pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata,
  614. st_code, &st_text, NULL, NULL, NULL);
  615. PJSUA_UNLOCK();
  616. return PJ_TRUE;
  617. }
  618. }
  619. /*
  620. * Get which account is most likely to be associated with this incoming
  621. * call. We need the account to find which contact URI to put for
  622. * the call.
  623. */
  624. acc_id = call->acc_id = pjsua_acc_find_for_incoming(rdata);
  625. call->call_hold_type = pjsua_var.acc[acc_id].cfg.call_hold_type;
  626. /* Get call's secure level */
  627. if (PJSIP_URI_SCHEME_IS_SIPS(rdata->msg_info.msg->line.req.uri))
  628. call->secure_level = 2;
  629. else if (PJSIP_TRANSPORT_IS_SECURE(rdata->tp_info.transport))
  630. call->secure_level = 1;
  631. else
  632. call->secure_level = 0;
  633. /* Parse SDP from incoming request */
  634. if (rdata->msg_info.msg->body) {
  635. pjsip_rdata_sdp_info *sdp_info;
  636. sdp_info = pjsip_rdata_get_sdp_info(rdata);
  637. offer = sdp_info->sdp;
  638. status = sdp_info->sdp_err;
  639. if (status==PJ_SUCCESS && sdp_info->sdp==NULL)
  640. status = PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE);
  641. if (status != PJ_SUCCESS) {
  642. const pj_str_t reason = pj_str("Bad SDP");
  643. pjsip_hdr hdr_list;
  644. pjsip_warning_hdr *w;
  645. pjsua_perror(THIS_FILE, "Bad SDP in incoming INVITE",
  646. status);
  647. w = pjsip_warning_hdr_create_from_status(rdata->tp_info.pool,
  648. pjsip_endpt_name(pjsua_var.endpt),
  649. status);
  650. pj_list_init(&hdr_list);
  651. pj_list_push_back(&hdr_list, w);
  652. pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, 400,
  653. &reason, &hdr_list, NULL, NULL);
  654. PJSUA_UNLOCK();
  655. return PJ_TRUE;
  656. }
  657. /* Do quick checks on SDP before passing it to transports. More elabore
  658. * checks will be done in pjsip_inv_verify_request2() below.
  659. */
  660. if (offer->media_count==0) {
  661. const pj_str_t reason = pj_str("Missing media in SDP");
  662. pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, 400, &reason,
  663. NULL, NULL, NULL);
  664. PJSUA_UNLOCK();
  665. return PJ_TRUE;
  666. }
  667. } else {
  668. offer = NULL;
  669. }
  670. /* Init media channel */
  671. status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAS,
  672. call->secure_level,
  673. rdata->tp_info.pool, offer,
  674. &sip_err_code);
  675. if (status != PJ_SUCCESS) {
  676. pjsua_perror(THIS_FILE, "Error initializing media channel", status);
  677. pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata,
  678. sip_err_code, NULL, NULL, NULL, NULL);
  679. PJSUA_UNLOCK();
  680. return PJ_TRUE;
  681. }
  682. /* Create answer */
  683. status = pjsua_media_channel_create_sdp(call->index, rdata->tp_info.pool,
  684. offer, &answer, &sip_err_code);
  685. if (status != PJ_SUCCESS) {
  686. pjsua_perror(THIS_FILE, "Error creating SDP answer", status);
  687. pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata,
  688. sip_err_code, NULL, NULL, NULL, NULL);
  689. PJSUA_UNLOCK();
  690. return PJ_TRUE;
  691. }
  692. /* Verify that we can handle the request. */
  693. options |= PJSIP_INV_SUPPORT_100REL;
  694. options |= PJSIP_INV_SUPPORT_TIMER;
  695. if (pjsua_var.acc[acc_id].cfg.require_100rel == PJSUA_100REL_MANDATORY)
  696. options |= PJSIP_INV_REQUIRE_100REL;
  697. if (pjsua_var.media_cfg.enable_ice)
  698. options |= PJSIP_INV_SUPPORT_ICE;
  699. if (pjsua_var.acc[acc_id].cfg.use_timer == PJSUA_SIP_TIMER_REQUIRED)
  700. options |= PJSIP_INV_REQUIRE_TIMER;
  701. else if (pjsua_var.acc[acc_id].cfg.use_timer == PJSUA_SIP_TIMER_ALWAYS)
  702. options |= PJSIP_INV_ALWAYS_USE_TIMER;
  703. status = pjsip_inv_verify_request2(rdata, &options, offer, answer, NULL,
  704. pjsua_var.endpt, &response);
  705. if (status != PJ_SUCCESS) {
  706. /*
  707. * No we can't handle the incoming INVITE request.
  708. */
  709. if (response) {
  710. pjsip_response_addr res_addr;
  711. pjsip_get_response_addr(response->pool, rdata, &res_addr);
  712. pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, response,
  713. NULL, NULL);
  714. } else {
  715. /* Respond with 500 (Internal Server Error) */
  716. pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, 500, NULL,
  717. NULL, NULL, NULL);
  718. }
  719. pjsua_media_channel_deinit(call->index);
  720. PJSUA_UNLOCK();
  721. return PJ_TRUE;
  722. }
  723. /* Get suitable Contact header */
  724. if (pjsua_var.acc[acc_id].contact.slen) {
  725. contact = pjsua_var.acc[acc_id].contact;
  726. } else {
  727. status = pjsua_acc_create_uas_contact(rdata->tp_info.pool, &contact,
  728. acc_id, rdata);
  729. if (status != PJ_SUCCESS) {
  730. pjsua_perror(THIS_FILE, "Unable to generate Contact header",
  731. status);
  732. pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL,
  733. NULL, NULL);
  734. pjsua_media_channel_deinit(call->index);
  735. PJSUA_UNLOCK();
  736. return PJ_TRUE;
  737. }
  738. }
  739. /* Create dialog: */
  740. status = pjsip_dlg_create_uas( pjsip_ua_instance(), rdata,
  741. &contact, &dlg);
  742. if (status != PJ_SUCCESS) {
  743. pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL,
  744. NULL, NULL);
  745. pjsua_media_channel_deinit(call->index);
  746. PJSUA_UNLOCK();
  747. return PJ_TRUE;
  748. }
  749. /* Set credentials */
  750. if (pjsua_var.acc[acc_id].cred_cnt) {
  751. pjsip_auth_clt_set_credentials(&dlg->auth_sess,
  752. pjsua_var.acc[acc_id].cred_cnt,
  753. pjsua_var.acc[acc_id].cred);
  754. }
  755. /* Set preference */
  756. pjsip_auth_clt_set_prefs(&dlg->auth_sess,
  757. &pjsua_var.acc[acc_id].cfg.auth_pref);
  758. /* Disable Session Timers if not prefered and the incoming INVITE request
  759. * did not require it.
  760. */
  761. if (pjsua_var.acc[acc_id].cfg.use_timer == PJSUA_SIP_TIMER_INACTIVE &&
  762. (options & PJSIP_INV_REQUIRE_TIMER) == 0)
  763. {
  764. options &= ~(PJSIP_INV_SUPPORT_TIMER);
  765. }
  766. /* If 100rel is optional and UAC supports it, use it. */
  767. if ((options & PJSIP_INV_REQUIRE_100REL)==0 &&
  768. pjsua_var.acc[acc_id].cfg.require_100rel == PJSUA_100REL_OPTIONAL)
  769. {
  770. const pj_str_t token = { "100rel", 6};
  771. pjsip_dialog_cap_status cap_status;
  772. cap_status = pjsip_dlg_remote_has_cap(dlg, PJSIP_H_SUPPORTED, NULL,
  773. &token);
  774. if (cap_status == PJSIP_DIALOG_CAP_SUPPORTED)
  775. options |= PJSIP_INV_REQUIRE_100REL;
  776. }
  777. /* Create invite session: */
  778. status = pjsip_inv_create_uas( dlg, rdata, answer, options, &inv);
  779. if (status != PJ_SUCCESS) {
  780. pjsip_hdr hdr_list;
  781. pjsip_warning_hdr *w;
  782. w = pjsip_warning_hdr_create_from_status(dlg->pool,
  783. pjsip_endpt_name(pjsua_var.endpt),
  784. status);
  785. pj_list_init(&hdr_list);
  786. pj_list_push_back(&hdr_list, w);
  787. pjsip_dlg_respond(dlg, rdata, 500, NULL, &hdr_list, NULL);
  788. /* Can't terminate dialog because transaction is in progress.
  789. pjsip_dlg_terminate(dlg);
  790. */
  791. pjsua_media_channel_deinit(call->index);
  792. PJSUA_UNLOCK();
  793. return PJ_TRUE;
  794. }
  795. /* Init Session Timers */
  796. status = pjsip_timer_init_session(inv,
  797. &pjsua_var.acc[acc_id].cfg.timer_setting);
  798. if (status != PJ_SUCCESS) {
  799. pjsua_perror(THIS_FILE, "Session Timer init failed", status);
  800. status = pjsip_inv_end_session(inv, PJSIP_SC_INTERNAL_SERVER_ERROR,
  801. NULL, &response);
  802. if (status == PJ_SUCCESS && response)
  803. status = pjsip_inv_send_msg(inv, response);
  804. pjsua_media_channel_deinit(call->index);
  805. PJSUA_UNLOCK();
  806. return PJ_TRUE;
  807. }
  808. /* Update NAT type of remote endpoint, only when there is SDP in
  809. * incoming INVITE!
  810. */
  811. if (pjsua_var.ua_cfg.nat_type_in_sdp &&
  812. pjmedia_sdp_neg_get_state(inv->neg) > PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER)
  813. {
  814. const pjmedia_sdp_session *remote_sdp;
  815. if (pjmedia_sdp_neg_get_neg_remote(inv->neg, &remote_sdp)==PJ_SUCCESS)
  816. update_remote_nat_type(call, remote_sdp);
  817. }
  818. /* If account is locked to specific transport, then lock dialog
  819. * to this transport too.
  820. */
  821. if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
  822. pjsip_tpselector tp_sel;
  823. pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
  824. pjsip_dlg_set_transport(dlg, &tp_sel);
  825. }
  826. /* Must answer with some response to initial INVITE. We'll do this before
  827. * attaching the call to the invite session/dialog, so that the application
  828. * will not get notification about this event (on another scenario, it is
  829. * also possible that inv_send_msg() fails and causes the invite session to
  830. * be disconnected. If we have the call attached at this time, this will
  831. * cause the disconnection callback to be called before on_incoming_call()
  832. * callback is called, which is not right).
  833. */
  834. status = pjsip_inv_initial_answer(inv, rdata,
  835. 100, NULL, NULL, &response);
  836. if (status != PJ_SUCCESS) {
  837. if (response == NULL) {
  838. pjsua_perror(THIS_FILE, "Unable to send answer to incoming INVITE",
  839. status);
  840. pjsip_dlg_respond(dlg, rdata, 500, NULL, NULL, NULL);
  841. pjsip_inv_terminate(inv, 500, PJ_FALSE);
  842. } else {
  843. pjsip_inv_send_msg(inv, response);
  844. pjsip_inv_terminate(inv, response->msg->line.status.code,
  845. PJ_FALSE);
  846. }
  847. pjsua_media_channel_deinit(call->index);
  848. PJSUA_UNLOCK();
  849. return PJ_TRUE;
  850. } else {
  851. status = pjsip_inv_send_msg(inv, response);
  852. if (status != PJ_SUCCESS) {
  853. pjsua_perror(THIS_FILE, "Unable to send 100 response", status);
  854. PJSUA_UNLOCK();
  855. return PJ_TRUE;
  856. }
  857. }
  858. /* Create and attach pjsua_var data to the dialog: */
  859. call->inv = inv;
  860. dlg->mod_data[pjsua_var.mod.id] = call;
  861. inv->mod_data[pjsua_var.mod.id] = call;
  862. ++pjsua_var.call_cnt;
  863. /* Check if this request should replace existing call */
  864. if (replaced_dlg) {
  865. pjsip_inv_session *replaced_inv;
  866. struct pjsua_call *replaced_call;
  867. pjsip_tx_data *tdata;
  868. /* Get the invite session in the dialog */
  869. replaced_inv = pjsip_dlg_get_inv_session(replaced_dlg);
  870. /* Get the replaced call instance */
  871. replaced_call = (pjsua_call*) replaced_dlg->mod_data[pjsua_var.mod.id];
  872. /* Notify application */
  873. if (pjsua_var.ua_cfg.cb.on_call_replaced)
  874. pjsua_var.ua_cfg.cb.on_call_replaced(replaced_call->index,
  875. call_id);
  876. PJ_LOG(4,(THIS_FILE, "Answering replacement call %d with 200/OK",
  877. call_id));
  878. /* Answer the new call with 200 response */
  879. status = pjsip_inv_answer(inv, 200, NULL, NULL, &tdata);
  880. if (status == PJ_SUCCESS)
  881. status = pjsip_inv_send_msg(inv, tdata);
  882. if (status != PJ_SUCCESS)
  883. pjsua_perror(THIS_FILE, "Error answering session", status);
  884. /* Note that inv may be invalid if 200/OK has caused error in
  885. * starting the media.
  886. */
  887. PJ_LOG(4,(THIS_FILE, "Disconnecting replaced call %d",
  888. replaced_call->index));
  889. /* Disconnect replaced invite session */
  890. status = pjsip_inv_end_session(replaced_inv, PJSIP_SC_GONE, NULL,
  891. &tdata);
  892. if (status == PJ_SUCCESS && tdata)
  893. status = pjsip_inv_send_msg(replaced_inv, tdata);
  894. if (status != PJ_SUCCESS)
  895. pjsua_perror(THIS_FILE, "Error terminating session", status);
  896. } else {
  897. /* Notify application if on_incoming_call() is overriden,
  898. * otherwise hangup the call with 480
  899. */
  900. if (pjsua_var.ua_cfg.cb.on_incoming_call) {
  901. pjsua_var.ua_cfg.cb.on_incoming_call(acc_id, call_id, rdata);
  902. } else {
  903. pjsua_call_hangup(call_id, PJSIP_SC_TEMPORARILY_UNAVAILABLE,
  904. NULL, NULL);
  905. }
  906. }
  907. /* This INVITE request has been handled. */
  908. PJSUA_UNLOCK();
  909. return PJ_TRUE;
  910. }
  911. /*
  912. * Check if the specified call has active INVITE session and the INVITE
  913. * session has not been disconnected.
  914. */
  915. PJ_DEF(pj_bool_t) pjsua_call_is_active(pjsua_call_id call_id)
  916. {
  917. PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
  918. PJ_EINVAL);
  919. return pjsua_var.calls[call_id].inv != NULL &&
  920. pjsua_var.calls[call_id].inv->state != PJSIP_INV_STATE_DISCONNECTED;
  921. }
  922. /*
  923. * Check if call has an active media session.
  924. */
  925. PJ_DEF(pj_bool_t) pjsua_call_has_media(pjsua_call_id call_id)
  926. {
  927. PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
  928. PJ_EINVAL);
  929. return pjsua_var.calls[call_id].session != NULL;
  930. }
  931. /*
  932. * Retrieve the media session associated with this call.
  933. */
  934. PJ_DEF(pjmedia_session*) pjsua_call_get_media_session(pjsua_call_id call_id)
  935. {
  936. PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
  937. NULL);
  938. return pjsua_var.calls[call_id].session;
  939. }
  940. /*
  941. * Retrieve the media transport instance that is used for this call.
  942. */
  943. PJ_DEF(pjmedia_transport*) pjsua_call_get_media_transport(pjsua_call_id cid)
  944. {
  945. PJ_ASSERT_RETURN(cid>=0 && cid<(int)pjsua_var.ua_cfg.max_calls,
  946. NULL);
  947. return pjsua_var.calls[cid].med_tp;
  948. }
  949. /* Acquire lock to the specified call_id */
  950. pj_status_t acquire_call(const char *title,
  951. pjsua_call_id call_id,
  952. pjsua_call **p_call,
  953. pjsip_dialog **p_dlg)
  954. {
  955. unsigned retry;
  956. pjsua_call *call = NULL;
  957. pj_bool_t has_pjsua_lock = PJ_FALSE;
  958. pj_status_t status = PJ_SUCCESS;
  959. pj_time_val time_start, timeout;
  960. pj_gettimeofday(&time_start);
  961. timeout.sec = 0;
  962. timeout.msec = PJSUA_ACQUIRE_CALL_TIMEOUT;
  963. pj_time_val_normalize(&timeout);
  964. for (retry=0; ; ++retry) {
  965. if (retry % 10 == 9) {
  966. pj_time_val dtime;
  967. pj_gettimeofday(&dtime);
  968. PJ_TIME_VAL_SUB(dtime, time_start);
  969. if (!PJ_TIME_VAL_LT(dtime, timeout))
  970. break;
  971. }
  972. has_pjsua_lock = PJ_FALSE;
  973. status = PJSUA_TRY_LOCK();
  974. if (status != PJ_SUCCESS) {
  975. pj_thread_sleep(retry/10);
  976. continue;
  977. }
  978. has_pjsua_lock = PJ_TRUE;
  979. call = &pjsua_var.calls[call_id];
  980. if (call->inv == NULL) {
  981. PJSUA_UNLOCK();
  982. PJ_LOG(3,(THIS_FILE, "Invalid call_id %d in %s", call_id, title));
  983. return PJSIP_ESESSIONTERMINATED;
  984. }
  985. status = pjsip_dlg_try_inc_lock(call->inv->dlg);
  986. if (status != PJ_SUCCESS) {
  987. PJSUA_UNLOCK();
  988. pj_thread_sleep(retry/10);
  989. continue;
  990. }
  991. PJSUA_UNLOCK();
  992. break;
  993. }
  994. if (status != PJ_SUCCESS) {
  995. if (has_pjsua_lock == PJ_FALSE)
  996. PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire PJSUA mutex "
  997. "(possibly system has deadlocked) in %s",
  998. title));
  999. else
  1000. PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire dialog mutex "
  1001. "(possibly system has deadlocked) in %s",
  1002. title));
  1003. return PJ_ETIMEDOUT;
  1004. }
  1005. *p_call = call;
  1006. *p_dlg = call->inv->dlg;
  1007. return PJ_SUCCESS;
  1008. }
  1009. /*
  1010. * Get the conference port identification associated with the call.
  1011. */
  1012. PJ_DEF(pjsua_conf_port_id) pjsua_call_get_conf_port(pjsua_call_id call_id)
  1013. {
  1014. pjsua_call *call;
  1015. pjsua_conf_port_id port_id = PJSUA_INVALID_ID;
  1016. PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
  1017. PJ_EINVAL);
  1018. /* Use PJSUA_LOCK() instead of acquire_call():
  1019. * https://trac.pjsip.org/repos/ticket/1371
  1020. */
  1021. PJSUA_LOCK();
  1022. if (!pjsua_call_is_active(call_id))
  1023. goto on_return;
  1024. call = &pjsua_var.calls[call_id];
  1025. port_id = call->conf_slot;
  1026. on_return:
  1027. PJSUA_UNLOCK();
  1028. return port_id;
  1029. }
  1030. /*
  1031. * Obtain detail information about the specified call.
  1032. */
  1033. PJ_DEF(pj_status_t) pjsua_call_get_info( pjsua_call_id call_id,
  1034. pjsua_call_info *info)
  1035. {
  1036. pjsua_call *call;
  1037. PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
  1038. PJ_EINVAL);
  1039. pj_bzero(info, sizeof(*info));
  1040. /* Use PJSUA_LOCK() instead of acquire_call():
  1041. * https://trac.pjsip.org/repos/ticket/1371
  1042. */
  1043. PJSUA_LOCK();
  1044. call = &pjsua_var.calls[call_id];
  1045. if (!call->inv) {
  1046. PJSUA_UNLOCK();
  1047. return PJSIP_ESESSIONTERMINATED;
  1048. }
  1049. /* id and role */
  1050. info->id = call_id;
  1051. info->role = call->inv->role;
  1052. info->acc_id = call->acc_id;
  1053. /* local info */
  1054. info->local_info.ptr = info->buf_.local_info;
  1055. pj_strncpy(&info->local_info, &call->inv->dlg->local.info_str,
  1056. sizeof(info->buf_.local_info));
  1057. /* local contact */
  1058. info->local_contact.ptr = info->buf_.local_contact;
  1059. info->local_contact.slen = pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR,
  1060. call->inv->dlg->local.contact->uri,
  1061. info->local_contact.ptr,
  1062. sizeof(info->buf_.local_contact));
  1063. /* remote info */
  1064. info->remote_info.ptr = info->buf_.remote_info;
  1065. pj_strncpy(&info->remote_info, &call->inv->dlg->remote.info_str,
  1066. sizeof(info->buf_.remote_info));
  1067. /* remote contact */
  1068. if (call->inv->dlg->remote.contact) {
  1069. int len;
  1070. info->remote_contact.ptr = info->buf_.remote_contact;
  1071. len = pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR,
  1072. call->inv->dlg->remote.contact->uri,
  1073. info->remote_contact.ptr,
  1074. sizeof(info->buf_.remote_contact));
  1075. if (len < 0) len = 0;
  1076. info->remote_contact.slen = len;
  1077. } else {
  1078. info->remote_contact.slen = 0;
  1079. }
  1080. /* call id */
  1081. info->call_id.ptr = info->buf_.call_id;
  1082. pj_strncpy(&info->call_id, &call->inv->dlg->call_id->id,
  1083. sizeof(info->buf_.call_id));
  1084. /* state, state_text */
  1085. info->state = call->inv->state;
  1086. info->state_text = pj_str((char*)pjsip_inv_state_name(info->state));
  1087. /* If call is disconnected, set the last_status from the cause code */
  1088. if (call->inv->state >= PJSIP_INV_STATE_DISCONNECTED) {
  1089. /* last_status, last_status_text */
  1090. info->last_status = call->inv->cause;
  1091. info->last_status_text.ptr = info->buf_.last_status_text;
  1092. pj_strncpy(&info->last_status_text, &call->inv->cause_text,
  1093. sizeof(info->buf_.last_status_text));
  1094. } else {
  1095. /* last_status, last_status_text */
  1096. info->last_status = call->last_code;
  1097. info->last_status_text.ptr = info->buf_.last_status_text;
  1098. pj_strncpy(&info->last_status_text, &call->last_text,
  1099. sizeof(info->buf_.last_status_text));
  1100. }
  1101. /* media status and dir */
  1102. info->media_status = call->media_st;
  1103. info->media_dir = call->media_dir;
  1104. /* conference slot number */
  1105. info->conf_slot = call->conf_slot;
  1106. /* calculate duration */
  1107. if (info->state >= PJSIP_INV_STATE_DISCONNECTED) {
  1108. info->total_duration = call->dis_time;
  1109. PJ_TIME_VAL_SUB(info->total_duration, call->start_time);
  1110. if (call->conn_time.sec) {
  1111. info->connect_duration = call->dis_time;
  1112. PJ_TIME_VAL_SUB(info->connect_duration, call->conn_time);
  1113. }
  1114. } else if (info->state == PJSIP_INV_STATE_CONFIRMED) {
  1115. pj_gettimeofday(&info->total_duration);
  1116. PJ_TIME_VAL_SUB(info->total_duration, call->start_time);
  1117. pj_gettimeofday(&info->connect_duration);
  1118. PJ_TIME_VAL_SUB(info->connect_duration, call->conn_time);
  1119. } else {
  1120. pj_gettimeofday(&info->total_duration);
  1121. PJ_TIME_VAL_SUB(info->total_duration, call->start_time);
  1122. }
  1123. PJSUA_UNLOCK();
  1124. return PJ_SUCCESS;
  1125. }
  1126. /*
  1127. * Check if call remote peer support the specified capability.
  1128. */
  1129. PJ_DEF(pjsip_dialog_cap_status) pjsua_call_remote_has_cap(
  1130. pjsua_call_id call_id,
  1131. int htype,
  1132. const pj_str_t *hname,
  1133. const pj_str_t *token)
  1134. {
  1135. pjsua_call *call;
  1136. pjsip_dialog *dlg;
  1137. pj_status_t status;
  1138. pjsip_dialog_cap_status cap_status;
  1139. status = acquire_call("pjsua_call_peer_has_cap()", call_id, &call, &dlg);
  1140. if (status != PJ_SUCCESS)
  1141. return PJSIP_DIALOG_CAP_UNKNOWN;
  1142. cap_status = pjsip_dlg_remote_has_cap(dlg, htype, hname, token);
  1143. pjsip_dlg_dec_lock(dlg);
  1144. return cap_status;
  1145. }
  1146. /*
  1147. * Attach application specific data to the call.
  1148. */
  1149. PJ_DEF(pj_status_t) pjsua_call_set_user_data( pjsua_call_id call_id,
  1150. void *user_data)
  1151. {
  1152. PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
  1153. PJ_EINVAL);
  1154. pjsua_var.calls[call_id].user_data = user_data;
  1155. return PJ_SUCCESS;
  1156. }
  1157. /*
  1158. * Get user data attached to the call.
  1159. */
  1160. PJ_DEF(void*) pjsua_call_get_user_data(pjsua_call_id call_id)
  1161. {
  1162. PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
  1163. NULL);
  1164. return pjsua_var.calls[call_id].user_data;
  1165. }
  1166. /*
  1167. * Get remote's NAT type.
  1168. */
  1169. PJ_DEF(pj_status_t) pjsua_call_get_rem_nat_type(pjsua_call_id call_id,
  1170. pj_stun_nat_type *p_type)
  1171. {
  1172. PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
  1173. PJ_EINVAL);
  1174. PJ_ASSERT_RETURN(p_type != NULL, PJ_EINVAL);
  1175. *p_type = pjsua_var.calls[call_id].rem_nat_type;
  1176. return PJ_SUCCESS;
  1177. }
  1178. /*
  1179. * Send response to incoming INVITE request.
  1180. */
  1181. PJ_DEF(pj_status_t) pjsua_call_answer( pjsua_call_id call_id,
  1182. unsigned code,
  1183. const pj_str_t *reason,
  1184. const pjsua_msg_data *msg_data)
  1185. {
  1186. pjsua_call *call;
  1187. pjsip_dialog *dlg;
  1188. pjsip_tx_data *tdata;
  1189. pj_status_t status;
  1190. PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
  1191. PJ_EINVAL);
  1192. status = acquire_call("pjsua_call_answer()", call_id, &call, &dlg);
  1193. if (status != PJ_SUCCESS)
  1194. return status;
  1195. if (call->res_time.sec == 0)
  1196. pj_gettimeofday(&call->res_time);
  1197. if (reason && reason->slen == 0)
  1198. reason = NULL;
  1199. /* Create response message */
  1200. status = pjsip_inv_answer(call->inv, code, reason, NULL, &tdata);
  1201. if (status != PJ_SUCCESS) {
  1202. pjsua_perror(THIS_FILE, "Error creating response",
  1203. status);
  1204. pjsip_dlg_dec_lock(dlg);
  1205. return status;
  1206. }
  1207. /* Call might have been disconnected if application is answering with
  1208. * 200/OK and the media failed to start.
  1209. */
  1210. if (call->inv == NULL) {
  1211. pjsip_dlg_dec_lock(dlg);
  1212. return PJSIP_ESESSIONTERMINATED;
  1213. }
  1214. /* Add additional headers etc */
  1215. pjsua_process_msg_data( tdata, msg_data);
  1216. /* Send the message */
  1217. status = pjsip_inv_send_msg(call->inv, tdata);
  1218. if (status != PJ_SUCCESS)
  1219. pjsua_perror(THIS_FILE, "Error sending response",
  1220. status);
  1221. pjsip_dlg_dec_lock(dlg);
  1222. return status;
  1223. }
  1224. /*
  1225. * Hangup call by using method that is appropriate according to the
  1226. * call state.
  1227. */
  1228. PJ_DEF(pj_status_t) pjsua_call_hangup(pjsua_call_id call_id,
  1229. unsigned code,
  1230. const pj_str_t *reason,
  1231. const pjsua_msg_data *msg_data)
  1232. {
  1233. pjsua_call *call;
  1234. pjsip_dialog *dlg;
  1235. pj_status_t status;
  1236. pjsip_tx_data *tdata;
  1237. if (call_id<0 || call_id>=(int)pjsua_var.ua_cfg.max_calls) {
  1238. PJ_LOG(1,(THIS_FILE, "pjsua_call_hangup(): invalid call id %d",
  1239. call_id));
  1240. }
  1241. PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
  1242. PJ_EINVAL);
  1243. status = acquire_call("pjsua_call_hangup()", call_id, &call, &dlg);
  1244. if (status != PJ_SUCCESS)
  1245. return status;
  1246. if (code==0) {
  1247. if (call->inv->state == PJSIP_INV_STATE_CONFIRMED)
  1248. code = PJSIP_SC_OK;
  1249. else if (call->inv->role == PJSIP_ROLE_UAS)
  1250. code = PJSIP_SC_DECLINE;
  1251. else
  1252. code = PJSIP_SC_REQUEST_TERMINATED;
  1253. }
  1254. status = pjsip_inv_end_session(call->inv, code, reason, &tdata);
  1255. if (status != PJ_SUCCESS) {
  1256. pjsua_perror(THIS_FILE,
  1257. "Failed to create end session message",
  1258. status);
  1259. pjsip_dlg_dec_lock(dlg);
  1260. return status;
  1261. }
  1262. /* pjsip_inv_end_session may return PJ_SUCCESS with NULL
  1263. * as p_tdata when INVITE transaction has not been answered
  1264. * with any provisional responses.
  1265. */
  1266. if (tdata == NULL) {
  1267. pjsip_dlg_dec_lock(dlg);
  1268. return PJ_SUCCESS;
  1269. }
  1270. /* Add additional headers etc */
  1271. pjsua_process_msg_data( tdata, msg_data);
  1272. /* Send the message */
  1273. status = pjsip_inv_send_msg(call->inv, tdata);
  1274. if (status != PJ_SUCCESS) {
  1275. pjsua_perror(THIS_FILE,
  1276. "Failed to send end session message",
  1277. status);
  1278. pjsip_dlg_dec_lock(dlg);
  1279. return status;
  1280. }
  1281. /* Stop lock codec timer, if it is active */
  1282. if (call->lock_codec.reinv_timer.id) {
  1283. pjsip_endpt_cancel_timer(pjsua_var.endpt,
  1284. &call->lock_codec.reinv_timer);
  1285. call->lock_codec.reinv_timer.id = PJ_FALSE;
  1286. }
  1287. pjsip_dlg_dec_lock(dlg);
  1288. return PJ_SUCCESS;
  1289. }
  1290. /*
  1291. * Accept or reject redirection.
  1292. */
  1293. PJ_DEF(pj_status_t) pjsua_call_process_redirect( pjsua_call_id call_id,
  1294. pjsip_redirect_op cmd)
  1295. {
  1296. pjsua_call *call;
  1297. pjsip_dialog *dlg;
  1298. pj_status_t status;
  1299. PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
  1300. PJ_EINVAL);
  1301. status = acquire_call("pjsua_call_process_redirect()", call_id,
  1302. &call, &dlg);
  1303. if (status != PJ_SUCCESS)
  1304. return status;
  1305. status = pjsip_inv_process_redirect(call->inv, cmd, NULL);
  1306. pjsip_dlg_dec_lock(dlg);
  1307. return status;
  1308. }
  1309. /*
  1310. * Put the specified call on hold.
  1311. */
  1312. PJ_DEF(pj_status_t) pjsua_call_set_hold(pjsua_call_id call_id,
  1313. const pjsua_msg_data *msg_data)
  1314. {
  1315. pjmedia_sdp_session *sdp;
  1316. pjsua_call *call;
  1317. pjsip_dialog *dlg;
  1318. pjsip_tx_data *tdata;
  1319. pj_status_t status;
  1320. PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
  1321. PJ_EINVAL);
  1322. status = acquire_call("pjsua_call_set_hold()", call_id, &call, &dlg);
  1323. if (status != PJ_SUCCESS)
  1324. return status;
  1325. if (call->inv->state != PJSIP_INV_STATE_CONFIRMED) {
  1326. PJ_LOG(3,(THIS_FILE, "Can not hold call that is not confirmed"));
  1327. pjsip_dlg_dec_lock(dlg);
  1328. return PJSIP_ESESSIONSTATE;
  1329. }
  1330. status = create_sdp_of_call_hold(call, &sdp);
  1331. if (status != PJ_SUCCESS) {
  1332. pjsip_dlg_dec_lock(dlg);
  1333. return status;
  1334. }
  1335. /* Create re-INVITE with new offer */
  1336. status = pjsip_inv_reinvite( call->inv, NULL, sdp, &tdata);
  1337. if (status != PJ_SUCCESS) {
  1338. pjsua_perror(THIS_FILE, "Unable to create re-INVITE", status);
  1339. pjsip_dlg_dec_lock(dlg);
  1340. return status;
  1341. }
  1342. /* Add additional headers etc */
  1343. pjsua_process_msg_data( tdata, msg_data);
  1344. /* Record the tx_data to keep track the operation */
  1345. call->hold_msg = (void*) tdata;
  1346. /* Send the request */
  1347. status = pjsip_inv_send_msg( call->inv, tdata);
  1348. if (status != PJ_SUCCESS) {
  1349. pjsua_perror(THIS_FILE, "Unable to send re-INVITE", status);
  1350. call->hold_msg = NULL;
  1351. pjsip_dlg_dec_lock(dlg);
  1352. return status;
  1353. }
  1354. /* Set flag that local put the call on hold */
  1355. call->local_hold = PJ_TRUE;
  1356. pjsip_dlg_dec_lock(dlg);
  1357. return PJ_SUCCESS;
  1358. }
  1359. /*
  1360. * Send re-INVITE (to release hold).
  1361. */
  1362. PJ_DEF(pj_status_t) pjsua_call_reinvite( pjsua_call_id call_id,
  1363. unsigned options,
  1364. const pjsua_msg_data *msg_data)
  1365. {
  1366. pjmedia_sdp_session *sdp;
  1367. pj_str_t *new_contact = NULL;
  1368. pjsip_tx_data *tdata;
  1369. pjsua_call *call;
  1370. pjsip_dialog *dlg;
  1371. pj_status_t status;
  1372. PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
  1373. PJ_EINVAL);
  1374. status = acquire_call("pjsua_call_reinvite()", call_id, &call, &dlg);
  1375. if (status != PJ_SUCCESS)
  1376. return status;
  1377. if (call->inv->state != PJSIP_INV_STATE_CONFIRMED) {
  1378. PJ_LOG(3,(THIS_FILE, "Can not re-INVITE call that is not confirmed"));
  1379. pjsip_dlg_dec_lock(dlg);
  1380. return PJSIP_ESESSIONSTATE;
  1381. }
  1382. /* Create SDP */
  1383. if (call->local_hold && (options & PJSUA_CALL_UNHOLD)==0) {
  1384. status = create_sdp_of_call_hold(call, &sdp);
  1385. } else {
  1386. status = pjsua_media_channel_create_sdp(call->index,
  1387. call->inv->pool_prov,
  1388. NULL, &sdp, NULL);
  1389. call->local_hold = PJ_FALSE;
  1390. }
  1391. if (status != PJ_SUCCESS) {
  1392. pjsua_perror(THIS_FILE, "Unable to get SDP from media endpoint",
  1393. status);
  1394. pjsip_dlg_dec_lock(dlg);
  1395. return status;
  1396. }
  1397. if ((options & PJSUA_CALL_UPDATE_CONTACT) &
  1398. pjsua_acc_is_valid(call->acc_id))
  1399. {
  1400. new_contact = &pjsua_var.acc[call->acc_id].contact;
  1401. }
  1402. /* Create re-INVITE with new offer */
  1403. status = pjsip_inv_reinvite( call->inv, new_contact, sdp, &tdata);
  1404. if (status != PJ_SUCCESS) {
  1405. pjsua_perror(THIS_FILE, "Unable to create re-INVITE", status);
  1406. pjsip_dlg_dec_lock(dlg);
  1407. return status;
  1408. }
  1409. /* Add additional headers etc */
  1410. pjsua_process_msg_data( tdata, msg_data);
  1411. /* Send the request */
  1412. status = pjsip_inv_send_msg( call->inv, tdata);
  1413. if (status != PJ_SUCCESS) {
  1414. pjsua_perror(THIS_FILE, "Unable to send re-INVITE", status);
  1415. pjsip_dlg_dec_lock(dlg);
  1416. return status;
  1417. }
  1418. pjsip_dlg_dec_lock(dlg);
  1419. return PJ_SUCCESS;
  1420. }
  1421. /*
  1422. * Send UPDATE request.
  1423. */
  1424. PJ_DEF(pj_status_t) pjsua_call_update( pjsua_call_id call_id,
  1425. unsigned options,
  1426. const pjsua_msg_data *msg_data)
  1427. {
  1428. pjmedia_sdp_session *sdp;
  1429. pj_str_t *new_contact = NULL;
  1430. pjsip_tx_data *tdata;
  1431. pjsua_call *call;
  1432. pjsip_dialog *dlg;
  1433. pj_status_t status;
  1434. PJ_UNUSED_ARG(options);
  1435. PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
  1436. PJ_EINVAL);
  1437. status = acquire_call("pjsua_call_update()", call_id, &call, &dlg);
  1438. if (status != PJ_SUCCESS)
  1439. return status;
  1440. /* Create SDP */
  1441. status = pjsua_media_channel_create_sdp(call->index,
  1442. call->inv->pool_prov,
  1443. NULL, &sdp, NULL);
  1444. if (status != PJ_SUCCESS) {
  1445. pjsua_perror(THIS_FILE, "Unable to get SDP from media endpoint",
  1446. status);
  1447. pjsip_dlg_dec_lock(dlg);
  1448. return status;
  1449. }
  1450. if ((options & PJSUA_CALL_UPDATE_CONTACT) &
  1451. pjsua_acc_is_valid(call->acc_id))
  1452. {
  1453. new_contact = &pjsua_var.acc[call->acc_id].contact;
  1454. }
  1455. /* Create UPDATE with new offer */
  1456. status = pjsip_inv_update(call->inv, new_contact, sdp, &tdata);
  1457. if (status != PJ_SUCCESS) {
  1458. pjsua_perror(THIS_FILE, "Unable to create UPDATE request", status);
  1459. pjsip_dlg_dec_lock(dlg);
  1460. return status;
  1461. }
  1462. /* Add additional headers etc */
  1463. pjsua_process_msg_data( tdata, msg_data);
  1464. /* Send the request */
  1465. status = pjsip_inv_send_msg( call->inv, tdata);
  1466. if (status != PJ_SUCCESS) {
  1467. pjsua_perror(THIS_FILE, "Unable to send UPDATE request", status);
  1468. pjsip_dlg_dec_lock(dlg);
  1469. return status;
  1470. }
  1471. call->local_hold = PJ_FALSE;
  1472. pjsip_dlg_dec_lock(dlg);
  1473. return PJ_SUCCESS;
  1474. }
  1475. /*
  1476. * Initiate call transfer to the specified address.
  1477. */
  1478. PJ_DEF(pj_status_t) pjsua_call_xfer( pjsua_call_id call_id,
  1479. const pj_str_t *dest,
  1480. const pjsua_msg_data *msg_data)
  1481. {
  1482. pjsip_evsub *sub;
  1483. pjsip_tx_data *tdata;
  1484. pjsua_call *call;
  1485. pjsip_dialog *dlg;
  1486. pjsip_generic_string_hdr *gs_hdr;
  1487. const pj_str_t str_ref_by = { "Referred-By", 11 };
  1488. struct pjsip_evsub_user xfer_cb;
  1489. pj_status_t status;
  1490. PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
  1491. PJ_EINVAL);
  1492. status = acquire_call("pjsua_call_xfer()", call_id, &call, &dlg);
  1493. if (status != PJ_SUCCESS)
  1494. return status;
  1495. /* Create xfer client subscription. */
  1496. pj_bzero(&xfer_cb, sizeof(xfer_cb));
  1497. xfer_cb.on_evsub_state = &xfer_client_on_e