blob: 46abb1f7ef7b9e56ae5085ea326e334e7ed4de46 [file] [log] [blame]
Benny Prijono84126ab2006-02-09 09:30:09 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
Benny Prijono4f9f64e2006-02-27 00:00:30 +000019#include <pjsua-lib/pjsua.h>
Benny Prijonoeebe9af2006-06-13 22:57:13 +000020#include <pjsua-lib/pjsua_internal.h>
21
22
23#define THIS_FILE "pjsua_call.c"
24
25
26/* This callback receives notification from invite session when the
27 * session state has changed.
28 */
29static void pjsua_call_on_state_changed(pjsip_inv_session *inv,
30 pjsip_event *e);
31
32/* This callback is called by invite session framework when UAC session
33 * has forked.
34 */
35static void pjsua_call_on_forked( pjsip_inv_session *inv,
36 pjsip_event *e);
Benny Prijono84126ab2006-02-09 09:30:09 +000037
38/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +000039 * Callback to be called when SDP offer/answer negotiation has just completed
40 * in the session. This function will start/update media if negotiation
41 * has succeeded.
Benny Prijono84126ab2006-02-09 09:30:09 +000042 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +000043static void pjsua_call_on_media_update(pjsip_inv_session *inv,
44 pj_status_t status);
Benny Prijono105217f2006-03-06 16:25:59 +000045
46/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +000047 * Called when session received new offer.
Benny Prijono105217f2006-03-06 16:25:59 +000048 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +000049static void pjsua_call_on_rx_offer(pjsip_inv_session *inv,
50 const pjmedia_sdp_session *offer);
51
52/*
53 * This callback is called when transaction state has changed in INVITE
54 * session. We use this to trap:
55 * - incoming REFER request.
56 * - incoming MESSAGE request.
57 */
58static void pjsua_call_on_tsx_state_changed(pjsip_inv_session *inv,
59 pjsip_transaction *tsx,
60 pjsip_event *e);
61
62
63/* Destroy the call's media */
64static pj_status_t call_destroy_media(int call_id);
65
66/* Create inactive SDP for call hold. */
67static pj_status_t create_inactive_sdp(pjsua_call *call,
68 pjmedia_sdp_session **p_answer);
69
Benny Prijonod524e822006-09-22 12:48:18 +000070/*
71 * Callback called by event framework when the xfer subscription state
72 * has changed.
73 */
Benny Prijono4ddad2c2006-10-18 17:16:34 +000074static void xfer_client_on_evsub_state( pjsip_evsub *sub, pjsip_event *event);
75static void xfer_server_on_evsub_state( pjsip_evsub *sub, pjsip_event *event);
76
77/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +000078 * Reset call descriptor.
79 */
80static void reset_call(pjsua_call_id id)
Benny Prijono105217f2006-03-06 16:25:59 +000081{
Benny Prijonoeebe9af2006-06-13 22:57:13 +000082 pjsua_call *call = &pjsua_var.calls[id];
Benny Prijono105217f2006-03-06 16:25:59 +000083
Benny Prijonoeebe9af2006-06-13 22:57:13 +000084 call->index = id;
85 call->inv = NULL;
86 call->user_data = NULL;
87 call->session = NULL;
88 call->xfer_sub = NULL;
89 call->last_code = 0;
90 call->conf_slot = PJSUA_INVALID_ID;
91 call->last_text.ptr = call->last_text_buf_;
92 call->last_text.slen = 0;
Benny Prijono4be63b52006-11-25 14:50:25 +000093 call->conn_time.sec = 0;
94 call->conn_time.msec = 0;
95 call->res_time.sec = 0;
96 call->res_time.msec = 0;
Benny Prijono105217f2006-03-06 16:25:59 +000097}
98
99
Benny Prijono275fd682006-03-22 11:59:11 +0000100/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000101 * Init call subsystem.
Benny Prijono275fd682006-03-22 11:59:11 +0000102 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000103pj_status_t pjsua_call_subsys_init(const pjsua_config *cfg)
Benny Prijono275fd682006-03-22 11:59:11 +0000104{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000105 pjsip_inv_callback inv_cb;
106 unsigned i;
Benny Prijonoc8141a82006-08-20 09:12:19 +0000107 const pj_str_t str_norefersub = { "norefersub", 10 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000108 pj_status_t status;
Benny Prijono275fd682006-03-22 11:59:11 +0000109
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000110 /* Init calls array. */
111 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.calls); ++i)
112 reset_call(i);
Benny Prijono275fd682006-03-22 11:59:11 +0000113
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000114 /* Copy config */
115 pjsua_config_dup(pjsua_var.pool, &pjsua_var.ua_cfg, cfg);
Benny Prijono275fd682006-03-22 11:59:11 +0000116
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000117 /* Initialize invite session callback. */
Benny Prijonoac623b32006-07-03 15:19:31 +0000118 pj_bzero(&inv_cb, sizeof(inv_cb));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000119 inv_cb.on_state_changed = &pjsua_call_on_state_changed;
120 inv_cb.on_new_session = &pjsua_call_on_forked;
121 inv_cb.on_media_update = &pjsua_call_on_media_update;
122 inv_cb.on_rx_offer = &pjsua_call_on_rx_offer;
123 inv_cb.on_tsx_state_changed = &pjsua_call_on_tsx_state_changed;
Benny Prijono275fd682006-03-22 11:59:11 +0000124
Benny Prijono275fd682006-03-22 11:59:11 +0000125
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000126 /* Initialize invite session module: */
127 status = pjsip_inv_usage_init(pjsua_var.endpt, &inv_cb);
128 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
129
Benny Prijonoc8141a82006-08-20 09:12:19 +0000130 /* Add "norefersub" in Supported header */
131 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_SUPPORTED,
132 NULL, 1, &str_norefersub);
133
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000134 return status;
135}
136
137
138/*
139 * Start call subsystem.
140 */
141pj_status_t pjsua_call_subsys_start(void)
142{
143 /* Nothing to do */
Benny Prijono275fd682006-03-22 11:59:11 +0000144 return PJ_SUCCESS;
145}
146
147
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000148/*
Benny Prijono9fc735d2006-05-28 14:58:12 +0000149 * Get maximum number of calls configured in pjsua.
150 */
Benny Prijono8b1889b2006-06-06 18:40:40 +0000151PJ_DEF(unsigned) pjsua_call_get_max_count(void)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000152{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000153 return pjsua_var.ua_cfg.max_calls;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000154}
155
156
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000157/*
158 * Get number of currently active calls.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000159 */
Benny Prijono8b1889b2006-06-06 18:40:40 +0000160PJ_DEF(unsigned) pjsua_call_get_count(void)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000161{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000162 return pjsua_var.call_cnt;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000163}
164
165
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000166/*
167 * Enum calls.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000168 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000169PJ_DEF(pj_status_t) pjsua_enum_calls( pjsua_call_id ids[],
170 unsigned *count)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000171{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000172 unsigned i, c;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000173
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000174 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
Benny Prijono9fc735d2006-05-28 14:58:12 +0000175
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000176 PJSUA_LOCK();
Benny Prijono9fc735d2006-05-28 14:58:12 +0000177
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000178 for (i=0, c=0; c<*count && i<pjsua_var.ua_cfg.max_calls; ++i) {
179 if (!pjsua_var.calls[i].inv)
180 continue;
181 ids[c] = i;
182 ++c;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000183 }
184
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000185 *count = c;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000186
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000187 PJSUA_UNLOCK();
Benny Prijono9fc735d2006-05-28 14:58:12 +0000188
189 return PJ_SUCCESS;
190}
191
192
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000193/*
194 * Make outgoing call to the specified URI using the specified account.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000195 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000196PJ_DEF(pj_status_t) pjsua_call_make_call( pjsua_acc_id acc_id,
197 const pj_str_t *dest_uri,
198 unsigned options,
199 void *user_data,
200 const pjsua_msg_data *msg_data,
201 pjsua_call_id *p_call_id)
Benny Prijono84126ab2006-02-09 09:30:09 +0000202{
Benny Prijono1c2bf462006-03-05 11:54:02 +0000203 pjsip_dialog *dlg = NULL;
Benny Prijono84126ab2006-02-09 09:30:09 +0000204 pjmedia_sdp_session *offer;
Benny Prijono1c2bf462006-03-05 11:54:02 +0000205 pjsip_inv_session *inv = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000206 pjsua_acc *acc;
207 pjsua_call *call;
208 unsigned call_id;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000209 pj_str_t contact;
Benny Prijono84126ab2006-02-09 09:30:09 +0000210 pjsip_tx_data *tdata;
211 pj_status_t status;
212
Benny Prijono9fc735d2006-05-28 14:58:12 +0000213
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000214 /* Check that account is valid */
215 PJ_ASSERT_RETURN(acc_id>=0 || acc_id<PJ_ARRAY_SIZE(pjsua_var.acc),
Benny Prijono9fc735d2006-05-28 14:58:12 +0000216 PJ_EINVAL);
217
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000218 /* Options must be zero for now */
219 PJ_ASSERT_RETURN(options == 0, PJ_EINVAL);
220
Benny Prijono320fa4d2006-12-07 10:09:16 +0000221 /* Check arguments */
222 PJ_ASSERT_RETURN(dest_uri, PJ_EINVAL);
223
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000224 PJSUA_LOCK();
225
226 acc = &pjsua_var.acc[acc_id];
227 if (!acc->valid) {
228 pjsua_perror(THIS_FILE, "Unable to make call because account "
229 "is not valid", PJ_EINVALIDOP);
230 PJSUA_UNLOCK();
231 return PJ_EINVALIDOP;
232 }
Benny Prijono84126ab2006-02-09 09:30:09 +0000233
Benny Prijonoa91a0032006-02-26 21:23:45 +0000234 /* Find free call slot. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000235 for (call_id=0; call_id<pjsua_var.ua_cfg.max_calls; ++call_id) {
236 if (pjsua_var.calls[call_id].inv == NULL)
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000237 break;
238 }
239
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000240 if (call_id == pjsua_var.ua_cfg.max_calls) {
241 pjsua_perror(THIS_FILE, "Error making file", PJ_ETOOMANY);
242 PJSUA_UNLOCK();
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000243 return PJ_ETOOMANY;
244 }
245
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000246 call = &pjsua_var.calls[call_id];
247
Benny Prijono320fa4d2006-12-07 10:09:16 +0000248 /* Verify that destination URI is valid before calling
249 * pjsua_acc_create_uac_contact, or otherwise there
250 * a misleading "Invalid Contact URI" error will be printed
251 * when pjsua_acc_create_uac_contact() fails.
252 */
253 if (1) {
254 pj_pool_t *pool;
255 pjsip_uri *uri;
256 pj_str_t dup;
257
258 pool = pjsua_pool_create("tmp-uri", 4000, 4000);
259 if (!pool) {
260 pjsua_perror(THIS_FILE, "Unable to create pool", PJ_ENOMEM);
261 PJSUA_UNLOCK();
262 return PJ_ENOMEM;
263 }
264
265 pj_strdup_with_null(pool, &dup, dest_uri);
266 uri = pjsip_parse_uri(pool, dup.ptr, dup.slen, 0);
267 pj_pool_release(pool);
268
269 if (uri == NULL) {
270 pjsua_perror(THIS_FILE, "Unable to make call",
271 PJSIP_EINVALIDREQURI);
272 PJSUA_UNLOCK();
273 return PJSIP_EINVALIDREQURI;
274 }
275 }
276
Benny Prijono093d3022006-09-24 00:07:11 +0000277 PJ_LOG(4,(THIS_FILE, "Making call with acc #%d to %.*s", acc_id,
278 (int)dest_uri->slen, dest_uri->ptr));
279
Benny Prijonoe21e7842006-04-09 16:46:05 +0000280 /* Mark call start time. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000281 pj_gettimeofday(&call->start_time);
Benny Prijono84126ab2006-02-09 09:30:09 +0000282
Benny Prijonoe21e7842006-04-09 16:46:05 +0000283 /* Reset first response time */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000284 call->res_time.sec = 0;
Benny Prijonoe21e7842006-04-09 16:46:05 +0000285
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000286 /* Create suitable Contact header */
287 status = pjsua_acc_create_uac_contact(pjsua_var.pool, &contact,
288 acc_id, dest_uri);
289 if (status != PJ_SUCCESS) {
290 pjsua_perror(THIS_FILE, "Unable to generate Contact header", status);
291 PJSUA_UNLOCK();
292 return status;
293 }
294
Benny Prijonoe21e7842006-04-09 16:46:05 +0000295 /* Create outgoing dialog: */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000296 status = pjsip_dlg_create_uac( pjsip_ua_instance(),
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000297 &acc->cfg.id, &contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000298 dest_uri, dest_uri, &dlg);
Benny Prijono84126ab2006-02-09 09:30:09 +0000299 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000300 pjsua_perror(THIS_FILE, "Dialog creation failed", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000301 PJSUA_UNLOCK();
Benny Prijono84126ab2006-02-09 09:30:09 +0000302 return status;
303 }
304
305 /* Get media capability from media endpoint: */
306
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000307 status = pjmedia_endpt_create_sdp( pjsua_var.med_endpt, dlg->pool, 1,
308 &call->skinfo, &offer);
Benny Prijono84126ab2006-02-09 09:30:09 +0000309 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000310 pjsua_perror(THIS_FILE, "pjmedia unable to create SDP", status);
Benny Prijono84126ab2006-02-09 09:30:09 +0000311 goto on_error;
312 }
313
314 /* Create the INVITE session: */
315
316 status = pjsip_inv_create_uac( dlg, offer, 0, &inv);
317 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000318 pjsua_perror(THIS_FILE, "Invite session creation failed", status);
Benny Prijono84126ab2006-02-09 09:30:09 +0000319 goto on_error;
320 }
321
322
323 /* Create and associate our data in the session. */
324
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000325 call->inv = inv;
Benny Prijonoa91a0032006-02-26 21:23:45 +0000326
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000327 dlg->mod_data[pjsua_var.mod.id] = call;
328 inv->mod_data[pjsua_var.mod.id] = call;
Benny Prijono84126ab2006-02-09 09:30:09 +0000329
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000330 /* Attach user data */
331 call->user_data = user_data;
Benny Prijono84126ab2006-02-09 09:30:09 +0000332
333 /* Set dialog Route-Set: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000334 if (!pj_list_empty(&acc->route_set))
335 pjsip_dlg_set_route_set(dlg, &acc->route_set);
Benny Prijono84126ab2006-02-09 09:30:09 +0000336
337
338 /* Set credentials: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000339 if (acc->cred_cnt) {
Benny Prijonodc39fe82006-05-26 12:17:46 +0000340 pjsip_auth_clt_set_credentials( &dlg->auth_sess,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000341 acc->cred_cnt, acc->cred);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000342 }
Benny Prijono84126ab2006-02-09 09:30:09 +0000343
344
345 /* Create initial INVITE: */
346
347 status = pjsip_inv_invite(inv, &tdata);
348 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000349 pjsua_perror(THIS_FILE, "Unable to create initial INVITE request",
350 status);
Benny Prijono84126ab2006-02-09 09:30:09 +0000351 goto on_error;
352 }
353
354
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000355 /* Add additional headers etc */
356
357 pjsua_process_msg_data( tdata, msg_data);
358
Benny Prijono093d3022006-09-24 00:07:11 +0000359 /* Must increment call counter now */
360 ++pjsua_var.call_cnt;
361
Benny Prijono84126ab2006-02-09 09:30:09 +0000362 /* Send initial INVITE: */
363
Benny Prijonoe8b0d3b2006-03-17 17:57:52 +0000364 status = pjsip_inv_send_msg(inv, tdata);
Benny Prijono84126ab2006-02-09 09:30:09 +0000365 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000366 pjsua_perror(THIS_FILE, "Unable to send initial INVITE request",
367 status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000368
369 /* Upon failure to send first request, both dialog and invite
370 * session would have been cleared.
371 */
372 inv = NULL;
373 dlg = NULL;
Benny Prijono84126ab2006-02-09 09:30:09 +0000374 goto on_error;
375 }
376
Benny Prijono84126ab2006-02-09 09:30:09 +0000377 /* Done. */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000378
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000379 if (p_call_id)
380 *p_call_id = call_id;
381
382 PJSUA_UNLOCK();
Benny Prijono84126ab2006-02-09 09:30:09 +0000383
384 return PJ_SUCCESS;
385
386
387on_error:
Benny Prijono1c2bf462006-03-05 11:54:02 +0000388 if (inv != NULL) {
389 pjsip_inv_terminate(inv, PJSIP_SC_OK, PJ_FALSE);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000390 } else if (dlg) {
Benny Prijono1c2bf462006-03-05 11:54:02 +0000391 pjsip_dlg_terminate(dlg);
392 }
393
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000394 if (call_id != -1) {
395 reset_call(call_id);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000396 }
397
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000398 PJSUA_UNLOCK();
Benny Prijono8b1889b2006-06-06 18:40:40 +0000399 return status;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000400}
401
402
403/**
Benny Prijono84126ab2006-02-09 09:30:09 +0000404 * Handle incoming INVITE request.
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000405 * Called by pjsua_core.c
Benny Prijono84126ab2006-02-09 09:30:09 +0000406 */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000407pj_bool_t pjsua_call_on_incoming(pjsip_rx_data *rdata)
Benny Prijono84126ab2006-02-09 09:30:09 +0000408{
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000409 pj_str_t contact;
Benny Prijono84126ab2006-02-09 09:30:09 +0000410 pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata);
Benny Prijono053f5222006-11-11 16:16:04 +0000411 pjsip_dialog *replaced_dlg = NULL;
Benny Prijono84126ab2006-02-09 09:30:09 +0000412 pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
413 pjsip_msg *msg = rdata->msg_info.msg;
Benny Prijono26ff9062006-02-21 23:47:00 +0000414 pjsip_tx_data *response = NULL;
415 unsigned options = 0;
Benny Prijono1c2bf462006-03-05 11:54:02 +0000416 pjsip_inv_session *inv = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000417 int acc_id;
418 pjsua_call *call;
419 int call_id = -1;
Benny Prijono26ff9062006-02-21 23:47:00 +0000420 pjmedia_sdp_session *answer;
Benny Prijono26ff9062006-02-21 23:47:00 +0000421 pj_status_t status;
Benny Prijono84126ab2006-02-09 09:30:09 +0000422
Benny Prijono26ff9062006-02-21 23:47:00 +0000423 /* Don't want to handle anything but INVITE */
424 if (msg->line.req.method.id != PJSIP_INVITE_METHOD)
425 return PJ_FALSE;
426
427 /* Don't want to handle anything that's already associated with
428 * existing dialog or transaction.
Benny Prijono84126ab2006-02-09 09:30:09 +0000429 */
Benny Prijono26ff9062006-02-21 23:47:00 +0000430 if (dlg || tsx)
431 return PJ_FALSE;
Benny Prijono84126ab2006-02-09 09:30:09 +0000432
Benny Prijono148c9dd2006-09-19 13:37:53 +0000433 PJSUA_LOCK();
Benny Prijono84126ab2006-02-09 09:30:09 +0000434
Benny Prijono26ff9062006-02-21 23:47:00 +0000435 /* Find free call slot. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000436 for (call_id=0; call_id<(int)pjsua_var.ua_cfg.max_calls; ++call_id) {
437 if (pjsua_var.calls[call_id].inv == NULL)
Benny Prijono26ff9062006-02-21 23:47:00 +0000438 break;
439 }
440
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000441 if (call_id == (int)pjsua_var.ua_cfg.max_calls) {
442 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata,
Benny Prijono26ff9062006-02-21 23:47:00 +0000443 PJSIP_SC_BUSY_HERE, NULL,
444 NULL, NULL);
Benny Prijonoa38ada02006-07-02 14:22:35 +0000445 PJ_LOG(2,(THIS_FILE,
446 "Unable to accept incoming call (too many calls)"));
Benny Prijono148c9dd2006-09-19 13:37:53 +0000447 PJSUA_UNLOCK();
Benny Prijono84126ab2006-02-09 09:30:09 +0000448 return PJ_TRUE;
449 }
450
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000451 /* Clear call descriptor */
452 reset_call(call_id);
Benny Prijonoe21e7842006-04-09 16:46:05 +0000453
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000454 call = &pjsua_var.calls[call_id];
455
456 /* Mark call start time. */
457 pj_gettimeofday(&call->start_time);
Benny Prijono26ff9062006-02-21 23:47:00 +0000458
Benny Prijono053f5222006-11-11 16:16:04 +0000459 /* Check INVITE request for Replaces header. If Replaces header is
460 * present, the function will make sure that we can handle the request.
461 */
462 status = pjsip_replaces_verify_request(rdata, &replaced_dlg, PJ_FALSE,
463 &response);
464 if (status != PJ_SUCCESS) {
465 /*
466 * Something wrong with the Replaces header.
467 */
468 if (response) {
469 pjsip_response_addr res_addr;
470
471 pjsip_get_response_addr(response->pool, rdata, &res_addr);
472 pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, response,
473 NULL, NULL);
474
475 } else {
476
477 /* Respond with 500 (Internal Server Error) */
478 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL,
479 NULL, NULL);
480 }
481
482 PJSUA_UNLOCK();
483 return PJ_TRUE;
484 }
485
486 /* If this INVITE request contains Replaces header, notify application
487 * about the request so that application can do subsequent checking
488 * if it wants to.
489 */
490 if (replaced_dlg != NULL && pjsua_var.ua_cfg.cb.on_call_replace_request) {
491 pjsua_call *replaced_call;
492 int st_code = 200;
493 pj_str_t st_text = { "OK", 2 };
494
495 /* Get the replaced call instance */
496 replaced_call = replaced_dlg->mod_data[pjsua_var.mod.id];
497
498 /* Notify application */
499 pjsua_var.ua_cfg.cb.on_call_replace_request(replaced_call->index,
500 rdata, &st_code, &st_text);
501
502 /* Must specify final response */
503 PJ_ASSERT_ON_FAIL(st_code >= 200, st_code = 200);
504
505 /* Check if application rejects this request. */
506 if (st_code >= 300) {
507
508 if (st_text.slen == 2)
509 st_text = *pjsip_get_status_text(st_code);
510
511 pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata,
512 st_code, &st_text, NULL, NULL, NULL);
513 PJSUA_UNLOCK();
514 return PJ_TRUE;
515 }
516 }
517
518
Benny Prijono26ff9062006-02-21 23:47:00 +0000519 /* Get media capability from media endpoint: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000520 status = pjmedia_endpt_create_sdp( pjsua_var.med_endpt,
521 rdata->tp_info.pool, 1,
522 &call->skinfo, &answer );
Benny Prijono26ff9062006-02-21 23:47:00 +0000523 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000524 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL,
Benny Prijono26ff9062006-02-21 23:47:00 +0000525 NULL, NULL);
Benny Prijono148c9dd2006-09-19 13:37:53 +0000526 PJSUA_UNLOCK();
Benny Prijono26ff9062006-02-21 23:47:00 +0000527 return PJ_TRUE;
528 }
529
Benny Prijono1d9b9a42006-09-25 13:40:12 +0000530
531 /* Verify that we can handle the request. */
532 status = pjsip_inv_verify_request(rdata, &options, answer, NULL,
533 pjsua_var.endpt, &response);
534 if (status != PJ_SUCCESS) {
535
536 /*
537 * No we can't handle the incoming INVITE request.
538 */
539
540 if (response) {
541 pjsip_response_addr res_addr;
542
543 pjsip_get_response_addr(response->pool, rdata, &res_addr);
544 pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, response,
545 NULL, NULL);
546
547 } else {
548
549 /* Respond with 500 (Internal Server Error) */
550 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL,
551 NULL, NULL);
552 }
553
554 PJSUA_UNLOCK();
555 return PJ_TRUE;
556 }
557
558
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000559 /*
Benny Prijonoa91a0032006-02-26 21:23:45 +0000560 * Get which account is most likely to be associated with this incoming
561 * call. We need the account to find which contact URI to put for
562 * the call.
563 */
Benny Prijono093d3022006-09-24 00:07:11 +0000564 acc_id = call->acc_id = pjsua_acc_find_for_incoming(rdata);
Benny Prijonoa91a0032006-02-26 21:23:45 +0000565
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000566 /* Get suitable Contact header */
567 status = pjsua_acc_create_uas_contact(rdata->tp_info.pool, &contact,
568 acc_id, rdata);
569 if (status != PJ_SUCCESS) {
570 pjsua_perror(THIS_FILE, "Unable to generate Contact header", status);
571 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL,
572 NULL, NULL);
Benny Prijono148c9dd2006-09-19 13:37:53 +0000573 PJSUA_UNLOCK();
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000574 return PJ_TRUE;
575 }
576
Benny Prijono26ff9062006-02-21 23:47:00 +0000577 /* Create dialog: */
Benny Prijono26ff9062006-02-21 23:47:00 +0000578 status = pjsip_dlg_create_uas( pjsip_ua_instance(), rdata,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000579 &contact, &dlg);
Benny Prijono26ff9062006-02-21 23:47:00 +0000580 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000581 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL,
Benny Prijono26ff9062006-02-21 23:47:00 +0000582 NULL, NULL);
Benny Prijono148c9dd2006-09-19 13:37:53 +0000583 PJSUA_UNLOCK();
Benny Prijono26ff9062006-02-21 23:47:00 +0000584 return PJ_TRUE;
585 }
586
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000587 /* Set credentials */
588 if (pjsua_var.acc[acc_id].cred_cnt) {
589 pjsip_auth_clt_set_credentials(&dlg->auth_sess,
590 pjsua_var.acc[acc_id].cred_cnt,
591 pjsua_var.acc[acc_id].cred);
592 }
Benny Prijono26ff9062006-02-21 23:47:00 +0000593
594 /* Create invite session: */
Benny Prijono26ff9062006-02-21 23:47:00 +0000595 status = pjsip_inv_create_uas( dlg, rdata, answer, 0, &inv);
596 if (status != PJ_SUCCESS) {
Benny Prijonoa38ada02006-07-02 14:22:35 +0000597 pjsip_hdr hdr_list;
598 pjsip_warning_hdr *w;
599
600 w = pjsip_warning_hdr_create_from_status(dlg->pool,
601 pjsip_endpt_name(pjsua_var.endpt),
602 status);
603 pj_list_init(&hdr_list);
604 pj_list_push_back(&hdr_list, w);
605
606 pjsip_dlg_respond(dlg, rdata, 500, NULL, &hdr_list, NULL);
607
608 /* Can't terminate dialog because transaction is in progress.
Benny Prijono1c2bf462006-03-05 11:54:02 +0000609 pjsip_dlg_terminate(dlg);
Benny Prijonoa38ada02006-07-02 14:22:35 +0000610 */
Benny Prijono148c9dd2006-09-19 13:37:53 +0000611 PJSUA_UNLOCK();
Benny Prijono26ff9062006-02-21 23:47:00 +0000612 return PJ_TRUE;
613 }
614
615
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000616 /* Create and attach pjsua_var data to the dialog: */
617 call->inv = inv;
Benny Prijono26ff9062006-02-21 23:47:00 +0000618
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000619 dlg->mod_data[pjsua_var.mod.id] = call;
620 inv->mod_data[pjsua_var.mod.id] = call;
Benny Prijono26ff9062006-02-21 23:47:00 +0000621
622
Benny Prijono64f851e2006-02-23 13:49:28 +0000623 /* Must answer with some response to initial INVITE.
Benny Prijono64f851e2006-02-23 13:49:28 +0000624 */
Benny Prijono64f851e2006-02-23 13:49:28 +0000625 status = pjsip_inv_initial_answer(inv, rdata,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000626 100, NULL, NULL, &response);
Benny Prijono26ff9062006-02-21 23:47:00 +0000627 if (status != PJ_SUCCESS) {
Benny Prijonoccb03fa2006-03-06 13:35:47 +0000628 pjsua_perror(THIS_FILE, "Unable to send answer to incoming INVITE",
629 status);
630
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000631 pjsip_dlg_respond(dlg, rdata, 500, NULL, NULL, NULL);
632 pjsip_inv_terminate(inv, 500, PJ_FALSE);
Benny Prijono148c9dd2006-09-19 13:37:53 +0000633 PJSUA_UNLOCK();
Benny Prijonoccb03fa2006-03-06 13:35:47 +0000634 return PJ_TRUE;
Benny Prijono26ff9062006-02-21 23:47:00 +0000635
636 } else {
Benny Prijonoe8b0d3b2006-03-17 17:57:52 +0000637 status = pjsip_inv_send_msg(inv, response);
Benny Prijonod2ae29d2006-02-22 15:42:31 +0000638 if (status != PJ_SUCCESS)
639 pjsua_perror(THIS_FILE, "Unable to send 100 response", status);
Benny Prijono26ff9062006-02-21 23:47:00 +0000640 }
641
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000642 ++pjsua_var.call_cnt;
Benny Prijono64f851e2006-02-23 13:49:28 +0000643
Benny Prijono105217f2006-03-06 16:25:59 +0000644
Benny Prijono053f5222006-11-11 16:16:04 +0000645 /* Check if this request should replace existing call */
646 if (replaced_dlg) {
647 pjsip_inv_session *replaced_inv;
648 struct pjsua_call *replaced_call;
649 pjsip_tx_data *tdata;
650
651 /* Get the invite session in the dialog */
652 replaced_inv = pjsip_dlg_get_inv_session(replaced_dlg);
653
654 /* Get the replaced call instance */
655 replaced_call = replaced_dlg->mod_data[pjsua_var.mod.id];
656
657 /* Notify application */
658 if (pjsua_var.ua_cfg.cb.on_call_replaced)
659 pjsua_var.ua_cfg.cb.on_call_replaced(replaced_call->index,
660 call_id);
661
662 PJ_LOG(4,(THIS_FILE, "Answering replacement call %d with 200/OK",
663 call_id));
664
665 /* Answer the new call with 200 response */
666 status = pjsip_inv_answer(inv, 200, NULL, NULL, &tdata);
667 if (status == PJ_SUCCESS)
668 status = pjsip_inv_send_msg(inv, tdata);
669
670 if (status != PJ_SUCCESS)
671 pjsua_perror(THIS_FILE, "Error answering session", status);
672
673
674 PJ_LOG(4,(THIS_FILE, "Disconnecting replaced call %d",
675 replaced_call->index));
676
677 /* Disconnect replaced invite session */
678 status = pjsip_inv_end_session(replaced_inv, PJSIP_SC_GONE, NULL,
679 &tdata);
680 if (status == PJ_SUCCESS && tdata)
681 status = pjsip_inv_send_msg(replaced_inv, tdata);
682
683 if (status != PJ_SUCCESS)
684 pjsua_perror(THIS_FILE, "Error terminating session", status);
685
686
687 } else {
688
689 /* Notify application */
690 if (pjsua_var.ua_cfg.cb.on_incoming_call)
691 pjsua_var.ua_cfg.cb.on_incoming_call(acc_id, call_id, rdata);
692
693 }
694
Benny Prijono8b1889b2006-06-06 18:40:40 +0000695
Benny Prijono26ff9062006-02-21 23:47:00 +0000696 /* This INVITE request has been handled. */
Benny Prijono148c9dd2006-09-19 13:37:53 +0000697 PJSUA_UNLOCK();
Benny Prijono26ff9062006-02-21 23:47:00 +0000698 return PJ_TRUE;
Benny Prijono84126ab2006-02-09 09:30:09 +0000699}
700
701
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000702
703/*
704 * Check if the specified call has active INVITE session and the INVITE
705 * session has not been disconnected.
706 */
707PJ_DEF(pj_bool_t) pjsua_call_is_active(pjsua_call_id call_id)
708{
709 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
710 PJ_EINVAL);
711 return pjsua_var.calls[call_id].inv != NULL &&
712 pjsua_var.calls[call_id].inv->state != PJSIP_INV_STATE_DISCONNECTED;
713}
714
715
716/*
717 * Check if call has an active media session.
718 */
719PJ_DEF(pj_bool_t) pjsua_call_has_media(pjsua_call_id call_id)
720{
721 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
722 PJ_EINVAL);
723 return pjsua_var.calls[call_id].session != NULL;
724}
725
726
Benny Prijono148c9dd2006-09-19 13:37:53 +0000727/* Acquire lock to the specified call_id */
728static pj_status_t acquire_call(const char *title,
729 pjsua_call_id call_id,
Benny Prijonodc752ca2006-09-22 16:55:42 +0000730 pjsua_call **p_call,
731 pjsip_dialog **p_dlg)
Benny Prijono148c9dd2006-09-19 13:37:53 +0000732{
733 enum { MAX_RETRY=50 };
734 unsigned retry;
Benny Prijonod524e822006-09-22 12:48:18 +0000735 pjsua_call *call = NULL;
736 pj_bool_t has_pjsua_lock = PJ_FALSE;
737 pj_status_t status = PJ_SUCCESS;
Benny Prijono148c9dd2006-09-19 13:37:53 +0000738
739 for (retry=0; retry<MAX_RETRY; ++retry) {
740
741 has_pjsua_lock = PJ_FALSE;
742
743 status = PJSUA_TRY_LOCK();
744 if (status != PJ_SUCCESS) {
745 pj_thread_sleep(retry/10);
746 continue;
747 }
748
749 has_pjsua_lock = PJ_TRUE;
750 call = &pjsua_var.calls[call_id];
751
752 if (call->inv == NULL) {
753 PJSUA_UNLOCK();
754 PJ_LOG(3,(THIS_FILE, "Invalid call_id %d in %s", call_id, title));
755 return PJSIP_ESESSIONTERMINATED;
756 }
757
758 status = pjsip_dlg_try_inc_lock(call->inv->dlg);
759 if (status != PJ_SUCCESS) {
760 PJSUA_UNLOCK();
761 pj_thread_sleep(retry/10);
762 continue;
763 }
764
765 PJSUA_UNLOCK();
766
767 break;
768 }
769
770 if (status != PJ_SUCCESS) {
771 if (has_pjsua_lock == PJ_FALSE)
772 PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire PJSUA mutex "
773 "(possibly system has deadlocked) in %s",
774 title));
775 else
776 PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire dialog mutex "
777 "(possibly system has deadlocked) in %s",
778 title));
779 return PJ_ETIMEDOUT;
780 }
781
782 *p_call = call;
Benny Prijonodc752ca2006-09-22 16:55:42 +0000783 *p_dlg = call->inv->dlg;
Benny Prijono148c9dd2006-09-19 13:37:53 +0000784
785 return PJ_SUCCESS;
786}
787
788
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000789/*
790 * Get the conference port identification associated with the call.
791 */
792PJ_DEF(pjsua_conf_port_id) pjsua_call_get_conf_port(pjsua_call_id call_id)
793{
Benny Prijono148c9dd2006-09-19 13:37:53 +0000794 pjsua_call *call;
795 pjsua_conf_port_id port_id;
Benny Prijonodc752ca2006-09-22 16:55:42 +0000796 pjsip_dialog *dlg;
Benny Prijono148c9dd2006-09-19 13:37:53 +0000797 pj_status_t status;
798
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000799 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
800 PJ_EINVAL);
Benny Prijono148c9dd2006-09-19 13:37:53 +0000801
Benny Prijonodc752ca2006-09-22 16:55:42 +0000802 status = acquire_call("pjsua_call_get_conf_port()", call_id, &call, &dlg);
Benny Prijono148c9dd2006-09-19 13:37:53 +0000803 if (status != PJ_SUCCESS)
Benny Prijonod524e822006-09-22 12:48:18 +0000804 return PJSUA_INVALID_ID;
Benny Prijono148c9dd2006-09-19 13:37:53 +0000805
806 port_id = call->conf_slot;
807
Benny Prijonodc752ca2006-09-22 16:55:42 +0000808 pjsip_dlg_dec_lock(dlg);
Benny Prijono148c9dd2006-09-19 13:37:53 +0000809
810 return port_id;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000811}
812
813
Benny Prijono148c9dd2006-09-19 13:37:53 +0000814
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000815/*
816 * Obtain detail information about the specified call.
817 */
818PJ_DEF(pj_status_t) pjsua_call_get_info( pjsua_call_id call_id,
819 pjsua_call_info *info)
820{
821 pjsua_call *call;
Benny Prijonodc752ca2006-09-22 16:55:42 +0000822 pjsip_dialog *dlg;
Benny Prijono148c9dd2006-09-19 13:37:53 +0000823 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000824
825 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
826 PJ_EINVAL);
827
Benny Prijonoac623b32006-07-03 15:19:31 +0000828 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000829
Benny Prijonodc752ca2006-09-22 16:55:42 +0000830 status = acquire_call("pjsua_call_get_info()", call_id, &call, &dlg);
Benny Prijono148c9dd2006-09-19 13:37:53 +0000831 if (status != PJ_SUCCESS) {
832 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000833 }
834
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000835 /* id and role */
836 info->id = call_id;
837 info->role = call->inv->role;
Benny Prijono90315512006-09-14 16:05:16 +0000838 info->acc_id = call->acc_id;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000839
840 /* local info */
841 info->local_info.ptr = info->buf_.local_info;
842 pj_strncpy(&info->local_info, &call->inv->dlg->local.info_str,
843 sizeof(info->buf_.local_info));
844
845 /* local contact */
846 info->local_contact.ptr = info->buf_.local_contact;
847 info->local_contact.slen = pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR,
848 call->inv->dlg->local.contact->uri,
849 info->local_contact.ptr,
850 sizeof(info->buf_.local_contact));
851
852 /* remote info */
853 info->remote_info.ptr = info->buf_.remote_info;
854 pj_strncpy(&info->remote_info, &call->inv->dlg->remote.info_str,
855 sizeof(info->buf_.remote_info));
856
857 /* remote contact */
858 if (call->inv->dlg->remote.contact) {
859 int len;
860 info->remote_contact.ptr = info->buf_.remote_contact;
861 len = pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR,
862 call->inv->dlg->remote.contact->uri,
863 info->remote_contact.ptr,
864 sizeof(info->buf_.remote_contact));
865 if (len < 0) len = 0;
866 info->remote_contact.slen = len;
867 } else {
868 info->remote_contact.slen = 0;
869 }
870
871 /* call id */
872 info->call_id.ptr = info->buf_.call_id;
873 pj_strncpy(&info->call_id, &call->inv->dlg->call_id->id,
874 sizeof(info->buf_.call_id));
875
876 /* state, state_text */
877 info->state = call->inv->state;
878 info->state_text = pj_str((char*)pjsip_inv_state_name(info->state));
879
880 /* If call is disconnected, set the last_status from the cause code */
881 if (call->inv->state >= PJSIP_INV_STATE_DISCONNECTED) {
882 /* last_status, last_status_text */
883 info->last_status = call->inv->cause;
884
885 info->last_status_text.ptr = info->buf_.last_status_text;
886 pj_strncpy(&info->last_status_text, &call->inv->cause_text,
887 sizeof(info->buf_.last_status_text));
888 } else {
889 /* last_status, last_status_text */
890 info->last_status = call->last_code;
891
892 info->last_status_text.ptr = info->buf_.last_status_text;
893 pj_strncpy(&info->last_status_text, &call->last_text,
894 sizeof(info->buf_.last_status_text));
895 }
896
897 /* media status and dir */
898 info->media_status = call->media_st;
899 info->media_dir = call->media_dir;
900
901
902 /* conference slot number */
903 info->conf_slot = call->conf_slot;
904
905 /* calculate duration */
906 if (info->state >= PJSIP_INV_STATE_DISCONNECTED) {
907
908 info->total_duration = call->dis_time;
909 PJ_TIME_VAL_SUB(info->total_duration, call->start_time);
910
911 if (call->conn_time.sec) {
912 info->connect_duration = call->dis_time;
913 PJ_TIME_VAL_SUB(info->connect_duration, call->conn_time);
914 }
915
916 } else if (info->state == PJSIP_INV_STATE_CONFIRMED) {
917
918 pj_gettimeofday(&info->total_duration);
919 PJ_TIME_VAL_SUB(info->total_duration, call->start_time);
920
921 pj_gettimeofday(&info->connect_duration);
922 PJ_TIME_VAL_SUB(info->connect_duration, call->conn_time);
923
924 } else {
925 pj_gettimeofday(&info->total_duration);
926 PJ_TIME_VAL_SUB(info->total_duration, call->start_time);
927 }
928
Benny Prijonodc752ca2006-09-22 16:55:42 +0000929 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000930
931 return PJ_SUCCESS;
932}
933
934
935/*
936 * Attach application specific data to the call.
937 */
938PJ_DEF(pj_status_t) pjsua_call_set_user_data( pjsua_call_id call_id,
939 void *user_data)
940{
941 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
942 PJ_EINVAL);
943 pjsua_var.calls[call_id].user_data = user_data;
944
945 return PJ_SUCCESS;
946}
947
948
949/*
950 * Get user data attached to the call.
951 */
952PJ_DEF(void*) pjsua_call_get_user_data(pjsua_call_id call_id)
953{
954 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
955 NULL);
956 return pjsua_var.calls[call_id].user_data;
957}
958
959
960/*
961 * Send response to incoming INVITE request.
962 */
963PJ_DEF(pj_status_t) pjsua_call_answer( pjsua_call_id call_id,
964 unsigned code,
965 const pj_str_t *reason,
966 const pjsua_msg_data *msg_data)
967{
968 pjsua_call *call;
Benny Prijonodc752ca2006-09-22 16:55:42 +0000969 pjsip_dialog *dlg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000970 pjsip_tx_data *tdata;
971 pj_status_t status;
972
973 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
974 PJ_EINVAL);
975
Benny Prijonodc752ca2006-09-22 16:55:42 +0000976 status = acquire_call("pjsua_call_answer()", call_id, &call, &dlg);
Benny Prijono148c9dd2006-09-19 13:37:53 +0000977 if (status != PJ_SUCCESS)
978 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000979
Benny Prijono2e507c22006-06-23 15:04:11 +0000980 if (call->res_time.sec == 0)
981 pj_gettimeofday(&call->res_time);
982
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000983 /* Create response message */
984 status = pjsip_inv_answer(call->inv, code, reason, NULL, &tdata);
985 if (status != PJ_SUCCESS) {
986 pjsua_perror(THIS_FILE, "Error creating response",
987 status);
Benny Prijonodc752ca2006-09-22 16:55:42 +0000988 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000989 return status;
990 }
991
992 /* Add additional headers etc */
993 pjsua_process_msg_data( tdata, msg_data);
994
995 /* Send the message */
996 status = pjsip_inv_send_msg(call->inv, tdata);
997 if (status != PJ_SUCCESS)
998 pjsua_perror(THIS_FILE, "Error sending response",
999 status);
1000
Benny Prijonodc752ca2006-09-22 16:55:42 +00001001 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001002
1003 return status;
1004}
1005
1006
1007/*
1008 * Hangup call by using method that is appropriate according to the
1009 * call state.
1010 */
1011PJ_DEF(pj_status_t) pjsua_call_hangup(pjsua_call_id call_id,
1012 unsigned code,
1013 const pj_str_t *reason,
1014 const pjsua_msg_data *msg_data)
1015{
1016 pjsua_call *call;
Benny Prijonodc752ca2006-09-22 16:55:42 +00001017 pjsip_dialog *dlg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001018 pj_status_t status;
1019 pjsip_tx_data *tdata;
1020
1021
Benny Prijono148c9dd2006-09-19 13:37:53 +00001022 if (call_id<0 || call_id>=(int)pjsua_var.ua_cfg.max_calls) {
1023 PJ_LOG(1,(THIS_FILE, "pjsua_call_hangup(): invalid call id %d",
1024 call_id));
1025 }
1026
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001027 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
1028 PJ_EINVAL);
1029
Benny Prijonodc752ca2006-09-22 16:55:42 +00001030 status = acquire_call("pjsua_call_hangup()", call_id, &call, &dlg);
Benny Prijono148c9dd2006-09-19 13:37:53 +00001031 if (status != PJ_SUCCESS)
1032 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001033
1034 if (code==0) {
1035 if (call->inv->state == PJSIP_INV_STATE_CONFIRMED)
1036 code = PJSIP_SC_OK;
1037 else if (call->inv->role == PJSIP_ROLE_UAS)
1038 code = PJSIP_SC_DECLINE;
1039 else
1040 code = PJSIP_SC_REQUEST_TERMINATED;
1041 }
1042
1043 status = pjsip_inv_end_session(call->inv, code, reason, &tdata);
1044 if (status != PJ_SUCCESS) {
1045 pjsua_perror(THIS_FILE,
1046 "Failed to create end session message",
1047 status);
Benny Prijonodc752ca2006-09-22 16:55:42 +00001048 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001049 return status;
1050 }
1051
1052 /* pjsip_inv_end_session may return PJ_SUCCESS with NULL
1053 * as p_tdata when INVITE transaction has not been answered
1054 * with any provisional responses.
1055 */
1056 if (tdata == NULL) {
Benny Prijonodc752ca2006-09-22 16:55:42 +00001057 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001058 return PJ_SUCCESS;
1059 }
1060
1061 /* Add additional headers etc */
1062 pjsua_process_msg_data( tdata, msg_data);
1063
1064 /* Send the message */
1065 status = pjsip_inv_send_msg(call->inv, tdata);
1066 if (status != PJ_SUCCESS) {
1067 pjsua_perror(THIS_FILE,
1068 "Failed to send end session message",
1069 status);
Benny Prijonodc752ca2006-09-22 16:55:42 +00001070 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001071 return status;
1072 }
1073
Benny Prijonodc752ca2006-09-22 16:55:42 +00001074 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001075
1076 return PJ_SUCCESS;
1077}
1078
1079
1080/*
1081 * Put the specified call on hold.
1082 */
1083PJ_DEF(pj_status_t) pjsua_call_set_hold(pjsua_call_id call_id,
1084 const pjsua_msg_data *msg_data)
1085{
1086 pjmedia_sdp_session *sdp;
1087 pjsua_call *call;
Benny Prijonodc752ca2006-09-22 16:55:42 +00001088 pjsip_dialog *dlg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001089 pjsip_tx_data *tdata;
1090 pj_status_t status;
1091
1092 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
1093 PJ_EINVAL);
1094
Benny Prijonodc752ca2006-09-22 16:55:42 +00001095 status = acquire_call("pjsua_call_set_hold()", call_id, &call, &dlg);
Benny Prijono148c9dd2006-09-19 13:37:53 +00001096 if (status != PJ_SUCCESS)
1097 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001098
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001099
1100 if (call->inv->state != PJSIP_INV_STATE_CONFIRMED) {
1101 PJ_LOG(3,(THIS_FILE, "Can not hold call that is not confirmed"));
Benny Prijonodc752ca2006-09-22 16:55:42 +00001102 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001103 return PJSIP_ESESSIONSTATE;
1104 }
1105
1106 status = create_inactive_sdp(call, &sdp);
1107 if (status != PJ_SUCCESS) {
Benny Prijonodc752ca2006-09-22 16:55:42 +00001108 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001109 return status;
1110 }
1111
1112 /* Create re-INVITE with new offer */
1113 status = pjsip_inv_reinvite( call->inv, NULL, sdp, &tdata);
1114 if (status != PJ_SUCCESS) {
1115 pjsua_perror(THIS_FILE, "Unable to create re-INVITE", status);
Benny Prijonodc752ca2006-09-22 16:55:42 +00001116 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001117 return status;
1118 }
1119
1120 /* Add additional headers etc */
1121 pjsua_process_msg_data( tdata, msg_data);
1122
1123 /* Send the request */
1124 status = pjsip_inv_send_msg( call->inv, tdata);
1125 if (status != PJ_SUCCESS) {
1126 pjsua_perror(THIS_FILE, "Unable to send re-INVITE", status);
Benny Prijonodc752ca2006-09-22 16:55:42 +00001127 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001128 return status;
1129 }
1130
Benny Prijonodc752ca2006-09-22 16:55:42 +00001131 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001132
1133 return PJ_SUCCESS;
1134}
1135
1136
1137/*
1138 * Send re-INVITE (to release hold).
1139 */
1140PJ_DEF(pj_status_t) pjsua_call_reinvite( pjsua_call_id call_id,
1141 pj_bool_t unhold,
1142 const pjsua_msg_data *msg_data)
1143{
1144 pjmedia_sdp_session *sdp;
1145 pjsip_tx_data *tdata;
1146 pjsua_call *call;
Benny Prijonodc752ca2006-09-22 16:55:42 +00001147 pjsip_dialog *dlg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001148 pj_status_t status;
1149
1150
1151 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
1152 PJ_EINVAL);
1153
Benny Prijonodc752ca2006-09-22 16:55:42 +00001154 status = acquire_call("pjsua_call_reinvite()", call_id, &call, &dlg);
Benny Prijono148c9dd2006-09-19 13:37:53 +00001155 if (status != PJ_SUCCESS)
1156 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001157
1158 if (call->inv->state != PJSIP_INV_STATE_CONFIRMED) {
1159 PJ_LOG(3,(THIS_FILE, "Can not re-INVITE call that is not confirmed"));
Benny Prijonodc752ca2006-09-22 16:55:42 +00001160 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001161 return PJSIP_ESESSIONSTATE;
1162 }
1163
1164 /* Create SDP */
Benny Prijono00cae612006-07-31 15:19:36 +00001165 PJ_UNUSED_ARG(unhold);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001166 PJ_TODO(create_active_inactive_sdp_based_on_unhold_arg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001167 status = pjmedia_endpt_create_sdp( pjsua_var.med_endpt, call->inv->pool,
1168 1, &call->skinfo, &sdp);
1169 if (status != PJ_SUCCESS) {
1170 pjsua_perror(THIS_FILE, "Unable to get SDP from media endpoint",
1171 status);
Benny Prijonodc752ca2006-09-22 16:55:42 +00001172 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001173 return status;
1174 }
1175
1176 /* Create re-INVITE with new offer */
1177 status = pjsip_inv_reinvite( call->inv, NULL, sdp, &tdata);
1178 if (status != PJ_SUCCESS) {
1179 pjsua_perror(THIS_FILE, "Unable to create re-INVITE", status);
Benny Prijonodc752ca2006-09-22 16:55:42 +00001180 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001181 return status;
1182 }
1183
1184 /* Add additional headers etc */
1185 pjsua_process_msg_data( tdata, msg_data);
1186
1187 /* Send the request */
1188 status = pjsip_inv_send_msg( call->inv, tdata);
1189 if (status != PJ_SUCCESS) {
1190 pjsua_perror(THIS_FILE, "Unable to send re-INVITE", status);
Benny Prijonodc752ca2006-09-22 16:55:42 +00001191 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001192 return status;
1193 }
1194
Benny Prijonodc752ca2006-09-22 16:55:42 +00001195 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001196
1197 return PJ_SUCCESS;
1198}
1199
1200
1201/*
1202 * Initiate call transfer to the specified address.
1203 */
1204PJ_DEF(pj_status_t) pjsua_call_xfer( pjsua_call_id call_id,
1205 const pj_str_t *dest,
1206 const pjsua_msg_data *msg_data)
1207{
1208 pjsip_evsub *sub;
1209 pjsip_tx_data *tdata;
1210 pjsua_call *call;
Benny Prijonodc752ca2006-09-22 16:55:42 +00001211 pjsip_dialog *dlg;
Benny Prijono053f5222006-11-11 16:16:04 +00001212 pjsip_generic_string_hdr *gs_hdr;
1213 const pj_str_t str_ref_by = { "Referred-By", 11 };
Benny Prijonod524e822006-09-22 12:48:18 +00001214 struct pjsip_evsub_user xfer_cb;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001215 pj_status_t status;
1216
1217
1218 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
1219 PJ_EINVAL);
1220
Benny Prijonodc752ca2006-09-22 16:55:42 +00001221 status = acquire_call("pjsua_call_xfer()", call_id, &call, &dlg);
Benny Prijono148c9dd2006-09-19 13:37:53 +00001222 if (status != PJ_SUCCESS)
1223 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001224
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001225
Benny Prijonod524e822006-09-22 12:48:18 +00001226 /* Create xfer client subscription. */
1227 pj_bzero(&xfer_cb, sizeof(xfer_cb));
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001228 xfer_cb.on_evsub_state = &xfer_client_on_evsub_state;
Benny Prijonod524e822006-09-22 12:48:18 +00001229
1230 status = pjsip_xfer_create_uac(call->inv->dlg, &xfer_cb, &sub);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001231 if (status != PJ_SUCCESS) {
1232 pjsua_perror(THIS_FILE, "Unable to create xfer", status);
Benny Prijonodc752ca2006-09-22 16:55:42 +00001233 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001234 return status;
1235 }
1236
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001237 /* Associate this call with the client subscription */
1238 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, call);
1239
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001240 /*
1241 * Create REFER request.
1242 */
1243 status = pjsip_xfer_initiate(sub, dest, &tdata);
1244 if (status != PJ_SUCCESS) {
1245 pjsua_perror(THIS_FILE, "Unable to create REFER request", status);
Benny Prijonodc752ca2006-09-22 16:55:42 +00001246 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001247 return status;
1248 }
1249
Benny Prijono053f5222006-11-11 16:16:04 +00001250 /* Add Referred-By header */
1251 gs_hdr = pjsip_generic_string_hdr_create(tdata->pool, &str_ref_by,
1252 &dlg->local.info_str);
1253 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)gs_hdr);
1254
1255
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001256 /* Add additional headers etc */
1257 pjsua_process_msg_data( tdata, msg_data);
1258
1259 /* Send. */
1260 status = pjsip_xfer_send_request(sub, tdata);
1261 if (status != PJ_SUCCESS) {
1262 pjsua_perror(THIS_FILE, "Unable to send REFER request", status);
Benny Prijonodc752ca2006-09-22 16:55:42 +00001263 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001264 return status;
1265 }
1266
1267 /* For simplicity (that's what this program is intended to be!),
1268 * leave the original invite session as it is. More advanced application
1269 * may want to hold the INVITE, or terminate the invite, or whatever.
1270 */
1271
Benny Prijonodc752ca2006-09-22 16:55:42 +00001272 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001273
1274 return PJ_SUCCESS;
1275
1276}
1277
1278
1279/*
Benny Prijono053f5222006-11-11 16:16:04 +00001280 * Initiate attended call transfer to the specified address.
1281 */
1282PJ_DEF(pj_status_t) pjsua_call_xfer_replaces( pjsua_call_id call_id,
1283 pjsua_call_id dest_call_id,
1284 unsigned options,
1285 const pjsua_msg_data *msg_data)
1286{
1287 pjsua_call *dest_call;
1288 pjsip_dialog *dest_dlg;
1289 char str_dest_buf[512];
1290 pj_str_t str_dest;
1291 int len;
1292 pjsip_uri *uri;
1293 pj_status_t status;
1294
1295
1296 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
1297 PJ_EINVAL);
1298 PJ_ASSERT_RETURN(dest_call_id>=0 &&
1299 dest_call_id<(int)pjsua_var.ua_cfg.max_calls,
1300 PJ_EINVAL);
1301
1302 status = acquire_call("pjsua_call_xfer_replaces()", dest_call_id,
1303 &dest_call, &dest_dlg);
1304 if (status != PJ_SUCCESS)
1305 return status;
1306
1307 /*
1308 * Create REFER destination URI with Replaces field.
1309 */
1310
1311 /* Make sure we have sufficient buffer's length */
1312 PJ_ASSERT_RETURN( dest_dlg->remote.info_str.slen +
1313 dest_dlg->call_id->id.slen +
1314 dest_dlg->remote.info->tag.slen +
1315 dest_dlg->local.info->tag.slen + 32
1316 < sizeof(str_dest_buf), PJSIP_EURITOOLONG);
1317
1318 /* Print URI */
1319 str_dest_buf[0] = '<';
1320 str_dest.slen = 1;
1321
1322 uri = pjsip_uri_get_uri(dest_dlg->remote.info->uri);
1323 len = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, uri,
1324 str_dest_buf+1, sizeof(str_dest_buf)-1);
1325 if (len < 0)
1326 return PJSIP_EURITOOLONG;
1327
1328 str_dest.slen += len;
1329
1330
1331 /* Build the URI */
1332 len = pj_ansi_snprintf(str_dest_buf + str_dest.slen,
1333 sizeof(str_dest_buf) - str_dest.slen,
1334 "?%s"
1335 "Replaces=%.*s"
1336 "%%3Bto-tag%%3D%.*s"
1337 "%%3Bfrom-tag%%3D%.*s>",
1338 ((options&PJSUA_XFER_NO_REQUIRE_REPLACES) ?
1339 "" : "Require=replaces&"),
1340 (int)dest_dlg->call_id->id.slen,
1341 dest_dlg->call_id->id.ptr,
1342 (int)dest_dlg->remote.info->tag.slen,
1343 dest_dlg->remote.info->tag.ptr,
1344 (int)dest_dlg->local.info->tag.slen,
1345 dest_dlg->local.info->tag.ptr);
1346
1347 PJ_ASSERT_RETURN(len > 0 && len <= (int)sizeof(str_dest_buf)-str_dest.slen,
1348 PJSIP_EURITOOLONG);
1349
1350 str_dest.ptr = str_dest_buf;
1351 str_dest.slen += len;
1352
1353 pjsip_dlg_dec_lock(dest_dlg);
1354
1355 return pjsua_call_xfer(call_id, &str_dest, msg_data);
1356}
1357
1358
1359/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001360 * Send DTMF digits to remote using RFC 2833 payload formats.
1361 */
1362PJ_DEF(pj_status_t) pjsua_call_dial_dtmf( pjsua_call_id call_id,
1363 const pj_str_t *digits)
1364{
1365 pjsua_call *call;
Benny Prijonodc752ca2006-09-22 16:55:42 +00001366 pjsip_dialog *dlg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001367 pj_status_t status;
1368
1369 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
1370 PJ_EINVAL);
1371
Benny Prijonodc752ca2006-09-22 16:55:42 +00001372 status = acquire_call("pjsua_call_dial_dtmf()", call_id, &call, &dlg);
Benny Prijono148c9dd2006-09-19 13:37:53 +00001373 if (status != PJ_SUCCESS)
1374 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001375
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001376 if (!call->session) {
1377 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
Benny Prijonodc752ca2006-09-22 16:55:42 +00001378 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001379 return PJ_EINVALIDOP;
1380 }
1381
1382 status = pjmedia_session_dial_dtmf( call->session, 0, digits);
1383
Benny Prijonodc752ca2006-09-22 16:55:42 +00001384 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001385
1386 return status;
1387}
1388
1389
1390/**
1391 * Send instant messaging inside INVITE session.
1392 */
1393PJ_DEF(pj_status_t) pjsua_call_send_im( pjsua_call_id call_id,
1394 const pj_str_t *mime_type,
1395 const pj_str_t *content,
1396 const pjsua_msg_data *msg_data,
1397 void *user_data)
1398{
1399 pjsua_call *call;
Benny Prijonodc752ca2006-09-22 16:55:42 +00001400 pjsip_dialog *dlg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001401 const pj_str_t mime_text_plain = pj_str("text/plain");
1402 pjsip_media_type ctype;
1403 pjsua_im_data *im_data;
1404 pjsip_tx_data *tdata;
1405 pj_status_t status;
1406
1407
1408 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
1409 PJ_EINVAL);
1410
Benny Prijonodc752ca2006-09-22 16:55:42 +00001411 status = acquire_call("pjsua_call_send_im()", call_id, &call, &dlg);
Benny Prijono148c9dd2006-09-19 13:37:53 +00001412 if (status != PJ_SUCCESS)
1413 return status;
1414
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001415 /* Set default media type if none is specified */
1416 if (mime_type == NULL) {
1417 mime_type = &mime_text_plain;
1418 }
1419
1420 /* Create request message. */
1421 status = pjsip_dlg_create_request( call->inv->dlg, &pjsip_message_method,
1422 -1, &tdata);
1423 if (status != PJ_SUCCESS) {
1424 pjsua_perror(THIS_FILE, "Unable to create MESSAGE request", status);
1425 goto on_return;
1426 }
1427
1428 /* Add accept header. */
1429 pjsip_msg_add_hdr( tdata->msg,
1430 (pjsip_hdr*)pjsua_im_create_accept(tdata->pool));
1431
1432 /* Parse MIME type */
1433 pjsua_parse_media_type(tdata->pool, mime_type, &ctype);
1434
1435 /* Create "text/plain" message body. */
1436 tdata->msg->body = pjsip_msg_body_create( tdata->pool, &ctype.type,
1437 &ctype.subtype, content);
1438 if (tdata->msg->body == NULL) {
1439 pjsua_perror(THIS_FILE, "Unable to create msg body", PJ_ENOMEM);
1440 pjsip_tx_data_dec_ref(tdata);
1441 goto on_return;
1442 }
1443
1444 /* Add additional headers etc */
1445 pjsua_process_msg_data( tdata, msg_data);
1446
1447 /* Create IM data and attach to the request. */
1448 im_data = pj_pool_zalloc(tdata->pool, sizeof(*im_data));
1449 im_data->acc_id = call->acc_id;
1450 im_data->call_id = call_id;
1451 im_data->to = call->inv->dlg->remote.info_str;
1452 pj_strdup_with_null(tdata->pool, &im_data->body, content);
1453 im_data->user_data = user_data;
1454
1455
1456 /* Send the request. */
1457 status = pjsip_dlg_send_request( call->inv->dlg, tdata,
1458 pjsua_var.mod.id, im_data);
1459 if (status != PJ_SUCCESS) {
1460 pjsua_perror(THIS_FILE, "Unable to send MESSAGE request", status);
1461 goto on_return;
1462 }
1463
1464on_return:
Benny Prijonodc752ca2006-09-22 16:55:42 +00001465 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001466 return status;
1467}
1468
1469
1470/*
1471 * Send IM typing indication inside INVITE session.
1472 */
1473PJ_DEF(pj_status_t) pjsua_call_send_typing_ind( pjsua_call_id call_id,
1474 pj_bool_t is_typing,
1475 const pjsua_msg_data*msg_data)
1476{
1477 pjsua_call *call;
Benny Prijonodc752ca2006-09-22 16:55:42 +00001478 pjsip_dialog *dlg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001479 pjsip_tx_data *tdata;
1480 pj_status_t status;
1481
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001482 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
1483 PJ_EINVAL);
1484
Benny Prijonodc752ca2006-09-22 16:55:42 +00001485 status = acquire_call("pjsua_call_send_typing_ind", call_id, &call, &dlg);
Benny Prijono148c9dd2006-09-19 13:37:53 +00001486 if (status != PJ_SUCCESS)
1487 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001488
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001489 /* Create request message. */
1490 status = pjsip_dlg_create_request( call->inv->dlg, &pjsip_message_method,
1491 -1, &tdata);
1492 if (status != PJ_SUCCESS) {
1493 pjsua_perror(THIS_FILE, "Unable to create MESSAGE request", status);
1494 goto on_return;
1495 }
1496
1497 /* Create "application/im-iscomposing+xml" msg body. */
1498 tdata->msg->body = pjsip_iscomposing_create_body(tdata->pool, is_typing,
1499 NULL, NULL, -1);
1500
1501 /* Add additional headers etc */
1502 pjsua_process_msg_data( tdata, msg_data);
1503
1504 /* Send the request. */
1505 status = pjsip_dlg_send_request( call->inv->dlg, tdata, -1, NULL);
1506 if (status != PJ_SUCCESS) {
1507 pjsua_perror(THIS_FILE, "Unable to send MESSAGE request", status);
1508 goto on_return;
1509 }
1510
1511on_return:
Benny Prijonodc752ca2006-09-22 16:55:42 +00001512 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001513 return status;
1514}
1515
1516
1517/*
1518 * Terminate all calls.
1519 */
1520PJ_DEF(void) pjsua_call_hangup_all(void)
1521{
1522 unsigned i;
1523
1524 PJSUA_LOCK();
1525
1526 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
1527 if (pjsua_var.calls[i].inv)
1528 pjsua_call_hangup(i, 0, NULL, NULL);
1529 }
1530
1531 PJSUA_UNLOCK();
1532}
1533
1534
1535static const char *good_number(char *buf, pj_int32_t val)
1536{
1537 if (val < 1000) {
1538 pj_ansi_sprintf(buf, "%d", val);
1539 } else if (val < 1000000) {
1540 pj_ansi_sprintf(buf, "%d.%dK",
1541 val / 1000,
1542 (val % 1000) / 100);
1543 } else {
1544 pj_ansi_sprintf(buf, "%d.%02dM",
1545 val / 1000000,
1546 (val % 1000000) / 10000);
1547 }
1548
1549 return buf;
1550}
1551
1552
1553/* Dump media session */
1554static void dump_media_session(const char *indent,
1555 char *buf, unsigned maxlen,
1556 pjmedia_session *session)
1557{
1558 unsigned i;
1559 char *p = buf, *end = buf+maxlen;
1560 int len;
1561 pjmedia_session_info info;
1562
1563 pjmedia_session_get_info(session, &info);
1564
1565 for (i=0; i<info.stream_cnt; ++i) {
1566 pjmedia_rtcp_stat stat;
1567 const char *rem_addr;
1568 int rem_port;
1569 const char *dir;
Benny Prijonoe85bc412006-07-29 20:29:24 +00001570 char last_update[64];
Benny Prijono80019eb2006-08-07 13:22:23 +00001571 char packets[32], bytes[32], ipbytes[32], avg_bps[32];
1572 pj_time_val media_duration, now;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001573
1574 pjmedia_session_get_stream_stat(session, i, &stat);
1575 rem_addr = pj_inet_ntoa(info.stream_info[i].rem_addr.sin_addr);
1576 rem_port = pj_ntohs(info.stream_info[i].rem_addr.sin_port);
1577
1578 if (info.stream_info[i].dir == PJMEDIA_DIR_ENCODING)
1579 dir = "sendonly";
1580 else if (info.stream_info[i].dir == PJMEDIA_DIR_DECODING)
1581 dir = "recvonly";
1582 else if (info.stream_info[i].dir == PJMEDIA_DIR_ENCODING_DECODING)
1583 dir = "sendrecv";
1584 else
1585 dir = "inactive";
1586
1587
1588 len = pj_ansi_snprintf(buf, end-p,
1589 "%s #%d %.*s @%dKHz, %s, peer=%s:%d",
1590 indent, i,
Benny Prijono172cd732006-06-14 20:22:31 +00001591 (int)info.stream_info[i].fmt.encoding_name.slen,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001592 info.stream_info[i].fmt.encoding_name.ptr,
1593 info.stream_info[i].fmt.clock_rate / 1000,
1594 dir,
1595 rem_addr, rem_port);
1596 if (len < 1 || len > end-p) {
1597 *p = '\0';
1598 return;
1599 }
1600
1601 p += len;
1602 *p++ = '\n';
1603 *p = '\0';
1604
1605 if (stat.rx.update_cnt == 0)
1606 strcpy(last_update, "never");
1607 else {
1608 pj_gettimeofday(&now);
1609 PJ_TIME_VAL_SUB(now, stat.rx.update);
1610 sprintf(last_update, "%02ldh:%02ldm:%02ld.%03lds ago",
1611 now.sec / 3600,
1612 (now.sec % 3600) / 60,
1613 now.sec % 60,
1614 now.msec);
1615 }
1616
Benny Prijono80019eb2006-08-07 13:22:23 +00001617 pj_gettimeofday(&media_duration);
1618 PJ_TIME_VAL_SUB(media_duration, stat.start);
1619 if (PJ_TIME_VAL_MSEC(media_duration) == 0)
1620 media_duration.msec = 1;
1621
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001622 len = pj_ansi_snprintf(p, end-p,
1623 "%s RX pt=%d, stat last update: %s\n"
Benny Prijono80019eb2006-08-07 13:22:23 +00001624 "%s total %spkt %sB (%sB +IP hdr) @avg=%sbps\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001625 "%s pkt loss=%d (%3.1f%%), dup=%d (%3.1f%%), reorder=%d (%3.1f%%)\n"
1626 "%s (msec) min avg max last\n"
1627 "%s loss period: %7.3f %7.3f %7.3f %7.3f\n"
1628 "%s jitter : %7.3f %7.3f %7.3f %7.3f%s",
1629 indent, info.stream_info[i].fmt.pt,
1630 last_update,
1631 indent,
1632 good_number(packets, stat.rx.pkt),
1633 good_number(bytes, stat.rx.bytes),
1634 good_number(ipbytes, stat.rx.bytes + stat.rx.pkt * 32),
Benny Prijono80019eb2006-08-07 13:22:23 +00001635 good_number(avg_bps, stat.rx.bytes * 8 * 1000 / PJ_TIME_VAL_MSEC(media_duration)),
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001636 indent,
1637 stat.rx.loss,
Benny Prijonof9016512006-06-29 09:41:34 +00001638 stat.rx.loss * 100.0 / (stat.rx.pkt + stat.rx.loss),
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001639 stat.rx.dup,
Benny Prijonob12106f2006-06-29 14:45:17 +00001640 stat.rx.dup * 100.0 / (stat.rx.pkt + stat.rx.loss),
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001641 stat.rx.reorder,
Benny Prijonob12106f2006-06-29 14:45:17 +00001642 stat.rx.reorder * 100.0 / (stat.rx.pkt + stat.rx.loss),
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001643 indent, indent,
1644 stat.rx.loss_period.min / 1000.0,
1645 stat.rx.loss_period.avg / 1000.0,
1646 stat.rx.loss_period.max / 1000.0,
1647 stat.rx.loss_period.last / 1000.0,
1648 indent,
1649 stat.rx.jitter.min / 1000.0,
1650 stat.rx.jitter.avg / 1000.0,
1651 stat.rx.jitter.max / 1000.0,
1652 stat.rx.jitter.last / 1000.0,
1653 ""
1654 );
1655
1656 if (len < 1 || len > end-p) {
1657 *p = '\0';
1658 return;
1659 }
1660
1661 p += len;
1662 *p++ = '\n';
1663 *p = '\0';
1664
1665 if (stat.tx.update_cnt == 0)
1666 strcpy(last_update, "never");
1667 else {
1668 pj_gettimeofday(&now);
1669 PJ_TIME_VAL_SUB(now, stat.tx.update);
1670 sprintf(last_update, "%02ldh:%02ldm:%02ld.%03lds ago",
1671 now.sec / 3600,
1672 (now.sec % 3600) / 60,
1673 now.sec % 60,
1674 now.msec);
1675 }
1676
1677 len = pj_ansi_snprintf(p, end-p,
1678 "%s TX pt=%d, ptime=%dms, stat last update: %s\n"
Benny Prijono80019eb2006-08-07 13:22:23 +00001679 "%s total %spkt %sB (%sB +IP hdr) @avg %sbps\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001680 "%s pkt loss=%d (%3.1f%%), dup=%d (%3.1f%%), reorder=%d (%3.1f%%)\n"
1681 "%s (msec) min avg max last\n"
1682 "%s loss period: %7.3f %7.3f %7.3f %7.3f\n"
1683 "%s jitter : %7.3f %7.3f %7.3f %7.3f%s",
1684 indent,
1685 info.stream_info[i].tx_pt,
1686 info.stream_info[i].param->info.frm_ptime *
1687 info.stream_info[i].param->setting.frm_per_pkt,
1688 last_update,
1689
1690 indent,
1691 good_number(packets, stat.tx.pkt),
1692 good_number(bytes, stat.tx.bytes),
1693 good_number(ipbytes, stat.tx.bytes + stat.tx.pkt * 32),
Benny Prijono80019eb2006-08-07 13:22:23 +00001694 good_number(avg_bps, stat.tx.bytes * 8 * 1000 / PJ_TIME_VAL_MSEC(media_duration)),
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001695
1696 indent,
1697 stat.tx.loss,
Benny Prijonof9016512006-06-29 09:41:34 +00001698 stat.tx.loss * 100.0 / (stat.tx.pkt + stat.tx.loss),
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001699 stat.tx.dup,
Benny Prijonob12106f2006-06-29 14:45:17 +00001700 stat.tx.dup * 100.0 / (stat.tx.pkt + stat.tx.loss),
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001701 stat.tx.reorder,
Benny Prijonob12106f2006-06-29 14:45:17 +00001702 stat.tx.reorder * 100.0 / (stat.tx.pkt + stat.tx.loss),
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001703
1704 indent, indent,
1705 stat.tx.loss_period.min / 1000.0,
1706 stat.tx.loss_period.avg / 1000.0,
1707 stat.tx.loss_period.max / 1000.0,
1708 stat.tx.loss_period.last / 1000.0,
1709 indent,
1710 stat.tx.jitter.min / 1000.0,
1711 stat.tx.jitter.avg / 1000.0,
1712 stat.tx.jitter.max / 1000.0,
1713 stat.tx.jitter.last / 1000.0,
1714 ""
1715 );
1716
1717 if (len < 1 || len > end-p) {
1718 *p = '\0';
1719 return;
1720 }
1721
1722 p += len;
1723 *p++ = '\n';
1724 *p = '\0';
1725
1726 len = pj_ansi_snprintf(p, end-p,
1727 "%s RTT msec : %7.3f %7.3f %7.3f %7.3f",
1728 indent,
1729 stat.rtt.min / 1000.0,
1730 stat.rtt.avg / 1000.0,
1731 stat.rtt.max / 1000.0,
1732 stat.rtt.last / 1000.0
1733 );
1734 if (len < 1 || len > end-p) {
1735 *p = '\0';
1736 return;
1737 }
1738
1739 p += len;
1740 *p++ = '\n';
1741 *p = '\0';
1742 }
1743}
1744
1745
1746/* Print call info */
1747static void print_call(const char *title,
1748 int call_id,
1749 char *buf, pj_size_t size)
1750{
1751 int len;
1752 pjsip_inv_session *inv = pjsua_var.calls[call_id].inv;
1753 pjsip_dialog *dlg = inv->dlg;
1754 char userinfo[128];
1755
1756 /* Dump invite sesion info. */
1757
1758 len = pjsip_hdr_print_on(dlg->remote.info, userinfo, sizeof(userinfo));
1759 if (len < 1)
1760 pj_ansi_strcpy(userinfo, "<--uri too long-->");
1761 else
1762 userinfo[len] = '\0';
1763
1764 len = pj_ansi_snprintf(buf, size, "%s[%s] %s",
1765 title,
1766 pjsip_inv_state_name(inv->state),
1767 userinfo);
1768 if (len < 1 || len >= (int)size) {
1769 pj_ansi_strcpy(buf, "<--uri too long-->");
1770 len = 18;
1771 } else
1772 buf[len] = '\0';
1773}
1774
1775
1776/*
1777 * Dump call and media statistics to string.
1778 */
1779PJ_DEF(pj_status_t) pjsua_call_dump( pjsua_call_id call_id,
1780 pj_bool_t with_media,
1781 char *buffer,
1782 unsigned maxlen,
1783 const char *indent)
1784{
1785 pjsua_call *call;
Benny Prijonodc752ca2006-09-22 16:55:42 +00001786 pjsip_dialog *dlg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001787 pj_time_val duration, res_delay, con_delay;
1788 char tmp[128];
1789 char *p, *end;
Benny Prijono148c9dd2006-09-19 13:37:53 +00001790 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001791 int len;
1792
1793 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
1794 PJ_EINVAL);
1795
Benny Prijonodc752ca2006-09-22 16:55:42 +00001796 status = acquire_call("pjsua_call_dump()", call_id, &call, &dlg);
Benny Prijono148c9dd2006-09-19 13:37:53 +00001797 if (status != PJ_SUCCESS)
1798 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001799
1800 *buffer = '\0';
1801 p = buffer;
1802 end = buffer + maxlen;
1803 len = 0;
1804
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001805 print_call(indent, call_id, tmp, sizeof(tmp));
1806
1807 len = pj_ansi_strlen(tmp);
1808 pj_ansi_strcpy(buffer, tmp);
1809
1810 p += len;
1811 *p++ = '\r';
1812 *p++ = '\n';
1813
1814 /* Calculate call duration */
Benny Prijono4be63b52006-11-25 14:50:25 +00001815 if (call->conn_time.sec != 0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001816 pj_gettimeofday(&duration);
1817 PJ_TIME_VAL_SUB(duration, call->conn_time);
1818 con_delay = call->conn_time;
1819 PJ_TIME_VAL_SUB(con_delay, call->start_time);
1820 } else {
1821 duration.sec = duration.msec = 0;
1822 con_delay.sec = con_delay.msec = 0;
1823 }
1824
1825 /* Calculate first response delay */
Benny Prijono4be63b52006-11-25 14:50:25 +00001826 if (call->res_time.sec != 0) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001827 res_delay = call->res_time;
1828 PJ_TIME_VAL_SUB(res_delay, call->start_time);
1829 } else {
1830 res_delay.sec = res_delay.msec = 0;
1831 }
1832
1833 /* Print duration */
1834 len = pj_ansi_snprintf(p, end-p,
1835 "%s Call time: %02dh:%02dm:%02ds, "
1836 "1st res in %d ms, conn in %dms",
1837 indent,
Benny Prijono172cd732006-06-14 20:22:31 +00001838 (int)(duration.sec / 3600),
1839 (int)((duration.sec % 3600)/60),
1840 (int)(duration.sec % 60),
1841 (int)PJ_TIME_VAL_MSEC(res_delay),
1842 (int)PJ_TIME_VAL_MSEC(con_delay));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001843
1844 if (len > 0 && len < end-p) {
1845 p += len;
1846 *p++ = '\n';
1847 *p = '\0';
1848 }
1849
1850 /* Dump session statistics */
1851 if (with_media && call->session)
1852 dump_media_session(indent, p, end-p, call->session);
1853
Benny Prijonodc752ca2006-09-22 16:55:42 +00001854 pjsip_dlg_dec_lock(dlg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001855
1856 return PJ_SUCCESS;
1857}
1858
1859
1860/*
1861 * Destroy the call's media
1862 */
1863static pj_status_t call_destroy_media(int call_id)
1864{
1865 pjsua_call *call = &pjsua_var.calls[call_id];
1866
1867 if (call->conf_slot != PJSUA_INVALID_ID) {
1868 pjmedia_conf_remove_port(pjsua_var.mconf, call->conf_slot);
1869 call->conf_slot = PJSUA_INVALID_ID;
1870 }
1871
1872 if (call->session) {
1873 /* Destroy session (this will also close RTP/RTCP sockets). */
1874 pjmedia_session_destroy(call->session);
1875 call->session = NULL;
1876
1877 PJ_LOG(4,(THIS_FILE, "Media session for call %d is destroyed",
1878 call_id));
1879
1880 }
1881
1882 call->media_st = PJSUA_CALL_MEDIA_NONE;
1883
1884 return PJ_SUCCESS;
1885}
1886
1887
Benny Prijono84126ab2006-02-09 09:30:09 +00001888/*
1889 * This callback receives notification from invite session when the
1890 * session state has changed.
1891 */
Benny Prijonoa91a0032006-02-26 21:23:45 +00001892static void pjsua_call_on_state_changed(pjsip_inv_session *inv,
1893 pjsip_event *e)
Benny Prijono84126ab2006-02-09 09:30:09 +00001894{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001895 pjsua_call *call;
Benny Prijono26ff9062006-02-21 23:47:00 +00001896
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001897 PJSUA_LOCK();
1898
1899 call = inv->dlg->mod_data[pjsua_var.mod.id];
1900
1901 if (!call) {
1902 PJSUA_UNLOCK();
Benny Prijonoe21e7842006-04-09 16:46:05 +00001903 return;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001904 }
1905
Benny Prijonoe21e7842006-04-09 16:46:05 +00001906
1907 /* Get call times */
1908 switch (inv->state) {
1909 case PJSIP_INV_STATE_EARLY:
1910 case PJSIP_INV_STATE_CONNECTING:
1911 if (call->res_time.sec == 0)
1912 pj_gettimeofday(&call->res_time);
Benny Prijono8b1889b2006-06-06 18:40:40 +00001913 call->last_code = e->body.tsx_state.tsx->status_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001914 pj_strncpy(&call->last_text,
1915 &e->body.tsx_state.tsx->status_text,
1916 sizeof(call->last_text_buf_));
Benny Prijonoe21e7842006-04-09 16:46:05 +00001917 break;
1918 case PJSIP_INV_STATE_CONFIRMED:
1919 pj_gettimeofday(&call->conn_time);
1920 break;
1921 case PJSIP_INV_STATE_DISCONNECTED:
1922 pj_gettimeofday(&call->dis_time);
Benny Prijono2e507c22006-06-23 15:04:11 +00001923 if (call->res_time.sec == 0)
1924 pj_gettimeofday(&call->res_time);
Benny Prijono8b1889b2006-06-06 18:40:40 +00001925 if (e->body.tsx_state.tsx->status_code > call->last_code) {
1926 call->last_code = e->body.tsx_state.tsx->status_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001927 pj_strncpy(&call->last_text,
1928 &e->body.tsx_state.tsx->status_text,
1929 sizeof(call->last_text_buf_));
Benny Prijono8b1889b2006-06-06 18:40:40 +00001930 }
Benny Prijonoe21e7842006-04-09 16:46:05 +00001931 break;
Benny Prijono8befd9f2006-05-13 22:46:23 +00001932 default:
Benny Prijono8b1889b2006-06-06 18:40:40 +00001933 call->last_code = e->body.tsx_state.tsx->status_code;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001934 pj_strncpy(&call->last_text,
1935 &e->body.tsx_state.tsx->status_text,
1936 sizeof(call->last_text_buf_));
Benny Prijono8befd9f2006-05-13 22:46:23 +00001937 break;
Benny Prijonoe21e7842006-04-09 16:46:05 +00001938 }
1939
Benny Prijono26ff9062006-02-21 23:47:00 +00001940 /* If this is an outgoing INVITE that was created because of
1941 * REFER/transfer, send NOTIFY to transferer.
1942 */
Benny Prijonoe21e7842006-04-09 16:46:05 +00001943 if (call->xfer_sub && e->type==PJSIP_EVENT_TSX_STATE) {
Benny Prijono26ff9062006-02-21 23:47:00 +00001944 int st_code = -1;
1945 pjsip_evsub_state ev_state = PJSIP_EVSUB_STATE_ACTIVE;
1946
1947
Benny Prijonoa91a0032006-02-26 21:23:45 +00001948 switch (call->inv->state) {
Benny Prijono26ff9062006-02-21 23:47:00 +00001949 case PJSIP_INV_STATE_NULL:
1950 case PJSIP_INV_STATE_CALLING:
1951 /* Do nothing */
1952 break;
1953
1954 case PJSIP_INV_STATE_EARLY:
1955 case PJSIP_INV_STATE_CONNECTING:
1956 st_code = e->body.tsx_state.tsx->status_code;
1957 ev_state = PJSIP_EVSUB_STATE_ACTIVE;
1958 break;
1959
1960 case PJSIP_INV_STATE_CONFIRMED:
1961 /* When state is confirmed, send the final 200/OK and terminate
1962 * subscription.
1963 */
1964 st_code = e->body.tsx_state.tsx->status_code;
1965 ev_state = PJSIP_EVSUB_STATE_TERMINATED;
1966 break;
1967
1968 case PJSIP_INV_STATE_DISCONNECTED:
1969 st_code = e->body.tsx_state.tsx->status_code;
1970 ev_state = PJSIP_EVSUB_STATE_TERMINATED;
1971 break;
Benny Prijono8befd9f2006-05-13 22:46:23 +00001972
Benny Prijono8b1889b2006-06-06 18:40:40 +00001973 case PJSIP_INV_STATE_INCOMING:
Benny Prijono8befd9f2006-05-13 22:46:23 +00001974 /* Nothing to do. Just to keep gcc from complaining about
1975 * unused enums.
1976 */
1977 break;
Benny Prijono26ff9062006-02-21 23:47:00 +00001978 }
1979
1980 if (st_code != -1) {
1981 pjsip_tx_data *tdata;
1982 pj_status_t status;
1983
Benny Prijonoa91a0032006-02-26 21:23:45 +00001984 status = pjsip_xfer_notify( call->xfer_sub,
Benny Prijono26ff9062006-02-21 23:47:00 +00001985 ev_state, st_code,
1986 NULL, &tdata);
1987 if (status != PJ_SUCCESS) {
1988 pjsua_perror(THIS_FILE, "Unable to create NOTIFY", status);
1989 } else {
Benny Prijonoa91a0032006-02-26 21:23:45 +00001990 status = pjsip_xfer_send_request(call->xfer_sub, tdata);
Benny Prijono26ff9062006-02-21 23:47:00 +00001991 if (status != PJ_SUCCESS) {
1992 pjsua_perror(THIS_FILE, "Unable to send NOTIFY", status);
1993 }
1994 }
1995 }
1996 }
1997
Benny Prijono84126ab2006-02-09 09:30:09 +00001998
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001999 if (pjsua_var.ua_cfg.cb.on_call_state)
2000 (*pjsua_var.ua_cfg.cb.on_call_state)(call->index, e);
Benny Prijonoa91a0032006-02-26 21:23:45 +00002001
2002 /* call->inv may be NULL now */
2003
Benny Prijono84126ab2006-02-09 09:30:09 +00002004 /* Destroy media session when invite session is disconnected. */
2005 if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
Benny Prijono632ce712006-02-09 14:01:40 +00002006
Benny Prijonoa91a0032006-02-26 21:23:45 +00002007 pj_assert(call != NULL);
Benny Prijono632ce712006-02-09 14:01:40 +00002008
Benny Prijono275fd682006-03-22 11:59:11 +00002009 if (call)
2010 call_destroy_media(call->index);
Benny Prijono84126ab2006-02-09 09:30:09 +00002011
Benny Prijono105217f2006-03-06 16:25:59 +00002012 /* Free call */
Benny Prijonoa91a0032006-02-26 21:23:45 +00002013 call->inv = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002014 --pjsua_var.call_cnt;
Benny Prijono4be63b52006-11-25 14:50:25 +00002015
2016 /* Reset call */
2017 reset_call(call->index);
2018
Benny Prijono84126ab2006-02-09 09:30:09 +00002019 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002020
2021 PJSUA_UNLOCK();
2022}
2023
2024/*
2025 * This callback is called by invite session framework when UAC session
2026 * has forked.
2027 */
2028static void pjsua_call_on_forked( pjsip_inv_session *inv,
2029 pjsip_event *e)
2030{
2031 PJ_UNUSED_ARG(inv);
2032 PJ_UNUSED_ARG(e);
2033
2034 PJ_TODO(HANDLE_FORKED_DIALOG);
2035}
2036
2037
2038/*
Benny Prijonoa38ada02006-07-02 14:22:35 +00002039 * Disconnect call upon error.
2040 */
2041static void call_disconnect( pjsip_inv_session *inv,
2042 int code )
2043{
2044 pjsip_tx_data *tdata;
2045 pj_status_t status;
2046
2047 status = pjsip_inv_end_session(inv, code, NULL, &tdata);
2048 if (status == PJ_SUCCESS)
2049 pjsip_inv_send_msg(inv, tdata);
2050}
2051
2052/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002053 * Callback to be called when SDP offer/answer negotiation has just completed
2054 * in the session. This function will start/update media if negotiation
2055 * has succeeded.
2056 */
2057static void pjsua_call_on_media_update(pjsip_inv_session *inv,
2058 pj_status_t status)
2059{
2060 int prev_media_st = 0;
2061 pjsua_call *call;
2062 pjmedia_session_info sess_info;
2063 const pjmedia_sdp_session *local_sdp;
2064 const pjmedia_sdp_session *remote_sdp;
2065 pjmedia_port *media_port;
2066 pj_str_t port_name;
2067 char tmp[PJSIP_MAX_URL_SIZE];
2068
2069 PJSUA_LOCK();
2070
2071 call = inv->dlg->mod_data[pjsua_var.mod.id];
2072
2073 if (status != PJ_SUCCESS) {
2074
2075 pjsua_perror(THIS_FILE, "SDP negotiation has failed", status);
2076
Benny Prijono1d9b9a42006-09-25 13:40:12 +00002077 /* Stop/destroy media, if any */
2078 call_destroy_media(call->index);
2079
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002080 /* Disconnect call if we're not in the middle of initializing an
2081 * UAS dialog and if this is not a re-INVITE
2082 */
2083 if (inv->state != PJSIP_INV_STATE_NULL &&
2084 inv->state != PJSIP_INV_STATE_CONFIRMED)
2085 {
2086 //call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE);
2087 }
2088
2089 PJSUA_UNLOCK();
2090 return;
2091 }
2092
2093 /* Destroy existing media session, if any. */
2094
2095 if (call) {
2096 prev_media_st = call->media_st;
2097 call_destroy_media(call->index);
2098 }
2099
2100 /* Get local and remote SDP */
2101
2102 status = pjmedia_sdp_neg_get_active_local(call->inv->neg, &local_sdp);
2103 if (status != PJ_SUCCESS) {
2104 pjsua_perror(THIS_FILE,
2105 "Unable to retrieve currently active local SDP",
2106 status);
2107 //call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE);
2108 PJSUA_UNLOCK();
2109 return;
2110 }
2111
2112
2113 status = pjmedia_sdp_neg_get_active_remote(call->inv->neg, &remote_sdp);
2114 if (status != PJ_SUCCESS) {
2115 pjsua_perror(THIS_FILE,
2116 "Unable to retrieve currently active remote SDP",
2117 status);
2118 //call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE);
2119 PJSUA_UNLOCK();
2120 return;
2121 }
2122
2123 /* Create media session info based on SDP parameters.
2124 * We only support one stream per session at the moment
2125 */
2126 status = pjmedia_session_info_from_sdp( call->inv->dlg->pool,
2127 pjsua_var.med_endpt,
2128 1,&sess_info,
2129 local_sdp, remote_sdp);
2130 if (status != PJ_SUCCESS) {
2131 pjsua_perror(THIS_FILE, "Unable to create media session",
2132 status);
Benny Prijonoa38ada02006-07-02 14:22:35 +00002133 call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002134 PJSUA_UNLOCK();
2135 return;
2136 }
2137
2138 /* Check if media is put on-hold */
2139 if (sess_info.stream_cnt == 0 ||
2140 sess_info.stream_info[0].dir == PJMEDIA_DIR_NONE)
2141 {
2142
2143 /* Determine who puts the call on-hold */
2144 if (prev_media_st == PJSUA_CALL_MEDIA_ACTIVE) {
2145 if (pjmedia_sdp_neg_was_answer_remote(call->inv->neg)) {
2146 /* It was local who offer hold */
2147 call->media_st = PJSUA_CALL_MEDIA_LOCAL_HOLD;
2148 } else {
2149 call->media_st = PJSUA_CALL_MEDIA_REMOTE_HOLD;
2150 }
2151 }
2152
2153 call->media_dir = PJMEDIA_DIR_NONE;
2154
2155 } else {
2156
2157 /* Override ptime, if this option is specified. */
Benny Prijono0a12f002006-07-26 17:05:39 +00002158 if (pjsua_var.media_cfg.ptime != 0) {
Benny Prijono00cae612006-07-31 15:19:36 +00002159 sess_info.stream_info[0].param->setting.frm_per_pkt = (pj_uint8_t)
2160 (pjsua_var.media_cfg.ptime / sess_info.stream_info[0].param->info.frm_ptime);
Benny Prijono0a12f002006-07-26 17:05:39 +00002161 if (sess_info.stream_info[0].param->setting.frm_per_pkt == 0)
2162 sess_info.stream_info[0].param->setting.frm_per_pkt = 1;
2163 }
2164
2165 /* Disable VAD, if this option is specified. */
2166 if (pjsua_var.media_cfg.no_vad) {
2167 sess_info.stream_info[0].param->setting.vad = 0;
2168 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002169
2170
2171 /* Optionally, application may modify other stream settings here
2172 * (such as jitter buffer parameters, codec ptime, etc.)
2173 */
Benny Prijono1d0ca0c2006-11-21 09:06:47 +00002174 sess_info.stream_info[0].jb_init = pjsua_var.media_cfg.jb_init;
2175 sess_info.stream_info[0].jb_min_pre = pjsua_var.media_cfg.jb_min_pre;
2176 sess_info.stream_info[0].jb_max_pre = pjsua_var.media_cfg.jb_max_pre;
2177 sess_info.stream_info[0].jb_max = pjsua_var.media_cfg.jb_max;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002178
2179 /* Create session based on session info. */
2180 status = pjmedia_session_create( pjsua_var.med_endpt, &sess_info,
2181 &call->med_tp,
2182 call, &call->session );
2183 if (status != PJ_SUCCESS) {
2184 pjsua_perror(THIS_FILE, "Unable to create media session",
2185 status);
2186 //call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE);
2187 PJSUA_UNLOCK();
2188 return;
2189 }
2190
2191
2192 /* Get the port interface of the first stream in the session.
2193 * We need the port interface to add to the conference bridge.
2194 */
2195 pjmedia_session_get_port(call->session, 0, &media_port);
2196
2197
2198 /*
2199 * Add the call to conference bridge.
2200 */
2201 port_name.ptr = tmp;
2202 port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
2203 call->inv->dlg->remote.info->uri,
2204 tmp, sizeof(tmp));
2205 if (port_name.slen < 1) {
2206 port_name = pj_str("call");
2207 }
2208 status = pjmedia_conf_add_port( pjsua_var.mconf, call->inv->pool,
2209 media_port,
2210 &port_name,
2211 (unsigned*)&call->conf_slot);
2212 if (status != PJ_SUCCESS) {
2213 pjsua_perror(THIS_FILE, "Unable to create conference slot",
2214 status);
2215 call_destroy_media(call->index);
2216 //call_disconnect(inv, PJSIP_SC_INTERNAL_SERVER_ERROR);
2217 PJSUA_UNLOCK();
2218 return;
2219 }
2220
2221 /* Call's media state is active */
2222 call->media_st = PJSUA_CALL_MEDIA_ACTIVE;
2223 call->media_dir = sess_info.stream_info[0].dir;
2224 }
2225
2226 /* Print info. */
2227 {
2228 char info[80];
2229 int info_len = 0;
2230 unsigned i;
2231
2232 for (i=0; i<sess_info.stream_cnt; ++i) {
2233 int len;
2234 const char *dir;
2235 pjmedia_stream_info *strm_info = &sess_info.stream_info[i];
2236
2237 switch (strm_info->dir) {
2238 case PJMEDIA_DIR_NONE:
2239 dir = "inactive";
2240 break;
2241 case PJMEDIA_DIR_ENCODING:
2242 dir = "sendonly";
2243 break;
2244 case PJMEDIA_DIR_DECODING:
2245 dir = "recvonly";
2246 break;
2247 case PJMEDIA_DIR_ENCODING_DECODING:
2248 dir = "sendrecv";
2249 break;
2250 default:
2251 dir = "unknown";
2252 break;
2253 }
2254 len = pj_ansi_sprintf( info+info_len,
2255 ", stream #%d: %.*s (%s)", i,
2256 (int)strm_info->fmt.encoding_name.slen,
2257 strm_info->fmt.encoding_name.ptr,
2258 dir);
2259 if (len > 0)
2260 info_len += len;
2261 }
2262 PJ_LOG(4,(THIS_FILE,"Media updates%s", info));
2263 }
2264
2265 /* Call application callback, if any */
2266 if (pjsua_var.ua_cfg.cb.on_call_media_state)
2267 pjsua_var.ua_cfg.cb.on_call_media_state(call->index);
2268
2269
2270 PJSUA_UNLOCK();
2271}
2272
2273
2274/*
2275 * Create inactive SDP for call hold.
2276 */
2277static pj_status_t create_inactive_sdp(pjsua_call *call,
2278 pjmedia_sdp_session **p_answer)
2279{
2280 pj_status_t status;
2281 pjmedia_sdp_conn *conn;
2282 pjmedia_sdp_attr *attr;
2283 pjmedia_sdp_session *sdp;
2284
2285 /* Create new offer */
2286 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pjsua_var.pool, 1,
2287 &call->skinfo, &sdp);
2288 if (status != PJ_SUCCESS) {
2289 pjsua_perror(THIS_FILE, "Unable to create local SDP", status);
2290 return status;
2291 }
2292
2293 /* Get SDP media connection line */
2294 conn = sdp->media[0]->conn;
2295 if (!conn)
2296 conn = sdp->conn;
2297
2298 /* Modify address */
2299 conn->addr = pj_str("0.0.0.0");
2300
2301 /* Remove existing directions attributes */
2302 pjmedia_sdp_media_remove_all_attr(sdp->media[0], "sendrecv");
2303 pjmedia_sdp_media_remove_all_attr(sdp->media[0], "sendonly");
2304 pjmedia_sdp_media_remove_all_attr(sdp->media[0], "recvonly");
2305 pjmedia_sdp_media_remove_all_attr(sdp->media[0], "inactive");
2306
2307 /* Add inactive attribute */
2308 attr = pjmedia_sdp_attr_create(pjsua_var.pool, "inactive", NULL);
2309 pjmedia_sdp_media_add_attr(sdp->media[0], attr);
2310
2311 *p_answer = sdp;
2312
2313 return status;
2314}
2315
2316
2317/*
2318 * Called when session received new offer.
2319 */
2320static void pjsua_call_on_rx_offer(pjsip_inv_session *inv,
2321 const pjmedia_sdp_session *offer)
2322{
2323 const char *remote_state;
2324 pjsua_call *call;
2325 pjmedia_sdp_conn *conn;
2326 pjmedia_sdp_session *answer;
2327 pj_bool_t is_remote_active;
2328 pj_status_t status;
2329
2330 PJSUA_LOCK();
2331
2332 call = inv->dlg->mod_data[pjsua_var.mod.id];
2333
2334 /*
2335 * See if remote is offering active media (i.e. not on-hold)
2336 */
2337 is_remote_active = PJ_TRUE;
2338
2339 conn = offer->media[0]->conn;
2340 if (!conn)
2341 conn = offer->conn;
2342
2343 if (pj_strcmp2(&conn->addr, "0.0.0.0")==0 ||
2344 pj_strcmp2(&conn->addr, "0")==0)
2345 {
2346 is_remote_active = PJ_FALSE;
2347
2348 }
2349 else if (pjmedia_sdp_media_find_attr2(offer->media[0], "inactive", NULL))
2350 {
2351 is_remote_active = PJ_FALSE;
2352 }
2353
2354 remote_state = (is_remote_active ? "active" : "inactive");
2355
2356 /* Supply candidate answer */
2357 if (call->media_st == PJSUA_CALL_MEDIA_LOCAL_HOLD || !is_remote_active) {
2358 PJ_LOG(4,(THIS_FILE,
2359 "Call %d: RX new media offer, creating inactive SDP "
2360 "(media in offer is %s)", call->index, remote_state));
2361 status = create_inactive_sdp( call, &answer );
2362 } else {
2363 PJ_LOG(4,(THIS_FILE, "Call %d: received updated media offer",
2364 call->index));
2365 status = pjmedia_endpt_create_sdp( pjsua_var.med_endpt,
2366 call->inv->pool, 1,
2367 &call->skinfo, &answer);
2368 }
2369
2370 if (status != PJ_SUCCESS) {
2371 pjsua_perror(THIS_FILE, "Unable to create local SDP", status);
2372 PJSUA_UNLOCK();
2373 return;
2374 }
2375
2376 status = pjsip_inv_set_sdp_answer(call->inv, answer);
2377 if (status != PJ_SUCCESS) {
2378 pjsua_perror(THIS_FILE, "Unable to set answer", status);
2379 PJSUA_UNLOCK();
2380 return;
2381 }
2382
2383 PJSUA_UNLOCK();
Benny Prijono84126ab2006-02-09 09:30:09 +00002384}
2385
2386
2387/*
Benny Prijono26ff9062006-02-21 23:47:00 +00002388 * Callback called by event framework when the xfer subscription state
2389 * has changed.
2390 */
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002391static void xfer_client_on_evsub_state( pjsip_evsub *sub, pjsip_event *event)
2392{
2393
2394 PJ_UNUSED_ARG(event);
2395
2396 /*
2397 * When subscription is accepted (got 200/OK to REFER), check if
2398 * subscription suppressed.
2399 */
2400 if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACCEPTED) {
2401
2402 pjsip_rx_data *rdata;
2403 pjsip_generic_string_hdr *refer_sub;
2404 const pj_str_t REFER_SUB = { "Refer-Sub", 9 };
2405 pjsua_call *call;
2406
2407 call = pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
2408
2409 /* Must be receipt of response message */
2410 pj_assert(event->type == PJSIP_EVENT_TSX_STATE &&
2411 event->body.tsx_state.type == PJSIP_EVENT_RX_MSG);
2412 rdata = event->body.tsx_state.src.rdata;
2413
2414 /* Find Refer-Sub header */
2415 refer_sub = (pjsip_generic_string_hdr*)
2416 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg,
2417 &REFER_SUB, NULL);
2418
2419 /* Check if subscription is suppressed */
2420 if (refer_sub && pj_stricmp2(&refer_sub->hvalue, "false")==0) {
2421 /* Since no subscription is desired, assume that call has been
2422 * transfered successfully.
2423 */
2424 if (call && pjsua_var.ua_cfg.cb.on_call_transfer_status) {
2425 const pj_str_t ACCEPTED = { "Accepted", 8 };
2426 pj_bool_t cont = PJ_FALSE;
2427 (*pjsua_var.ua_cfg.cb.on_call_transfer_status)(call->index,
2428 200,
2429 &ACCEPTED,
2430 PJ_TRUE,
2431 &cont);
2432 }
2433
2434 /* Yes, subscription is suppressed.
2435 * Terminate our subscription now.
2436 */
2437 PJ_LOG(4,(THIS_FILE, "Xfer subscription suppressed, terminating "
2438 "event subcription..."));
2439 pjsip_evsub_terminate(sub, PJ_TRUE);
2440
2441 } else {
2442 /* Notify application about call transfer progress.
2443 * Initially notify with 100/Accepted status.
2444 */
2445 if (call && pjsua_var.ua_cfg.cb.on_call_transfer_status) {
2446 const pj_str_t ACCEPTED = { "Accepted", 8 };
2447 pj_bool_t cont = PJ_FALSE;
2448 (*pjsua_var.ua_cfg.cb.on_call_transfer_status)(call->index,
2449 100,
2450 &ACCEPTED,
2451 PJ_FALSE,
2452 &cont);
2453 }
2454 }
2455 }
2456 /*
2457 * On incoming NOTIFY, notify application about call transfer progress.
2458 */
2459 else if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACTIVE ||
2460 pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED)
2461 {
2462 pjsua_call *call;
2463 pjsip_msg *msg;
2464 pjsip_msg_body *body;
2465 pjsip_status_line status_line;
2466 pj_bool_t is_last;
2467 pj_bool_t cont;
2468 pj_status_t status;
2469
2470 call = pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
2471
2472 /* When subscription is terminated, clear the xfer_sub member of
2473 * the inv_data.
2474 */
2475 if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
2476 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
2477 PJ_LOG(4,(THIS_FILE, "Xfer client subscription terminated"));
2478
2479 }
2480
2481 if (!call || !event || !pjsua_var.ua_cfg.cb.on_call_transfer_status) {
2482 /* Application is not interested with call progress status */
2483 return;
2484 }
2485
2486 /* This better be a NOTIFY request */
2487 if (event->type == PJSIP_EVENT_TSX_STATE &&
2488 event->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
2489 {
2490 pjsip_rx_data *rdata;
2491
2492 rdata = event->body.tsx_state.src.rdata;
2493
2494 /* Check if there's body */
2495 msg = rdata->msg_info.msg;
2496 body = msg->body;
2497 if (!body) {
2498 PJ_LOG(4,(THIS_FILE,
2499 "Warning: received NOTIFY without message body"));
2500 return;
2501 }
2502
2503 /* Check for appropriate content */
2504 if (pj_stricmp2(&body->content_type.type, "message") != 0 ||
2505 pj_stricmp2(&body->content_type.subtype, "sipfrag") != 0)
2506 {
2507 PJ_LOG(4,(THIS_FILE,
2508 "Warning: received NOTIFY with non message/sipfrag "
2509 "content"));
2510 return;
2511 }
2512
2513 /* Try to parse the content */
2514 status = pjsip_parse_status_line(body->data, body->len,
2515 &status_line);
2516 if (status != PJ_SUCCESS) {
2517 PJ_LOG(4,(THIS_FILE,
2518 "Warning: received NOTIFY with invalid "
2519 "message/sipfrag content"));
2520 return;
2521 }
2522
2523 } else {
2524 status_line.code = 500;
2525 status_line.reason = *pjsip_get_status_text(500);
2526 }
2527
2528 /* Notify application */
2529 is_last = (pjsip_evsub_get_state(sub)==PJSIP_EVSUB_STATE_TERMINATED);
2530 cont = !is_last;
2531 (*pjsua_var.ua_cfg.cb.on_call_transfer_status)(call->index,
2532 status_line.code,
2533 &status_line.reason,
2534 is_last, &cont);
2535
2536 if (!cont) {
2537 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
2538 }
2539 }
2540}
2541
2542
2543/*
2544 * Callback called by event framework when the xfer subscription state
2545 * has changed.
2546 */
2547static void xfer_server_on_evsub_state( pjsip_evsub *sub, pjsip_event *event)
Benny Prijono26ff9062006-02-21 23:47:00 +00002548{
2549
2550 PJ_UNUSED_ARG(event);
2551
2552 /*
Benny Prijonod524e822006-09-22 12:48:18 +00002553 * When subscription is terminated, clear the xfer_sub member of
2554 * the inv_data.
Benny Prijono26ff9062006-02-21 23:47:00 +00002555 */
2556 if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
Benny Prijonoa91a0032006-02-26 21:23:45 +00002557 pjsua_call *call;
Benny Prijono26ff9062006-02-21 23:47:00 +00002558
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002559 call = pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
Benny Prijonoa91a0032006-02-26 21:23:45 +00002560 if (!call)
Benny Prijono26ff9062006-02-21 23:47:00 +00002561 return;
2562
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002563 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
Benny Prijonoa91a0032006-02-26 21:23:45 +00002564 call->xfer_sub = NULL;
Benny Prijono26ff9062006-02-21 23:47:00 +00002565
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002566 PJ_LOG(4,(THIS_FILE, "Xfer server subscription terminated"));
Benny Prijono26ff9062006-02-21 23:47:00 +00002567 }
2568}
2569
2570
2571/*
2572 * Follow transfer (REFER) request.
2573 */
2574static void on_call_transfered( pjsip_inv_session *inv,
2575 pjsip_rx_data *rdata )
2576{
2577 pj_status_t status;
2578 pjsip_tx_data *tdata;
Benny Prijonoa91a0032006-02-26 21:23:45 +00002579 pjsua_call *existing_call;
2580 int new_call;
Benny Prijono26ff9062006-02-21 23:47:00 +00002581 const pj_str_t str_refer_to = { "Refer-To", 8};
Benny Prijonoc8141a82006-08-20 09:12:19 +00002582 const pj_str_t str_refer_sub = { "Refer-Sub", 9 };
Benny Prijono053f5222006-11-11 16:16:04 +00002583 const pj_str_t str_ref_by = { "Referred-By", 11 };
Benny Prijono26ff9062006-02-21 23:47:00 +00002584 pjsip_generic_string_hdr *refer_to;
Benny Prijonoc8141a82006-08-20 09:12:19 +00002585 pjsip_generic_string_hdr *refer_sub;
Benny Prijono053f5222006-11-11 16:16:04 +00002586 pjsip_hdr *ref_by_hdr;
Benny Prijonoc8141a82006-08-20 09:12:19 +00002587 pj_bool_t no_refer_sub = PJ_FALSE;
Benny Prijono26ff9062006-02-21 23:47:00 +00002588 char *uri;
Benny Prijono053f5222006-11-11 16:16:04 +00002589 pjsua_msg_data msg_data;
Benny Prijono9fc735d2006-05-28 14:58:12 +00002590 pj_str_t tmp;
Benny Prijono9fc735d2006-05-28 14:58:12 +00002591 pjsip_status_code code;
Benny Prijono26ff9062006-02-21 23:47:00 +00002592 pjsip_evsub *sub;
2593
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002594 existing_call = inv->dlg->mod_data[pjsua_var.mod.id];
Benny Prijonoa91a0032006-02-26 21:23:45 +00002595
Benny Prijono26ff9062006-02-21 23:47:00 +00002596 /* Find the Refer-To header */
2597 refer_to = (pjsip_generic_string_hdr*)
2598 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_to, NULL);
2599
2600 if (refer_to == NULL) {
2601 /* Invalid Request.
2602 * No Refer-To header!
2603 */
2604 PJ_LOG(4,(THIS_FILE, "Received REFER without Refer-To header!"));
Benny Prijonob0808372006-03-02 21:18:58 +00002605 pjsip_dlg_respond( inv->dlg, rdata, 400, NULL, NULL, NULL);
Benny Prijono26ff9062006-02-21 23:47:00 +00002606 return;
2607 }
2608
Benny Prijonoc8141a82006-08-20 09:12:19 +00002609 /* Find optional Refer-Sub header */
2610 refer_sub = (pjsip_generic_string_hdr*)
2611 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_sub, NULL);
2612
2613 if (refer_sub) {
2614 if (!pj_strnicmp2(&refer_sub->hvalue, "true", 4)==0)
2615 no_refer_sub = PJ_TRUE;
2616 }
2617
Benny Prijono053f5222006-11-11 16:16:04 +00002618 /* Find optional Referred-By header (to be copied onto outgoing INVITE
2619 * request.
2620 */
2621 ref_by_hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_ref_by,
2622 NULL);
Benny Prijonoc8141a82006-08-20 09:12:19 +00002623
Benny Prijono9fc735d2006-05-28 14:58:12 +00002624 /* Notify callback */
2625 code = PJSIP_SC_OK;
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002626 if (pjsua_var.ua_cfg.cb.on_call_transfer_request)
2627 (*pjsua_var.ua_cfg.cb.on_call_transfer_request)(existing_call->index,
2628 &refer_to->hvalue,
2629 &code);
Benny Prijono9fc735d2006-05-28 14:58:12 +00002630
2631 if (code < 200)
2632 code = 200;
2633 if (code >= 300) {
2634 /* Application rejects call transfer request */
2635 pjsip_dlg_respond( inv->dlg, rdata, code, NULL, NULL, NULL);
2636 return;
2637 }
2638
Benny Prijono26ff9062006-02-21 23:47:00 +00002639 PJ_LOG(3,(THIS_FILE, "Call to %.*s is being transfered to %.*s",
2640 (int)inv->dlg->remote.info_str.slen,
2641 inv->dlg->remote.info_str.ptr,
2642 (int)refer_to->hvalue.slen,
2643 refer_to->hvalue.ptr));
2644
Benny Prijonoc8141a82006-08-20 09:12:19 +00002645 if (no_refer_sub) {
2646 /*
2647 * Always answer with 200.
2648 */
2649 pjsip_tx_data *tdata;
2650 const pj_str_t str_false = { "false", 5};
2651 pjsip_hdr *hdr;
Benny Prijono26ff9062006-02-21 23:47:00 +00002652
Benny Prijonoc8141a82006-08-20 09:12:19 +00002653 status = pjsip_dlg_create_response(inv->dlg, rdata, 200, NULL, &tdata);
2654 if (status != PJ_SUCCESS) {
2655 pjsua_perror(THIS_FILE, "Unable to create 200 response to REFER",
2656 status);
2657 return;
2658 }
Benny Prijono26ff9062006-02-21 23:47:00 +00002659
Benny Prijonoc8141a82006-08-20 09:12:19 +00002660 /* Add Refer-Sub header */
2661 hdr = (pjsip_hdr*)
2662 pjsip_generic_string_hdr_create(tdata->pool, &str_refer_sub,
2663 &str_false);
2664 pjsip_msg_add_hdr(tdata->msg, hdr);
Benny Prijono26ff9062006-02-21 23:47:00 +00002665
Benny Prijono26ff9062006-02-21 23:47:00 +00002666
Benny Prijonoc8141a82006-08-20 09:12:19 +00002667 /* Send answer */
2668 status = pjsip_dlg_send_response(inv->dlg, pjsip_rdata_get_tsx(rdata),
2669 tdata);
2670 if (status != PJ_SUCCESS) {
2671 pjsua_perror(THIS_FILE, "Unable to create 200 response to REFER",
2672 status);
2673 return;
2674 }
2675
2676 /* Don't have subscription */
2677 sub = NULL;
2678
2679 } else {
2680 struct pjsip_evsub_user xfer_cb;
2681 pjsip_hdr hdr_list;
2682
2683 /* Init callback */
2684 pj_bzero(&xfer_cb, sizeof(xfer_cb));
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002685 xfer_cb.on_evsub_state = &xfer_server_on_evsub_state;
Benny Prijonoc8141a82006-08-20 09:12:19 +00002686
2687 /* Init additional header list to be sent with REFER response */
2688 pj_list_init(&hdr_list);
2689
2690 /* Create transferee event subscription */
2691 status = pjsip_xfer_create_uas( inv->dlg, &xfer_cb, rdata, &sub);
2692 if (status != PJ_SUCCESS) {
2693 pjsua_perror(THIS_FILE, "Unable to create xfer uas", status);
2694 pjsip_dlg_respond( inv->dlg, rdata, 500, NULL, NULL, NULL);
2695 return;
2696 }
2697
2698 /* If there's Refer-Sub header and the value is "true", send back
2699 * Refer-Sub in the response with value "true" too.
2700 */
2701 if (refer_sub) {
2702 const pj_str_t str_true = { "true", 4 };
2703 pjsip_hdr *hdr;
2704
2705 hdr = (pjsip_hdr*)
2706 pjsip_generic_string_hdr_create(inv->dlg->pool,
2707 &str_refer_sub,
2708 &str_true);
2709 pj_list_push_back(&hdr_list, hdr);
2710
2711 }
2712
2713 /* Accept the REFER request, send 200 (OK). */
2714 pjsip_xfer_accept(sub, rdata, code, &hdr_list);
2715
2716 /* Create initial NOTIFY request */
2717 status = pjsip_xfer_notify( sub, PJSIP_EVSUB_STATE_ACTIVE,
2718 100, NULL, &tdata);
2719 if (status != PJ_SUCCESS) {
2720 pjsua_perror(THIS_FILE, "Unable to create NOTIFY to REFER",
2721 status);
2722 return;
2723 }
2724
2725 /* Send initial NOTIFY request */
2726 status = pjsip_xfer_send_request( sub, tdata);
2727 if (status != PJ_SUCCESS) {
2728 pjsua_perror(THIS_FILE, "Unable to send NOTIFY to REFER", status);
2729 return;
2730 }
Benny Prijono26ff9062006-02-21 23:47:00 +00002731 }
2732
2733 /* We're cheating here.
2734 * We need to get a null terminated string from a pj_str_t.
2735 * So grab the pointer from the hvalue and NULL terminate it, knowing
2736 * that the NULL position will be occupied by a newline.
2737 */
2738 uri = refer_to->hvalue.ptr;
2739 uri[refer_to->hvalue.slen] = '\0';
2740
Benny Prijono053f5222006-11-11 16:16:04 +00002741 /* Init msg_data */
2742 pjsua_msg_data_init(&msg_data);
2743
2744 /* If Referred-By header is present in the REFER request, copy this
2745 * to the outgoing INVITE request.
2746 */
2747 if (ref_by_hdr != NULL) {
2748 pjsip_hdr *dup = pjsip_hdr_clone(rdata->tp_info.pool, ref_by_hdr);
2749 pj_list_push_back(&msg_data.hdr_list, dup);
2750 }
2751
Benny Prijono26ff9062006-02-21 23:47:00 +00002752 /* Now make the outgoing call. */
Benny Prijono9fc735d2006-05-28 14:58:12 +00002753 tmp = pj_str(uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002754 status = pjsua_call_make_call(existing_call->acc_id, &tmp, 0,
Benny Prijono053f5222006-11-11 16:16:04 +00002755 existing_call->user_data, &msg_data,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002756 &new_call);
Benny Prijono26ff9062006-02-21 23:47:00 +00002757 if (status != PJ_SUCCESS) {
2758
Benny Prijonoc8141a82006-08-20 09:12:19 +00002759 /* Notify xferer about the error (if we have subscription) */
2760 if (sub) {
2761 status = pjsip_xfer_notify(sub, PJSIP_EVSUB_STATE_TERMINATED,
2762 500, NULL, &tdata);
2763 if (status != PJ_SUCCESS) {
2764 pjsua_perror(THIS_FILE, "Unable to create NOTIFY to REFER",
2765 status);
2766 return;
2767 }
2768 status = pjsip_xfer_send_request(sub, tdata);
2769 if (status != PJ_SUCCESS) {
2770 pjsua_perror(THIS_FILE, "Unable to send NOTIFY to REFER",
2771 status);
2772 return;
2773 }
Benny Prijono26ff9062006-02-21 23:47:00 +00002774 }
2775 return;
2776 }
2777
Benny Prijonoc8141a82006-08-20 09:12:19 +00002778 if (sub) {
2779 /* Put the server subscription in inv_data.
2780 * Subsequent state changed in pjsua_inv_on_state_changed() will be
2781 * reported back to the server subscription.
2782 */
2783 pjsua_var.calls[new_call].xfer_sub = sub;
Benny Prijono26ff9062006-02-21 23:47:00 +00002784
Benny Prijonoc8141a82006-08-20 09:12:19 +00002785 /* Put the invite_data in the subscription. */
2786 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id,
2787 &pjsua_var.calls[new_call]);
2788 }
Benny Prijono26ff9062006-02-21 23:47:00 +00002789}
2790
2791
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002792
Benny Prijono26ff9062006-02-21 23:47:00 +00002793/*
2794 * This callback is called when transaction state has changed in INVITE
Benny Prijonob0808372006-03-02 21:18:58 +00002795 * session. We use this to trap:
2796 * - incoming REFER request.
2797 * - incoming MESSAGE request.
Benny Prijono26ff9062006-02-21 23:47:00 +00002798 */
Benny Prijonoa91a0032006-02-26 21:23:45 +00002799static void pjsua_call_on_tsx_state_changed(pjsip_inv_session *inv,
2800 pjsip_transaction *tsx,
2801 pjsip_event *e)
Benny Prijono26ff9062006-02-21 23:47:00 +00002802{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002803 pjsua_call *call = inv->dlg->mod_data[pjsua_var.mod.id];
2804
2805 PJSUA_LOCK();
Benny Prijonoa91a0032006-02-26 21:23:45 +00002806
Benny Prijono26ff9062006-02-21 23:47:00 +00002807 if (tsx->role==PJSIP_ROLE_UAS &&
2808 tsx->state==PJSIP_TSX_STATE_TRYING &&
2809 pjsip_method_cmp(&tsx->method, &pjsip_refer_method)==0)
2810 {
2811 /*
2812 * Incoming REFER request.
2813 */
Benny Prijonoa91a0032006-02-26 21:23:45 +00002814 on_call_transfered(call->inv, e->body.tsx_state.src.rdata);
Benny Prijonob0808372006-03-02 21:18:58 +00002815
Benny Prijono26ff9062006-02-21 23:47:00 +00002816 }
Benny Prijonob0808372006-03-02 21:18:58 +00002817 else if (tsx->role==PJSIP_ROLE_UAS &&
2818 tsx->state==PJSIP_TSX_STATE_TRYING &&
2819 pjsip_method_cmp(&tsx->method, &pjsip_message_method)==0)
2820 {
2821 /*
2822 * Incoming MESSAGE request!
2823 */
2824 pjsip_rx_data *rdata;
2825 pjsip_msg *msg;
2826 pjsip_accept_hdr *accept_hdr;
2827 pj_status_t status;
2828
2829 rdata = e->body.tsx_state.src.rdata;
2830 msg = rdata->msg_info.msg;
2831
2832 /* Request MUST have message body, with Content-Type equal to
2833 * "text/plain".
2834 */
2835 if (pjsua_im_accept_pager(rdata, &accept_hdr) == PJ_FALSE) {
2836
2837 pjsip_hdr hdr_list;
2838
2839 pj_list_init(&hdr_list);
2840 pj_list_push_back(&hdr_list, accept_hdr);
2841
2842 pjsip_dlg_respond( inv->dlg, rdata, PJSIP_SC_NOT_ACCEPTABLE_HERE,
2843 NULL, &hdr_list, NULL );
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002844 PJSUA_UNLOCK();
Benny Prijonob0808372006-03-02 21:18:58 +00002845 return;
2846 }
2847
2848 /* Respond with 200 first, so that remote doesn't retransmit in case
2849 * the UI takes too long to process the message.
2850 */
2851 status = pjsip_dlg_respond( inv->dlg, rdata, 200, NULL, NULL, NULL);
2852
2853 /* Process MESSAGE request */
2854 pjsua_im_process_pager(call->index, &inv->dlg->remote.info_str,
2855 &inv->dlg->local.info_str, rdata);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002856
Benny Prijonob0808372006-03-02 21:18:58 +00002857 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002858 else if (tsx->role == PJSIP_ROLE_UAC &&
2859 pjsip_method_cmp(&tsx->method, &pjsip_message_method)==0)
Benny Prijono26ff9062006-02-21 23:47:00 +00002860 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002861 /* Handle outgoing pager status */
2862 if (tsx->status_code >= 200) {
2863 pjsua_im_data *im_data;
Benny Prijono26ff9062006-02-21 23:47:00 +00002864
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002865 im_data = tsx->mod_data[pjsua_var.mod.id];
2866 /* im_data can be NULL if this is typing indication */
Benny Prijono26ff9062006-02-21 23:47:00 +00002867
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002868 if (im_data && pjsua_var.ua_cfg.cb.on_pager_status) {
2869 pjsua_var.ua_cfg.cb.on_pager_status(im_data->call_id,
2870 &im_data->to,
2871 &im_data->body,
2872 im_data->user_data,
2873 tsx->status_code,
2874 &tsx->status_text);
Benny Prijono26ff9062006-02-21 23:47:00 +00002875 }
Benny Prijonofccab712006-02-22 22:23:22 +00002876 }
Benny Prijono834aee32006-02-19 01:38:06 +00002877 }
Benny Prijono834aee32006-02-19 01:38:06 +00002878
Benny Prijono26ff9062006-02-21 23:47:00 +00002879
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002880 PJSUA_UNLOCK();
Benny Prijono9fc735d2006-05-28 14:58:12 +00002881}