Debian Codegraph @

Xpjproject/pjsip/src/pjsua-lib/pjsua_call.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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 */
19 #include <pjsua-lib/pjsua.h>
20 #include <pjsua-lib/pjsua_internal.h>
21
22
23 #define THIS_FILE "pjsua_call.c"
24
25
26 /* Retry interval of sending re-INVITE for locking a codec when remote
27 * SDP answer contains multiple codec, in milliseconds.
28 */
29 #define LOCK_CODEC_RETRY_INTERVAL 200
30
31 /*
32 * Max UPDATE/re-INVITE retry to lock codec
33 */
34 #define LOCK_CODEC_MAX_RETRY 5
35
36 /* Determine whether we should restart ICE upon receiving a re-INVITE
37 * with no SDP.
38 */
39 #define RESTART_ICE_ON_REINVITE 1
40
41 /* Retry interval of trying to hangup a call. */
42 #define CALL_HANGUP_RETRY_INTERVAL 5000
43
44 /* Max number of hangup retries. */
45 #define CALL_HANGUP_MAX_RETRY 4
46
47 /*
48 * The INFO method.
49 */
50 const pjsip_method pjsip_info_method =
51 {
52 PJSIP_OTHER_METHOD,
53 { "INFO", 4 }
54 };
55
56 /* UPDATE method */
57 static const pjsip_method pjsip_update_method =
58 {
59 PJSIP_OTHER_METHOD,
60 { "UPDATE", 6 }
61 };
62
63 /* This callback receives notification from invite session when the
64 * session state has changed.
65 */
66 static void pjsua_call_on_state_changed(pjsip_inv_session *inv,
67 pjsip_event *e);
68
69 /* This callback is called by invite session framework when UAC session
70 * has forked.
71 */
72 static void pjsua_call_on_forked( pjsip_inv_session *inv,
73 pjsip_event *e);
74
75 /*
76 * Callback to be called when SDP offer/answer negotiation has just completed
77 * in the session. This function will start/update media if negotiation
78 * has succeeded.
79 */
80 static void pjsua_call_on_media_update(pjsip_inv_session *inv,
81 pj_status_t status);
82
83 /*
84 * Called when session received new offer.
85 */
86 static void pjsua_call_on_rx_offer(pjsip_inv_session *inv,
87 struct pjsip_inv_on_rx_offer_cb_param *param);
88
89 /*
90 * Called when receiving re-INVITE.
91 */
92 static pj_status_t pjsua_call_on_rx_reinvite(pjsip_inv_session *inv,
93 const pjmedia_sdp_session *offer,
94 pjsip_rx_data *rdata);
95
96 /*
97 * Called to generate new offer.
98 */
99 static void pjsua_call_on_create_offer(pjsip_inv_session *inv,
100 pjmedia_sdp_session **offer);
101
102 /*
103 * This callback is called when transaction state has changed in INVITE
104 * session. We use this to trap:
105 * - incoming REFER request.
106 * - incoming MESSAGE request.
107 */
108 static void pjsua_call_on_tsx_state_changed(pjsip_inv_session *inv,
109 pjsip_transaction *tsx,
110 pjsip_event *e);
111
112 /*
113 * Redirection handler.
114 */
115 static pjsip_redirect_op pjsua_call_on_redirected(pjsip_inv_session *inv,
116 const pjsip_uri *target,
117 const pjsip_event *e);
118
119
120 /* Create SDP for call hold. */
121 static pj_status_t create_sdp_of_call_hold(pjsua_call *call,
122 pjmedia_sdp_session **p_sdp);
123
124 /*
125 * Callback called by event framework when the xfer subscription state
126 * has changed.
127 */
128 static void xfer_client_on_evsub_state( pjsip_evsub *sub, pjsip_event *event);
129 static void xfer_server_on_evsub_state( pjsip_evsub *sub, pjsip_event *event);
130
131 /* Timer callback to send re-INVITE/UPDATE to lock codec or ICE update */
132 static void reinv_timer_cb(pj_timer_heap_t *th, pj_timer_entry *entry);
133
134 /* Timer callback to hangup the call */
135 static void hangup_timer_cb(pj_timer_heap_t *th, pj_timer_entry *entry);
136
137 /* Check and send reinvite for lock codec and ICE update */
138 static pj_status_t process_pending_reinvite(pjsua_call *call);
139
140 /* Timer callbacks for trickle ICE */
141 static void trickle_ice_send_sip_info(pj_timer_heap_t *th,
142 struct pj_timer_entry *te);
143 static void trickle_ice_retrans_18x(pj_timer_heap_t *th,
144 struct pj_timer_entry *te);
145
146 /* End call session */
147 static pj_status_t call_inv_end_session(pjsua_call *call,
148 unsigned code,
149 const pj_str_t *reason,
150 const pjsua_msg_data *msg_data);
151
152 /*
153 * Reset call descriptor.
154 */
155 static void reset_call(pjsua_call_id id)
156 {
157 pjsua_call *call = &pjsua_var.calls[id];
158 unsigned i;
159
160 if (call->incoming_data) {
161 pjsip_rx_data_free_cloned(call->incoming_data);
162 call->incoming_data = NULL;
163 }
164 pj_bzero(call, sizeof(*call));
165 call->index = id;
166 call->last_text.ptr = call->last_text_buf_;
167 call->cname.ptr = call->cname_buf;
168 call->cname.slen = sizeof(call->cname_buf);
169 for (i=0; i<PJ_ARRAY_SIZE(call->media); ++i) {
170 pjsua_call_media *call_med = &call->media[i];
171 call_med->ssrc = pj_rand();
172 call_med->strm.a.conf_slot = PJSUA_INVALID_ID;
173 call_med->strm.v.cap_win_id = PJSUA_INVALID_ID;
174 call_med->strm.v.rdr_win_id = PJSUA_INVALID_ID;
175 call_med->strm.v.strm_dec_slot = PJSUA_INVALID_ID;
176 call_med->strm.v.strm_enc_slot = PJSUA_INVALID_ID;
177 call_med->call = call;
178 call_med->idx = i;
179 call_med->tp_auto_del = PJ_TRUE;
180 }
181 pjsua_call_setting_default(&call->opt);
182 pj_timer_entry_init(&call->reinv_timer, PJ_FALSE,
183 (void*)(pj_size_t)id, &reinv_timer_cb);
184 pj_bzero(&call->trickle_ice, sizeof(call->trickle_ice));
185 pj_timer_entry_init(&call->trickle_ice.timer, 0, call,
186 &trickle_ice_send_sip_info);
187 }
188
189 /* Get DTMF method type name */
190 static const char* get_dtmf_method_name(int type)
191 {
192 switch (type) {
193 case PJSUA_DTMF_METHOD_RFC2833:
194 return "RFC2833";
195 case PJSUA_DTMF_METHOD_SIP_INFO:
196 return "SIP INFO";
197 }
198 return "(Unknown)";
199 }
200
201 /*
202 * Init call subsystem.
203 */
204 pj_status_t pjsua_call_subsys_init(const pjsua_config *cfg)
205 {
206 pjsip_inv_callback inv_cb;
207 unsigned i;
208 const pj_str_t str_norefersub = { "norefersub", 10 };
209 const pj_str_t str_trickle_ice = { "trickle-ice", 11 };
210 pj_status_t status;
211
212 /* Copy config */
213 pjsua_config_dup(pjsua_var.pool, &pjsua_var.ua_cfg, cfg);
214
215 /* Verify settings */
216 if (pjsua_var.ua_cfg.max_calls > PJSUA_MAX_CALLS)
217 pjsua_var.ua_cfg.max_calls = PJSUA_MAX_CALLS;
218
219 pjsua_var.calls = (pjsua_call *)pj_pool_zalloc(pjsua_var.pool,
220 sizeof(pjsua_call) *
221 pjsua_var.ua_cfg.max_calls);
222 if (!pjsua_var.calls)
223 return PJ_ENOMEM;
224
225 /* Init calls array. */
226 for (i = 0U; i < pjsua_var.ua_cfg.max_calls; ++i)
227 reset_call(i);
228
229 /* Check the route URI's and force loose route if required */
230 for (i=0; i<pjsua_var.ua_cfg.outbound_proxy_cnt; ++i) {
231 status = normalize_route_uri(pjsua_var.pool,
232 &pjsua_var.ua_cfg.outbound_proxy[i]);
233 if (status != PJ_SUCCESS)
234 return status;
235 }
236
237 /* Initialize invite session callback. */
238 pj_bzero(&inv_cb, sizeof(inv_cb));
239 inv_cb.on_state_changed = &pjsua_call_on_state_changed;
240 inv_cb.on_new_session = &pjsua_call_on_forked;
241 inv_cb.on_media_update = &pjsua_call_on_media_update;
242 inv_cb.on_rx_offer2 = &pjsua_call_on_rx_offer;
243 inv_cb.on_create_offer = &pjsua_call_on_create_offer;
244 inv_cb.on_tsx_state_changed = &pjsua_call_on_tsx_state_changed;
245 inv_cb.on_redirected = &pjsua_call_on_redirected;
246 if (pjsua_var.ua_cfg.cb.on_call_rx_reinvite) {
247 inv_cb.on_rx_reinvite = &pjsua_call_on_rx_reinvite;
248 }
249
250 /* Initialize invite session module: */
251 status = pjsip_inv_usage_init(pjsua_var.endpt, &inv_cb);
252 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
253
254 /* Add "norefersub" in Supported header */
255 if (pjsua_var.ua_cfg.no_refer_sub) {
256 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_SUPPORTED,
257 NULL, 1, &str_norefersub);
258 }
259
260 /* Add "INFO" in Allow header, for DTMF and video key frame request. */
261 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW,
262 NULL, 1, &pjsip_info_method.name);
263
264 /* Add "trickle-ice" in Supported header */
265 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_SUPPORTED,
266 NULL, 1, &str_trickle_ice);
267
268 return status;
269 }
270
271
272 /*
273 * Start call subsystem.
274 */
275 pj_status_t pjsua_call_subsys_start(void)
276 {
277 /* Nothing to do */
278 return PJ_SUCCESS;
279 }
280
281
282 /*
283 * Get maximum number of calls configured in pjsua.
284 */
285 PJ_DEF(unsigned) pjsua_call_get_max_count(void)
286 {
287 return pjsua_var.ua_cfg.max_calls;
288 }
289
290
291 /*
292 * Get number of currently active calls.
293 */
294 PJ_DEF(unsigned) pjsua_call_get_count(void)
295 {
296 return pjsua_var.call_cnt;
297 }
298
299
300 /*
301 * Enum calls.
302 */
303 PJ_DEF(pj_status_t) pjsua_enum_calls( pjsua_call_id ids[],
304 unsigned *count)
305 {
306 unsigned i, c;
307
308 PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
309
310 PJSUA_LOCK();
311
312 for (i=0, c=0; c<*count && i<pjsua_var.ua_cfg.max_calls; ++i) {
313 if (!pjsua_var.calls[i].inv)
314 continue;
315 ids[c] = i;
316 ++c;
317 }
318
319 *count = c;
320
321 PJSUA_UNLOCK();
322
323 return PJ_SUCCESS;
324 }
325
326
327 /* Allocate one call id */
328 static pjsua_call_id alloc_call_id(void)
329 {
330 pjsua_call_id cid;
331
332 #if 1
333 /* New algorithm: round-robin */
334 if (pjsua_var.next_call_id >= (int)pjsua_var.ua_cfg.max_calls ||
335 pjsua_var.next_call_id < 0)
336 {
337 pjsua_var.next_call_id = 0;
338 }
339
340 for (cid=pjsua_var.next_call_id;
341 cid<(int)pjsua_var.ua_cfg.max_calls;
342 ++cid)
343 {
344 if (pjsua_var.calls[cid].inv == NULL &&
345 pjsua_var.calls[cid].async_call.dlg == NULL)
346 {
347 ++pjsua_var.next_call_id;
348 return cid;
349 }
350 }
351
352 for (cid=0; cid < pjsua_var.next_call_id; ++cid) {
353 if (pjsua_var.calls[cid].inv == NULL &&
354 pjsua_var.calls[cid].async_call.dlg == NULL)
355 {
356 ++pjsua_var.next_call_id;
357 return cid;
358 }
359 }
360
361 #else
362 /* Old algorithm */
363 for (cid=0; cid<(int)pjsua_var.ua_cfg.max_calls; ++cid) {
364 if (pjsua_var.calls[cid].inv == NULL)
365 return cid;
366 }
367 #endif
368
369 return PJSUA_INVALID_ID;
370 }
371
372 /* Get signaling secure level.
373 * Return:
374 * 0: if signaling is not secure
375 * 1: if TLS transport is used for immediate hop
376 * 2: if end-to-end signaling is secure.
377 */
378 static int get_secure_level(pjsua_acc_id acc_id, const pj_str_t *dst_uri)
379 {
380 const pj_str_t tls = pj_str(";transport=tls");
381 const pj_str_t sips = pj_str("sips:");
382 pjsua_acc *acc = &pjsua_var.acc[acc_id];
383
384 if (pj_stristr(dst_uri, &sips))
385 return 2;
386
387 if (!pj_list_empty(&acc->route_set)) {
388 pjsip_route_hdr *r = acc->route_set.next;
389 pjsip_uri *uri = r->name_addr.uri;
390 pjsip_sip_uri *sip_uri;
391
392 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
393 if (pj_stricmp2(&sip_uri->transport_param, "tls")==0)
394 return 1;
395
396 } else {
397 if (pj_stristr(dst_uri, &tls))
398 return 1;
399 }
400
401 return 0;
402 }
403
404 /*
405 static int call_get_secure_level(pjsua_call *call)
406 {
407 if (call->inv->dlg->secure)
408 return 2;
409
410 if (!pj_list_empty(&call->inv->dlg->route_set)) {
411 pjsip_route_hdr *r = call->inv->dlg->route_set.next;
412 pjsip_uri *uri = r->name_addr.uri;
413 pjsip_sip_uri *sip_uri;
414
415 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
416 if (pj_stricmp2(&sip_uri->transport_param, "tls")==0)
417 return 1;
418
419 } else {
420 pjsip_sip_uri *sip_uri;
421
422 if (PJSIP_URI_SCHEME_IS_SIPS(call->inv->dlg->target))
423 return 2;
424 if (!PJSIP_URI_SCHEME_IS_SIP(call->inv->dlg->target))
425 return 0;
426
427 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(call->inv->dlg->target);
428 if (pj_stricmp2(&sip_uri->transport_param, "tls")==0)
429 return 1;
430 }
431
432 return 0;
433 }
434 */
435
436 /* Outgoing call callback when media transport creation is completed. */
437 static pj_status_t
438 on_make_call_med_tp_complete(pjsua_call_id call_id,
439 const pjsua_med_tp_state_info *info)
440 {
441 pjmedia_sdp_session *offer = NULL;
442 pjsip_inv_session *inv = NULL;
443 pjsua_call *call;
444 pjsua_acc *acc;
445 pjsip_dialog *dlg;
446 unsigned options = 0;
447 pjsip_tx_data *tdata;
448 pj_bool_t cb_called = PJ_FALSE;
449 pjsip_tpselector tp_sel;
450 pj_status_t status = (info? info->status: PJ_SUCCESS);
451
452 /* Get call, account, and dialog */
453 PJ_ASSERT_RETURN(call_id != PJSUA_INVALID_ID, PJ_EINVAL);
454 call = &pjsua_var.calls[call_id];
455 acc = &pjsua_var.acc[call->acc_id];
456 dlg = call->async_call.dlg;
457
458 PJSUA_LOCK();
459
460 /* Increment the dialog's lock otherwise when invite session creation
461 * fails the dialog will be destroyed prematurely.
462 */
463 pjsip_dlg_inc_lock(dlg);
464
465 /* Decrement dialog session. */
466 pjsip_dlg_dec_session(dlg, &pjsua_var.mod);
467
468 if (status != PJ_SUCCESS) {
469 pj_str_t err_str;
470 pj_ssize_t title_len;
471
472 call->last_code = PJSIP_SC_TEMPORARILY_UNAVAILABLE;
473 pj_strcpy2(&call->last_text, "Media init error: ");
474
475 title_len = call->last_text.slen;
476 err_str = pj_strerror(status, call->last_text_buf_ + title_len,
477 sizeof(call->last_text_buf_) - title_len);
478 call->last_text.slen += err_str.slen;
479
480 pjsua_perror(THIS_FILE, "Error initializing media channel", status);
481 goto on_error;
482 }
483
484 /* pjsua_media_channel_deinit() has been called or
485 * call has been hung up.
486 */
487 if (call->async_call.med_ch_deinit ||
488 call->async_call.call_var.out_call.hangup)
489 {
490 PJ_LOG(4,(THIS_FILE, "Call has been hung up or media channel has "
491 "been deinitialized"));
492 goto on_error;
493 }
494
495 /* Create offer */
496 if ((call->opt.flag & PJSUA_CALL_NO_SDP_OFFER) == 0) {
497 status = pjsua_media_channel_create_sdp(call->index, dlg->pool, NULL,
498 &offer, NULL);
499 if (status != PJ_SUCCESS) {
500 pjsua_perror(THIS_FILE, "Error initializing media channel", status);
501 goto on_error;
502 }
503 }
504
505 /* Create the INVITE session: */
506 options |= PJSIP_INV_SUPPORT_100REL;
507 if (acc->cfg.require_100rel == PJSUA_100REL_MANDATORY)
508 options |= PJSIP_INV_REQUIRE_100REL;
509 if (acc->cfg.use_timer != PJSUA_SIP_TIMER_INACTIVE) {
510 options |= PJSIP_INV_SUPPORT_TIMER;
511 if (acc->cfg.use_timer == PJSUA_SIP_TIMER_REQUIRED)
512 options |= PJSIP_INV_REQUIRE_TIMER;
513 else if (acc->cfg.use_timer == PJSUA_SIP_TIMER_ALWAYS)
514 options |= PJSIP_INV_ALWAYS_USE_TIMER;
515 }
516 if (acc->cfg.ice_cfg.enable_ice &&
517 acc->cfg.ice_cfg.ice_opt.trickle != PJ_ICE_SESS_TRICKLE_DISABLED)
518 {
519 options |= PJSIP_INV_SUPPORT_TRICKLE_ICE;
520 }
521
522 status = pjsip_inv_create_uac( dlg, offer, options, &inv);
523 if (status != PJ_SUCCESS) {
524 pjsua_perror(THIS_FILE, "Invite session creation failed", status);
525 goto on_error;
526 }
527
528 /* Init Session Timers */
529 status = pjsip_timer_init_session(inv, &acc->cfg.timer_setting);
530 if (status != PJ_SUCCESS) {
531 pjsua_perror(THIS_FILE, "Session Timer init failed", status);
532 goto on_error;
533 }
534
535 /* Create and associate our data in the session. */
536 call->inv = inv;
537
538 dlg->mod_data[pjsua_var.mod.id] = call;
539 inv->mod_data[pjsua_var.mod.id] = call;
540
541 /* Set dialog's transport based on acc's config. */
542 pjsua_init_tpselector(call->acc_id, &tp_sel);
543 pjsip_dlg_set_transport(dlg, &tp_sel);
544
545 /* Set dialog Route-Set: */
546 if (!pj_list_empty(&acc->route_set))
547 pjsip_dlg_set_route_set(dlg, &acc->route_set);
548
549
550 /* Set credentials: */
551 if (acc->cred_cnt) {
552 pjsip_auth_clt_set_credentials( &dlg->auth_sess,
553 acc->cred_cnt, acc->cred);
554 }
555
556 /* Set authentication preference */
557 pjsip_auth_clt_set_prefs(&dlg->auth_sess, &acc->cfg.auth_pref);
558
559 /* Create initial INVITE: */
560
561 status = pjsip_inv_invite(inv, &tdata);
562 if (status != PJ_SUCCESS) {
563 pjsua_perror(THIS_FILE, "Unable to create initial INVITE request",
564 status);
565 goto on_error;
566 }
567
568
569 /* Add additional headers etc */
570
571 pjsua_process_msg_data( tdata,
572 call->async_call.call_var.out_call.msg_data);
573
574 /* Must increment call counter now */
575 ++pjsua_var.call_cnt;
576
577 /* Send initial INVITE: */
578
579 status = pjsip_inv_send_msg(inv, tdata);
580 if (status != PJ_SUCCESS) {
581 cb_called = PJ_TRUE;
582
583 /* If call inv hasn't been cleared from on_call_state(DISCONNECTED),
584 * we clear it here.
585 */
586 if (call->inv) {
587 pjsip_inv_terminate(inv, PJSIP_SC_OK, PJ_FALSE);
588 --pjsua_var.call_cnt;
589 }
590
591 call->inv = inv = NULL;
592 goto on_error;
593 }
594
595 /* Done. */
596 call->med_ch_cb = NULL;
597
598 pjsip_dlg_dec_lock(dlg);
599 PJSUA_UNLOCK();
600
601 return PJ_SUCCESS;
602
603 on_error:
604 if (inv == NULL && call_id != -1 && !cb_called &&
605 !call->hanging_up &&
606 pjsua_var.ua_cfg.cb.on_call_state)
607 {
608 /* Use user event rather than NULL to avoid crash in
609 * unsuspecting app.
610 */
611 pjsip_event user_event;
612 PJSIP_EVENT_INIT_USER(user_event, 0, 0, 0, 0);
613
614 (*pjsua_var.ua_cfg.cb.on_call_state)(call_id, &user_event);
615 }
616
617 /* This may destroy the dialog */
618 pjsip_dlg_dec_lock(dlg);
619 call->async_call.dlg = NULL;
620
621 if (inv != NULL) {
622 pjsip_inv_terminate(inv, PJSIP_SC_OK, PJ_FALSE);
623 call->inv = NULL;
624 }
625
626 if (call_id != -1) {
627 pjsua_media_channel_deinit(call_id);
628 reset_call(call_id);
629 }
630
631 call->med_ch_cb = NULL;
632
633 pjsua_check_snd_dev_idle();
634
635 PJSUA_UNLOCK();
636 return status;
637 }
638
639
640 /*
641 * Cleanup call setting flag to avoid one time flags, such as
642 * PJSUA_CALL_UNHOLD, PJSUA_CALL_UPDATE_CONTACT, or
643 * PJSUA_CALL_NO_SDP_OFFER, to be sticky (ticket #1793).
644 */
645 void pjsua_call_cleanup_flag(pjsua_call_setting *opt)
646 {
647 opt->flag &= ~(PJSUA_CALL_UNHOLD | PJSUA_CALL_UPDATE_CONTACT |
648 PJSUA_CALL_NO_SDP_OFFER | PJSUA_CALL_REINIT_MEDIA |
649 PJSUA_CALL_UPDATE_VIA | PJSUA_CALL_SET_MEDIA_DIR);
650 }
651
652
653 /*
654 * Initialize call settings based on account ID.
655 */
656 PJ_DEF(void) pjsua_call_setting_default(pjsua_call_setting *opt)
657 {
658 unsigned i;
659
660 pj_assert(opt);
661
662 pj_bzero(opt, sizeof(*opt));
663 opt->flag = 0; //PJSUA_CALL_INCLUDE_DISABLED_MEDIA;
664 opt->aud_cnt = 1;
665 opt->txt_cnt = 1;
666
667 #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
668 opt->vid_cnt = 1;
669 opt->req_keyframe_method = PJSUA_VID_REQ_KEYFRAME_SIP_INFO |
670 PJSUA_VID_REQ_KEYFRAME_RTCP_PLI;
671 #endif
672
673 for (i = 0; i < PJMEDIA_MAX_SDP_MEDIA; i++) {
674 opt->media_dir[i] = PJMEDIA_DIR_ENCODING_DECODING;
675 }
676 }
677
678 /*
679 * Initialize pjsua_call_send_dtmf_param default values.
680 */
681 PJ_DEF(void) pjsua_call_send_dtmf_param_default(
682 pjsua_call_send_dtmf_param *param)
683 {
684 pj_bzero(param, sizeof(*param));
685 param->duration = PJSUA_CALL_SEND_DTMF_DURATION_DEFAULT;
686 }
687
688 /*
689 * Initialize pjsua_call_send_text_param default values.
690 */
691 PJ_DEF(void) pjsua_call_send_text_param_default(
692 pjsua_call_send_text_param *param)
693 {
694 pj_bzero(param, sizeof(*param));
695 param->med_idx = -1;
696 }
697
698
699 static pj_status_t apply_call_setting(pjsua_call *call,
700 const pjsua_call_setting *opt,
701 const pjmedia_sdp_session *rem_sdp)
702 {
703 pj_assert(call);
704
705 if (!opt) {
706 pjsua_call_cleanup_flag(&call->opt);
707 } else {
708 call->opt = *opt;
709 }
710
711 #if !PJMEDIA_HAS_VIDEO
712 pj_assert(call->opt.vid_cnt == 0);
713 #endif
714
715 if (call->opt.flag & PJSUA_CALL_REINIT_MEDIA) {
716 PJ_LOG(4, (THIS_FILE, "PJSUA_CALL_REINIT_MEDIA"));
717 pjsua_media_channel_deinit(call->index);
718 }
719
720 /* If call is established or media channel hasn't been initialized,
721 * reinit media channel.
722 */
723 if (call->inv &&
724 ((call->inv->state == PJSIP_INV_STATE_CONNECTING &&
725 call->med_cnt == 0) ||
726 (call->inv->state == PJSIP_INV_STATE_CONFIRMED) ||
727 (call->opt.flag & PJSUA_CALL_REINIT_MEDIA)))
728 {
729 pjsip_role_e role = rem_sdp? PJSIP_ROLE_UAS : PJSIP_ROLE_UAC;
730 pj_status_t status;
731
732 status = pjsua_media_channel_init(call->index, role,
733 call->secure_level,
734 call->inv->pool_prov,
735 rem_sdp, NULL,
736 PJ_FALSE, NULL);
737 if (status != PJ_SUCCESS) {
738 pjsua_perror(THIS_FILE, "Error re-initializing media channel",
739 status);
740 return status;
741 }
742 }
743
744 return PJ_SUCCESS;
745 }
746
747 static void dlg_set_via(pjsip_dialog *dlg, pjsua_acc *acc)
748 {
749 if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0) {
750 pjsip_dlg_set_via_sent_by(dlg, &acc->via_addr, acc->via_tp);
751 } else if (!pjsua_sip_acc_is_using_stun(acc->index) &&
752 !pjsua_sip_acc_is_using_upnp(acc->index))
753 {
754 /* Choose local interface to use in Via if acc is not using
755 * STUN nor UPnP. See https://github.com/pjsip/pjproject/issues/1804
756 */
757 pjsip_host_port via_addr;
758 const void *via_tp;
759
760 if (pjsua_acc_get_uac_addr(acc->index, dlg->pool, &acc->cfg.id,
761 &via_addr, NULL, NULL,
762 &via_tp) == PJ_SUCCESS)
763 {
764 pjsip_dlg_set_via_sent_by(dlg, &via_addr,
765 (pjsip_transport*)via_tp);
766 }
767 }
768 }
769
770
771 static pj_status_t dlg_set_target(pjsip_dialog *dlg, const pj_str_t *target)
772 {
773 pjsip_uri *target_uri;
774 pj_str_t tmp;
775 pj_status_t status;
776
777 /* Parse target & verify */
778 pj_strdup_with_null(dlg->pool, &tmp, target);
779 target_uri = pjsip_parse_uri(dlg->pool, tmp.ptr, tmp.slen, 0);
780 if (!target_uri) {
781 return PJSIP_EINVALIDURI;
782 }
783 if (!PJSIP_URI_SCHEME_IS_SIP(target_uri) &&
784 !PJSIP_URI_SCHEME_IS_SIPS(target_uri))
785 {
786 return PJSIP_EINVALIDSCHEME;
787 }
788
789 /* Add the new target */
790 status = pjsip_target_set_add_uri(&dlg->target_set, dlg->pool,
791 target_uri, 0);
792 if (status != PJ_SUCCESS)
793 return status;
794
795 /* Set it as current target */
796 status = pjsip_target_set_set_current(&dlg->target_set,
797 pjsip_target_set_get_next(&dlg->target_set));
798 if (status != PJ_SUCCESS)
799 return status;
800
801 /* Update dialog target URI */
802 dlg->target = target_uri;
803
804 return PJ_SUCCESS;
805 }
806
807
808 /* Get account contact for call and update dialog transport */
809 void call_update_contact(pjsua_call *call, pj_str_t **new_contact)
810 {
811 pjsip_tpselector tp_sel;
812 pjsua_acc *acc = &pjsua_var.acc[call->acc_id];
813
814 if (acc->cfg.force_contact.slen)
815 *new_contact = &acc->cfg.force_contact;
816 else if (acc->contact.slen)
817 *new_contact = &acc->contact;
818 else {
819 /* Non-registering account */
820 pjsip_dialog *dlg = call->inv->dlg;
821 pj_str_t tmp_contact;
822 pj_status_t status;
823
824 status = pjsua_acc_create_uac_contact(dlg->pool,
825 &tmp_contact,
826 acc->index,
827 &dlg->remote.info_str);
828 if (status == PJ_SUCCESS) {
829 *new_contact = PJ_POOL_ZALLOC_T(dlg->pool, pj_str_t);
830 **new_contact = tmp_contact;
831 } else {
832 PJ_PERROR(3,(THIS_FILE, status,
833 "Call %d: failed creating contact "
834 "for contact update", call->index));
835 }
836 }
837
838
839 /* When contact is changed, the account transport may have been
840 * changed too, so let's update the dialog's transport too.
841 */
842 pjsua_init_tpselector(call->acc_id, &tp_sel);
843 pjsip_dlg_set_transport(call->inv->dlg, &tp_sel);
844 }
845
846
847
848 /*
849 * Make outgoing call to the specified URI using the specified account.
850 */
851 PJ_DEF(pj_status_t) pjsua_call_make_call(pjsua_acc_id acc_id,
852 const pj_str_t *dest_uri,
853 const pjsua_call_setting *opt,
854 void *user_data,
855 const pjsua_msg_data *msg_data,
856 pjsua_call_id *p_call_id)
857 {
858 pj_pool_t *tmp_pool = NULL;
859 pjsip_dialog *dlg = NULL;
860 pjsua_acc *acc;
861 pjsua_call *call = NULL;
862 int call_id = -1;
863 pj_str_t contact;
864 pj_status_t status;
865
866 /* Check that account is valid */
867 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
868 PJ_EINVAL);
869
870 /* Check arguments */
871 PJ_ASSERT_RETURN(dest_uri, PJ_EINVAL);
872
873 PJ_LOG(4,(THIS_FILE, "Making call with acc #%d to %.*s", acc_id,
874 (int)dest_uri->slen, dest_uri->ptr));
875
876 pj_log_push_indent();
877
878 PJSUA_LOCK();
879
880 acc = &pjsua_var.acc[acc_id];
881 if (!acc->valid) {
882 pjsua_perror(THIS_FILE, "Unable to make call because account "
883 "is not valid", PJ_EINVALIDOP);
884 status = PJ_EINVALIDOP;
885 goto on_error;
886 }
887
888 /* Find free call slot. */
889 call_id = alloc_call_id();
890
891 if (call_id == PJSUA_INVALID_ID) {
892 pjsua_perror(THIS_FILE, "Error making call", PJ_ETOOMANY);
893 status = PJ_ETOOMANY;
894 goto on_error;
895 }
896
897 /* Clear call descriptor */
898 reset_call(call_id);
899
900 call = &pjsua_var.calls[call_id];
901
902 /* Associate session with account */
903 call->acc_id = acc_id;
904 call->call_hold_type = acc->cfg.call_hold_type;
905
906 /* Generate per-session RTCP CNAME, according to RFC 7022. */
907 pj_create_random_string(call->cname_buf, call->cname.slen);
908
909 /* Apply call setting */
910 status = apply_call_setting(call, opt, NULL);
911 if (status != PJ_SUCCESS) {
912 pjsua_perror(THIS_FILE, "Failed to apply call setting", status);
913 goto on_error;
914 } else if (!opt) {
915 opt = &call->opt;
916 }
917
918 /* Create sound port if none is instantiated, to check if sound device
919 * can be used. But only do this with the conference bridge, as with
920 * audio switchboard (i.e. APS-Direct), we can only open the sound
921 * device once the correct format has been known
922 */
923 if (!pjsua_var.is_mswitch && pjsua_var.snd_port==NULL &&
924 pjsua_var.null_snd==NULL && !pjsua_var.no_snd && call->opt.aud_cnt > 0)
925 {
926 status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
927 if (status != PJ_SUCCESS)
928 goto on_error;
929 }
930
931 /* Create temporary pool */
932 tmp_pool = pjsua_pool_create("tmpcall10", 512, 256);
933
934 /* Verify that destination URI is valid before calling
935 * pjsua_acc_create_uac_contact, or otherwise there
936 * a misleading "Invalid Contact URI" error will be printed
937 * when pjsua_acc_create_uac_contact() fails.
938 */
939 if (1) {
940 pjsip_uri *uri;
941 pj_str_t dup;
942
943 pj_strdup_with_null(tmp_pool, &dup, dest_uri);
944 uri = pjsip_parse_uri(tmp_pool, dup.ptr, dup.slen, 0);
945
946 if (uri == NULL) {
947 pjsua_perror(THIS_FILE, "Unable to make call",
948 PJSIP_EINVALIDREQURI);
949 status = PJSIP_EINVALIDREQURI;
950 goto on_error;
951 }
952
953 /* Verify contact URI if provided */
954 if (msg_data && msg_data->contact_uri.slen) {
955 pj_strdup_with_null(tmp_pool, &dup, &msg_data->contact_uri);
956 uri = pjsip_parse_uri(tmp_pool, dup.ptr, dup.slen, 0);
957 if (uri == NULL) {
958 pjsua_perror(THIS_FILE, "Invalid contact URI",
959 PJSIP_EINVALIDREQURI);
960 status = PJSIP_EINVALIDREQURI;
961 goto on_error;
962 }
963 }
964
965 /* Verify local URI if provided */
966 if (msg_data && msg_data->local_uri.slen) {
967 pj_strdup_with_null(tmp_pool, &dup, &msg_data->local_uri);
968 uri = pjsip_parse_uri(tmp_pool, dup.ptr, dup.slen, 0);
969 if (uri == NULL) {
970 pjsua_perror(THIS_FILE, "Invalid local URI",
971 PJSIP_EINVALIDREQURI);
972 status = PJSIP_EINVALIDREQURI;
973 goto on_error;
974 }
975 }
976 }
977
978 /* Mark call start time. */
979 pj_gettimeofday(&call->start_time);
980
981 /* Reset first response time */
982 call->res_time.sec = 0;
983
984 /* Create suitable Contact header unless a Contact header has been
985 * set in the account or message data.
986 */
987 if (msg_data && msg_data->contact_uri.slen) {
988 contact = msg_data->contact_uri;
989 } else if (acc->contact.slen) {
990 contact = acc->contact;
991 } else {
992 status = pjsua_acc_create_uac_contact(tmp_pool, &contact,
993 acc_id, dest_uri);
994 if (status != PJ_SUCCESS) {
995 pjsua_perror(THIS_FILE, "Unable to generate Contact header",
996 status);
997 goto on_error;
998 }
999 }
1000
1001 /* Create outgoing dialog: */
1002 status = pjsip_dlg_create_uac( pjsip_ua_instance(),
1003 (msg_data && msg_data->local_uri.slen?
1004 &msg_data->local_uri: &acc->cfg.id),
1005 &contact,
1006 dest_uri,
1007 (msg_data && msg_data->target_uri.slen?
1008 &msg_data->target_uri: dest_uri),
1009 &dlg);
1010 if (status != PJ_SUCCESS) {
1011 pjsua_perror(THIS_FILE, "Dialog creation failed", status);
1012 goto on_error;
1013 }
1014
1015 /*
1016 * Use pre defined Call-ID to be sent out with INVITE as opposed
1017 * to using a randomly generated Call-ID
1018 */
1019
1020 if( opt->custom_call_id.slen > 0 ){
1021 pj_strdup(dlg->pool, &dlg->call_id->id, &opt->custom_call_id);
1022 PJ_LOG(4,(THIS_FILE, "Set user defined "
1023 "Call-ID (%.*s)",
1024 (int)dlg->call_id->id.slen, dlg->call_id->id.ptr ));
1025 pj_bzero(&call->opt.custom_call_id,
1026 sizeof(call->opt.custom_call_id));
1027 }
1028
1029 /* Increment the dialog's lock otherwise when invite session creation
1030 * fails the dialog will be destroyed prematurely.
1031 */
1032 pjsip_dlg_inc_lock(dlg);
1033
1034 dlg_set_via(dlg, acc);
1035
1036 if (acc->cfg.use_shared_auth) {
1037 pjsip_dlg_set_auth_sess(dlg, &acc->shared_auth_sess);
1038 } else if (pjsua_var.ua_cfg.cb.on_auth_challenge) {
1039 pjsip_auth_clt_async_setting async_opt;
1040 pj_bzero(&async_opt, sizeof(async_opt));
1041 async_opt.cb = &pjsua_auth_on_challenge;
1042 async_opt.user_data = (void*)(pj_ssize_t)acc->index;
1043 pjsip_auth_clt_async_configure(&dlg->auth_sess, &async_opt);
1044 }
1045
1046 /* Calculate call's secure level */
1047 call->secure_level = get_secure_level(acc_id, dest_uri);
1048
1049 /* Attach user data */
1050 call->user_data = user_data;
1051
1052 /* Store variables required for the callback after the async
1053 * media transport creation is completed.
1054 */
1055 if (msg_data) {
1056 call->async_call.call_var.out_call.msg_data = pjsua_msg_data_clone(
1057 dlg->pool, msg_data);
1058 }
1059 call->async_call.dlg = dlg;
1060
1061 /* Temporarily increment dialog session. Without this, dialog will be
1062 * prematurely destroyed if dec_lock() is called on the dialog before
1063 * the invite session is created.
1064 */
1065 pjsip_dlg_inc_session(dlg, &pjsua_var.mod);
1066
1067 if ((call->opt.flag & PJSUA_CALL_NO_SDP_OFFER) == 0) {
1068 /* Init media channel */
1069 status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC,
1070 call->secure_level, dlg->pool,
1071 NULL, NULL, PJ_TRUE,
1072 &on_make_call_med_tp_complete);
1073 }
1074 if (status == PJ_SUCCESS) {
1075 status = on_make_call_med_tp_complete(call->index, NULL);
1076 if (status != PJ_SUCCESS)
1077 goto on_error;
1078 } else if (status != PJ_EPENDING) {
1079 pjsua_perror(THIS_FILE, "Error initializing media channel", status);
1080 pjsip_dlg_dec_session(dlg, &pjsua_var.mod);
1081 goto on_error;
1082 }
1083
1084 /* Done. */
1085
1086 if (p_call_id)
1087 *p_call_id = call_id;
1088
1089 pjsip_dlg_dec_lock(dlg);
1090 pj_pool_release(tmp_pool);
1091 PJSUA_UNLOCK();
1092
1093 pj_log_pop_indent();
1094
1095 return PJ_SUCCESS;
1096
1097
1098 on_error:
1099 if (dlg && call) {
1100 /* This may destroy the dialog */
1101 pjsip_dlg_dec_lock(dlg);
1102 call->async_call.dlg = NULL;
1103 }
1104
1105 if (call_id != -1) {
1106 pjsua_media_channel_deinit(call_id);
1107 reset_call(call_id);
1108 }
1109
1110 pjsua_check_snd_dev_idle();
1111
1112 if (tmp_pool)
1113 pj_pool_release(tmp_pool);
1114 PJSUA_UNLOCK();
1115
1116 pj_log_pop_indent();
1117 return status;
1118 }
1119
1120
1121 /* Get the NAT type information in remote's SDP */
1122 static void update_remote_nat_type(pjsua_call *call,
1123 const pjmedia_sdp_session *sdp)
1124 {
1125 const pjmedia_sdp_attr *xnat;
1126
1127 xnat = pjmedia_sdp_attr_find2(sdp->attr_count, sdp->attr, "X-nat", NULL);
1128 if (xnat && xnat->value.slen > 0) {
1129 int type = xnat->value.ptr[0] - '0';
1130 if (type < 0 || type > PJ_STUN_NAT_TYPE_PORT_RESTRICTED)
1131 type = PJ_STUN_NAT_TYPE_UNKNOWN;
1132 call->rem_nat_type = (pj_stun_nat_type) type;
1133 } else {
1134 call->rem_nat_type = PJ_STUN_NAT_TYPE_UNKNOWN;
1135 }
1136
1137 PJ_LOG(5,(THIS_FILE, "Call %d: remote NAT type is %d (%s)", call->index,
1138 call->rem_nat_type, pj_stun_get_nat_name(call->rem_nat_type)));
1139 }
1140
1141
1142 static pj_status_t process_incoming_call_replace(pjsua_call *call,
1143 pjsip_dialog *replaced_dlg)
1144 {
1145 pjsip_inv_session *replaced_inv;
1146 struct pjsua_call *replaced_call;
1147 pjsip_tx_data *tdata = NULL;
1148 pj_status_t status = PJ_SUCCESS;
1149
1150 /* Get the invite session in the dialog */
1151 replaced_inv = pjsip_dlg_get_inv_session(replaced_dlg);
1152
1153 /* Get the replaced call instance */
1154 replaced_call = (pjsua_call*) replaced_dlg->mod_data[pjsua_var.mod.id];
1155
1156 /* Notify application */
1157 if (!replaced_call->hanging_up && pjsua_var.ua_cfg.cb.on_call_replaced)
1158 pjsua_var.ua_cfg.cb.on_call_replaced(replaced_call->index,
1159 call->index);
1160
1161 if (replaced_call->inv->state <= PJSIP_INV_STATE_EARLY &&
1162 replaced_call->inv->role != PJSIP_ROLE_UAC)
1163 {
1164 if (replaced_call->last_code > 100 && replaced_call->last_code < 200)
1165 {
1166 pjsip_status_code code = replaced_call->last_code;
1167 pj_str_t *text = &replaced_call->last_text;
1168
1169 PJ_LOG(4,(THIS_FILE, "Answering replacement call %d with %d/%.*s",
1170 call->index, code, (int)text->slen, text->ptr));
1171
1172 /* Answer the new call with last response in the replaced call */
1173 status = pjsip_inv_answer(call->inv, code, text, NULL, &tdata);
1174 }
1175 } else {
1176 PJ_LOG(4,(THIS_FILE, "Answering replacement call %d with 200/OK",
1177 call->index));
1178
1179 /* Answer the new call with 200 response */
1180 status = pjsip_inv_answer(call->inv, 200, NULL, NULL, &tdata);
1181 }
1182
1183 if (status == PJ_SUCCESS && tdata)
1184 status = pjsip_inv_send_msg(call->inv, tdata);
1185
1186 if (status != PJ_SUCCESS)
1187 pjsua_perror(THIS_FILE, "Error answering session", status);
1188
1189 /* Note that inv may be invalid if 200/OK has caused error in
1190 * starting the media.
1191 */
1192
1193 PJ_LOG(4,(THIS_FILE, "Disconnecting replaced call %d",
1194 replaced_call->index));
1195
1196 /* Disconnect replaced invite session */
1197 status = pjsip_inv_end_session(replaced_inv, PJSIP_SC_GONE, NULL,
1198 &tdata);
1199 if (status == PJ_SUCCESS && tdata)
1200 status = pjsip_inv_send_msg(replaced_inv, tdata);
1201
1202 if (status != PJ_SUCCESS)
1203 pjsua_perror(THIS_FILE, "Error terminating session", status);
1204
1205 return status;
1206 }
1207
1208
1209 static void process_pending_call_answer(pjsua_call *call)
1210 {
1211 struct call_answer *answer, *next;
1212
1213 /* No initial answer yet, this function should be called again later */
1214 if (call->inv && !call->inv->last_answer)
1215 return;
1216
1217 answer = call->async_call.call_var.inc_call.answers.next;
1218 while (answer != &call->async_call.call_var.inc_call.answers) {
1219 next = answer->next;
1220 pjsua_call_answer2(call->index, answer->opt, answer->code,
1221 answer->reason, answer->msg_data);
1222
1223 /* Call might have been disconnected if application is answering
1224 * with 200/OK and the media failed to start.
1225 * See pjsua_call_answer() below.
1226 */
1227 if (!call->inv || !call->inv->pool_prov)
1228 break;
1229
1230 pj_list_erase(answer);
1231 answer = next;
1232 }
1233 }
1234
1235 static pj_status_t process_pending_call_hangup(pjsua_call *call)
1236 {
1237 pjsip_dialog *dlg = NULL;
1238 pj_status_t status;
1239
1240 PJ_LOG(4,(THIS_FILE, "Call %d processing pending hangup: code=%d..",
1241 call->index, call->last_code));
1242 pj_log_push_indent();
1243
1244 status = acquire_call("pending_hangup()", call->index, &call, &dlg);
1245 if (status != PJ_SUCCESS) {
1246 PJ_LOG(3, (THIS_FILE, "Call %d failed to process pending hangup",
1247 call->index));
1248 goto on_return;
1249 }
1250
1251 pjsua_media_channel_deinit(call->index);
1252 pjsua_check_snd_dev_idle();
1253
1254 if (call->inv)
1255 call_inv_end_session(call, call->last_code, &call->last_text, NULL);
1256
1257 on_return:
1258 if (dlg) pjsip_dlg_dec_lock(dlg);
1259 pj_log_pop_indent();
1260 return status;
1261 }
1262
1263 pj_status_t create_temp_sdp(pj_pool_t *pool,
1264 const pjmedia_sdp_session *rem_sdp,
1265 pjmedia_sdp_session **p_sdp)
1266 {
1267 const pj_str_t STR_AUDIO = { "audio", 5 };
1268 const pj_str_t STR_VIDEO = { "video", 5 };
1269 const pj_str_t STR_TEXT = { "text", 4 };
1270 const pj_str_t STR_IP6 = { "IP6", 3};
1271
1272 pjmedia_sdp_session *sdp;
1273 pj_sockaddr origin;
1274 pj_uint16_t tmp_port = 50123;
1275 pj_status_t status = PJ_SUCCESS;
1276 pj_str_t tmp_st;
1277 unsigned i = 0;
1278 pj_bool_t sess_use_ipv4 = PJ_TRUE;
1279
1280 /* Get one address to use in the origin field */
1281 pj_sockaddr_init(PJ_AF_INET, &origin, pj_strset2(&tmp_st, "127.0.0.1"), 0);
1282
1283 /* Create the base (blank) SDP */
1284 status = pjmedia_endpt_create_base_sdp(pjsua_var.med_endpt, pool, NULL,
1285 &origin, &sdp);
1286 if (status != PJ_SUCCESS)
1287 return status;
1288
1289 if (rem_sdp->conn && pj_stricmp(&rem_sdp->conn->addr_type, &STR_IP6)==0) {
1290 sess_use_ipv4 = PJ_FALSE;
1291 }
1292
1293 for (; i< rem_sdp->media_count ; ++i) {
1294 pjmedia_sdp_media *m = NULL;
1295 pjmedia_sock_info sock_info;
1296 pj_bool_t med_use_ipv4 = sess_use_ipv4;
1297
1298 if (rem_sdp->media[i]->conn &&
1299 pj_stricmp(&rem_sdp->media[i]->conn->addr_type, &STR_IP6) == 0)
1300 {
1301 med_use_ipv4 = PJ_FALSE;
1302 }
1303
1304 pj_sockaddr_init(med_use_ipv4?PJ_AF_INET:PJ_AF_INET6,
1305 &sock_info.rtp_addr_name,
1306 med_use_ipv4?pj_strset2(&tmp_st, "127.0.0.1"):
1307 pj_strset2(&tmp_st, "::1"),
1308 rem_sdp->media[i]->desc.port? (tmp_port++):0);
1309
1310 pj_sockaddr_init(med_use_ipv4?PJ_AF_INET:PJ_AF_INET6,
1311 &sock_info.rtcp_addr_name,
1312 med_use_ipv4?pj_strset2(&tmp_st, "127.0.0.1"):
1313 pj_strset2(&tmp_st, "::1"),
1314 rem_sdp->media[i]->desc.port? (tmp_port++):0);
1315
1316 if (pj_stricmp(&rem_sdp->media[i]->desc.media, &STR_AUDIO)==0) {
1317 m = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_media);
1318 status = pjmedia_endpt_create_audio_sdp(pjsua_var.med_endpt,
1319 pool, &sock_info, 0, &m);
1320
1321 if (status != PJ_SUCCESS)
1322 return status;
1323
1324 } else if (pj_stricmp(&rem_sdp->media[i]->desc.media, &STR_VIDEO)==0) {
1325 #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
1326 m = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_media);
1327 status = pjmedia_endpt_create_video_sdp(pjsua_var.med_endpt, pool,
1328 &sock_info, 0, &m);
1329 if (status != PJ_SUCCESS)
1330 return status;
1331 #else
1332 m = pjmedia_sdp_media_clone_deactivate(pool, rem_sdp->media[i]);
1333 #endif
1334 } else if (pj_stricmp(&rem_sdp->media[i]->desc.media, &STR_TEXT)==0) {
1335 m = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_media);
1336 status = pjmedia_endpt_create_text_sdp(pjsua_var.med_endpt,
1337 pool, &sock_info, 0, &m);
1338
1339 if (status != PJ_SUCCESS)
1340 return status;
1341
1342 } else {
1343 m = pjmedia_sdp_media_clone_deactivate(pool, rem_sdp->media[i]);
1344 }
1345
1346 /* Add connection line, if none */
1347 if (m->conn == NULL && sdp->conn == NULL) {
1348 m->conn = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_conn);
1349 m->conn->net_type = pj_str("IN");
1350 if (med_use_ipv4) {
1351 m->conn->addr_type = pj_str("IP4");
1352 m->conn->addr = pj_str("127.0.0.1");
1353 } else {
1354 m->conn->addr_type = pj_str("IP6");
1355 m->conn->addr = pj_str("::1");
1356 }
1357 }
1358
1359 /* Disable media if it has zero format/codec */
1360 if (m->desc.fmt_count == 0) {
1361 m->desc.fmt[m->desc.fmt_count++] = pj_str("0");
1362 pjmedia_sdp_media_deactivate(pool, m);
1363 }
1364
1365 sdp->media[sdp->media_count++] = m;
1366 }
1367
1368 *p_sdp = sdp;
1369 return PJ_SUCCESS;
1370 }
1371
1372 static pj_status_t verify_request(const pjsua_call *call,
1373 pjsip_rx_data *rdata,
1374 pj_bool_t use_tmp_sdp,
1375 int *sip_err_code,
1376 pjsip_tx_data **response)
1377 {
1378 const pjmedia_sdp_session *offer = NULL;
1379 pjmedia_sdp_session *answer;
1380 int err_code = 0;
1381 pj_status_t status;
1382
1383 /* Get remote SDP offer (if any). */
1384 if (call->inv->neg)
1385 {
1386 pjmedia_sdp_neg_get_neg_remote(call->inv->neg, &offer);
1387 }
1388
1389 if (use_tmp_sdp) {
1390 if (offer == NULL)
1391 return PJ_SUCCESS;
1392
1393 /* Create temporary SDP to check for codec support and capability
1394 * to handle the required SIP extensions.
1395 */
1396 status = create_temp_sdp(call->inv->pool_prov, offer, &answer);
1397
1398 if (status != PJ_SUCCESS) {
1399 err_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
1400 pjsua_perror(THIS_FILE, "Error creating SDP answer", status);
1401 }
1402 } else {
1403 status = pjsua_media_channel_create_sdp(call->index,
1404 call->async_call.dlg->pool,
1405 offer, &answer, sip_err_code);
1406
1407 if (status != PJ_SUCCESS) {
1408 err_code = *sip_err_code;
1409 pjsua_perror(THIS_FILE, "Error creating SDP answer", status);
1410 } else {
1411 status = pjsip_inv_set_local_sdp(call->inv, answer);
1412 if (status != PJ_SUCCESS) {
1413 err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
1414 pjsua_perror(THIS_FILE, "Error setting local SDP", status);
1415 }
1416 }
1417 }
1418
1419 if (status == PJ_SUCCESS) {
1420 unsigned options = 0;
1421 pjsip_tx_data *tx_data_resp = NULL;
1422
1423 /* Add SIPREC support to prevent the "bad extension" error */
1424 options |= PJSIP_INV_SUPPORT_SIPREC;
1425
1426 /* Verify that we can handle the request. */
1427 status = pjsip_inv_verify_request3(rdata,
1428 call->inv->pool_prov, &options,
1429 offer, answer, NULL,
1430 pjsua_var.endpt, &tx_data_resp);
1431 if (status != PJ_SUCCESS) {
1432 /*
1433 * No we can't handle the incoming INVITE request.
1434 */
1435 pjsua_perror(THIS_FILE, "Request verification failed", status);
1436
1437 if (tx_data_resp)
1438 err_code = tx_data_resp->msg->line.status.code;
1439 else
1440 err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
1441
1442 if (response)
1443 *response = tx_data_resp;
1444 }
1445 }
1446
1447 if (sip_err_code && status != PJ_SUCCESS)
1448 *sip_err_code = err_code? err_code:PJSIP_ERRNO_TO_SIP_STATUS(status);
1449
1450 return status;
1451 }
1452
1453 static void rejected_incoming_call_cb(pjsua_call_id call_id,
1454 pjsip_rx_data *rdata,
1455 pjsip_tx_data *tdata,
1456 int st_code,
1457 pj_str_t *st_text)
1458 {
1459 pjsip_from_hdr *from_hdr = NULL;
1460 pjsip_to_hdr *to_hdr = NULL;
1461 pjsip_sip_uri *uri;
1462 pjsua_on_rejected_incoming_call_param param;
1463
1464 if (!pjsua_var.ua_cfg.cb.on_rejected_incoming_call)
1465 return;
1466
1467 pj_bzero(&param, sizeof(pjsua_on_rejected_incoming_call_param));
1468 param.call_id = call_id;
1469 param.st_code = st_code;
1470 if (rdata) {
1471 param.rdata = rdata;
1472 to_hdr = rdata->msg_info.to;
1473 from_hdr = rdata->msg_info.from;
1474 } else if (tdata) {
1475 pjsip_msg *msg = tdata->msg;
1476 to_hdr = PJSIP_MSG_TO_HDR(msg);
1477 from_hdr = PJSIP_MSG_FROM_HDR(msg);
1478 }
1479
1480 if (to_hdr) {
1481 param.local_info.ptr = param.buf_.local_info;
1482 uri = (pjsip_sip_uri*)pjsip_uri_get_uri(to_hdr->uri);
1483 param.local_info.slen = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR,
1484 uri,
1485 param.buf_.local_info,
1486 sizeof(param.buf_.local_info));
1487 }
1488 if (from_hdr) {
1489 param.remote_info.ptr = param.buf_.remote_info;
1490 uri = (pjsip_sip_uri*)pjsip_uri_get_uri(from_hdr->uri);
1491 param.remote_info.slen = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR,
1492 uri,
1493 param.buf_.remote_info,
1494 sizeof(param.buf_.remote_info));
1495 }
1496
1497 if (st_text && st_text->slen) {
1498 pj_strassign(&param.st_text, st_text);
1499 } else {
1500 param.st_text = *pjsip_get_status_text(st_code);
1501 }
1502
1503 pjsua_var.ua_cfg.cb.on_rejected_incoming_call(&param);
1504 }
1505
1506 /* Incoming call callback when media transport creation is completed. */
1507 static pj_status_t
1508 on_incoming_call_med_tp_complete2(pjsua_call_id call_id,
1509 const pjsua_med_tp_state_info *info,
1510 pjsip_rx_data *rdata,
1511 int *sip_err_code,
1512 pjsip_tx_data **tdata)
1513 {
1514 pjsua_call *call = &pjsua_var.calls[call_id];
1515 pjsip_dialog *dlg = call->async_call.dlg;
1516 pj_status_t status = (info? info->status: PJ_SUCCESS);
1517 int err_code = (info? info->sip_err_code: 0);
1518 pjsip_tx_data *response = NULL;
1519
1520 PJSUA_LOCK();
1521
1522 /* Increment the dialog's lock to prevent it to be destroyed prematurely,
1523 * such as in case of transport error.
1524 */
1525 pjsip_dlg_inc_lock(dlg);
1526
1527 /* Decrement dialog session. */
1528 pjsip_dlg_dec_session(dlg, &pjsua_var.mod);
1529
1530 if (status != PJ_SUCCESS) {
1531 pjsua_perror(THIS_FILE, "Error initializing media channel", status);
1532 goto on_return;
1533 }
1534
1535 /* pjsua_media_channel_deinit() has been called. */
1536 if (call->async_call.med_ch_deinit) {
1537 pjsua_media_channel_deinit(call->index);
1538 call->med_ch_cb = NULL;
1539 pjsip_dlg_dec_lock(dlg);
1540 PJSUA_UNLOCK();
1541 return PJ_SUCCESS;
1542 }
1543
1544 status = verify_request(call, rdata, PJ_FALSE, &err_code, &response);
1545
1546 on_return:
1547 if (status != PJ_SUCCESS) {
1548 if (err_code == 0)
1549 err_code = PJSIP_ERRNO_TO_SIP_STATUS(status);
1550
1551 if (sip_err_code)
1552 *sip_err_code = err_code;
1553
1554 /* If the callback is called from pjsua_call_on_incoming(), the
1555 * invite's state is PJSIP_INV_STATE_NULL, so the invite session
1556 * will be terminated later, otherwise we end the session here.
1557 */
1558 if (call->inv->state > PJSIP_INV_STATE_NULL) {
1559 pj_status_t status_ = PJ_SUCCESS;
1560 pj_str_t reason = pj_str("");
1561
1562 if (response == NULL) {
1563 reason = pj_str("Failed creating media transport");
1564 status_ = pjsip_inv_end_session(call->inv, err_code, &reason,
1565 &response);
1566 }
1567
1568 if (status_ == PJ_SUCCESS && response)
1569 status_ = pjsip_inv_send_msg(call->inv, response);
1570
1571 if ((err_code / 100 != 1) && (err_code / 100 != 2)) {
1572 rejected_incoming_call_cb(call->index, rdata, response,
1573 err_code, &reason);
1574 }
1575 }
1576 pjsua_media_channel_deinit(call->index);
1577 }
1578
1579 /* Set the callback to NULL to indicate that the async operation
1580 * has completed.
1581 */
1582 call->med_ch_cb = NULL;
1583
1584 /* Finish any pending process */
1585 if (status == PJ_SUCCESS) {
1586 if (call->async_call.call_var.inc_call.replaced_dlg) {
1587 /* Process pending call replace */
1588 pjsip_dialog *replaced_dlg =
1589 call->async_call.call_var.inc_call.replaced_dlg;
1590 process_incoming_call_replace(call, replaced_dlg);
1591 } else {
1592 /* Process pending call answers */
1593 process_pending_call_answer(call);
1594 }
1595 }
1596
1597 pjsip_dlg_dec_lock(dlg);
1598
1599 if (tdata)
1600 *tdata = response;
1601
1602 PJSUA_UNLOCK();
1603 return status;
1604 }
1605
1606 static pj_status_t
1607 on_incoming_call_med_tp_complete(pjsua_call_id call_id,
1608 const pjsua_med_tp_state_info *info)
1609 {
1610 return on_incoming_call_med_tp_complete2(call_id, info, NULL, NULL, NULL);
1611 }
1612
1613 /**
1614 * Handle incoming INVITE request.
1615 * Called by pjsua_core.c
1616 */
1617 pj_bool_t pjsua_call_on_incoming(pjsip_rx_data *rdata)
1618 {
1619 pj_str_t contact;
1620 pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata);
1621 pjsip_dialog *replaced_dlg = NULL;
1622 pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
1623 pjsip_msg *msg = rdata->msg_info.msg;
1624 pjsip_tx_data *response = NULL;
1625 unsigned options = 0;
1626 pjsip_inv_session *inv = NULL;
1627 int acc_id;
1628 pjsua_call *call = NULL;
1629 int call_id = PJSUA_INVALID_ID;
1630 int sip_err_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
1631 pjmedia_sdp_session *offer=NULL;
1632 pj_bool_t should_dec_dlg = PJ_FALSE;
1633 pjsip_tpselector tp_sel;
1634 pj_str_t st_reason = pj_str("");
1635 int ret_st_code = 0;
1636 pj_status_t status;
1637
1638 /* Don't want to handle anything but INVITE */
1639 if (msg->line.req.method.id != PJSIP_INVITE_METHOD)
1640 return PJ_FALSE;
1641
1642 /* Don't want to handle anything that's already associated with
1643 * existing dialog or transaction.
1644 */
1645 if (dlg || tsx)
1646 return PJ_FALSE;
1647
1648 /* Don't want to accept the call if shutdown is in progress */
1649 if (pjsua_var.thread_quit_flag) {
1650 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata,
1651 PJSIP_SC_TEMPORARILY_UNAVAILABLE, NULL,
1652 NULL, NULL);
1653 return PJ_TRUE;
1654 }
1655
1656 PJ_LOG(4,(THIS_FILE, "Incoming %s", rdata->msg_info.info));
1657 pj_log_push_indent();
1658
1659 PJSUA_LOCK();
1660
1661 /* Find free call slot. */
1662 call_id = alloc_call_id();
1663
1664 if (call_id == PJSUA_INVALID_ID) {
1665 ret_st_code = PJSIP_SC_BUSY_HERE;
1666 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, ret_st_code,
1667 NULL, NULL, NULL);
1668 PJ_LOG(2,(THIS_FILE,
1669 "Unable to accept incoming call (too many calls)"));
1670 goto on_return;
1671 }
1672
1673 /* Clear call descriptor */
1674 reset_call(call_id);
1675
1676 call = &pjsua_var.calls[call_id];
1677
1678 /* Generate per-session RTCP CNAME, according to RFC 7022. */
1679 pj_create_random_string(call->cname_buf, call->cname.slen);
1680
1681 /* Mark call start time. */
1682 pj_gettimeofday(&call->start_time);
1683
1684 /* Check INVITE request for Replaces header. If Replaces header is
1685 * present, the function will make sure that we can handle the request.
1686 */
1687 status = pjsip_replaces_verify_request(rdata, &replaced_dlg, PJ_FALSE,
1688 &response);
1689 if (status != PJ_SUCCESS) {
1690 /*
1691 * Something wrong with the Replaces header.
1692 */
1693 if (response) {
1694 pjsip_response_addr res_addr;
1695 ret_st_code = response->msg->line.status.code;
1696
1697 pjsip_get_response_addr(response->pool, rdata, &res_addr);
1698 status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr,
1699 response, NULL, NULL);
1700 if (status != PJ_SUCCESS) pjsip_tx_data_dec_ref(response);
1701 } else {
1702 ret_st_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
1703 /* Respond with 500 (Internal Server Error) */
1704 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, ret_st_code,
1705 NULL, NULL, NULL);
1706 }
1707
1708 goto on_return;
1709 }
1710
1711 /* If this INVITE request contains Replaces header, notify application
1712 * about the request so that application can do subsequent checking
1713 * if it wants to.
1714 */
1715 if (replaced_dlg != NULL &&
1716 (pjsua_var.ua_cfg.cb.on_call_replace_request ||
1717 pjsua_var.ua_cfg.cb.on_call_replace_request2))
1718 {
1719 pjsua_call *replaced_call;
1720 int st_code = 200;
1721 pj_str_t st_text = { "OK", 2 };
1722
1723 /* Get the replaced call instance */
1724 replaced_call = (pjsua_call*) replaced_dlg->mod_data[pjsua_var.mod.id];
1725
1726 /* Copy call setting from the replaced call */
1727 call->opt = replaced_call->opt;
1728 pjsua_call_cleanup_flag(&call->opt);
1729
1730 /* Notify application */
1731 if (!replaced_call->hanging_up &&
1732 pjsua_var.ua_cfg.cb.on_call_replace_request)
1733 {
1734 pjsua_var.ua_cfg.cb.on_call_replace_request(replaced_call->index,
1735 rdata,
1736 &st_code, &st_text);
1737 }
1738
1739 if (!replaced_call->hanging_up &&
1740 pjsua_var.ua_cfg.cb.on_call_replace_request2)
1741 {
1742 pjsua_var.ua_cfg.cb.on_call_replace_request2(replaced_call->index,
1743 rdata,
1744 &st_code, &st_text,
1745 &call->opt);
1746 }
1747
1748 /* Must specify final response */
1749 PJ_ASSERT_ON_FAIL(st_code >= 200, st_code = 200);
1750
1751 /* Check if application rejects this request. */
1752 if (st_code >= 300) {
1753
1754 if (st_text.slen == 2)
1755 st_text = *pjsip_get_status_text(st_code);
1756
1757 pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata,
1758 st_code, &st_text, NULL, NULL, NULL);
1759 ret_st_code = st_code;
1760 goto on_return;
1761 }
1762
1763 /* Set the user_data of the new call to the existing/parent call,
1764 * it is needed by PJSUA2 to update its states. While PJSUA app can
1765 * always override it anytime.
1766 */
1767 pjsua_call_set_user_data(call_id, replaced_call->user_data);
1768 }
1769
1770 if (!replaced_dlg) {
1771 /* Clone rdata — needed for on_incoming_call callback.
1772 * Without it, incoming_data stays NULL and on_incoming_call
1773 * is silently skipped, so reject the call on failure.
1774 */
1775 status = pjsip_rx_data_clone(rdata, 0, &call->incoming_data);
1776 if (status != PJ_SUCCESS) {
1777 PJ_PERROR(1, (THIS_FILE, status,
1778 "Failed to clone rdata for incoming call"));
1779 ret_st_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
1780 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata,
1781 ret_st_code, NULL, NULL, NULL);
1782 goto on_return;
1783 }
1784 }
1785
1786 /*
1787 * Get which account is most likely to be associated with this incoming
1788 * call. We need the account to find which contact URI to put for
1789 * the call.
1790 */
1791 if (replaced_dlg) {
1792 /* For call replace, use the same account as the replaced call */
1793 pjsua_call *replaced_call;
1794 replaced_call = (pjsua_call*)replaced_dlg->mod_data[pjsua_var.mod.id];
1795 acc_id = call->acc_id = replaced_call->acc_id;
1796 } else {
1797 acc_id = call->acc_id = pjsua_acc_find_for_incoming(rdata);
1798 if (acc_id == PJSUA_INVALID_ID) {
1799 ret_st_code = PJSIP_SC_TEMPORARILY_UNAVAILABLE;
1800 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, ret_st_code,
1801 NULL, NULL, NULL);
1802
1803 PJ_LOG(2,(THIS_FILE,
1804 "Unable to accept incoming call (no available account)"));
1805
1806 goto on_return;
1807 }
1808 }
1809 call->call_hold_type = pjsua_var.acc[acc_id].cfg.call_hold_type;
1810
1811 /* Get call's secure level */
1812 if (PJSIP_URI_SCHEME_IS_SIPS(rdata->msg_info.msg->line.req.uri))
1813 call->secure_level = 2;
1814 else if (PJSIP_TRANSPORT_IS_SECURE(rdata->tp_info.transport))
1815 call->secure_level = 1;
1816 else
1817 call->secure_level = 0;
1818
1819 /* Parse SDP from incoming request */
1820 if (rdata->msg_info.msg->body) {
1821 pjsip_rdata_sdp_info *sdp_info;
1822
1823 sdp_info = pjsip_rdata_get_sdp_info(rdata);
1824 offer = sdp_info->sdp;
1825
1826 status = sdp_info->sdp_err;
1827 if (status==PJ_SUCCESS && sdp_info->sdp==NULL &&
1828 !PJSIP_INV_ACCEPT_UNKNOWN_BODY)
1829 {
1830 if (sdp_info->body.ptr == NULL) {
1831 status = PJSIP_ERRNO_FROM_SIP_STATUS(
1832 PJSIP_SC_UNSUPPORTED_MEDIA_TYPE);
1833 } else {
1834 status = PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE);
1835 }
1836 }
1837
1838 if (status != PJ_SUCCESS) {
1839 pjsip_hdr hdr_list;
1840
1841 /* Check if body really contains SDP. */
1842 if (sdp_info->body.ptr == NULL) {
1843 /* Couldn't find "application/sdp" */
1844 pjsip_accept_hdr *acc;
1845
1846 pjsua_perror(THIS_FILE, "Unknown Content-Type in incoming "\
1847 "INVITE", status);
1848
1849 /* Add Accept header to response */
1850 acc = pjsip_accept_hdr_create(rdata->tp_info.pool);
1851 PJ_ASSERT_RETURN(acc, PJ_ENOMEM);
1852 acc->values[acc->count++] = pj_str("application/sdp");
1853 pj_list_init(&hdr_list);
1854 pj_list_push_back(&hdr_list, acc);
1855 ret_st_code = PJSIP_SC_UNSUPPORTED_MEDIA_TYPE;
1856
1857 pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, ret_st_code,
1858 NULL, &hdr_list, NULL, NULL);
1859 } else {
1860 pjsip_warning_hdr *w;
1861 st_reason = pj_str("Bad SDP");
1862
1863 pjsua_perror(THIS_FILE, "Bad SDP in incoming INVITE",
1864 status);
1865
1866 w = pjsip_warning_hdr_create_from_status(rdata->tp_info.pool,
1867 pjsip_endpt_name(pjsua_var.endpt),
1868 status);
1869 pj_list_init(&hdr_list);
1870 pj_list_push_back(&hdr_list, w);
1871 ret_st_code = PJSIP_SC_BAD_REQUEST;
1872
1873 pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, ret_st_code,
1874 &st_reason, &hdr_list, NULL, NULL);
1875 }
1876 goto on_return;
1877 }
1878
1879 /* Do quick checks on SDP before passing it to transports. More elabore
1880 * checks will be done in pjsip_inv_verify_request2() below.
1881 */
1882 if ((offer) && (offer->media_count==0)) {
1883 st_reason = pj_str("Missing media in SDP");
1884 ret_st_code = PJSIP_SC_BAD_REQUEST;
1885
1886 pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, ret_st_code,
1887 &st_reason, NULL, NULL, NULL);
1888 goto on_return;
1889 }
1890
1891 } else {
1892 offer = NULL;
1893 }
1894
1895 /* Verify that we can handle the request. */
1896 options |= PJSIP_INV_SUPPORT_100REL;
1897 options |= PJSIP_INV_SUPPORT_TIMER;
1898
1899 #if PJSUA_HAS_SIPREC
1900
1901 if(pjsua_var.acc[acc_id].cfg.use_siprec != PJSUA_SIP_SIPREC_INACTIVE){
1902 options |= PJSIP_INV_SUPPORT_SIPREC;
1903 if(pjsua_var.acc[acc_id].cfg.use_siprec == PJSUA_SIP_SIPREC_MANDATORY){
1904 options |= PJSIP_INV_REQUIRE_SIPREC;
1905 }
1906 }
1907
1908 /* Check if the INVITE request is a siprec
1909 * this function add PJSIP_INV_REQUIRE_SIPREC to options
1910 * and returns the value PJ_SUCCESS
1911 */
1912 status = pjsip_siprec_verify_request(rdata, &call->siprec_metadata, offer,
1913 &options, NULL, pjsua_var.endpt, &response);
1914
1915 if(status != PJ_SUCCESS){
1916 /*
1917 * No we can't handle the incoming INVITE request.
1918 */
1919 if (response) {
1920 pjsip_response_addr res_addr;
1921 ret_st_code = response->msg->line.status.code;
1922
1923 pjsip_get_response_addr(response->pool, rdata, &res_addr);
1924 status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr,
1925 response, NULL, NULL);
1926 if (status != PJ_SUCCESS) pjsip_tx_data_dec_ref(response);
1927
1928 } else {
1929 /* Respond with 500 (Internal Server Error) */
1930 ret_st_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
1931 pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, ret_st_code,
1932 NULL, NULL, NULL, NULL);
1933 }
1934
1935 goto on_return;
1936 }
1937
1938 #endif
1939
1940 if (pjsua_var.acc[acc_id].cfg.require_100rel == PJSUA_100REL_MANDATORY)
1941 options |= PJSIP_INV_REQUIRE_100REL;
1942 if (pjsua_var.acc[acc_id].cfg.ice_cfg.enable_ice) {
1943 options |= PJSIP_INV_SUPPORT_ICE;
1944 if (pjsua_var.acc[acc_id].cfg.ice_cfg.ice_opt.trickle !=
1945 PJ_ICE_SESS_TRICKLE_DISABLED)
1946 {
1947 options |= PJSIP_INV_SUPPORT_TRICKLE_ICE;
1948 }
1949 }
1950 if (pjsua_var.acc[acc_id].cfg.use_timer == PJSUA_SIP_TIMER_REQUIRED)
1951 options |= PJSIP_INV_REQUIRE_TIMER;
1952 else if (pjsua_var.acc[acc_id].cfg.use_timer == PJSUA_SIP_TIMER_ALWAYS)
1953 options |= PJSIP_INV_ALWAYS_USE_TIMER;
1954
1955 status = pjsip_inv_verify_request2(rdata, &options, offer, NULL, NULL,
1956 pjsua_var.endpt, &response);
1957 if (status != PJ_SUCCESS) {
1958
1959 /*
1960 * No we can't handle the incoming INVITE request.
1961 */
1962 if (response) {
1963 pjsip_response_addr res_addr;
1964 ret_st_code = response->msg->line.status.code;
1965
1966 pjsip_get_response_addr(response->pool, rdata, &res_addr);
1967 status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr,
1968 response, NULL, NULL);
1969 if (status != PJ_SUCCESS) pjsip_tx_data_dec_ref(response);
1970
1971 } else {
1972 /* Respond with 500 (Internal Server Error) */
1973 ret_st_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
1974 pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, ret_st_code,
1975 NULL, NULL, NULL, NULL);
1976 }
1977
1978 goto on_return;
1979 }
1980
1981 /* Get suitable Contact header */
1982 if (pjsua_var.acc[acc_id].contact.slen) {
1983 contact = pjsua_var.acc[acc_id].contact;
1984 } else {
1985 status = pjsua_acc_create_uas_contact(rdata->tp_info.pool, &contact,
1986 acc_id, rdata);
1987 if (status != PJ_SUCCESS) {
1988 ret_st_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
1989 pjsua_perror(THIS_FILE, "Unable to generate Contact header",
1990 status);
1991 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, ret_st_code,
1992 NULL, NULL, NULL);
1993 goto on_return;
1994 }
1995 }
1996
1997 /* Create dialog: */
1998 status = pjsip_dlg_create_uas_and_inc_lock( pjsip_ua_instance(), rdata,
1999 &contact, &dlg);
2000 if (status != PJ_SUCCESS) {
2001 ret_st_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
2002 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, ret_st_code,
2003 NULL, NULL, NULL);
2004 goto on_return;
2005 }
2006
2007 if (pjsua_var.acc[acc_id].cfg.allow_via_rewrite &&
2008 pjsua_var.acc[acc_id].via_addr.host.slen > 0)
2009 {
2010 pjsip_dlg_set_via_sent_by(dlg, &pjsua_var.acc[acc_id].via_addr,
2011 pjsua_var.acc[acc_id].via_tp);
2012 } else if (!pjsua_sip_acc_is_using_stun(acc_id) &&
2013 !pjsua_sip_acc_is_using_upnp(acc_id))
2014 {
2015 /* Choose local interface to use in Via if acc is not using
2016 * STUN nor UPnP. See https://github.com/pjsip/pjproject/issues/1804
2017 */
2018 char target_buf[PJSIP_MAX_URL_SIZE];
2019 pj_str_t target;
2020 pjsip_host_port via_addr;
2021 const void *via_tp;
2022
2023 target.ptr = target_buf;
2024 target.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
2025 dlg->target,
2026 target_buf, sizeof(target_buf));
2027 if (target.slen < 0) target.slen = 0;
2028
2029 if (pjsua_acc_get_uac_addr(acc_id, dlg->pool, &target,
2030 &via_addr, NULL, NULL,
2031 &via_tp) == PJ_SUCCESS)
2032 {
2033 pjsip_dlg_set_via_sent_by(dlg, &via_addr,
2034 (pjsip_transport*)via_tp);
2035 }
2036 }
2037
2038 /* Set credentials */
2039 if (pjsua_var.acc[acc_id].cred_cnt) {
2040 pjsip_auth_clt_set_credentials(&dlg->auth_sess,
2041 pjsua_var.acc[acc_id].cred_cnt,
2042 pjsua_var.acc[acc_id].cred);
2043 }
2044
2045 /* Set shared or non-shared async auth for incoming call dialog */
2046 if (pjsua_var.acc[acc_id].cfg.use_shared_auth) {
2047 pjsip_dlg_set_auth_sess(dlg,
2048 &pjsua_var.acc[acc_id].shared_auth_sess);
2049 } else if (pjsua_var.ua_cfg.cb.on_auth_challenge) {
2050 pjsip_auth_clt_async_setting async_opt;
2051 pj_bzero(&async_opt, sizeof(async_opt));
2052 async_opt.cb = &pjsua_auth_on_challenge;
2053 async_opt.user_data = (void*)(pj_ssize_t)acc_id;
2054 pjsip_auth_clt_async_configure(&dlg->auth_sess, &async_opt);
2055 }
2056
2057 /* Set preference */
2058 pjsip_auth_clt_set_prefs(&dlg->auth_sess,
2059 &pjsua_var.acc[acc_id].cfg.auth_pref);
2060
2061 /* Disable Session Timers if not prefered and the incoming INVITE request
2062 * did not require it.
2063 */
2064 if (pjsua_var.acc[acc_id].cfg.use_timer == PJSUA_SIP_TIMER_INACTIVE &&
2065 (options & PJSIP_INV_REQUIRE_TIMER) == 0)
2066 {
2067 options &= ~(PJSIP_INV_SUPPORT_TIMER);
2068 }
2069
2070 /* If 100rel is optional and UAC supports it, use it. */
2071 if ((options & PJSIP_INV_REQUIRE_100REL)==0 &&
2072 pjsua_var.acc[acc_id].cfg.require_100rel == PJSUA_100REL_OPTIONAL)
2073 {
2074 const pj_str_t token = { "100rel", 6};
2075 pjsip_dialog_cap_status cap_status;
2076
2077 cap_status = pjsip_dlg_remote_has_cap(dlg, PJSIP_H_SUPPORTED, NULL,
2078 &token);
2079 if (cap_status == PJSIP_DIALOG_CAP_SUPPORTED)
2080 options |= PJSIP_INV_REQUIRE_100REL;
2081 }
2082
2083 /* Create invite session: */
2084 status = pjsip_inv_create_uas( dlg, rdata, NULL, options, &inv);
2085 if (status != PJ_SUCCESS) {
2086 pjsip_hdr hdr_list;
2087 pjsip_warning_hdr *w;
2088
2089 w = pjsip_warning_hdr_create_from_status(dlg->pool,
2090 pjsip_endpt_name(pjsua_var.endpt),
2091 status);
2092 pj_list_init(&hdr_list);
2093 pj_list_push_back(&hdr_list, w);
2094 ret_st_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
2095 pjsip_dlg_respond(dlg, rdata, ret_st_code, NULL, &hdr_list, NULL);
2096
2097 /* Can't terminate dialog because transaction is in progress.
2098 pjsip_dlg_terminate(dlg);
2099 */
2100 goto on_return;
2101 }
2102
2103 /* Set dialog's transport based on acc's config. */
2104 pjsua_init_tpselector(acc_id, &tp_sel);
2105 pjsip_dlg_set_transport(dlg, &tp_sel);
2106
2107 /* Create and attach pjsua_var data to the dialog */
2108 call->inv = inv;
2109
2110 /* Store variables required for the callback after the async
2111 * media transport creation is completed.
2112 */
2113 call->async_call.dlg = dlg;
2114 pj_list_init(&call->async_call.call_var.inc_call.answers);
2115
2116 pjsip_dlg_inc_session(dlg, &pjsua_var.mod);
2117 should_dec_dlg = PJ_TRUE;
2118
2119 /* Init media channel, only when there is offer or call replace request.
2120 * For incoming call without SDP offer, media channel init will be done
2121 * in pjsua_call_answer(), see ticket #1526.
2122 */
2123 if (offer || replaced_dlg) {
2124
2125 /* This is only for initial verification, it will check the SDP for
2126 * codec support and the capability to handle the required
2127 * SIP extensions.
2128 */
2129 status = verify_request(call, rdata, PJ_TRUE, &sip_err_code,
2130 &response);
2131
2132 if (status != PJ_SUCCESS) {
2133 pjsip_dlg_inc_lock(dlg);
2134
2135 if (response) {
2136 ret_st_code = response->msg->line.status.code;
2137
2138 pjsip_dlg_send_response(dlg, call->inv->invite_tsx, response);
2139 } else {
2140 ret_st_code = sip_err_code;
2141
2142 pjsip_dlg_respond(dlg, rdata, sip_err_code, NULL, NULL, NULL);
2143 }
2144
2145 if (call->inv->dlg) {
2146 pjsip_inv_terminate(call->inv, sip_err_code, PJ_FALSE);
2147 }
2148 pjsip_dlg_dec_lock(dlg);
2149
2150 call->inv = NULL;
2151 call->async_call.dlg = NULL;
2152 goto on_return;
2153 }
2154 status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAS,
2155 call->secure_level,
2156 rdata->tp_info.pool,
2157 offer,
2158 &sip_err_code, PJ_TRUE,
2159 &on_incoming_call_med_tp_complete);
2160 if (status == PJ_EPENDING) {
2161 /* on_incoming_call_med_tp_complete() will call
2162 * pjsip_dlg_dec_session().
2163 */
2164 should_dec_dlg = PJ_FALSE;
2165 } else if (status == PJ_SUCCESS) {
2166 /* on_incoming_call_med_tp_complete2() will call
2167 * pjsip_dlg_dec_session().
2168 */
2169 should_dec_dlg = PJ_FALSE;
2170
2171 status = on_incoming_call_med_tp_complete2(call_id, NULL,
2172 rdata, &sip_err_code,
2173 &response);
2174 if (status != PJ_SUCCESS) {
2175 /* Since the call invite's state is still PJSIP_INV_STATE_NULL,
2176 * the invite session was not ended in
2177 * on_incoming_call_med_tp_complete(), so we need to send
2178 * a response message and terminate the invite here.
2179 */
2180 pjsip_dlg_inc_lock(dlg);
2181
2182 if (response) {
2183 ret_st_code = response->msg->line.status.code;
2184
2185 pjsip_dlg_send_response(dlg, call->inv->invite_tsx,
2186 response);
2187
2188 } else {
2189 ret_st_code = sip_err_code;
2190
2191 pjsip_dlg_respond(dlg, rdata, sip_err_code, NULL, NULL,
2192 NULL);
2193 }
2194
2195 if (call->inv && call->inv->dlg) {
2196 pjsip_inv_terminate(call->inv, sip_err_code, PJ_FALSE);
2197 }
2198 pjsip_dlg_dec_lock(dlg);
2199
2200 call->inv = NULL;
2201 call->async_call.dlg = NULL;
2202 goto on_return;
2203 }
2204 } else if (status != PJ_EPENDING) {
2205 pjsua_perror(THIS_FILE, "Error initializing media channel", status);
2206
2207 pjsip_dlg_inc_lock(dlg);
2208 ret_st_code = sip_err_code;
2209 pjsip_dlg_respond(dlg, rdata, sip_err_code, NULL, NULL, NULL);
2210 if (call->inv->dlg) {
2211 pjsip_inv_terminate(call->inv, sip_err_code, PJ_FALSE);
2212 }
2213 pjsip_dlg_dec_lock(dlg);
2214
2215 call->inv = NULL;
2216 call->async_call.dlg = NULL;
2217 goto on_return;
2218 }
2219 }
2220
2221 /* Create answer */
2222 /*
2223 status = pjsua_media_channel_create_sdp(call->index, rdata->tp_info.pool,
2224 offer, &answer, &sip_err_code);
2225 if (status != PJ_SUCCESS) {
2226 pjsua_perror(THIS_FILE, "Error creating SDP answer", status);
2227 pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata,
2228 sip_err_code, NULL, NULL, NULL, NULL);
2229 goto on_return;
2230 }
2231 */
2232
2233 /* Init Session Timers */
2234 status = pjsip_timer_init_session(inv,
2235 &pjsua_var.acc[acc_id].cfg.timer_setting);
2236 if (status != PJ_SUCCESS) {
2237 pjsua_perror(THIS_FILE, "Session Timer init failed", status);
2238 ret_st_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
2239 pjsip_dlg_respond(dlg, rdata, ret_st_code, NULL, NULL, NULL);
2240 pjsip_inv_terminate(inv, ret_st_code, PJ_FALSE);
2241
2242 pjsua_media_channel_deinit(call->index);
2243 call->inv = NULL;
2244 call->async_call.dlg = NULL;
2245
2246 goto on_return;
2247 }
2248
2249 /* Update NAT type of remote endpoint, only when there is SDP in
2250 * incoming INVITE!
2251 */
2252 if (pjsua_var.ua_cfg.nat_type_in_sdp && inv->neg &&
2253 pjmedia_sdp_neg_get_state(inv->neg) > PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER)
2254 {
2255 const pjmedia_sdp_session *remote_sdp;
2256
2257 if (pjmedia_sdp_neg_get_neg_remote(inv->neg, &remote_sdp)==PJ_SUCCESS)
2258 update_remote_nat_type(call, remote_sdp);
2259 }
2260
2261 /* Must answer with some response to initial INVITE. We'll do this before
2262 * attaching the call to the invite session/dialog, so that the application
2263 * will not get notification about this event (on another scenario, it is
2264 * also possible that inv_send_msg() fails and causes the invite session to
2265 * be disconnected. If we have the call attached at this time, this will
2266 * cause the disconnection callback to be called before on_incoming_call()
2267 * callback is called, which is not right).
2268 */
2269 status = pjsip_inv_initial_answer(inv, rdata,
2270 100, NULL, NULL, &response);
2271 if (status != PJ_SUCCESS) {
2272 if (response == NULL) {
2273 pjsua_perror(THIS_FILE, "Unable to send answer to incoming INVITE",
2274 status);
2275 ret_st_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
2276 pjsip_dlg_respond(dlg, rdata, ret_st_code, NULL, NULL, NULL);
2277 pjsip_inv_terminate(inv, ret_st_code, PJ_FALSE);
2278 } else {
2279 ret_st_code = response->msg->line.status.code;
2280
2281 pjsip_inv_send_msg(inv, response);
2282 pjsip_inv_terminate(inv, ret_st_code, PJ_FALSE);
2283 }
2284 pjsua_media_channel_deinit(call->index);
2285 call->inv = NULL;
2286 call->async_call.dlg = NULL;
2287 goto on_return;
2288
2289 } else {
2290 #if !PJSUA_DISABLE_AUTO_SEND_100
2291 status = pjsip_inv_send_msg(inv, response);
2292 if (status != PJ_SUCCESS) {
2293 pjsua_perror(THIS_FILE, "Unable to send 100 response", status);
2294 pjsua_media_channel_deinit(call->index);
2295 call->inv = NULL;
2296 call->async_call.dlg = NULL;
2297 goto on_return;
2298 }
2299 #endif
2300 }
2301
2302 /* Only do this after sending 100/Trying (really! see the long comment
2303 * above)
2304 */
2305 if (dlg->mod_data[pjsua_var.mod.id] == NULL) {
2306 /* In PJSUA2, on_incoming_call() may be called from
2307 * on_create_media_transport() hence this might already set
2308 * to allow notification about fail events via on_call_state() and
2309 * on_call_tsx_state().
2310 */
2311 dlg->mod_data[pjsua_var.mod.id] = call;
2312 inv->mod_data[pjsua_var.mod.id] = call;
2313 ++pjsua_var.call_cnt;
2314 }
2315
2316 /* Check if this request should replace existing call */
2317 if (replaced_dlg) {
2318 /* Process call replace. If the media channel init has been completed,
2319 * just process now, otherwise, just queue the replaced dialog so
2320 * it will be processed once the media channel async init is finished
2321 * successfully.
2322 */
2323 if (call->med_ch_cb == NULL) {
2324 process_incoming_call_replace(call, replaced_dlg);
2325 } else {
2326 call->async_call.call_var.inc_call.replaced_dlg = replaced_dlg;
2327 }
2328 } else {
2329 /* Notify application if on_incoming_call() is overriden,
2330 * otherwise hangup the call with 480
2331 */
2332 if (pjsua_var.ua_cfg.cb.on_incoming_call) {
2333 /* For PJSUA2, avoid invoking this callback again when it has been
2334 * invoked from on_create_media_transport().
2335 */
2336 if (call->incoming_data) {
2337 pjsua_var.ua_cfg.cb.on_incoming_call(acc_id, call_id, rdata);
2338 }
2339
2340 /* Notes:
2341 * - the call might be reset when it's rejected or hangup
2342 * by application from the callback.
2343 * - onIncomingCall() may be simulated by onCreateMediaTransport()
2344 * when media init is done synchrounously (see #1916). And if app
2345 * happens to answer/hangup the call from the callback, the
2346 * answer/hangup should have been delayed (see #1923),
2347 * so let's process the answer/hangup now.
2348 */
2349
2350 if (call->async_call.call_var.inc_call.hangup) {
2351 process_pending_call_hangup(call);
2352 } else if (call->med_ch_cb == NULL && call->inv) {
2353 process_pending_call_answer(call);
2354 }
2355 } else {
2356 pjsua_call_hangup(call_id, PJSIP_SC_TEMPORARILY_UNAVAILABLE,
2357 NULL, NULL);
2358 }
2359 }
2360
2361
2362 /* This INVITE request has been handled. */
2363 on_return:
2364 if (dlg) {
2365 if (should_dec_dlg)
2366 pjsip_dlg_dec_session(dlg, &pjsua_var.mod);
2367
2368 pjsip_dlg_dec_lock(dlg);
2369 }
2370
2371 if (call && call->incoming_data) {
2372 pjsip_rx_data_free_cloned(call->incoming_data);
2373 call->incoming_data = NULL;
2374 }
2375
2376 if ((ret_st_code != 0) && (ret_st_code / 100 != 1) &&
2377 (ret_st_code / 100 != 2))
2378 {
2379 rejected_incoming_call_cb(call_id, rdata, NULL, ret_st_code,
2380 &st_reason);
2381 }
2382
2383 pj_log_pop_indent();
2384 PJSUA_UNLOCK();
2385 return PJ_TRUE;
2386 }
2387
2388
2389
2390 /*
2391 * Check if the specified call has active INVITE session and the INVITE
2392 * session has not been disconnected.
2393 */
2394 PJ_DEF(pj_bool_t) pjsua_call_is_active(pjsua_call_id call_id)
2395 {
2396 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
2397 PJ_EINVAL);
2398 return !pjsua_var.calls[call_id].hanging_up &&
2399 pjsua_var.calls[call_id].inv != NULL &&
2400 pjsua_var.calls[call_id].inv->state != PJSIP_INV_STATE_DISCONNECTED;
2401 }
2402
2403
2404 /* Acquire lock to the specified call_id */
2405 pj_status_t acquire_call(const char *title,
2406 pjsua_call_id call_id,
2407 pjsua_call **p_call,
2408 pjsip_dialog **p_dlg)
2409 {
2410 unsigned retry;
2411 pjsua_call *call = NULL;
2412 pj_bool_t has_pjsua_lock = PJ_FALSE;
2413 pj_status_t status = PJ_SUCCESS;
2414 pj_time_val time_start, timeout;
2415 pjsip_dialog *dlg = NULL;
2416
2417 pj_gettimeofday(&time_start);
2418 timeout.sec = 0;
2419 timeout.msec = PJSUA_ACQUIRE_CALL_TIMEOUT;
2420 pj_time_val_normalize(&timeout);
2421
2422 for (retry=0; ; ++retry) {
2423
2424 if (retry % 10 == 9) {
2425 pj_time_val dtime;
2426
2427 pj_gettimeofday(&dtime);
2428 PJ_TIME_VAL_SUB(dtime, time_start);
2429 if (!PJ_TIME_VAL_LT(dtime, timeout))
2430 break;
2431 }
2432
2433 has_pjsua_lock = PJ_FALSE;
2434
2435 status = PJSUA_TRY_LOCK();
2436 if (status != PJ_SUCCESS) {
2437 pj_thread_sleep(retry/10);
2438 continue;
2439 }
2440
2441 has_pjsua_lock = PJ_TRUE;
2442 call = &pjsua_var.calls[call_id];
2443 if (call->inv)
2444 dlg = call->inv->dlg;
2445 else
2446 dlg = call->async_call.dlg;
2447
2448 if (dlg == NULL) {
2449 PJSUA_UNLOCK();
2450 PJ_LOG(3,(THIS_FILE, "Invalid call_id %d in %s", call_id, title));
2451 return PJSIP_ESESSIONTERMINATED;
2452 }
2453
2454 status = pjsip_dlg_try_inc_lock(dlg);
2455 if (status != PJ_SUCCESS) {
2456 PJSUA_UNLOCK();
2457 pj_thread_sleep(retry/10);
2458 continue;
2459 }
2460
2461 PJSUA_UNLOCK();
2462
2463 break;
2464 }
2465
2466 if (status != PJ_SUCCESS) {
2467 if (has_pjsua_lock == PJ_FALSE)
2468 PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire PJSUA mutex "
2469 "(possibly system has deadlocked) in %s",
2470 title));
2471 else
2472 PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire dialog mutex "
2473 "(possibly system has deadlocked) in %s",
2474 title));
2475 return PJ_ETIMEDOUT;
2476 }
2477
2478 *p_call = call;
2479 *p_dlg = dlg;
2480
2481 return PJ_SUCCESS;
2482 }
2483
2484
2485 /*
2486 * Obtain detail information about the specified call.
2487 */
2488 PJ_DEF(pj_status_t) pjsua_call_get_info( pjsua_call_id call_id,
2489 pjsua_call_info *info)
2490 {
2491 pjsua_call *call;
2492 pjsip_dialog *dlg;
2493 unsigned mi;
2494
2495 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
2496 PJ_EINVAL);
2497
2498 pj_bzero(info, sizeof(*info));
2499
2500 /* Use PJSUA_LOCK() instead of acquire_call():
2501 * https://github.com/pjsip/pjproject/issues/1371
2502 */
2503 PJSUA_LOCK();
2504
2505 call = &pjsua_var.calls[call_id];
2506 dlg = (call->inv ? call->inv->dlg : call->async_call.dlg);
2507 if (!dlg) {
2508 PJSUA_UNLOCK();
2509 return PJSIP_ESESSIONTERMINATED;
2510 }
2511
2512 /* id and role */
2513 info->id = call_id;
2514 info->role = dlg->role;
2515 info->acc_id = call->acc_id;
2516
2517 /* local info */
2518 info->local_info.ptr = info->buf_.local_info;
2519 pj_strncpy(&info->local_info, &dlg->local.info_str,
2520 sizeof(info->buf_.local_info));
2521
2522 /* local contact */
2523 info->local_contact.ptr = info->buf_.local_contact;
2524 info->local_contact.slen = pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR,
2525 dlg->local.contact->uri,
2526 info->local_contact.ptr,
2527 sizeof(info->buf_.local_contact));
2528 if (info->local_contact.slen < 0)
2529 info->local_contact.slen = 0;
2530
2531 /* remote info */
2532 info->remote_info.ptr = info->buf_.remote_info;
2533 pj_strncpy(&info->remote_info, &dlg->remote.info_str,
2534 sizeof(info->buf_.remote_info));
2535
2536 /* remote contact */
2537 if (dlg->remote.contact) {
2538 int len;
2539 info->remote_contact.ptr = info->buf_.remote_contact;
2540 len = pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR,
2541 dlg->remote.contact->uri,
2542 info->remote_contact.ptr,
2543 sizeof(info->buf_.remote_contact));
2544 if (len < 0) len = 0;
2545 info->remote_contact.slen = len;
2546 } else {
2547 info->remote_contact.slen = 0;
2548 }
2549
2550 /* call id */
2551 info->call_id.ptr = info->buf_.call_id;
2552 pj_strncpy(&info->call_id, &dlg->call_id->id,
2553 sizeof(info->buf_.call_id));
2554
2555 /* call setting */
2556 pj_memcpy(&info->setting, &call->opt, sizeof(call->opt));
2557
2558 /* state, state_text */
2559 if (call->hanging_up) {
2560 info->state = PJSIP_INV_STATE_DISCONNECTED;
2561 } else if (call->inv) {
2562 info->state = call->inv->state;
2563 if (call->inv->role == PJSIP_ROLE_UAS &&
2564 info->state == PJSIP_INV_STATE_NULL)
2565 {
2566 info->state = PJSIP_INV_STATE_INCOMING;
2567 }
2568 } else if (call->async_call.dlg && call->last_code==0) {
2569 info->state = PJSIP_INV_STATE_NULL;
2570 } else {
2571 info->state = PJSIP_INV_STATE_DISCONNECTED;
2572 }
2573 info->state_text = pj_str((char*)pjsip_inv_state_name(info->state));
2574
2575 /* If call is disconnected, set the last_status from the cause code */
2576 if (call->inv && call->inv->state >= PJSIP_INV_STATE_DISCONNECTED) {
2577 /* last_status, last_status_text */
2578 info->last_status = call->inv->cause;
2579
2580 info->last_status_text.ptr = info->buf_.last_status_text;
2581 pj_strncpy(&info->last_status_text, &call->inv->cause_text,
2582 sizeof(info->buf_.last_status_text));
2583 } else {
2584 /* last_status, last_status_text */
2585 info->last_status = call->last_code;
2586
2587 info->last_status_text.ptr = info->buf_.last_status_text;
2588 pj_strncpy(&info->last_status_text, &call->last_text,
2589 sizeof(info->buf_.last_status_text));
2590 }
2591
2592 /* Audio & video count offered by remote */
2593 info->rem_offerer = call->rem_offerer;
2594 if (call->rem_offerer) {
2595 info->rem_aud_cnt = call->rem_aud_cnt;
2596 info->rem_vid_cnt = call->rem_vid_cnt;
2597 info->rem_txt_cnt = call->rem_txt_cnt;
2598 }
2599
2600 /* Build array of active media info */
2601 info->media_cnt = 0;
2602 for (mi=0; mi < call->med_cnt &&
2603 info->media_cnt < PJ_ARRAY_SIZE(info->media); ++mi)
2604 {
2605 pjsua_call_media *call_med = &call->media[mi];
2606
2607 info->media[info->media_cnt].index = mi;
2608 info->media[info->media_cnt].status = call_med->state;
2609 info->media[info->media_cnt].dir = call_med->dir;
2610 info->media[info->media_cnt].type = call_med->type;
2611
2612 if (call_med->type == PJMEDIA_TYPE_AUDIO) {
2613 info->media[info->media_cnt].stream.aud.conf_slot =
2614 call_med->strm.a.conf_slot;
2615 } else if (call_med->type == PJMEDIA_TYPE_VIDEO) {
2616 pjmedia_vid_dev_index cap_dev = PJMEDIA_VID_INVALID_DEV;
2617
2618 info->media[info->media_cnt].stream.vid.win_in =
2619 call_med->strm.v.rdr_win_id;
2620
2621 info->media[info->media_cnt].stream.vid.dec_slot =
2622 call_med->strm.v.strm_dec_slot;
2623 info->media[info->media_cnt].stream.vid.enc_slot =
2624 call_med->strm.v.strm_enc_slot;
2625
2626 if (call_med->strm.v.cap_win_id != PJSUA_INVALID_ID) {
2627 cap_dev = call_med->strm.v.cap_dev;
2628 }
2629 info->media[info->media_cnt].stream.vid.cap_dev = cap_dev;
2630 } else if (call_med->type == PJMEDIA_TYPE_TEXT) {
2631 } else {
2632 continue;
2633 }
2634 ++info->media_cnt;
2635 }
2636
2637 if (call->audio_idx != -1) {
2638 info->media_status = call->media[call->audio_idx].state;
2639 info->media_dir = call->media[call->audio_idx].dir;
2640 info->conf_slot = call->media[call->audio_idx].strm.a.conf_slot;
2641 } else {
2642 info->media_status = PJSUA_CALL_MEDIA_NONE;
2643 info->media_dir = PJMEDIA_DIR_NONE;
2644 info->conf_slot = PJSUA_INVALID_ID;
2645 }
2646
2647 /* Build array of provisional media info */
2648 info->prov_media_cnt = 0;
2649 for (mi=0; mi < call->med_prov_cnt &&
2650 info->prov_media_cnt < PJ_ARRAY_SIZE(info->prov_media); ++mi)
2651 {
2652 pjsua_call_media *call_med = &call->media_prov[mi];
2653
2654 info->prov_media[info->prov_media_cnt].index = mi;
2655 info->prov_media[info->prov_media_cnt].status = call_med->state;
2656 info->prov_media[info->prov_media_cnt].dir = call_med->dir;
2657 info->prov_media[info->prov_media_cnt].type = call_med->type;
2658 if (call_med->type == PJMEDIA_TYPE_AUDIO) {
2659 info->prov_media[info->prov_media_cnt].stream.aud.conf_slot =
2660 call_med->strm.a.conf_slot;
2661 } else if (call_med->type == PJMEDIA_TYPE_VIDEO) {
2662 pjmedia_vid_dev_index cap_dev = PJMEDIA_VID_INVALID_DEV;
2663
2664 info->prov_media[info->prov_media_cnt].stream.vid.win_in =
2665 call_med->strm.v.rdr_win_id;
2666
2667 if (call_med->strm.v.cap_win_id != PJSUA_INVALID_ID) {
2668 cap_dev = call_med->strm.v.cap_dev;
2669 }
2670 info->prov_media[info->prov_media_cnt].stream.vid.cap_dev=cap_dev;
2671 } else if (call_med->type == PJMEDIA_TYPE_TEXT) {
2672 } else {
2673 continue;
2674 }
2675 ++info->prov_media_cnt;
2676 }
2677
2678 /* calculate duration */
2679 if (info->state >= PJSIP_INV_STATE_DISCONNECTED) {
2680
2681 info->total_duration = call->dis_time;
2682 PJ_TIME_VAL_SUB(info->total_duration, call->start_time);
2683
2684 if (call->conn_time.sec) {
2685 info->connect_duration = call->dis_time;
2686 PJ_TIME_VAL_SUB(info->connect_duration, call->conn_time);
2687 }
2688
2689 } else if (info->state == PJSIP_INV_STATE_CONFIRMED) {
2690
2691 pj_gettimeofday(&info->total_duration);
2692 PJ_TIME_VAL_SUB(info->total_duration, call->start_time);
2693
2694 pj_gettimeofday(&info->connect_duration);
2695 PJ_TIME_VAL_SUB(info->connect_duration, call->conn_time);
2696
2697 } else {
2698 pj_gettimeofday(&info->total_duration);
2699 PJ_TIME_VAL_SUB(info->total_duration, call->start_time);
2700 }
2701
2702 PJSUA_UNLOCK();
2703
2704 return PJ_SUCCESS;
2705 }
2706
2707 /*
2708 * Check if call remote peer support the specified capability.
2709 */
2710 PJ_DEF(pjsip_dialog_cap_status) pjsua_call_remote_has_cap(
2711 pjsua_call_id call_id,
2712 int htype,
2713 const pj_str_t *hname,
2714 const pj_str_t *token)
2715 {
2716 pjsua_call *call;
2717 pjsip_dialog *dlg;
2718 pj_status_t status;
2719 pjsip_dialog_cap_status cap_status;
2720
2721 status = acquire_call("pjsua_call_peer_has_cap()", call_id, &call, &dlg);
2722 if (status != PJ_SUCCESS)
2723 return PJSIP_DIALOG_CAP_UNKNOWN;
2724
2725 cap_status = pjsip_dlg_remote_has_cap(dlg, htype, hname, token);
2726
2727 pjsip_dlg_dec_lock(dlg);
2728
2729 return cap_status;
2730 }
2731
2732
2733 /*
2734 * Attach application specific data to the call.
2735 */
2736 PJ_DEF(pj_status_t) pjsua_call_set_user_data( pjsua_call_id call_id,
2737 void *user_data)
2738 {
2739 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
2740 PJ_EINVAL);
2741 pjsua_var.calls[call_id].user_data = user_data;
2742
2743 return PJ_SUCCESS;
2744 }
2745
2746
2747 /*
2748 * Get user data attached to the call.
2749 */
2750 PJ_DEF(void*) pjsua_call_get_user_data(pjsua_call_id call_id)
2751 {
2752 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
2753 NULL);
2754 return pjsua_var.calls[call_id].user_data;
2755 }
2756
2757
2758 /*
2759 * Get remote's NAT type.
2760 */
2761 PJ_DEF(pj_status_t) pjsua_call_get_rem_nat_type(pjsua_call_id call_id,
2762 pj_stun_nat_type *p_type)
2763 {
2764 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
2765 PJ_EINVAL);
2766 PJ_ASSERT_RETURN(p_type != NULL, PJ_EINVAL);
2767
2768 *p_type = pjsua_var.calls[call_id].rem_nat_type;
2769 return PJ_SUCCESS;
2770 }
2771
2772
2773 /*
2774 * Get media transport info for the specified media index.
2775 */
2776 PJ_DEF(pj_status_t)
2777 pjsua_call_get_med_transport_info(pjsua_call_id call_id,
2778 unsigned med_idx,
2779 pjmedia_transport_info *t)
2780 {
2781 pjsua_call *call;
2782 pjsua_call_media *call_med;
2783 pj_status_t status;
2784
2785 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
2786 PJ_EINVAL);
2787 PJ_ASSERT_RETURN(t, PJ_EINVAL);
2788
2789 PJSUA_LOCK();
2790
2791 call = &pjsua_var.calls[call_id];
2792
2793 if (med_idx >= call->med_cnt) {
2794 PJSUA_UNLOCK();
2795 return PJ_EINVAL;
2796 }
2797
2798 call_med = &call->media[med_idx];
2799
2800 pjmedia_transport_info_init(t);
2801 status = pjmedia_transport_get_info(call_med->tp, t);
2802
2803 PJSUA_UNLOCK();
2804 return status;
2805 }
2806
2807
2808 /* Media channel init callback for pjsua_call_answer(). */
2809 static pj_status_t
2810 on_answer_call_med_tp_complete(pjsua_call_id call_id,
2811 const pjsua_med_tp_state_info *info)
2812 {
2813 pjsua_call *call = &pjsua_var.calls[call_id];
2814 pjmedia_sdp_session *sdp;
2815 int sip_err_code = (info? info->sip_err_code: 0);
2816 pj_status_t status = (info? info->status: PJ_SUCCESS);
2817
2818 PJSUA_LOCK();
2819
2820 if (status != PJ_SUCCESS) {
2821 pjsua_perror(THIS_FILE, "Error initializing media channel", status);
2822 goto on_return;
2823 }
2824
2825 /* pjsua_media_channel_deinit() has been called. */
2826 if (call->async_call.med_ch_deinit) {
2827 pjsua_media_channel_deinit(call->index);
2828 call->med_ch_cb = NULL;
2829 PJSUA_UNLOCK();
2830 return PJ_SUCCESS;
2831 }
2832
2833 status = pjsua_media_channel_create_sdp(call_id,
2834 call->async_call.dlg->pool,
2835 NULL, &sdp, &sip_err_code);
2836 if (status != PJ_SUCCESS) {
2837 pjsua_perror(THIS_FILE, "Error creating SDP answer", status);
2838 goto on_return;
2839 }
2840
2841 status = pjsip_inv_set_local_sdp(call->inv, sdp);
2842 if (status != PJ_SUCCESS) {
2843 pjsua_perror(THIS_FILE, "Error setting local SDP", status);
2844 sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
2845 goto on_return;
2846 }
2847
2848 on_return:
2849 if (status != PJ_SUCCESS) {
2850 /* If the callback is called from pjsua_call_on_incoming(), the
2851 * invite's state is PJSIP_INV_STATE_NULL, so the invite session
2852 * will be terminated later, otherwise we end the session here.
2853 */
2854 if (call->inv->state > PJSIP_INV_STATE_NULL) {
2855 pjsip_tx_data *tdata;
2856 pj_status_t status_;
2857 pj_str_t reason = pj_str("Failed creating media transport");
2858
2859 if (sip_err_code == 0)
2860 sip_err_code = PJSIP_ERRNO_TO_SIP_STATUS(status);
2861
2862 status_ = pjsip_inv_end_session(call->inv, sip_err_code, &reason,
2863 &tdata);
2864 if (status_ == PJ_SUCCESS && tdata)
2865 status_ = pjsip_inv_send_msg(call->inv, tdata);
2866 }
2867
2868 pjsua_media_channel_deinit(call->index);
2869 }
2870
2871 /* Set the callback to NULL to indicate that the async operation
2872 * has completed.
2873 */
2874 call->med_ch_cb = NULL;
2875
2876 /* Finish any pending process */
2877 if (status == PJ_SUCCESS) {
2878 /* Process pending call answers */
2879 process_pending_call_answer(call);
2880 }
2881
2882 PJSUA_UNLOCK();
2883 return status;
2884 }
2885
2886
2887 /*
2888 * Send response to incoming INVITE request.
2889 */
2890 PJ_DEF(pj_status_t) pjsua_call_answer( pjsua_call_id call_id,
2891 unsigned code,
2892 const pj_str_t *reason,
2893 const pjsua_msg_data *msg_data)
2894 {
2895 return pjsua_call_answer2(call_id, NULL, code, reason, msg_data);
2896 }
2897
2898
2899 /*
2900 * Send response to incoming INVITE request.
2901 */
2902 PJ_DEF(pj_status_t) pjsua_call_answer2(pjsua_call_id call_id,
2903 const pjsua_call_setting *opt,
2904 unsigned code,
2905 const pj_str_t *reason,
2906 const pjsua_msg_data *msg_data)
2907 {
2908 pjsua_call *call;
2909 pjsip_dialog *dlg = NULL;
2910 pjsip_tx_data *tdata;
2911 pj_status_t status;
2912
2913 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
2914 PJ_EINVAL);
2915
2916 PJ_LOG(4,(THIS_FILE, "Answering call %d: code=%d", call_id, code));
2917 pj_log_push_indent();
2918
2919 status = acquire_call("pjsua_call_answer()", call_id, &call, &dlg);
2920 if (status != PJ_SUCCESS)
2921 goto on_return;
2922
2923 if (!call->inv->invite_tsx ||
2924 call->inv->invite_tsx->role != PJSIP_ROLE_UAS ||
2925 call->inv->invite_tsx->state >= PJSIP_TSX_STATE_COMPLETED)
2926 {
2927 PJ_LOG(3,(THIS_FILE, "Unable to answer call (no incoming INVITE or "
2928 "already answered)"));
2929 status = PJ_EINVALIDOP;
2930 goto on_return;
2931 }
2932
2933 /* Apply call setting, only if status code is 1xx or 2xx. */
2934 if (opt && code < 300) {
2935 /* Check if it has not been set previously or it is different to
2936 * the previous one.
2937 */
2938 if (!call->opt_inited) {
2939 call->opt_inited = PJ_TRUE;
2940 apply_call_setting(call, opt, NULL);
2941 } else if (pj_memcmp(opt, &call->opt, sizeof(*opt)) != 0) {
2942 /* Warn application about call setting inconsistency */
2943 PJ_LOG(2,(THIS_FILE, "The call setting changes is ignored."));
2944 }
2945 }
2946
2947 PJSUA_LOCK();
2948
2949 /* Ticket #1526: When the incoming call contains no SDP offer, the media
2950 * channel may have not been initialized at this stage. The media channel
2951 * will be initialized here (along with SDP local offer generation) when
2952 * the following conditions are met:
2953 * - no pending media channel init
2954 * - local SDP has not been generated
2955 * - call setting has just been set, or SDP offer needs to be sent, i.e:
2956 * answer code 183 or 2xx is issued
2957 */
2958 if (!call->med_ch_cb &&
2959 (call->opt_inited || (code==183 || code/100==2)) &&
2960 (!call->inv->neg ||
2961 pjmedia_sdp_neg_get_state(call->inv->neg) ==
2962 PJMEDIA_SDP_NEG_STATE_NULL))
2963 {
2964 /* Mark call setting as initialized as it is just about to be used
2965 * for initializing the media channel.
2966 */
2967 call->opt_inited = PJ_TRUE;
2968
2969 status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC,
2970 call->secure_level,
2971 dlg->pool,
2972 NULL, NULL, PJ_TRUE,
2973 &on_answer_call_med_tp_complete);
2974 if (status == PJ_SUCCESS) {
2975 status = on_answer_call_med_tp_complete(call->index, NULL);
2976 if (status != PJ_SUCCESS) {
2977 PJSUA_UNLOCK();
2978 goto on_return;
2979 }
2980 } else if (status != PJ_EPENDING) {
2981 PJSUA_UNLOCK();
2982 pjsua_perror(THIS_FILE, "Error initializing media channel", status);
2983 goto on_return;
2984 }
2985 }
2986
2987 /* If media transport creation is not yet completed, we will answer
2988 * the call in the media transport creation callback instead.
2989 * Or if initial answer is not sent yet, we will answer the call after
2990 * initial answer is sent (see #1923).
2991 */
2992 if (call->med_ch_cb || !call->inv->last_answer) {
2993 struct call_answer *answer;
2994
2995 PJ_LOG(4,(THIS_FILE, "Pending answering call %d upon completion "
2996 "of media transport", call_id));
2997
2998 answer = PJ_POOL_ZALLOC_T(call->inv->pool_prov, struct call_answer);
2999 answer->code = code;
3000 if (opt) {
3001 answer->opt = PJ_POOL_ZALLOC_T(call->inv->pool_prov,
3002 pjsua_call_setting);
3003 *answer->opt = *opt;
3004 }
3005 if (reason) {
3006 answer->reason = PJ_POOL_ZALLOC_T(call->inv->pool_prov, pj_str_t);
3007 pj_strdup(call->inv->pool_prov, answer->reason, reason);
3008 }
3009 if (msg_data) {
3010 answer->msg_data = pjsua_msg_data_clone(call->inv->pool_prov,
3011 msg_data);
3012 }
3013 pj_list_push_back(&call->async_call.call_var.inc_call.answers,
3014 answer);
3015
3016 PJSUA_UNLOCK();
3017 if (dlg) pjsip_dlg_dec_lock(dlg);
3018 pj_log_pop_indent();
3019 return status;
3020 }
3021
3022 PJSUA_UNLOCK();
3023
3024 if (call->res_time.sec == 0)
3025 pj_gettimeofday(&call->res_time);
3026
3027 if (reason && reason->slen == 0)
3028 reason = NULL;
3029
3030 /* Create response message */
3031 status = pjsip_inv_answer(call->inv, code, reason, NULL, &tdata);
3032 if (status != PJ_SUCCESS) {
3033 pjsua_perror(THIS_FILE, "Error creating response",
3034 status);
3035 goto on_return;
3036 }
3037
3038 /* Call might have been disconnected if application is answering with
3039 * 200/OK and the media failed to start.
3040 */
3041 if (call->inv == NULL)
3042 goto on_return;
3043
3044 /* Add additional headers etc */
3045 pjsua_process_msg_data( tdata, msg_data);
3046
3047 /* Send the message */
3048 status = pjsip_inv_send_msg(call->inv, tdata);
3049 if (status != PJ_SUCCESS)
3050 pjsua_perror(THIS_FILE, "Error sending response",
3051 status);
3052
3053 on_return:
3054 if (dlg) pjsip_dlg_dec_lock(dlg);
3055 pj_log_pop_indent();
3056 return status;
3057 }
3058
3059
3060 /*
3061 * Send response to incoming INVITE request.
3062 */
3063 PJ_DEF(pj_status_t)
3064 pjsua_call_answer_with_sdp(pjsua_call_id call_id,
3065 const pjmedia_sdp_session *sdp,
3066 const pjsua_call_setting *opt,
3067 unsigned code,
3068 const pj_str_t *reason,
3069 const pjsua_msg_data *msg_data)
3070 {
3071 pjsua_call *call;
3072 pjsip_dialog *dlg = NULL;
3073 pj_status_t status;
3074
3075 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
3076 PJ_EINVAL);
3077
3078 status = acquire_call("pjsua_call_answer_with_sdp()",
3079 call_id, &call, &dlg);
3080 if (status != PJ_SUCCESS)
3081 return status;
3082
3083 status = pjsip_inv_set_sdp_answer(call->inv, sdp);
3084
3085 pjsip_dlg_dec_lock(dlg);
3086
3087 if (status != PJ_SUCCESS)
3088 return status;
3089
3090 return pjsua_call_answer2(call_id, opt, code, reason, msg_data);
3091 }
3092
3093
3094 static pj_status_t call_inv_end_session(pjsua_call *call,
3095 unsigned code,
3096 const pj_str_t *reason,
3097 const pjsua_msg_data *msg_data)
3098 {
3099 pjsip_tx_data *tdata;
3100 pj_status_t status;
3101
3102 if (code==0) {
3103 if (call->inv->state == PJSIP_INV_STATE_CONFIRMED)
3104 code = PJSIP_SC_OK;
3105 else if (call->inv->role == PJSIP_ROLE_UAS)
3106 code = PJSIP_SC_DECLINE;
3107 else
3108 code = PJSIP_SC_REQUEST_TERMINATED;
3109 }
3110
3111 /* Stop hangup timer, if it is active. */
3112 if (call->hangup_timer.id) {
3113 pjsua_cancel_timer(&call->hangup_timer);
3114 call->hangup_timer.id = PJ_FALSE;
3115 }
3116
3117 status = pjsip_inv_end_session(call->inv, code, reason, &tdata);
3118 if (status != PJ_SUCCESS) {
3119 pjsua_perror(THIS_FILE,
3120 "Failed to create end session message",
3121 status);
3122 goto on_return;
3123 }
3124
3125 /* pjsip_inv_end_session may return PJ_SUCCESS with NULL
3126 * as p_tdata when INVITE transaction has not been answered
3127 * with any provisional responses.
3128 */
3129 if (tdata == NULL) {
3130 goto on_return;
3131 }
3132
3133 /* Add additional headers etc */
3134 pjsua_process_msg_data( tdata, msg_data);
3135
3136 /* Send the message */
3137 status = pjsip_inv_send_msg(call->inv, tdata);
3138 if (status != PJ_SUCCESS) {
3139 pjsua_perror(THIS_FILE,
3140 "Failed to send end session message",
3141 status);
3142 goto on_return;
3143 }
3144
3145 on_return:
3146 /* Failure in pjsip_inv_send_msg() can cause
3147 * pjsua_call_on_state_changed() to be called and call to be reset,
3148 * so we need to check for call->inv as well.
3149 */
3150 if (status != PJ_SUCCESS && status != PJSIP_ESESSIONTERMINATED &&
3151 call->inv)
3152 {
3153 pj_time_val delay;
3154
3155 /* Schedule a retry */
3156 if (call->hangup_retry >= CALL_HANGUP_MAX_RETRY) {
3157 /* Forcefully terminate the invite session. */
3158 PJ_LOG(1,(THIS_FILE,"Call %d: failed to hangup after %d retries, "
3159 "terminating the session forcefully now!",
3160 call->index, call->hangup_retry));
3161 pjsip_inv_terminate(call->inv, call->hangup_code, PJ_TRUE);
3162 return PJ_SUCCESS;
3163 }
3164
3165 if (call->hangup_retry == 0) {
3166 pj_timer_entry_init(&call->hangup_timer, PJ_FALSE,
3167 (void*)call, &hangup_timer_cb);
3168
3169 call->hangup_code = code;
3170 if (reason) {
3171 pj_strdup(call->inv->pool_prov, &call->hangup_reason,
3172 reason);
3173 }
3174 if (msg_data) {
3175 call->hangup_msg_data = pjsua_msg_data_clone(
3176 call->inv->pool_prov,
3177 msg_data);
3178 }
3179 }
3180
3181 delay.sec = 0;
3182 delay.msec = CALL_HANGUP_RETRY_INTERVAL;
3183 pj_time_val_normalize(&delay);
3184 call->hangup_timer.id = PJ_TRUE;
3185 pjsua_schedule_timer(&call->hangup_timer, &delay);
3186 call->hangup_retry++;
3187
3188 PJ_LOG(4, (THIS_FILE, "Will retry call %d hangup in %d msec",
3189 call->index, CALL_HANGUP_RETRY_INTERVAL));
3190 }
3191
3192 return PJ_SUCCESS;
3193 }
3194
3195 /* Timer callback to hangup call */
3196 static void hangup_timer_cb(pj_timer_heap_t *th, pj_timer_entry *entry)
3197 {
3198 pjsua_call* call = (pjsua_call *)entry->user_data;
3199 pjsip_dialog *dlg;
3200 pj_status_t status;
3201
3202 PJ_UNUSED_ARG(th);
3203
3204 pj_log_push_indent();
3205
3206 status = acquire_call("hangup_timer_cb()", call->index, &call, &dlg);
3207 if (status != PJ_SUCCESS) {
3208 pj_log_pop_indent();
3209 return;
3210 }
3211
3212 call->hangup_timer.id = PJ_FALSE;
3213 call_inv_end_session(call, call->hangup_code, &call->hangup_reason,
3214 call->hangup_msg_data);
3215
3216 pjsip_dlg_dec_lock(dlg);
3217
3218 pj_log_pop_indent();
3219 }
3220
3221 /*
3222 * Hangup call by using method that is appropriate according to the
3223 * call state.
3224 */
3225 PJ_DEF(pj_status_t) pjsua_call_hangup(pjsua_call_id call_id,
3226 unsigned code,
3227 const pj_str_t *reason,
3228 const pjsua_msg_data *msg_data)
3229 {
3230 pjsua_call *call;
3231 pjsip_dialog *dlg = NULL;
3232 pj_status_t status;
3233
3234 if (call_id<0 || call_id>=(int)pjsua_var.ua_cfg.max_calls) {
3235 PJ_LOG(1,(THIS_FILE, "pjsua_call_hangup(): invalid call id %d",
3236 call_id));
3237 }
3238
3239 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
3240 PJ_EINVAL);
3241
3242 PJ_LOG(4,(THIS_FILE, "Call %d hanging up: code=%d..", call_id, code));
3243 pj_log_push_indent();
3244
3245 status = acquire_call("pjsua_call_hangup()", call_id, &call, &dlg);
3246 if (status != PJ_SUCCESS)
3247 goto on_return;
3248
3249 if (!call->hanging_up) {
3250 pj_bool_t delay_hangup = PJ_FALSE;
3251 pjsip_event user_event;
3252
3253 pj_gettimeofday(&call->dis_time);
3254 if (call->res_time.sec == 0)
3255 pj_gettimeofday(&call->res_time);
3256
3257 if (code==0) {
3258 if (call->inv && call->inv->state == PJSIP_INV_STATE_CONFIRMED)
3259 code = PJSIP_SC_OK;
3260 else if (call->inv && call->inv->role == PJSIP_ROLE_UAS)
3261 code = PJSIP_SC_DECLINE;
3262 else
3263 code = PJSIP_SC_REQUEST_TERMINATED;
3264 }
3265
3266 call->last_code = code;
3267 pj_strncpy(&call->last_text,
3268 pjsip_get_status_text(call->last_code),
3269 sizeof(call->last_text_buf_));
3270
3271 /* Stop reinvite timer, if it is active. */
3272 if (call->reinv_timer.id) {
3273 pjsua_cancel_timer(&call->reinv_timer);
3274 call->reinv_timer.id = PJ_FALSE;
3275 }
3276
3277 /* If media transport creation is not yet completed, we will continue
3278 * from the media transport creation callback instead.
3279 */
3280 if ((call->med_ch_cb && !call->inv) ||
3281 ((call->inv != NULL) &&
3282 (call->inv->state == PJSIP_INV_STATE_NULL)))
3283 {
3284 delay_hangup = PJ_TRUE;
3285 PJ_LOG(4,(THIS_FILE, "Will continue call %d hangup upon "
3286 "completion of media transport", call_id));
3287
3288 if (call->inv && call->inv->role == PJSIP_ROLE_UAS)
3289 call->async_call.call_var.inc_call.hangup = PJ_TRUE;
3290 else
3291 call->async_call.call_var.out_call.hangup = PJ_TRUE;
3292
3293 if (reason) {
3294 pj_strncpy(&call->last_text, reason,
3295 sizeof(call->last_text_buf_));
3296 }
3297
3298 call->hanging_up = PJ_TRUE;
3299 } else {
3300 /* Destroy media session. */
3301 pjsua_media_channel_deinit(call_id);
3302 call->hanging_up = PJ_TRUE;
3303 pjsua_check_snd_dev_idle();
3304 }
3305
3306 /* Call callback which will report DISCONNECTED state.
3307 * Use user event rather than NULL to avoid crash in
3308 * unsuspecting app.
3309 */
3310 PJSIP_EVENT_INIT_USER(user_event, 0, 0, 0, 0);
3311 if (pjsua_var.ua_cfg.cb.on_call_state) {
3312 (*pjsua_var.ua_cfg.cb.on_call_state)(call->index,
3313 &user_event);
3314 }
3315
3316 if (call->inv && !delay_hangup) {
3317 call_inv_end_session(call, code, reason, msg_data);
3318 }
3319 } else {
3320 /* Already requested and on progress */
3321 PJ_LOG(4,(THIS_FILE, "Call %d hangup request ignored as "
3322 "it is on progress", call_id));
3323 }
3324
3325 on_return:
3326 if (dlg) pjsip_dlg_dec_lock(dlg);
3327 pj_log_pop_indent();
3328 return status;
3329 }
3330
3331
3332 /*
3333 * Accept or reject redirection.
3334 */
3335 PJ_DEF(pj_status_t) pjsua_call_process_redirect( pjsua_call_id call_id,
3336 pjsip_redirect_op cmd)
3337 {
3338 pjsua_call *call;
3339 pjsip_dialog *dlg;
3340 pj_status_t status;
3341
3342 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
3343 PJ_EINVAL);
3344
3345 status = acquire_call("pjsua_call_process_redirect()", call_id,
3346 &call, &dlg);
3347 if (status != PJ_SUCCESS)
3348 return status;
3349
3350 status = pjsip_inv_process_redirect(call->inv, cmd, NULL);
3351
3352 pjsip_dlg_dec_lock(dlg);
3353
3354 return status;
3355 }
3356
3357
3358 /*
3359 * Put the specified call on hold.
3360 */
3361 PJ_DEF(pj_status_t) pjsua_call_set_hold(pjsua_call_id call_id,
3362 const pjsua_msg_data *msg_data)
3363 {
3364 return pjsua_call_set_hold2(call_id, 0, msg_data);
3365 }
3366
3367 PJ_DEF(pj_status_t) pjsua_call_set_hold2(pjsua_call_id call_id,
3368 unsigned options,
3369 const pjsua_msg_data *msg_data)
3370 {
3371 pjmedia_sdp_session *sdp;
3372 pjsua_call *call;
3373 pjsip_dialog *dlg = NULL;
3374 pjsip_tx_data *tdata;
3375 pj_str_t *new_contact = NULL;
3376 pj_status_t status;
3377
3378 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
3379 PJ_EINVAL);
3380
3381 PJ_LOG(4,(THIS_FILE, "Putting call %d on hold", call_id));
3382 pj_log_push_indent();
3383
3384 status = acquire_call("pjsua_call_set_hold()", call_id, &call, &dlg);
3385 if (status != PJ_SUCCESS)
3386 goto on_return;
3387
3388 if (call->inv->state != PJSIP_INV_STATE_CONFIRMED) {
3389 PJ_LOG(3,(THIS_FILE, "Can not hold call that is not confirmed"));
3390 status = PJSIP_ESESSIONSTATE;
3391 goto on_return;
3392 }
3393
3394 /* We may need to re-initialize media before creating SDP */
3395 if (call->med_prov_cnt == 0) {
3396 status = apply_call_setting(call, &call->opt, NULL);
3397 if (status != PJ_SUCCESS)
3398 goto on_return;
3399 }
3400
3401 status = create_sdp_of_call_hold(call, &sdp);
3402 if (status != PJ_SUCCESS)
3403 goto on_return;
3404
3405 if ((options & PJSUA_CALL_UPDATE_CONTACT) &&
3406 pjsua_acc_is_valid(call->acc_id))
3407 {
3408 call_update_contact(call, &new_contact);
3409 }
3410
3411 if ((options & PJSUA_CALL_UPDATE_VIA) &&
3412 pjsua_acc_is_valid(call->acc_id))
3413 {
3414 dlg_set_via(call->inv->dlg, &pjsua_var.acc[call->acc_id]);
3415 }
3416
3417 if ((call->opt.flag & PJSUA_CALL_UPDATE_TARGET) &&
3418 msg_data && msg_data->target_uri.slen)
3419 {
3420 status = dlg_set_target(dlg, &msg_data->target_uri);
3421 if (status != PJ_SUCCESS) {
3422 pjsua_perror(THIS_FILE, "Unable to set new target", status);
3423 goto on_return;
3424 }
3425 }
3426
3427 /* Create re-INVITE with new offer */
3428 status = pjsip_inv_reinvite( call->inv, new_contact, sdp, &tdata);
3429 if (status != PJ_SUCCESS) {
3430 pjsua_perror(THIS_FILE, "Unable to create re-INVITE", status);
3431 goto on_return;
3432 }
3433
3434 /* Add additional headers etc */
3435 pjsua_process_msg_data( tdata, msg_data);
3436
3437 /* Record the tx_data to keep track the operation */
3438 call->hold_msg = (void*) tdata;
3439
3440 /* Send the request */
3441 status = pjsip_inv_send_msg( call->inv, tdata);
3442 if (status != PJ_SUCCESS) {
3443 pjsua_perror(THIS_FILE, "Unable to send re-INVITE", status);
3444 call->hold_msg = NULL;
3445 goto on_return;
3446 }
3447
3448 /* Set flag that local put the call on hold */
3449 call->local_hold = PJ_TRUE;
3450
3451 /* Clear unhold flag */
3452 call->opt.flag &= ~PJSUA_CALL_UNHOLD;
3453
3454 on_return:
3455 if (dlg) pjsip_dlg_dec_lock(dlg);
3456 pj_log_pop_indent();
3457 return status;
3458 }
3459
3460
3461 /*
3462 * Send re-INVITE (to release hold).
3463 */
3464 PJ_DEF(pj_status_t) pjsua_call_reinvite( pjsua_call_id call_id,
3465 unsigned options,
3466 const pjsua_msg_data *msg_data)
3467 {
3468 pjsua_call *call;
3469 pjsip_dialog *dlg = NULL;
3470 pj_status_t status;
3471
3472 status = acquire_call("pjsua_call_reinvite()", call_id, &call, &dlg);
3473 if (status != PJ_SUCCESS)
3474 goto on_return;
3475
3476 if (options != call->opt.flag)
3477 call->opt.flag = options;
3478
3479 status = pjsua_call_reinvite2(call_id, &call->opt, msg_data);
3480
3481 on_return:
3482 if (dlg) pjsip_dlg_dec_lock(dlg);
3483 return status;
3484 }
3485
3486
3487 /*
3488 * Send re-INVITE (to release hold).
3489 */
3490 PJ_DEF(pj_status_t) pjsua_call_reinvite2(pjsua_call_id call_id,
3491 const pjsua_call_setting *opt,
3492 const pjsua_msg_data *msg_data)
3493 {
3494 pjmedia_sdp_session *sdp = NULL;
3495 pj_str_t *new_contact = NULL;
3496 pjsip_tx_data *tdata;
3497 pjsua_call *call;
3498 pjsip_dialog *dlg = NULL;
3499 pj_status_t status;
3500
3501
3502 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
3503 PJ_EINVAL);
3504
3505 PJ_LOG(4,(THIS_FILE, "Sending re-INVITE on call %d", call_id));
3506 pj_log_push_indent();
3507
3508 status = acquire_call("pjsua_call_reinvite2()", call_id, &call, &dlg);
3509 if (status != PJ_SUCCESS)
3510 goto on_return;
3511
3512 if (pjsua_call_media_is_changing(call)) {
3513 PJ_LOG(1,(THIS_FILE, "Unable to reinvite" ERR_MEDIA_CHANGING));
3514 status = PJ_EINVALIDOP;
3515 goto on_return;
3516 }
3517
3518 if (call->inv->state != PJSIP_INV_STATE_CONFIRMED) {
3519 PJ_LOG(3,(THIS_FILE, "Can not re-INVITE call that is not confirmed"));
3520 status = PJSIP_ESESSIONSTATE;
3521 goto on_return;
3522 }
3523
3524 status = apply_call_setting(call, opt, NULL);
3525 if (status != PJ_SUCCESS) {
3526 pjsua_perror(THIS_FILE, "Failed to apply call setting", status);
3527 goto on_return;
3528 }
3529
3530 /* Create SDP */
3531 if (call->local_hold && (call->opt.flag & PJSUA_CALL_UNHOLD)==0) {
3532 status = create_sdp_of_call_hold(call, &sdp);
3533 } else if ((call->opt.flag & PJSUA_CALL_NO_SDP_OFFER) == 0) {
3534 status = pjsua_media_channel_create_sdp(call->index,
3535 call->inv->pool_prov,
3536 NULL, &sdp, NULL);
3537 }
3538 if (status != PJ_SUCCESS) {
3539 pjsua_perror(THIS_FILE, "Unable to get SDP from media endpoint",
3540 status);
3541 goto on_return;
3542 }
3543
3544 if ((call->opt.flag & PJSUA_CALL_UPDATE_CONTACT) &&
3545 pjsua_acc_is_valid(call->acc_id))
3546 {
3547 call_update_contact(call, &new_contact);
3548 }
3549
3550 if ((call->opt.flag & PJSUA_CALL_UPDATE_VIA) &&
3551 pjsua_acc_is_valid(call->acc_id))
3552 {
3553 dlg_set_via(call->inv->dlg, &pjsua_var.acc[call->acc_id]);
3554 }
3555
3556 if ((call->opt.flag & PJSUA_CALL_UPDATE_TARGET) &&
3557 msg_data && msg_data->target_uri.slen)
3558 {
3559 status = dlg_set_target(dlg, &msg_data->target_uri);
3560 if (status != PJ_SUCCESS) {
3561 pjsua_perror(THIS_FILE, "Unable to set new target", status);
3562 goto on_return;
3563 }
3564 }
3565
3566 /* Create re-INVITE with new offer */
3567 status = pjsip_inv_reinvite( call->inv, new_contact, sdp, &tdata);
3568 if (status != PJ_SUCCESS) {
3569 pjsua_perror(THIS_FILE, "Unable to create re-INVITE", status);
3570 goto on_return;
3571 }
3572
3573 /* Add additional headers etc */
3574 pjsua_process_msg_data( tdata, msg_data);
3575
3576 /* Send the request */
3577 call->med_update_success = PJ_FALSE;
3578 status = pjsip_inv_send_msg( call->inv, tdata);
3579 if (status == PJ_SUCCESS &&
3580 ((call->opt.flag & PJSUA_CALL_UNHOLD) &&
3581 (call->opt.flag & PJSUA_CALL_NO_SDP_OFFER) == 0))
3582 {
3583 call->local_hold = PJ_FALSE;
3584 } else if (status != PJ_SUCCESS) {
3585 pjsua_perror(THIS_FILE, "Unable to send re-INVITE", status);
3586 goto on_return;
3587 }
3588
3589 on_return:
3590 if (dlg) pjsip_dlg_dec_lock(dlg);
3591 pj_log_pop_indent();
3592 return status;
3593 }
3594
3595
3596 /*
3597 * Send UPDATE request.
3598 */
3599 PJ_DEF(pj_status_t) pjsua_call_update( pjsua_call_id call_id,
3600 unsigned options,
3601 const pjsua_msg_data *msg_data)
3602 {
3603 pjsua_call *call;
3604 pjsip_dialog *dlg = NULL;
3605 pj_status_t status;
3606
3607 status = acquire_call("pjsua_call_update()", call_id, &call, &dlg);
3608 if (status != PJ_SUCCESS)
3609 goto on_return;
3610
3611 if (options != call->opt.flag)
3612 call->opt.flag = options;
3613
3614 status = pjsua_call_update2(call_id, &call->opt, msg_data);
3615
3616 on_return:
3617 if (dlg) pjsip_dlg_dec_lock(dlg);
3618 return status;
3619 }
3620
3621
3622 /*
3623 * Send UPDATE request.
3624 */
3625 PJ_DEF(pj_status_t) pjsua_call_update2(pjsua_call_id call_id,
3626 const pjsua_call_setting *opt,
3627 const pjsua_msg_data *msg_data)
3628 {
3629 pjmedia_sdp_session *sdp = NULL;
3630 pj_str_t *new_contact = NULL;
3631 pjsip_tx_data *tdata;
3632 pjsua_call *call;
3633 pjsip_dialog *dlg = NULL;
3634 pj_status_t status;
3635
3636 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
3637 PJ_EINVAL);
3638
3639 PJ_LOG(4,(THIS_FILE, "Sending UPDATE on call %d", call_id));
3640 pj_log_push_indent();
3641
3642 status = acquire_call("pjsua_call_update2()", call_id, &call, &dlg);
3643 if (status != PJ_SUCCESS)
3644 goto on_return;
3645
3646 /* Don't check media changing if UPDATE is sent without SDP */
3647 if (pjsua_call_media_is_changing(call) &&
3648 (opt && opt->flag & PJSUA_CALL_NO_SDP_OFFER) == 0)
3649 {
3650 PJ_LOG(1,(THIS_FILE, "Unable to send UPDATE" ERR_MEDIA_CHANGING));
3651 status = PJ_EINVALIDOP;
3652 goto on_return;
3653 }
3654
3655 status = apply_call_setting(call, opt, NULL);
3656 if (status != PJ_SUCCESS) {
3657 pjsua_perror(THIS_FILE, "Failed to apply call setting", status);
3658 goto on_return;
3659 }
3660
3661 /* Create SDP */
3662 if (call->local_hold && (call->opt.flag & PJSUA_CALL_UNHOLD)==0) {
3663 status = create_sdp_of_call_hold(call, &sdp);
3664 } else if ((call->opt.flag & PJSUA_CALL_NO_SDP_OFFER) == 0) {
3665 status = pjsua_media_channel_create_sdp(call->index,
3666 call->inv->pool_prov,
3667 NULL, &sdp, NULL);
3668 }
3669
3670 if (status != PJ_SUCCESS) {
3671 pjsua_perror(THIS_FILE, "Unable to get SDP from media endpoint",
3672 status);
3673 goto on_return;
3674 }
3675
3676 if ((call->opt.flag & PJSUA_CALL_UPDATE_CONTACT) &&
3677 pjsua_acc_is_valid(call->acc_id))
3678 {
3679 call_update_contact(call, &new_contact);
3680 }
3681
3682 if ((call->opt.flag & PJSUA_CALL_UPDATE_VIA) &&
3683 pjsua_acc_is_valid(call->acc_id))
3684 {
3685 dlg_set_via(call->inv->dlg, &pjsua_var.acc[call->acc_id]);
3686 }
3687
3688 if ((call->opt.flag & PJSUA_CALL_UPDATE_TARGET) &&
3689 msg_data && msg_data->target_uri.slen)
3690 {
3691 status = dlg_set_target(dlg, &msg_data->target_uri);
3692 if (status != PJ_SUCCESS) {
3693 pjsua_perror(THIS_FILE, "Unable to set new target", status);
3694 goto on_return;
3695 }
3696 }
3697
3698 /* Create UPDATE with new offer */
3699 status = pjsip_inv_update(call->inv, new_contact, sdp, &tdata);
3700 if (status != PJ_SUCCESS) {
3701 pjsua_perror(THIS_FILE, "Unable to create UPDATE request", status);
3702 goto on_return;
3703 }
3704
3705 /* Add additional headers etc */
3706 pjsua_process_msg_data( tdata, msg_data);
3707
3708 /* Send the request */
3709 call->med_update_success = PJ_FALSE;
3710 status = pjsip_inv_send_msg( call->inv, tdata);
3711 if (status == PJ_SUCCESS &&
3712 ((call->opt.flag & PJSUA_CALL_UNHOLD) &&
3713 (call->opt.flag & PJSUA_CALL_NO_SDP_OFFER) == 0))
3714 {
3715 call->local_hold = PJ_FALSE;
3716 } else if (status != PJ_SUCCESS) {
3717 pjsua_perror(THIS_FILE, "Unable to send UPDATE request", status);
3718 goto on_return;
3719 }
3720
3721 on_return:
3722 if (dlg) pjsip_dlg_dec_lock(dlg);
3723 pj_log_pop_indent();
3724 return status;
3725 }
3726
3727
3728 /*
3729 * Initiate call transfer to the specified address.
3730 */
3731 PJ_DEF(pj_status_t) pjsua_call_xfer( pjsua_call_id call_id,
3732 const pj_str_t *dest,
3733 const pjsua_msg_data *msg_data)
3734 {
3735 pjsip_evsub *sub;
3736 pjsip_tx_data *tdata;
3737 pjsua_call *call;
3738 pjsip_dialog *dlg = NULL;
3739 pjsip_generic_string_hdr *gs_hdr;
3740 const pj_str_t str_ref_by = { "Referred-By", 11 };
3741 struct pjsip_evsub_user xfer_cb;
3742 pj_status_t status;
3743
3744
3745 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls &&
3746 dest, PJ_EINVAL);
3747
3748 PJ_LOG(4,(THIS_FILE, "Transferring call %d to %.*s", call_id,
3749 (int)dest->slen, dest->ptr));
3750 pj_log_push_indent();
3751
3752 status = acquire_call("pjsua_call_xfer()", call_id, &call, &dlg);
3753 if (status != PJ_SUCCESS)
3754 goto on_return;
3755
3756 /* Create xfer client subscription. */
3757 pj_bzero(&xfer_cb, sizeof(xfer_cb));
3758 xfer_cb.on_evsub_state = &xfer_client_on_evsub_state;
3759
3760 status = pjsip_xfer_create_uac(call->inv->dlg, &xfer_cb, &sub);
3761 if (status != PJ_SUCCESS) {
3762 pjsua_perror(THIS_FILE, "Unable to create xfer", status);
3763 goto on_return;
3764 }
3765
3766 /* Associate this call with the client subscription */
3767 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, call);
3768
3769 /*
3770 * Create REFER request.
3771 */
3772 status = pjsip_xfer_initiate(sub, dest, &tdata);
3773 if (status != PJ_SUCCESS) {
3774 pjsua_perror(THIS_FILE, "Unable to create REFER request", status);
3775 goto on_return;
3776 }
3777
3778 /* Add Referred-By header */
3779 gs_hdr = pjsip_generic_string_hdr_create(tdata->pool, &str_ref_by,
3780 &dlg->local.info_str);
3781 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)gs_hdr);
3782
3783
3784 /* Add additional headers etc */
3785 pjsua_process_msg_data( tdata, msg_data);
3786
3787 /* Send. */
3788 status = pjsip_xfer_send_request(sub, tdata);
3789 if (status != PJ_SUCCESS) {
3790 pjsua_perror(THIS_FILE, "Unable to send REFER request", status);
3791 goto on_return;
3792 }
3793
3794 /* For simplicity (that's what this program is intended to be!),
3795 * leave the original invite session as it is. More advanced application
3796 * may want to hold the INVITE, or terminate the invite, or whatever.
3797 */
3798 on_return:
3799 if (dlg) pjsip_dlg_dec_lock(dlg);
3800 pj_log_pop_indent();
3801 return status;
3802
3803 }
3804
3805
3806 /*
3807 * Initiate attended call transfer to the specified address.
3808 */
3809 PJ_DEF(pj_status_t) pjsua_call_xfer_replaces( pjsua_call_id call_id,
3810 pjsua_call_id dest_call_id,
3811 unsigned options,
3812 const pjsua_msg_data *msg_data)
3813 {
3814 pjsua_call *dest_call;
3815 pjsip_dialog *dest_dlg;
3816 char str_dest_buf[PJSIP_MAX_URL_SIZE*2];
3817 pj_str_t str_dest;
3818 int len;
3819 char call_id_dest_buf[PJSIP_MAX_URL_SIZE * 2];
3820 int call_id_len;
3821 pjsip_uri *uri;
3822 pj_status_t status;
3823 const pjsip_parser_const_t *pconst;
3824
3825
3826 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
3827 PJ_EINVAL);
3828 PJ_ASSERT_RETURN(dest_call_id>=0 &&
3829 dest_call_id<(int)pjsua_var.ua_cfg.max_calls,
3830 PJ_EINVAL);
3831
3832 PJ_LOG(4,(THIS_FILE, "Transferring call %d replacing with call %d",
3833 call_id, dest_call_id));
3834 pj_log_push_indent();
3835
3836 status = acquire_call("pjsua_call_xfer_replaces()", dest_call_id,
3837 &dest_call, &dest_dlg);
3838 if (status != PJ_SUCCESS) {
3839 pj_log_pop_indent();
3840 return status;
3841 }
3842
3843 /*
3844 * Create REFER destination URI with Replaces field.
3845 */
3846
3847 /* Make sure we have sufficient buffer's length */
3848 PJ_ASSERT_ON_FAIL(dest_dlg->remote.info_str.slen +
3849 dest_dlg->call_id->id.slen +
3850 dest_dlg->remote.info->tag.slen +
3851 dest_dlg->local.info->tag.slen + 32
3852 < (long)sizeof(str_dest_buf),
3853 { status=PJSIP_EURITOOLONG; goto on_error; });
3854
3855 /* Print URI */
3856 str_dest_buf[0] = '<';
3857 str_dest.slen = 1;
3858
3859 uri = (pjsip_uri*) pjsip_uri_get_uri(dest_dlg->remote.info->uri);
3860 len = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, uri,
3861 str_dest_buf+1, sizeof(str_dest_buf)-1);
3862 if (len < 0) {
3863 status = PJSIP_EURITOOLONG;
3864 goto on_error;
3865 }
3866
3867 str_dest.slen += len;
3868
3869 /* This uses the the same scanner definition used for SIP parsing
3870 * to escape the call-id in the refer.
3871 *
3872 * A common pattern for call-ids is: name@domain. The '@' character,
3873 * when used in a URL parameter, throws off many SIP parsers.
3874 * URL escape it based off of the allowed characters for header values.
3875 */
3876 pconst = pjsip_parser_const();
3877 call_id_len = (int)pj_strncpy2_escape(call_id_dest_buf, &dest_dlg->call_id->id,
3878 PJ_ARRAY_SIZE(call_id_dest_buf),
3879 &pconst->pjsip_HDR_CHAR_SPEC);
3880 if (call_id_len < 0) {
3881 status = PJSIP_EURITOOLONG;
3882 goto on_error;
3883 }
3884
3885 /* Build the URI */
3886 len = pj_ansi_snprintf(str_dest_buf + str_dest.slen,
3887 sizeof(str_dest_buf) - str_dest.slen,
3888 "?%s"
3889 "Replaces=%.*s"
3890 "%%3Bto-tag%%3D%.*s"
3891 "%%3Bfrom-tag%%3D%.*s>",
3892 ((options&PJSUA_XFER_NO_REQUIRE_REPLACES) ?
3893 "" : "Require=replaces&"),
3894 call_id_len,
3895 call_id_dest_buf,
3896 (int)dest_dlg->remote.info->tag.slen,
3897 dest_dlg->remote.info->tag.ptr,
3898 (int)dest_dlg->local.info->tag.slen,
3899 dest_dlg->local.info->tag.ptr);
3900
3901 PJ_ASSERT_ON_FAIL(len > 0 && len <= (int)sizeof(str_dest_buf)-str_dest.slen,
3902 { status=PJSIP_EURITOOLONG; goto on_error; });
3903
3904 str_dest.ptr = str_dest_buf;
3905 str_dest.slen += len;
3906
3907 pjsip_dlg_dec_lock(dest_dlg);
3908
3909 status = pjsua_call_xfer(call_id, &str_dest, msg_data);
3910
3911 pj_log_pop_indent();
3912 return status;
3913
3914 on_error:
3915 pjsip_dlg_dec_lock(dest_dlg);
3916 pj_log_pop_indent();
3917 return status;
3918 }
3919
3920 /*
3921 * Send DTMF digits to remote.
3922 */
3923 PJ_DEF(pj_status_t) pjsua_call_send_dtmf(pjsua_call_id call_id,
3924 const pjsua_call_send_dtmf_param *param)
3925 {
3926 pj_status_t status = PJ_EINVAL;
3927
3928 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls &&
3929 param, PJ_EINVAL);
3930
3931 PJ_LOG(4,(THIS_FILE, "Call %d sending DTMF %.*s using %s method",
3932 call_id, (int)param->digits.slen, param->digits.ptr,
3933 get_dtmf_method_name(param->method)));
3934
3935 if (param->method == PJSUA_DTMF_METHOD_RFC2833) {
3936 status = pjsua_call_dial_dtmf2(call_id, &param->digits, param->duration);
3937 } else if (param->method == PJSUA_DTMF_METHOD_SIP_INFO) {
3938 const pj_str_t SIP_INFO = pj_str("INFO");
3939 int i;
3940
3941 for (i = 0; i < param->digits.slen; ++i) {
3942 char body[80];
3943 pjsua_msg_data msg_data_;
3944
3945 pjsua_msg_data_init(&msg_data_);
3946 msg_data_.content_type = pj_str("application/dtmf-relay");
3947
3948 pj_ansi_snprintf(body, sizeof(body),
3949 "Signal=%c\r\n"
3950 "Duration=%d",
3951 param->digits.ptr[i], param->duration);
3952 msg_data_.msg_body = pj_str(body);
3953
3954 status = pjsua_call_send_request(call_id, &SIP_INFO, &msg_data_);
3955 }
3956 }
3957
3958 return status;
3959 }
3960
3961 /**
3962 * Send instant messaging inside INVITE session.
3963 */
3964 PJ_DEF(pj_status_t) pjsua_call_send_im( pjsua_call_id call_id,
3965 const pj_str_t *mime_type,
3966 const pj_str_t *content,
3967 const pjsua_msg_data *msg_data,
3968 void *user_data)
3969 {
3970 pjsua_call *call;
3971 pjsip_dialog *dlg = NULL;
3972 const pj_str_t mime_text_plain = pj_str("text/plain");
3973 pjsip_media_type ctype;
3974 pjsua_im_data *im_data;
3975 pjsip_tx_data *tdata;
3976 pj_bool_t content_in_msg_data;
3977 pj_status_t status;
3978
3979 content_in_msg_data = msg_data && (msg_data->msg_body.slen ||
3980 msg_data->multipart_ctype.type.slen);
3981
3982 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
3983 PJ_EINVAL);
3984
3985 /* Message body must be specified. */
3986 PJ_ASSERT_RETURN(content || content_in_msg_data, PJ_EINVAL);
3987
3988 if (content) {
3989 PJ_LOG(4,(THIS_FILE, "Call %d sending %d bytes MESSAGE..",
3990 call_id, (int)content->slen));
3991 } else {
3992 PJ_LOG(4,(THIS_FILE, "Call %d sending MESSAGE..",
3993 call_id));
3994 }
3995
3996 pj_log_push_indent();
3997
3998 status = acquire_call("pjsua_call_send_im()", call_id, &call, &dlg);
3999 if (status != PJ_SUCCESS)
4000 goto on_return;
4001
4002 /* Create request message. */
4003 status = pjsip_dlg_create_request( call->inv->dlg, &pjsip_message_method,
4004 -1, &tdata);
4005 if (status != PJ_SUCCESS) {
4006 pjsua_perror(THIS_FILE, "Unable to create MESSAGE request", status);
4007 goto on_return;
4008 }
4009
4010 /* Add accept header. */
4011 pjsip_msg_add_hdr( tdata->msg,
4012 (pjsip_hdr*)pjsua_im_create_accept(tdata->pool));
4013
4014 /* Add message body, if content is set */
4015 if (content) {
4016 /* Set default media type if none is specified */
4017 if (mime_type == NULL) {
4018 mime_type = &mime_text_plain;
4019 }
4020
4021 /* Parse MIME type */
4022 pjsua_parse_media_type(tdata->pool, mime_type, &ctype);
4023
4024 /* Create "text/plain" message body. */
4025 tdata->msg->body = pjsip_msg_body_create( tdata->pool, &ctype.type,
4026 &ctype.subtype, content);
4027 if (tdata->msg->body == NULL) {
4028 pjsua_perror(THIS_FILE, "Unable to create msg body", PJ_ENOMEM);
4029 pjsip_tx_data_dec_ref(tdata);
4030 goto on_return;
4031 }
4032 }
4033
4034 /* Add additional headers etc */
4035 pjsua_process_msg_data( tdata, msg_data);
4036
4037 /* Create IM data and attach to the request. */
4038 im_data = PJ_POOL_ZALLOC_T(tdata->pool, pjsua_im_data);
4039 im_data->acc_id = call->acc_id;
4040 im_data->call_id = call_id;
4041 im_data->to = call->inv->dlg->remote.info_str;
4042 if (content)
4043 pj_strdup_with_null(tdata->pool, &im_data->body, content);
4044 im_data->user_data = user_data;
4045
4046
4047 /* Send the request. */
4048 status = pjsip_dlg_send_request( call->inv->dlg, tdata,
4049 pjsua_var.mod.id, im_data);
4050 if (status != PJ_SUCCESS) {
4051 pjsua_perror(THIS_FILE, "Unable to send MESSAGE request", status);
4052 goto on_return;
4053 }
4054
4055 on_return:
4056 if (dlg) pjsip_dlg_dec_lock(dlg);
4057 pj_log_pop_indent();
4058 return status;
4059 }
4060
4061
4062 /*
4063 * Send IM typing indication inside INVITE session.
4064 */
4065 PJ_DEF(pj_status_t) pjsua_call_send_typing_ind( pjsua_call_id call_id,
4066 pj_bool_t is_typing,
4067 const pjsua_msg_data*msg_data)
4068 {
4069 pjsua_call *call;
4070 pjsip_dialog *dlg = NULL;
4071 pjsip_tx_data *tdata;
4072 pj_status_t status;
4073
4074 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
4075 PJ_EINVAL);
4076
4077 PJ_LOG(4,(THIS_FILE, "Call %d sending typing indication..",
4078 call_id));
4079 pj_log_push_indent();
4080
4081 status = acquire_call("pjsua_call_send_typing_ind", call_id, &call, &dlg);
4082 if (status != PJ_SUCCESS)
4083 goto on_return;
4084
4085 /* Create request message. */
4086 status = pjsip_dlg_create_request( call->inv->dlg, &pjsip_message_method,
4087 -1, &tdata);
4088 if (status != PJ_SUCCESS) {
4089 pjsua_perror(THIS_FILE, "Unable to create MESSAGE request", status);
4090 goto on_return;
4091 }
4092
4093 /* Create "application/im-iscomposing+xml" msg body. */
4094 tdata->msg->body = pjsip_iscomposing_create_body(tdata->pool, is_typing,
4095 NULL, NULL, -1);
4096
4097 /* Add additional headers etc */
4098 pjsua_process_msg_data( tdata, msg_data);
4099
4100 /* Send the request. */
4101 status = pjsip_dlg_send_request( call->inv->dlg, tdata, -1, NULL);
4102 if (status != PJ_SUCCESS) {
4103 pjsua_perror(THIS_FILE, "Unable to send MESSAGE request", status);
4104 goto on_return;
4105 }
4106
4107 on_return:
4108 if (dlg) pjsip_dlg_dec_lock(dlg);
4109 pj_log_pop_indent();
4110 return status;
4111 }
4112
4113
4114 /*
4115 * Send arbitrary request.
4116 */
4117 PJ_DEF(pj_status_t) pjsua_call_send_request(pjsua_call_id call_id,
4118 const pj_str_t *method_str,
4119 const pjsua_msg_data *msg_data)
4120 {
4121 pjsua_call *call;
4122 pjsip_dialog *dlg = NULL;
4123 pjsip_method method;
4124 pjsip_tx_data *tdata;
4125 pj_status_t status;
4126
4127 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
4128 PJ_EINVAL);
4129
4130 PJ_LOG(4,(THIS_FILE, "Call %d sending %.*s request..",
4131 call_id, (int)method_str->slen, method_str->ptr));
4132 pj_log_push_indent();
4133
4134 status = acquire_call("pjsua_call_send_request", call_id, &call, &dlg);
4135 if (status != PJ_SUCCESS)
4136 goto on_return;
4137
4138 /* Init method */
4139 pjsip_method_init_np(&method, (pj_str_t*)method_str);
4140
4141 /* Create request message. */
4142 status = pjsip_dlg_create_request( call->inv->dlg, &method, -1, &tdata);
4143 if (status != PJ_SUCCESS) {
4144 pjsua_perror(THIS_FILE, "Unable to create request", status);
4145 goto on_return;
4146 }
4147
4148 /* Add additional headers etc */
4149 pjsua_process_msg_data( tdata, msg_data);
4150
4151 /* Send the request. */
4152 status = pjsip_dlg_send_request( call->inv->dlg, tdata, -1, NULL);
4153 if (status != PJ_SUCCESS) {
4154 pjsua_perror(THIS_FILE, "Unable to send request", status);
4155 goto on_return;
4156 }
4157
4158 on_return:
4159 if (dlg) pjsip_dlg_dec_lock(dlg);
4160 pj_log_pop_indent();
4161 return status;
4162 }
4163
4164
4165 /*
4166 * Terminate all calls.
4167 */
4168 PJ_DEF(void) pjsua_call_hangup_all(void)
4169 {
4170 unsigned i;
4171
4172 PJ_LOG(4,(THIS_FILE, "Hangup all calls.."));
4173 pj_log_push_indent();
4174
4175 // This may deadlock, see https://github.com/pjsip/pjproject/issues/1305
4176 //PJSUA_LOCK();
4177
4178 if (pjsua_var.calls) {
4179 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
4180 if (pjsua_var.calls[i].inv)
4181 pjsua_call_hangup(i, 0, NULL, NULL);
4182 }
4183 }
4184
4185 //PJSUA_UNLOCK();
4186 pj_log_pop_indent();
4187 }
4188
4189
4190 /* Timer callback to send re-INVITE/UPDATE to lock codec or ICE update */
4191 static void reinv_timer_cb(pj_timer_heap_t *th, pj_timer_entry *entry)
4192 {
4193 pjsua_call_id call_id = (pjsua_call_id)(pj_size_t)entry->user_data;
4194 pjsip_dialog *dlg;
4195 pjsua_call *call;
4196 pj_status_t status;
4197
4198 PJ_UNUSED_ARG(th);
4199
4200 pjsua_var.calls[call_id].reinv_timer.id = PJ_FALSE;
4201
4202 pj_log_push_indent();
4203
4204 status = acquire_call("reinv_timer_cb()", call_id, &call, &dlg);
4205 if (status != PJ_SUCCESS) {
4206 pj_log_pop_indent();
4207 return;
4208 }
4209
4210 process_pending_reinvite(call);
4211
4212 pjsip_dlg_dec_lock(dlg);
4213
4214 pj_log_pop_indent();
4215 }
4216
4217
4218 /* Check if the specified format can be skipped in counting codecs */
4219 static pj_bool_t is_non_av_fmt(const pjmedia_sdp_media *m,
4220 const pj_str_t *fmt)
4221 {
4222 const pj_str_t STR_TEL = {"telephone-event", 15};
4223 const pj_str_t STR_RED = {"red", 3};
4224 unsigned pt;
4225
4226 pt = pj_strtoul(fmt);
4227
4228 /* Check for comfort noise */
4229 if (pt == PJMEDIA_RTP_PT_CN)
4230 return PJ_TRUE;
4231
4232 /* Dynamic PT, check the format name */
4233 if (pt >= 96) {
4234 pjmedia_sdp_attr *a;
4235 pjmedia_sdp_rtpmap rtpmap;
4236
4237 /* Get the format name */
4238 a = pjmedia_sdp_attr_find2(m->attr_count, m->attr, "rtpmap", fmt);
4239 if (a && pjmedia_sdp_attr_get_rtpmap(a, &rtpmap)==PJ_SUCCESS) {
4240 /* Check for telephone-event and redundancy*/
4241 if (pj_stricmp(&rtpmap.enc_name, &STR_TEL)==0 ||
4242 pj_stricmp(&rtpmap.enc_name, &STR_RED)==0)
4243 {
4244 return PJ_TRUE;
4245 }
4246 } else {
4247 /* Invalid SDP, should not reach here */
4248 pj_assert(!"SDP should have been validated!");
4249 return PJ_TRUE;
4250 }
4251 }
4252
4253 return PJ_FALSE;
4254 }
4255
4256
4257 /* Schedule check for the need of re-INVITE/UPDATE after media update, cases:
4258 * - lock codec if remote answerer has given us more than one codecs
4259 * - update ICE default transport address if it has changed after ICE
4260 * connectivity check.
4261 */
4262 void pjsua_call_schedule_reinvite_check(pjsua_call *call, unsigned delay_ms)
4263 {
4264 pj_time_val delay;
4265
4266 /* Stop reinvite timer, if it is active */
4267 if (call->reinv_timer.id)
4268 pjsua_cancel_timer(&call->reinv_timer);
4269
4270 delay.sec = 0;
4271 delay.msec = delay_ms;
4272 pj_time_val_normalize(&delay);
4273 call->reinv_timer.id = PJ_TRUE;
4274 pjsua_schedule_timer(&call->reinv_timer, &delay);
4275 }
4276
4277
4278 /* Check if lock codec is needed */
4279 static pj_bool_t check_lock_codec(pjsua_call *call)
4280 {
4281 const pjmedia_sdp_session *local_sdp, *remote_sdp;
4282 pj_bool_t has_mult_fmt = PJ_FALSE;
4283 unsigned i;
4284 pj_status_t status;
4285
4286 /* Check if lock codec is disabled */
4287 if (!pjsua_var.acc[call->acc_id].cfg.lock_codec)
4288 return PJ_FALSE;
4289
4290 /* Check lock codec retry count */
4291 if (call->lock_codec.retry_cnt >= LOCK_CODEC_MAX_RETRY)
4292 return PJ_FALSE;
4293
4294 /* Check if we are the answerer, we shouldn't need to lock codec */
4295 if (!call->inv->neg || !pjmedia_sdp_neg_was_answer_remote(call->inv->neg))
4296 return PJ_FALSE;
4297
4298 /* Check if remote answerer has given us more than one codecs. */
4299 status = pjmedia_sdp_neg_get_active_local(call->inv->neg, &local_sdp);
4300 if (status != PJ_SUCCESS)
4301 return PJ_FALSE;
4302 status = pjmedia_sdp_neg_get_active_remote(call->inv->neg, &remote_sdp);
4303 if (status != PJ_SUCCESS)
4304 return PJ_FALSE;
4305
4306 for (i = 0; i < call->med_cnt && !has_mult_fmt; ++i) {
4307 pjsua_call_media *call_med = &call->media[i];
4308 const pjmedia_sdp_media *rem_m, *loc_m;
4309 unsigned codec_cnt = 0;
4310 unsigned j;
4311
4312 /* Skip this if the media is inactive or error */
4313 if (call_med->state == PJSUA_CALL_MEDIA_NONE ||
4314 call_med->state == PJSUA_CALL_MEDIA_ERROR ||
4315 call_med->dir == PJMEDIA_DIR_NONE)
4316 {
4317 continue;
4318 }
4319
4320 /* Remote may answer with less media lines. */
4321 if (i >= remote_sdp->media_count)
4322 continue;
4323
4324 rem_m = remote_sdp->media[i];
4325 loc_m = local_sdp->media[i];
4326
4327 /* Verify that media must be active. */
4328 pj_assert(loc_m->desc.port && rem_m->desc.port);
4329 PJ_UNUSED_ARG(loc_m);
4330
4331 /* Count the formats in the answer. */
4332 for (j=0; j<rem_m->desc.fmt_count && codec_cnt <= 1; ++j) {
4333 if (!is_non_av_fmt(rem_m, &rem_m->desc.fmt[j]) && ++codec_cnt > 1)
4334 has_mult_fmt = PJ_TRUE;
4335 }
4336 }
4337
4338 /* Reset retry count when remote answer has one codec */
4339 if (!has_mult_fmt)
4340 call->lock_codec.retry_cnt = 0;
4341
4342 return has_mult_fmt;
4343 }
4344
4345 /* Check if ICE setup is complete and if it needs to send reinvite */
4346 static pj_bool_t check_ice_complete(pjsua_call *call, pj_bool_t *need_reinv)
4347 {
4348 pj_bool_t ice_need_reinv = PJ_FALSE;
4349 pj_bool_t ice_complete = PJ_TRUE;
4350 unsigned i;
4351
4352 /* Check if ICE setup is complete and if it needs reinvite */
4353 for (i = 0; i < call->med_cnt; ++i) {
4354 pjsua_call_media *call_med = &call->media[i];
4355 pjmedia_transport_info tpinfo;
4356 pjmedia_ice_transport_info *ice_info;
4357
4358 if (call_med->tp_st == PJSUA_MED_TP_NULL ||
4359 call_med->tp_st == PJSUA_MED_TP_DISABLED ||
4360 call_med->state == PJSUA_CALL_MEDIA_ERROR)
4361 {
4362 continue;
4363 }
4364
4365 pjmedia_transport_info_init(&tpinfo);
4366 pjmedia_transport_get_info(call_med->tp, &tpinfo);
4367 ice_info = (pjmedia_ice_transport_info*)
4368 pjmedia_transport_info_get_spc_info(
4369 &tpinfo, PJMEDIA_TRANSPORT_TYPE_ICE);
4370
4371 /* Check if ICE is active */
4372 if (!ice_info || !ice_info->active)
4373 continue;
4374
4375 /* Check if ICE setup not completed yet */
4376 if (ice_info->sess_state < PJ_ICE_STRANS_STATE_RUNNING) {
4377 ice_complete = PJ_FALSE;
4378 break;
4379 }
4380
4381 /* Check if ICE needs to send reinvite */
4382 if (!ice_need_reinv &&
4383 ice_info->sess_state == PJ_ICE_STRANS_STATE_RUNNING &&
4384 ice_info->role == PJ_ICE_SESS_ROLE_CONTROLLING)
4385 {
4386 pjsua_ice_config *cfg=&pjsua_var.acc[call->acc_id].cfg.ice_cfg;
4387 if ((cfg->ice_always_update && !call->reinv_ice_sent) ||
4388 pj_sockaddr_cmp(&tpinfo.sock_info.rtp_addr_name,
4389 &call_med->rtp_addr))
4390 {
4391 ice_need_reinv = PJ_TRUE;
4392 }
4393 }
4394 }
4395
4396 if (ice_complete && need_reinv)
4397 *need_reinv = ice_need_reinv;
4398
4399 return ice_complete;
4400 }
4401
4402 /* Check and send reinvite for lock codec and ICE update */
4403 static pj_status_t process_pending_reinvite(pjsua_call *call)
4404 {
4405 const pj_str_t ST_UPDATE = {"UPDATE", 6};
4406 pj_pool_t *pool = call->inv->pool_prov;
4407 pjsip_inv_session *inv = call->inv;
4408 pj_bool_t ice_need_reinv;
4409 pj_bool_t ice_completed;
4410 pj_bool_t need_lock_codec;
4411 pj_bool_t rem_can_update;
4412 pjmedia_sdp_session *new_offer;
4413 pjsip_tx_data *tdata;
4414 unsigned i;
4415 pj_status_t status;
4416
4417 /* Verify if another SDP negotiation is in progress, e.g: session timer
4418 * or another re-INVITE.
4419 */
4420 if (inv==NULL || inv->neg==NULL ||
4421 pjmedia_sdp_neg_get_state(inv->neg)!=PJMEDIA_SDP_NEG_STATE_DONE)
4422 {
4423 return PJMEDIA_SDPNEG_EINSTATE;
4424 }
4425
4426 /* Don't do this if call is disconnecting! */
4427 if (inv->state > PJSIP_INV_STATE_CONFIRMED || inv->cause >= 200)
4428 {
4429 return PJ_EINVALIDOP;
4430 }
4431
4432 if (inv->state == PJSIP_INV_STATE_EARLY) {
4433 if (pjsip_dlg_remote_has_cap(inv->dlg, PJSIP_H_ALLOW, NULL,
4434 &ST_UPDATE) == PJSIP_DIALOG_CAP_SUPPORTED &&
4435 inv->sdp_done_early_rel &&
4436 !PJSUA_LOCK_CODEC_DONT_USE_UPDATE)
4437 {
4438 /* Yes, remote supports UPDATE and SDP negotiation was done
4439 * using reliable provisional responses. We can proceed.
4440 */
4441 } else {
4442 call->reinv_pending = PJ_TRUE;
4443 return PJ_EPENDING;
4444 }
4445 }
4446
4447 /* Check if ICE setup is complete and if it needs reinvite */
4448 ice_completed = check_ice_complete(call, &ice_need_reinv);
4449 if (!ice_completed)
4450 return PJ_EPENDING;
4451
4452 /* Check if we need to lock codec */
4453 need_lock_codec = check_lock_codec(call);
4454
4455 /* Check if reinvite is really needed */
4456 if (!need_lock_codec && !ice_need_reinv)
4457 return PJ_SUCCESS;
4458
4459
4460 /* Okay! So we need to send re-INVITE/UPDATE */
4461
4462 /* Check if remote support UPDATE */
4463 rem_can_update = !PJSUA_LOCK_CODEC_DONT_USE_UPDATE &&
4464 pjsip_dlg_remote_has_cap(inv->dlg, PJSIP_H_ALLOW, NULL,
4465 &ST_UPDATE) ==
4466 PJSIP_DIALOG_CAP_SUPPORTED;
4467
4468 /* Logging stuff */
4469 {
4470 const char *ST_ICE_UPDATE = "ICE transport address after "
4471 "ICE negotiation";
4472 const char *ST_LOCK_CODEC = "media session to use only one codec";
4473 PJ_LOG(4,(THIS_FILE, "Call %d sending %s for updating %s%s%s",
4474 call->index,
4475 (rem_can_update? "UPDATE" : "re-INVITE"),
4476 (ice_need_reinv? ST_ICE_UPDATE : ST_LOCK_CODEC),
4477 (ice_need_reinv && need_lock_codec? " and " : ""),
4478 (ice_need_reinv && need_lock_codec? ST_LOCK_CODEC : "")
4479 ));
4480 }
4481
4482 /* Clear reinit media flag. Should we also cleanup other flags here? */
4483 call->opt.flag &= ~PJSUA_CALL_REINIT_MEDIA;
4484
4485 /* Generate SDP re-offer */
4486 status = pjsua_media_channel_create_sdp(call->index, pool, NULL,
4487 &new_offer, NULL);
4488 if (status != PJ_SUCCESS) {
4489 pjsua_perror(THIS_FILE, "Unable to create local SDP", status);
4490 return status;
4491 }
4492
4493 /* Update the new offer so it contains only a codec. Note that
4494 * SDP nego has removed unmatched codecs from the offer and the codec
4495 * order in the offer has been matched to the answer, so we'll override
4496 * the codecs in the just generated SDP with the ones from the active
4497 * local SDP and leave just one codec for the next SDP re-offer.
4498 */
4499 if (need_lock_codec) {
4500 const pjmedia_sdp_session *ref_sdp;
4501
4502 /* Get local active SDP as reference */
4503 status = pjmedia_sdp_neg_get_active_local(call->inv->neg, &ref_sdp);
4504 if (status != PJ_SUCCESS)
4505 return status;
4506
4507 /* Verify media count. Note that remote may add/remove media line
4508 * in the answer. When answer has less media, it must have been
4509 * handled by pjsua_media_channel_update() as disabled media.
4510 * When answer has more media, it must have been ignored (treated
4511 * as non-exist) anywhere. Local media count should not be updated
4512 * at this point, as modifying media count operation (i.e: reinvite,
4513 * update, vid_set_strm) is currently blocking, protected with
4514 * dialog mutex, and eventually reset SDP nego state to LOCAL OFFER.
4515 */
4516 if (call->med_cnt != ref_sdp->media_count ||
4517 ref_sdp->media_count != new_offer->media_count)
4518 {
4519 /* Anyway, just in case, let's just return error */
4520 return PJMEDIA_SDPNEG_EINSTATE;
4521 }
4522
4523 for (i = 0; i < call->med_cnt; ++i) {
4524 unsigned j, codec_cnt = 0;
4525 const pjmedia_sdp_media *ref_m = ref_sdp->media[i];
4526 pjmedia_sdp_media *m = new_offer->media[i];
4527 pjsua_call_media *call_med = &call->media[i];
4528
4529 /* Verify if media is deactivated */
4530 if (call_med->state == PJSUA_CALL_MEDIA_NONE ||
4531 call_med->state == PJSUA_CALL_MEDIA_ERROR ||
4532 call_med->dir == PJMEDIA_DIR_NONE)
4533 {
4534 continue;
4535 }
4536
4537 /* Reset formats */
4538 m->desc.fmt_count = 0;
4539 pjmedia_sdp_attr_remove_all(&m->attr_count, m->attr, "rtpmap");
4540 pjmedia_sdp_attr_remove_all(&m->attr_count, m->attr, "fmtp");
4541
4542 /* Copy only the first format + any non-AV formats from
4543 * the active local SDP.
4544 */
4545 for (j = 0; j < ref_m->desc.fmt_count; ++j) {
4546 const pj_str_t *fmt = &ref_m->desc.fmt[j];
4547
4548 if (is_non_av_fmt(ref_m, fmt) || (++codec_cnt == 1)) {
4549 pjmedia_sdp_attr *a;
4550
4551 m->desc.fmt[m->desc.fmt_count++] = *fmt;
4552 a = pjmedia_sdp_attr_find2(ref_m->attr_count, ref_m->attr,
4553 "rtpmap", fmt);
4554 if (a) {
4555 pjmedia_sdp_attr_add(&m->attr_count, m->attr,
4556 pjmedia_sdp_attr_clone(pool, a));
4557 }
4558 a = pjmedia_sdp_attr_find2(ref_m->attr_count, ref_m->attr,
4559 "fmtp", fmt);
4560 if (a) {
4561 pjmedia_sdp_attr_add(&m->attr_count, m->attr,
4562 pjmedia_sdp_attr_clone(pool, a));
4563 }
4564 }
4565 }
4566 }
4567 }
4568
4569 /* Put back original direction and "c=0.0.0.0" line */
4570 {
4571 const pjmedia_sdp_session *cur_sdp;
4572
4573 /* Get local active SDP */
4574 status = pjmedia_sdp_neg_get_active_local(call->inv->neg, &cur_sdp);
4575 if (status != PJ_SUCCESS)
4576 return status;
4577
4578 /* Make sure media count has not been changed */
4579 if (call->med_cnt != cur_sdp->media_count)
4580 return PJMEDIA_SDPNEG_EINSTATE;
4581
4582 for (i = 0; i < call->med_cnt; ++i) {
4583 const pjmedia_sdp_media *m = cur_sdp->media[i];
4584 pjmedia_sdp_media *new_m = new_offer->media[i];
4585 pjsua_call_media *call_med = &call->media[i];
4586 pjmedia_sdp_attr *a = NULL;
4587
4588 /* Update direction to the current dir */
4589 pjmedia_sdp_media_remove_all_attr(new_m, "sendrecv");
4590 pjmedia_sdp_media_remove_all_attr(new_m, "sendonly");
4591 pjmedia_sdp_media_remove_all_attr(new_m, "recvonly");
4592 pjmedia_sdp_media_remove_all_attr(new_m, "inactive");
4593
4594 if (call_med->dir == PJMEDIA_DIR_ENCODING_DECODING) {
4595 a = pjmedia_sdp_attr_create(pool, "sendrecv", NULL);
4596 } else if (call_med->dir == PJMEDIA_DIR_ENCODING) {
4597 a = pjmedia_sdp_attr_create(pool, "sendonly", NULL);
4598 } else if (call_med->dir == PJMEDIA_DIR_DECODING) {
4599 a = pjmedia_sdp_attr_create(pool, "recvonly", NULL);
4600 } else {
4601 const pjmedia_sdp_conn *conn;
4602 a = pjmedia_sdp_attr_create(pool, "inactive", NULL);
4603
4604 /* Also check if the original c= line address is zero */
4605 conn = m->conn;
4606 if (!conn)
4607 conn = cur_sdp->conn;
4608 if (pj_strcmp2(&conn->addr, "0.0.0.0")==0 ||
4609 pj_strcmp2(&conn->addr, "0")==0)
4610 {
4611 if (!new_m->conn) {
4612 new_m->conn = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_conn);
4613 }
4614
4615 if (pj_strcmp2(&new_m->conn->addr, "0.0.0.0")) {
4616 new_m->conn->net_type = pj_str("IN");
4617 new_m->conn->addr_type = pj_str("IP4");
4618 new_m->conn->addr = pj_str("0.0.0.0");
4619 }
4620 }
4621 }
4622
4623 pj_assert(a);
4624 pjmedia_sdp_media_add_attr(new_m, a);
4625 }
4626 }
4627
4628
4629 if (rem_can_update) {
4630 status = pjsip_inv_update(inv, NULL, new_offer, &tdata);
4631 } else {
4632 status = pjsip_inv_reinvite(inv, NULL, new_offer, &tdata);
4633 }
4634
4635 if (status==PJ_EINVALIDOP &&
4636 ++call->lock_codec.retry_cnt < LOCK_CODEC_MAX_RETRY)
4637 {
4638 /* Ups, let's reschedule again */
4639 pjsua_call_schedule_reinvite_check(call, LOCK_CODEC_RETRY_INTERVAL);
4640 return PJ_SUCCESS;
4641 } else if (status != PJ_SUCCESS) {
4642 pjsua_perror(THIS_FILE, "Error creating UPDATE/re-INVITE",
4643 status);
4644 return status;
4645 }
4646
4647 /* Send the UPDATE/re-INVITE request */
4648 status = pjsip_inv_send_msg(inv, tdata);
4649 if (status != PJ_SUCCESS) {
4650 pjsua_perror(THIS_FILE, "Error sending UPDATE/re-INVITE",
4651 status);
4652 return status;
4653 }
4654
4655 /* Update flags */
4656 if (ice_need_reinv)
4657 call->reinv_ice_sent = PJ_TRUE;
4658 if (need_lock_codec)
4659 ++call->lock_codec.retry_cnt;
4660
4661 return PJ_SUCCESS;
4662 }
4663
4664
4665 static void trickle_ice_retrans_18x(pj_timer_heap_t *th,
4666 struct pj_timer_entry *te)
4667 {
4668 pjsua_call *call = (pjsua_call*)te->user_data;
4669 pjsip_tx_data *tdata = NULL;
4670 pj_time_val delay;
4671
4672 PJ_UNUSED_ARG(th);
4673
4674 /* If trickling has been started or dialog has been established on
4675 * both sides, stop 18x retransmission.
4676 */
4677 if (call->trickle_ice.trickling >= PJSUA_OP_STATE_RUNNING ||
4678 call->trickle_ice.remote_dlg_est)
4679 {
4680 return;
4681 }
4682
4683 /* Make sure last tdata is 18x response */
4684 if (call->inv->invite_tsx)
4685 tdata = call->inv->invite_tsx->last_tx;
4686 if (!tdata || tdata->msg->type != PJSIP_RESPONSE_MSG ||
4687 tdata->msg->line.status.code/10 != 18)
4688 {
4689 return;
4690 }
4691
4692 /* Retransmit 18x */
4693 ++call->trickle_ice.retrans18x_count;
4694 PJ_LOG(4,(THIS_FILE,
4695 "Call %d: ICE trickle retransmitting 18x (retrans #%d)",
4696 call->index, call->trickle_ice.retrans18x_count));
4697
4698 pjsip_tx_data_add_ref(tdata);
4699 pjsip_tsx_retransmit_no_state(call->inv->invite_tsx, tdata);
4700
4701 /* Schedule next retransmission */
4702 if (call->trickle_ice.retrans18x_count < 6) {
4703 pj_uint32_t tmp;
4704 tmp = (1 << call->trickle_ice.retrans18x_count) * pjsip_cfg()->tsx.t1;
4705 delay.sec = 0;
4706 delay.msec = tmp;
4707 pj_time_val_normalize(&delay);
4708 } else {
4709 delay.sec = 1;
4710 delay.msec = 500;
4711 }
4712 pjsua_schedule_timer(te, &delay);
4713 }
4714
4715
4716 static void trickle_ice_recv_sip_info(pjsua_call *call, pjsip_rx_data *rdata)
4717 {
4718 pjsip_media_type med_type;
4719 pjsip_rdata_sdp_info *sdp_info;
4720 pj_status_t status;
4721 unsigned i, j, med_cnt;
4722 pj_bool_t use_med_prov;
4723
4724 pjsip_media_type_init2(&med_type, "application", "trickle-ice-sdpfrag");
4725
4726 /* Parse the SDP */
4727 sdp_info = pjsip_rdata_get_sdp_info2(rdata, &med_type);
4728 if (!sdp_info->sdp) {
4729 pj_status_t err = sdp_info->body.ptr? sdp_info->sdp_err:PJ_ENOTFOUND;
4730 pjsua_perror(THIS_FILE, "Failed to parse trickle ICE SDP in "
4731 "incoming INFO", err);
4732 return;
4733 }
4734
4735 PJSUA_LOCK();
4736
4737 /* Retrieve the candidates from the SDP */
4738 use_med_prov = call->med_prov_cnt > call->med_cnt;
4739 med_cnt = use_med_prov? call->med_prov_cnt : call->med_cnt;
4740 for (i = 0; i < sdp_info->sdp->media_count; ++i) {
4741 pjmedia_transport *tp = NULL;
4742 pj_str_t mid, ufrag, pwd;
4743 unsigned cand_cnt = PJ_ICE_ST_MAX_CAND;
4744 pj_ice_sess_cand cand[PJ_ICE_ST_MAX_CAND];
4745 pj_bool_t end_of_cand;
4746
4747 status = pjmedia_ice_trickle_decode_sdp(sdp_info->sdp, i, &mid,
4748 &ufrag, &pwd,
4749 &cand_cnt, cand,
4750 &end_of_cand);
4751 if (status != PJ_SUCCESS) {
4752 pjsua_perror(THIS_FILE, "Failed to retrive ICE candidates from "
4753 "SDP in incoming INFO", status);
4754 continue;
4755 }
4756
4757 for (j = 0; j < med_cnt; ++j) {
4758 pjsua_call_media *cm = use_med_prov? &call->media_prov[j] :
4759 &call->media[j];
4760 tp = cm->tp_orig;
4761
4762 if (tp && tp->type == PJMEDIA_TRANSPORT_TYPE_ICE &&
4763 pj_strcmp(&cm->rem_mid, &mid) == 0)
4764 {
4765 break;
4766 }
4767 }
4768
4769 if (j == med_cnt) {
4770 pjsua_perror(THIS_FILE, "Cannot add remote candidates from SDP in "
4771 "incoming INFO because media ID (SDP a=mid) is not "
4772 "recognized",
4773 PJ_EIGNORED);
4774 continue;
4775 }
4776
4777 /* Update ICE checklist */
4778 status = pjmedia_ice_trickle_update(tp, &ufrag, &pwd, cand_cnt, cand,
4779 end_of_cand);
4780 if (status != PJ_SUCCESS) {
4781 pjsua_perror(THIS_FILE, "Failed to update ICE checklist from "
4782 "incoming INFO", status);
4783 }
4784 }
4785
4786 PJSUA_UNLOCK();
4787 }
4788
4789
4790 static void trickle_ice_send_sip_info(pj_timer_heap_t *th,
4791 struct pj_timer_entry *te)
4792 {
4793 pjsua_call *call = (pjsua_call*)te->user_data;
4794 pj_pool_t *tmp_pool = NULL;
4795 pj_bool_t all_end_of_cand, use_med_prov;
4796 pjmedia_sdp_session *sdp;
4797 unsigned i, med_cnt;
4798 pjsua_msg_data msg_data;
4799 pjsip_generic_string_hdr hdr1, hdr2;
4800 pj_status_t status = PJ_SUCCESS;
4801 pj_bool_t forced, need_send = PJ_FALSE;
4802 pj_sockaddr orig_addr;
4803
4804 pj_str_t SIP_INFO = {"INFO", 4};
4805 pj_str_t CONTENT_DISP_STR = {"Content-Disposition", 19};
4806 pj_str_t INFO_PKG_STR = {"Info-Package", 12};
4807 pj_str_t TRICKLE_ICE_STR = {"trickle-ice", 11};
4808
4809 PJ_UNUSED_ARG(th);
4810
4811 PJSUA_LOCK();
4812
4813 /* Check provisional media or active media to use */
4814 use_med_prov = call->med_prov_cnt > call->med_cnt;
4815 med_cnt = use_med_prov? call->med_prov_cnt : call->med_cnt;
4816
4817 /* Check if any pending INFO already */
4818 if (call->trickle_ice.pending_info)
4819 goto on_return;
4820
4821 /* Check if any new candidate, if not forced */
4822 forced = (te->id == 2);
4823 if (!forced) {
4824 for (i = 0; i < med_cnt; ++i) {
4825 pjsua_call_media *cm = use_med_prov? &call->media_prov[i] :
4826 &call->media[i];
4827 pjmedia_transport *tp = cm->tp_orig;
4828
4829 if (!tp || tp->type != PJMEDIA_TRANSPORT_TYPE_ICE)
4830 continue;
4831
4832 if (pjmedia_ice_trickle_has_new_cand(tp))
4833 break;
4834 }
4835
4836 /* No new local candidate */
4837 if (i == med_cnt)
4838 goto on_return;
4839 }
4840
4841 PJ_LOG(4,(THIS_FILE, "Call %d: ICE trickle sending SIP INFO%s",
4842 call->index, (forced? " (forced)":"")));
4843
4844 /* Create temporary pool */
4845 tmp_pool = pjsua_pool_create("tmp_ice", 128, 128);
4846
4847 /* Create empty SDP */
4848 pj_sockaddr_init(pj_AF_INET(), &orig_addr, NULL, 0);
4849 status = pjmedia_endpt_create_base_sdp(pjsua_var.med_endpt, tmp_pool,
4850 NULL, &orig_addr, &sdp);
4851 if (status != PJ_SUCCESS)
4852 goto on_return;
4853
4854 /* Generate SDP for SIP INFO */
4855 all_end_of_cand = PJ_TRUE;
4856 for (i = 0; i < med_cnt; ++i) {
4857 pjsua_call_media *cm = use_med_prov? &call->media_prov[i] :
4858 &call->media[i];
4859 pjmedia_transport *tp = cm->tp_orig;
4860 pj_bool_t end_of_cand = PJ_FALSE;
4861
4862 if (!tp || tp->type != PJMEDIA_TRANSPORT_TYPE_ICE)
4863 continue;
4864
4865 status = pjmedia_ice_trickle_send_local_cand(tp, tmp_pool, sdp,
4866 &end_of_cand);
4867 if (status != PJ_SUCCESS || !end_of_cand)
4868 all_end_of_cand = PJ_FALSE;
4869
4870 need_send |= (status==PJ_SUCCESS);
4871 }
4872
4873 if (!need_send)
4874 goto on_return;
4875
4876 /* Generate and send SIP INFO */
4877 pjsua_msg_data_init(&msg_data);
4878
4879 pjsip_generic_string_hdr_init2(&hdr1, &INFO_PKG_STR, &TRICKLE_ICE_STR);
4880 pj_list_push_back(&msg_data.hdr_list, &hdr1);
4881 pjsip_generic_string_hdr_init2(&hdr2, &CONTENT_DISP_STR, &INFO_PKG_STR);
4882 pj_list_push_back(&msg_data.hdr_list, &hdr2);
4883
4884 msg_data.content_type = pj_str("application/trickle-ice-sdpfrag");
4885 msg_data.msg_body.ptr = pj_pool_alloc(tmp_pool, PJSIP_MAX_PKT_LEN);
4886 msg_data.msg_body.slen = pjmedia_sdp_print(sdp, msg_data.msg_body.ptr,
4887 PJSIP_MAX_PKT_LEN);
4888 if (msg_data.msg_body.slen == -1) {
4889 PJ_LOG(3,(THIS_FILE,
4890 "Warning! Call %d: ICE trickle failed to print SDP for "
4891 "SIP INFO due to insufficient buffer", call->index));
4892 goto on_return;
4893 }
4894
4895 status = pjsua_call_send_request(call->index, &SIP_INFO, &msg_data);
4896 if (status != PJ_SUCCESS)
4897 goto on_return;
4898
4899 /* Set flag for pending SIP INFO */
4900 call->trickle_ice.pending_info = PJ_TRUE;
4901
4902 /* Stop trickling if local candidate gathering for all media is done */
4903 if (all_end_of_cand) {
4904 PJ_LOG(4,(THIS_FILE, "Call %d: ICE trickle stopped trickling "
4905 "as local candidate gathering completed",
4906 call->index));
4907 call->trickle_ice.trickling = PJSUA_OP_STATE_DONE;
4908 }
4909
4910 /* Update ICE checklist after conveying local candidates. */
4911 for (i = 0; i < med_cnt; ++i) {
4912 pjsua_call_media *cm = use_med_prov? &call->media_prov[i] :
4913 &call->media[i];
4914 pjmedia_transport *tp = cm->tp_orig;
4915 if (!tp || tp->type != PJMEDIA_TRANSPORT_TYPE_ICE)
4916 continue;
4917
4918 pjmedia_ice_trickle_update(tp, NULL, NULL, 0, NULL, PJ_FALSE);
4919 }
4920
4921 on_return:
4922 if (tmp_pool)
4923 pj_pool_release(tmp_pool);
4924
4925 /* Reschedule if we are trickling */
4926 if (call->trickle_ice.trickling == PJSUA_OP_STATE_RUNNING) {
4927 pj_time_val delay = {0, PJSUA_TRICKLE_ICE_NEW_CAND_CHECK_INTERVAL};
4928
4929 /* Reset forced mode after successfully sending forced SIP INFO */
4930 te->id = (status==PJ_SUCCESS? 0 : 2);
4931
4932 pj_time_val_normalize(&delay);
4933 pjsua_schedule_timer(te, &delay);
4934 }
4935
4936 PJSUA_UNLOCK();
4937 }
4938
4939
4940 /* Before sending INFO can be started, UA needs to confirm these:
4941 * 1. dialog is established (perhaps early) at both sides,
4942 * 2. trickle ICE is supported by peer.
4943 *
4944 * This function needs to be called when:
4945 * - UAS sending 18x, to start 18x retrans
4946 * - UAC receiving 18x, to forcefully send SIP INFO & start trickling
4947 * - UAS receiving INFO, to cease 18x retrans & start trickling
4948 * - UAS receiving PRACK, to start trickling
4949 * - UAC/UAS receiving remote SDP (and check for trickle ICE support),
4950 * to start trickling.
4951 */
4952 void pjsua_ice_check_start_trickling(pjsua_call *call,
4953 pj_bool_t forceful,
4954 pjsip_event *e)
4955 {
4956 pjsip_inv_session *inv = call->inv;
4957
4958 /* Make sure trickling/sending-INFO has not been started */
4959 if (!forceful && call->trickle_ice.trickling >= PJSUA_OP_STATE_RUNNING)
4960 return;
4961
4962 /* Make sure trickle ICE is enabled */
4963 if (!call->trickle_ice.enabled)
4964 return;
4965
4966 /* Make sure the dialog state is established */
4967 if (!inv || inv->dlg->state != PJSIP_DIALOG_STATE_ESTABLISHED)
4968 return;
4969
4970 /* First, make sure remote dialog is also established. */
4971 if (inv->state == PJSIP_INV_STATE_CONFIRMED) {
4972 /* Set flag indicating remote dialog is established */
4973 call->trickle_ice.remote_dlg_est = PJ_TRUE;
4974 } else if (inv->state > PJSIP_INV_STATE_CONFIRMED) {
4975 /* Call is terminating/terminated (just trying to be safe) */
4976 call->trickle_ice.remote_dlg_est = PJ_FALSE;
4977 } else if (!call->trickle_ice.remote_dlg_est && e) {
4978 /* Call is being initialized */
4979 pjsip_msg *msg = NULL;
4980 pjsip_rx_data *rdata = NULL;
4981 pjsip_tx_data *tdata = NULL;
4982 pj_bool_t has_100rel = (inv->options & PJSIP_INV_REQUIRE_100REL);
4983 pj_timer_entry *te = &call->trickle_ice.timer;
4984
4985 if (e->type == PJSIP_EVENT_TSX_STATE &&
4986 e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
4987 {
4988 rdata = e->body.tsx_state.src.rdata;
4989 } else if (e->type == PJSIP_EVENT_TSX_STATE &&
4990 e->body.tsx_state.type == PJSIP_EVENT_TX_MSG)
4991 {
4992 tdata = e->body.tsx_state.src.tdata;
4993 } else {
4994 return;
4995 }
4996
4997 /* UAC must have received 18x at this point, so dialog must have been
4998 * established at the remote side.
4999 */
5000 if (inv->role == PJSIP_ROLE_UAC) {
5001 /* UAC needs to send SIP INFO when receiving 18x and 100rel is not
5002 * active.
5003 * Note that 18x may not have SDP (so we don't know if remote
5004 * supports trickle ICE), but we should send INFO anyway, as the
5005 * draft allows start trickling without answer.
5006 */
5007 if (!has_100rel && rdata &&
5008 rdata->msg_info.msg->type == PJSIP_RESPONSE_MSG &&
5009 rdata->msg_info.msg->line.status.code/10 == 18)
5010 {
5011 pjsip_rdata_sdp_info *sdp_info;
5012 sdp_info = pjsip_rdata_get_sdp_info(rdata);
5013 if (sdp_info->sdp) {
5014 unsigned i;
5015 for (i = 0; i < sdp_info->sdp->media_count; ++i) {
5016 if (pjmedia_ice_sdp_has_trickle(sdp_info->sdp, i)) {
5017 call->trickle_ice.remote_sup = PJ_TRUE;
5018 break;
5019 }
5020 }
5021 } else {
5022 /* Start sending SIP INFO forcefully */
5023 forceful = PJ_TRUE;
5024 }
5025
5026 if (forceful || call->trickle_ice.remote_sup) {
5027 PJ_LOG(4,(THIS_FILE,
5028 "Call %d: ICE trickle started after UAC "
5029 "receiving 18x (with%s SDP)",
5030 call->index, sdp_info->sdp?"":"out"));
5031 }
5032 }
5033 }
5034
5035 /* But if we are the UAS, we need to wait for SIP PRACK or INFO to
5036 * confirm dialog state at remote. And while waiting, 18x needs to be
5037 * retransmitted.
5038 */
5039 else {
5040
5041 if (tdata && e->body.tsx_state.tsx == inv->invite_tsx &&
5042 call->trickle_ice.retrans18x_count == 0)
5043 {
5044 /* Ignite 18x retransmission */
5045 msg = tdata->msg;
5046 if (msg->type == PJSIP_RESPONSE_MSG &&
5047 msg->line.status.code/10 == 18)
5048 {
5049 pj_time_val delay;
5050 delay.sec = pjsip_cfg()->tsx.t1 / 1000;
5051 delay.msec = pjsip_cfg()->tsx.t1 % 1000;
5052 pj_assert(!pj_timer_entry_running(te));
5053 te->cb = &trickle_ice_retrans_18x;
5054 pjsua_schedule_timer(te, &delay);
5055
5056 PJ_LOG(4,(THIS_FILE,
5057 "Call %d: ICE trickle start retransmitting 18x",
5058 call->index));
5059 }
5060 return;
5061 }
5062
5063 /* Check for incoming PRACK or INFO to stop 18x retransmission */
5064 if (!rdata)
5065 return;
5066
5067 msg = rdata->msg_info.msg;
5068 if (has_100rel) {
5069 /* With 100rel, has received PRACK? */
5070 if (msg->type != PJSIP_REQUEST_MSG ||
5071 pjsip_method_cmp(&msg->line.req.method,
5072 pjsip_get_prack_method()))
5073 {
5074 return;
5075 }
5076 } else {
5077 pj_str_t INFO_PKG_STR = {"Info-Package", 12};
5078 pjsip_generic_string_hdr *hdr;
5079
5080 /* Without 100rel, has received INFO? */
5081 if (msg->type != PJSIP_REQUEST_MSG ||
5082 pjsip_method_cmp(&msg->line.req.method,
5083 &pjsip_info_method))
5084 {
5085 return;
5086 }
5087
5088 /* With Info-Package header containing 'trickle-ice' */
5089 hdr = (pjsip_generic_string_hdr*)
5090 pjsip_msg_find_hdr_by_name(msg, &INFO_PKG_STR, NULL);
5091 if (!hdr || pj_strcmp2(&hdr->hvalue, "trickle-ice"))
5092 return;
5093
5094 /* Set the flag indicating remote supports trickle ICE */
5095 call->trickle_ice.remote_sup = PJ_TRUE;
5096 }
5097 PJ_LOG(4,(THIS_FILE,
5098 "Call %d: ICE trickle stop retransmitting 18x after "
5099 "receiving %s",
5100 call->index, (has_100rel?"PRACK":"INFO")));
5101 }
5102
5103 /* Set flag indicating remote dialog is established.
5104 * Any 18x retransmission should be ceased automatically.
5105 */
5106 call->trickle_ice.remote_dlg_est = PJ_TRUE;
5107 }
5108
5109 /* Check if ICE trickling can be started */
5110 if (!forceful &&
5111 (!call->trickle_ice.remote_dlg_est || !call->trickle_ice.remote_sup))
5112 {
5113 return;
5114 }
5115
5116 /* Let's start trickling (or sending SIP INFO) */
5117 if (forceful || call->trickle_ice.trickling < PJSUA_OP_STATE_RUNNING)
5118 {
5119 pj_timer_entry *te = &call->trickle_ice.timer;
5120 pj_time_val delay = {0,0};
5121
5122 if (call->trickle_ice.trickling < PJSUA_OP_STATE_RUNNING)
5123 call->trickle_ice.trickling = PJSUA_OP_STATE_RUNNING;
5124
5125 pjsua_cancel_timer(te);
5126 te->id = forceful? 2 : 0;
5127 te->cb = &trickle_ice_send_sip_info;
5128 pjsua_schedule_timer(te, &delay);
5129
5130 PJ_LOG(4,(THIS_FILE,
5131 "Call %d: ICE trickle start trickling",
5132 call->index));
5133 }
5134 }
5135
5136
5137 /*
5138 * This callback receives notification from invite session when the
5139 * session state has changed.
5140 */
5141 static void pjsua_call_on_state_changed(pjsip_inv_session *inv,
5142 pjsip_event *e)
5143 {
5144 pjsua_call *call;
5145 unsigned num_locks = 0;
5146
5147 pj_log_push_indent();
5148
5149 call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id];
5150
5151 if (!call) {
5152 pj_log_pop_indent();
5153 return;
5154 }
5155
5156
5157 /* Get call times */
5158 switch (inv->state) {
5159 case PJSIP_INV_STATE_EARLY:
5160 case PJSIP_INV_STATE_CONNECTING:
5161 if (call->res_time.sec == 0)
5162 pj_gettimeofday(&call->res_time);
5163 call->last_code = (pjsip_status_code)
5164 e->body.tsx_state.tsx->status_code;
5165 pj_strncpy(&call->last_text,
5166 &e->body.tsx_state.tsx->status_text,
5167 sizeof(call->last_text_buf_));
5168 break;
5169 case PJSIP_INV_STATE_CONFIRMED:
5170 if (call->hanging_up) {
5171 /* This can happen if there is a crossover between
5172 * our CANCEL request and the remote's 200 response.
5173 * So we send BYE here.
5174 */
5175 call_inv_end_session(call, 200, NULL, NULL);
5176 return;
5177 }
5178 pj_gettimeofday(&call->conn_time);
5179
5180 if (call->trickle_ice.enabled) {
5181 call->trickle_ice.remote_dlg_est = PJ_TRUE;
5182 pjsua_ice_check_start_trickling(call, PJ_FALSE, NULL);
5183 }
5184
5185 /* See if auto reinvite was pended as media update was done in the
5186 * EARLY state and remote does not support UPDATE.
5187 */
5188 if (call->reinv_pending) {
5189 call->reinv_pending = PJ_FALSE;
5190 pjsua_call_schedule_reinvite_check(call, 0);
5191 }
5192 break;
5193 case PJSIP_INV_STATE_DISCONNECTED:
5194 pj_gettimeofday(&call->dis_time);
5195 if (call->res_time.sec == 0)
5196 pj_gettimeofday(&call->res_time);
5197 if (e->type == PJSIP_EVENT_TSX_STATE &&
5198 e->body.tsx_state.tsx->status_code > (int)call->last_code)
5199 {
5200 call->last_code = (pjsip_status_code)
5201 e->body.tsx_state.tsx->status_code;
5202 pj_strncpy(&call->last_text,
5203 &e->body.tsx_state.tsx->status_text,
5204 sizeof(call->last_text_buf_));
5205 } else {
5206 call->last_code = PJSIP_SC_REQUEST_TERMINATED;
5207 pj_strncpy(&call->last_text,
5208 pjsip_get_status_text(call->last_code),
5209 sizeof(call->last_text_buf_));
5210 }
5211
5212 /* Stop reinvite timer, if it is active */
5213 if (call->reinv_timer.id) {
5214 pjsua_cancel_timer(&call->reinv_timer);
5215 call->reinv_timer.id = PJ_FALSE;
5216 }
5217 break;
5218 default:
5219 call->last_code = (pjsip_status_code)
5220 e->body.tsx_state.tsx->status_code;
5221 pj_strncpy(&call->last_text,
5222 &e->body.tsx_state.tsx->status_text,
5223 sizeof(call->last_text_buf_));
5224 break;
5225 }
5226
5227 /* If this is an outgoing INVITE that was created because of
5228 * REFER/transfer, send NOTIFY to transferer.
5229 */
5230 if (call->xfer_sub && e->type==PJSIP_EVENT_TSX_STATE) {
5231 int st_code = -1;
5232 pjsip_evsub_state ev_state = PJSIP_EVSUB_STATE_ACTIVE;
5233
5234
5235 switch (call->inv->state) {
5236 case PJSIP_INV_STATE_NULL:
5237 case PJSIP_INV_STATE_CALLING:
5238 /* Do nothing */
5239 break;
5240
5241 case PJSIP_INV_STATE_EARLY:
5242 case PJSIP_INV_STATE_CONNECTING:
5243 st_code = e->body.tsx_state.tsx->status_code;
5244 if (call->inv->state == PJSIP_INV_STATE_CONNECTING)
5245 ev_state = PJSIP_EVSUB_STATE_TERMINATED;
5246 else
5247 ev_state = PJSIP_EVSUB_STATE_ACTIVE;
5248 break;
5249
5250 case PJSIP_INV_STATE_CONFIRMED:
5251 #if 0
5252 /* We don't need this, as we've terminated the subscription in
5253 * CONNECTING state.
5254 */
5255 /* When state is confirmed, send the final 200/OK and terminate
5256 * subscription.
5257 */
5258 st_code = e->body.tsx_state.tsx->status_code;
5259 ev_state = PJSIP_EVSUB_STATE_TERMINATED;
5260 #endif
5261 break;
5262
5263 case PJSIP_INV_STATE_DISCONNECTED:
5264 st_code = e->body.tsx_state.tsx->status_code;
5265 ev_state = PJSIP_EVSUB_STATE_TERMINATED;
5266 break;
5267
5268 case PJSIP_INV_STATE_INCOMING:
5269 /* Nothing to do. Just to keep gcc from complaining about
5270 * unused enums.
5271 */
5272 break;
5273 }
5274
5275 if (st_code != -1) {
5276 pjsip_tx_data *tdata;
5277 pj_status_t status;
5278
5279 status = pjsip_xfer_notify( call->xfer_sub,
5280 ev_state, st_code,
5281 NULL, &tdata);
5282 if (status != PJ_SUCCESS) {
5283 pjsua_perror(THIS_FILE, "Unable to create NOTIFY", status);
5284 } else {
5285 status = pjsip_xfer_send_request(call->xfer_sub, tdata);
5286 if (status != PJ_SUCCESS) {
5287 pjsua_perror(THIS_FILE, "Unable to send NOTIFY", status);
5288 }
5289 }
5290 }
5291 }
5292
5293 /* Destroy media session when invite session is disconnected. */
5294 if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
5295 PJSUA_LOCK();
5296
5297 if (!call->hanging_up)
5298 pjsua_media_channel_deinit(call->index);
5299
5300 PJSUA_UNLOCK();
5301 }
5302
5303 /* Release locks before calling callbacks, to avoid deadlock. */
5304 num_locks = PJSUA_RELEASE_LOCK();
5305
5306 /* Ticket #1627: Invoke on_call_tsx_state() when call is disconnected.
5307 *
5308 * Update: No longer necessary. Since #2600 (immediate hangup), call is
5309 * disconnected before BYE is sent.
5310 */
5311 /*
5312 if (inv->state == PJSIP_INV_STATE_DISCONNECTED &&
5313 e->type == PJSIP_EVENT_TSX_STATE &&
5314 call->inv &&
5315 pjsua_var.ua_cfg.cb.on_call_tsx_state)
5316 {
5317 (*pjsua_var.ua_cfg.cb.on_call_tsx_state)(call->index,
5318 e->body.tsx_state.tsx, e);
5319 }
5320 */
5321
5322 if (!call->hanging_up && pjsua_var.ua_cfg.cb.on_call_state)
5323 (*pjsua_var.ua_cfg.cb.on_call_state)(call->index, e);
5324
5325 /* Re-acquire the locks. */
5326 PJSUA_RELOCK(num_locks);
5327
5328 /* call->inv may be NULL now */
5329
5330 /* Finally, free call when invite session is disconnected. */
5331 if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
5332
5333 PJSUA_LOCK();
5334
5335 /* Free call */
5336 call->inv = NULL;
5337
5338 pj_assert(pjsua_var.call_cnt > 0);
5339 --pjsua_var.call_cnt;
5340
5341 /* Reset call */
5342 reset_call(call->index);
5343
5344 pjsua_check_snd_dev_idle();
5345
5346 PJSUA_UNLOCK();
5347 }
5348 pj_log_pop_indent();
5349 }
5350
5351 /*
5352 * This callback is called by invite session framework when UAC session
5353 * has forked.
5354 */
5355 static void pjsua_call_on_forked( pjsip_inv_session *inv,
5356 pjsip_event *e)
5357 {
5358 PJ_UNUSED_ARG(inv);
5359 PJ_UNUSED_ARG(e);
5360
5361 PJ_TODO(HANDLE_FORKED_DIALOG);
5362 }
5363
5364
5365 /*
5366 * Callback from UA layer when forked dialog response is received.
5367 */
5368 pjsip_dialog* on_dlg_forked(pjsip_dialog *dlg, pjsip_rx_data *res)
5369 {
5370 if (dlg->uac_has_2xx &&
5371 res->msg_info.cseq->method.id == PJSIP_INVITE_METHOD &&
5372 pjsip_rdata_get_tsx(res) == NULL &&
5373 res->msg_info.msg->line.status.code/100 == 2)
5374 {
5375 pjsip_dialog *forked_dlg;
5376 pjsip_tx_data *bye, *ack;
5377 pj_status_t status;
5378
5379 /* Create forked dialog */
5380 status = pjsip_dlg_fork(dlg, res, &forked_dlg);
5381 if (status != PJ_SUCCESS)
5382 return NULL;
5383
5384 pjsip_dlg_inc_lock(forked_dlg);
5385
5386 /* Respond with ACK first */
5387 status = pjsip_dlg_create_request(forked_dlg, &pjsip_ack_method,
5388 res->msg_info.cseq->cseq, &ack);
5389 if (status == PJ_SUCCESS)
5390 pjsip_dlg_send_request(forked_dlg, ack, -1, NULL);
5391
5392 /* Disconnect the call */
5393 status = pjsip_dlg_create_request(forked_dlg, pjsip_get_bye_method(),
5394 -1, &bye);
5395 if (status == PJ_SUCCESS) {
5396 status = pjsip_dlg_send_request(forked_dlg, bye, -1, NULL);
5397 }
5398
5399 pjsip_dlg_dec_lock(forked_dlg);
5400
5401 if (status != PJ_SUCCESS) {
5402 return NULL;
5403 }
5404
5405 return forked_dlg;
5406
5407 } else {
5408 return dlg;
5409 }
5410 }
5411
5412 /*
5413 * Disconnect call upon error.
5414 */
5415 static void call_disconnect( pjsip_inv_session *inv,
5416 int code,
5417 const pj_str_t *reason)
5418 {
5419 pjsip_tx_data *tdata;
5420 pj_status_t status;
5421
5422 status = pjsip_inv_end_session(inv, code, reason, &tdata);
5423 if (status != PJ_SUCCESS || !tdata)
5424 return;
5425
5426 #if DISABLED_FOR_TICKET_1185
5427 pjsua_call *call;
5428
5429 /* Add SDP in 488 status */
5430 call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id];
5431
5432 if (call && call->tp && tdata->msg->type==PJSIP_RESPONSE_MSG &&
5433 code==PJSIP_SC_NOT_ACCEPTABLE_HERE)
5434 {
5435 pjmedia_sdp_session *local_sdp;
5436 pjmedia_transport_info ti;
5437
5438 pjmedia_transport_info_init(&ti);
5439 pjmedia_transport_get_info(call->med_tp, &ti);
5440 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool,
5441 1, &ti.sock_info, &local_sdp);
5442 if (status == PJ_SUCCESS) {
5443 pjsip_create_sdp_body(tdata->pool, local_sdp,
5444 &tdata->msg->body);
5445 }
5446 }
5447 #endif
5448
5449 pjsip_inv_send_msg(inv, tdata);
5450 }
5451
5452 /*
5453 * Callback to be called when SDP offer/answer negotiation has just completed
5454 * in the session. This function will start/update media if negotiation
5455 * has succeeded.
5456 */
5457 static void pjsua_call_on_media_update(pjsip_inv_session *inv,
5458 pj_status_t status)
5459 {
5460 pjsua_call *call;
5461 const pjmedia_sdp_session *local_sdp;
5462 const pjmedia_sdp_session *remote_sdp;
5463 //const pj_str_t st_update = {"UPDATE", 6};
5464
5465 pj_log_push_indent();
5466
5467 call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id];
5468
5469 if (call->hanging_up)
5470 goto on_return;
5471
5472 if (status != PJ_SUCCESS) {
5473
5474 pjsua_perror(THIS_FILE, "SDP negotiation has failed", status);
5475
5476 /* Revert back provisional media. */
5477 pjsua_media_prov_revert(call->index);
5478
5479 /* Do not deinitialize media since this may be a re-INVITE or
5480 * UPDATE (which in this case the media should not get affected
5481 * by the failed re-INVITE/UPDATE). The media will be shutdown
5482 * when call is disconnected anyway.
5483 */
5484 /* Stop/destroy media, if any */
5485 /*pjsua_media_channel_deinit(call->index);*/
5486
5487 /* Disconnect call if we're not in the middle of initializing an
5488 * UAS dialog and if this is not a re-INVITE
5489 */
5490 if ((inv->state != PJSIP_INV_STATE_NULL &&
5491 inv->state != PJSIP_INV_STATE_EARLY &&
5492 inv->state != PJSIP_INV_STATE_CONFIRMED) ||
5493 (inv->state == PJSIP_INV_STATE_EARLY && call->med_cnt == 0))
5494 {
5495 pj_str_t reason = pj_str("SDP negotiation failed");
5496 call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE, &reason);
5497 }
5498
5499 goto on_return;
5500 }
5501
5502
5503 /* Get local and remote SDP */
5504 status = pjmedia_sdp_neg_get_active_local(call->inv->neg, &local_sdp);
5505 if (status != PJ_SUCCESS) {
5506 pjsua_perror(THIS_FILE,
5507 "Unable to retrieve currently active local SDP",
5508 status);
5509 //call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE);
5510 goto on_return;
5511 }
5512
5513 status = pjmedia_sdp_neg_get_active_remote(call->inv->neg, &remote_sdp);
5514 if (status != PJ_SUCCESS) {
5515 pjsua_perror(THIS_FILE,
5516 "Unable to retrieve currently active remote SDP",
5517 status);
5518 //call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE);
5519 goto on_return;
5520 }
5521
5522 call->med_update_success = (status == PJ_SUCCESS);
5523
5524 /* Trickle ICE tasks:
5525 * - Check remote SDP for trickle ICE support & start sending SIP INFO.
5526 */
5527 {
5528 unsigned i;
5529 for (i = 0; i < remote_sdp->media_count; ++i) {
5530 if (pjmedia_ice_sdp_has_trickle(remote_sdp, i))
5531 break;
5532 }
5533 call->trickle_ice.remote_sup = (i < remote_sdp->media_count);
5534 if (call->trickle_ice.remote_sup)
5535 pjsua_ice_check_start_trickling(call, PJ_FALSE, NULL);
5536 }
5537
5538 /* Update remote's NAT type */
5539 if (pjsua_var.ua_cfg.nat_type_in_sdp) {
5540 update_remote_nat_type(call, remote_sdp);
5541 }
5542
5543 /* Update media channel with the new SDP */
5544 status = pjsua_media_channel_update(call->index, local_sdp, remote_sdp);
5545
5546 /* If this is not the initial INVITE, don't disconnect call due to
5547 * no media after SDP negotiation.
5548 */
5549 if (status == PJMEDIA_SDPNEG_ENOMEDIA &&
5550 call->inv->state == PJSIP_INV_STATE_CONFIRMED)
5551 {
5552 status = PJ_SUCCESS;
5553 }
5554
5555 /* Disconnect call after failure in media channel update */
5556 if (status != PJ_SUCCESS) {
5557 pj_str_t reason = pj_str("Unable to create media session");
5558
5559 pjsua_perror(THIS_FILE, "Unable to create media session",
5560 status);
5561 call_disconnect(inv, PJSIP_SC_NOT_ACCEPTABLE_HERE, &reason);
5562 /* No need to deinitialize; media will be shutdown when call
5563 * state is disconnected anyway.
5564 */
5565 /*pjsua_media_channel_deinit(call->index);*/
5566 goto on_return;
5567 }
5568
5569 /* Ticket #476: make sure only one codec is specified in the answer. */
5570 pjsua_call_schedule_reinvite_check(call, 0);
5571
5572 /* Call application callback, if any */
5573 if (!call->hanging_up && pjsua_var.ua_cfg.cb.on_call_media_state)
5574 pjsua_var.ua_cfg.cb.on_call_media_state(call->index);
5575
5576 on_return:
5577 pj_log_pop_indent();
5578 }
5579
5580
5581 /* Modify SDP for call hold. */
5582 static pj_status_t modify_sdp_of_call_hold(pjsua_call *call,
5583 pj_pool_t *pool,
5584 pjmedia_sdp_session *sdp,
5585 pj_bool_t as_answerer)
5586 {
5587 unsigned mi;
5588
5589 /* Call-hold is done by set the media direction to 'sendonly'
5590 * (PJMEDIA_DIR_ENCODING), except when current media direction is
5591 * 'inactive' (PJMEDIA_DIR_NONE).
5592 * (See RFC 3264 Section 8.4 and RFC 4317 Section 3.1)
5593 */
5594 /* https://github.com/pjsip/pjproject/issues/880
5595 if (call->dir != PJMEDIA_DIR_ENCODING) {
5596 */
5597 /* https://github.com/pjsip/pjproject/issues/1142:
5598 * configuration to use c=0.0.0.0 for call hold.
5599 */
5600
5601 for (mi=0; mi<sdp->media_count; ++mi) {
5602 pjmedia_sdp_media *m = sdp->media[mi];
5603
5604 if (call->call_hold_type == PJSUA_CALL_HOLD_TYPE_RFC2543) {
5605 pjmedia_sdp_conn *conn;
5606 pjmedia_sdp_attr *attr;
5607
5608 /* Get SDP media connection line */
5609 conn = m->conn;
5610 if (!conn)
5611 conn = sdp->conn;
5612
5613 /* Modify address */
5614 conn->addr = pj_str("0.0.0.0");
5615
5616 /* Remove existing directions attributes */
5617 pjmedia_sdp_media_remove_all_attr(m, "sendrecv");
5618 pjmedia_sdp_media_remove_all_attr(m, "sendonly");
5619 pjmedia_sdp_media_remove_all_attr(m, "recvonly");
5620 pjmedia_sdp_media_remove_all_attr(m, "inactive");
5621
5622 /* Add inactive attribute */
5623 attr = pjmedia_sdp_attr_create(pool, "inactive", NULL);
5624 pjmedia_sdp_media_add_attr(m, attr);
5625
5626
5627 } else {
5628 pjmedia_sdp_attr *attr;
5629
5630 /* Remove existing directions attributes */
5631 pjmedia_sdp_media_remove_all_attr(m, "sendrecv");
5632 pjmedia_sdp_media_remove_all_attr(m, "sendonly");
5633 pjmedia_sdp_media_remove_all_attr(m, "recvonly");
5634 pjmedia_sdp_media_remove_all_attr(m, "inactive");
5635
5636 /* When as answerer, just simply set dir to "sendonly", note that
5637 * if the offer uses "sendonly" or "inactive", the SDP negotiator
5638 * will change our answer dir to "inactive".
5639 */
5640 if (as_answerer || (call->media[mi].dir & PJMEDIA_DIR_ENCODING)) {
5641 /* Add sendonly attribute */
5642 attr = pjmedia_sdp_attr_create(pool, "sendonly", NULL);
5643 pjmedia_sdp_media_add_attr(m, attr);
5644 } else {
5645 /* Add inactive attribute */
5646 attr = pjmedia_sdp_attr_create(pool, "inactive", NULL);
5647 pjmedia_sdp_media_add_attr(m, attr);
5648 }
5649 }
5650 }
5651
5652 return PJ_SUCCESS;
5653 }
5654
5655 /* Create SDP for call hold. */
5656 static pj_status_t create_sdp_of_call_hold(pjsua_call *call,
5657 pjmedia_sdp_session **p_sdp)
5658 {
5659 pj_status_t status;
5660 pj_pool_t *pool;
5661 pjmedia_sdp_session *sdp;
5662
5663 /* Use call's provisional pool */
5664 pool = call->inv->pool_prov;
5665
5666 /* Create new offer */
5667 status = pjsua_media_channel_create_sdp(call->index, pool, NULL, &sdp,
5668 NULL);
5669 if (status != PJ_SUCCESS) {
5670 pjsua_perror(THIS_FILE, "Unable to create local SDP", status);
5671 return status;
5672 }
5673
5674 status = modify_sdp_of_call_hold(call, pool, sdp, PJ_FALSE);
5675 if (status != PJ_SUCCESS)
5676 return status;
5677
5678 *p_sdp = sdp;
5679
5680 return PJ_SUCCESS;
5681 }
5682
5683 /*
5684 * Called when session received new offer.
5685 */
5686 static void pjsua_call_on_rx_offer(pjsip_inv_session *inv,
5687 struct pjsip_inv_on_rx_offer_cb_param *param)
5688 {
5689 pjsua_call *call;
5690 pjmedia_sdp_session *answer;
5691 unsigned i;
5692 pj_status_t status;
5693 const pjmedia_sdp_session *offer = param->offer;
5694 pjsua_call_setting opt;
5695 pj_bool_t async = PJ_FALSE;
5696
5697 call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id];
5698 if (call->hanging_up)
5699 return;
5700
5701 /* Supply candidate answer */
5702 PJ_LOG(4,(THIS_FILE, "Call %d: received updated media offer",
5703 call->index));
5704
5705 pj_log_push_indent();
5706
5707 if (pjsua_call_media_is_changing(call)) {
5708 PJ_LOG(1,(THIS_FILE, "Unable to process offer" ERR_MEDIA_CHANGING));
5709 goto on_return;
5710 }
5711
5712 pjsua_call_cleanup_flag(&call->opt);
5713 opt = call->opt;
5714
5715 if (pjsua_var.ua_cfg.cb.on_call_rx_reinvite &&
5716 param->rdata->msg_info.msg->type == PJSIP_REQUEST_MSG &&
5717 param->rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD)
5718 {
5719 pjsip_status_code code = PJSIP_SC_OK;
5720
5721 /* If on_call_rx_reinvite() callback is implemented,
5722 * call it first.
5723 */
5724 (*pjsua_var.ua_cfg.cb.on_call_rx_reinvite)(
5725 call->index, offer,
5726 (pjsip_rx_data *)param->rdata,
5727 NULL, &async, &code, &opt);
5728 if (async) {
5729 pjsip_tx_data *response;
5730
5731 status = pjsip_inv_initial_answer(inv,
5732 (pjsip_rx_data *)param->rdata,
5733 100, NULL, NULL, &response);
5734 if (status != PJ_SUCCESS) {
5735 if (response) {
5736 pjsip_tx_data_dec_ref(response);
5737 }
5738 PJ_PERROR(3, (THIS_FILE, status,
5739 "Failed to create initial answer"));
5740 goto on_return;
5741 }
5742
5743 status = pjsip_inv_send_msg(inv, response);
5744 if (status != PJ_SUCCESS) {
5745 PJ_PERROR(3, (THIS_FILE, status,
5746 "Failed to send initial answer"));
5747 goto on_return;
5748 }
5749
5750 PJ_LOG(4,(THIS_FILE, "App will manually answer the re-INVITE "
5751 "on call %d", call->index));
5752 }
5753 if (code != PJSIP_SC_OK) {
5754 PJ_LOG(4,(THIS_FILE, "Rejecting re-INVITE updated media offer "
5755 "on call %d", call->index));
5756 goto on_return;
5757 }
5758
5759 call->opt = opt;
5760 }
5761
5762 if (pjsua_var.ua_cfg.cb.on_call_rx_offer && !async) {
5763 pjsip_status_code code = PJSIP_SC_OK;
5764
5765 (*pjsua_var.ua_cfg.cb.on_call_rx_offer)(call->index, offer, NULL,
5766 &code, &opt);
5767
5768 if (code != PJSIP_SC_OK) {
5769 PJ_LOG(4,(THIS_FILE, "Rejecting updated media offer on call %d",
5770 call->index));
5771 goto on_return;
5772 }
5773
5774 call->opt = opt;
5775 }
5776
5777 /* Re-init media for the new remote offer before creating SDP */
5778 status = apply_call_setting(call, &call->opt, offer);
5779 if (status != PJ_SUCCESS)
5780 goto on_return;
5781
5782 status = pjsua_media_channel_create_sdp(call->index,
5783 call->inv->pool_prov,
5784 offer, &answer, NULL);
5785 if (status != PJ_SUCCESS) {
5786 pjsua_perror(THIS_FILE, "Unable to create local SDP", status);
5787 goto on_return;
5788 }
5789
5790 if (async) {
5791 call->rx_reinv_async = async;
5792 goto on_return;
5793 }
5794
5795 /* Validate media count in the generated answer */
5796 pj_assert(answer->media_count == offer->media_count);
5797
5798 /* Check if offer's conn address is zero */
5799 for (i = 0; i < answer->media_count; ++i) {
5800 pjmedia_sdp_conn *conn;
5801
5802 conn = offer->media[i]->conn;
5803 if (!conn)
5804 conn = offer->conn;
5805
5806 if (pj_strcmp2(&conn->addr, "0.0.0.0")==0 ||
5807 pj_strcmp2(&conn->addr, "0")==0)
5808 {
5809 pjmedia_sdp_conn *a_conn = answer->media[i]->conn;
5810
5811 /* Modify answer address */
5812 if (a_conn) {
5813 a_conn->addr = pj_str("0.0.0.0");
5814 } else if (answer->conn == NULL ||
5815 pj_strcmp2(&answer->conn->addr, "0.0.0.0") != 0)
5816 {
5817 a_conn = PJ_POOL_ZALLOC_T(call->inv->pool_prov,
5818 pjmedia_sdp_conn);
5819 a_conn->net_type = pj_str("IN");
5820 a_conn->addr_type = pj_str("IP4");
5821 a_conn->addr = pj_str("0.0.0.0");
5822 answer->media[i]->conn = a_conn;
5823 }
5824 }
5825 }
5826
5827 /* Check if call is on-hold */
5828 if (call->local_hold) {
5829 modify_sdp_of_call_hold(call, call->inv->pool_prov, answer, PJ_TRUE);
5830 }
5831
5832 status = pjsip_inv_set_sdp_answer(call->inv, answer);
5833 if (status != PJ_SUCCESS) {
5834 pjsua_perror(THIS_FILE, "Unable to set answer", status);
5835 goto on_return;
5836 }
5837
5838 on_return:
5839 pj_log_pop_indent();
5840 }
5841
5842
5843 /*
5844 * Called when receiving re-INVITE.
5845 */
5846 static pj_status_t pjsua_call_on_rx_reinvite(pjsip_inv_session *inv,
5847 const pjmedia_sdp_session *offer,
5848 pjsip_rx_data *rdata)
5849 {
5850 pjsua_call *call;
5851 pj_bool_t async;
5852
5853 PJ_UNUSED_ARG(offer);
5854 PJ_UNUSED_ARG(rdata);
5855
5856 call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id];
5857 async = call->rx_reinv_async;
5858 call->rx_reinv_async = PJ_FALSE;
5859
5860 return (async? PJ_SUCCESS: !PJ_SUCCESS);
5861 }
5862
5863
5864 /*
5865 * Called to generate new offer.
5866 */
5867 static void pjsua_call_on_create_offer(pjsip_inv_session *inv,
5868 pjmedia_sdp_session **offer)
5869 {
5870 pjsua_call *call;
5871 pj_status_t status;
5872 unsigned mi;
5873
5874 pj_log_push_indent();
5875
5876 call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id];
5877 if (call->hanging_up || pjsua_call_media_is_changing(call)) {
5878 *offer = NULL;
5879 PJ_LOG(1,(THIS_FILE, "Unable to create offer%s",
5880 call->hanging_up? ", call hanging up":
5881 ERR_MEDIA_CHANGING));
5882 goto on_return;
5883 }
5884
5885 #if RESTART_ICE_ON_REINVITE
5886
5887 /* Ticket #1783, RFC 5245 section 12.5:
5888 * If an agent receives a mid-dialog re-INVITE that contains no offer,
5889 * it MUST restart ICE for each media stream and go through the process
5890 * of gathering new candidates.
5891 */
5892 for (mi=0; mi<call->med_cnt; ++mi) {
5893 pjsua_call_media *call_med = &call->media[mi];
5894 pjmedia_transport_info tpinfo;
5895 pjmedia_ice_transport_info *ice_info;
5896
5897 /* Check if the media is using ICE */
5898 pjmedia_transport_info_init(&tpinfo);
5899 pjmedia_transport_get_info(call_med->tp, &tpinfo);
5900 ice_info = (pjmedia_ice_transport_info*)
5901 pjmedia_transport_info_get_spc_info(
5902 &tpinfo, PJMEDIA_TRANSPORT_TYPE_ICE);
5903 if (!ice_info)
5904 continue;
5905
5906 /* Stop and re-init ICE stream transport.
5907 * According to RFC 5245 section 9.1.1.1, during ICE restart,
5908 * media can continue to be sent to the previously validated pair.
5909 */
5910 pjmedia_transport_media_stop(call_med->tp);
5911 pjmedia_transport_media_create(call_med->tp, call->inv->pool_prov,
5912 (call_med->enable_rtcp_mux?
5913 PJMEDIA_TPMED_RTCP_MUX: 0),
5914 NULL, mi);
5915
5916 PJ_LOG(4, (THIS_FILE, "Restarting ICE for media %d", mi));
5917 }
5918 #endif
5919
5920 pjsua_call_cleanup_flag(&call->opt);
5921
5922 if (pjsua_var.ua_cfg.cb.on_call_tx_offer) {
5923 (*pjsua_var.ua_cfg.cb.on_call_tx_offer)(call->index, NULL,
5924 &call->opt);
5925 }
5926
5927 /* We may need to re-initialize media before creating SDP */
5928 if (call->med_prov_cnt == 0 || pjsua_var.ua_cfg.cb.on_call_tx_offer) {
5929 status = apply_call_setting(call, &call->opt, NULL);
5930 if (status != PJ_SUCCESS)
5931 goto on_return;
5932 }
5933
5934 /* See if we've put call on hold. */
5935 if (call->local_hold) {
5936 PJ_LOG(4,(THIS_FILE,
5937 "Call %d: call is on-hold locally, creating call-hold SDP ",
5938 call->index));
5939 status = create_sdp_of_call_hold( call, offer );
5940 } else {
5941 PJ_LOG(4,(THIS_FILE, "Call %d: asked to send a new offer",
5942 call->index));
5943
5944 status = pjsua_media_channel_create_sdp(call->index,
5945 call->inv->pool_prov,
5946 NULL, offer, NULL);
5947 }
5948
5949 if (status != PJ_SUCCESS) {
5950 pjsua_perror(THIS_FILE, "Unable to create local SDP", status);
5951 goto on_return;
5952 }
5953
5954 on_return:
5955 pj_log_pop_indent();
5956 }
5957
5958
5959 /*
5960 * Callback called by event framework when the xfer subscription state
5961 * has changed.
5962 */
5963 static void xfer_client_on_evsub_state( pjsip_evsub *sub, pjsip_event *event)
5964 {
5965
5966 PJ_UNUSED_ARG(event);
5967
5968 pj_log_push_indent();
5969
5970 /*
5971 * When subscription is accepted (got 200/OK to REFER), check if
5972 * subscription suppressed.
5973 */
5974 if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACCEPTED) {
5975
5976 pjsip_rx_data *rdata;
5977 pjsip_generic_string_hdr *refer_sub;
5978 const pj_str_t REFER_SUB = { "Refer-Sub", 9 };
5979 pjsua_call *call;
5980
5981 call = (pjsua_call*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
5982
5983 /* Must be receipt of response message */
5984 pj_assert(event->type == PJSIP_EVENT_TSX_STATE &&
5985 event->body.tsx_state.type == PJSIP_EVENT_RX_MSG);
5986 rdata = event->body.tsx_state.src.rdata;
5987
5988 /* Find Refer-Sub header */
5989 refer_sub = (pjsip_generic_string_hdr*)
5990 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg,
5991 &REFER_SUB, NULL);
5992
5993 /* Check if subscription is suppressed */
5994 if (refer_sub && pj_stricmp2(&refer_sub->hvalue, "false")==0) {
5995 /* Since no subscription is desired, assume that call has been
5996 * transferred successfully.
5997 */
5998 if (call && !call->hanging_up &&
5999 pjsua_var.ua_cfg.cb.on_call_transfer_status)
6000 {
6001 const pj_str_t ACCEPTED = { "Accepted", 8 };
6002 pj_bool_t cont = PJ_FALSE;
6003 (*pjsua_var.ua_cfg.cb.on_call_transfer_status)(call->index,
6004 200,
6005 &ACCEPTED,
6006 PJ_TRUE,
6007 &cont);
6008 }
6009
6010 /* Yes, subscription is suppressed.
6011 * Terminate our subscription now.
6012 */
6013 PJ_LOG(4,(THIS_FILE, "Xfer subscription suppressed, terminating "
6014 "event subcription..."));
6015 pjsip_evsub_terminate(sub, PJ_TRUE);
6016
6017 } else {
6018 /* Notify application about call transfer progress.
6019 * Initially notify with 100/Accepted status.
6020 */
6021 if (call && !call->hanging_up &&
6022 pjsua_var.ua_cfg.cb.on_call_transfer_status)
6023 {
6024 const pj_str_t ACCEPTED = { "Accepted", 8 };
6025 pj_bool_t cont = PJ_FALSE;
6026 (*pjsua_var.ua_cfg.cb.on_call_transfer_status)(call->index,
6027 100,
6028 &ACCEPTED,
6029 PJ_FALSE,
6030 &cont);
6031 }
6032 }
6033 }
6034 /*
6035 * On incoming NOTIFY or an error response, notify application about call
6036 * transfer progress.
6037 */
6038 else if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACTIVE ||
6039 pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED)
6040 {
6041 pjsua_call *call;
6042 pjsip_msg *msg;
6043 pjsip_msg_body *body;
6044 pjsip_status_line status_line;
6045 pj_bool_t is_last;
6046 pj_bool_t cont;
6047 pj_status_t status;
6048
6049 call = (pjsua_call*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
6050
6051 /* When subscription is terminated, clear the xfer_sub member of
6052 * the inv_data.
6053 */
6054 if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
6055 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
6056 PJ_LOG(4,(THIS_FILE, "Xfer client subscription terminated"));
6057
6058 }
6059
6060 if (!call || call->hanging_up || !event ||
6061 !pjsua_var.ua_cfg.cb.on_call_transfer_status)
6062 {
6063 /* Application is not interested with call progress status */
6064 goto on_return;
6065 }
6066
6067 if (event->type == PJSIP_EVENT_TSX_STATE &&
6068 event->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
6069 {
6070 pjsip_rx_data *rdata;
6071
6072 rdata = event->body.tsx_state.src.rdata;
6073 msg = rdata->msg_info.msg;
6074
6075 /* This better be a NOTIFY request */
6076 if (pjsip_method_cmp(&msg->line.req.method,
6077 pjsip_get_notify_method()) == 0)
6078 {
6079 /* Check if there's body */
6080 body = msg->body;
6081 if (!body) {
6082 PJ_LOG(2, (THIS_FILE,
6083 "Warning: received NOTIFY without message "
6084 "body"));
6085 goto on_return;
6086 }
6087
6088 /* Check for appropriate content */
6089 if (pj_stricmp2(&body->content_type.type, "message") != 0 ||
6090 pj_stricmp2(&body->content_type.subtype, "sipfrag") != 0)
6091 {
6092 PJ_LOG(2, (THIS_FILE,
6093 "Warning: received NOTIFY with non "
6094 "message/sipfrag content"));
6095 goto on_return;
6096 }
6097
6098 /* Try to parse the content */
6099 status = pjsip_parse_status_line((char*)body->data, body->len,
6100 &status_line);
6101 if (status != PJ_SUCCESS) {
6102 PJ_LOG(2, (THIS_FILE,
6103 "Warning: received NOTIFY with invalid "
6104 "message/sipfrag content"));
6105 goto on_return;
6106 }
6107 } else {
6108 status_line.code = msg->line.status.code;
6109 status_line.reason = msg->line.status.reason;
6110 }
6111 } else {
6112 status_line.code = 500;
6113 status_line.reason = *pjsip_get_status_text(500);
6114 }
6115
6116 /* Notify application */
6117 is_last = (pjsip_evsub_get_state(sub)==PJSIP_EVSUB_STATE_TERMINATED);
6118 cont = !is_last;
6119 (*pjsua_var.ua_cfg.cb.on_call_transfer_status)(call->index,
6120 status_line.code,
6121 &status_line.reason,
6122 is_last, &cont);
6123
6124 if (!cont) {
6125 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
6126 }
6127
6128 /* If the call transfer has completed but the subscription is
6129 * not terminated, terminate it now.
6130 */
6131 if (status_line.code/100 == 2 && !is_last) {
6132 pjsip_tx_data *tdata;
6133
6134 status = pjsip_evsub_initiate(sub, pjsip_get_subscribe_method(),
6135 0, &tdata);
6136 if (status == PJ_SUCCESS)
6137 status = pjsip_evsub_send_request(sub, tdata);
6138 }
6139 }
6140
6141 on_return:
6142 pj_log_pop_indent();
6143 }
6144
6145
6146 /*
6147 * Callback called by event framework when the xfer subscription state
6148 * has changed.
6149 */
6150 static void xfer_server_on_evsub_state( pjsip_evsub *sub, pjsip_event *event)
6151 {
6152 PJ_UNUSED_ARG(event);
6153
6154 pj_log_push_indent();
6155
6156 /*
6157 * When subscription is terminated, clear the xfer_sub member of
6158 * the inv_data.
6159 */
6160 if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
6161 pjsua_call *call;
6162
6163 call = (pjsua_call*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
6164 if (!call)
6165 goto on_return;
6166
6167 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
6168 call->xfer_sub = NULL;
6169
6170 PJ_LOG(4,(THIS_FILE, "Xfer server subscription terminated"));
6171 }
6172
6173 on_return:
6174 pj_log_pop_indent();
6175 }
6176
6177
6178 /*
6179 * Follow transfer (REFER) request.
6180 */
6181 static void on_call_transferred( pjsip_inv_session *inv,
6182 pjsip_rx_data *rdata )
6183 {
6184 pj_status_t status;
6185 pjsip_tx_data *tdata;
6186 pjsua_call *existing_call;
6187 int new_call;
6188 const pj_str_t str_refer_to = { "Refer-To", 8};
6189 const pj_str_t str_refer_sub = { "Refer-Sub", 9 };
6190 const pj_str_t str_ref_by = { "Referred-By", 11 };
6191 pjsip_generic_string_hdr *refer_to;
6192 pjsip_generic_string_hdr *refer_sub;
6193 pjsip_hdr *ref_by_hdr;
6194 pj_bool_t no_refer_sub = PJ_FALSE;
6195 char *uri;
6196 pjsua_msg_data msg_data;
6197 pj_str_t tmp;
6198 pjsip_status_code code;
6199 pjsip_evsub *sub;
6200 pjsua_call_setting call_opt;
6201
6202 pj_log_push_indent();
6203
6204 existing_call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id];
6205 if (existing_call->hanging_up) {
6206 pjsip_dlg_respond( inv->dlg, rdata, 487, NULL, NULL, NULL);
6207 goto on_return;
6208 }
6209
6210 /* Find the Refer-To header */
6211 refer_to = (pjsip_generic_string_hdr*)
6212 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_to, NULL);
6213
6214 if (refer_to == NULL) {
6215 /* Invalid Request.
6216 * No Refer-To header!
6217 */
6218 PJ_LOG(4,(THIS_FILE, "Received REFER without Refer-To header!"));
6219 pjsip_dlg_respond( inv->dlg, rdata, 400, NULL, NULL, NULL);
6220 goto on_return;
6221 }
6222
6223 /* Find optional Refer-Sub header */
6224 refer_sub = (pjsip_generic_string_hdr*)
6225 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_sub, NULL);
6226
6227 if (refer_sub) {
6228 if (pj_strnicmp2(&refer_sub->hvalue, "true", 4)!=0)
6229 no_refer_sub = PJ_TRUE;
6230 }
6231
6232 /* Find optional Referred-By header (to be copied onto outgoing INVITE
6233 * request.
6234 */
6235 ref_by_hdr = (pjsip_hdr*)
6236 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_ref_by,
6237 NULL);
6238
6239 /* Notify callback */
6240 code = PJSIP_SC_ACCEPTED;
6241 if (pjsua_var.ua_cfg.cb.on_call_transfer_request) {
6242 (*pjsua_var.ua_cfg.cb.on_call_transfer_request)(existing_call->index,
6243 &refer_to->hvalue,
6244 &code);
6245 }
6246
6247 pjsua_call_cleanup_flag(&existing_call->opt);
6248 call_opt = existing_call->opt;
6249 if (pjsua_var.ua_cfg.cb.on_call_transfer_request2) {
6250 (*pjsua_var.ua_cfg.cb.on_call_transfer_request2)(existing_call->index,
6251 &refer_to->hvalue,
6252 &code,
6253 &call_opt);
6254 }
6255
6256 if (code < 200)
6257 code = PJSIP_SC_ACCEPTED;
6258 if (code >= 300) {
6259 /* Application rejects call transfer request */
6260 pjsip_dlg_respond( inv->dlg, rdata, code, NULL, NULL, NULL);
6261 goto on_return;
6262 }
6263
6264 PJ_LOG(3,(THIS_FILE, "Call to %.*s is being transferred to %.*s",
6265 (int)inv->dlg->remote.info_str.slen,
6266 inv->dlg->remote.info_str.ptr,
6267 (int)refer_to->hvalue.slen,
6268 refer_to->hvalue.ptr));
6269
6270 if (no_refer_sub) {
6271 /*
6272 * Always answer with 2xx.
6273 */
6274 pjsip_tx_data *tdata2;
6275 const pj_str_t str_false = { "false", 5};
6276 pjsip_hdr *hdr;
6277
6278 status = pjsip_dlg_create_response(inv->dlg, rdata, code, NULL,
6279 &tdata2);
6280 if (status != PJ_SUCCESS) {
6281 pjsua_perror(THIS_FILE, "Unable to create 2xx response to REFER",
6282 status);
6283 goto on_return;
6284 }
6285
6286 /* Add Refer-Sub header */
6287 hdr = (pjsip_hdr*)
6288 pjsip_generic_string_hdr_create(tdata2->pool, &str_refer_sub,
6289 &str_false);
6290 pjsip_msg_add_hdr(tdata2->msg, hdr);
6291
6292
6293 /* Send answer */
6294 status = pjsip_dlg_send_response(inv->dlg, pjsip_rdata_get_tsx(rdata),
6295 tdata2);
6296 if (status != PJ_SUCCESS) {
6297 pjsua_perror(THIS_FILE, "Unable to create 2xx response to REFER",
6298 status);
6299 goto on_return;
6300 }
6301
6302 /* Don't have subscription */
6303 sub = NULL;
6304
6305 } else {
6306 struct pjsip_evsub_user xfer_cb;
6307 pjsip_hdr hdr_list;
6308
6309 /* Init callback */
6310 pj_bzero(&xfer_cb, sizeof(xfer_cb));
6311 xfer_cb.on_evsub_state = &xfer_server_on_evsub_state;
6312
6313 /* Init additional header list to be sent with REFER response */
6314 pj_list_init(&hdr_list);
6315
6316 /* Create transferee event subscription */
6317 status = pjsip_xfer_create_uas( inv->dlg, &xfer_cb, rdata, &sub);
6318 if (status != PJ_SUCCESS) {
6319 pjsua_perror(THIS_FILE, "Unable to create xfer uas", status);
6320 pjsip_dlg_respond( inv->dlg, rdata, 500, NULL, NULL, NULL);
6321 goto on_return;
6322 }
6323
6324 /* If there's Refer-Sub header and the value is "true", send back
6325 * Refer-Sub in the response with value "true" too.
6326 */
6327 if (refer_sub) {
6328 const pj_str_t str_true = { "true", 4 };
6329 pjsip_hdr *hdr;
6330
6331 hdr = (pjsip_hdr*)
6332 pjsip_generic_string_hdr_create(inv->dlg->pool,
6333 &str_refer_sub,
6334 &str_true);
6335 pj_list_push_back(&hdr_list, hdr);
6336
6337 }
6338
6339 /* Accept the REFER request, send 2xx. */
6340 pjsip_xfer_accept(sub, rdata, code, &hdr_list);
6341
6342 /* Create initial NOTIFY request */
6343 status = pjsip_xfer_notify( sub, PJSIP_EVSUB_STATE_ACTIVE,
6344 100, NULL, &tdata);
6345 if (status != PJ_SUCCESS) {
6346 pjsua_perror(THIS_FILE, "Unable to create NOTIFY to REFER",
6347 status);
6348 goto on_return;
6349 }
6350
6351 /* Send initial NOTIFY request */
6352 status = pjsip_xfer_send_request( sub, tdata);
6353 if (status != PJ_SUCCESS) {
6354 pjsua_perror(THIS_FILE, "Unable to send NOTIFY to REFER", status);
6355 goto on_return;
6356 }
6357 }
6358
6359 /* We're cheating here.
6360 * We need to get a null terminated string from a pj_str_t.
6361 * So grab the pointer from the hvalue and NULL terminate it, knowing
6362 * that the NULL position will be occupied by a newline.
6363 */
6364 uri = refer_to->hvalue.ptr;
6365 uri[refer_to->hvalue.slen] = '\0';
6366
6367 /* Init msg_data */
6368 pjsua_msg_data_init(&msg_data);
6369
6370 /* If Referred-By header is present in the REFER request, copy this
6371 * to the outgoing INVITE request.
6372 */
6373 if (ref_by_hdr != NULL) {
6374 pjsip_hdr *dup = (pjsip_hdr*)
6375 pjsip_hdr_clone(rdata->tp_info.pool, ref_by_hdr);
6376 pj_list_push_back(&msg_data.hdr_list, dup);
6377 }
6378
6379 /* Now make the outgoing call.
6380 * Note that the user_data of the new call is initialized to the
6381 * original call, it is needed by PJSUA2 to update its states.
6382 * While PJSUA app can always override it anytime.
6383 */
6384 tmp = pj_str(uri);
6385 status = pjsua_call_make_call(existing_call->acc_id, &tmp, &call_opt,
6386 existing_call->user_data, &msg_data,
6387 &new_call);
6388 if (status != PJ_SUCCESS) {
6389
6390 /* Notify xferer about the error (if we have subscription) */
6391 if (sub) {
6392 status = pjsip_xfer_notify(sub, PJSIP_EVSUB_STATE_TERMINATED,
6393 500, NULL, &tdata);
6394 if (status != PJ_SUCCESS) {
6395 pjsua_perror(THIS_FILE, "Unable to create NOTIFY to REFER",
6396 status);
6397 goto on_return;
6398 }
6399 status = pjsip_xfer_send_request(sub, tdata);
6400 if (status != PJ_SUCCESS) {
6401 pjsua_perror(THIS_FILE, "Unable to send NOTIFY to REFER",
6402 status);
6403 goto on_return;
6404 }
6405 }
6406 goto on_return;
6407 }
6408
6409 if (sub) {
6410 /* Put the server subscription in inv_data.
6411 * Subsequent state changed in pjsua_inv_on_state_changed() will be
6412 * reported back to the server subscription.
6413 */
6414 pjsua_var.calls[new_call].xfer_sub = sub;
6415
6416 /* Put the invite_data in the subscription. */
6417 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id,
6418 &pjsua_var.calls[new_call]);
6419 }
6420
6421 on_return:
6422 pj_log_pop_indent();
6423 }
6424
6425
6426
6427 /*
6428 * This callback is called when transaction state has changed in INVITE
6429 * session. We use this to trap:
6430 * - incoming REFER request.
6431 * - incoming MESSAGE request.
6432 */
6433 static void pjsua_call_on_tsx_state_changed(pjsip_inv_session *inv,
6434 pjsip_transaction *tsx,
6435 pjsip_event *e)
6436 {
6437 /* Incoming INFO request for media control, DTMF, trickle ICE, etc. */
6438 const pj_str_t STR_APPLICATION = { "application", 11};
6439 const pj_str_t STR_MEDIA_CONTROL_XML = { "media_control+xml", 17 };
6440 const pj_str_t STR_DTMF_RELAY = { "dtmf-relay", 10 };
6441 const pj_str_t STR_TRICKLE_ICE_SDP = { "trickle-ice-sdpfrag", 19 };
6442
6443 pjsua_call *call;
6444
6445 pj_log_push_indent();
6446
6447 call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id];
6448
6449 if (call == NULL)
6450 goto on_return;
6451
6452 if (call->inv == NULL || call->hanging_up) {
6453 /* Call has been disconnected. */
6454 goto on_return;
6455 }
6456
6457 /* https://github.com/pjsip/pjproject/issues/1452:
6458 * If a request is retried due to 401/407 challenge, don't process the
6459 * transaction first but wait until we've retried it.
6460 */
6461 if (tsx->role == PJSIP_ROLE_UAC &&
6462 (tsx->status_code==401 || tsx->status_code==407) &&
6463 tsx->last_tx && tsx->last_tx->auth_retry)
6464 {
6465 goto on_return;
6466 }
6467
6468 /* Notify application callback first */
6469 if (!call->hanging_up && pjsua_var.ua_cfg.cb.on_call_tsx_state) {
6470 (*pjsua_var.ua_cfg.cb.on_call_tsx_state)(call->index, tsx, e);
6471 }
6472
6473 if (tsx->role==PJSIP_ROLE_UAS &&
6474 tsx->state==PJSIP_TSX_STATE_TRYING &&
6475 pjsip_method_cmp(&tsx->method, pjsip_get_refer_method())==0)
6476 {
6477 /*
6478 * Incoming REFER request.
6479 */
6480 on_call_transferred(call->inv, e->body.tsx_state.src.rdata);
6481
6482 }
6483 else if (tsx->role==PJSIP_ROLE_UAS &&
6484 tsx->state==PJSIP_TSX_STATE_TRYING &&
6485 pjsip_method_cmp(&tsx->method, &pjsip_message_method)==0)
6486 {
6487 /*
6488 * Incoming MESSAGE request!
6489 */
6490 pjsip_rx_data *rdata;
6491 pjsip_accept_hdr *accept_hdr;
6492
6493 rdata = e->body.tsx_state.src.rdata;
6494
6495 /* Request MUST have message body, with Content-Type equal to
6496 * "text/plain".
6497 */
6498 if (pjsua_im_accept_pager(rdata, &accept_hdr) == PJ_FALSE) {
6499
6500 pjsip_hdr hdr_list;
6501
6502 pj_list_init(&hdr_list);
6503 pj_list_push_back(&hdr_list, accept_hdr);
6504
6505 pjsip_dlg_respond( inv->dlg, rdata, PJSIP_SC_NOT_ACCEPTABLE_HERE,
6506 NULL, &hdr_list, NULL );
6507 goto on_return;
6508 }
6509
6510 /* Respond with 200 first, so that remote doesn't retransmit in case
6511 * the UI takes too long to process the message.
6512 */
6513 pjsip_dlg_respond( inv->dlg, rdata, 200, NULL, NULL, NULL);
6514
6515 /* Process MESSAGE request */
6516 pjsua_im_process_pager(call->index, &inv->dlg->remote.info_str,
6517 &inv->dlg->local.info_str, rdata);
6518
6519 }
6520 else if (e->type == PJSIP_EVENT_TSX_STATE &&
6521 tsx->role == PJSIP_ROLE_UAC &&
6522 pjsip_method_cmp(&tsx->method, &pjsip_message_method)==0 &&
6523 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
6524 (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
6525 e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED)))
6526 {
6527 /* Handle outgoing pager status */
6528 if (tsx->status_code >= 200) {
6529 pjsua_im_data *im_data;
6530
6531 im_data = (pjsua_im_data*) tsx->mod_data[pjsua_var.mod.id];
6532 /* im_data can be NULL if this is typing indication */
6533
6534 if (im_data) {
6535 pj_str_t im_body = im_data->body;
6536 if (im_body.slen==0 && tsx->last_tx) {
6537 pjsip_msg_body *body = tsx->last_tx->msg->body;
6538 pj_strset(&im_body, body->data, body->len);
6539 }
6540
6541 if (pjsua_var.ua_cfg.cb.on_pager_status) {
6542 pjsua_var.ua_cfg.cb.on_pager_status(im_data->call_id,
6543 &im_data->to,
6544 &im_body,
6545 im_data->user_data,
6546 (pjsip_status_code)
6547 tsx->status_code,
6548 &tsx->status_text);
6549 }
6550
6551 if (pjsua_var.ua_cfg.cb.on_pager_status2) {
6552 pjsip_rx_data* rdata;
6553
6554 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
6555 rdata = e->body.tsx_state.src.rdata;
6556 else
6557 rdata = NULL;
6558
6559 pjsua_var.ua_cfg.cb.on_pager_status2(im_data->call_id,
6560 &im_data->to,
6561 &im_body,
6562 im_data->user_data,
6563 (pjsip_status_code)
6564 tsx->status_code,
6565 &tsx->status_text,
6566 tsx->last_tx,
6567 rdata, im_data->acc_id);
6568 }
6569 }
6570 }
6571 } else if (tsx->role == PJSIP_ROLE_UAC &&
6572 pjsip_method_cmp(&tsx->method, pjsip_get_invite_method())==0 &&
6573 tsx->state >= PJSIP_TSX_STATE_COMPLETED &&
6574 e->body.tsx_state.prev_state < PJSIP_TSX_STATE_COMPLETED &&
6575 (!PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 300) &&
6576 tsx->status_code!=401 && tsx->status_code!=407 &&
6577 tsx->status_code!=422))
6578 {
6579 if (tsx->status_code/100 == 2) {
6580 /* If we have sent CANCEL and the original INVITE returns a 2xx,
6581 * we then send BYE.
6582 */
6583 if (call->hanging_up) {
6584 pj_str_t reason = pj_str("Cancelling call");
6585 PJ_LOG(3,(THIS_FILE, "Unsuccessful in cancelling the original "
6586 "INVITE for call %d due to %d response, sending BYE "
6587 "instead", call->index, tsx->status_code));
6588 call_disconnect(call->inv, PJSIP_SC_OK, &reason);
6589 }
6590 } else {
6591 /* Monitor the status of call hold/unhold request */
6592 if (tsx->last_tx == (pjsip_tx_data*)call->hold_msg) {
6593 /* Outgoing call hold failed */
6594 call->local_hold = PJ_FALSE;
6595 PJ_LOG(3,(THIS_FILE, "Error putting call %d on hold "
6596 "(reason=%d)", call->index, tsx->status_code));
6597 } else if (call->opt.flag & PJSUA_CALL_UNHOLD) {
6598 /* Call unhold failed */
6599 call->local_hold = PJ_TRUE;
6600 PJ_LOG(3,(THIS_FILE, "Error releasing hold on call %d "
6601 "(reason=%d)", call->index, tsx->status_code));
6602 }
6603 }
6604
6605 if (tsx->last_tx == (pjsip_tx_data*)call->hold_msg) {
6606 call->hold_msg = NULL;
6607 }
6608
6609 if (tsx->last_tx->msg->body &&
6610 (tsx->status_code/100 != 2 || !call->med_update_success))
6611 {
6612 /* Either we get non-2xx or media update failed,
6613 * revert back provisional media.
6614 */
6615 if (inv->invite_tsx == NULL || tsx == inv->invite_tsx) {
6616 pjsua_media_prov_revert(call->index);
6617 }
6618 else
6619 {
6620 PJ_LOG(4, (THIS_FILE, "Retaining provisional media for call %d "
6621 "since this is not the active Invite",
6622 call->index));
6623 }
6624 }
6625 } else if (tsx->role == PJSIP_ROLE_UAC &&
6626 pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0 &&
6627 tsx->state >= PJSIP_TSX_STATE_COMPLETED &&
6628 e->body.tsx_state.prev_state < PJSIP_TSX_STATE_COMPLETED &&
6629 (!PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 300) &&
6630 tsx->status_code!=401 && tsx->status_code!=407 &&
6631 tsx->status_code!=422))
6632 {
6633 if (tsx->last_tx->msg->body &&
6634 (tsx->status_code/100 != 2 || !call->med_update_success))
6635 {
6636 /* Either we get non-2xx or media update failed,
6637 * revert back provisional media.
6638 */
6639 pjsua_media_prov_revert(call->index);
6640 }
6641 } else if (tsx->role==PJSIP_ROLE_UAS &&
6642 tsx->state==PJSIP_TSX_STATE_TRYING &&
6643 pjsip_method_cmp(&tsx->method, &pjsip_info_method)==0)
6644 {
6645 pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
6646 pjsip_msg_body *body = rdata->msg_info.msg->body;
6647
6648 /* Check Media Control content in the INFO message */
6649 if (body && body->len &&
6650 pj_stricmp(&body->content_type.type, &STR_APPLICATION)==0 &&
6651 pj_stricmp(&body->content_type.subtype, &STR_MEDIA_CONTROL_XML)==0)
6652 {
6653 pjsip_tx_data *tdata;
6654 pj_str_t control_st;
6655 pj_status_t status;
6656
6657 /* Apply and answer the INFO request */
6658 pj_strset(&control_st, (char*)body->data, body->len);
6659 status = pjsua_media_apply_xml_control(call->index, &control_st);
6660 if (status == PJ_SUCCESS) {
6661 status = pjsip_endpt_create_response(tsx->endpt, rdata,
6662 200, NULL, &tdata);
6663 if (status == PJ_SUCCESS)
6664 status = pjsip_tsx_send_msg(tsx, tdata);
6665 } else {
6666 status = pjsip_endpt_create_response(tsx->endpt, rdata,
6667 400, NULL, &tdata);
6668 if (status == PJ_SUCCESS)
6669 status = pjsip_tsx_send_msg(tsx, tdata);
6670 }
6671 }
6672
6673 /* Check DTMF content in the INFO message */
6674 else if (body && body->len &&
6675 pj_stricmp(&body->content_type.type, &STR_APPLICATION)==0 &&
6676 pj_stricmp(&body->content_type.subtype, &STR_DTMF_RELAY)==0)
6677 {
6678 pjsip_tx_data *tdata;
6679 pj_status_t status;
6680 pj_bool_t is_handled = PJ_FALSE;
6681
6682 if (pjsua_var.ua_cfg.cb.on_dtmf_digit2 ||
6683 pjsua_var.ua_cfg.cb.on_dtmf_event)
6684 {
6685 pjsua_dtmf_info info = {0};
6686 pj_str_t delim, token, input;
6687 pj_ssize_t found_idx;
6688
6689 delim = pj_str("\r\n");
6690 input = pj_str(rdata->msg_info.msg->body->data);
6691 found_idx = pj_strtok(&input, &delim, &token, 0);
6692 if (found_idx != input.slen) {
6693 /* Get signal/digit */
6694 const pj_str_t STR_SIGNAL = { "Signal", 6 };
6695 const pj_str_t STR_DURATION = { "Duration", 8 };
6696 char *val;
6697 pj_ssize_t count_equal_sign;
6698
6699 val = pj_strstr(&input, &STR_SIGNAL);
6700 if (val) {
6701 char* p = val + STR_SIGNAL.slen;
6702 count_equal_sign = 0;
6703 while ((p - input.ptr < input.slen) && (*p == ' ' || *p == '=')) {
6704 if(*p == '=')
6705 count_equal_sign++;
6706 ++p;
6707 }
6708
6709 if (count_equal_sign == 1 && (p - input.ptr < input.slen)) {
6710 info.digit = *p;
6711 is_handled = PJ_TRUE;
6712 } else {
6713 PJ_LOG(2, (THIS_FILE, "Invalid dtmf-relay format"));
6714 }
6715
6716 /* Get duration */
6717 input.ptr += token.slen + 2;
6718 input.slen -= (token.slen + 2);
6719
6720 val = pj_strstr(&input, &STR_DURATION);
6721 if (val && is_handled) {
6722 pj_str_t val_str;
6723 char* ptr = val + STR_DURATION.slen;
6724 count_equal_sign = 0;
6725 while ((ptr - input.ptr < input.slen) &&
6726 (*ptr == ' ' || *ptr == '='))
6727 {
6728 if (*ptr == '=')
6729 count_equal_sign++;
6730 ++ptr;
6731 }
6732
6733 if ((count_equal_sign == 1) &&
6734 (ptr - input.ptr < input.slen))
6735 {
6736 val_str.ptr = ptr;
6737 val_str.slen = input.slen - (ptr - input.ptr);
6738 info.duration = pj_strtoul(&val_str);
6739 } else {
6740 info.duration = PJSUA_UNKNOWN_DTMF_DURATION;
6741 is_handled = PJ_FALSE;
6742 PJ_LOG(2, (THIS_FILE,
6743 "Invalid dtmf-relay format"));
6744 }
6745 }
6746
6747 if (is_handled) {
6748 info.method = PJSUA_DTMF_METHOD_SIP_INFO;
6749 if (pjsua_var.ua_cfg.cb.on_dtmf_event) {
6750 pjsua_dtmf_event evt;
6751 pj_timestamp begin_of_time, timestamp;
6752 /* Use the current instant as the events start
6753 * time.
6754 */
6755 begin_of_time.u64 = 0;
6756 pj_get_timestamp(&timestamp);
6757 evt.method = info.method;
6758 evt.timestamp = pj_elapsed_msec(&begin_of_time,
6759 &timestamp);
6760 evt.digit = info.digit;
6761 evt.duration = info.duration;
6762 /* There is only one message indicating the full
6763 * duration of the digit.
6764 */
6765 evt.flags = PJMEDIA_STREAM_DTMF_IS_END;
6766 (*pjsua_var.ua_cfg.cb.on_dtmf_event)(call->index,
6767 &evt);
6768 } else {
6769 (*pjsua_var.ua_cfg.cb.on_dtmf_digit2)(call->index,
6770 &info);
6771 }
6772
6773 status = pjsip_endpt_create_response(tsx->endpt, rdata,
6774 200, NULL, &tdata);
6775 if (status == PJ_SUCCESS)
6776 status = pjsip_tsx_send_msg(tsx, tdata);
6777 }
6778 }
6779 }
6780 }
6781
6782 if (!is_handled) {
6783 status = pjsip_endpt_create_response(tsx->endpt, rdata,
6784 400, NULL, &tdata);
6785 if (status == PJ_SUCCESS)
6786 status = pjsip_tsx_send_msg(tsx, tdata);
6787 }
6788 }
6789
6790 /* Check Trickle ICE content in the INFO message */
6791 else if (body && body->len &&
6792 pj_stricmp(&body->content_type.type, &STR_APPLICATION)==0 &&
6793 pj_stricmp(&body->content_type.subtype,
6794 &STR_TRICKLE_ICE_SDP)==0)
6795 {
6796 pjsip_tx_data *tdata;
6797 pj_status_t status;
6798
6799 /* Trickle ICE tasks:
6800 * - UAS receiving INFO, cease 18x retrans & start trickling
6801 */
6802 if (call->trickle_ice.enabled) {
6803 pjsua_ice_check_start_trickling(call, PJ_FALSE, e);
6804
6805 /* Process the SIP INFO content */
6806 trickle_ice_recv_sip_info(call, rdata);
6807
6808 /* Send 200 response, regardless */
6809 status = pjsip_endpt_create_response(tsx->endpt, rdata,
6810 200, NULL, &tdata);
6811 } else {
6812 /* Trickle ICE not enabled, send 400 response */
6813 status = pjsip_endpt_create_response(tsx->endpt, rdata,
6814 400, NULL, &tdata);
6815 }
6816 if (status == PJ_SUCCESS)
6817 status = pjsip_tsx_send_msg(tsx, tdata);
6818 }
6819
6820 } else if (tsx->role == PJSIP_ROLE_UAC &&
6821 pjsip_method_cmp(&tsx->method, &pjsip_info_method)==0 &&
6822 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
6823 (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
6824 e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED)))
6825 {
6826 pjsip_msg_body *body = NULL;
6827
6828 if (e->body.tsx_state.type == PJSIP_EVENT_TX_MSG)
6829 body = e->body.tsx_state.src.tdata->msg->body;
6830 else
6831 body = e->body.tsx_state.tsx->last_tx->msg->body;
6832
6833 /* Check DTMF content in the INFO message */
6834 if (body && body->len &&
6835 pj_stricmp(&body->content_type.type, &STR_APPLICATION)==0 &&
6836 pj_stricmp(&body->content_type.subtype, &STR_DTMF_RELAY)==0)
6837 {
6838 /* Status of outgoing INFO request */
6839 if (tsx->status_code >= 200 && tsx->status_code < 300) {
6840 PJ_LOG(4,(THIS_FILE,
6841 "Call %d: DTMF sent successfully with INFO",
6842 call->index));
6843 } else if (tsx->status_code >= 300) {
6844 PJ_LOG(4,(THIS_FILE,
6845 "Call %d: Failed to send DTMF with INFO: %d/%.*s",
6846 call->index,
6847 tsx->status_code,
6848 (int)tsx->status_text.slen,
6849 tsx->status_text.ptr));
6850 }
6851 }
6852
6853 /* Check Trickle ICE content in the INFO message */
6854 else if (body && body->len &&
6855 pj_stricmp(&body->content_type.type, &STR_APPLICATION)==0 &&
6856 pj_stricmp(&body->content_type.subtype,
6857 &STR_TRICKLE_ICE_SDP)==0)
6858 {
6859 /* Reset pending SIP INFO for Trickle ICE */
6860 call->trickle_ice.pending_info = PJ_FALSE;
6861 }
6862 } else if (inv->state < PJSIP_INV_STATE_CONFIRMED &&
6863 pjsip_method_cmp(&tsx->method, pjsip_get_invite_method())==0 &&
6864 tsx->state == PJSIP_TSX_STATE_PROCEEDING &&
6865 tsx->status_code/10 == 18)
6866 {
6867 /* Trickle ICE tasks:
6868 * - UAS sending 18x, start 18x retrans
6869 * - UAC receiving 18x, forcefully send SIP INFO & start trickling
6870 */
6871 pj_bool_t force = call->trickle_ice.trickling<PJSUA_OP_STATE_RUNNING;
6872 pjsua_ice_check_start_trickling(call, force, e);
6873 } else if (tsx->role == PJSIP_ROLE_UAS &&
6874 pjsip_method_cmp(&tsx->method, pjsip_get_prack_method())==0 &&
6875 tsx->state==PJSIP_TSX_STATE_TRYING)
6876 {
6877 /* Trickle ICE tasks:
6878 * - UAS receiving PRACK, start trickling
6879 */
6880 pjsua_ice_check_start_trickling(call, PJ_FALSE, e);
6881 }
6882
6883 on_return:
6884 pj_log_pop_indent();
6885 }
6886
6887
6888 /* Redirection handler */
6889 static pjsip_redirect_op pjsua_call_on_redirected(pjsip_inv_session *inv,
6890 const pjsip_uri *target,
6891 const pjsip_event *e)
6892 {
6893 pjsua_call *call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id];
6894 pjsip_redirect_op op;
6895
6896 pj_log_push_indent();
6897
6898 if (!call->hanging_up && pjsua_var.ua_cfg.cb.on_call_redirected) {
6899 op = (*pjsua_var.ua_cfg.cb.on_call_redirected)(call->index,
6900 target, e);
6901 } else {
6902 if (!call->hanging_up) {
6903 PJ_LOG(4,(THIS_FILE, "Unhandled redirection for call %d "
6904 "(callback not implemented by application). "
6905 "Disconnecting call.",
6906 call->index));
6907 }
6908 op = PJSIP_REDIRECT_STOP;
6909 }
6910
6911 pj_log_pop_indent();
6912
6913 return op;
6914 }
6915
6916 #if defined(PJSUA_MEDIA_HAS_PJMEDIA) && PJSUA_MEDIA_HAS_PJMEDIA != 0
6917
6918 /*
6919 * Get media stream info for the specified media index.
6920 */
6921 PJ_DEF(pj_status_t) pjsua_call_get_stream_info( pjsua_call_id call_id,
6922 unsigned med_idx,
6923 pjsua_stream_info *psi)
6924 {
6925 pjsua_call *call;
6926 pjsua_call_media *call_med;
6927 pj_status_t status = PJ_EINVAL;
6928
6929 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
6930 PJ_EINVAL);
6931 PJ_ASSERT_RETURN(psi, PJ_EINVAL);
6932
6933 PJSUA_LOCK();
6934
6935 call = &pjsua_var.calls[call_id];
6936
6937 if (med_idx >= call->med_cnt)
6938 goto on_return;
6939
6940 call_med = &call->media[med_idx];
6941
6942 if ((call_med->type == PJMEDIA_TYPE_AUDIO && !call_med->strm.a.stream) ||
6943 (call_med->type == PJMEDIA_TYPE_VIDEO && !call_med->strm.v.stream) ||
6944 (call_med->type == PJMEDIA_TYPE_TEXT && !call_med->strm.t.stream))
6945 {
6946 goto on_return;
6947 }
6948
6949 psi->type = call_med->type;
6950 switch (call_med->type) {
6951 case PJMEDIA_TYPE_AUDIO:
6952 status = pjmedia_stream_get_info(call_med->strm.a.stream,
6953 &psi->info.aud);
6954 break;
6955 #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
6956 case PJMEDIA_TYPE_VIDEO:
6957 status = pjmedia_vid_stream_get_info(call_med->strm.v.stream,
6958 &psi->info.vid);
6959 break;
6960 #endif
6961 case PJMEDIA_TYPE_TEXT:
6962 status = pjmedia_txt_stream_get_info(call_med->strm.t.stream,
6963 &psi->info.txt);
6964 break;
6965 default:
6966 status = PJMEDIA_EINVALIMEDIATYPE;
6967 break;
6968 }
6969
6970 on_return:
6971 PJSUA_UNLOCK();
6972 return status;
6973 }
6974
6975
6976 /*
6977 * Get media stream statistic for the specified media index.
6978 */
6979 PJ_DEF(pj_status_t) pjsua_call_get_stream_stat( pjsua_call_id call_id,
6980 unsigned med_idx,
6981 pjsua_stream_stat *stat)
6982 {
6983 pjsua_call *call;
6984 pjsua_call_media *call_med;
6985 pjmedia_stream_common *c_strm = NULL;
6986 pj_status_t status = PJ_EINVAL;
6987
6988 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
6989 PJ_EINVAL);
6990 PJ_ASSERT_RETURN(stat, PJ_EINVAL);
6991
6992 PJSUA_LOCK();
6993
6994 call = &pjsua_var.calls[call_id];
6995
6996 if (med_idx >= call->med_cnt)
6997 goto on_return;
6998
6999 call_med = &call->media[med_idx];
7000
7001 if ((call_med->type == PJMEDIA_TYPE_AUDIO && !call_med->strm.a.stream) ||
7002 (call_med->type == PJMEDIA_TYPE_VIDEO && !call_med->strm.v.stream) ||
7003 (call_med->type == PJMEDIA_TYPE_TEXT && !call_med->strm.t.stream))
7004 {
7005 goto on_return;
7006 }
7007
7008 switch (call_med->type) {
7009 case PJMEDIA_TYPE_AUDIO:
7010 c_strm = (pjmedia_stream_common *)call_med->strm.a.stream;
7011 break;
7012 #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
7013 case PJMEDIA_TYPE_VIDEO:
7014 c_strm = (pjmedia_stream_common *)call_med->strm.v.stream;
7015 break;
7016 #endif
7017 case PJMEDIA_TYPE_TEXT:
7018 c_strm = (pjmedia_stream_common *)call_med->strm.t.stream;
7019 break;
7020 default:
7021 status = PJMEDIA_EINVALIMEDIATYPE;
7022 break;
7023 }
7024
7025 if (c_strm) {
7026 status = pjmedia_stream_common_get_stat(c_strm, &stat->rtcp);
7027 if (status == PJ_SUCCESS)
7028 status = pjmedia_stream_common_get_stat_jbuf(c_strm, &stat->jbuf);
7029 }
7030
7031 on_return:
7032 PJSUA_UNLOCK();
7033 return status;
7034 }
7035
7036 #endif