forked from baresip/baresip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall.c
3177 lines (2489 loc) · 65.4 KB
/
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
/**
* @file src/call.c Call Control
*
* Copyright (C) 2010 Alfred E. Heggestad
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <re.h>
#include <baresip.h>
#include "core.h"
/** Magic number */
#define MAGIC 0xca11ca11
#include "magic.h"
#define FOREACH_STREAM \
for (le = call->streaml.head; le; le = le->next)
/** SIP Call Control object */
struct call {
MAGIC_DECL /**< Magic number for debugging */
struct le le; /**< Linked list element */
const struct config *cfg; /**< Global configuration */
struct ua *ua; /**< SIP User-agent */
struct account *acc; /**< Account (ref.) */
struct sipsess *sess; /**< SIP Session */
struct sdp_session *sdp; /**< SDP Session */
struct sipsub *sub; /**< Call transfer REFER subscription */
struct sipnot *not; /**< REFER/NOTIFY client */
struct call *xcall; /**< Cross ref Transfer call */
struct list streaml; /**< List of mediastreams (struct stream) */
struct audio *audio; /**< Audio stream */
struct video *video; /**< Video stream */
enum call_state state; /**< Call state */
int32_t adelay; /**< Auto answer delay in ms */
char *aluri; /**< Alert-Info URI */
char *local_uri; /**< Local SIP uri */
char *local_name; /**< Local display name */
char *peer_uri; /**< Peer SIP Address */
char *peer_name; /**< Peer display name */
char *diverter_uri; /**< Diverter SIP Address */
char *id; /**< Cached session call-id */
char *replaces; /**< Replaces parameter */
uint16_t supported; /**< Supported header tags */
struct tmr tmr_inv; /**< Timer for incoming calls */
struct tmr tmr_dtmf; /**< Timer for incoming DTMF events */
struct tmr tmr_answ; /**< Timer for delayed answer */
struct tmr tmr_reinv; /**< Timer for outgoing re-INVITES */
time_t time_start; /**< Time when call started */
time_t time_conn; /**< Time when call initiated */
time_t time_stop; /**< Time when call stopped */
bool outgoing; /**< True if outgoing, false if incoming */
bool answered; /**< True if call has been answered */
bool got_offer; /**< Got SDP Offer from Peer */
bool sent_answer; /**< Sent an SDP Answer to Peer */
bool on_hold; /**< True if call is on hold (local) */
bool ans_queued; /**< True if an (auto) answer is queued */
struct mnat_sess *mnats; /**< Media NAT session */
bool mnat_wait; /**< Waiting for MNAT to establish */
struct menc_sess *mencs; /**< Media encryption session state */
int af; /**< Preferred Address Family */
uint16_t scode; /**< Termination status code */
call_event_h *eh; /**< Event handler */
call_dtmf_h *dtmfh; /**< DTMF handler */
void *arg; /**< Handler argument */
struct config_avt config_avt; /**< AVT config */
struct config_call config_call; /**< Call config */
uint32_t rtp_timeout_ms; /**< RTP Timeout in [ms] */
uint32_t linenum; /**< Line number from 1 to N */
struct list custom_hdrs; /**< List of custom headers if any */
enum sdp_dir estadir; /**< Established audio direction */
enum sdp_dir estvdir; /**< Established video direction */
bool use_video;
bool use_rtp;
char *user_data; /**< User data related to the call */
bool evstop; /**< UA events stopped flag */
};
static int send_invite(struct call *call);
static int send_dtmf_info(struct call *call, char key);
static const char *state_name(enum call_state st)
{
switch (st) {
case CALL_STATE_IDLE: return "IDLE";
case CALL_STATE_INCOMING: return "INCOMING";
case CALL_STATE_OUTGOING: return "OUTGOING";
case CALL_STATE_RINGING: return "RINGING";
case CALL_STATE_EARLY: return "EARLY";
case CALL_STATE_ESTABLISHED: return "ESTABLISHED";
case CALL_STATE_TERMINATED: return "TERMINATED";
case CALL_STATE_TRANSFER: return "TRANSFER";
case CALL_STATE_UNKNOWN: return "UNKNOWN";
default: return "???";
}
}
static void set_state(struct call *call, enum call_state st)
{
call->state = st;
}
static const struct sdp_format *sdp_media_rcodec(const struct sdp_media *m)
{
const struct list *lst;
struct le *le;
if (!m || !sdp_media_rport(m))
return NULL;
lst = sdp_media_format_lst(m, false);
for (le=list_head(lst); le; le=le->next) {
const struct sdp_format *fmt = le->data;
if (!fmt->sup)
continue;
if (!fmt->data)
continue;
return fmt;
}
return NULL;
}
static int start_audio(struct call *call)
{
const struct sdp_format *sc;
const struct sdp_media *m = stream_sdpmedia(audio_strm(call->audio));
struct aucodec *ac;
enum sdp_dir dir = sdp_media_dir(m);
int err = 0;
/* Audio Stream */
sc = sdp_media_rcodec(m);
if (!sc) {
info("call: audio stream is disabled\n");
return 0;
}
ac = sc->data;
if (dir & SDP_SENDONLY)
err |= audio_encoder_set(call->audio, ac,
sc->pt, sc->params);
if (dir & SDP_RECVONLY)
err |= audio_decoder_set(call->audio, ac,
sc->pt, sc->params);
if (err) {
warning("call: start:"
" audio codec setup error (%m)\n", err);
return err;
}
err = audio_start(call->audio);
if (err)
return err;
return 0;
}
static void call_stream_start(struct call *call, bool active)
{
int err;
struct le *le;
debug("call: stream start (active=%d)\n", active);
if (stream_is_ready(audio_strm(call->audio))) {
err = start_audio(call);
if (err) {
warning("call: could not start audio: %m\n", err);
}
}
if (stream_is_ready(video_strm(call->video))) {
err = video_update(call->video, call->peer_uri);
if (err) {
warning("call: could not start video: %m\n", err);
}
}
if (active) {
tmr_cancel(&call->tmr_inv);
call->time_start = time(NULL);
stream_flush(audio_strm(call->audio));
}
FOREACH_STREAM {
stream_enable(le->data, true);
}
}
static void call_stream_stop(struct call *call)
{
if (!call)
return;
call->time_stop = time(NULL);
/* Audio */
audio_stop(call->audio);
/* Video */
video_stop(call->video);
tmr_cancel(&call->tmr_inv);
}
static void call_event_handler(struct call *call, enum call_event ev,
const char *fmt, ...)
{
call_event_h *eh = call->eh;
void *eh_arg = call->arg;
char buf[256];
va_list ap;
if (!eh)
return;
va_start(ap, fmt);
(void)re_vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
eh(call, ev, buf, eh_arg);
}
static void invite_timeout(void *arg)
{
struct call *call = arg;
info("%s: Local timeout after %u seconds\n",
call->peer_uri, call->config_call.local_timeout);
call_event_handler(call, CALL_EVENT_CLOSED, "Local timeout");
}
/* Called when all media streams are established */
static void mnat_handler(int err, uint16_t scode, const char *reason,
void *arg)
{
struct call *call = arg;
MAGIC_CHECK(call);
if (err) {
warning("call: medianat '%s' failed: %m\n",
call->acc->mnatid, err);
call_event_handler(call, CALL_EVENT_CLOSED, "%m", err);
return;
}
else if (scode) {
warning("call: medianat failed: %u %s\n", scode, reason);
call_event_handler(call, CALL_EVENT_CLOSED, "%u %s",
scode, reason);
return;
}
info("call: media-nat '%s' established/gathered\n",
call->acc->mnatid);
/* Re-INVITE */
if (!call->mnat_wait) {
info("call: medianat established -- sending Re-INVITE\n");
(void)call_modify(call);
return;
}
call->mnat_wait = false;
switch (call->state) {
case CALL_STATE_OUTGOING:
(void)send_invite(call);
break;
case CALL_STATE_INCOMING:
call_event_handler(call, CALL_EVENT_INCOMING, "%s",
call->peer_uri);
break;
default:
break;
}
}
static int update_audio(struct call *call)
{
const struct sdp_format *sc;
int err = 0;
debug("audio: update\n");
sc = sdp_media_rcodec(stream_sdpmedia(audio_strm(call->audio)));
if (sc) {
struct aucodec *ac = sc->data;
err = audio_decoder_set(call->audio, ac,
sc->pt, sc->params);
if (err) {
warning("call: update:"
" audio_decoder_set error: %m\n", err);
}
err |= audio_encoder_set(call->audio, ac,
sc->pt, sc->params);
}
else {
info("audio stream is disabled..\n");
}
return err;
}
int call_update_media(struct call *call)
{
struct le *le;
int err = 0;
/* media attributes */
audio_sdp_attr_decode(call->audio);
if (call->video)
video_sdp_attr_decode(call->video);
/* Update each stream */
FOREACH_STREAM {
struct stream *strm = le->data;
stream_update(strm);
if (stream_is_ready(strm)) {
stream_start_rtcp(strm);
}
}
if (call->acc->mnat && call->acc->mnat->updateh && call->mnats)
err = call->acc->mnat->updateh(call->mnats);
if (stream_is_ready(audio_strm(call->audio)))
err |= update_audio(call);
else
audio_stop(call->audio);
if (stream_is_ready(video_strm(call->video)))
err |= video_update(call->video, call->peer_uri);
else
video_stop(call->video);
return err;
}
static int update_media(struct call *call)
{
debug("call: update media\n");
ua_event(call->ua, UA_EVENT_CALL_REMOTE_SDP, call,
call->got_offer ? "offer" : "answer");
return call_update_media(call);
}
static void print_summary(const struct call *call)
{
uint32_t dur = call_duration(call);
if (!dur)
return;
info("%s: Call with %s terminated (duration: %H)\n",
call->local_uri, call->peer_uri, fmt_human_time, &dur);
}
static void call_destructor(void *arg)
{
struct call *call = arg;
if (call->state != CALL_STATE_IDLE)
print_summary(call);
call_stream_stop(call);
list_unlink(&call->le);
tmr_cancel(&call->tmr_dtmf);
tmr_cancel(&call->tmr_answ);
tmr_cancel(&call->tmr_reinv);
mem_deref(call->sess);
mem_deref(call->id);
mem_deref(call->local_uri);
mem_deref(call->local_name);
mem_deref(call->peer_uri);
mem_deref(call->peer_name);
mem_deref(call->replaces);
mem_deref(call->aluri);
mem_deref(call->diverter_uri);
mem_deref(call->audio);
mem_deref(call->video);
mem_deref(call->sdp);
mem_deref(call->mnats);
mem_deref(call->mencs);
mem_deref(call->sub);
mem_deref(call->not);
mem_deref(call->acc);
mem_deref(call->user_data);
list_flush(&call->custom_hdrs);
}
static void audio_event_handler(int key, bool end, void *arg)
{
struct call *call = arg;
MAGIC_CHECK(call);
info("received in-band DTMF event: '%c' (end=%d)\n", key, end);
if (call->dtmfh)
call->dtmfh(call, end ? KEYCODE_REL : key, call->arg);
}
static void audio_level_handler(bool tx, double lvl, void *arg)
{
struct call *call = arg;
MAGIC_CHECK(call);
ua_event(call->ua, tx ? UA_EVENT_VU_TX : UA_EVENT_VU_RX,
call, "%.2f", lvl);
}
static void audio_error_handler(int err, const char *str, void *arg)
{
struct call *call = arg;
MAGIC_CHECK(call);
if (err) {
warning("call: audio device error: %m (%s)\n", err, str);
}
ua_event(call->ua, UA_EVENT_AUDIO_ERROR, call, "%d,%s", err, str);
call_stream_stop(call);
call_event_handler(call, CALL_EVENT_CLOSED, "%s", str);
}
static void video_error_handler(int err, const char *str, void *arg)
{
struct call *call = arg;
MAGIC_CHECK(call);
warning("call: video device error: %m (%s)\n", err, str);
call_stream_stop(call);
call_event_handler(call, CALL_EVENT_CLOSED, "%s", str);
}
static void menc_event_handler(enum menc_event event,
const char *prm, struct stream *strm, void *arg)
{
struct call *call = arg;
int err;
(void)strm;
MAGIC_CHECK(call);
debug("call: mediaenc event '%s' (%s)\n", menc_event_name(event), prm);
switch (event) {
case MENC_EVENT_SECURE:
if (strstr(prm, "audio")) {
stream_set_secure(audio_strm(call->audio), true);
stream_start_rtcp(audio_strm(call->audio));
err = start_audio(call);
if (err) {
warning("call: secure: could not"
" start audio: %m\n", err);
}
}
else if (strstr(prm, "video")) {
stream_set_secure(video_strm(call->video), true);
stream_start_rtcp(video_strm(call->video));
err = video_update(call->video, call->peer_uri);
if (err) {
warning("call: secure: could not"
" start video: %m\n", err);
}
}
else {
info("call: mediaenc: no match for stream (%s)\n",
prm);
}
break;
default:
break;
}
if (str_isset(prm))
call_event_handler(call, CALL_EVENT_MENC, "%u,%s", event,
prm);
else
call_event_handler(call, CALL_EVENT_MENC, "%u", event);
}
static void menc_error_handler(int err, void *arg)
{
struct call *call = arg;
MAGIC_CHECK(call);
warning("call: mediaenc '%s' error: %m\n", call->acc->mencid, err);
call_stream_stop(call);
call_event_handler(call, CALL_EVENT_CLOSED, "mediaenc failed");
}
static void stream_mnatconn_handler(struct stream *strm, void *arg)
{
struct call *call = arg;
int err;
MAGIC_CHECK(call);
if (call->mencs) {
err = stream_start_mediaenc(strm);
if (err) {
call_event_handler(call, CALL_EVENT_CLOSED,
"mediaenc failed %m", err);
}
}
else if (stream_is_ready(strm)) {
stream_start_rtcp(strm);
switch (stream_type(strm)) {
case MEDIA_AUDIO:
err = start_audio(call);
if (err) {
warning("call: mnatconn: could not"
" start audio: %m\n", err);
}
break;
case MEDIA_VIDEO:
err = video_update(call->video, call->peer_uri);
if (err) {
warning("call: mnatconn: could not"
" start video: %m\n", err);
}
break;
}
}
}
static void stream_rtpestab_handler(struct stream *strm, void *arg)
{
struct call *call = arg;
MAGIC_CHECK(call);
ua_event(call->ua, UA_EVENT_CALL_RTPESTAB, call,
"%s", sdp_media_name(stream_sdpmedia(strm)));
}
static void stream_rtcp_handler(struct stream *strm,
struct rtcp_msg *msg, void *arg)
{
struct call *call = arg;
MAGIC_CHECK(call);
switch (msg->hdr.pt) {
case RTCP_SR:
if (call->config_avt.rtp_stats)
call_set_xrtpstat(call);
ua_event(call->ua, UA_EVENT_CALL_RTCP, call,
"%s", sdp_media_name(stream_sdpmedia(strm)));
break;
case RTCP_APP:
ua_event(call->ua, UA_EVENT_CALL_RTCP, call,
"%s", sdp_media_name(stream_sdpmedia(strm)));
break;
}
}
static void stream_error_handler(struct stream *strm, int err, void *arg)
{
struct call *call = arg;
MAGIC_CHECK(call);
info("call: error in \"%s\" rtp stream (%m)\n",
sdp_media_name(stream_sdpmedia(strm)), err);
call->scode = 701;
set_state(call, CALL_STATE_TERMINATED);
call_stream_stop(call);
call_event_handler(call, CALL_EVENT_CLOSED, "rtp stream error");
}
static int assign_linenum(uint32_t *linenum, const struct list *lst)
{
uint32_t num;
for (num=CALL_LINENUM_MIN; num<CALL_LINENUM_MAX; num++) {
if (!call_find_linenum(lst, num)) {
*linenum = num;
return 0;
}
}
return ENOENT;
}
/**
* Decode the SIP-Header for RFC 5373 auto answer of incoming call
*
* @param call Call object
* @param msg SIP message
* @param name SIP header name
*/
static void call_rfc5373_autoanswer(struct call *call,
const struct sip_msg *msg, const char *name)
{
const struct sip_hdr *hdr;
struct pl v1;
hdr = sip_msg_xhdr(msg, name);
if (!hdr || pl_strcasecmp(&hdr->val, "Auto"))
return;
if (!msg_param_exists(&hdr->val, "require", &v1) &&
!account_sip_autoanswer(call->acc)) {
warning("call: rejected, since %s is not allowed\n", name);
call_hangup(call, 0, NULL);
return;
}
call->adelay = 0;
}
/**
* Decodes given SIP header for auto answer options of incoming call
*
* @param call Call object
* @param hdr SIP header (Call-Info or Alert-Info)
* @return true if success, otherwise false
*/
static bool call_hdr_dec_sip_autoanswer(struct call *call,
const struct sip_hdr *hdr)
{
struct pl v1, v2;
if (!call || !hdr)
return false;
if (!msg_param_decode(&hdr->val, "answer-after", &v1)) {
call->adelay = pl_u32(&v1) * 1000;
return true;
}
if (!msg_param_decode(&hdr->val, "info", &v1) &&
!msg_param_decode(&hdr->val, "delay", &v2)) {
if (!pl_strcmp(&v1, "alert-autoanswer")) {
call->adelay = pl_u32(&v2) * 1000;
return true;
}
}
if (!msg_param_decode(&hdr->val, "info", &v1)) {
if (!pl_strcmp(&v1, "alert-autoanswer")) {
call->adelay = 0;
return true;
}
}
return false;
}
static void call_decode_diverter(struct call *call, const struct sip_msg *msg)
{
const struct sip_hdr *hdr;
struct sip_addr addr;
int err;
if (!call || !msg)
return;
hdr = sip_msg_hdr(msg, SIP_HDR_HISTORY_INFO);
if (!hdr)
hdr = sip_msg_xhdr(msg, "Diversion");
if (!hdr)
return;
err = sip_addr_decode(&addr, &hdr->val);
if (err) {
warning("call: error parsing diverter address: %r\n",
&hdr->val);
return;
}
err = pl_strdup(&call->diverter_uri, &addr.auri);
if (err) {
warning("call: could not extract diverter uri");
return;
}
}
/**
* Decode the SIP message for auto answer options of incoming call
*
* @param call Call object
* @param msg SIP message
*/
static void call_decode_sip_autoanswer(struct call *call,
const struct sip_msg *msg)
{
const struct sip_hdr *hdr;
struct pl v;
int err = 0;
call->adelay = -1;
/* polycom (HDA50), avaya, grandstream, snom, gigaset, yealink */
hdr = sip_msg_hdr(msg, SIP_HDR_CALL_INFO);
if (call_hdr_dec_sip_autoanswer(call, hdr))
return;
hdr = sip_msg_hdr(msg, SIP_HDR_ALERT_INFO);
if (call_hdr_dec_sip_autoanswer(call, hdr)) {
if (!re_regex(hdr->val.p, hdr->val.l, "<[^<>]*>", &v))
err = pl_strdup(&call->aluri, &v);
if (err) {
warning("call: could not extract Alert-Info URI\n");
return;
}
return;
}
/* RFC 5373 */
call_rfc5373_autoanswer(call, msg, "Answer-Mode");
call_rfc5373_autoanswer(call, msg, "Priv-Answer-Mode");
}
int call_streams_alloc(struct call *call)
{
struct account *acc = call->acc;
struct stream_param strm_prm;
struct le *le;
int label = 0;
int err;
memset(&strm_prm, 0, sizeof(strm_prm));
strm_prm.use_rtp = call->use_rtp;
strm_prm.af = call->af;
strm_prm.cname = call->local_uri;
strm_prm.peer = call->peer_uri;
strm_prm.rtcp_mux = call->acc->rtcp_mux;
/* Audio stream */
err = audio_alloc(&call->audio, &call->streaml, &strm_prm,
call->cfg, acc, call->sdp,
acc->mnat, call->mnats, acc->menc, call->mencs,
acc->ptime, account_aucodecl(call->acc),
!call->got_offer,
audio_event_handler, audio_level_handler,
audio_error_handler, call);
if (err)
return err;
/* Video stream */
if (call->use_video) {
err = video_alloc(&call->video, &call->streaml, &strm_prm,
call->cfg, call->sdp,
acc->mnat, call->mnats,
acc->menc, call->mencs,
"main",
account_vidcodecl(call->acc),
baresip_vidfiltl(), !call->got_offer,
video_error_handler, call);
if (err)
return err;
}
FOREACH_STREAM {
struct stream *strm = le->data;
sdp_media_set_lattr(stream_sdpmedia(strm), true,
"label", "%d", ++label);
stream_set_session_handlers(strm, stream_mnatconn_handler,
stream_rtpestab_handler,
stream_rtcp_handler,
stream_error_handler, call);
stream_enable_natpinhole(strm, acc->pinhole);
}
if (call->cfg->avt.bundle) {
FOREACH_STREAM {
struct stream *strm = le->data;
err = stream_bundle_init(strm, !call->got_offer);
if (err)
return err;
}
err = bundle_sdp_encode(call->sdp, &call->streaml);
if (err)
return err;
}
return 0;
}
/**
* Set stream sdp media line direction attribute
*
* @param call Call object
* @param a Audio SDP direction
* @param v Video SDP direction if video available
*/
static void call_set_mdir(struct call *call, enum sdp_dir a, enum sdp_dir v)
{
if (!call)
return;
stream_set_ldir(audio_strm(call_audio(call)), a);
if (video_strm(call_video(call))) {
if (vidisp_find(baresip_vidispl(), NULL) == NULL)
stream_set_ldir(video_strm(
call_video(call)), v & SDP_SENDONLY);
else
stream_set_ldir(video_strm(call_video(call)), v);
}
}
/**
* Allocate a new Call state object
*
* @param callp Pointer to allocated Call state object
* @param cfg Global configuration
* @param lst List of call objects
* @param local_name Local display name (optional)
* @param local_uri Local SIP uri
* @param acc Account parameters
* @param ua User-Agent
* @param prm Call parameters
* @param msg SIP message for incoming calls
* @param xcall Optional call to inherit properties from
* @param dnsc DNS Client
* @param eh Call event handler
* @param arg Handler argument
*
* @return 0 if success, otherwise errorcode
*/
int call_alloc(struct call **callp, const struct config *cfg, struct list *lst,
const char *local_name, const char *local_uri,
struct account *acc, struct ua *ua, const struct call_prm *prm,
const struct sip_msg *msg, struct call *xcall,
struct dnsc *dnsc,
call_event_h *eh, void *arg)
{
struct call *call;
enum vidmode vidmode = prm ? prm->vidmode : VIDMODE_OFF;
int err = 0;
if (!cfg || !local_uri || !acc || !ua || !prm)
return EINVAL;
debug("call: alloc with params laddr=%j, af=%s, use_rtp=%d\n",
&prm->laddr, net_af2name(prm->af), prm->use_rtp);
call = mem_zalloc(sizeof(*call), call_destructor);
if (!call)
return ENOMEM;
MAGIC_INIT(call);
call->config_avt = cfg->avt;
call->config_call = cfg->call;
tmr_init(&call->tmr_inv);
tmr_init(&call->tmr_answ);
tmr_init(&call->tmr_reinv);
call->cfg = cfg;
call->acc = mem_ref(acc);
call->ua = ua;
call->state = CALL_STATE_IDLE;
call->eh = eh;
call->arg = arg;
call->af = prm->af;
call->estadir = SDP_SENDRECV;
call->estvdir = SDP_SENDRECV;
call->use_rtp = prm->use_rtp;
call_decode_sip_autoanswer(call, msg);
call_decode_diverter(call, msg);
err = str_dup(&call->local_uri, local_uri);
if (local_name)
err |= str_dup(&call->local_name, local_name);
if (msg)
err |= pl_strdup(&call->peer_uri, &msg->from.auri);
if (err)
goto out;
if (sip_msg_hdr_has_value(msg, SIP_HDR_SUPPORTED, "replaces"))
call->supported |= REPLACES;
/* Init SDP info */
err = sdp_session_alloc(&call->sdp, &prm->laddr);
if (err)
goto out;
/* Check for incoming SDP Offer */
if (msg && mbuf_get_left(msg->mb))
call->got_offer = true;
/* Initialise media NAT handling */
if (acc->mnat) {
err = acc->mnat->sessh(&call->mnats, acc->mnat,
dnsc, call->af,
acc->stun_host,
acc->stun_user, acc->stun_pass,
call->sdp, !call->got_offer,
mnat_handler, call);
if (err) {
warning("call: medianat session: %m\n", err);
goto out;
}
}
call->mnat_wait = true;
/* Media encryption */
if (acc->menc) {
if (acc->menc->sessh) {
err = acc->menc->sessh(&call->mencs, call->sdp,
!call->got_offer,
menc_event_handler,
menc_error_handler, call);
if (err) {
warning("call: mediaenc session: %m\n", err);
goto out;
}
}
}