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