-
Notifications
You must be signed in to change notification settings - Fork 820
/
Copy pathpjsua_call.c
6797 lines (5660 loc) · 226 KB
/
pjsua_call.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
* Copyright (C) 2003-2008 Benny Prijono <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <pjsua-lib/pjsua.h>
#include <pjsua-lib/pjsua_internal.h>
#define THIS_FILE "pjsua_call.c"
/* Retry interval of sending re-INVITE for locking a codec when remote
* SDP answer contains multiple codec, in milliseconds.
*/
#define LOCK_CODEC_RETRY_INTERVAL 200
/*
* Max UPDATE/re-INVITE retry to lock codec
*/
#define LOCK_CODEC_MAX_RETRY 5
/* Determine whether we should restart ICE upon receiving a re-INVITE
* with no SDP.
*/
#define RESTART_ICE_ON_REINVITE 1
/* Retry interval of trying to hangup a call. */
#define CALL_HANGUP_RETRY_INTERVAL 5000
/* Max number of hangup retries. */
#define CALL_HANGUP_MAX_RETRY 4
/*
* The INFO method.
*/
const pjsip_method pjsip_info_method =
{
PJSIP_OTHER_METHOD,
{ "INFO", 4 }
};
/* UPDATE method */
static const pjsip_method pjsip_update_method =
{
PJSIP_OTHER_METHOD,
{ "UPDATE", 6 }
};
/* This callback receives notification from invite session when the
* session state has changed.
*/
static void pjsua_call_on_state_changed(pjsip_inv_session *inv,
pjsip_event *e);
/* This callback is called by invite session framework when UAC session
* has forked.
*/
static void pjsua_call_on_forked( pjsip_inv_session *inv,
pjsip_event *e);
/*
* Callback to be called when SDP offer/answer negotiation has just completed
* in the session. This function will start/update media if negotiation
* has succeeded.
*/
static void pjsua_call_on_media_update(pjsip_inv_session *inv,
pj_status_t status);
/*
* Called when session received new offer.
*/
static void pjsua_call_on_rx_offer(pjsip_inv_session *inv,
struct pjsip_inv_on_rx_offer_cb_param *param);
/*
* Called when receiving re-INVITE.
*/
static pj_status_t pjsua_call_on_rx_reinvite(pjsip_inv_session *inv,
const pjmedia_sdp_session *offer,
pjsip_rx_data *rdata);
/*
* Called to generate new offer.
*/
static void pjsua_call_on_create_offer(pjsip_inv_session *inv,
pjmedia_sdp_session **offer);
/*
* This callback is called when transaction state has changed in INVITE
* session. We use this to trap:
* - incoming REFER request.
* - incoming MESSAGE request.
*/
static void pjsua_call_on_tsx_state_changed(pjsip_inv_session *inv,
pjsip_transaction *tsx,
pjsip_event *e);
/*
* Redirection handler.
*/
static pjsip_redirect_op pjsua_call_on_redirected(pjsip_inv_session *inv,
const pjsip_uri *target,
const pjsip_event *e);
/* Create SDP for call hold. */
static pj_status_t create_sdp_of_call_hold(pjsua_call *call,
pjmedia_sdp_session **p_sdp);
/*
* Callback called by event framework when the xfer subscription state
* has changed.
*/
static void xfer_client_on_evsub_state( pjsip_evsub *sub, pjsip_event *event);
static void xfer_server_on_evsub_state( pjsip_evsub *sub, pjsip_event *event);
/* Timer callback to send re-INVITE/UPDATE to lock codec or ICE update */
static void reinv_timer_cb(pj_timer_heap_t *th, pj_timer_entry *entry);
/* Timer callback to hangup the call */
static void hangup_timer_cb(pj_timer_heap_t *th, pj_timer_entry *entry);
/* Check and send reinvite for lock codec and ICE update */
static pj_status_t process_pending_reinvite(pjsua_call *call);
/* Timer callbacks for trickle ICE */
static void trickle_ice_send_sip_info(pj_timer_heap_t *th,
struct pj_timer_entry *te);
static void trickle_ice_retrans_18x(pj_timer_heap_t *th,
struct pj_timer_entry *te);
/* End call session */
static pj_status_t call_inv_end_session(pjsua_call *call,
unsigned code,
const pj_str_t *reason,
const pjsua_msg_data *msg_data);
/*
* Reset call descriptor.
*/
static void reset_call(pjsua_call_id id)
{
pjsua_call *call = &pjsua_var.calls[id];
unsigned i;
if (call->incoming_data) {
pjsip_rx_data_free_cloned(call->incoming_data);
call->incoming_data = NULL;
}
pj_bzero(call, sizeof(*call));
call->index = id;
call->last_text.ptr = call->last_text_buf_;
call->cname.ptr = call->cname_buf;
call->cname.slen = sizeof(call->cname_buf);
for (i=0; i<PJ_ARRAY_SIZE(call->media); ++i) {
pjsua_call_media *call_med = &call->media[i];
call_med->ssrc = pj_rand();
call_med->strm.a.conf_slot = PJSUA_INVALID_ID;
call_med->strm.v.cap_win_id = PJSUA_INVALID_ID;
call_med->strm.v.rdr_win_id = PJSUA_INVALID_ID;
call_med->strm.v.strm_dec_slot = PJSUA_INVALID_ID;
call_med->strm.v.strm_enc_slot = PJSUA_INVALID_ID;
call_med->call = call;
call_med->idx = i;
call_med->tp_auto_del = PJ_TRUE;
}
pjsua_call_setting_default(&call->opt);
pj_timer_entry_init(&call->reinv_timer, PJ_FALSE,
(void*)(pj_size_t)id, &reinv_timer_cb);
pj_bzero(&call->trickle_ice, sizeof(call->trickle_ice));
pj_timer_entry_init(&call->trickle_ice.timer, 0, call,
&trickle_ice_send_sip_info);
}
/* Get DTMF method type name */
static const char* get_dtmf_method_name(int type)
{
switch (type) {
case PJSUA_DTMF_METHOD_RFC2833:
return "RFC2833";
case PJSUA_DTMF_METHOD_SIP_INFO:
return "SIP INFO";
}
return "(Unknown)";
}
/*
* Init call subsystem.
*/
pj_status_t pjsua_call_subsys_init(const pjsua_config *cfg)
{
pjsip_inv_callback inv_cb;
unsigned i;
const pj_str_t str_norefersub = { "norefersub", 10 };
const pj_str_t str_trickle_ice = { "trickle-ice", 11 };
pj_status_t status;
/* Init calls array. */
for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.calls); ++i)
reset_call(i);
/* Copy config */
pjsua_config_dup(pjsua_var.pool, &pjsua_var.ua_cfg, cfg);
/* Verify settings */
if (pjsua_var.ua_cfg.max_calls >= PJSUA_MAX_CALLS) {
pjsua_var.ua_cfg.max_calls = PJSUA_MAX_CALLS;
}
/* Check the route URI's and force loose route if required */
for (i=0; i<pjsua_var.ua_cfg.outbound_proxy_cnt; ++i) {
status = normalize_route_uri(pjsua_var.pool,
&pjsua_var.ua_cfg.outbound_proxy[i]);
if (status != PJ_SUCCESS)
return status;
}
/* Initialize invite session callback. */
pj_bzero(&inv_cb, sizeof(inv_cb));
inv_cb.on_state_changed = &pjsua_call_on_state_changed;
inv_cb.on_new_session = &pjsua_call_on_forked;
inv_cb.on_media_update = &pjsua_call_on_media_update;
inv_cb.on_rx_offer2 = &pjsua_call_on_rx_offer;
inv_cb.on_create_offer = &pjsua_call_on_create_offer;
inv_cb.on_tsx_state_changed = &pjsua_call_on_tsx_state_changed;
inv_cb.on_redirected = &pjsua_call_on_redirected;
if (pjsua_var.ua_cfg.cb.on_call_rx_reinvite) {
inv_cb.on_rx_reinvite = &pjsua_call_on_rx_reinvite;
}
/* Initialize invite session module: */
status = pjsip_inv_usage_init(pjsua_var.endpt, &inv_cb);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
/* Add "norefersub" in Supported header */
pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_SUPPORTED,
NULL, 1, &str_norefersub);
/* Add "INFO" in Allow header, for DTMF and video key frame request. */
pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW,
NULL, 1, &pjsip_info_method.name);
/* Add "trickle-ice" in Supported header */
pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_SUPPORTED,
NULL, 1, &str_trickle_ice);
return status;
}
/*
* Start call subsystem.
*/
pj_status_t pjsua_call_subsys_start(void)
{
/* Nothing to do */
return PJ_SUCCESS;
}
/*
* Get maximum number of calls configured in pjsua.
*/
PJ_DEF(unsigned) pjsua_call_get_max_count(void)
{
return pjsua_var.ua_cfg.max_calls;
}
/*
* Get number of currently active calls.
*/
PJ_DEF(unsigned) pjsua_call_get_count(void)
{
return pjsua_var.call_cnt;
}
/*
* Enum calls.
*/
PJ_DEF(pj_status_t) pjsua_enum_calls( pjsua_call_id ids[],
unsigned *count)
{
unsigned i, c;
PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL);
PJSUA_LOCK();
for (i=0, c=0; c<*count && i<pjsua_var.ua_cfg.max_calls; ++i) {
if (!pjsua_var.calls[i].inv)
continue;
ids[c] = i;
++c;
}
*count = c;
PJSUA_UNLOCK();
return PJ_SUCCESS;
}
/* Allocate one call id */
static pjsua_call_id alloc_call_id(void)
{
pjsua_call_id cid;
#if 1
/* New algorithm: round-robin */
if (pjsua_var.next_call_id >= (int)pjsua_var.ua_cfg.max_calls ||
pjsua_var.next_call_id < 0)
{
pjsua_var.next_call_id = 0;
}
for (cid=pjsua_var.next_call_id;
cid<(int)pjsua_var.ua_cfg.max_calls;
++cid)
{
if (pjsua_var.calls[cid].inv == NULL &&
pjsua_var.calls[cid].async_call.dlg == NULL)
{
++pjsua_var.next_call_id;
return cid;
}
}
for (cid=0; cid < pjsua_var.next_call_id; ++cid) {
if (pjsua_var.calls[cid].inv == NULL &&
pjsua_var.calls[cid].async_call.dlg == NULL)
{
++pjsua_var.next_call_id;
return cid;
}
}
#else
/* Old algorithm */
for (cid=0; cid<(int)pjsua_var.ua_cfg.max_calls; ++cid) {
if (pjsua_var.calls[cid].inv == NULL)
return cid;
}
#endif
return PJSUA_INVALID_ID;
}
/* Get signaling secure level.
* Return:
* 0: if signaling is not secure
* 1: if TLS transport is used for immediate hop
* 2: if end-to-end signaling is secure.
*/
static int get_secure_level(pjsua_acc_id acc_id, const pj_str_t *dst_uri)
{
const pj_str_t tls = pj_str(";transport=tls");
const pj_str_t sips = pj_str("sips:");
pjsua_acc *acc = &pjsua_var.acc[acc_id];
if (pj_stristr(dst_uri, &sips))
return 2;
if (!pj_list_empty(&acc->route_set)) {
pjsip_route_hdr *r = acc->route_set.next;
pjsip_uri *uri = r->name_addr.uri;
pjsip_sip_uri *sip_uri;
sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
if (pj_stricmp2(&sip_uri->transport_param, "tls")==0)
return 1;
} else {
if (pj_stristr(dst_uri, &tls))
return 1;
}
return 0;
}
/*
static int call_get_secure_level(pjsua_call *call)
{
if (call->inv->dlg->secure)
return 2;
if (!pj_list_empty(&call->inv->dlg->route_set)) {
pjsip_route_hdr *r = call->inv->dlg->route_set.next;
pjsip_uri *uri = r->name_addr.uri;
pjsip_sip_uri *sip_uri;
sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
if (pj_stricmp2(&sip_uri->transport_param, "tls")==0)
return 1;
} else {
pjsip_sip_uri *sip_uri;
if (PJSIP_URI_SCHEME_IS_SIPS(call->inv->dlg->target))
return 2;
if (!PJSIP_URI_SCHEME_IS_SIP(call->inv->dlg->target))
return 0;
sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(call->inv->dlg->target);
if (pj_stricmp2(&sip_uri->transport_param, "tls")==0)
return 1;
}
return 0;
}
*/
/* Outgoing call callback when media transport creation is completed. */
static pj_status_t
on_make_call_med_tp_complete(pjsua_call_id call_id,
const pjsua_med_tp_state_info *info)
{
pjmedia_sdp_session *offer = NULL;
pjsip_inv_session *inv = NULL;
pjsua_call *call;
pjsua_acc *acc;
pjsip_dialog *dlg;
unsigned options = 0;
pjsip_tx_data *tdata;
pj_bool_t cb_called = PJ_FALSE;
pjsip_tpselector tp_sel;
pj_status_t status = (info? info->status: PJ_SUCCESS);
/* Get call, account, and dialog */
PJ_ASSERT_RETURN(call_id != PJSUA_INVALID_ID, PJ_EINVAL);
call = &pjsua_var.calls[call_id];
acc = &pjsua_var.acc[call->acc_id];
dlg = call->async_call.dlg;
PJSUA_LOCK();
/* Increment the dialog's lock otherwise when invite session creation
* fails the dialog will be destroyed prematurely.
*/
pjsip_dlg_inc_lock(dlg);
/* Decrement dialog session. */
pjsip_dlg_dec_session(dlg, &pjsua_var.mod);
if (status != PJ_SUCCESS) {
pj_str_t err_str;
pj_ssize_t title_len;
call->last_code = PJSIP_SC_TEMPORARILY_UNAVAILABLE;
pj_strcpy2(&call->last_text, "Media init error: ");
title_len = call->last_text.slen;
err_str = pj_strerror(status, call->last_text_buf_ + title_len,
sizeof(call->last_text_buf_) - title_len);
call->last_text.slen += err_str.slen;
pjsua_perror(THIS_FILE, "Error initializing media channel", status);
goto on_error;
}
/* pjsua_media_channel_deinit() has been called or
* call has been hung up.
*/
if (call->async_call.med_ch_deinit ||
call->async_call.call_var.out_call.hangup)
{
PJ_LOG(4,(THIS_FILE, "Call has been hung up or media channel has "
"been deinitialized"));
goto on_error;
}
/* Create offer */
if ((call->opt.flag & PJSUA_CALL_NO_SDP_OFFER) == 0) {
status = pjsua_media_channel_create_sdp(call->index, dlg->pool, NULL,
&offer, NULL);
if (status != PJ_SUCCESS) {
pjsua_perror(THIS_FILE, "Error initializing media channel", status);
goto on_error;
}
}
/* Create the INVITE session: */
options |= PJSIP_INV_SUPPORT_100REL;
if (acc->cfg.require_100rel == PJSUA_100REL_MANDATORY)
options |= PJSIP_INV_REQUIRE_100REL;
if (acc->cfg.use_timer != PJSUA_SIP_TIMER_INACTIVE) {
options |= PJSIP_INV_SUPPORT_TIMER;
if (acc->cfg.use_timer == PJSUA_SIP_TIMER_REQUIRED)
options |= PJSIP_INV_REQUIRE_TIMER;
else if (acc->cfg.use_timer == PJSUA_SIP_TIMER_ALWAYS)
options |= PJSIP_INV_ALWAYS_USE_TIMER;
}
if (acc->cfg.ice_cfg.enable_ice &&
acc->cfg.ice_cfg.ice_opt.trickle != PJ_ICE_SESS_TRICKLE_DISABLED)
{
options |= PJSIP_INV_SUPPORT_TRICKLE_ICE;
}
status = pjsip_inv_create_uac( dlg, offer, options, &inv);
if (status != PJ_SUCCESS) {
pjsua_perror(THIS_FILE, "Invite session creation failed", status);
goto on_error;
}
/* Init Session Timers */
status = pjsip_timer_init_session(inv, &acc->cfg.timer_setting);
if (status != PJ_SUCCESS) {
pjsua_perror(THIS_FILE, "Session Timer init failed", status);
goto on_error;
}
/* Create and associate our data in the session. */
call->inv = inv;
dlg->mod_data[pjsua_var.mod.id] = call;
inv->mod_data[pjsua_var.mod.id] = call;
/* Set dialog's transport based on acc's config. */
pjsua_init_tpselector(call->acc_id, &tp_sel);
pjsip_dlg_set_transport(dlg, &tp_sel);
/* Set dialog Route-Set: */
if (!pj_list_empty(&acc->route_set))
pjsip_dlg_set_route_set(dlg, &acc->route_set);
/* Set credentials: */
if (acc->cred_cnt) {
pjsip_auth_clt_set_credentials( &dlg->auth_sess,
acc->cred_cnt, acc->cred);
}
/* Set authentication preference */
pjsip_auth_clt_set_prefs(&dlg->auth_sess, &acc->cfg.auth_pref);
/* Create initial INVITE: */
status = pjsip_inv_invite(inv, &tdata);
if (status != PJ_SUCCESS) {
pjsua_perror(THIS_FILE, "Unable to create initial INVITE request",
status);
goto on_error;
}
/* Add additional headers etc */
pjsua_process_msg_data( tdata,
call->async_call.call_var.out_call.msg_data);
/* Must increment call counter now */
++pjsua_var.call_cnt;
/* Send initial INVITE: */
status = pjsip_inv_send_msg(inv, tdata);
if (status != PJ_SUCCESS) {
cb_called = PJ_TRUE;
/* Upon failure to send first request, the invite
* session would have been cleared.
*/
call->inv = inv = NULL;
goto on_error;
}
/* Done. */
call->med_ch_cb = NULL;
pjsip_dlg_dec_lock(dlg);
PJSUA_UNLOCK();
return PJ_SUCCESS;
on_error:
if (inv == NULL && call_id != -1 && !cb_called &&
!call->hanging_up &&
pjsua_var.ua_cfg.cb.on_call_state)
{
/* Use user event rather than NULL to avoid crash in
* unsuspecting app.
*/
pjsip_event user_event;
PJSIP_EVENT_INIT_USER(user_event, 0, 0, 0, 0);
(*pjsua_var.ua_cfg.cb.on_call_state)(call_id, &user_event);
}
/* This may destroy the dialog */
pjsip_dlg_dec_lock(dlg);
call->async_call.dlg = NULL;
if (inv != NULL) {
pjsip_inv_terminate(inv, PJSIP_SC_OK, PJ_FALSE);
call->inv = NULL;
}
if (call_id != -1) {
pjsua_media_channel_deinit(call_id);
reset_call(call_id);
}
call->med_ch_cb = NULL;
pjsua_check_snd_dev_idle();
PJSUA_UNLOCK();
return status;
}
/*
* Cleanup call setting flag to avoid one time flags, such as
* PJSUA_CALL_UNHOLD, PJSUA_CALL_UPDATE_CONTACT, or
* PJSUA_CALL_NO_SDP_OFFER, to be sticky (ticket #1793).
*/
void pjsua_call_cleanup_flag(pjsua_call_setting *opt)
{
opt->flag &= ~(PJSUA_CALL_UNHOLD | PJSUA_CALL_UPDATE_CONTACT |
PJSUA_CALL_NO_SDP_OFFER | PJSUA_CALL_REINIT_MEDIA |
PJSUA_CALL_UPDATE_VIA | PJSUA_CALL_SET_MEDIA_DIR);
}
/*
* Initialize call settings based on account ID.
*/
PJ_DEF(void) pjsua_call_setting_default(pjsua_call_setting *opt)
{
unsigned i;
pj_assert(opt);
pj_bzero(opt, sizeof(*opt));
opt->flag = PJSUA_CALL_INCLUDE_DISABLED_MEDIA;
opt->aud_cnt = 1;
#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
opt->vid_cnt = 1;
opt->req_keyframe_method = PJSUA_VID_REQ_KEYFRAME_SIP_INFO |
PJSUA_VID_REQ_KEYFRAME_RTCP_PLI;
#endif
for (i = 0; i < PJMEDIA_MAX_SDP_MEDIA; i++) {
opt->media_dir[i] = PJMEDIA_DIR_ENCODING_DECODING;
}
}
/*
* Initialize pjsua_call_send_dtmf_param default values.
*/
PJ_DEF(void) pjsua_call_send_dtmf_param_default(
pjsua_call_send_dtmf_param *param)
{
pj_bzero(param, sizeof(*param));
param->duration = PJSUA_CALL_SEND_DTMF_DURATION_DEFAULT;
}
static pj_status_t apply_call_setting(pjsua_call *call,
const pjsua_call_setting *opt,
const pjmedia_sdp_session *rem_sdp)
{
pj_assert(call);
if (!opt) {
pjsua_call_cleanup_flag(&call->opt);
} else {
call->opt = *opt;
}
#if !PJMEDIA_HAS_VIDEO
pj_assert(call->opt.vid_cnt == 0);
#endif
if (call->opt.flag & PJSUA_CALL_REINIT_MEDIA) {
PJ_LOG(4, (THIS_FILE, "PJSUA_CALL_REINIT_MEDIA"));
pjsua_media_channel_deinit(call->index);
}
/* If call is established or media channel hasn't been initialized,
* reinit media channel.
*/
if (call->inv &&
((call->inv->state == PJSIP_INV_STATE_CONNECTING &&
call->med_cnt == 0) ||
(call->inv->state == PJSIP_INV_STATE_CONFIRMED) ||
(call->opt.flag & PJSUA_CALL_REINIT_MEDIA)))
{
pjsip_role_e role = rem_sdp? PJSIP_ROLE_UAS : PJSIP_ROLE_UAC;
pj_status_t status;
status = pjsua_media_channel_init(call->index, role,
call->secure_level,
call->inv->pool_prov,
rem_sdp, NULL,
PJ_FALSE, NULL);
if (status != PJ_SUCCESS) {
pjsua_perror(THIS_FILE, "Error re-initializing media channel",
status);
return status;
}
}
return PJ_SUCCESS;
}
static void dlg_set_via(pjsip_dialog *dlg, pjsua_acc *acc)
{
if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0) {
pjsip_dlg_set_via_sent_by(dlg, &acc->via_addr, acc->via_tp);
} else if (!pjsua_sip_acc_is_using_stun(acc->index) &&
!pjsua_sip_acc_is_using_upnp(acc->index))
{
/* Choose local interface to use in Via if acc is not using
* STUN nor UPnP. See https://github.com/pjsip/pjproject/issues/1804
*/
pjsip_host_port via_addr;
const void *via_tp;
if (pjsua_acc_get_uac_addr(acc->index, dlg->pool, &acc->cfg.id,
&via_addr, NULL, NULL,
&via_tp) == PJ_SUCCESS)
{
pjsip_dlg_set_via_sent_by(dlg, &via_addr,
(pjsip_transport*)via_tp);
}
}
}
static pj_status_t dlg_set_target(pjsip_dialog *dlg, const pj_str_t *target)
{
pjsip_uri *target_uri;
pj_str_t tmp;
pj_status_t status;
/* Parse target & verify */
pj_strdup_with_null(dlg->pool, &tmp, target);
target_uri = pjsip_parse_uri(dlg->pool, tmp.ptr, tmp.slen, 0);
if (!target_uri) {
return PJSIP_EINVALIDURI;
}
if (!PJSIP_URI_SCHEME_IS_SIP(target_uri) &&
!PJSIP_URI_SCHEME_IS_SIPS(target_uri))
{
return PJSIP_EINVALIDSCHEME;
}
/* Add the new target */
status = pjsip_target_set_add_uri(&dlg->target_set, dlg->pool,
target_uri, 0);
if (status != PJ_SUCCESS)
return status;
/* Set it as current target */
status = pjsip_target_set_set_current(&dlg->target_set,
pjsip_target_set_get_next(&dlg->target_set));
if (status != PJ_SUCCESS)
return status;
/* Update dialog target URI */
dlg->target = target_uri;
return PJ_SUCCESS;
}
/* Get account contact for call and update dialog transport */
void call_update_contact(pjsua_call *call, pj_str_t **new_contact)
{
pjsip_tpselector tp_sel;
pjsua_acc *acc = &pjsua_var.acc[call->acc_id];
if (acc->cfg.force_contact.slen)
*new_contact = &acc->cfg.force_contact;
else if (acc->contact.slen)
*new_contact = &acc->contact;
else {
/* Non-registering account */
pjsip_dialog *dlg = call->inv->dlg;
pj_str_t tmp_contact;
pj_status_t status;
status = pjsua_acc_create_uac_contact(dlg->pool,
&tmp_contact,
acc->index,
&dlg->remote.info_str);
if (status == PJ_SUCCESS) {
*new_contact = PJ_POOL_ZALLOC_T(dlg->pool, pj_str_t);
**new_contact = tmp_contact;
} else {
PJ_PERROR(3,(THIS_FILE, status,
"Call %d: failed creating contact "
"for contact update", call->index));
}
}
/* When contact is changed, the account transport may have been
* changed too, so let's update the dialog's transport too.
*/
pjsua_init_tpselector(call->acc_id, &tp_sel);
pjsip_dlg_set_transport(call->inv->dlg, &tp_sel);
}
/*
* Make outgoing call to the specified URI using the specified account.
*/
PJ_DEF(pj_status_t) pjsua_call_make_call(pjsua_acc_id acc_id,
const pj_str_t *dest_uri,
const pjsua_call_setting *opt,
void *user_data,
const pjsua_msg_data *msg_data,
pjsua_call_id *p_call_id)
{
pj_pool_t *tmp_pool = NULL;
pjsip_dialog *dlg = NULL;
pjsua_acc *acc;
pjsua_call *call = NULL;
int call_id = -1;
pj_str_t contact;
pj_status_t status;
/* Check that account is valid */
PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
PJ_EINVAL);
/* Check arguments */
PJ_ASSERT_RETURN(dest_uri, PJ_EINVAL);
PJ_LOG(4,(THIS_FILE, "Making call with acc #%d to %.*s", acc_id,
(int)dest_uri->slen, dest_uri->ptr));
pj_log_push_indent();
PJSUA_LOCK();
acc = &pjsua_var.acc[acc_id];
if (!acc->valid) {
pjsua_perror(THIS_FILE, "Unable to make call because account "
"is not valid", PJ_EINVALIDOP);
status = PJ_EINVALIDOP;
goto on_error;
}
/* Find free call slot. */
call_id = alloc_call_id();
if (call_id == PJSUA_INVALID_ID) {
pjsua_perror(THIS_FILE, "Error making call", PJ_ETOOMANY);
status = PJ_ETOOMANY;
goto on_error;
}
/* Clear call descriptor */
reset_call(call_id);
call = &pjsua_var.calls[call_id];
/* Associate session with account */
call->acc_id = acc_id;
call->call_hold_type = acc->cfg.call_hold_type;
/* Generate per-session RTCP CNAME, according to RFC 7022. */
pj_create_random_string(call->cname_buf, call->cname.slen);
/* Apply call setting */
status = apply_call_setting(call, opt, NULL);
if (status != PJ_SUCCESS) {
pjsua_perror(THIS_FILE, "Failed to apply call setting", status);
goto on_error;
} else if (!opt) {
opt = &call->opt;
}
/* Create sound port if none is instantiated, to check if sound device
* can be used. But only do this with the conference bridge, as with
* audio switchboard (i.e. APS-Direct), we can only open the sound
* device once the correct format has been known
*/
if (!pjsua_var.is_mswitch && pjsua_var.snd_port==NULL &&
pjsua_var.null_snd==NULL && !pjsua_var.no_snd && call->opt.aud_cnt > 0)
{
status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
if (status != PJ_SUCCESS)
goto on_error;
}
/* Create temporary pool */
tmp_pool = pjsua_pool_create("tmpcall10", 512, 256);
/* Verify that destination URI is valid before calling
* pjsua_acc_create_uac_contact, or otherwise there
* a misleading "Invalid Contact URI" error will be printed
* when pjsua_acc_create_uac_contact() fails.
*/
if (1) {
pjsip_uri *uri;
pj_str_t dup;
pj_strdup_with_null(tmp_pool, &dup, dest_uri);
uri = pjsip_parse_uri(tmp_pool, dup.ptr, dup.slen, 0);
if (uri == NULL) {
pjsua_perror(THIS_FILE, "Unable to make call",
PJSIP_EINVALIDREQURI);
status = PJSIP_EINVALIDREQURI;
goto on_error;
}
}
/* Mark call start time. */
pj_gettimeofday(&call->start_time);
/* Reset first response time */
call->res_time.sec = 0;
/* Create suitable Contact header unless a Contact header has been
* set in the account.
*/
if (acc->contact.slen) {
contact = acc->contact;
} else {
status = pjsua_acc_create_uac_contact(tmp_pool, &contact,
acc_id, dest_uri);
if (status != PJ_SUCCESS) {
pjsua_perror(THIS_FILE, "Unable to generate Contact header",
status);
goto on_error;
}
}
/* Create outgoing dialog: */
status = pjsip_dlg_create_uac( pjsip_ua_instance(),
(msg_data && msg_data->local_uri.slen?
&msg_data->local_uri: &acc->cfg.id),
&contact,
dest_uri,
(msg_data && msg_data->target_uri.slen?
&msg_data->target_uri: dest_uri),
&dlg);
if (status != PJ_SUCCESS) {
pjsua_perror(THIS_FILE, "Dialog creation failed", status);
goto on_error;
}
/*
* Use pre defined Call-ID to be sent out with INVITE as opposed
* to using a randomly generated Call-ID
*/
if( opt->custom_call_id.slen > 0 ){
pj_strdup(dlg->pool, &dlg->call_id->id, &opt->custom_call_id);
PJ_LOG(4,(THIS_FILE, "Set user defined "
"Call-ID (%.*s)",
(int)dlg->call_id->id.slen, dlg->call_id->id.ptr ));
pj_bzero(&call->opt.custom_call_id,
sizeof(call->opt.custom_call_id));
}
/* Increment the dialog's lock otherwise when invite session creation
* fails the dialog will be destroyed prematurely.
*/
pjsip_dlg_inc_lock(dlg);
dlg_set_via(dlg, acc);
if (acc->cfg.use_shared_auth) {
pjsip_dlg_set_auth_sess(dlg, &acc->shared_auth_sess);
}
/* Calculate call's secure level */
call->secure_level = get_secure_level(acc_id, dest_uri);
/* Attach user data */
call->user_data = user_data;
/* Store variables required for the callback after the async
* media transport creation is completed.
*/
if (msg_data) {
call->async_call.call_var.out_call.msg_data = pjsua_msg_data_clone(