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