Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Nanang Izzuddin | a62ffc9 | 2011-05-05 06:14:19 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame] | 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 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 | */ |
Benny Prijono | 4f9f64e | 2006-02-27 00:00:30 +0000 | [diff] [blame] | 20 | #include <pjsua-lib/pjsua.h> |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 21 | #include <pjsua-lib/pjsua_internal.h> |
| 22 | |
| 23 | |
| 24 | #define THIS_FILE "pjsua_call.c" |
| 25 | |
| 26 | |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 27 | /* Retry interval of sending re-INVITE for locking a codec when remote |
| 28 | * SDP answer contains multiple codec, in milliseconds. |
| 29 | */ |
| 30 | #define LOCK_CODEC_RETRY_INTERVAL 200 |
| 31 | |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 32 | /* |
| 33 | * Max UPDATE/re-INVITE retry to lock codec |
| 34 | */ |
| 35 | #define LOCK_CODEC_MAX_RETRY 5 |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 36 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 37 | /* This callback receives notification from invite session when the |
| 38 | * session state has changed. |
| 39 | */ |
| 40 | static void pjsua_call_on_state_changed(pjsip_inv_session *inv, |
| 41 | pjsip_event *e); |
| 42 | |
| 43 | /* This callback is called by invite session framework when UAC session |
| 44 | * has forked. |
| 45 | */ |
| 46 | static void pjsua_call_on_forked( pjsip_inv_session *inv, |
| 47 | pjsip_event *e); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 48 | |
| 49 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 50 | * Callback to be called when SDP offer/answer negotiation has just completed |
| 51 | * in the session. This function will start/update media if negotiation |
| 52 | * has succeeded. |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 53 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 54 | static void pjsua_call_on_media_update(pjsip_inv_session *inv, |
| 55 | pj_status_t status); |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 56 | |
| 57 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 58 | * Called when session received new offer. |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 59 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 60 | static void pjsua_call_on_rx_offer(pjsip_inv_session *inv, |
| 61 | const pjmedia_sdp_session *offer); |
| 62 | |
| 63 | /* |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 64 | * Called to generate new offer. |
| 65 | */ |
| 66 | static void pjsua_call_on_create_offer(pjsip_inv_session *inv, |
| 67 | pjmedia_sdp_session **offer); |
| 68 | |
| 69 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 70 | * This callback is called when transaction state has changed in INVITE |
| 71 | * session. We use this to trap: |
| 72 | * - incoming REFER request. |
| 73 | * - incoming MESSAGE request. |
| 74 | */ |
| 75 | static void pjsua_call_on_tsx_state_changed(pjsip_inv_session *inv, |
| 76 | pjsip_transaction *tsx, |
| 77 | pjsip_event *e); |
| 78 | |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 79 | /* |
| 80 | * Redirection handler. |
| 81 | */ |
Benny Prijono | 08a48b8 | 2008-11-27 12:42:07 +0000 | [diff] [blame] | 82 | static pjsip_redirect_op pjsua_call_on_redirected(pjsip_inv_session *inv, |
| 83 | const pjsip_uri *target, |
| 84 | const pjsip_event *e); |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 85 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 86 | |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 87 | /* Create SDP for call hold. */ |
| 88 | static pj_status_t create_sdp_of_call_hold(pjsua_call *call, |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame] | 89 | pjmedia_sdp_session **p_sdp); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 90 | |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 91 | /* |
| 92 | * Callback called by event framework when the xfer subscription state |
| 93 | * has changed. |
| 94 | */ |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 95 | static void xfer_client_on_evsub_state( pjsip_evsub *sub, pjsip_event *event); |
| 96 | static void xfer_server_on_evsub_state( pjsip_evsub *sub, pjsip_event *event); |
| 97 | |
| 98 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 99 | * Reset call descriptor. |
| 100 | */ |
| 101 | static void reset_call(pjsua_call_id id) |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 102 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 103 | pjsua_call *call = &pjsua_var.calls[id]; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 104 | unsigned i; |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 105 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 106 | pj_bzero(call, sizeof(*call)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 107 | call->index = id; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 108 | call->last_text.ptr = call->last_text_buf_; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 109 | for (i=0; i<PJ_ARRAY_SIZE(call->media); ++i) { |
| 110 | pjsua_call_media *call_med = &call->media[i]; |
| 111 | call_med->ssrc = pj_rand(); |
| 112 | call_med->strm.a.conf_slot = PJSUA_INVALID_ID; |
Nanang Izzuddin | 62053a6 | 2011-07-12 11:08:32 +0000 | [diff] [blame] | 113 | call_med->strm.v.cap_win_id = PJSUA_INVALID_ID; |
| 114 | call_med->strm.v.rdr_win_id = PJSUA_INVALID_ID; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 115 | call_med->call = call; |
| 116 | call_med->idx = i; |
| 117 | call_med->tp_auto_del = PJ_TRUE; |
| 118 | } |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 122 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 123 | * Init call subsystem. |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 124 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 125 | pj_status_t pjsua_call_subsys_init(const pjsua_config *cfg) |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 126 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 127 | pjsip_inv_callback inv_cb; |
| 128 | unsigned i; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 129 | const pj_str_t str_norefersub = { "norefersub", 10 }; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 130 | pj_status_t status; |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 131 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 132 | /* Init calls array. */ |
| 133 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.calls); ++i) |
| 134 | reset_call(i); |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 135 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 136 | /* Copy config */ |
| 137 | pjsua_config_dup(pjsua_var.pool, &pjsua_var.ua_cfg, cfg); |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 138 | |
Benny Prijono | 1cd713b | 2009-11-11 00:33:00 +0000 | [diff] [blame] | 139 | /* Verify settings */ |
| 140 | if (pjsua_var.ua_cfg.max_calls >= PJSUA_MAX_CALLS) { |
| 141 | pjsua_var.ua_cfg.max_calls = PJSUA_MAX_CALLS; |
| 142 | } |
| 143 | |
Benny Prijono | 91d06b6 | 2008-09-20 12:16:56 +0000 | [diff] [blame] | 144 | /* Check the route URI's and force loose route if required */ |
| 145 | for (i=0; i<pjsua_var.ua_cfg.outbound_proxy_cnt; ++i) { |
| 146 | status = normalize_route_uri(pjsua_var.pool, |
| 147 | &pjsua_var.ua_cfg.outbound_proxy[i]); |
| 148 | if (status != PJ_SUCCESS) |
| 149 | return status; |
| 150 | } |
| 151 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 152 | /* Initialize invite session callback. */ |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 153 | pj_bzero(&inv_cb, sizeof(inv_cb)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 154 | inv_cb.on_state_changed = &pjsua_call_on_state_changed; |
| 155 | inv_cb.on_new_session = &pjsua_call_on_forked; |
| 156 | inv_cb.on_media_update = &pjsua_call_on_media_update; |
| 157 | inv_cb.on_rx_offer = &pjsua_call_on_rx_offer; |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 158 | inv_cb.on_create_offer = &pjsua_call_on_create_offer; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 159 | inv_cb.on_tsx_state_changed = &pjsua_call_on_tsx_state_changed; |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 160 | inv_cb.on_redirected = &pjsua_call_on_redirected; |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 161 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 162 | /* Initialize invite session module: */ |
| 163 | status = pjsip_inv_usage_init(pjsua_var.endpt, &inv_cb); |
| 164 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 165 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 166 | /* Add "norefersub" in Supported header */ |
| 167 | pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_SUPPORTED, |
| 168 | NULL, 1, &str_norefersub); |
| 169 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 170 | return status; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | /* |
| 175 | * Start call subsystem. |
| 176 | */ |
| 177 | pj_status_t pjsua_call_subsys_start(void) |
| 178 | { |
| 179 | /* Nothing to do */ |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 180 | return PJ_SUCCESS; |
| 181 | } |
| 182 | |
| 183 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 184 | /* |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 185 | * Get maximum number of calls configured in pjsua. |
| 186 | */ |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 187 | PJ_DEF(unsigned) pjsua_call_get_max_count(void) |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 188 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 189 | return pjsua_var.ua_cfg.max_calls; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 193 | /* |
| 194 | * Get number of currently active calls. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 195 | */ |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 196 | PJ_DEF(unsigned) pjsua_call_get_count(void) |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 197 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 198 | return pjsua_var.call_cnt; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 202 | /* |
| 203 | * Enum calls. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 204 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 205 | PJ_DEF(pj_status_t) pjsua_enum_calls( pjsua_call_id ids[], |
| 206 | unsigned *count) |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 207 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 208 | unsigned i, c; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 209 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 210 | PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 211 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 212 | PJSUA_LOCK(); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 213 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 214 | for (i=0, c=0; c<*count && i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 215 | if (!pjsua_var.calls[i].inv) |
| 216 | continue; |
| 217 | ids[c] = i; |
| 218 | ++c; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 221 | *count = c; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 222 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 223 | PJSUA_UNLOCK(); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 224 | |
| 225 | return PJ_SUCCESS; |
| 226 | } |
| 227 | |
| 228 | |
Benny Prijono | 5773cd6 | 2008-01-19 13:01:42 +0000 | [diff] [blame] | 229 | /* Allocate one call id */ |
| 230 | static pjsua_call_id alloc_call_id(void) |
| 231 | { |
| 232 | pjsua_call_id cid; |
| 233 | |
| 234 | #if 1 |
| 235 | /* New algorithm: round-robin */ |
| 236 | if (pjsua_var.next_call_id >= (int)pjsua_var.ua_cfg.max_calls || |
| 237 | pjsua_var.next_call_id < 0) |
| 238 | { |
Benny Prijono | 5d177b8 | 2008-02-26 10:31:28 +0000 | [diff] [blame] | 239 | pjsua_var.next_call_id = 0; |
Benny Prijono | 5773cd6 | 2008-01-19 13:01:42 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | for (cid=pjsua_var.next_call_id; |
| 243 | cid<(int)pjsua_var.ua_cfg.max_calls; |
| 244 | ++cid) |
| 245 | { |
| 246 | if (pjsua_var.calls[cid].inv == NULL) { |
| 247 | ++pjsua_var.next_call_id; |
| 248 | return cid; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | for (cid=0; cid < pjsua_var.next_call_id; ++cid) { |
| 253 | if (pjsua_var.calls[cid].inv == NULL) { |
| 254 | ++pjsua_var.next_call_id; |
| 255 | return cid; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | #else |
| 260 | /* Old algorithm */ |
| 261 | for (cid=0; cid<(int)pjsua_var.ua_cfg.max_calls; ++cid) { |
| 262 | if (pjsua_var.calls[cid].inv == NULL) |
| 263 | return cid; |
| 264 | } |
| 265 | #endif |
| 266 | |
| 267 | return PJSUA_INVALID_ID; |
| 268 | } |
| 269 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 270 | /* Get signaling secure level. |
| 271 | * Return: |
| 272 | * 0: if signaling is not secure |
| 273 | * 1: if TLS transport is used for immediate hop |
| 274 | * 2: if end-to-end signaling is secure. |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 275 | */ |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 276 | static int get_secure_level(pjsua_acc_id acc_id, const pj_str_t *dst_uri) |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 277 | { |
| 278 | const pj_str_t tls = pj_str(";transport=tls"); |
| 279 | const pj_str_t sips = pj_str("sips:"); |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 280 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 281 | |
| 282 | if (pj_stristr(dst_uri, &sips)) |
| 283 | return 2; |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 284 | |
| 285 | if (!pj_list_empty(&acc->route_set)) { |
| 286 | pjsip_route_hdr *r = acc->route_set.next; |
| 287 | pjsip_uri *uri = r->name_addr.uri; |
| 288 | pjsip_sip_uri *sip_uri; |
| 289 | |
| 290 | sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri); |
| 291 | if (pj_stricmp2(&sip_uri->transport_param, "tls")==0) |
| 292 | return 1; |
| 293 | |
| 294 | } else { |
| 295 | if (pj_stristr(dst_uri, &tls)) |
| 296 | return 1; |
| 297 | } |
| 298 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 302 | /* |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 303 | static int call_get_secure_level(pjsua_call *call) |
| 304 | { |
| 305 | if (call->inv->dlg->secure) |
| 306 | return 2; |
| 307 | |
| 308 | if (!pj_list_empty(&call->inv->dlg->route_set)) { |
| 309 | pjsip_route_hdr *r = call->inv->dlg->route_set.next; |
| 310 | pjsip_uri *uri = r->name_addr.uri; |
| 311 | pjsip_sip_uri *sip_uri; |
| 312 | |
| 313 | sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri); |
| 314 | if (pj_stricmp2(&sip_uri->transport_param, "tls")==0) |
| 315 | return 1; |
| 316 | |
| 317 | } else { |
| 318 | pjsip_sip_uri *sip_uri; |
| 319 | |
| 320 | if (PJSIP_URI_SCHEME_IS_SIPS(call->inv->dlg->target)) |
| 321 | return 2; |
| 322 | if (!PJSIP_URI_SCHEME_IS_SIP(call->inv->dlg->target)) |
| 323 | return 0; |
| 324 | |
| 325 | sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(call->inv->dlg->target); |
| 326 | if (pj_stricmp2(&sip_uri->transport_param, "tls")==0) |
| 327 | return 1; |
| 328 | } |
| 329 | |
| 330 | return 0; |
| 331 | } |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 332 | */ |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 333 | |
Sauw Ming | ec76535 | 2011-10-03 02:04:36 +0000 | [diff] [blame] | 334 | /* Outgoing call callback when media transport creation is completed. */ |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 335 | static pj_status_t |
| 336 | on_make_call_med_tp_complete(pjsua_call_id call_id, |
| 337 | const pjsua_med_tp_state_info *info) |
| 338 | { |
| 339 | pjmedia_sdp_session *offer; |
| 340 | pjsip_inv_session *inv = NULL; |
| 341 | pjsua_call *call = &pjsua_var.calls[call_id]; |
| 342 | pjsua_acc *acc = &pjsua_var.acc[call->acc_id]; |
| 343 | pjsip_dialog *dlg = call->async_call.dlg; |
| 344 | unsigned options = call->async_call.call_var.out_call.options; |
| 345 | pjsip_tx_data *tdata; |
| 346 | pj_status_t status = (info? info->status: PJ_SUCCESS); |
| 347 | |
| 348 | PJSUA_LOCK(); |
| 349 | |
| 350 | /* Increment the dialog's lock otherwise when invite session creation |
| 351 | * fails the dialog will be destroyed prematurely. |
| 352 | */ |
| 353 | pjsip_dlg_inc_lock(dlg); |
| 354 | |
| 355 | if (status != PJ_SUCCESS) { |
| 356 | pjsua_perror(THIS_FILE, "Error initializing media channel", status); |
| 357 | goto on_error; |
| 358 | } |
| 359 | |
Sauw Ming | 903154f | 2011-10-03 08:22:48 +0000 | [diff] [blame] | 360 | /* pjsua_media_channel_deinit() has been called. */ |
| 361 | if (call->async_call.med_ch_deinit) |
| 362 | goto on_error; |
| 363 | |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 364 | /* Create offer */ |
| 365 | status = pjsua_media_channel_create_sdp(call->index, dlg->pool, NULL, |
| 366 | &offer, NULL); |
| 367 | if (status != PJ_SUCCESS) { |
| 368 | pjsua_perror(THIS_FILE, "Error initializing media channel", status); |
| 369 | goto on_error; |
| 370 | } |
| 371 | |
| 372 | /* Create the INVITE session: */ |
| 373 | options |= PJSIP_INV_SUPPORT_100REL; |
| 374 | if (acc->cfg.require_100rel) |
| 375 | options |= PJSIP_INV_REQUIRE_100REL; |
| 376 | if (acc->cfg.use_timer != PJSUA_SIP_TIMER_INACTIVE) { |
| 377 | options |= PJSIP_INV_SUPPORT_TIMER; |
| 378 | if (acc->cfg.use_timer == PJSUA_SIP_TIMER_REQUIRED) |
| 379 | options |= PJSIP_INV_REQUIRE_TIMER; |
| 380 | else if (acc->cfg.use_timer == PJSUA_SIP_TIMER_ALWAYS) |
| 381 | options |= PJSIP_INV_ALWAYS_USE_TIMER; |
| 382 | } |
| 383 | |
| 384 | status = pjsip_inv_create_uac( dlg, offer, options, &inv); |
| 385 | if (status != PJ_SUCCESS) { |
| 386 | pjsua_perror(THIS_FILE, "Invite session creation failed", status); |
| 387 | goto on_error; |
| 388 | } |
| 389 | |
| 390 | /* Init Session Timers */ |
| 391 | status = pjsip_timer_init_session(inv, &acc->cfg.timer_setting); |
| 392 | if (status != PJ_SUCCESS) { |
| 393 | pjsua_perror(THIS_FILE, "Session Timer init failed", status); |
| 394 | goto on_error; |
| 395 | } |
| 396 | |
| 397 | /* Create and associate our data in the session. */ |
| 398 | call->inv = inv; |
| 399 | |
| 400 | dlg->mod_data[pjsua_var.mod.id] = call; |
| 401 | inv->mod_data[pjsua_var.mod.id] = call; |
| 402 | |
| 403 | /* If account is locked to specific transport, then lock dialog |
| 404 | * to this transport too. |
| 405 | */ |
| 406 | if (acc->cfg.transport_id != PJSUA_INVALID_ID) { |
| 407 | pjsip_tpselector tp_sel; |
| 408 | |
| 409 | pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel); |
| 410 | pjsip_dlg_set_transport(dlg, &tp_sel); |
| 411 | } |
| 412 | |
| 413 | /* Set dialog Route-Set: */ |
| 414 | if (!pj_list_empty(&acc->route_set)) |
| 415 | pjsip_dlg_set_route_set(dlg, &acc->route_set); |
| 416 | |
| 417 | |
| 418 | /* Set credentials: */ |
| 419 | if (acc->cred_cnt) { |
| 420 | pjsip_auth_clt_set_credentials( &dlg->auth_sess, |
| 421 | acc->cred_cnt, acc->cred); |
| 422 | } |
| 423 | |
| 424 | /* Set authentication preference */ |
| 425 | pjsip_auth_clt_set_prefs(&dlg->auth_sess, &acc->cfg.auth_pref); |
| 426 | |
| 427 | /* Create initial INVITE: */ |
| 428 | |
| 429 | status = pjsip_inv_invite(inv, &tdata); |
| 430 | if (status != PJ_SUCCESS) { |
| 431 | pjsua_perror(THIS_FILE, "Unable to create initial INVITE request", |
| 432 | status); |
| 433 | goto on_error; |
| 434 | } |
| 435 | |
| 436 | |
| 437 | /* Add additional headers etc */ |
| 438 | |
| 439 | pjsua_process_msg_data( tdata, |
| 440 | call->async_call.call_var.out_call.msg_data); |
| 441 | |
| 442 | /* Must increment call counter now */ |
| 443 | ++pjsua_var.call_cnt; |
| 444 | |
| 445 | /* Send initial INVITE: */ |
| 446 | |
| 447 | status = pjsip_inv_send_msg(inv, tdata); |
| 448 | if (status != PJ_SUCCESS) { |
| 449 | pjsua_perror(THIS_FILE, "Unable to send initial INVITE request", |
| 450 | status); |
| 451 | |
| 452 | /* Upon failure to send first request, the invite |
| 453 | * session would have been cleared. |
| 454 | */ |
| 455 | inv = NULL; |
| 456 | goto on_error; |
| 457 | } |
| 458 | |
| 459 | /* Done. */ |
| 460 | |
| 461 | pjsip_dlg_dec_lock(dlg); |
| 462 | PJSUA_UNLOCK(); |
| 463 | |
| 464 | return PJ_SUCCESS; |
| 465 | |
| 466 | on_error: |
| 467 | if (dlg) { |
| 468 | /* This may destroy the dialog */ |
| 469 | pjsip_dlg_dec_lock(dlg); |
| 470 | } |
| 471 | |
| 472 | if (inv != NULL) { |
| 473 | pjsip_inv_terminate(inv, PJSIP_SC_OK, PJ_FALSE); |
| 474 | } |
| 475 | |
| 476 | if (call_id != -1) { |
| 477 | reset_call(call_id); |
| 478 | pjsua_media_channel_deinit(call_id); |
| 479 | } |
| 480 | |
| 481 | PJSUA_UNLOCK(); |
| 482 | return status; |
| 483 | } |
| 484 | |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 485 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 486 | /* |
| 487 | * Make outgoing call to the specified URI using the specified account. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 488 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 489 | PJ_DEF(pj_status_t) pjsua_call_make_call( pjsua_acc_id acc_id, |
| 490 | const pj_str_t *dest_uri, |
| 491 | unsigned options, |
| 492 | void *user_data, |
| 493 | const pjsua_msg_data *msg_data, |
| 494 | pjsua_call_id *p_call_id) |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 495 | { |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 496 | pj_pool_t *tmp_pool = NULL; |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 497 | pjsip_dialog *dlg = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 498 | pjsua_acc *acc; |
| 499 | pjsua_call *call; |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 500 | int call_id = -1; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 501 | pj_str_t contact; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 502 | pj_status_t status; |
| 503 | |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 504 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 505 | /* Check that account is valid */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 506 | PJ_ASSERT_RETURN(acc_id>=0 || acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 507 | PJ_EINVAL); |
| 508 | |
Benny Prijono | 320fa4d | 2006-12-07 10:09:16 +0000 | [diff] [blame] | 509 | /* Check arguments */ |
| 510 | PJ_ASSERT_RETURN(dest_uri, PJ_EINVAL); |
| 511 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 512 | PJ_LOG(4,(THIS_FILE, "Making call with acc #%d to %.*s", acc_id, |
| 513 | (int)dest_uri->slen, dest_uri->ptr)); |
| 514 | |
| 515 | pj_log_push_indent(); |
| 516 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 517 | PJSUA_LOCK(); |
| 518 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 519 | /* Create sound port if none is instantiated, to check if sound device |
| 520 | * can be used. But only do this with the conference bridge, as with |
| 521 | * audio switchboard (i.e. APS-Direct), we can only open the sound |
| 522 | * device once the correct format has been known |
| 523 | */ |
| 524 | if (!pjsua_var.is_mswitch && pjsua_var.snd_port==NULL && |
| 525 | pjsua_var.null_snd==NULL && !pjsua_var.no_snd) |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 526 | { |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 527 | status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 528 | if (status != PJ_SUCCESS) |
| 529 | goto on_error; |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 532 | acc = &pjsua_var.acc[acc_id]; |
| 533 | if (!acc->valid) { |
| 534 | pjsua_perror(THIS_FILE, "Unable to make call because account " |
| 535 | "is not valid", PJ_EINVALIDOP); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 536 | status = PJ_EINVALIDOP; |
| 537 | goto on_error; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 538 | } |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 539 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 540 | /* Find free call slot. */ |
Benny Prijono | 5773cd6 | 2008-01-19 13:01:42 +0000 | [diff] [blame] | 541 | call_id = alloc_call_id(); |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 542 | |
Benny Prijono | 5773cd6 | 2008-01-19 13:01:42 +0000 | [diff] [blame] | 543 | if (call_id == PJSUA_INVALID_ID) { |
Nanang Izzuddin | 79f4f20 | 2009-10-20 13:14:40 +0000 | [diff] [blame] | 544 | pjsua_perror(THIS_FILE, "Error making call", PJ_ETOOMANY); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 545 | status = PJ_ETOOMANY; |
| 546 | goto on_error; |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 549 | call = &pjsua_var.calls[call_id]; |
| 550 | |
Nanang Izzuddin | 79f4f20 | 2009-10-20 13:14:40 +0000 | [diff] [blame] | 551 | /* Associate session with account */ |
| 552 | call->acc_id = acc_id; |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame] | 553 | call->call_hold_type = acc->cfg.call_hold_type; |
Nanang Izzuddin | 79f4f20 | 2009-10-20 13:14:40 +0000 | [diff] [blame] | 554 | |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 555 | /* Create temporary pool */ |
| 556 | tmp_pool = pjsua_pool_create("tmpcall10", 512, 256); |
| 557 | |
Benny Prijono | 320fa4d | 2006-12-07 10:09:16 +0000 | [diff] [blame] | 558 | /* Verify that destination URI is valid before calling |
| 559 | * pjsua_acc_create_uac_contact, or otherwise there |
| 560 | * a misleading "Invalid Contact URI" error will be printed |
| 561 | * when pjsua_acc_create_uac_contact() fails. |
| 562 | */ |
| 563 | if (1) { |
Benny Prijono | 320fa4d | 2006-12-07 10:09:16 +0000 | [diff] [blame] | 564 | pjsip_uri *uri; |
| 565 | pj_str_t dup; |
| 566 | |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 567 | pj_strdup_with_null(tmp_pool, &dup, dest_uri); |
| 568 | uri = pjsip_parse_uri(tmp_pool, dup.ptr, dup.slen, 0); |
Benny Prijono | 320fa4d | 2006-12-07 10:09:16 +0000 | [diff] [blame] | 569 | |
| 570 | if (uri == NULL) { |
| 571 | pjsua_perror(THIS_FILE, "Unable to make call", |
| 572 | PJSIP_EINVALIDREQURI); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 573 | status = PJSIP_EINVALIDREQURI; |
| 574 | goto on_error; |
Benny Prijono | 320fa4d | 2006-12-07 10:09:16 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 578 | /* Mark call start time. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 579 | pj_gettimeofday(&call->start_time); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 580 | |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 581 | /* Reset first response time */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 582 | call->res_time.sec = 0; |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 583 | |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 584 | /* Create suitable Contact header unless a Contact header has been |
| 585 | * set in the account. |
| 586 | */ |
| 587 | if (acc->contact.slen) { |
| 588 | contact = acc->contact; |
| 589 | } else { |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 590 | status = pjsua_acc_create_uac_contact(tmp_pool, &contact, |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 591 | acc_id, dest_uri); |
| 592 | if (status != PJ_SUCCESS) { |
| 593 | pjsua_perror(THIS_FILE, "Unable to generate Contact header", |
| 594 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 595 | goto on_error; |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 596 | } |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 599 | /* Create outgoing dialog: */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 600 | status = pjsip_dlg_create_uac( pjsip_ua_instance(), |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 601 | &acc->cfg.id, &contact, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 602 | dest_uri, dest_uri, &dlg); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 603 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 604 | pjsua_perror(THIS_FILE, "Dialog creation failed", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 605 | goto on_error; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Benny Prijono | 0bd5eb9 | 2009-04-14 11:10:31 +0000 | [diff] [blame] | 608 | /* Increment the dialog's lock otherwise when invite session creation |
| 609 | * fails the dialog will be destroyed prematurely. |
| 610 | */ |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 611 | // pjsip_dlg_inc_lock(dlg); |
Benny Prijono | 0bd5eb9 | 2009-04-14 11:10:31 +0000 | [diff] [blame] | 612 | |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 613 | /* Calculate call's secure level */ |
| 614 | call->secure_level = get_secure_level(acc_id, dest_uri); |
| 615 | |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 616 | /* Attach user data */ |
| 617 | call->user_data = user_data; |
| 618 | |
Sauw Ming | ec76535 | 2011-10-03 02:04:36 +0000 | [diff] [blame] | 619 | /* Store variables required for the callback after the async |
| 620 | * media transport creation is completed. |
| 621 | */ |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 622 | call->async_call.call_var.out_call.options = options; |
Sauw Ming | 848742f | 2011-09-28 04:20:30 +0000 | [diff] [blame] | 623 | if (msg_data) { |
| 624 | call->async_call.call_var.out_call.msg_data = pjsua_msg_data_clone( |
| 625 | dlg->pool, msg_data); |
| 626 | } |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 627 | call->async_call.dlg = dlg; |
| 628 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 629 | /* Init media channel */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 630 | status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC, |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 631 | call->secure_level, dlg->pool, |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 632 | NULL, NULL, PJ_TRUE, |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 633 | &on_make_call_med_tp_complete); |
| 634 | if (status == PJ_SUCCESS) { |
| 635 | status = on_make_call_med_tp_complete(call->index, NULL); |
| 636 | if (status != PJ_SUCCESS) |
| 637 | goto on_error; |
| 638 | } else if (status != PJ_EPENDING) { |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 639 | pjsua_perror(THIS_FILE, "Error initializing media channel", status); |
| 640 | goto on_error; |
| 641 | } |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 642 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 643 | /* Done. */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 644 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 645 | if (p_call_id) |
| 646 | *p_call_id = call_id; |
| 647 | |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 648 | pj_pool_release(tmp_pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 649 | PJSUA_UNLOCK(); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 650 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 651 | pj_log_pop_indent(); |
| 652 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 653 | return PJ_SUCCESS; |
| 654 | |
| 655 | |
| 656 | on_error: |
Benny Prijono | 0bd5eb9 | 2009-04-14 11:10:31 +0000 | [diff] [blame] | 657 | if (dlg) { |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 658 | pjsip_dlg_inc_lock(dlg); |
Benny Prijono | 0bd5eb9 | 2009-04-14 11:10:31 +0000 | [diff] [blame] | 659 | /* This may destroy the dialog */ |
| 660 | pjsip_dlg_dec_lock(dlg); |
| 661 | } |
| 662 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 663 | if (call_id != -1) { |
| 664 | reset_call(call_id); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 665 | pjsua_media_channel_deinit(call_id); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 668 | if (tmp_pool) |
| 669 | pj_pool_release(tmp_pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 670 | PJSUA_UNLOCK(); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 671 | |
| 672 | pj_log_pop_indent(); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 673 | return status; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | |
Benny Prijono | 91a6a17 | 2007-10-31 08:59:29 +0000 | [diff] [blame] | 677 | /* Get the NAT type information in remote's SDP */ |
| 678 | static void update_remote_nat_type(pjsua_call *call, |
| 679 | const pjmedia_sdp_session *sdp) |
| 680 | { |
| 681 | const pjmedia_sdp_attr *xnat; |
| 682 | |
| 683 | xnat = pjmedia_sdp_attr_find2(sdp->attr_count, sdp->attr, "X-nat", NULL); |
| 684 | if (xnat) { |
| 685 | call->rem_nat_type = (pj_stun_nat_type) (xnat->value.ptr[0] - '0'); |
| 686 | } else { |
| 687 | call->rem_nat_type = PJ_STUN_NAT_TYPE_UNKNOWN; |
| 688 | } |
| 689 | |
| 690 | PJ_LOG(5,(THIS_FILE, "Call %d: remote NAT type is %d (%s)", call->index, |
| 691 | call->rem_nat_type, pj_stun_get_nat_name(call->rem_nat_type))); |
| 692 | } |
| 693 | |
| 694 | |
Sauw Ming | ec76535 | 2011-10-03 02:04:36 +0000 | [diff] [blame] | 695 | /* Incoming call callback when media transport creation is completed. */ |
| 696 | static pj_status_t |
| 697 | on_incoming_call_med_tp_complete(pjsua_call_id call_id, |
| 698 | const pjsua_med_tp_state_info *info) |
| 699 | { |
| 700 | pjsua_call *call = &pjsua_var.calls[call_id]; |
| 701 | const pjmedia_sdp_session *offer=NULL; |
| 702 | pjmedia_sdp_session *answer; |
| 703 | pjsip_tx_data *response = NULL; |
| 704 | unsigned options = 0; |
| 705 | int sip_err_code = (info? info->sip_err_code: 0); |
| 706 | pj_status_t status = (info? info->status: PJ_SUCCESS); |
| 707 | |
| 708 | PJSUA_LOCK(); |
| 709 | |
| 710 | if (status != PJ_SUCCESS) { |
| 711 | pjsua_perror(THIS_FILE, "Error initializing media channel", status); |
| 712 | goto on_return; |
| 713 | } |
Sauw Ming | 903154f | 2011-10-03 08:22:48 +0000 | [diff] [blame] | 714 | |
| 715 | /* pjsua_media_channel_deinit() has been called. */ |
| 716 | if (call->async_call.med_ch_deinit) { |
| 717 | pjsua_media_channel_deinit(call->index); |
| 718 | call->med_ch_cb = NULL; |
| 719 | PJSUA_UNLOCK(); |
| 720 | return PJ_SUCCESS; |
| 721 | } |
| 722 | |
Sauw Ming | ec76535 | 2011-10-03 02:04:36 +0000 | [diff] [blame] | 723 | /* Get remote SDP offer (if any). */ |
| 724 | if (call->inv->neg) |
| 725 | pjmedia_sdp_neg_get_neg_remote(call->inv->neg, &offer); |
| 726 | |
| 727 | status = pjsua_media_channel_create_sdp(call_id, |
| 728 | call->async_call.dlg->pool, |
| 729 | offer, &answer, &sip_err_code); |
| 730 | if (status != PJ_SUCCESS) { |
| 731 | pjsua_perror(THIS_FILE, "Error creating SDP answer", status); |
| 732 | goto on_return; |
| 733 | } |
| 734 | |
| 735 | status = pjsip_inv_set_local_sdp(call->inv, answer); |
| 736 | if (status != PJ_SUCCESS) { |
| 737 | pjsua_perror(THIS_FILE, "Error setting local SDP", status); |
| 738 | sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE; |
| 739 | goto on_return; |
| 740 | } |
| 741 | |
| 742 | /* Verify that we can handle the request. */ |
| 743 | status = pjsip_inv_verify_request3(NULL, |
| 744 | call->inv->pool_prov, &options, offer, |
| 745 | answer, NULL, pjsua_var.endpt, &response); |
| 746 | if (status != PJ_SUCCESS) { |
| 747 | /* |
| 748 | * No we can't handle the incoming INVITE request. |
| 749 | */ |
| 750 | goto on_return; |
| 751 | } |
| 752 | |
| 753 | on_return: |
| 754 | if (status != PJ_SUCCESS) { |
| 755 | pjsip_tx_data *tdata; |
| 756 | pj_status_t status_; |
| 757 | |
| 758 | status_ = pjsip_inv_end_session(call->inv, sip_err_code, NULL, &tdata); |
| 759 | if (status_ == PJ_SUCCESS && tdata) |
| 760 | status_ = pjsip_inv_send_msg(call->inv, tdata); |
| 761 | |
| 762 | pjsua_media_channel_deinit(call->index); |
| 763 | } |
| 764 | |
| 765 | /* Set the callback to NULL to indicate that the async operation |
| 766 | * has completed. |
| 767 | */ |
| 768 | call->med_ch_cb = NULL; |
| 769 | |
| 770 | if (status == PJ_SUCCESS && |
| 771 | !pj_list_empty(&call->async_call.call_var.inc_call.answers)) |
| 772 | { |
| 773 | struct call_answer *answer, *next; |
| 774 | |
| 775 | answer = call->async_call.call_var.inc_call.answers.next; |
| 776 | while (answer != &call->async_call.call_var.inc_call.answers) { |
| 777 | next = answer->next; |
| 778 | pjsua_call_answer(call_id, answer->code, answer->reason, |
| 779 | answer->msg_data); |
| 780 | pj_list_erase(answer); |
| 781 | answer = next; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | PJSUA_UNLOCK(); |
| 786 | return status; |
| 787 | } |
| 788 | |
| 789 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 790 | /** |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 791 | * Handle incoming INVITE request. |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 792 | * Called by pjsua_core.c |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 793 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 794 | pj_bool_t pjsua_call_on_incoming(pjsip_rx_data *rdata) |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 795 | { |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 796 | pj_str_t contact; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 797 | pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 798 | pjsip_dialog *replaced_dlg = NULL; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 799 | pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata); |
| 800 | pjsip_msg *msg = rdata->msg_info.msg; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 801 | pjsip_tx_data *response = NULL; |
| 802 | unsigned options = 0; |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 803 | pjsip_inv_session *inv = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 804 | int acc_id; |
| 805 | pjsua_call *call; |
| 806 | int call_id = -1; |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 807 | int sip_err_code; |
Sauw Ming | ec76535 | 2011-10-03 02:04:36 +0000 | [diff] [blame] | 808 | pjmedia_sdp_session *offer=NULL; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 809 | pj_status_t status; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 810 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 811 | /* Don't want to handle anything but INVITE */ |
| 812 | if (msg->line.req.method.id != PJSIP_INVITE_METHOD) |
| 813 | return PJ_FALSE; |
| 814 | |
| 815 | /* Don't want to handle anything that's already associated with |
| 816 | * existing dialog or transaction. |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 817 | */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 818 | if (dlg || tsx) |
| 819 | return PJ_FALSE; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 820 | |
Benny Prijono | 384dab4 | 2009-10-14 01:58:04 +0000 | [diff] [blame] | 821 | /* Don't want to accept the call if shutdown is in progress */ |
| 822 | if (pjsua_var.thread_quit_flag) { |
| 823 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, |
| 824 | PJSIP_SC_TEMPORARILY_UNAVAILABLE, NULL, |
| 825 | NULL, NULL); |
| 826 | return PJ_TRUE; |
| 827 | } |
| 828 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 829 | PJ_LOG(4,(THIS_FILE, "Incoming %s", rdata->msg_info.info)); |
| 830 | pj_log_push_indent(); |
| 831 | |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 832 | PJSUA_LOCK(); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 833 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 834 | /* Find free call slot. */ |
Benny Prijono | 5773cd6 | 2008-01-19 13:01:42 +0000 | [diff] [blame] | 835 | call_id = alloc_call_id(); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 836 | |
Benny Prijono | 5773cd6 | 2008-01-19 13:01:42 +0000 | [diff] [blame] | 837 | if (call_id == PJSUA_INVALID_ID) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 838 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 839 | PJSIP_SC_BUSY_HERE, NULL, |
| 840 | NULL, NULL); |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 841 | PJ_LOG(2,(THIS_FILE, |
| 842 | "Unable to accept incoming call (too many calls)")); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 843 | goto on_return; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 846 | /* Clear call descriptor */ |
| 847 | reset_call(call_id); |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 848 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 849 | call = &pjsua_var.calls[call_id]; |
| 850 | |
| 851 | /* Mark call start time. */ |
| 852 | pj_gettimeofday(&call->start_time); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 853 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 854 | /* Check INVITE request for Replaces header. If Replaces header is |
| 855 | * present, the function will make sure that we can handle the request. |
| 856 | */ |
| 857 | status = pjsip_replaces_verify_request(rdata, &replaced_dlg, PJ_FALSE, |
| 858 | &response); |
| 859 | if (status != PJ_SUCCESS) { |
| 860 | /* |
| 861 | * Something wrong with the Replaces header. |
| 862 | */ |
| 863 | if (response) { |
| 864 | pjsip_response_addr res_addr; |
| 865 | |
| 866 | pjsip_get_response_addr(response->pool, rdata, &res_addr); |
| 867 | pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, response, |
| 868 | NULL, NULL); |
| 869 | |
| 870 | } else { |
| 871 | |
| 872 | /* Respond with 500 (Internal Server Error) */ |
| 873 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL, |
| 874 | NULL, NULL); |
| 875 | } |
| 876 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 877 | goto on_return; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /* If this INVITE request contains Replaces header, notify application |
| 881 | * about the request so that application can do subsequent checking |
| 882 | * if it wants to. |
| 883 | */ |
| 884 | if (replaced_dlg != NULL && pjsua_var.ua_cfg.cb.on_call_replace_request) { |
| 885 | pjsua_call *replaced_call; |
| 886 | int st_code = 200; |
| 887 | pj_str_t st_text = { "OK", 2 }; |
| 888 | |
| 889 | /* Get the replaced call instance */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 890 | replaced_call = (pjsua_call*) replaced_dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 891 | |
| 892 | /* Notify application */ |
| 893 | pjsua_var.ua_cfg.cb.on_call_replace_request(replaced_call->index, |
| 894 | rdata, &st_code, &st_text); |
| 895 | |
| 896 | /* Must specify final response */ |
| 897 | PJ_ASSERT_ON_FAIL(st_code >= 200, st_code = 200); |
| 898 | |
| 899 | /* Check if application rejects this request. */ |
| 900 | if (st_code >= 300) { |
| 901 | |
| 902 | if (st_text.slen == 2) |
| 903 | st_text = *pjsip_get_status_text(st_code); |
| 904 | |
| 905 | pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, |
| 906 | st_code, &st_text, NULL, NULL, NULL); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 907 | goto on_return; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 911 | /* |
| 912 | * Get which account is most likely to be associated with this incoming |
| 913 | * call. We need the account to find which contact URI to put for |
| 914 | * the call. |
| 915 | */ |
| 916 | acc_id = call->acc_id = pjsua_acc_find_for_incoming(rdata); |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame] | 917 | call->call_hold_type = pjsua_var.acc[acc_id].cfg.call_hold_type; |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 918 | |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 919 | /* Get call's secure level */ |
| 920 | if (PJSIP_URI_SCHEME_IS_SIPS(rdata->msg_info.msg->line.req.uri)) |
| 921 | call->secure_level = 2; |
| 922 | else if (PJSIP_TRANSPORT_IS_SECURE(rdata->tp_info.transport)) |
| 923 | call->secure_level = 1; |
| 924 | else |
| 925 | call->secure_level = 0; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 926 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 927 | /* Parse SDP from incoming request */ |
| 928 | if (rdata->msg_info.msg->body) { |
Benny Prijono | 1c1d734 | 2010-08-01 09:48:51 +0000 | [diff] [blame] | 929 | pjsip_rdata_sdp_info *sdp_info; |
| 930 | |
| 931 | sdp_info = pjsip_rdata_get_sdp_info(rdata); |
| 932 | offer = sdp_info->sdp; |
| 933 | |
| 934 | status = sdp_info->sdp_err; |
| 935 | if (status==PJ_SUCCESS && sdp_info->sdp==NULL) |
| 936 | status = PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE); |
Benny Prijono | 2c484e4 | 2008-06-26 19:47:23 +0000 | [diff] [blame] | 937 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 938 | if (status != PJ_SUCCESS) { |
Benny Prijono | 617b860 | 2008-04-07 10:10:31 +0000 | [diff] [blame] | 939 | const pj_str_t reason = pj_str("Bad SDP"); |
Benny Prijono | 2c484e4 | 2008-06-26 19:47:23 +0000 | [diff] [blame] | 940 | pjsip_hdr hdr_list; |
| 941 | pjsip_warning_hdr *w; |
| 942 | |
| 943 | pjsua_perror(THIS_FILE, "Bad SDP in incoming INVITE", |
Benny Prijono | 617b860 | 2008-04-07 10:10:31 +0000 | [diff] [blame] | 944 | status); |
Benny Prijono | 2c484e4 | 2008-06-26 19:47:23 +0000 | [diff] [blame] | 945 | |
| 946 | w = pjsip_warning_hdr_create_from_status(rdata->tp_info.pool, |
| 947 | pjsip_endpt_name(pjsua_var.endpt), |
| 948 | status); |
| 949 | pj_list_init(&hdr_list); |
| 950 | pj_list_push_back(&hdr_list, w); |
| 951 | |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 952 | pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, 400, |
Benny Prijono | 2c484e4 | 2008-06-26 19:47:23 +0000 | [diff] [blame] | 953 | &reason, &hdr_list, NULL, NULL); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 954 | goto on_return; |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 955 | } |
Benny Prijono | 617b860 | 2008-04-07 10:10:31 +0000 | [diff] [blame] | 956 | |
| 957 | /* Do quick checks on SDP before passing it to transports. More elabore |
| 958 | * checks will be done in pjsip_inv_verify_request2() below. |
| 959 | */ |
| 960 | if (offer->media_count==0) { |
| 961 | const pj_str_t reason = pj_str("Missing media in SDP"); |
| 962 | pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, 400, &reason, |
| 963 | NULL, NULL, NULL); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 964 | goto on_return; |
Benny Prijono | 617b860 | 2008-04-07 10:10:31 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 967 | } else { |
| 968 | offer = NULL; |
| 969 | } |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 970 | |
| 971 | /* Verify that we can handle the request. */ |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 972 | options |= PJSIP_INV_SUPPORT_100REL; |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 973 | options |= PJSIP_INV_SUPPORT_TIMER; |
Sauw Ming | e7dbbc8 | 2011-10-24 09:28:13 +0000 | [diff] [blame^] | 974 | if (pjsua_var.acc[acc_id].cfg.require_100rel == PJSUA_100REL_MANDATORY) |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 975 | options |= PJSIP_INV_REQUIRE_100REL; |
Benny Prijono | 07fe230 | 2010-06-24 12:33:18 +0000 | [diff] [blame] | 976 | if (pjsua_var.media_cfg.enable_ice) |
| 977 | options |= PJSIP_INV_SUPPORT_ICE; |
Nanang Izzuddin | 742ef4b | 2010-09-07 09:36:15 +0000 | [diff] [blame] | 978 | if (pjsua_var.acc[acc_id].cfg.use_timer == PJSUA_SIP_TIMER_REQUIRED) |
| 979 | options |= PJSIP_INV_REQUIRE_TIMER; |
| 980 | else if (pjsua_var.acc[acc_id].cfg.use_timer == PJSUA_SIP_TIMER_ALWAYS) |
| 981 | options |= PJSIP_INV_ALWAYS_USE_TIMER; |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 982 | |
Sauw Ming | ec76535 | 2011-10-03 02:04:36 +0000 | [diff] [blame] | 983 | status = pjsip_inv_verify_request2(rdata, &options, offer, NULL, NULL, |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 984 | pjsua_var.endpt, &response); |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 985 | if (status != PJ_SUCCESS) { |
| 986 | |
| 987 | /* |
| 988 | * No we can't handle the incoming INVITE request. |
| 989 | */ |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 990 | if (response) { |
| 991 | pjsip_response_addr res_addr; |
| 992 | |
| 993 | pjsip_get_response_addr(response->pool, rdata, &res_addr); |
| 994 | pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, response, |
| 995 | NULL, NULL); |
| 996 | |
| 997 | } else { |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 998 | /* Respond with 500 (Internal Server Error) */ |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 999 | pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, 500, NULL, |
| 1000 | NULL, NULL, NULL); |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1003 | goto on_return; |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1006 | /* Get suitable Contact header */ |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 1007 | if (pjsua_var.acc[acc_id].contact.slen) { |
| 1008 | contact = pjsua_var.acc[acc_id].contact; |
| 1009 | } else { |
| 1010 | status = pjsua_acc_create_uas_contact(rdata->tp_info.pool, &contact, |
| 1011 | acc_id, rdata); |
| 1012 | if (status != PJ_SUCCESS) { |
| 1013 | pjsua_perror(THIS_FILE, "Unable to generate Contact header", |
| 1014 | status); |
| 1015 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL, |
| 1016 | NULL, NULL); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1017 | goto on_return; |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 1018 | } |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1021 | /* Create dialog: */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1022 | status = pjsip_dlg_create_uas( pjsip_ua_instance(), rdata, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1023 | &contact, &dlg); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1024 | if (status != PJ_SUCCESS) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1025 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1026 | NULL, NULL); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1027 | goto on_return; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1030 | /* Set credentials */ |
| 1031 | if (pjsua_var.acc[acc_id].cred_cnt) { |
| 1032 | pjsip_auth_clt_set_credentials(&dlg->auth_sess, |
| 1033 | pjsua_var.acc[acc_id].cred_cnt, |
| 1034 | pjsua_var.acc[acc_id].cred); |
| 1035 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1036 | |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 1037 | /* Set preference */ |
| 1038 | pjsip_auth_clt_set_prefs(&dlg->auth_sess, |
| 1039 | &pjsua_var.acc[acc_id].cfg.auth_pref); |
| 1040 | |
Nanang Izzuddin | 742ef4b | 2010-09-07 09:36:15 +0000 | [diff] [blame] | 1041 | /* Disable Session Timers if not prefered and the incoming INVITE request |
| 1042 | * did not require it. |
| 1043 | */ |
| 1044 | if (pjsua_var.acc[acc_id].cfg.use_timer == PJSUA_SIP_TIMER_INACTIVE && |
| 1045 | (options & PJSIP_INV_REQUIRE_TIMER) == 0) |
| 1046 | { |
| 1047 | options &= ~(PJSIP_INV_SUPPORT_TIMER); |
| 1048 | } |
| 1049 | |
Sauw Ming | e7dbbc8 | 2011-10-24 09:28:13 +0000 | [diff] [blame^] | 1050 | /* If 100rel is optional and UAC supports it, use it. */ |
| 1051 | if ((options & PJSIP_INV_REQUIRE_100REL)==0 && |
| 1052 | pjsua_var.acc[acc_id].cfg.require_100rel == PJSUA_100REL_OPTIONAL) |
| 1053 | { |
| 1054 | const pj_str_t token = { "100rel", 6}; |
| 1055 | pjsip_dialog_cap_status cap_status; |
| 1056 | |
| 1057 | cap_status = pjsip_dlg_remote_has_cap(dlg, PJSIP_H_SUPPORTED, NULL, |
| 1058 | &token); |
| 1059 | if (cap_status == PJSIP_DIALOG_CAP_SUPPORTED) |
| 1060 | options |= PJSIP_INV_REQUIRE_100REL; |
| 1061 | } |
| 1062 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1063 | /* Create invite session: */ |
Sauw Ming | ec76535 | 2011-10-03 02:04:36 +0000 | [diff] [blame] | 1064 | status = pjsip_inv_create_uas( dlg, rdata, NULL, options, &inv); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1065 | if (status != PJ_SUCCESS) { |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 1066 | pjsip_hdr hdr_list; |
| 1067 | pjsip_warning_hdr *w; |
| 1068 | |
| 1069 | w = pjsip_warning_hdr_create_from_status(dlg->pool, |
| 1070 | pjsip_endpt_name(pjsua_var.endpt), |
| 1071 | status); |
| 1072 | pj_list_init(&hdr_list); |
| 1073 | pj_list_push_back(&hdr_list, w); |
| 1074 | |
| 1075 | pjsip_dlg_respond(dlg, rdata, 500, NULL, &hdr_list, NULL); |
| 1076 | |
| 1077 | /* Can't terminate dialog because transaction is in progress. |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 1078 | pjsip_dlg_terminate(dlg); |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 1079 | */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1080 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1081 | goto on_return; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Sauw Ming | 903154f | 2011-10-03 08:22:48 +0000 | [diff] [blame] | 1084 | /* If account is locked to specific transport, then lock dialog |
| 1085 | * to this transport too. |
| 1086 | */ |
| 1087 | if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) { |
| 1088 | pjsip_tpselector tp_sel; |
| 1089 | |
| 1090 | pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel); |
| 1091 | pjsip_dlg_set_transport(dlg, &tp_sel); |
| 1092 | } |
| 1093 | |
Sauw Ming | ec76535 | 2011-10-03 02:04:36 +0000 | [diff] [blame] | 1094 | /* Create and attach pjsua_var data to the dialog: */ |
| 1095 | call->inv = inv; |
| 1096 | dlg->mod_data[pjsua_var.mod.id] = call; |
| 1097 | inv->mod_data[pjsua_var.mod.id] = call; |
| 1098 | |
| 1099 | /* Store variables required for the callback after the async |
| 1100 | * media transport creation is completed. |
| 1101 | */ |
| 1102 | call->async_call.dlg = dlg; |
| 1103 | pj_list_init(&call->async_call.call_var.inc_call.answers); |
| 1104 | |
| 1105 | /* Init media channel */ |
| 1106 | status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAS, |
| 1107 | call->secure_level, |
| 1108 | rdata->tp_info.pool, |
| 1109 | offer, |
| 1110 | &sip_err_code, PJ_TRUE, |
Sauw Ming | ec76535 | 2011-10-03 02:04:36 +0000 | [diff] [blame] | 1111 | &on_incoming_call_med_tp_complete); |
| 1112 | if (status == PJ_SUCCESS) { |
| 1113 | status = on_incoming_call_med_tp_complete(call_id, NULL); |
| 1114 | if (status != PJ_SUCCESS) { |
| 1115 | sip_err_code = PJSIP_SC_NOT_ACCEPTABLE; |
| 1116 | pjsip_dlg_respond(dlg, rdata, sip_err_code, NULL, NULL, NULL); |
| 1117 | goto on_return; |
| 1118 | } |
| 1119 | } else if (status != PJ_EPENDING) { |
| 1120 | pjsua_perror(THIS_FILE, "Error initializing media channel", status); |
| 1121 | pjsip_dlg_respond(dlg, rdata, sip_err_code, NULL, NULL, NULL); |
| 1122 | goto on_return; |
| 1123 | } |
| 1124 | |
| 1125 | /* Create answer */ |
| 1126 | /* |
| 1127 | status = pjsua_media_channel_create_sdp(call->index, rdata->tp_info.pool, |
| 1128 | offer, &answer, &sip_err_code); |
| 1129 | if (status != PJ_SUCCESS) { |
| 1130 | pjsua_perror(THIS_FILE, "Error creating SDP answer", status); |
| 1131 | pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, |
| 1132 | sip_err_code, NULL, NULL, NULL, NULL); |
| 1133 | goto on_return; |
| 1134 | } |
| 1135 | */ |
| 1136 | |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 1137 | /* Init Session Timers */ |
Nanang Izzuddin | 65add62 | 2009-08-11 16:26:20 +0000 | [diff] [blame] | 1138 | status = pjsip_timer_init_session(inv, |
| 1139 | &pjsua_var.acc[acc_id].cfg.timer_setting); |
| 1140 | if (status != PJ_SUCCESS) { |
| 1141 | pjsua_perror(THIS_FILE, "Session Timer init failed", status); |
| 1142 | status = pjsip_inv_end_session(inv, PJSIP_SC_INTERNAL_SERVER_ERROR, |
| 1143 | NULL, &response); |
| 1144 | if (status == PJ_SUCCESS && response) |
| 1145 | status = pjsip_inv_send_msg(inv, response); |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 1146 | |
Nanang Izzuddin | 65add62 | 2009-08-11 16:26:20 +0000 | [diff] [blame] | 1147 | pjsua_media_channel_deinit(call->index); |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 1148 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1149 | goto on_return; |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Benny Prijono | ea9fd39 | 2007-11-06 03:41:40 +0000 | [diff] [blame] | 1152 | /* Update NAT type of remote endpoint, only when there is SDP in |
| 1153 | * incoming INVITE! |
| 1154 | */ |
Sauw Ming | ec76535 | 2011-10-03 02:04:36 +0000 | [diff] [blame] | 1155 | if (pjsua_var.ua_cfg.nat_type_in_sdp && inv->neg && |
Benny Prijono | ea9fd39 | 2007-11-06 03:41:40 +0000 | [diff] [blame] | 1156 | pjmedia_sdp_neg_get_state(inv->neg) > PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) |
| 1157 | { |
Benny Prijono | 91a6a17 | 2007-10-31 08:59:29 +0000 | [diff] [blame] | 1158 | const pjmedia_sdp_session *remote_sdp; |
| 1159 | |
| 1160 | if (pjmedia_sdp_neg_get_neg_remote(inv->neg, &remote_sdp)==PJ_SUCCESS) |
| 1161 | update_remote_nat_type(call, remote_sdp); |
| 1162 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1163 | |
Benny Prijono | 2285e7e | 2008-12-17 14:28:18 +0000 | [diff] [blame] | 1164 | /* Must answer with some response to initial INVITE. We'll do this before |
| 1165 | * attaching the call to the invite session/dialog, so that the application |
| 1166 | * will not get notification about this event (on another scenario, it is |
| 1167 | * also possible that inv_send_msg() fails and causes the invite session to |
| 1168 | * be disconnected. If we have the call attached at this time, this will |
| 1169 | * cause the disconnection callback to be called before on_incoming_call() |
| 1170 | * callback is called, which is not right). |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1171 | */ |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1172 | status = pjsip_inv_initial_answer(inv, rdata, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1173 | 100, NULL, NULL, &response); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1174 | if (status != PJ_SUCCESS) { |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 1175 | if (response == NULL) { |
| 1176 | pjsua_perror(THIS_FILE, "Unable to send answer to incoming INVITE", |
| 1177 | status); |
| 1178 | pjsip_dlg_respond(dlg, rdata, 500, NULL, NULL, NULL); |
| 1179 | pjsip_inv_terminate(inv, 500, PJ_FALSE); |
| 1180 | } else { |
| 1181 | pjsip_inv_send_msg(inv, response); |
Nanang Izzuddin | 65add62 | 2009-08-11 16:26:20 +0000 | [diff] [blame] | 1182 | pjsip_inv_terminate(inv, response->msg->line.status.code, |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 1183 | PJ_FALSE); |
| 1184 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1185 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1186 | goto on_return; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1187 | |
| 1188 | } else { |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 1189 | status = pjsip_inv_send_msg(inv, response); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1190 | if (status != PJ_SUCCESS) { |
Benny Prijono | d2ae29d | 2006-02-22 15:42:31 +0000 | [diff] [blame] | 1191 | pjsua_perror(THIS_FILE, "Unable to send 100 response", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1192 | goto on_return; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1193 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1196 | ++pjsua_var.call_cnt; |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1197 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1198 | /* Check if this request should replace existing call */ |
| 1199 | if (replaced_dlg) { |
| 1200 | pjsip_inv_session *replaced_inv; |
| 1201 | struct pjsua_call *replaced_call; |
| 1202 | pjsip_tx_data *tdata; |
| 1203 | |
| 1204 | /* Get the invite session in the dialog */ |
| 1205 | replaced_inv = pjsip_dlg_get_inv_session(replaced_dlg); |
| 1206 | |
| 1207 | /* Get the replaced call instance */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1208 | replaced_call = (pjsua_call*) replaced_dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1209 | |
| 1210 | /* Notify application */ |
| 1211 | if (pjsua_var.ua_cfg.cb.on_call_replaced) |
| 1212 | pjsua_var.ua_cfg.cb.on_call_replaced(replaced_call->index, |
| 1213 | call_id); |
| 1214 | |
| 1215 | PJ_LOG(4,(THIS_FILE, "Answering replacement call %d with 200/OK", |
| 1216 | call_id)); |
| 1217 | |
| 1218 | /* Answer the new call with 200 response */ |
| 1219 | status = pjsip_inv_answer(inv, 200, NULL, NULL, &tdata); |
| 1220 | if (status == PJ_SUCCESS) |
| 1221 | status = pjsip_inv_send_msg(inv, tdata); |
| 1222 | |
| 1223 | if (status != PJ_SUCCESS) |
| 1224 | pjsua_perror(THIS_FILE, "Error answering session", status); |
| 1225 | |
Benny Prijono | fed7f4c | 2007-03-27 11:01:45 +0000 | [diff] [blame] | 1226 | /* Note that inv may be invalid if 200/OK has caused error in |
| 1227 | * starting the media. |
| 1228 | */ |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1229 | |
| 1230 | PJ_LOG(4,(THIS_FILE, "Disconnecting replaced call %d", |
| 1231 | replaced_call->index)); |
| 1232 | |
| 1233 | /* Disconnect replaced invite session */ |
| 1234 | status = pjsip_inv_end_session(replaced_inv, PJSIP_SC_GONE, NULL, |
| 1235 | &tdata); |
| 1236 | if (status == PJ_SUCCESS && tdata) |
| 1237 | status = pjsip_inv_send_msg(replaced_inv, tdata); |
| 1238 | |
| 1239 | if (status != PJ_SUCCESS) |
| 1240 | pjsua_perror(THIS_FILE, "Error terminating session", status); |
| 1241 | |
| 1242 | |
| 1243 | } else { |
| 1244 | |
Benny Prijono | b5388cf | 2007-01-04 22:45:08 +0000 | [diff] [blame] | 1245 | /* Notify application if on_incoming_call() is overriden, |
| 1246 | * otherwise hangup the call with 480 |
| 1247 | */ |
| 1248 | if (pjsua_var.ua_cfg.cb.on_incoming_call) { |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1249 | pjsua_var.ua_cfg.cb.on_incoming_call(acc_id, call_id, rdata); |
Benny Prijono | b5388cf | 2007-01-04 22:45:08 +0000 | [diff] [blame] | 1250 | } else { |
| 1251 | pjsua_call_hangup(call_id, PJSIP_SC_TEMPORARILY_UNAVAILABLE, |
| 1252 | NULL, NULL); |
| 1253 | } |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1256 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1257 | /* This INVITE request has been handled. */ |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1258 | on_return: |
| 1259 | pj_log_pop_indent(); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1260 | PJSUA_UNLOCK(); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1261 | return PJ_TRUE; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1265 | |
| 1266 | /* |
| 1267 | * Check if the specified call has active INVITE session and the INVITE |
| 1268 | * session has not been disconnected. |
| 1269 | */ |
| 1270 | PJ_DEF(pj_bool_t) pjsua_call_is_active(pjsua_call_id call_id) |
| 1271 | { |
| 1272 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1273 | PJ_EINVAL); |
| 1274 | return pjsua_var.calls[call_id].inv != NULL && |
| 1275 | pjsua_var.calls[call_id].inv->state != PJSIP_INV_STATE_DISCONNECTED; |
| 1276 | } |
| 1277 | |
| 1278 | |
| 1279 | /* |
| 1280 | * Check if call has an active media session. |
| 1281 | */ |
| 1282 | PJ_DEF(pj_bool_t) pjsua_call_has_media(pjsua_call_id call_id) |
| 1283 | { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1284 | pjsua_call *call = &pjsua_var.calls[call_id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1285 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1286 | PJ_EINVAL); |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1287 | return call->audio_idx >= 0 && call->media[call->audio_idx].strm.a.stream; |
Benny Prijono | cf986c4 | 2008-09-02 11:25:07 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1291 | /* Acquire lock to the specified call_id */ |
Benny Prijono | 627cbb4 | 2007-09-25 20:48:49 +0000 | [diff] [blame] | 1292 | pj_status_t acquire_call(const char *title, |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1293 | pjsua_call_id call_id, |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1294 | pjsua_call **p_call, |
| 1295 | pjsip_dialog **p_dlg) |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1296 | { |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1297 | unsigned retry; |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 1298 | pjsua_call *call = NULL; |
| 1299 | pj_bool_t has_pjsua_lock = PJ_FALSE; |
| 1300 | pj_status_t status = PJ_SUCCESS; |
Sauw Ming | 844c1c9 | 2010-09-07 05:12:02 +0000 | [diff] [blame] | 1301 | pj_time_val time_start, timeout; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1302 | |
Sauw Ming | 844c1c9 | 2010-09-07 05:12:02 +0000 | [diff] [blame] | 1303 | pj_gettimeofday(&time_start); |
Sauw Ming | e7dbbc8 | 2011-10-24 09:28:13 +0000 | [diff] [blame^] | 1304 | timeout.sec = 0; |
Sauw Ming | 844c1c9 | 2010-09-07 05:12:02 +0000 | [diff] [blame] | 1305 | timeout.msec = PJSUA_ACQUIRE_CALL_TIMEOUT; |
| 1306 | pj_time_val_normalize(&timeout); |
| 1307 | |
| 1308 | for (retry=0; ; ++retry) { |
| 1309 | |
| 1310 | if (retry % 10 == 9) { |
| 1311 | pj_time_val dtime; |
| 1312 | |
| 1313 | pj_gettimeofday(&dtime); |
| 1314 | PJ_TIME_VAL_SUB(dtime, time_start); |
| 1315 | if (!PJ_TIME_VAL_LT(dtime, timeout)) |
| 1316 | break; |
| 1317 | } |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1318 | |
| 1319 | has_pjsua_lock = PJ_FALSE; |
| 1320 | |
| 1321 | status = PJSUA_TRY_LOCK(); |
| 1322 | if (status != PJ_SUCCESS) { |
| 1323 | pj_thread_sleep(retry/10); |
| 1324 | continue; |
| 1325 | } |
| 1326 | |
| 1327 | has_pjsua_lock = PJ_TRUE; |
| 1328 | call = &pjsua_var.calls[call_id]; |
| 1329 | |
| 1330 | if (call->inv == NULL) { |
| 1331 | PJSUA_UNLOCK(); |
| 1332 | PJ_LOG(3,(THIS_FILE, "Invalid call_id %d in %s", call_id, title)); |
| 1333 | return PJSIP_ESESSIONTERMINATED; |
| 1334 | } |
| 1335 | |
| 1336 | status = pjsip_dlg_try_inc_lock(call->inv->dlg); |
| 1337 | if (status != PJ_SUCCESS) { |
| 1338 | PJSUA_UNLOCK(); |
| 1339 | pj_thread_sleep(retry/10); |
| 1340 | continue; |
| 1341 | } |
| 1342 | |
| 1343 | PJSUA_UNLOCK(); |
| 1344 | |
| 1345 | break; |
| 1346 | } |
| 1347 | |
| 1348 | if (status != PJ_SUCCESS) { |
| 1349 | if (has_pjsua_lock == PJ_FALSE) |
| 1350 | PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire PJSUA mutex " |
| 1351 | "(possibly system has deadlocked) in %s", |
| 1352 | title)); |
| 1353 | else |
| 1354 | PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire dialog mutex " |
| 1355 | "(possibly system has deadlocked) in %s", |
| 1356 | title)); |
| 1357 | return PJ_ETIMEDOUT; |
| 1358 | } |
| 1359 | |
| 1360 | *p_call = call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1361 | *p_dlg = call->inv->dlg; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1362 | |
| 1363 | return PJ_SUCCESS; |
| 1364 | } |
| 1365 | |
| 1366 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1367 | /* |
| 1368 | * Get the conference port identification associated with the call. |
| 1369 | */ |
| 1370 | PJ_DEF(pjsua_conf_port_id) pjsua_call_get_conf_port(pjsua_call_id call_id) |
| 1371 | { |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1372 | pjsua_call *call; |
Sauw Ming | e7dbbc8 | 2011-10-24 09:28:13 +0000 | [diff] [blame^] | 1373 | pjsua_conf_port_id port_id = PJSUA_INVALID_ID; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1374 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1375 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1376 | PJ_EINVAL); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1377 | |
Sauw Ming | e7dbbc8 | 2011-10-24 09:28:13 +0000 | [diff] [blame^] | 1378 | /* Use PJSUA_LOCK() instead of acquire_call(): |
| 1379 | * https://trac.pjsip.org/repos/ticket/1371 |
| 1380 | */ |
| 1381 | PJSUA_LOCK(); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1382 | |
Sauw Ming | e7dbbc8 | 2011-10-24 09:28:13 +0000 | [diff] [blame^] | 1383 | if (!pjsua_call_is_active(call_id)) |
| 1384 | goto on_return; |
| 1385 | |
| 1386 | call = &pjsua_var.calls[call_id]; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1387 | port_id = call->media[call->audio_idx].strm.a.conf_slot; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1388 | |
Sauw Ming | e7dbbc8 | 2011-10-24 09:28:13 +0000 | [diff] [blame^] | 1389 | on_return: |
| 1390 | PJSUA_UNLOCK(); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1391 | |
| 1392 | return port_id; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1396 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1397 | /* |
| 1398 | * Obtain detail information about the specified call. |
| 1399 | */ |
| 1400 | PJ_DEF(pj_status_t) pjsua_call_get_info( pjsua_call_id call_id, |
| 1401 | pjsua_call_info *info) |
| 1402 | { |
| 1403 | pjsua_call *call; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1404 | unsigned mi; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1405 | |
| 1406 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1407 | PJ_EINVAL); |
| 1408 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 1409 | pj_bzero(info, sizeof(*info)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1410 | |
Sauw Ming | e7dbbc8 | 2011-10-24 09:28:13 +0000 | [diff] [blame^] | 1411 | /* Use PJSUA_LOCK() instead of acquire_call(): |
| 1412 | * https://trac.pjsip.org/repos/ticket/1371 |
| 1413 | */ |
| 1414 | PJSUA_LOCK(); |
| 1415 | |
| 1416 | call = &pjsua_var.calls[call_id]; |
| 1417 | |
| 1418 | if (!call->inv) { |
| 1419 | PJSUA_UNLOCK(); |
| 1420 | return PJSIP_ESESSIONTERMINATED; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1423 | /* id and role */ |
| 1424 | info->id = call_id; |
| 1425 | info->role = call->inv->role; |
Benny Prijono | 9031551 | 2006-09-14 16:05:16 +0000 | [diff] [blame] | 1426 | info->acc_id = call->acc_id; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1427 | |
| 1428 | /* local info */ |
| 1429 | info->local_info.ptr = info->buf_.local_info; |
| 1430 | pj_strncpy(&info->local_info, &call->inv->dlg->local.info_str, |
| 1431 | sizeof(info->buf_.local_info)); |
| 1432 | |
| 1433 | /* local contact */ |
| 1434 | info->local_contact.ptr = info->buf_.local_contact; |
| 1435 | info->local_contact.slen = pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, |
| 1436 | call->inv->dlg->local.contact->uri, |
| 1437 | info->local_contact.ptr, |
| 1438 | sizeof(info->buf_.local_contact)); |
| 1439 | |
| 1440 | /* remote info */ |
| 1441 | info->remote_info.ptr = info->buf_.remote_info; |
| 1442 | pj_strncpy(&info->remote_info, &call->inv->dlg->remote.info_str, |
| 1443 | sizeof(info->buf_.remote_info)); |
| 1444 | |
| 1445 | /* remote contact */ |
| 1446 | if (call->inv->dlg->remote.contact) { |
| 1447 | int len; |
| 1448 | info->remote_contact.ptr = info->buf_.remote_contact; |
| 1449 | len = pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, |
| 1450 | call->inv->dlg->remote.contact->uri, |
| 1451 | info->remote_contact.ptr, |
| 1452 | sizeof(info->buf_.remote_contact)); |
| 1453 | if (len < 0) len = 0; |
| 1454 | info->remote_contact.slen = len; |
| 1455 | } else { |
| 1456 | info->remote_contact.slen = 0; |
| 1457 | } |
| 1458 | |
| 1459 | /* call id */ |
| 1460 | info->call_id.ptr = info->buf_.call_id; |
| 1461 | pj_strncpy(&info->call_id, &call->inv->dlg->call_id->id, |
| 1462 | sizeof(info->buf_.call_id)); |
| 1463 | |
| 1464 | /* state, state_text */ |
| 1465 | info->state = call->inv->state; |
| 1466 | info->state_text = pj_str((char*)pjsip_inv_state_name(info->state)); |
| 1467 | |
| 1468 | /* If call is disconnected, set the last_status from the cause code */ |
| 1469 | if (call->inv->state >= PJSIP_INV_STATE_DISCONNECTED) { |
| 1470 | /* last_status, last_status_text */ |
| 1471 | info->last_status = call->inv->cause; |
| 1472 | |
| 1473 | info->last_status_text.ptr = info->buf_.last_status_text; |
| 1474 | pj_strncpy(&info->last_status_text, &call->inv->cause_text, |
| 1475 | sizeof(info->buf_.last_status_text)); |
| 1476 | } else { |
| 1477 | /* last_status, last_status_text */ |
| 1478 | info->last_status = call->last_code; |
| 1479 | |
| 1480 | info->last_status_text.ptr = info->buf_.last_status_text; |
| 1481 | pj_strncpy(&info->last_status_text, &call->last_text, |
| 1482 | sizeof(info->buf_.last_status_text)); |
| 1483 | } |
| 1484 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1485 | /* Build array of media status and dir */ |
Nanang Izzuddin | bf26db1 | 2011-03-18 07:54:50 +0000 | [diff] [blame] | 1486 | info->media_cnt = 0; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1487 | for (mi=0; mi < call->med_cnt && |
Nanang Izzuddin | bf26db1 | 2011-03-18 07:54:50 +0000 | [diff] [blame] | 1488 | info->media_cnt < PJ_ARRAY_SIZE(info->media); ++mi) |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1489 | { |
| 1490 | pjsua_call_media *call_med = &call->media[mi]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1491 | |
Nanang Izzuddin | bf26db1 | 2011-03-18 07:54:50 +0000 | [diff] [blame] | 1492 | info->media[info->media_cnt].index = mi; |
| 1493 | info->media[info->media_cnt].status = call_med->state; |
| 1494 | info->media[info->media_cnt].dir = call_med->dir; |
| 1495 | info->media[info->media_cnt].type = call_med->type; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1496 | |
Nanang Izzuddin | bf26db1 | 2011-03-18 07:54:50 +0000 | [diff] [blame] | 1497 | if (call_med->type == PJMEDIA_TYPE_AUDIO) { |
Benny Prijono | 9f468d1 | 2011-07-07 07:46:33 +0000 | [diff] [blame] | 1498 | info->media[info->media_cnt].stream.aud.conf_slot = |
Nanang Izzuddin | bf26db1 | 2011-03-18 07:54:50 +0000 | [diff] [blame] | 1499 | call_med->strm.a.conf_slot; |
| 1500 | } else if (call_med->type == PJMEDIA_TYPE_VIDEO) { |
Nanang Izzuddin | 62053a6 | 2011-07-12 11:08:32 +0000 | [diff] [blame] | 1501 | pjmedia_vid_dev_index cap_dev = PJMEDIA_VID_INVALID_DEV; |
| 1502 | |
| 1503 | info->media[info->media_cnt].stream.vid.win_in = |
| 1504 | call_med->strm.v.rdr_win_id; |
| 1505 | |
| 1506 | if (call_med->strm.v.cap_win_id != PJSUA_INVALID_ID) { |
Nanang Izzuddin | d93c68a | 2011-07-19 08:40:20 +0000 | [diff] [blame] | 1507 | cap_dev = call_med->strm.v.cap_dev; |
Nanang Izzuddin | 62053a6 | 2011-07-12 11:08:32 +0000 | [diff] [blame] | 1508 | } |
| 1509 | info->media[info->media_cnt].stream.vid.cap_dev = cap_dev; |
Nanang Izzuddin | bf26db1 | 2011-03-18 07:54:50 +0000 | [diff] [blame] | 1510 | } else { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1511 | continue; |
Nanang Izzuddin | bf26db1 | 2011-03-18 07:54:50 +0000 | [diff] [blame] | 1512 | } |
| 1513 | ++info->media_cnt; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1514 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1515 | |
Nanang Izzuddin | bf26db1 | 2011-03-18 07:54:50 +0000 | [diff] [blame] | 1516 | if (call->audio_idx != -1) { |
| 1517 | info->media_status = call->media[call->audio_idx].state; |
| 1518 | info->media_dir = call->media[call->audio_idx].dir; |
| 1519 | info->conf_slot = call->media[call->audio_idx].strm.a.conf_slot; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1520 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1521 | |
| 1522 | /* calculate duration */ |
| 1523 | if (info->state >= PJSIP_INV_STATE_DISCONNECTED) { |
| 1524 | |
| 1525 | info->total_duration = call->dis_time; |
| 1526 | PJ_TIME_VAL_SUB(info->total_duration, call->start_time); |
| 1527 | |
| 1528 | if (call->conn_time.sec) { |
| 1529 | info->connect_duration = call->dis_time; |
| 1530 | PJ_TIME_VAL_SUB(info->connect_duration, call->conn_time); |
| 1531 | } |
| 1532 | |
| 1533 | } else if (info->state == PJSIP_INV_STATE_CONFIRMED) { |
| 1534 | |
| 1535 | pj_gettimeofday(&info->total_duration); |
| 1536 | PJ_TIME_VAL_SUB(info->total_duration, call->start_time); |
| 1537 | |
| 1538 | pj_gettimeofday(&info->connect_duration); |
| 1539 | PJ_TIME_VAL_SUB(info->connect_duration, call->conn_time); |
| 1540 | |
| 1541 | } else { |
| 1542 | pj_gettimeofday(&info->total_duration); |
| 1543 | PJ_TIME_VAL_SUB(info->total_duration, call->start_time); |
| 1544 | } |
| 1545 | |
Sauw Ming | e7dbbc8 | 2011-10-24 09:28:13 +0000 | [diff] [blame^] | 1546 | PJSUA_UNLOCK(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1547 | |
| 1548 | return PJ_SUCCESS; |
| 1549 | } |
| 1550 | |
Nanang Izzuddin | 2a1b9ee | 2010-06-03 10:41:32 +0000 | [diff] [blame] | 1551 | /* |
| 1552 | * Check if call remote peer support the specified capability. |
| 1553 | */ |
| 1554 | PJ_DEF(pjsip_dialog_cap_status) pjsua_call_remote_has_cap( |
| 1555 | pjsua_call_id call_id, |
| 1556 | int htype, |
| 1557 | const pj_str_t *hname, |
| 1558 | const pj_str_t *token) |
| 1559 | { |
| 1560 | pjsua_call *call; |
| 1561 | pjsip_dialog *dlg; |
| 1562 | pj_status_t status; |
| 1563 | pjsip_dialog_cap_status cap_status; |
| 1564 | |
| 1565 | status = acquire_call("pjsua_call_peer_has_cap()", call_id, &call, &dlg); |
| 1566 | if (status != PJ_SUCCESS) |
| 1567 | return PJSIP_DIALOG_CAP_UNKNOWN; |
| 1568 | |
| 1569 | cap_status = pjsip_dlg_remote_has_cap(dlg, htype, hname, token); |
| 1570 | |
| 1571 | pjsip_dlg_dec_lock(dlg); |
| 1572 | |
| 1573 | return cap_status; |
| 1574 | } |
| 1575 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1576 | |
| 1577 | /* |
| 1578 | * Attach application specific data to the call. |
| 1579 | */ |
| 1580 | PJ_DEF(pj_status_t) pjsua_call_set_user_data( pjsua_call_id call_id, |
| 1581 | void *user_data) |
| 1582 | { |
| 1583 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1584 | PJ_EINVAL); |
| 1585 | pjsua_var.calls[call_id].user_data = user_data; |
| 1586 | |
| 1587 | return PJ_SUCCESS; |
| 1588 | } |
| 1589 | |
| 1590 | |
| 1591 | /* |
| 1592 | * Get user data attached to the call. |
| 1593 | */ |
| 1594 | PJ_DEF(void*) pjsua_call_get_user_data(pjsua_call_id call_id) |
| 1595 | { |
| 1596 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1597 | NULL); |
| 1598 | return pjsua_var.calls[call_id].user_data; |
| 1599 | } |
| 1600 | |
| 1601 | |
| 1602 | /* |
Benny Prijono | 91a6a17 | 2007-10-31 08:59:29 +0000 | [diff] [blame] | 1603 | * Get remote's NAT type. |
| 1604 | */ |
| 1605 | PJ_DEF(pj_status_t) pjsua_call_get_rem_nat_type(pjsua_call_id call_id, |
| 1606 | pj_stun_nat_type *p_type) |
| 1607 | { |
| 1608 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1609 | PJ_EINVAL); |
| 1610 | PJ_ASSERT_RETURN(p_type != NULL, PJ_EINVAL); |
| 1611 | |
| 1612 | *p_type = pjsua_var.calls[call_id].rem_nat_type; |
| 1613 | return PJ_SUCCESS; |
| 1614 | } |
| 1615 | |
| 1616 | |
| 1617 | /* |
Nanang Izzuddin | f5269a2 | 2011-07-14 04:42:18 +0000 | [diff] [blame] | 1618 | * Get media stream info for the specified media index. |
| 1619 | */ |
| 1620 | PJ_DEF(pj_status_t) pjsua_call_get_stream_info( pjsua_call_id call_id, |
| 1621 | unsigned med_idx, |
| 1622 | pjsua_stream_info *psi) |
| 1623 | { |
| 1624 | pjsua_call *call; |
| 1625 | pjsua_call_media *call_med; |
| 1626 | pj_status_t status; |
| 1627 | |
| 1628 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1629 | PJ_EINVAL); |
| 1630 | PJ_ASSERT_RETURN(psi, PJ_EINVAL); |
| 1631 | |
| 1632 | PJSUA_LOCK(); |
| 1633 | |
| 1634 | call = &pjsua_var.calls[call_id]; |
| 1635 | |
| 1636 | if (med_idx >= call->med_cnt) { |
| 1637 | PJSUA_UNLOCK(); |
| 1638 | return PJ_EINVAL; |
| 1639 | } |
| 1640 | |
| 1641 | call_med = &call->media[med_idx]; |
| 1642 | psi->type = call_med->type; |
| 1643 | switch (call_med->type) { |
| 1644 | case PJMEDIA_TYPE_AUDIO: |
| 1645 | status = pjmedia_stream_get_info(call_med->strm.a.stream, |
| 1646 | &psi->info.aud); |
| 1647 | break; |
Nanang Izzuddin | 63b3c13 | 2011-07-19 11:11:07 +0000 | [diff] [blame] | 1648 | #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) |
Nanang Izzuddin | f5269a2 | 2011-07-14 04:42:18 +0000 | [diff] [blame] | 1649 | case PJMEDIA_TYPE_VIDEO: |
| 1650 | status = pjmedia_vid_stream_get_info(call_med->strm.v.stream, |
| 1651 | &psi->info.vid); |
| 1652 | break; |
Nanang Izzuddin | 63b3c13 | 2011-07-19 11:11:07 +0000 | [diff] [blame] | 1653 | #endif |
Nanang Izzuddin | f5269a2 | 2011-07-14 04:42:18 +0000 | [diff] [blame] | 1654 | default: |
| 1655 | status = PJMEDIA_EINVALIMEDIATYPE; |
| 1656 | break; |
| 1657 | } |
| 1658 | |
| 1659 | PJSUA_UNLOCK(); |
| 1660 | return status; |
| 1661 | } |
| 1662 | |
| 1663 | |
| 1664 | /* |
| 1665 | * Get media stream statistic for the specified media index. |
| 1666 | */ |
| 1667 | PJ_DEF(pj_status_t) pjsua_call_get_stream_stat( pjsua_call_id call_id, |
| 1668 | unsigned med_idx, |
| 1669 | pjsua_stream_stat *stat) |
| 1670 | { |
| 1671 | pjsua_call *call; |
| 1672 | pjsua_call_media *call_med; |
| 1673 | pj_status_t status; |
| 1674 | |
| 1675 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1676 | PJ_EINVAL); |
| 1677 | PJ_ASSERT_RETURN(stat, PJ_EINVAL); |
| 1678 | |
| 1679 | PJSUA_LOCK(); |
| 1680 | |
| 1681 | call = &pjsua_var.calls[call_id]; |
| 1682 | |
| 1683 | if (med_idx >= call->med_cnt) { |
| 1684 | PJSUA_UNLOCK(); |
| 1685 | return PJ_EINVAL; |
| 1686 | } |
| 1687 | |
| 1688 | call_med = &call->media[med_idx]; |
| 1689 | switch (call_med->type) { |
| 1690 | case PJMEDIA_TYPE_AUDIO: |
| 1691 | status = pjmedia_stream_get_stat(call_med->strm.a.stream, |
| 1692 | &stat->rtcp); |
| 1693 | if (status == PJ_SUCCESS) |
| 1694 | status = pjmedia_stream_get_stat_jbuf(call_med->strm.a.stream, |
| 1695 | &stat->jbuf); |
| 1696 | break; |
Nanang Izzuddin | 63b3c13 | 2011-07-19 11:11:07 +0000 | [diff] [blame] | 1697 | #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) |
Nanang Izzuddin | f5269a2 | 2011-07-14 04:42:18 +0000 | [diff] [blame] | 1698 | case PJMEDIA_TYPE_VIDEO: |
| 1699 | status = pjmedia_vid_stream_get_stat(call_med->strm.v.stream, |
| 1700 | &stat->rtcp); |
| 1701 | if (status == PJ_SUCCESS) |
| 1702 | status = pjmedia_vid_stream_get_stat_jbuf(call_med->strm.v.stream, |
| 1703 | &stat->jbuf); |
| 1704 | break; |
Nanang Izzuddin | 63b3c13 | 2011-07-19 11:11:07 +0000 | [diff] [blame] | 1705 | #endif |
Nanang Izzuddin | f5269a2 | 2011-07-14 04:42:18 +0000 | [diff] [blame] | 1706 | default: |
| 1707 | status = PJMEDIA_EINVALIMEDIATYPE; |
| 1708 | break; |
| 1709 | } |
| 1710 | |
| 1711 | PJSUA_UNLOCK(); |
| 1712 | return status; |
| 1713 | } |
| 1714 | |
| 1715 | |
| 1716 | /* |
| 1717 | * Get media transport info for the specified media index. |
| 1718 | */ |
Benny Prijono | e212bc1 | 2011-08-15 09:38:42 +0000 | [diff] [blame] | 1719 | PJ_DEF(pj_status_t) |
| 1720 | pjsua_call_get_med_transport_info(pjsua_call_id call_id, |
| 1721 | unsigned med_idx, |
| 1722 | pjmedia_transport_info *t) |
Nanang Izzuddin | f5269a2 | 2011-07-14 04:42:18 +0000 | [diff] [blame] | 1723 | { |
| 1724 | pjsua_call *call; |
| 1725 | pjsua_call_media *call_med; |
| 1726 | pj_status_t status; |
| 1727 | |
| 1728 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1729 | PJ_EINVAL); |
| 1730 | PJ_ASSERT_RETURN(t, PJ_EINVAL); |
| 1731 | |
| 1732 | PJSUA_LOCK(); |
| 1733 | |
| 1734 | call = &pjsua_var.calls[call_id]; |
| 1735 | |
| 1736 | if (med_idx >= call->med_cnt) { |
| 1737 | PJSUA_UNLOCK(); |
| 1738 | return PJ_EINVAL; |
| 1739 | } |
| 1740 | |
| 1741 | call_med = &call->media[med_idx]; |
| 1742 | |
| 1743 | pjmedia_transport_info_init(t); |
| 1744 | status = pjmedia_transport_get_info(call_med->tp, t); |
| 1745 | |
| 1746 | PJSUA_UNLOCK(); |
| 1747 | return status; |
| 1748 | } |
| 1749 | |
| 1750 | |
| 1751 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1752 | * Send response to incoming INVITE request. |
| 1753 | */ |
| 1754 | PJ_DEF(pj_status_t) pjsua_call_answer( pjsua_call_id call_id, |
| 1755 | unsigned code, |
| 1756 | const pj_str_t *reason, |
| 1757 | const pjsua_msg_data *msg_data) |
| 1758 | { |
| 1759 | pjsua_call *call; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1760 | pjsip_dialog *dlg = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1761 | pjsip_tx_data *tdata; |
| 1762 | pj_status_t status; |
| 1763 | |
| 1764 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1765 | PJ_EINVAL); |
| 1766 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1767 | PJ_LOG(4,(THIS_FILE, "Answering call %d: code=%d", call_id, code)); |
| 1768 | pj_log_push_indent(); |
| 1769 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1770 | status = acquire_call("pjsua_call_answer()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1771 | if (status != PJ_SUCCESS) |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1772 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1773 | |
Sauw Ming | ec76535 | 2011-10-03 02:04:36 +0000 | [diff] [blame] | 1774 | PJSUA_LOCK(); |
| 1775 | /* If media transport creation is not yet completed, we will answer |
| 1776 | * the call in the media transport creation callback instead. |
| 1777 | */ |
| 1778 | if (call->med_ch_cb) { |
| 1779 | struct call_answer *answer; |
| 1780 | |
| 1781 | PJ_LOG(4,(THIS_FILE, "Pending answering call %d upon completion " |
| 1782 | "of media transport", call_id)); |
| 1783 | |
| 1784 | answer = PJ_POOL_ZALLOC_T(call->inv->pool_prov, struct call_answer); |
| 1785 | answer->code = code; |
| 1786 | if (reason) { |
| 1787 | pj_strdup(call->inv->pool_prov, answer->reason, reason); |
| 1788 | } |
| 1789 | if (msg_data) { |
| 1790 | answer->msg_data = pjsua_msg_data_clone(call->inv->pool_prov, |
| 1791 | msg_data); |
| 1792 | } |
| 1793 | pj_list_push_back(&call->async_call.call_var.inc_call.answers, |
| 1794 | answer); |
| 1795 | |
| 1796 | PJSUA_UNLOCK(); |
| 1797 | if (dlg) pjsip_dlg_dec_lock(dlg); |
| 1798 | pj_log_pop_indent(); |
| 1799 | return status; |
| 1800 | } |
| 1801 | PJSUA_UNLOCK(); |
| 1802 | |
Benny Prijono | 2e507c2 | 2006-06-23 15:04:11 +0000 | [diff] [blame] | 1803 | if (call->res_time.sec == 0) |
| 1804 | pj_gettimeofday(&call->res_time); |
| 1805 | |
Benny Prijono | ed7a5a7 | 2007-01-29 18:36:38 +0000 | [diff] [blame] | 1806 | if (reason && reason->slen == 0) |
| 1807 | reason = NULL; |
| 1808 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1809 | /* Create response message */ |
| 1810 | status = pjsip_inv_answer(call->inv, code, reason, NULL, &tdata); |
| 1811 | if (status != PJ_SUCCESS) { |
| 1812 | pjsua_perror(THIS_FILE, "Error creating response", |
| 1813 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1814 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
Benny Prijono | fed7f4c | 2007-03-27 11:01:45 +0000 | [diff] [blame] | 1817 | /* Call might have been disconnected if application is answering with |
| 1818 | * 200/OK and the media failed to start. |
| 1819 | */ |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1820 | if (call->inv == NULL) |
| 1821 | goto on_return; |
Benny Prijono | fed7f4c | 2007-03-27 11:01:45 +0000 | [diff] [blame] | 1822 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1823 | /* Add additional headers etc */ |
| 1824 | pjsua_process_msg_data( tdata, msg_data); |
| 1825 | |
| 1826 | /* Send the message */ |
| 1827 | status = pjsip_inv_send_msg(call->inv, tdata); |
| 1828 | if (status != PJ_SUCCESS) |
| 1829 | pjsua_perror(THIS_FILE, "Error sending response", |
| 1830 | status); |
| 1831 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1832 | on_return: |
| 1833 | if (dlg) pjsip_dlg_dec_lock(dlg); |
| 1834 | pj_log_pop_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1835 | return status; |
| 1836 | } |
| 1837 | |
| 1838 | |
| 1839 | /* |
| 1840 | * Hangup call by using method that is appropriate according to the |
| 1841 | * call state. |
| 1842 | */ |
| 1843 | PJ_DEF(pj_status_t) pjsua_call_hangup(pjsua_call_id call_id, |
| 1844 | unsigned code, |
| 1845 | const pj_str_t *reason, |
| 1846 | const pjsua_msg_data *msg_data) |
| 1847 | { |
| 1848 | pjsua_call *call; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1849 | pjsip_dialog *dlg = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1850 | pj_status_t status; |
| 1851 | pjsip_tx_data *tdata; |
| 1852 | |
| 1853 | |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1854 | if (call_id<0 || call_id>=(int)pjsua_var.ua_cfg.max_calls) { |
| 1855 | PJ_LOG(1,(THIS_FILE, "pjsua_call_hangup(): invalid call id %d", |
| 1856 | call_id)); |
| 1857 | } |
| 1858 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1859 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1860 | PJ_EINVAL); |
| 1861 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1862 | PJ_LOG(4,(THIS_FILE, "Call %d hanging up: code=%d..", call_id, code)); |
| 1863 | pj_log_push_indent(); |
| 1864 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1865 | status = acquire_call("pjsua_call_hangup()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1866 | if (status != PJ_SUCCESS) |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1867 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1868 | |
| 1869 | if (code==0) { |
| 1870 | if (call->inv->state == PJSIP_INV_STATE_CONFIRMED) |
| 1871 | code = PJSIP_SC_OK; |
| 1872 | else if (call->inv->role == PJSIP_ROLE_UAS) |
| 1873 | code = PJSIP_SC_DECLINE; |
| 1874 | else |
| 1875 | code = PJSIP_SC_REQUEST_TERMINATED; |
| 1876 | } |
| 1877 | |
| 1878 | status = pjsip_inv_end_session(call->inv, code, reason, &tdata); |
| 1879 | if (status != PJ_SUCCESS) { |
| 1880 | pjsua_perror(THIS_FILE, |
| 1881 | "Failed to create end session message", |
| 1882 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1883 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | /* pjsip_inv_end_session may return PJ_SUCCESS with NULL |
| 1887 | * as p_tdata when INVITE transaction has not been answered |
| 1888 | * with any provisional responses. |
| 1889 | */ |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1890 | if (tdata == NULL) |
| 1891 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1892 | |
| 1893 | /* Add additional headers etc */ |
| 1894 | pjsua_process_msg_data( tdata, msg_data); |
| 1895 | |
| 1896 | /* Send the message */ |
| 1897 | status = pjsip_inv_send_msg(call->inv, tdata); |
| 1898 | if (status != PJ_SUCCESS) { |
| 1899 | pjsua_perror(THIS_FILE, |
| 1900 | "Failed to send end session message", |
| 1901 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1902 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 1905 | /* Stop lock codec timer, if it is active */ |
| 1906 | if (call->lock_codec.reinv_timer.id) { |
| 1907 | pjsip_endpt_cancel_timer(pjsua_var.endpt, |
| 1908 | &call->lock_codec.reinv_timer); |
| 1909 | call->lock_codec.reinv_timer.id = PJ_FALSE; |
| 1910 | } |
| 1911 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1912 | on_return: |
| 1913 | if (dlg) pjsip_dlg_dec_lock(dlg); |
| 1914 | pj_log_pop_indent(); |
| 1915 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | |
| 1919 | /* |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 1920 | * Accept or reject redirection. |
| 1921 | */ |
| 1922 | PJ_DEF(pj_status_t) pjsua_call_process_redirect( pjsua_call_id call_id, |
| 1923 | pjsip_redirect_op cmd) |
| 1924 | { |
| 1925 | pjsua_call *call; |
| 1926 | pjsip_dialog *dlg; |
| 1927 | pj_status_t status; |
| 1928 | |
| 1929 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1930 | PJ_EINVAL); |
| 1931 | |
| 1932 | status = acquire_call("pjsua_call_process_redirect()", call_id, |
| 1933 | &call, &dlg); |
| 1934 | if (status != PJ_SUCCESS) |
| 1935 | return status; |
| 1936 | |
| 1937 | status = pjsip_inv_process_redirect(call->inv, cmd, NULL); |
| 1938 | |
| 1939 | pjsip_dlg_dec_lock(dlg); |
| 1940 | |
| 1941 | return status; |
| 1942 | } |
| 1943 | |
| 1944 | |
| 1945 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1946 | * Put the specified call on hold. |
| 1947 | */ |
| 1948 | PJ_DEF(pj_status_t) pjsua_call_set_hold(pjsua_call_id call_id, |
| 1949 | const pjsua_msg_data *msg_data) |
| 1950 | { |
| 1951 | pjmedia_sdp_session *sdp; |
| 1952 | pjsua_call *call; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1953 | pjsip_dialog *dlg = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1954 | pjsip_tx_data *tdata; |
| 1955 | pj_status_t status; |
| 1956 | |
| 1957 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1958 | PJ_EINVAL); |
| 1959 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1960 | PJ_LOG(4,(THIS_FILE, "Putting call %d on hold", call_id)); |
| 1961 | pj_log_push_indent(); |
| 1962 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1963 | status = acquire_call("pjsua_call_set_hold()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1964 | if (status != PJ_SUCCESS) |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1965 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1966 | |
| 1967 | if (call->inv->state != PJSIP_INV_STATE_CONFIRMED) { |
| 1968 | PJ_LOG(3,(THIS_FILE, "Can not hold call that is not confirmed")); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1969 | status = PJSIP_ESESSIONSTATE; |
| 1970 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 1973 | status = create_sdp_of_call_hold(call, &sdp); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1974 | if (status != PJ_SUCCESS) |
| 1975 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1976 | |
| 1977 | /* Create re-INVITE with new offer */ |
| 1978 | status = pjsip_inv_reinvite( call->inv, NULL, sdp, &tdata); |
| 1979 | if (status != PJ_SUCCESS) { |
| 1980 | pjsua_perror(THIS_FILE, "Unable to create re-INVITE", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1981 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | /* Add additional headers etc */ |
| 1985 | pjsua_process_msg_data( tdata, msg_data); |
| 1986 | |
Sauw Ming | e7dbbc8 | 2011-10-24 09:28:13 +0000 | [diff] [blame^] | 1987 | /* Record the tx_data to keep track the operation */ |
| 1988 | call->hold_msg = (void*) tdata; |
| 1989 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1990 | /* Send the request */ |
| 1991 | status = pjsip_inv_send_msg( call->inv, tdata); |
| 1992 | if (status != PJ_SUCCESS) { |
| 1993 | pjsua_perror(THIS_FILE, "Unable to send re-INVITE", status); |
Sauw Ming | e7dbbc8 | 2011-10-24 09:28:13 +0000 | [diff] [blame^] | 1994 | call->hold_msg = NULL; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1995 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 1998 | /* Set flag that local put the call on hold */ |
| 1999 | call->local_hold = PJ_TRUE; |
| 2000 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2001 | on_return: |
| 2002 | if (dlg) pjsip_dlg_dec_lock(dlg); |
| 2003 | pj_log_pop_indent(); |
| 2004 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
| 2007 | |
| 2008 | /* |
| 2009 | * Send re-INVITE (to release hold). |
| 2010 | */ |
| 2011 | PJ_DEF(pj_status_t) pjsua_call_reinvite( pjsua_call_id call_id, |
Benny Prijono | dec3a37 | 2011-03-16 03:52:20 +0000 | [diff] [blame] | 2012 | unsigned options, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2013 | const pjsua_msg_data *msg_data) |
| 2014 | { |
| 2015 | pjmedia_sdp_session *sdp; |
Benny Prijono | dec3a37 | 2011-03-16 03:52:20 +0000 | [diff] [blame] | 2016 | pj_str_t *new_contact = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2017 | pjsip_tx_data *tdata; |
| 2018 | pjsua_call *call; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2019 | pjsip_dialog *dlg = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2020 | pj_status_t status; |
| 2021 | |
| 2022 | |
| 2023 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 2024 | PJ_EINVAL); |
| 2025 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2026 | PJ_LOG(4,(THIS_FILE, "Sending re-INVITE on call %d", call_id)); |
| 2027 | pj_log_push_indent(); |
| 2028 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 2029 | status = acquire_call("pjsua_call_reinvite()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 2030 | if (status != PJ_SUCCESS) |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2031 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2032 | |
| 2033 | if (call->inv->state != PJSIP_INV_STATE_CONFIRMED) { |
| 2034 | PJ_LOG(3,(THIS_FILE, "Can not re-INVITE call that is not confirmed")); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2035 | status = PJSIP_ESESSIONSTATE; |
| 2036 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | /* Create SDP */ |
Benny Prijono | dec3a37 | 2011-03-16 03:52:20 +0000 | [diff] [blame] | 2040 | if (call->local_hold && (options & PJSUA_CALL_UNHOLD)==0) { |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 2041 | status = create_sdp_of_call_hold(call, &sdp); |
| 2042 | } else { |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 2043 | status = pjsua_media_channel_create_sdp(call->index, |
| 2044 | call->inv->pool_prov, |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 2045 | NULL, &sdp, NULL); |
| 2046 | call->local_hold = PJ_FALSE; |
| 2047 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2048 | if (status != PJ_SUCCESS) { |
| 2049 | pjsua_perror(THIS_FILE, "Unable to get SDP from media endpoint", |
| 2050 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2051 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
Benny Prijono | dec3a37 | 2011-03-16 03:52:20 +0000 | [diff] [blame] | 2054 | if ((options & PJSUA_CALL_UPDATE_CONTACT) & |
| 2055 | pjsua_acc_is_valid(call->acc_id)) |
| 2056 | { |
| 2057 | new_contact = &pjsua_var.acc[call->acc_id].contact; |
| 2058 | } |
| 2059 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2060 | /* Create re-INVITE with new offer */ |
Benny Prijono | dec3a37 | 2011-03-16 03:52:20 +0000 | [diff] [blame] | 2061 | status = pjsip_inv_reinvite( call->inv, new_contact, sdp, &tdata); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2062 | if (status != PJ_SUCCESS) { |
| 2063 | pjsua_perror(THIS_FILE, "Unable to create re-INVITE", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2064 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | /* Add additional headers etc */ |
| 2068 | pjsua_process_msg_data( tdata, msg_data); |
| 2069 | |
| 2070 | /* Send the request */ |
| 2071 | status = pjsip_inv_send_msg( call->inv, tdata); |
| 2072 | if (status != PJ_SUCCESS) { |
| 2073 | pjsua_perror(THIS_FILE, "Unable to send re-INVITE", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2074 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2077 | on_return: |
Nanang Izzuddin | c12a19d | 2011-10-03 05:23:59 +0000 | [diff] [blame] | 2078 | if (dlg) pjsip_dlg_dec_lock(dlg); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2079 | pj_log_pop_indent(); |
| 2080 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
| 2083 | |
| 2084 | /* |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 2085 | * Send UPDATE request. |
| 2086 | */ |
| 2087 | PJ_DEF(pj_status_t) pjsua_call_update( pjsua_call_id call_id, |
| 2088 | unsigned options, |
| 2089 | const pjsua_msg_data *msg_data) |
| 2090 | { |
| 2091 | pjmedia_sdp_session *sdp; |
Benny Prijono | dec3a37 | 2011-03-16 03:52:20 +0000 | [diff] [blame] | 2092 | pj_str_t *new_contact = NULL; |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 2093 | pjsip_tx_data *tdata; |
| 2094 | pjsua_call *call; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2095 | pjsip_dialog *dlg = NULL; |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 2096 | pj_status_t status; |
| 2097 | |
| 2098 | PJ_UNUSED_ARG(options); |
| 2099 | |
| 2100 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 2101 | PJ_EINVAL); |
| 2102 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2103 | PJ_LOG(4,(THIS_FILE, "Sending UPDATE on call %d", call_id)); |
| 2104 | pj_log_push_indent(); |
| 2105 | |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 2106 | status = acquire_call("pjsua_call_update()", call_id, &call, &dlg); |
| 2107 | if (status != PJ_SUCCESS) |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2108 | goto on_return; |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 2109 | |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 2110 | /* Create SDP */ |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 2111 | status = pjsua_media_channel_create_sdp(call->index, |
| 2112 | call->inv->pool_prov, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 2113 | NULL, &sdp, NULL); |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 2114 | if (status != PJ_SUCCESS) { |
| 2115 | pjsua_perror(THIS_FILE, "Unable to get SDP from media endpoint", |
| 2116 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2117 | goto on_return; |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
Benny Prijono | dec3a37 | 2011-03-16 03:52:20 +0000 | [diff] [blame] | 2120 | if ((options & PJSUA_CALL_UPDATE_CONTACT) & |
| 2121 | pjsua_acc_is_valid(call->acc_id)) |
| 2122 | { |
| 2123 | new_contact = &pjsua_var.acc[call->acc_id].contact; |
| 2124 | } |
| 2125 | |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 2126 | /* Create UPDATE with new offer */ |
Benny Prijono | dec3a37 | 2011-03-16 03:52:20 +0000 | [diff] [blame] | 2127 | status = pjsip_inv_update(call->inv, new_contact, sdp, &tdata); |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 2128 | if (status != PJ_SUCCESS) { |
| 2129 | pjsua_perror(THIS_FILE, "Unable to create UPDATE request", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2130 | goto on_return; |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
| 2133 | /* Add additional headers etc */ |
| 2134 | pjsua_process_msg_data( tdata, msg_data); |
| 2135 | |
| 2136 | /* Send the request */ |
| 2137 | status = pjsip_inv_send_msg( call->inv, tdata); |
| 2138 | if (status != PJ_SUCCESS) { |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 2139 | pjsua_perror(THIS_FILE, "Unable to send UPDATE request", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2140 | goto on_return; |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 2141 | } |
| 2142 | |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 2143 | call->local_hold = PJ_FALSE; |
| 2144 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2145 | on_return: |
| 2146 | if (dlg) pjsip_dlg_dec_lock(dlg); |
| 2147 | pj_log_pop_indent(); |
| 2148 | return status; |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | |
| 2152 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2153 | * Initiate call transfer to the specified address. |
| 2154 | */ |
| 2155 | PJ_DEF(pj_status_t) pjsua_call_xfer( pjsua_call_id call_id, |
| 2156 | const pj_str_t *dest, |
| 2157 | const pjsua_msg_data *msg_data) |
| 2158 | { |
| 2159 | pjsip_evsub *sub; |
| 2160 | pjsip_tx_data *tdata; |
| 2161 | pjsua_call *call; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2162 | pjsip_dialog *dlg = NULL; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2163 | pjsip_generic_string_hdr *gs_hdr; |
| 2164 | const pj_str_t str_ref_by = { "Referred-By", 11 }; |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 2165 | struct pjsip_evsub_user xfer_cb; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2166 | pj_status_t status; |
| 2167 | |
| 2168 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2169 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls && |
| 2170 | dest, PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2171 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2172 | PJ_LOG(4,(THIS_FILE, "Transfering call %d to %.*s", call_id, |
| 2173 | (int)dest->slen, dest->ptr)); |
| 2174 | pj_log_push_indent(); |
| 2175 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 2176 | status = acquire_call("pjsua_call_xfer()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 2177 | if (status != PJ_SUCCESS) |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2178 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2179 | |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 2180 | /* Create xfer client subscription. */ |
| 2181 | pj_bzero(&xfer_cb, sizeof(xfer_cb)); |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 2182 | xfer_cb.on_evsub_state = &xfer_client_on_evsub_state; |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 2183 | |
| 2184 | status = pjsip_xfer_create_uac(call->inv->dlg, &xfer_cb, &sub); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2185 | if (status != PJ_SUCCESS) { |
| 2186 | pjsua_perror(THIS_FILE, "Unable to create xfer", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2187 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 2190 | /* Associate this call with the client subscription */ |
| 2191 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, call); |
| 2192 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2193 | /* |
| 2194 | * Create REFER request. |
| 2195 | */ |
| 2196 | status = pjsip_xfer_initiate(sub, dest, &tdata); |
| 2197 | if (status != PJ_SUCCESS) { |
| 2198 | pjsua_perror(THIS_FILE, "Unable to create REFER request", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2199 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2202 | /* Add Referred-By header */ |
| 2203 | gs_hdr = pjsip_generic_string_hdr_create(tdata->pool, &str_ref_by, |
| 2204 | &dlg->local.info_str); |
| 2205 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)gs_hdr); |
| 2206 | |
| 2207 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2208 | /* Add additional headers etc */ |
| 2209 | pjsua_process_msg_data( tdata, msg_data); |
| 2210 | |
| 2211 | /* Send. */ |
| 2212 | status = pjsip_xfer_send_request(sub, tdata); |
| 2213 | if (status != PJ_SUCCESS) { |
| 2214 | pjsua_perror(THIS_FILE, "Unable to send REFER request", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2215 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | /* For simplicity (that's what this program is intended to be!), |
| 2219 | * leave the original invite session as it is. More advanced application |
| 2220 | * may want to hold the INVITE, or terminate the invite, or whatever. |
| 2221 | */ |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2222 | on_return: |
| 2223 | if (dlg) pjsip_dlg_dec_lock(dlg); |
| 2224 | pj_log_pop_indent(); |
| 2225 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2226 | |
| 2227 | } |
| 2228 | |
| 2229 | |
| 2230 | /* |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2231 | * Initiate attended call transfer to the specified address. |
| 2232 | */ |
| 2233 | PJ_DEF(pj_status_t) pjsua_call_xfer_replaces( pjsua_call_id call_id, |
| 2234 | pjsua_call_id dest_call_id, |
| 2235 | unsigned options, |
| 2236 | const pjsua_msg_data *msg_data) |
| 2237 | { |
| 2238 | pjsua_call *dest_call; |
| 2239 | pjsip_dialog *dest_dlg; |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 2240 | char str_dest_buf[PJSIP_MAX_URL_SIZE*2]; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2241 | pj_str_t str_dest; |
| 2242 | int len; |
| 2243 | pjsip_uri *uri; |
| 2244 | pj_status_t status; |
| 2245 | |
| 2246 | |
| 2247 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 2248 | PJ_EINVAL); |
| 2249 | PJ_ASSERT_RETURN(dest_call_id>=0 && |
| 2250 | dest_call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 2251 | PJ_EINVAL); |
| 2252 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2253 | PJ_LOG(4,(THIS_FILE, "Transfering call %d replacing with call %d", |
| 2254 | call_id, dest_call_id)); |
| 2255 | pj_log_push_indent(); |
| 2256 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2257 | status = acquire_call("pjsua_call_xfer_replaces()", dest_call_id, |
| 2258 | &dest_call, &dest_dlg); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2259 | if (status != PJ_SUCCESS) { |
| 2260 | pj_log_pop_indent(); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2261 | return status; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2262 | } |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2263 | |
| 2264 | /* |
| 2265 | * Create REFER destination URI with Replaces field. |
| 2266 | */ |
| 2267 | |
| 2268 | /* Make sure we have sufficient buffer's length */ |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2269 | PJ_ASSERT_ON_FAIL(dest_dlg->remote.info_str.slen + |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2270 | dest_dlg->call_id->id.slen + |
| 2271 | dest_dlg->remote.info->tag.slen + |
| 2272 | dest_dlg->local.info->tag.slen + 32 |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2273 | < (long)sizeof(str_dest_buf), |
| 2274 | { status=PJSIP_EURITOOLONG; goto on_error; }); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2275 | |
| 2276 | /* Print URI */ |
| 2277 | str_dest_buf[0] = '<'; |
| 2278 | str_dest.slen = 1; |
| 2279 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2280 | uri = (pjsip_uri*) pjsip_uri_get_uri(dest_dlg->remote.info->uri); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2281 | len = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, uri, |
| 2282 | str_dest_buf+1, sizeof(str_dest_buf)-1); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2283 | if (len < 0) { |
| 2284 | status = PJSIP_EURITOOLONG; |
| 2285 | goto on_error; |
| 2286 | } |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2287 | |
| 2288 | str_dest.slen += len; |
| 2289 | |
| 2290 | |
| 2291 | /* Build the URI */ |
| 2292 | len = pj_ansi_snprintf(str_dest_buf + str_dest.slen, |
| 2293 | sizeof(str_dest_buf) - str_dest.slen, |
| 2294 | "?%s" |
| 2295 | "Replaces=%.*s" |
| 2296 | "%%3Bto-tag%%3D%.*s" |
| 2297 | "%%3Bfrom-tag%%3D%.*s>", |
| 2298 | ((options&PJSUA_XFER_NO_REQUIRE_REPLACES) ? |
| 2299 | "" : "Require=replaces&"), |
| 2300 | (int)dest_dlg->call_id->id.slen, |
| 2301 | dest_dlg->call_id->id.ptr, |
| 2302 | (int)dest_dlg->remote.info->tag.slen, |
| 2303 | dest_dlg->remote.info->tag.ptr, |
| 2304 | (int)dest_dlg->local.info->tag.slen, |
| 2305 | dest_dlg->local.info->tag.ptr); |
| 2306 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2307 | PJ_ASSERT_ON_FAIL(len > 0 && len <= (int)sizeof(str_dest_buf)-str_dest.slen, |
| 2308 | { status=PJSIP_EURITOOLONG; goto on_error; }); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2309 | |
| 2310 | str_dest.ptr = str_dest_buf; |
| 2311 | str_dest.slen += len; |
| 2312 | |
| 2313 | pjsip_dlg_dec_lock(dest_dlg); |
| 2314 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2315 | status = pjsua_call_xfer(call_id, &str_dest, msg_data); |
| 2316 | |
| 2317 | pj_log_pop_indent(); |
| 2318 | return status; |
| 2319 | |
| 2320 | on_error: |
| 2321 | if (dest_dlg) pjsip_dlg_dec_lock(dest_dlg); |
| 2322 | pj_log_pop_indent(); |
| 2323 | return status; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2324 | } |
| 2325 | |
| 2326 | |
| 2327 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2328 | * Send DTMF digits to remote using RFC 2833 payload formats. |
| 2329 | */ |
| 2330 | PJ_DEF(pj_status_t) pjsua_call_dial_dtmf( pjsua_call_id call_id, |
| 2331 | const pj_str_t *digits) |
| 2332 | { |
| 2333 | pjsua_call *call; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2334 | pjsip_dialog *dlg = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2335 | pj_status_t status; |
| 2336 | |
| 2337 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 2338 | PJ_EINVAL); |
| 2339 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2340 | PJ_LOG(4,(THIS_FILE, "Call %d dialing DTMF %.*s", |
| 2341 | call_id, (int)digits->slen, digits->ptr)); |
| 2342 | pj_log_push_indent(); |
| 2343 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 2344 | status = acquire_call("pjsua_call_dial_dtmf()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 2345 | if (status != PJ_SUCCESS) |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2346 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2347 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2348 | if (!pjsua_call_has_media(call_id)) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2349 | PJ_LOG(3,(THIS_FILE, "Media is not established yet!")); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2350 | status = PJ_EINVALIDOP; |
| 2351 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2354 | status = pjmedia_stream_dial_dtmf( |
| 2355 | call->media[call->audio_idx].strm.a.stream, digits); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2356 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2357 | on_return: |
| 2358 | if (dlg) pjsip_dlg_dec_lock(dlg); |
| 2359 | pj_log_pop_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2360 | return status; |
| 2361 | } |
| 2362 | |
| 2363 | |
| 2364 | /** |
| 2365 | * Send instant messaging inside INVITE session. |
| 2366 | */ |
| 2367 | PJ_DEF(pj_status_t) pjsua_call_send_im( pjsua_call_id call_id, |
| 2368 | const pj_str_t *mime_type, |
| 2369 | const pj_str_t *content, |
| 2370 | const pjsua_msg_data *msg_data, |
| 2371 | void *user_data) |
| 2372 | { |
| 2373 | pjsua_call *call; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2374 | pjsip_dialog *dlg = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2375 | const pj_str_t mime_text_plain = pj_str("text/plain"); |
| 2376 | pjsip_media_type ctype; |
| 2377 | pjsua_im_data *im_data; |
| 2378 | pjsip_tx_data *tdata; |
| 2379 | pj_status_t status; |
| 2380 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2381 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 2382 | PJ_EINVAL); |
| 2383 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2384 | PJ_LOG(4,(THIS_FILE, "Call %d sending %d bytes MESSAGE..", |
| 2385 | call_id, (int)content->slen)); |
| 2386 | pj_log_push_indent(); |
| 2387 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 2388 | status = acquire_call("pjsua_call_send_im()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 2389 | if (status != PJ_SUCCESS) |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2390 | goto on_return; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 2391 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2392 | /* Set default media type if none is specified */ |
| 2393 | if (mime_type == NULL) { |
| 2394 | mime_type = &mime_text_plain; |
| 2395 | } |
| 2396 | |
| 2397 | /* Create request message. */ |
| 2398 | status = pjsip_dlg_create_request( call->inv->dlg, &pjsip_message_method, |
| 2399 | -1, &tdata); |
| 2400 | if (status != PJ_SUCCESS) { |
| 2401 | pjsua_perror(THIS_FILE, "Unable to create MESSAGE request", status); |
| 2402 | goto on_return; |
| 2403 | } |
| 2404 | |
| 2405 | /* Add accept header. */ |
| 2406 | pjsip_msg_add_hdr( tdata->msg, |
| 2407 | (pjsip_hdr*)pjsua_im_create_accept(tdata->pool)); |
| 2408 | |
| 2409 | /* Parse MIME type */ |
| 2410 | pjsua_parse_media_type(tdata->pool, mime_type, &ctype); |
| 2411 | |
| 2412 | /* Create "text/plain" message body. */ |
| 2413 | tdata->msg->body = pjsip_msg_body_create( tdata->pool, &ctype.type, |
| 2414 | &ctype.subtype, content); |
| 2415 | if (tdata->msg->body == NULL) { |
| 2416 | pjsua_perror(THIS_FILE, "Unable to create msg body", PJ_ENOMEM); |
| 2417 | pjsip_tx_data_dec_ref(tdata); |
| 2418 | goto on_return; |
| 2419 | } |
| 2420 | |
| 2421 | /* Add additional headers etc */ |
| 2422 | pjsua_process_msg_data( tdata, msg_data); |
| 2423 | |
| 2424 | /* Create IM data and attach to the request. */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2425 | im_data = PJ_POOL_ZALLOC_T(tdata->pool, pjsua_im_data); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2426 | im_data->acc_id = call->acc_id; |
| 2427 | im_data->call_id = call_id; |
| 2428 | im_data->to = call->inv->dlg->remote.info_str; |
| 2429 | pj_strdup_with_null(tdata->pool, &im_data->body, content); |
| 2430 | im_data->user_data = user_data; |
| 2431 | |
| 2432 | |
| 2433 | /* Send the request. */ |
| 2434 | status = pjsip_dlg_send_request( call->inv->dlg, tdata, |
| 2435 | pjsua_var.mod.id, im_data); |
| 2436 | if (status != PJ_SUCCESS) { |
| 2437 | pjsua_perror(THIS_FILE, "Unable to send MESSAGE request", status); |
| 2438 | goto on_return; |
| 2439 | } |
| 2440 | |
| 2441 | on_return: |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2442 | if (dlg) pjsip_dlg_dec_lock(dlg); |
| 2443 | pj_log_pop_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2444 | return status; |
| 2445 | } |
| 2446 | |
| 2447 | |
| 2448 | /* |
| 2449 | * Send IM typing indication inside INVITE session. |
| 2450 | */ |
| 2451 | PJ_DEF(pj_status_t) pjsua_call_send_typing_ind( pjsua_call_id call_id, |
| 2452 | pj_bool_t is_typing, |
| 2453 | const pjsua_msg_data*msg_data) |
| 2454 | { |
| 2455 | pjsua_call *call; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2456 | pjsip_dialog *dlg = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2457 | pjsip_tx_data *tdata; |
| 2458 | pj_status_t status; |
| 2459 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2460 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 2461 | PJ_EINVAL); |
| 2462 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2463 | PJ_LOG(4,(THIS_FILE, "Call %d sending typing indication..", |
| 2464 | call_id)); |
| 2465 | pj_log_push_indent(); |
| 2466 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 2467 | status = acquire_call("pjsua_call_send_typing_ind", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 2468 | if (status != PJ_SUCCESS) |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2469 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2470 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2471 | /* Create request message. */ |
| 2472 | status = pjsip_dlg_create_request( call->inv->dlg, &pjsip_message_method, |
| 2473 | -1, &tdata); |
| 2474 | if (status != PJ_SUCCESS) { |
| 2475 | pjsua_perror(THIS_FILE, "Unable to create MESSAGE request", status); |
| 2476 | goto on_return; |
| 2477 | } |
| 2478 | |
| 2479 | /* Create "application/im-iscomposing+xml" msg body. */ |
| 2480 | tdata->msg->body = pjsip_iscomposing_create_body(tdata->pool, is_typing, |
| 2481 | NULL, NULL, -1); |
| 2482 | |
| 2483 | /* Add additional headers etc */ |
| 2484 | pjsua_process_msg_data( tdata, msg_data); |
| 2485 | |
| 2486 | /* Send the request. */ |
| 2487 | status = pjsip_dlg_send_request( call->inv->dlg, tdata, -1, NULL); |
| 2488 | if (status != PJ_SUCCESS) { |
| 2489 | pjsua_perror(THIS_FILE, "Unable to send MESSAGE request", status); |
| 2490 | goto on_return; |
| 2491 | } |
| 2492 | |
| 2493 | on_return: |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2494 | if (dlg) pjsip_dlg_dec_lock(dlg); |
| 2495 | pj_log_pop_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2496 | return status; |
| 2497 | } |
| 2498 | |
| 2499 | |
| 2500 | /* |
Benny Prijono | feb69f4 | 2007-10-05 09:12:26 +0000 | [diff] [blame] | 2501 | * Send arbitrary request. |
| 2502 | */ |
| 2503 | PJ_DEF(pj_status_t) pjsua_call_send_request(pjsua_call_id call_id, |
| 2504 | const pj_str_t *method_str, |
| 2505 | const pjsua_msg_data *msg_data) |
| 2506 | { |
| 2507 | pjsua_call *call; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2508 | pjsip_dialog *dlg = NULL; |
Benny Prijono | feb69f4 | 2007-10-05 09:12:26 +0000 | [diff] [blame] | 2509 | pjsip_method method; |
| 2510 | pjsip_tx_data *tdata; |
| 2511 | pj_status_t status; |
| 2512 | |
| 2513 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 2514 | PJ_EINVAL); |
| 2515 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2516 | PJ_LOG(4,(THIS_FILE, "Call %d sending %.*s request..", |
| 2517 | call_id, (int)method_str->slen, method_str->ptr)); |
| 2518 | pj_log_push_indent(); |
| 2519 | |
Benny Prijono | feb69f4 | 2007-10-05 09:12:26 +0000 | [diff] [blame] | 2520 | status = acquire_call("pjsua_call_send_request", call_id, &call, &dlg); |
| 2521 | if (status != PJ_SUCCESS) |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2522 | goto on_return; |
Benny Prijono | feb69f4 | 2007-10-05 09:12:26 +0000 | [diff] [blame] | 2523 | |
| 2524 | /* Init method */ |
| 2525 | pjsip_method_init_np(&method, (pj_str_t*)method_str); |
| 2526 | |
| 2527 | /* Create request message. */ |
| 2528 | status = pjsip_dlg_create_request( call->inv->dlg, &method, -1, &tdata); |
| 2529 | if (status != PJ_SUCCESS) { |
| 2530 | pjsua_perror(THIS_FILE, "Unable to create request", status); |
| 2531 | goto on_return; |
| 2532 | } |
| 2533 | |
| 2534 | /* Add additional headers etc */ |
| 2535 | pjsua_process_msg_data( tdata, msg_data); |
| 2536 | |
| 2537 | /* Send the request. */ |
| 2538 | status = pjsip_dlg_send_request( call->inv->dlg, tdata, -1, NULL); |
| 2539 | if (status != PJ_SUCCESS) { |
| 2540 | pjsua_perror(THIS_FILE, "Unable to send request", status); |
| 2541 | goto on_return; |
| 2542 | } |
| 2543 | |
| 2544 | on_return: |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2545 | if (dlg) pjsip_dlg_dec_lock(dlg); |
| 2546 | pj_log_pop_indent(); |
Benny Prijono | feb69f4 | 2007-10-05 09:12:26 +0000 | [diff] [blame] | 2547 | return status; |
| 2548 | } |
| 2549 | |
| 2550 | |
| 2551 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2552 | * Terminate all calls. |
| 2553 | */ |
| 2554 | PJ_DEF(void) pjsua_call_hangup_all(void) |
| 2555 | { |
| 2556 | unsigned i; |
| 2557 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2558 | PJ_LOG(4,(THIS_FILE, "Hangup all calls..")); |
| 2559 | pj_log_push_indent(); |
| 2560 | |
Sauw Ming | e7dbbc8 | 2011-10-24 09:28:13 +0000 | [diff] [blame^] | 2561 | // This may deadlock, see https://trac.pjsip.org/repos/ticket/1305 |
| 2562 | //PJSUA_LOCK(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2563 | |
| 2564 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 2565 | if (pjsua_var.calls[i].inv) |
| 2566 | pjsua_call_hangup(i, 0, NULL, NULL); |
| 2567 | } |
| 2568 | |
Sauw Ming | e7dbbc8 | 2011-10-24 09:28:13 +0000 | [diff] [blame^] | 2569 | //PJSUA_UNLOCK(); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2570 | pj_log_pop_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
| 2573 | |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 2574 | /* Proto */ |
| 2575 | static pj_status_t perform_lock_codec(pjsua_call *call); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2576 | |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 2577 | /* Timer callback to send re-INVITE or UPDATE to lock codec */ |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2578 | static void reinv_timer_cb(pj_timer_heap_t *th, |
| 2579 | pj_timer_entry *entry) |
| 2580 | { |
| 2581 | pjsua_call_id call_id = (pjsua_call_id)(pj_size_t)entry->user_data; |
| 2582 | pjsip_dialog *dlg; |
| 2583 | pjsua_call *call; |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2584 | pj_status_t status; |
| 2585 | |
| 2586 | PJ_UNUSED_ARG(th); |
| 2587 | |
| 2588 | pjsua_var.calls[call_id].lock_codec.reinv_timer.id = PJ_FALSE; |
| 2589 | |
| 2590 | status = acquire_call("reinv_timer_cb()", call_id, &call, &dlg); |
| 2591 | if (status != PJ_SUCCESS) |
| 2592 | return; |
| 2593 | |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 2594 | status = perform_lock_codec(call); |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2595 | |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2596 | pjsip_dlg_dec_lock(dlg); |
| 2597 | } |
| 2598 | |
| 2599 | |
| 2600 | /* Check if the specified format can be skipped in counting codecs */ |
| 2601 | static pj_bool_t is_non_av_fmt(const pjmedia_sdp_media *m, |
| 2602 | const pj_str_t *fmt) |
| 2603 | { |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 2604 | const pj_str_t STR_TEL = {"telephone-event", 15}; |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2605 | unsigned pt; |
| 2606 | |
| 2607 | pt = pj_strtoul(fmt); |
| 2608 | |
| 2609 | /* Check for comfort noise */ |
| 2610 | if (pt == PJMEDIA_RTP_PT_CN) |
| 2611 | return PJ_TRUE; |
| 2612 | |
| 2613 | /* Dynamic PT, check the format name */ |
| 2614 | if (pt >= 96) { |
| 2615 | pjmedia_sdp_attr *a; |
| 2616 | pjmedia_sdp_rtpmap rtpmap; |
| 2617 | |
| 2618 | /* Get the format name */ |
| 2619 | a = pjmedia_sdp_attr_find2(m->attr_count, m->attr, "rtpmap", fmt); |
| 2620 | if (a && pjmedia_sdp_attr_get_rtpmap(a, &rtpmap)==PJ_SUCCESS) { |
| 2621 | /* Check for telephone-event */ |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 2622 | if (pj_stricmp(&rtpmap.enc_name, &STR_TEL)==0) |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2623 | return PJ_TRUE; |
| 2624 | } else { |
| 2625 | /* Invalid SDP, should not reach here */ |
| 2626 | pj_assert(!"SDP should have been validated!"); |
| 2627 | return PJ_TRUE; |
| 2628 | } |
| 2629 | } |
| 2630 | |
| 2631 | return PJ_FALSE; |
| 2632 | } |
| 2633 | |
| 2634 | |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 2635 | /* Send re-INVITE or UPDATE with new SDP offer to select only one codec |
| 2636 | * out of several codecs presented by callee in his answer. |
| 2637 | */ |
| 2638 | static pj_status_t perform_lock_codec(pjsua_call *call) |
| 2639 | { |
| 2640 | const pj_str_t STR_UPDATE = {"UPDATE", 6}; |
| 2641 | const pjmedia_sdp_session *local_sdp = NULL, *new_sdp; |
Nanang Izzuddin | 5f32640 | 2011-05-20 08:47:14 +0000 | [diff] [blame] | 2642 | unsigned i; |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 2643 | pj_bool_t rem_can_update; |
Nanang Izzuddin | 5f32640 | 2011-05-20 08:47:14 +0000 | [diff] [blame] | 2644 | pj_bool_t need_lock_codec = PJ_FALSE; |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 2645 | pjsip_tx_data *tdata; |
| 2646 | pj_status_t status; |
| 2647 | |
| 2648 | PJ_ASSERT_RETURN(call->lock_codec.reinv_timer.id==PJ_FALSE, |
| 2649 | PJ_EINVALIDOP); |
| 2650 | |
| 2651 | /* Verify if another SDP negotiation is in progress, e.g: session timer |
| 2652 | * or another re-INVITE. |
| 2653 | */ |
| 2654 | if (call->inv==NULL || call->inv->neg==NULL || |
| 2655 | pjmedia_sdp_neg_get_state(call->inv->neg)!=PJMEDIA_SDP_NEG_STATE_DONE) |
| 2656 | { |
Nanang Izzuddin | ec91900 | 2010-11-25 09:27:06 +0000 | [diff] [blame] | 2657 | return PJMEDIA_SDPNEG_EINSTATE; |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
Benny Prijono | 0249327 | 2010-11-17 09:15:04 +0000 | [diff] [blame] | 2660 | /* Don't do this if call is disconnecting! */ |
| 2661 | if (call->inv->state > PJSIP_INV_STATE_CONFIRMED || |
| 2662 | call->inv->cause >= 200) |
| 2663 | { |
Nanang Izzuddin | ec91900 | 2010-11-25 09:27:06 +0000 | [diff] [blame] | 2664 | return PJ_EINVALIDOP; |
Benny Prijono | 0249327 | 2010-11-17 09:15:04 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 2667 | /* Verify if another SDP negotiation has been completed by comparing |
| 2668 | * the SDP version. |
| 2669 | */ |
| 2670 | status = pjmedia_sdp_neg_get_active_local(call->inv->neg, &local_sdp); |
| 2671 | if (status != PJ_SUCCESS) |
| 2672 | return status; |
| 2673 | if (local_sdp->origin.version > call->lock_codec.sdp_ver) |
| 2674 | return PJMEDIA_SDP_EINVER; |
| 2675 | |
| 2676 | PJ_LOG(3, (THIS_FILE, "Updating media session to use only one codec..")); |
| 2677 | |
| 2678 | /* Update the new offer so it contains only a codec. Note that formats |
| 2679 | * order in the offer should have been matched to the answer, so we can |
| 2680 | * just directly update the offer without looking-up the answer. |
| 2681 | */ |
| 2682 | new_sdp = pjmedia_sdp_session_clone(call->inv->pool_prov, local_sdp); |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 2683 | |
Nanang Izzuddin | 5f32640 | 2011-05-20 08:47:14 +0000 | [diff] [blame] | 2684 | for (i = 0; i < call->med_cnt; ++i) { |
| 2685 | unsigned j = 0, codec_cnt = 0; |
| 2686 | const pjmedia_sdp_media *ref_m; |
| 2687 | pjmedia_sdp_media *m; |
| 2688 | pjsua_call_media *call_med = &call->media[i]; |
| 2689 | |
| 2690 | /* Verify if media is deactivated */ |
| 2691 | if (call_med->state == PJSUA_CALL_MEDIA_NONE || |
| 2692 | call_med->state == PJSUA_CALL_MEDIA_ERROR || |
| 2693 | call_med->dir == PJMEDIA_DIR_NONE) |
| 2694 | { |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 2695 | continue; |
| 2696 | } |
| 2697 | |
Nanang Izzuddin | 5f32640 | 2011-05-20 08:47:14 +0000 | [diff] [blame] | 2698 | ref_m = local_sdp->media[i]; |
| 2699 | m = new_sdp->media[i]; |
| 2700 | |
| 2701 | /* Verify that media must be active. */ |
| 2702 | pj_assert(ref_m->desc.port); |
| 2703 | |
| 2704 | while (j < m->desc.fmt_count) { |
| 2705 | pjmedia_sdp_attr *a; |
| 2706 | pj_str_t *fmt = &m->desc.fmt[j]; |
| 2707 | |
| 2708 | if (is_non_av_fmt(m, fmt) || (++codec_cnt == 1)) { |
| 2709 | ++j; |
| 2710 | continue; |
| 2711 | } |
| 2712 | |
| 2713 | /* Remove format */ |
| 2714 | a = pjmedia_sdp_attr_find2(m->attr_count, m->attr, "rtpmap", fmt); |
| 2715 | if (a) pjmedia_sdp_attr_remove(&m->attr_count, m->attr, a); |
| 2716 | a = pjmedia_sdp_attr_find2(m->attr_count, m->attr, "fmtp", fmt); |
| 2717 | if (a) pjmedia_sdp_attr_remove(&m->attr_count, m->attr, a); |
| 2718 | pj_array_erase(m->desc.fmt, sizeof(m->desc.fmt[0]), |
| 2719 | m->desc.fmt_count, j); |
| 2720 | --m->desc.fmt_count; |
| 2721 | } |
| 2722 | |
| 2723 | need_lock_codec |= (ref_m->desc.fmt_count > m->desc.fmt_count); |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
Nanang Izzuddin | ec91900 | 2010-11-25 09:27:06 +0000 | [diff] [blame] | 2726 | /* Last check if SDP trully needs to be updated. It is possible that OA |
| 2727 | * negotiations have completed and SDP has changed but we didn't |
| 2728 | * increase the SDP version (should not happen!). |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 2729 | */ |
Nanang Izzuddin | 5f32640 | 2011-05-20 08:47:14 +0000 | [diff] [blame] | 2730 | if (!need_lock_codec) |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 2731 | return PJ_SUCCESS; |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 2732 | |
| 2733 | /* Send UPDATE or re-INVITE */ |
| 2734 | rem_can_update = pjsip_dlg_remote_has_cap(call->inv->dlg, |
| 2735 | PJSIP_H_ALLOW, |
| 2736 | NULL, &STR_UPDATE) == |
| 2737 | PJSIP_DIALOG_CAP_SUPPORTED; |
| 2738 | if (rem_can_update) { |
| 2739 | status = pjsip_inv_update(call->inv, NULL, new_sdp, &tdata); |
| 2740 | } else { |
| 2741 | status = pjsip_inv_reinvite(call->inv, NULL, new_sdp, &tdata); |
| 2742 | } |
| 2743 | |
| 2744 | if (status==PJ_EINVALIDOP && |
| 2745 | ++call->lock_codec.retry_cnt <= LOCK_CODEC_MAX_RETRY) |
| 2746 | { |
| 2747 | /* Ups, let's reschedule again */ |
| 2748 | pj_time_val delay = {0, LOCK_CODEC_RETRY_INTERVAL}; |
| 2749 | pj_time_val_normalize(&delay); |
| 2750 | call->lock_codec.reinv_timer.id = PJ_TRUE; |
| 2751 | pjsip_endpt_schedule_timer(pjsua_var.endpt, |
| 2752 | &call->lock_codec.reinv_timer, &delay); |
| 2753 | return status; |
| 2754 | } else if (status != PJ_SUCCESS) { |
| 2755 | pjsua_perror(THIS_FILE, "Error creating UPDATE/re-INVITE to lock codec", |
| 2756 | status); |
| 2757 | return status; |
| 2758 | } |
| 2759 | |
| 2760 | /* Send the UPDATE/re-INVITE request */ |
| 2761 | status = pjsip_inv_send_msg(call->inv, tdata); |
| 2762 | if (status != PJ_SUCCESS) { |
| 2763 | pjsua_perror(THIS_FILE, "Error sending UPDATE/re-INVITE in lock codec", |
| 2764 | status); |
| 2765 | return status; |
| 2766 | } |
| 2767 | |
| 2768 | return status; |
| 2769 | } |
| 2770 | |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2771 | /* Check if remote answerer has given us more than one codecs. If so, |
| 2772 | * create another offer with one codec only to lock down the codec. |
| 2773 | */ |
| 2774 | static pj_status_t lock_codec(pjsua_call *call) |
| 2775 | { |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2776 | pjsip_inv_session *inv = call->inv; |
Nanang Izzuddin | ec91900 | 2010-11-25 09:27:06 +0000 | [diff] [blame] | 2777 | const pjmedia_sdp_session *local_sdp, *remote_sdp; |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 2778 | pj_time_val delay = {0, 0}; |
Nanang Izzuddin | ec91900 | 2010-11-25 09:27:06 +0000 | [diff] [blame] | 2779 | const pj_str_t st_update = {"UPDATE", 6}; |
Nanang Izzuddin | 5f32640 | 2011-05-20 08:47:14 +0000 | [diff] [blame] | 2780 | unsigned i; |
| 2781 | pj_bool_t has_mult_fmt = PJ_FALSE; |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2782 | pj_status_t status; |
| 2783 | |
Nanang Izzuddin | ec91900 | 2010-11-25 09:27:06 +0000 | [diff] [blame] | 2784 | /* Stop lock codec timer, if it is active */ |
| 2785 | if (call->lock_codec.reinv_timer.id) { |
| 2786 | pjsip_endpt_cancel_timer(pjsua_var.endpt, |
| 2787 | &call->lock_codec.reinv_timer); |
| 2788 | call->lock_codec.reinv_timer.id = PJ_FALSE; |
| 2789 | } |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2790 | |
Nanang Izzuddin | ec91900 | 2010-11-25 09:27:06 +0000 | [diff] [blame] | 2791 | /* Skip this if we are the answerer */ |
| 2792 | if (!inv->neg || !pjmedia_sdp_neg_was_answer_remote(inv->neg)) { |
| 2793 | return PJ_SUCCESS; |
| 2794 | } |
| 2795 | |
Nanang Izzuddin | ec91900 | 2010-11-25 09:27:06 +0000 | [diff] [blame] | 2796 | /* Delay this when the SDP negotiation done in call state EARLY and |
| 2797 | * remote does not support UPDATE method. |
| 2798 | */ |
| 2799 | if (inv->state == PJSIP_INV_STATE_EARLY && |
| 2800 | pjsip_dlg_remote_has_cap(inv->dlg, PJSIP_H_ALLOW, NULL, &st_update)!= |
| 2801 | PJSIP_DIALOG_CAP_SUPPORTED) |
| 2802 | { |
| 2803 | call->lock_codec.pending = PJ_TRUE; |
| 2804 | return PJ_SUCCESS; |
| 2805 | } |
| 2806 | |
| 2807 | status = pjmedia_sdp_neg_get_active_local(inv->neg, &local_sdp); |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2808 | if (status != PJ_SUCCESS) |
| 2809 | return status; |
Nanang Izzuddin | ec91900 | 2010-11-25 09:27:06 +0000 | [diff] [blame] | 2810 | status = pjmedia_sdp_neg_get_active_remote(inv->neg, &remote_sdp); |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2811 | if (status != PJ_SUCCESS) |
| 2812 | return status; |
| 2813 | |
Nanang Izzuddin | 5f32640 | 2011-05-20 08:47:14 +0000 | [diff] [blame] | 2814 | /* Find multiple codecs answer in all media */ |
| 2815 | for (i = 0; i < call->med_cnt; ++i) { |
| 2816 | pjsua_call_media *call_med = &call->media[i]; |
| 2817 | const pjmedia_sdp_media *rem_m, *loc_m; |
| 2818 | unsigned codec_cnt = 0; |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2819 | |
Nanang Izzuddin | 5f32640 | 2011-05-20 08:47:14 +0000 | [diff] [blame] | 2820 | /* Skip this if the media is inactive or error */ |
| 2821 | if (call_med->state == PJSUA_CALL_MEDIA_NONE || |
| 2822 | call_med->state == PJSUA_CALL_MEDIA_ERROR || |
| 2823 | call_med->dir == PJMEDIA_DIR_NONE) |
| 2824 | { |
| 2825 | continue; |
| 2826 | } |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2827 | |
Nanang Izzuddin | 5f32640 | 2011-05-20 08:47:14 +0000 | [diff] [blame] | 2828 | /* Remote may answer with less media lines. */ |
| 2829 | if (i >= remote_sdp->media_count) |
| 2830 | continue; |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2831 | |
Nanang Izzuddin | 5f32640 | 2011-05-20 08:47:14 +0000 | [diff] [blame] | 2832 | rem_m = remote_sdp->media[i]; |
| 2833 | loc_m = local_sdp->media[i]; |
| 2834 | |
| 2835 | /* Verify that media must be active. */ |
| 2836 | pj_assert(loc_m->desc.port && rem_m->desc.port); |
| 2837 | |
| 2838 | /* Count the formats in the answer. */ |
| 2839 | if (rem_m->desc.fmt_count==1) { |
| 2840 | codec_cnt = 1; |
| 2841 | } else { |
| 2842 | unsigned j; |
| 2843 | for (j=0; j<rem_m->desc.fmt_count && codec_cnt <= 1; ++j) { |
| 2844 | if (!is_non_av_fmt(rem_m, &rem_m->desc.fmt[j])) |
| 2845 | ++codec_cnt; |
| 2846 | } |
| 2847 | } |
| 2848 | |
| 2849 | if (codec_cnt > 1) { |
| 2850 | has_mult_fmt = PJ_TRUE; |
| 2851 | break; |
| 2852 | } |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2853 | } |
Nanang Izzuddin | 5f32640 | 2011-05-20 08:47:14 +0000 | [diff] [blame] | 2854 | |
| 2855 | /* Each media in the answer already contains single codec. */ |
| 2856 | if (!has_mult_fmt) { |
| 2857 | call->lock_codec.retry_cnt = 0; |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2858 | return PJ_SUCCESS; |
| 2859 | } |
| 2860 | |
Nanang Izzuddin | ec91900 | 2010-11-25 09:27:06 +0000 | [diff] [blame] | 2861 | /* Remote keeps answering with multiple codecs, let's just give up |
| 2862 | * locking codec to avoid infinite retry loop. |
| 2863 | */ |
| 2864 | if (++call->lock_codec.retry_cnt > LOCK_CODEC_MAX_RETRY) |
| 2865 | return PJ_SUCCESS; |
| 2866 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2867 | PJ_LOG(4, (THIS_FILE, "Got answer with multiple codecs, scheduling " |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2868 | "updating media session to use only one codec..")); |
| 2869 | |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 2870 | call->lock_codec.sdp_ver = local_sdp->origin.version; |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2871 | |
Benny Prijono | 1e60155 | 2010-10-20 05:31:08 +0000 | [diff] [blame] | 2872 | /* Can't send UPDATE or re-INVITE now, so just schedule it immediately. |
| 2873 | * See: https://trac.pjsip.org/repos/ticket/1149 |
| 2874 | */ |
| 2875 | pj_timer_entry_init(&call->lock_codec.reinv_timer, PJ_TRUE, |
| 2876 | (void*)(pj_size_t)call->index, |
| 2877 | &reinv_timer_cb); |
| 2878 | pjsip_endpt_schedule_timer(pjsua_var.endpt, |
| 2879 | &call->lock_codec.reinv_timer, &delay); |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2880 | |
| 2881 | return PJ_SUCCESS; |
| 2882 | } |
| 2883 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2884 | /* |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 2885 | * This callback receives notification from invite session when the |
| 2886 | * session state has changed. |
| 2887 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2888 | static void pjsua_call_on_state_changed(pjsip_inv_session *inv, |
| 2889 | pjsip_event *e) |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 2890 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2891 | pjsua_call *call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2892 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2893 | pj_log_push_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2894 | PJSUA_LOCK(); |
| 2895 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2896 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2897 | |
| 2898 | if (!call) { |
| 2899 | PJSUA_UNLOCK(); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2900 | pj_log_pop_indent(); |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 2901 | return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 2904 | |
| 2905 | /* Get call times */ |
| 2906 | switch (inv->state) { |
| 2907 | case PJSIP_INV_STATE_EARLY: |
| 2908 | case PJSIP_INV_STATE_CONNECTING: |
| 2909 | if (call->res_time.sec == 0) |
| 2910 | pj_gettimeofday(&call->res_time); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 2911 | call->last_code = (pjsip_status_code) |
| 2912 | e->body.tsx_state.tsx->status_code; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2913 | pj_strncpy(&call->last_text, |
| 2914 | &e->body.tsx_state.tsx->status_text, |
| 2915 | sizeof(call->last_text_buf_)); |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 2916 | break; |
| 2917 | case PJSIP_INV_STATE_CONFIRMED: |
| 2918 | pj_gettimeofday(&call->conn_time); |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2919 | |
Nanang Izzuddin | ec91900 | 2010-11-25 09:27:06 +0000 | [diff] [blame] | 2920 | /* See if lock codec was pended as media update was done in the |
| 2921 | * EARLY state and remote does not support UPDATE. |
| 2922 | */ |
| 2923 | if (call->lock_codec.pending) { |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2924 | pj_status_t status; |
| 2925 | status = lock_codec(call); |
| 2926 | if (status != PJ_SUCCESS) { |
| 2927 | pjsua_perror(THIS_FILE, "Unable to lock codec", status); |
Nanang Izzuddin | ec91900 | 2010-11-25 09:27:06 +0000 | [diff] [blame] | 2928 | } |
| 2929 | call->lock_codec.pending = PJ_FALSE; |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2930 | } |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 2931 | break; |
| 2932 | case PJSIP_INV_STATE_DISCONNECTED: |
| 2933 | pj_gettimeofday(&call->dis_time); |
Benny Prijono | 2e507c2 | 2006-06-23 15:04:11 +0000 | [diff] [blame] | 2934 | if (call->res_time.sec == 0) |
| 2935 | pj_gettimeofday(&call->res_time); |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 2936 | if (e->type == PJSIP_EVENT_TSX_STATE && |
| 2937 | e->body.tsx_state.tsx->status_code > call->last_code) |
| 2938 | { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 2939 | call->last_code = (pjsip_status_code) |
| 2940 | e->body.tsx_state.tsx->status_code; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2941 | pj_strncpy(&call->last_text, |
| 2942 | &e->body.tsx_state.tsx->status_text, |
| 2943 | sizeof(call->last_text_buf_)); |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 2944 | } else { |
| 2945 | call->last_code = PJSIP_SC_REQUEST_TERMINATED; |
| 2946 | pj_strncpy(&call->last_text, |
| 2947 | pjsip_get_status_text(call->last_code), |
| 2948 | sizeof(call->last_text_buf_)); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 2949 | } |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2950 | |
| 2951 | /* Stop lock codec timer, if it is active */ |
| 2952 | if (call->lock_codec.reinv_timer.id) { |
| 2953 | pjsip_endpt_cancel_timer(pjsua_var.endpt, |
| 2954 | &call->lock_codec.reinv_timer); |
| 2955 | call->lock_codec.reinv_timer.id = PJ_FALSE; |
| 2956 | } |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 2957 | break; |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 2958 | default: |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 2959 | call->last_code = (pjsip_status_code) |
| 2960 | e->body.tsx_state.tsx->status_code; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2961 | pj_strncpy(&call->last_text, |
| 2962 | &e->body.tsx_state.tsx->status_text, |
| 2963 | sizeof(call->last_text_buf_)); |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 2964 | break; |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2967 | /* If this is an outgoing INVITE that was created because of |
| 2968 | * REFER/transfer, send NOTIFY to transferer. |
| 2969 | */ |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 2970 | if (call->xfer_sub && e->type==PJSIP_EVENT_TSX_STATE) { |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2971 | int st_code = -1; |
| 2972 | pjsip_evsub_state ev_state = PJSIP_EVSUB_STATE_ACTIVE; |
| 2973 | |
| 2974 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2975 | switch (call->inv->state) { |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2976 | case PJSIP_INV_STATE_NULL: |
| 2977 | case PJSIP_INV_STATE_CALLING: |
| 2978 | /* Do nothing */ |
| 2979 | break; |
| 2980 | |
| 2981 | case PJSIP_INV_STATE_EARLY: |
| 2982 | case PJSIP_INV_STATE_CONNECTING: |
| 2983 | st_code = e->body.tsx_state.tsx->status_code; |
Benny Prijono | 19eeb6e | 2009-06-04 22:16:47 +0000 | [diff] [blame] | 2984 | if (call->inv->state == PJSIP_INV_STATE_CONNECTING) |
| 2985 | ev_state = PJSIP_EVSUB_STATE_TERMINATED; |
| 2986 | else |
| 2987 | ev_state = PJSIP_EVSUB_STATE_ACTIVE; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2988 | break; |
| 2989 | |
Benny Prijono | 140beae | 2009-10-11 05:06:43 +0000 | [diff] [blame] | 2990 | case PJSIP_INV_STATE_CONFIRMED: |
Benny Prijono | 19eeb6e | 2009-06-04 22:16:47 +0000 | [diff] [blame] | 2991 | #if 0 |
| 2992 | /* We don't need this, as we've terminated the subscription in |
| 2993 | * CONNECTING state. |
| 2994 | */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2995 | /* When state is confirmed, send the final 200/OK and terminate |
| 2996 | * subscription. |
| 2997 | */ |
| 2998 | st_code = e->body.tsx_state.tsx->status_code; |
| 2999 | ev_state = PJSIP_EVSUB_STATE_TERMINATED; |
Benny Prijono | 19eeb6e | 2009-06-04 22:16:47 +0000 | [diff] [blame] | 3000 | #endif |
Benny Prijono | 140beae | 2009-10-11 05:06:43 +0000 | [diff] [blame] | 3001 | break; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3002 | |
| 3003 | case PJSIP_INV_STATE_DISCONNECTED: |
| 3004 | st_code = e->body.tsx_state.tsx->status_code; |
| 3005 | ev_state = PJSIP_EVSUB_STATE_TERMINATED; |
| 3006 | break; |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 3007 | |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 3008 | case PJSIP_INV_STATE_INCOMING: |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 3009 | /* Nothing to do. Just to keep gcc from complaining about |
| 3010 | * unused enums. |
| 3011 | */ |
| 3012 | break; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | if (st_code != -1) { |
| 3016 | pjsip_tx_data *tdata; |
| 3017 | pj_status_t status; |
| 3018 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3019 | status = pjsip_xfer_notify( call->xfer_sub, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3020 | ev_state, st_code, |
| 3021 | NULL, &tdata); |
| 3022 | if (status != PJ_SUCCESS) { |
| 3023 | pjsua_perror(THIS_FILE, "Unable to create NOTIFY", status); |
| 3024 | } else { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3025 | status = pjsip_xfer_send_request(call->xfer_sub, tdata); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3026 | if (status != PJ_SUCCESS) { |
| 3027 | pjsua_perror(THIS_FILE, "Unable to send NOTIFY", status); |
| 3028 | } |
| 3029 | } |
| 3030 | } |
| 3031 | } |
| 3032 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 3033 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3034 | if (pjsua_var.ua_cfg.cb.on_call_state) |
| 3035 | (*pjsua_var.ua_cfg.cb.on_call_state)(call->index, e); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3036 | |
| 3037 | /* call->inv may be NULL now */ |
| 3038 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 3039 | /* Destroy media session when invite session is disconnected. */ |
| 3040 | if (inv->state == PJSIP_INV_STATE_DISCONNECTED) { |
Benny Prijono | 632ce71 | 2006-02-09 14:01:40 +0000 | [diff] [blame] | 3041 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3042 | pj_assert(call != NULL); |
Benny Prijono | 632ce71 | 2006-02-09 14:01:40 +0000 | [diff] [blame] | 3043 | |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 3044 | if (call) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 3045 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 3046 | |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 3047 | /* Free call */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3048 | call->inv = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3049 | --pjsua_var.call_cnt; |
Benny Prijono | 4be63b5 | 2006-11-25 14:50:25 +0000 | [diff] [blame] | 3050 | |
| 3051 | /* Reset call */ |
| 3052 | reset_call(call->index); |
| 3053 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 3054 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3055 | |
| 3056 | PJSUA_UNLOCK(); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3057 | pj_log_pop_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
| 3060 | /* |
| 3061 | * This callback is called by invite session framework when UAC session |
| 3062 | * has forked. |
| 3063 | */ |
| 3064 | static void pjsua_call_on_forked( pjsip_inv_session *inv, |
| 3065 | pjsip_event *e) |
| 3066 | { |
| 3067 | PJ_UNUSED_ARG(inv); |
| 3068 | PJ_UNUSED_ARG(e); |
| 3069 | |
| 3070 | PJ_TODO(HANDLE_FORKED_DIALOG); |
| 3071 | } |
| 3072 | |
| 3073 | |
| 3074 | /* |
Benny Prijono | 3c5e28b | 2008-09-24 10:10:15 +0000 | [diff] [blame] | 3075 | * Callback from UA layer when forked dialog response is received. |
| 3076 | */ |
| 3077 | pjsip_dialog* on_dlg_forked(pjsip_dialog *dlg, pjsip_rx_data *res) |
| 3078 | { |
| 3079 | if (dlg->uac_has_2xx && |
| 3080 | res->msg_info.cseq->method.id == PJSIP_INVITE_METHOD && |
| 3081 | pjsip_rdata_get_tsx(res) == NULL && |
| 3082 | res->msg_info.msg->line.status.code/100 == 2) |
| 3083 | { |
| 3084 | pjsip_dialog *forked_dlg; |
| 3085 | pjsip_tx_data *bye; |
| 3086 | pj_status_t status; |
| 3087 | |
| 3088 | /* Create forked dialog */ |
| 3089 | status = pjsip_dlg_fork(dlg, res, &forked_dlg); |
| 3090 | if (status != PJ_SUCCESS) |
| 3091 | return NULL; |
| 3092 | |
| 3093 | pjsip_dlg_inc_lock(forked_dlg); |
| 3094 | |
| 3095 | /* Disconnect the call */ |
| 3096 | status = pjsip_dlg_create_request(forked_dlg, &pjsip_bye_method, |
| 3097 | -1, &bye); |
| 3098 | if (status == PJ_SUCCESS) { |
| 3099 | status = pjsip_dlg_send_request(forked_dlg, bye, -1, NULL); |
| 3100 | } |
| 3101 | |
| 3102 | pjsip_dlg_dec_lock(forked_dlg); |
| 3103 | |
| 3104 | if (status != PJ_SUCCESS) { |
| 3105 | return NULL; |
| 3106 | } |
| 3107 | |
| 3108 | return forked_dlg; |
| 3109 | |
| 3110 | } else { |
| 3111 | return dlg; |
| 3112 | } |
| 3113 | } |
| 3114 | |
| 3115 | /* |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 3116 | * Disconnect call upon error. |
| 3117 | */ |
| 3118 | static void call_disconnect( pjsip_inv_session *inv, |
| 3119 | int code ) |
| 3120 | { |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 3121 | pjsua_call *call; |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 3122 | pjsip_tx_data *tdata; |
| 3123 | pj_status_t status; |
| 3124 | |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 3125 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
| 3126 | |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 3127 | status = pjsip_inv_end_session(inv, code, NULL, &tdata); |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 3128 | if (status != PJ_SUCCESS) |
| 3129 | return; |
| 3130 | |
| 3131 | /* Add SDP in 488 status */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 3132 | #if DISABLED_FOR_TICKET_1185 |
| 3133 | if (call && call->tp && tdata->msg->type==PJSIP_RESPONSE_MSG && |
Benny Prijono | 9963998 | 2008-03-07 14:39:52 +0000 | [diff] [blame] | 3134 | code==PJSIP_SC_NOT_ACCEPTABLE_HERE) |
| 3135 | { |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 3136 | pjmedia_sdp_session *local_sdp; |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 3137 | pjmedia_transport_info ti; |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 3138 | |
Benny Prijono | 734fc2d | 2008-03-17 16:05:35 +0000 | [diff] [blame] | 3139 | pjmedia_transport_info_init(&ti); |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 3140 | pjmedia_transport_get_info(call->med_tp, &ti); |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 3141 | status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 3142 | 1, &ti.sock_info, &local_sdp); |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 3143 | if (status == PJ_SUCCESS) { |
| 3144 | pjsip_create_sdp_body(tdata->pool, local_sdp, |
| 3145 | &tdata->msg->body); |
| 3146 | } |
| 3147 | } |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 3148 | #endif |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 3149 | |
| 3150 | pjsip_inv_send_msg(inv, tdata); |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3154 | * Callback to be called when SDP offer/answer negotiation has just completed |
| 3155 | * in the session. This function will start/update media if negotiation |
| 3156 | * has succeeded. |
| 3157 | */ |
| 3158 | static void pjsua_call_on_media_update(pjsip_inv_session *inv, |
| 3159 | pj_status_t status) |
| 3160 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3161 | pjsua_call *call; |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 3162 | const pjmedia_sdp_session *local_sdp; |
Benny Prijono | dbce2cf | 2007-03-28 16:24:00 +0000 | [diff] [blame] | 3163 | const pjmedia_sdp_session *remote_sdp; |
Nanang Izzuddin | ec91900 | 2010-11-25 09:27:06 +0000 | [diff] [blame] | 3164 | //const pj_str_t st_update = {"UPDATE", 6}; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3165 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3166 | pj_log_push_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3167 | PJSUA_LOCK(); |
| 3168 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3169 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3170 | |
| 3171 | if (status != PJ_SUCCESS) { |
| 3172 | |
| 3173 | pjsua_perror(THIS_FILE, "SDP negotiation has failed", status); |
| 3174 | |
Benny Prijono | 2331d20 | 2008-06-26 15:46:52 +0000 | [diff] [blame] | 3175 | /* Do not deinitialize media since this may be a re-INVITE or |
| 3176 | * UPDATE (which in this case the media should not get affected |
| 3177 | * by the failed re-INVITE/UPDATE). The media will be shutdown |
| 3178 | * when call is disconnected anyway. |
| 3179 | */ |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 3180 | /* Stop/destroy media, if any */ |
Benny Prijono | 2331d20 | 2008-06-26 15:46:52 +0000 | [diff] [blame] | 3181 | /*pjsua_media_channel_deinit(call->index);*/ |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 3182 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3183 | /* Disconnect call if we're not in the middle of initializing an |
| 3184 | * UAS dialog and if this is not a re-INVITE |
| 3185 | */ |
| 3186 | if (inv->state != PJSIP_INV_STATE_NULL && |
| 3187 | inv->state != PJSIP_INV_STATE_CONFIRMED) |
| 3188 | { |
Benny Prijono | 2dbed82 | 2008-02-21 10:08:27 +0000 | [diff] [blame] | 3189 | call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3190 | } |
| 3191 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3192 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3195 | |
| 3196 | /* Get local and remote SDP */ |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 3197 | status = pjmedia_sdp_neg_get_active_local(call->inv->neg, &local_sdp); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3198 | if (status != PJ_SUCCESS) { |
| 3199 | pjsua_perror(THIS_FILE, |
| 3200 | "Unable to retrieve currently active local SDP", |
| 3201 | status); |
| 3202 | //call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3203 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3204 | } |
| 3205 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3206 | status = pjmedia_sdp_neg_get_active_remote(call->inv->neg, &remote_sdp); |
| 3207 | if (status != PJ_SUCCESS) { |
| 3208 | pjsua_perror(THIS_FILE, |
| 3209 | "Unable to retrieve currently active remote SDP", |
| 3210 | status); |
| 3211 | //call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3212 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3213 | } |
| 3214 | |
Benny Prijono | 91a6a17 | 2007-10-31 08:59:29 +0000 | [diff] [blame] | 3215 | /* Update remote's NAT type */ |
| 3216 | if (pjsua_var.ua_cfg.nat_type_in_sdp) { |
| 3217 | update_remote_nat_type(call, remote_sdp); |
| 3218 | } |
| 3219 | |
| 3220 | /* Update media channel with the new SDP */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 3221 | status = pjsua_media_channel_update(call->index, local_sdp, remote_sdp); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3222 | if (status != PJ_SUCCESS) { |
| 3223 | pjsua_perror(THIS_FILE, "Unable to create media session", |
| 3224 | status); |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 3225 | call_disconnect(inv, PJSIP_SC_NOT_ACCEPTABLE_HERE); |
Benny Prijono | 2331d20 | 2008-06-26 15:46:52 +0000 | [diff] [blame] | 3226 | /* No need to deinitialize; media will be shutdown when call |
| 3227 | * state is disconnected anyway. |
| 3228 | */ |
| 3229 | /*pjsua_media_channel_deinit(call->index);*/ |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3230 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3231 | } |
| 3232 | |
Nanang Izzuddin | ec91900 | 2010-11-25 09:27:06 +0000 | [diff] [blame] | 3233 | /* Ticket #476: make sure only one codec is specified in the answer. */ |
| 3234 | status = lock_codec(call); |
| 3235 | if (status != PJ_SUCCESS) { |
| 3236 | pjsua_perror(THIS_FILE, "Unable to lock codec", status); |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 3237 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3238 | |
| 3239 | /* Call application callback, if any */ |
| 3240 | if (pjsua_var.ua_cfg.cb.on_call_media_state) |
| 3241 | pjsua_var.ua_cfg.cb.on_call_media_state(call->index); |
| 3242 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3243 | on_return: |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3244 | PJSUA_UNLOCK(); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3245 | pj_log_pop_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
| 3248 | |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame] | 3249 | /* Modify SDP for call hold. */ |
| 3250 | static pj_status_t modify_sdp_of_call_hold(pjsua_call *call, |
| 3251 | pj_pool_t *pool, |
| 3252 | pjmedia_sdp_session *sdp) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3253 | { |
Benny Prijono | 316f02a | 2011-04-07 07:53:25 +0000 | [diff] [blame] | 3254 | unsigned mi; |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 3255 | |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3256 | /* Call-hold is done by set the media direction to 'sendonly' |
| 3257 | * (PJMEDIA_DIR_ENCODING), except when current media direction is |
| 3258 | * 'inactive' (PJMEDIA_DIR_NONE). |
| 3259 | * (See RFC 3264 Section 8.4 and RFC 4317 Section 3.1) |
| 3260 | */ |
Benny Prijono | e086013 | 2009-06-05 10:14:20 +0000 | [diff] [blame] | 3261 | /* http://trac.pjsip.org/repos/ticket/880 |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 3262 | if (call->dir != PJMEDIA_DIR_ENCODING) { |
Benny Prijono | e086013 | 2009-06-05 10:14:20 +0000 | [diff] [blame] | 3263 | */ |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame] | 3264 | /* https://trac.pjsip.org/repos/ticket/1142: |
| 3265 | * configuration to use c=0.0.0.0 for call hold. |
| 3266 | */ |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 3267 | |
Benny Prijono | 316f02a | 2011-04-07 07:53:25 +0000 | [diff] [blame] | 3268 | for (mi=0; mi<sdp->media_count; ++mi) { |
| 3269 | pjmedia_sdp_media *m = sdp->media[mi]; |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 3270 | |
Benny Prijono | 316f02a | 2011-04-07 07:53:25 +0000 | [diff] [blame] | 3271 | if (call->call_hold_type == PJSUA_CALL_HOLD_TYPE_RFC2543) { |
| 3272 | pjmedia_sdp_conn *conn; |
| 3273 | pjmedia_sdp_attr *attr; |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame] | 3274 | |
Benny Prijono | 316f02a | 2011-04-07 07:53:25 +0000 | [diff] [blame] | 3275 | /* Get SDP media connection line */ |
| 3276 | conn = m->conn; |
| 3277 | if (!conn) |
| 3278 | conn = sdp->conn; |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame] | 3279 | |
Benny Prijono | 316f02a | 2011-04-07 07:53:25 +0000 | [diff] [blame] | 3280 | /* Modify address */ |
| 3281 | conn->addr = pj_str("0.0.0.0"); |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame] | 3282 | |
Benny Prijono | 316f02a | 2011-04-07 07:53:25 +0000 | [diff] [blame] | 3283 | /* Remove existing directions attributes */ |
| 3284 | pjmedia_sdp_media_remove_all_attr(m, "sendrecv"); |
| 3285 | pjmedia_sdp_media_remove_all_attr(m, "sendonly"); |
| 3286 | pjmedia_sdp_media_remove_all_attr(m, "recvonly"); |
| 3287 | pjmedia_sdp_media_remove_all_attr(m, "inactive"); |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame] | 3288 | |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3289 | /* Add inactive attribute */ |
| 3290 | attr = pjmedia_sdp_attr_create(pool, "inactive", NULL); |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 3291 | pjmedia_sdp_media_add_attr(m, attr); |
Benny Prijono | 316f02a | 2011-04-07 07:53:25 +0000 | [diff] [blame] | 3292 | |
| 3293 | |
| 3294 | } else { |
| 3295 | pjmedia_sdp_attr *attr; |
| 3296 | |
| 3297 | /* Remove existing directions attributes */ |
| 3298 | pjmedia_sdp_media_remove_all_attr(m, "sendrecv"); |
| 3299 | pjmedia_sdp_media_remove_all_attr(m, "sendonly"); |
| 3300 | pjmedia_sdp_media_remove_all_attr(m, "recvonly"); |
| 3301 | pjmedia_sdp_media_remove_all_attr(m, "inactive"); |
| 3302 | |
| 3303 | if (call->media[mi].dir & PJMEDIA_DIR_ENCODING) { |
| 3304 | /* Add sendonly attribute */ |
| 3305 | attr = pjmedia_sdp_attr_create(pool, "sendonly", NULL); |
| 3306 | pjmedia_sdp_media_add_attr(m, attr); |
| 3307 | } else { |
| 3308 | /* Add inactive attribute */ |
| 3309 | attr = pjmedia_sdp_attr_create(pool, "inactive", NULL); |
| 3310 | pjmedia_sdp_media_add_attr(m, attr); |
| 3311 | } |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3312 | } |
| 3313 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3314 | |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame] | 3315 | return PJ_SUCCESS; |
| 3316 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3317 | |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame] | 3318 | /* Create SDP for call hold. */ |
| 3319 | static pj_status_t create_sdp_of_call_hold(pjsua_call *call, |
| 3320 | pjmedia_sdp_session **p_sdp) |
| 3321 | { |
| 3322 | pj_status_t status; |
| 3323 | pj_pool_t *pool; |
| 3324 | pjmedia_sdp_session *sdp; |
| 3325 | |
| 3326 | /* Use call's provisional pool */ |
| 3327 | pool = call->inv->pool_prov; |
| 3328 | |
| 3329 | /* Create new offer */ |
| 3330 | status = pjsua_media_channel_create_sdp(call->index, pool, NULL, &sdp, |
| 3331 | NULL); |
| 3332 | if (status != PJ_SUCCESS) { |
| 3333 | pjsua_perror(THIS_FILE, "Unable to create local SDP", status); |
| 3334 | return status; |
| 3335 | } |
| 3336 | |
| 3337 | status = modify_sdp_of_call_hold(call, pool, sdp); |
| 3338 | if (status != PJ_SUCCESS) |
| 3339 | return status; |
| 3340 | |
| 3341 | *p_sdp = sdp; |
| 3342 | |
| 3343 | return PJ_SUCCESS; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3344 | } |
| 3345 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3346 | /* |
| 3347 | * Called when session received new offer. |
| 3348 | */ |
| 3349 | static void pjsua_call_on_rx_offer(pjsip_inv_session *inv, |
| 3350 | const pjmedia_sdp_session *offer) |
| 3351 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3352 | pjsua_call *call; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3353 | pjmedia_sdp_session *answer; |
Nanang Izzuddin | e03faad | 2011-04-07 14:51:28 +0000 | [diff] [blame] | 3354 | unsigned i; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3355 | pj_status_t status; |
| 3356 | |
| 3357 | PJSUA_LOCK(); |
| 3358 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3359 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3360 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3361 | /* Supply candidate answer */ |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3362 | PJ_LOG(4,(THIS_FILE, "Call %d: received updated media offer", |
| 3363 | call->index)); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3364 | pj_log_push_indent(); |
Benny Prijono | 667952e | 2007-04-02 19:27:54 +0000 | [diff] [blame] | 3365 | |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 3366 | status = pjsua_media_channel_create_sdp(call->index, |
| 3367 | call->inv->pool_prov, |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3368 | offer, &answer, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3369 | if (status != PJ_SUCCESS) { |
| 3370 | pjsua_perror(THIS_FILE, "Unable to create local SDP", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3371 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
Nanang Izzuddin | e03faad | 2011-04-07 14:51:28 +0000 | [diff] [blame] | 3374 | /* Validate media count in the generated answer */ |
| 3375 | pj_assert(answer->media_count == offer->media_count); |
| 3376 | |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3377 | /* Check if offer's conn address is zero */ |
Nanang Izzuddin | e03faad | 2011-04-07 14:51:28 +0000 | [diff] [blame] | 3378 | for (i = 0; i < answer->media_count; ++i) { |
| 3379 | pjmedia_sdp_conn *conn; |
| 3380 | |
| 3381 | conn = offer->media[i]->conn; |
| 3382 | if (!conn) |
| 3383 | conn = offer->conn; |
| 3384 | |
| 3385 | if (pj_strcmp2(&conn->addr, "0.0.0.0")==0 || |
| 3386 | pj_strcmp2(&conn->addr, "0")==0) |
| 3387 | { |
| 3388 | pjmedia_sdp_conn *a_conn = answer->media[i]->conn; |
| 3389 | |
| 3390 | /* Modify answer address */ |
| 3391 | if (a_conn) { |
| 3392 | a_conn->addr = pj_str("0.0.0.0"); |
| 3393 | } else if (answer->conn == NULL || |
| 3394 | pj_strcmp2(&answer->conn->addr, "0.0.0.0") != 0) |
| 3395 | { |
| 3396 | a_conn = PJ_POOL_ZALLOC_T(call->inv->pool_prov, |
| 3397 | pjmedia_sdp_conn); |
| 3398 | a_conn->net_type = pj_str("IN"); |
| 3399 | a_conn->addr_type = pj_str("IP4"); |
| 3400 | a_conn->addr = pj_str("0.0.0.0"); |
| 3401 | answer->media[i]->conn = a_conn; |
| 3402 | } |
| 3403 | } |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3404 | } |
| 3405 | |
| 3406 | /* Check if call is on-hold */ |
| 3407 | if (call->local_hold) { |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame] | 3408 | modify_sdp_of_call_hold(call, call->inv->pool_prov, answer); |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3409 | } |
| 3410 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3411 | status = pjsip_inv_set_sdp_answer(call->inv, answer); |
| 3412 | if (status != PJ_SUCCESS) { |
| 3413 | pjsua_perror(THIS_FILE, "Unable to set answer", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3414 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3415 | } |
| 3416 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3417 | on_return: |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3418 | PJSUA_UNLOCK(); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3419 | pj_log_pop_indent(); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 3420 | } |
| 3421 | |
| 3422 | |
| 3423 | /* |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3424 | * Called to generate new offer. |
| 3425 | */ |
| 3426 | static void pjsua_call_on_create_offer(pjsip_inv_session *inv, |
| 3427 | pjmedia_sdp_session **offer) |
| 3428 | { |
| 3429 | pjsua_call *call; |
| 3430 | pj_status_t status; |
| 3431 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3432 | pj_log_push_indent(); |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3433 | PJSUA_LOCK(); |
| 3434 | |
| 3435 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
| 3436 | |
| 3437 | /* See if we've put call on hold. */ |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3438 | if (call->local_hold) { |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3439 | PJ_LOG(4,(THIS_FILE, |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3440 | "Call %d: call is on-hold locally, creating call-hold SDP ", |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3441 | call->index)); |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3442 | status = create_sdp_of_call_hold( call, offer ); |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3443 | } else { |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3444 | PJ_LOG(4,(THIS_FILE, "Call %d: asked to send a new offer", |
| 3445 | call->index)); |
| 3446 | |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 3447 | status = pjsua_media_channel_create_sdp(call->index, |
| 3448 | call->inv->pool_prov, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 3449 | NULL, offer, NULL); |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | if (status != PJ_SUCCESS) { |
| 3453 | pjsua_perror(THIS_FILE, "Unable to create local SDP", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3454 | goto on_return; |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3455 | } |
| 3456 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3457 | on_return: |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3458 | PJSUA_UNLOCK(); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3459 | pj_log_pop_indent(); |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3460 | } |
| 3461 | |
| 3462 | |
| 3463 | /* |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3464 | * Callback called by event framework when the xfer subscription state |
| 3465 | * has changed. |
| 3466 | */ |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3467 | static void xfer_client_on_evsub_state( pjsip_evsub *sub, pjsip_event *event) |
| 3468 | { |
| 3469 | |
| 3470 | PJ_UNUSED_ARG(event); |
| 3471 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3472 | pj_log_push_indent(); |
| 3473 | |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3474 | /* |
| 3475 | * When subscription is accepted (got 200/OK to REFER), check if |
| 3476 | * subscription suppressed. |
| 3477 | */ |
| 3478 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACCEPTED) { |
| 3479 | |
| 3480 | pjsip_rx_data *rdata; |
| 3481 | pjsip_generic_string_hdr *refer_sub; |
| 3482 | const pj_str_t REFER_SUB = { "Refer-Sub", 9 }; |
| 3483 | pjsua_call *call; |
| 3484 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3485 | call = (pjsua_call*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id); |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3486 | |
| 3487 | /* Must be receipt of response message */ |
| 3488 | pj_assert(event->type == PJSIP_EVENT_TSX_STATE && |
| 3489 | event->body.tsx_state.type == PJSIP_EVENT_RX_MSG); |
| 3490 | rdata = event->body.tsx_state.src.rdata; |
| 3491 | |
| 3492 | /* Find Refer-Sub header */ |
| 3493 | refer_sub = (pjsip_generic_string_hdr*) |
| 3494 | pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, |
| 3495 | &REFER_SUB, NULL); |
| 3496 | |
| 3497 | /* Check if subscription is suppressed */ |
| 3498 | if (refer_sub && pj_stricmp2(&refer_sub->hvalue, "false")==0) { |
| 3499 | /* Since no subscription is desired, assume that call has been |
| 3500 | * transfered successfully. |
| 3501 | */ |
| 3502 | if (call && pjsua_var.ua_cfg.cb.on_call_transfer_status) { |
| 3503 | const pj_str_t ACCEPTED = { "Accepted", 8 }; |
| 3504 | pj_bool_t cont = PJ_FALSE; |
| 3505 | (*pjsua_var.ua_cfg.cb.on_call_transfer_status)(call->index, |
| 3506 | 200, |
| 3507 | &ACCEPTED, |
| 3508 | PJ_TRUE, |
| 3509 | &cont); |
| 3510 | } |
| 3511 | |
| 3512 | /* Yes, subscription is suppressed. |
| 3513 | * Terminate our subscription now. |
| 3514 | */ |
| 3515 | PJ_LOG(4,(THIS_FILE, "Xfer subscription suppressed, terminating " |
| 3516 | "event subcription...")); |
| 3517 | pjsip_evsub_terminate(sub, PJ_TRUE); |
| 3518 | |
| 3519 | } else { |
| 3520 | /* Notify application about call transfer progress. |
| 3521 | * Initially notify with 100/Accepted status. |
| 3522 | */ |
| 3523 | if (call && pjsua_var.ua_cfg.cb.on_call_transfer_status) { |
| 3524 | const pj_str_t ACCEPTED = { "Accepted", 8 }; |
| 3525 | pj_bool_t cont = PJ_FALSE; |
| 3526 | (*pjsua_var.ua_cfg.cb.on_call_transfer_status)(call->index, |
| 3527 | 100, |
| 3528 | &ACCEPTED, |
| 3529 | PJ_FALSE, |
| 3530 | &cont); |
| 3531 | } |
| 3532 | } |
| 3533 | } |
| 3534 | /* |
| 3535 | * On incoming NOTIFY, notify application about call transfer progress. |
| 3536 | */ |
| 3537 | else if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACTIVE || |
| 3538 | pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) |
| 3539 | { |
| 3540 | pjsua_call *call; |
| 3541 | pjsip_msg *msg; |
| 3542 | pjsip_msg_body *body; |
| 3543 | pjsip_status_line status_line; |
| 3544 | pj_bool_t is_last; |
| 3545 | pj_bool_t cont; |
| 3546 | pj_status_t status; |
| 3547 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3548 | call = (pjsua_call*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id); |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3549 | |
| 3550 | /* When subscription is terminated, clear the xfer_sub member of |
| 3551 | * the inv_data. |
| 3552 | */ |
| 3553 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
| 3554 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
| 3555 | PJ_LOG(4,(THIS_FILE, "Xfer client subscription terminated")); |
| 3556 | |
| 3557 | } |
| 3558 | |
| 3559 | if (!call || !event || !pjsua_var.ua_cfg.cb.on_call_transfer_status) { |
| 3560 | /* Application is not interested with call progress status */ |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3561 | goto on_return; |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
| 3564 | /* This better be a NOTIFY request */ |
| 3565 | if (event->type == PJSIP_EVENT_TSX_STATE && |
| 3566 | event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 3567 | { |
| 3568 | pjsip_rx_data *rdata; |
| 3569 | |
| 3570 | rdata = event->body.tsx_state.src.rdata; |
| 3571 | |
| 3572 | /* Check if there's body */ |
| 3573 | msg = rdata->msg_info.msg; |
| 3574 | body = msg->body; |
| 3575 | if (!body) { |
Benny Prijono | c6ff4b8 | 2009-05-12 15:55:09 +0000 | [diff] [blame] | 3576 | PJ_LOG(2,(THIS_FILE, |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3577 | "Warning: received NOTIFY without message body")); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3578 | goto on_return; |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3579 | } |
| 3580 | |
| 3581 | /* Check for appropriate content */ |
| 3582 | if (pj_stricmp2(&body->content_type.type, "message") != 0 || |
| 3583 | pj_stricmp2(&body->content_type.subtype, "sipfrag") != 0) |
| 3584 | { |
Benny Prijono | c6ff4b8 | 2009-05-12 15:55:09 +0000 | [diff] [blame] | 3585 | PJ_LOG(2,(THIS_FILE, |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3586 | "Warning: received NOTIFY with non message/sipfrag " |
| 3587 | "content")); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3588 | goto on_return; |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3589 | } |
| 3590 | |
| 3591 | /* Try to parse the content */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3592 | status = pjsip_parse_status_line((char*)body->data, body->len, |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3593 | &status_line); |
| 3594 | if (status != PJ_SUCCESS) { |
Benny Prijono | c6ff4b8 | 2009-05-12 15:55:09 +0000 | [diff] [blame] | 3595 | PJ_LOG(2,(THIS_FILE, |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3596 | "Warning: received NOTIFY with invalid " |
| 3597 | "message/sipfrag content")); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3598 | goto on_return; |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | } else { |
| 3602 | status_line.code = 500; |
| 3603 | status_line.reason = *pjsip_get_status_text(500); |
| 3604 | } |
| 3605 | |
| 3606 | /* Notify application */ |
| 3607 | is_last = (pjsip_evsub_get_state(sub)==PJSIP_EVSUB_STATE_TERMINATED); |
| 3608 | cont = !is_last; |
| 3609 | (*pjsua_var.ua_cfg.cb.on_call_transfer_status)(call->index, |
| 3610 | status_line.code, |
| 3611 | &status_line.reason, |
| 3612 | is_last, &cont); |
| 3613 | |
| 3614 | if (!cont) { |
| 3615 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
| 3616 | } |
Benny Prijono | 19eeb6e | 2009-06-04 22:16:47 +0000 | [diff] [blame] | 3617 | |
| 3618 | /* If the call transfer has completed but the subscription is |
| 3619 | * not terminated, terminate it now. |
| 3620 | */ |
| 3621 | if (status_line.code/100 == 2 && !is_last) { |
| 3622 | pjsip_tx_data *tdata; |
| 3623 | |
| 3624 | status = pjsip_evsub_initiate(sub, &pjsip_subscribe_method, |
| 3625 | 0, &tdata); |
| 3626 | if (status == PJ_SUCCESS) |
| 3627 | status = pjsip_evsub_send_request(sub, tdata); |
| 3628 | } |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3629 | } |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3630 | |
| 3631 | on_return: |
| 3632 | pj_log_pop_indent(); |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3633 | } |
| 3634 | |
| 3635 | |
| 3636 | /* |
| 3637 | * Callback called by event framework when the xfer subscription state |
| 3638 | * has changed. |
| 3639 | */ |
| 3640 | static void xfer_server_on_evsub_state( pjsip_evsub *sub, pjsip_event *event) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3641 | { |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3642 | PJ_UNUSED_ARG(event); |
| 3643 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3644 | pj_log_push_indent(); |
| 3645 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3646 | /* |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 3647 | * When subscription is terminated, clear the xfer_sub member of |
| 3648 | * the inv_data. |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3649 | */ |
| 3650 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3651 | pjsua_call *call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3652 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3653 | call = (pjsua_call*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3654 | if (!call) |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3655 | goto on_return; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3656 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3657 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3658 | call->xfer_sub = NULL; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3659 | |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3660 | PJ_LOG(4,(THIS_FILE, "Xfer server subscription terminated")); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3661 | } |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3662 | |
| 3663 | on_return: |
| 3664 | pj_log_pop_indent(); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
| 3667 | |
| 3668 | /* |
| 3669 | * Follow transfer (REFER) request. |
| 3670 | */ |
| 3671 | static void on_call_transfered( pjsip_inv_session *inv, |
| 3672 | pjsip_rx_data *rdata ) |
| 3673 | { |
| 3674 | pj_status_t status; |
| 3675 | pjsip_tx_data *tdata; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3676 | pjsua_call *existing_call; |
| 3677 | int new_call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3678 | const pj_str_t str_refer_to = { "Refer-To", 8}; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3679 | const pj_str_t str_refer_sub = { "Refer-Sub", 9 }; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 3680 | const pj_str_t str_ref_by = { "Referred-By", 11 }; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3681 | pjsip_generic_string_hdr *refer_to; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3682 | pjsip_generic_string_hdr *refer_sub; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 3683 | pjsip_hdr *ref_by_hdr; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3684 | pj_bool_t no_refer_sub = PJ_FALSE; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3685 | char *uri; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 3686 | pjsua_msg_data msg_data; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 3687 | pj_str_t tmp; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 3688 | pjsip_status_code code; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3689 | pjsip_evsub *sub; |
| 3690 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3691 | pj_log_push_indent(); |
| 3692 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3693 | existing_call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3694 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3695 | /* Find the Refer-To header */ |
| 3696 | refer_to = (pjsip_generic_string_hdr*) |
| 3697 | pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_to, NULL); |
| 3698 | |
| 3699 | if (refer_to == NULL) { |
| 3700 | /* Invalid Request. |
| 3701 | * No Refer-To header! |
| 3702 | */ |
| 3703 | PJ_LOG(4,(THIS_FILE, "Received REFER without Refer-To header!")); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 3704 | pjsip_dlg_respond( inv->dlg, rdata, 400, NULL, NULL, NULL); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3705 | goto on_return; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3708 | /* Find optional Refer-Sub header */ |
| 3709 | refer_sub = (pjsip_generic_string_hdr*) |
| 3710 | pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_sub, NULL); |
| 3711 | |
| 3712 | if (refer_sub) { |
| 3713 | if (!pj_strnicmp2(&refer_sub->hvalue, "true", 4)==0) |
| 3714 | no_refer_sub = PJ_TRUE; |
| 3715 | } |
| 3716 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 3717 | /* Find optional Referred-By header (to be copied onto outgoing INVITE |
| 3718 | * request. |
| 3719 | */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3720 | ref_by_hdr = (pjsip_hdr*) |
| 3721 | pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_ref_by, |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 3722 | NULL); |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3723 | |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 3724 | /* Notify callback */ |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame] | 3725 | code = PJSIP_SC_ACCEPTED; |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3726 | if (pjsua_var.ua_cfg.cb.on_call_transfer_request) |
| 3727 | (*pjsua_var.ua_cfg.cb.on_call_transfer_request)(existing_call->index, |
| 3728 | &refer_to->hvalue, |
| 3729 | &code); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 3730 | |
| 3731 | if (code < 200) |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame] | 3732 | code = PJSIP_SC_ACCEPTED; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 3733 | if (code >= 300) { |
| 3734 | /* Application rejects call transfer request */ |
| 3735 | pjsip_dlg_respond( inv->dlg, rdata, code, NULL, NULL, NULL); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3736 | goto on_return; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3739 | PJ_LOG(3,(THIS_FILE, "Call to %.*s is being transfered to %.*s", |
| 3740 | (int)inv->dlg->remote.info_str.slen, |
| 3741 | inv->dlg->remote.info_str.ptr, |
| 3742 | (int)refer_to->hvalue.slen, |
| 3743 | refer_to->hvalue.ptr)); |
| 3744 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3745 | if (no_refer_sub) { |
| 3746 | /* |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame] | 3747 | * Always answer with 2xx. |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3748 | */ |
| 3749 | pjsip_tx_data *tdata; |
| 3750 | const pj_str_t str_false = { "false", 5}; |
| 3751 | pjsip_hdr *hdr; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3752 | |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame] | 3753 | status = pjsip_dlg_create_response(inv->dlg, rdata, code, NULL, |
| 3754 | &tdata); |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3755 | if (status != PJ_SUCCESS) { |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame] | 3756 | pjsua_perror(THIS_FILE, "Unable to create 2xx response to REFER", |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3757 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3758 | goto on_return; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3759 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3760 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3761 | /* Add Refer-Sub header */ |
| 3762 | hdr = (pjsip_hdr*) |
| 3763 | pjsip_generic_string_hdr_create(tdata->pool, &str_refer_sub, |
| 3764 | &str_false); |
| 3765 | pjsip_msg_add_hdr(tdata->msg, hdr); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3766 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3767 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3768 | /* Send answer */ |
| 3769 | status = pjsip_dlg_send_response(inv->dlg, pjsip_rdata_get_tsx(rdata), |
| 3770 | tdata); |
| 3771 | if (status != PJ_SUCCESS) { |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame] | 3772 | pjsua_perror(THIS_FILE, "Unable to create 2xx response to REFER", |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3773 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3774 | goto on_return; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | /* Don't have subscription */ |
| 3778 | sub = NULL; |
| 3779 | |
| 3780 | } else { |
| 3781 | struct pjsip_evsub_user xfer_cb; |
| 3782 | pjsip_hdr hdr_list; |
| 3783 | |
| 3784 | /* Init callback */ |
| 3785 | pj_bzero(&xfer_cb, sizeof(xfer_cb)); |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3786 | xfer_cb.on_evsub_state = &xfer_server_on_evsub_state; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3787 | |
| 3788 | /* Init additional header list to be sent with REFER response */ |
| 3789 | pj_list_init(&hdr_list); |
| 3790 | |
| 3791 | /* Create transferee event subscription */ |
| 3792 | status = pjsip_xfer_create_uas( inv->dlg, &xfer_cb, rdata, &sub); |
| 3793 | if (status != PJ_SUCCESS) { |
| 3794 | pjsua_perror(THIS_FILE, "Unable to create xfer uas", status); |
| 3795 | pjsip_dlg_respond( inv->dlg, rdata, 500, NULL, NULL, NULL); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3796 | goto on_return; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3797 | } |
| 3798 | |
| 3799 | /* If there's Refer-Sub header and the value is "true", send back |
| 3800 | * Refer-Sub in the response with value "true" too. |
| 3801 | */ |
| 3802 | if (refer_sub) { |
| 3803 | const pj_str_t str_true = { "true", 4 }; |
| 3804 | pjsip_hdr *hdr; |
| 3805 | |
| 3806 | hdr = (pjsip_hdr*) |
| 3807 | pjsip_generic_string_hdr_create(inv->dlg->pool, |
| 3808 | &str_refer_sub, |
| 3809 | &str_true); |
| 3810 | pj_list_push_back(&hdr_list, hdr); |
| 3811 | |
| 3812 | } |
| 3813 | |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame] | 3814 | /* Accept the REFER request, send 2xx. */ |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3815 | pjsip_xfer_accept(sub, rdata, code, &hdr_list); |
| 3816 | |
| 3817 | /* Create initial NOTIFY request */ |
| 3818 | status = pjsip_xfer_notify( sub, PJSIP_EVSUB_STATE_ACTIVE, |
| 3819 | 100, NULL, &tdata); |
| 3820 | if (status != PJ_SUCCESS) { |
| 3821 | pjsua_perror(THIS_FILE, "Unable to create NOTIFY to REFER", |
| 3822 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3823 | goto on_return; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3824 | } |
| 3825 | |
| 3826 | /* Send initial NOTIFY request */ |
| 3827 | status = pjsip_xfer_send_request( sub, tdata); |
| 3828 | if (status != PJ_SUCCESS) { |
| 3829 | pjsua_perror(THIS_FILE, "Unable to send NOTIFY to REFER", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3830 | goto on_return; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3831 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3832 | } |
| 3833 | |
| 3834 | /* We're cheating here. |
| 3835 | * We need to get a null terminated string from a pj_str_t. |
| 3836 | * So grab the pointer from the hvalue and NULL terminate it, knowing |
| 3837 | * that the NULL position will be occupied by a newline. |
| 3838 | */ |
| 3839 | uri = refer_to->hvalue.ptr; |
| 3840 | uri[refer_to->hvalue.slen] = '\0'; |
| 3841 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 3842 | /* Init msg_data */ |
| 3843 | pjsua_msg_data_init(&msg_data); |
| 3844 | |
| 3845 | /* If Referred-By header is present in the REFER request, copy this |
| 3846 | * to the outgoing INVITE request. |
| 3847 | */ |
| 3848 | if (ref_by_hdr != NULL) { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3849 | pjsip_hdr *dup = (pjsip_hdr*) |
| 3850 | pjsip_hdr_clone(rdata->tp_info.pool, ref_by_hdr); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 3851 | pj_list_push_back(&msg_data.hdr_list, dup); |
| 3852 | } |
| 3853 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3854 | /* Now make the outgoing call. */ |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 3855 | tmp = pj_str(uri); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3856 | status = pjsua_call_make_call(existing_call->acc_id, &tmp, 0, |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 3857 | existing_call->user_data, &msg_data, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3858 | &new_call); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3859 | if (status != PJ_SUCCESS) { |
| 3860 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3861 | /* Notify xferer about the error (if we have subscription) */ |
| 3862 | if (sub) { |
| 3863 | status = pjsip_xfer_notify(sub, PJSIP_EVSUB_STATE_TERMINATED, |
| 3864 | 500, NULL, &tdata); |
| 3865 | if (status != PJ_SUCCESS) { |
| 3866 | pjsua_perror(THIS_FILE, "Unable to create NOTIFY to REFER", |
| 3867 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3868 | goto on_return; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3869 | } |
| 3870 | status = pjsip_xfer_send_request(sub, tdata); |
| 3871 | if (status != PJ_SUCCESS) { |
| 3872 | pjsua_perror(THIS_FILE, "Unable to send NOTIFY to REFER", |
| 3873 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3874 | goto on_return; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3875 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3876 | } |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3877 | goto on_return; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3878 | } |
| 3879 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3880 | if (sub) { |
| 3881 | /* Put the server subscription in inv_data. |
| 3882 | * Subsequent state changed in pjsua_inv_on_state_changed() will be |
| 3883 | * reported back to the server subscription. |
| 3884 | */ |
| 3885 | pjsua_var.calls[new_call].xfer_sub = sub; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3886 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3887 | /* Put the invite_data in the subscription. */ |
| 3888 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, |
| 3889 | &pjsua_var.calls[new_call]); |
| 3890 | } |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3891 | |
| 3892 | on_return: |
| 3893 | pj_log_pop_indent(); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3894 | } |
| 3895 | |
| 3896 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3897 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3898 | /* |
| 3899 | * This callback is called when transaction state has changed in INVITE |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 3900 | * session. We use this to trap: |
| 3901 | * - incoming REFER request. |
| 3902 | * - incoming MESSAGE request. |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3903 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3904 | static void pjsua_call_on_tsx_state_changed(pjsip_inv_session *inv, |
| 3905 | pjsip_transaction *tsx, |
| 3906 | pjsip_event *e) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3907 | { |
Benny Prijono | 2285e7e | 2008-12-17 14:28:18 +0000 | [diff] [blame] | 3908 | pjsua_call *call; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3909 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3910 | pj_log_push_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3911 | PJSUA_LOCK(); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3912 | |
Benny Prijono | 2285e7e | 2008-12-17 14:28:18 +0000 | [diff] [blame] | 3913 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
| 3914 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3915 | if (call == NULL) |
| 3916 | goto on_return; |
Benny Prijono | 2285e7e | 2008-12-17 14:28:18 +0000 | [diff] [blame] | 3917 | |
Benny Prijono | 19eeb6e | 2009-06-04 22:16:47 +0000 | [diff] [blame] | 3918 | if (call->inv == NULL) { |
| 3919 | /* Shouldn't happen. It happens only when we don't terminate the |
| 3920 | * server subscription caused by REFER after the call has been |
| 3921 | * transfered (and this call has been disconnected), and we |
| 3922 | * receive another REFER for this call. |
| 3923 | */ |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3924 | goto on_return; |
Benny Prijono | 19eeb6e | 2009-06-04 22:16:47 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
Benny Prijono | feb69f4 | 2007-10-05 09:12:26 +0000 | [diff] [blame] | 3927 | /* Notify application callback first */ |
| 3928 | if (pjsua_var.ua_cfg.cb.on_call_tsx_state) { |
| 3929 | (*pjsua_var.ua_cfg.cb.on_call_tsx_state)(call->index, tsx, e); |
| 3930 | } |
| 3931 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3932 | if (tsx->role==PJSIP_ROLE_UAS && |
| 3933 | tsx->state==PJSIP_TSX_STATE_TRYING && |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 3934 | pjsip_method_cmp(&tsx->method, pjsip_get_refer_method())==0) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3935 | { |
| 3936 | /* |
| 3937 | * Incoming REFER request. |
| 3938 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3939 | on_call_transfered(call->inv, e->body.tsx_state.src.rdata); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 3940 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3941 | } |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 3942 | else if (tsx->role==PJSIP_ROLE_UAS && |
| 3943 | tsx->state==PJSIP_TSX_STATE_TRYING && |
| 3944 | pjsip_method_cmp(&tsx->method, &pjsip_message_method)==0) |
| 3945 | { |
| 3946 | /* |
| 3947 | * Incoming MESSAGE request! |
| 3948 | */ |
| 3949 | pjsip_rx_data *rdata; |
| 3950 | pjsip_msg *msg; |
| 3951 | pjsip_accept_hdr *accept_hdr; |
| 3952 | pj_status_t status; |
| 3953 | |
| 3954 | rdata = e->body.tsx_state.src.rdata; |
| 3955 | msg = rdata->msg_info.msg; |
| 3956 | |
| 3957 | /* Request MUST have message body, with Content-Type equal to |
| 3958 | * "text/plain". |
| 3959 | */ |
| 3960 | if (pjsua_im_accept_pager(rdata, &accept_hdr) == PJ_FALSE) { |
| 3961 | |
| 3962 | pjsip_hdr hdr_list; |
| 3963 | |
| 3964 | pj_list_init(&hdr_list); |
| 3965 | pj_list_push_back(&hdr_list, accept_hdr); |
| 3966 | |
| 3967 | pjsip_dlg_respond( inv->dlg, rdata, PJSIP_SC_NOT_ACCEPTABLE_HERE, |
| 3968 | NULL, &hdr_list, NULL ); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3969 | goto on_return; |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 3970 | } |
| 3971 | |
| 3972 | /* Respond with 200 first, so that remote doesn't retransmit in case |
| 3973 | * the UI takes too long to process the message. |
| 3974 | */ |
| 3975 | status = pjsip_dlg_respond( inv->dlg, rdata, 200, NULL, NULL, NULL); |
| 3976 | |
| 3977 | /* Process MESSAGE request */ |
| 3978 | pjsua_im_process_pager(call->index, &inv->dlg->remote.info_str, |
| 3979 | &inv->dlg->local.info_str, rdata); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3980 | |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 3981 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3982 | else if (tsx->role == PJSIP_ROLE_UAC && |
| 3983 | pjsip_method_cmp(&tsx->method, &pjsip_message_method)==0) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3984 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3985 | /* Handle outgoing pager status */ |
| 3986 | if (tsx->status_code >= 200) { |
| 3987 | pjsua_im_data *im_data; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3988 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3989 | im_data = (pjsua_im_data*) tsx->mod_data[pjsua_var.mod.id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3990 | /* im_data can be NULL if this is typing indication */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3991 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3992 | if (im_data && pjsua_var.ua_cfg.cb.on_pager_status) { |
| 3993 | pjsua_var.ua_cfg.cb.on_pager_status(im_data->call_id, |
| 3994 | &im_data->to, |
| 3995 | &im_data->body, |
| 3996 | im_data->user_data, |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 3997 | (pjsip_status_code) |
| 3998 | tsx->status_code, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3999 | &tsx->status_text); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4000 | } |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 4001 | } |
Sauw Ming | e7dbbc8 | 2011-10-24 09:28:13 +0000 | [diff] [blame^] | 4002 | } else if (tsx->role == PJSIP_ROLE_UAC && |
| 4003 | tsx->last_tx == (pjsip_tx_data*)call->hold_msg && |
| 4004 | tsx->state >= PJSIP_TSX_STATE_COMPLETED) |
| 4005 | { |
| 4006 | /* Monitor the status of call hold request */ |
| 4007 | call->hold_msg = NULL; |
| 4008 | if (tsx->status_code/100 != 2) { |
| 4009 | /* Outgoing call hold failed */ |
| 4010 | call->local_hold = PJ_FALSE; |
| 4011 | PJ_LOG(3,(THIS_FILE, "Error putting call %d on hold (reason=%d)", |
| 4012 | call->index, tsx->status_code)); |
| 4013 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 4014 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 4015 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 4016 | on_return: |
Sauw Ming | e7dbbc8 | 2011-10-24 09:28:13 +0000 | [diff] [blame^] | 4017 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 4018 | PJSUA_UNLOCK(); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 4019 | pj_log_pop_indent(); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 4020 | } |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 4021 | |
| 4022 | |
| 4023 | /* Redirection handler */ |
Benny Prijono | 08a48b8 | 2008-11-27 12:42:07 +0000 | [diff] [blame] | 4024 | static pjsip_redirect_op pjsua_call_on_redirected(pjsip_inv_session *inv, |
| 4025 | const pjsip_uri *target, |
| 4026 | const pjsip_event *e) |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 4027 | { |
| 4028 | pjsua_call *call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | 08a48b8 | 2008-11-27 12:42:07 +0000 | [diff] [blame] | 4029 | pjsip_redirect_op op; |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 4030 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 4031 | pj_log_push_indent(); |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 4032 | PJSUA_LOCK(); |
| 4033 | |
| 4034 | if (pjsua_var.ua_cfg.cb.on_call_redirected) { |
Benny Prijono | 08a48b8 | 2008-11-27 12:42:07 +0000 | [diff] [blame] | 4035 | op = (*pjsua_var.ua_cfg.cb.on_call_redirected)(call->index, |
| 4036 | target, e); |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 4037 | } else { |
| 4038 | PJ_LOG(4,(THIS_FILE, "Unhandled redirection for call %d " |
| 4039 | "(callback not implemented by application). Disconnecting " |
| 4040 | "call.", |
| 4041 | call->index)); |
Benny Prijono | 08a48b8 | 2008-11-27 12:42:07 +0000 | [diff] [blame] | 4042 | op = PJSIP_REDIRECT_STOP; |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 4043 | } |
| 4044 | |
| 4045 | PJSUA_UNLOCK(); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 4046 | pj_log_pop_indent(); |
Benny Prijono | 08a48b8 | 2008-11-27 12:42:07 +0000 | [diff] [blame] | 4047 | |
| 4048 | return op; |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 4049 | } |
| 4050 | |