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