-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathsoa_static.c
More file actions
1618 lines (1336 loc) · 40.2 KB
/
Copy pathsoa_static.c
File metadata and controls
1618 lines (1336 loc) · 40.2 KB
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
/*
* This file is part of the Sofia-SIP package
*
* Copyright (C) 2006 Nokia Corporation.
*
* Contact: Pekka Pessi <pekka.pessi@nokia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
/**@CFILE soa_static.c
*
* @brief Static implementation of Sofia SDP Offer/Answer Engine
*
* @author Pekka Pessi <Pekka.Pessi@nokia.com>
*
* @date Created: Tue Aug 16 17:06:06 EEST 2005
*
* @par Use-cases
* 1. no existing session
* a) generating offer (upgrade with user-SDP)
* b) generating answer (upgrade with remote-SDP, rejects with user-SDP)
* 2. session exists
* a) generating offer:
* upgrades with user-SDP
* b) generating answer:
* upgrades with remote-SDP, rejects with user-SDP
* c) processing answer:
* rejects with user-SDP, no upgrades
*
* Upgrading session with user SDP:
*/
#include "config.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
struct soa_static_complete;
#define SU_MSG_ARG_T struct soa_static_completed
#include <sofia-sip/su_wait.h>
#include <sofia-sip/su_tag_class.h>
#include <sofia-sip/su_tag_class.h>
#include <sofia-sip/su_tagarg.h>
#include <sofia-sip/su_strlst.h>
#include <sofia-sip/su_string.h>
#include <sofia-sip/bnf.h>
#include "sofia-sip/soa.h"
#include <sofia-sip/sdp.h>
#include "sofia-sip/soa_session.h"
#define NONE ((void *)-1)
#define XXX assert(!"implemented")
typedef struct soa_static_session
{
soa_session_t sss_session[1];
char *sss_audio_aux;
int sss_ordered_user; /**< User SDP is ordered */
int sss_reuse_rejected; /**< Try to reuse rejected media line slots */
/** Mapping from user SDP m= lines to session SDP m= lines */
int *sss_u2s;
/** Mapping from session SDP m= lines to user SDP m= lines */
int *sss_s2u;
/** State kept from SDP before current offer */
struct {
int *u2s, *s2u;
} sss_previous;
/** Our latest offer or answer */
sdp_session_t *sss_latest;
}
soa_static_session_t;
#define U2S_NOT_USED (-1)
#define U2S_SENTINEL (-2)
static int soa_static_init(char const *, soa_session_t *, soa_session_t *);
static void soa_static_deinit(soa_session_t *);
static int soa_static_set_params(soa_session_t *ss, tagi_t const *tags);
static int soa_static_get_params(soa_session_t const *ss, tagi_t *tags);
static tagi_t *soa_static_get_paramlist(soa_session_t const *ss,
tag_type_t tag, tag_value_t value,
...);
static int soa_static_set_capability_sdp(soa_session_t *ss,
sdp_session_t *sdp,
char const *, isize_t);
static int soa_static_set_remote_sdp(soa_session_t *ss,
int new_version,
sdp_session_t *sdp,
char const *, isize_t);
static int soa_static_set_user_sdp(soa_session_t *ss,
sdp_session_t *sdp,
char const *, isize_t);
static int soa_static_generate_offer(soa_session_t *ss, soa_callback_f *);
static int soa_static_generate_answer(soa_session_t *ss, soa_callback_f *);
static int soa_static_process_answer(soa_session_t *ss, soa_callback_f *);
static int soa_static_process_reject(soa_session_t *ss, soa_callback_f *);
static int soa_static_activate(soa_session_t *ss, char const *option);
static int soa_static_deactivate(soa_session_t *ss, char const *option);
static void soa_static_terminate(soa_session_t *ss, char const *option);
struct soa_session_actions const soa_default_actions =
{
(sizeof soa_default_actions),
sizeof (struct soa_static_session),
"static",
soa_static_init,
soa_static_deinit,
soa_static_set_params,
soa_static_get_params,
soa_static_get_paramlist,
soa_base_media_features,
soa_base_sip_require,
soa_base_sip_supported,
soa_base_remote_sip_features,
soa_static_set_capability_sdp,
soa_static_set_remote_sdp,
soa_static_set_user_sdp,
soa_static_generate_offer,
soa_static_generate_answer,
soa_static_process_answer,
soa_static_process_reject,
soa_static_activate,
soa_static_deactivate,
soa_static_terminate
};
/* Initialize session */
static int soa_static_init(char const *name,
soa_session_t *ss,
soa_session_t *parent)
{
return soa_base_init(name, ss, parent);
}
static void soa_static_deinit(soa_session_t *ss)
{
soa_base_deinit(ss);
}
static int soa_static_set_params(soa_session_t *ss, tagi_t const *tags)
{
soa_static_session_t *sss = (soa_static_session_t *)ss;
char const *audio_aux = sss->sss_audio_aux;
int ordered_user = sss->sss_ordered_user;
int reuse_rejected = sss->sss_reuse_rejected;
int n, m;
n = tl_gets(tags,
SOATAG_AUDIO_AUX_REF(audio_aux),
SOATAG_ORDERED_USER_REF(ordered_user),
SOATAG_REUSE_REJECTED_REF(reuse_rejected),
TAG_END());
if (n > 0 && !su_casematch(audio_aux, sss->sss_audio_aux)) {
char *s = su_strdup(ss->ss_home, audio_aux), *tbf = sss->sss_audio_aux;
if (s == NULL && audio_aux != NULL)
return -1;
sss->sss_audio_aux = s;
if (tbf)
su_free(ss->ss_home, tbf);
}
sss->sss_ordered_user = ordered_user != 0;
sss->sss_reuse_rejected = reuse_rejected != 0;
m = soa_base_set_params(ss, tags);
if (m < 0)
return m;
return n + m;
}
static int soa_static_get_params(soa_session_t const *ss, tagi_t *tags)
{
soa_static_session_t *sss = (soa_static_session_t *)ss;
int n, m;
n = tl_tgets(tags,
SOATAG_AUDIO_AUX(sss->sss_audio_aux),
SOATAG_ORDERED_USER(sss->sss_ordered_user),
SOATAG_REUSE_REJECTED(sss->sss_reuse_rejected),
TAG_END());
m = soa_base_get_params(ss, tags);
if (m < 0)
return m;
return n + m;
}
static tagi_t *soa_static_get_paramlist(soa_session_t const *ss,
tag_type_t tag, tag_value_t value,
...)
{
soa_static_session_t *sss = (soa_static_session_t *)ss;
ta_list ta;
tagi_t *tl;
ta_start(ta, tag, value);
tl = soa_base_get_paramlist(ss,
TAG_IF(sss->sss_audio_aux,
SOATAG_AUDIO_AUX(sss->sss_audio_aux)),
TAG_IF(sss->sss_ordered_user,
SOATAG_ORDERED_USER(1)),
TAG_IF(sss->sss_reuse_rejected,
SOATAG_REUSE_REJECTED(1)),
TAG_NEXT(ta_args(ta)));
ta_end(ta);
return tl;
}
static int soa_static_set_capability_sdp(soa_session_t *ss,
sdp_session_t *sdp,
char const *sdp_str,
isize_t sdp_len)
{
return soa_base_set_capability_sdp(ss, sdp, sdp_str, sdp_len);
}
static int soa_static_set_remote_sdp(soa_session_t *ss,
int new_version,
sdp_session_t *sdp,
char const *sdp_str,
isize_t sdp_len)
{
return soa_base_set_remote_sdp(ss, new_version, sdp, sdp_str, sdp_len);
}
static int soa_static_set_user_sdp(soa_session_t *ss,
sdp_session_t *sdp,
char const *sdp_str,
isize_t sdp_len)
{
return soa_base_set_user_sdp(ss, sdp, sdp_str, sdp_len);
}
/** Generate a rejected m= line */
static
sdp_media_t *soa_sdp_make_rejected_media(su_home_t *home,
sdp_media_t const *m,
sdp_session_t *sdp,
int include_all_codecs)
{
sdp_media_t rejected[1] = {{ sizeof (rejected) }};
rejected->m_type = m->m_type;
rejected->m_type_name = m->m_type_name;
rejected->m_port = 0;
rejected->m_proto = m->m_proto;
rejected->m_proto_name = m->m_proto_name;
if (include_all_codecs) {
if (m->m_rtpmaps) {
rejected->m_rtpmaps = m->m_rtpmaps;
}
else if (m->m_format) {
rejected->m_format = m->m_format;
}
}
rejected->m_rejected = 1;
return sdp_media_dup(home, rejected, sdp);
}
/** Expand a @a truncated SDP.
*/
static
sdp_session_t *soa_sdp_expand_media(su_home_t *home,
sdp_session_t const *truncated,
sdp_session_t const *complete)
{
sdp_session_t *tmp_truncated;
sdp_session_t *expanded;
sdp_media_t **mE;
sdp_media_t **mT;
sdp_media_t * const *mC;
/* Truncated list that we will be reducing */
tmp_truncated = sdp_session_dup(home, truncated);
/* New resulting list */
expanded = sdp_session_dup(home, truncated);
if (expanded) {
expanded->sdp_media = NULL; /* Empty the list of medias */
mE = &expanded->sdp_media; /* Pointer to the beginning of the new list */
/* Loop through all items in the complete list to preserve complete list's order */
for (mC = &complete->sdp_media; *mC; mC = &(*mC)->m_next) {
/* Find the corresponding media in the truncated list */
if ((mT = sdp_media_exists(tmp_truncated, *mC))) {
/* Copy the corresponding media from the truncated list to the new list */
*mE = sdp_media_dup(home, *mT, expanded);
if (!*mE)
return NULL;
/* Remove corresponding media from the truncated list */
*mT = (*mT)->m_next;
} else {
/* If the corresponding media was not found in the truncated list, add a rejected media */
*mE = soa_sdp_make_rejected_media(home, *mC, expanded, 0);
if (!*mE)
return NULL;
}
/* Prepare pointer of the new list for a new item */
mE = &(*mE)->m_next;
}
}
return expanded;
}
/** Check if @a session should be upgraded with @a remote */
int soa_sdp_upgrade_is_needed(sdp_session_t const *session,
sdp_session_t const *remote)
{
sdp_media_t const *rm, *lm;
if (!remote)
return 0;
if (!session)
return 1;
for (rm = remote->sdp_media, lm = session->sdp_media;
rm && lm ; rm = rm->m_next, lm = lm->m_next) {
if (rm->m_rejected)
continue;
if (lm->m_rejected)
break;
}
return rm != NULL;
}
/** Check if codec is in auxiliary list */
static
int soa_sdp_is_auxiliary_codec(sdp_rtpmap_t const *rm, char const *auxiliary)
{
char const *codec;
size_t clen, alen;
char const *match;
if (!rm || !rm->rm_encoding || !auxiliary)
return 0;
codec = rm->rm_encoding;
clen = strlen(codec), alen = strlen(auxiliary);
if (clen > alen)
return 0;
for (match = auxiliary;
(match = su_strcasestr(match, codec));
match = match + 1) {
if (IS_ALPHANUM(match[clen]) || match[clen] == '-')
continue;
if (match != auxiliary &&
(IS_ALPHANUM(match[-1]) || match[-1] == '-'))
continue;
return 1;
}
return 0;
}
static
sdp_rtpmap_t *soa_sdp_media_matching_rtpmap(sdp_rtpmap_t const *from,
sdp_rtpmap_t const *anylist,
char const *auxiliary)
{
sdp_rtpmap_t const *rm;
for (rm = anylist; rm; rm = rm->rm_next) {
/* Ignore auxiliary codecs */
if (auxiliary && soa_sdp_is_auxiliary_codec(rm, auxiliary))
continue;
if (sdp_rtpmap_find_matching(from, rm))
return (sdp_rtpmap_t *)rm;
}
return NULL;
}
#ifndef _MSC_VER
#define SDP_MEDIA_NONE ((sdp_media_t *)-1)
#else
#define SDP_MEDIA_NONE ((sdp_media_t *)(INT_PTR)-1)
#endif
/** Find first matching media in table @a mm.
*
* - if allow_rtp_mismatch == 0, search for a matching codec
* - if allow_rtp_mismatch == 1, prefer m=line with matching codec
* - if allow_rtp_mismatch > 1, ignore codecs
*/
static
int soa_sdp_matching_mindex(soa_session_t *ss,
sdp_media_t *mm[],
sdp_media_t const *with,
int *return_codec_mismatch)
{
int i, j = -1;
soa_static_session_t *sss = (soa_static_session_t *)ss;
int rtp = sdp_media_uses_rtp(with), dummy;
char const *auxiliary = NULL;
if (return_codec_mismatch == NULL)
return_codec_mismatch = &dummy;
if (with->m_type == sdp_media_audio) {
auxiliary = sss->sss_audio_aux;
/* Looking for a single codec */
if (with->m_rtpmaps && with->m_rtpmaps->rm_next == NULL)
auxiliary = NULL;
}
for (i = 0; mm[i]; i++) {
if (mm[i] == SDP_MEDIA_NONE)
continue;
if (!sdp_media_match_with(mm[i], with))
continue;
if (!rtp)
break;
if (soa_sdp_media_matching_rtpmap(with->m_rtpmaps,
mm[i]->m_rtpmaps,
auxiliary))
break;
if (j == -1)
j = i;
}
if (mm[i])
return *return_codec_mismatch = 0, i;
else
return *return_codec_mismatch = 1, j;
}
/** Set payload types in @a l_m according to the values in @a r_m.
*
* @retval number of common codecs
*/
static
int soa_sdp_set_rtpmap_pt(sdp_media_t *l_m,
sdp_media_t const *r_m)
{
sdp_rtpmap_t *lrm, **next_lrm;
sdp_rtpmap_t const *rrm;
int local_codecs = 0, common_codecs = 0;
unsigned char dynamic_pt[128];
unsigned pt;
for (next_lrm = &l_m->m_rtpmaps; (lrm = *next_lrm); ) {
if (lrm->rm_any) {
/* Remove codecs known only by pt number */
*next_lrm = lrm->rm_next;
continue;
}
else {
next_lrm = &lrm->rm_next;
}
local_codecs++;
rrm = sdp_rtpmap_find_matching(r_m->m_rtpmaps, lrm);
/* XXX - do fmtp comparison */
if (rrm) {
#if 0
/* Use same payload type as remote */
if (lrm->rm_pt != rrm->rm_pt) {
lrm->rm_predef = 0;
lrm->rm_pt = rrm->rm_pt;
}
#endif
common_codecs++;
}
else {
/* Determine payload type later */
lrm->rm_any = 1;
}
}
if (local_codecs == common_codecs)
return common_codecs;
/* Select unique dynamic payload type for each payload */
memset(dynamic_pt, 0, sizeof dynamic_pt);
for (lrm = l_m->m_rtpmaps; lrm; lrm = lrm->rm_next) {
if (!lrm->rm_any)
dynamic_pt[lrm->rm_pt] = 1;
}
for (rrm = r_m->m_rtpmaps; rrm; rrm = rrm->rm_next) {
dynamic_pt[rrm->rm_pt] = 1;
}
for (next_lrm = &l_m->m_rtpmaps; (lrm = *next_lrm); ) {
if (!lrm->rm_any) {
next_lrm = &lrm->rm_next;
continue;
}
lrm->rm_any = 0;
pt = lrm->rm_pt;
if (dynamic_pt[pt]) {
for (pt = 96; pt < 128; pt++)
if (!dynamic_pt[pt])
break;
if (pt == 128) {
for (pt = 0; pt < 128; pt++)
if (!sdp_rtpmap_well_known[pt] && !dynamic_pt[pt])
break;
}
if (pt == 128) {
for (pt = 0; pt < 128; pt++)
if (!dynamic_pt[pt])
break;
}
if (pt == 128) {
/* Too many payload types */
*next_lrm = lrm->rm_next;
continue;
}
lrm->rm_pt = pt;
lrm->rm_predef = 0;
}
dynamic_pt[pt] = 1;
next_lrm = &lrm->rm_next;
}
return common_codecs;
}
/** Sort rtpmaps in @a inout_list according to the values in @a rrm.
*
* @return Number of common codecs
*/
static
int soa_sdp_sort_rtpmap(sdp_rtpmap_t **inout_list,
sdp_rtpmap_t const *rrm,
char const *auxiliary)
{
sdp_rtpmap_t *sorted = NULL, **next = &sorted, **left;
sdp_rtpmap_t *aux = NULL, **next_aux = &aux;
int common_codecs = 0;
assert(inout_list);
if (!inout_list)
return 0;
/* If remote has only single codec, ignore list of auxiliary codecs */
if (rrm && !rrm->rm_next)
auxiliary = NULL;
/* Insertion sort from *inout_list to sorted */
for (; rrm && *inout_list; rrm = rrm->rm_next) {
for (left = inout_list; *left; left = &(*left)->rm_next) {
if (sdp_rtpmap_match(rrm, (*left)))
break;
}
if (!*left)
continue;
if (auxiliary && soa_sdp_is_auxiliary_codec(rrm, auxiliary)) {
*next_aux = *left, next_aux = &(*next_aux)->rm_next;
}
else {
common_codecs++;
*next = *left; next = &(*next)->rm_next;
}
*left = (*left)->rm_next;
}
/* Append common auxiliary codecs */
if (aux)
*next = aux, next = next_aux;
/* Append leftover codecs */
*next = *inout_list;
*inout_list = sorted;
return common_codecs;
}
/** Select rtpmaps in @a inout_list according to the values in @a rrm.
*
* @return Number of common codecs
*/
static
int soa_sdp_select_rtpmap(sdp_rtpmap_t **inout_list,
sdp_rtpmap_t const *rrm,
char const *auxiliary,
int select_single)
{
sdp_rtpmap_t **left;
sdp_rtpmap_t *aux = NULL, **next_aux = &aux;
int common_codecs = 0;
assert(inout_list);
if (!inout_list)
return 0;
for (left = inout_list; *left; ) {
if (auxiliary && soa_sdp_is_auxiliary_codec(*left, auxiliary))
/* Insert into list of auxiliary codecs */
*next_aux = *left, *left = (*left)->rm_next,
next_aux = &(*next_aux)->rm_next;
else if (!(select_single && common_codecs > 0)
&& sdp_rtpmap_find_matching(rrm, (*left)))
/* Select */
left = &(*left)->rm_next, common_codecs++;
else
/* Remove */
*left = (*left)->rm_next;
}
*left = aux, *next_aux = NULL;
return common_codecs;
}
/** Sort and select rtpmaps */
static
int soa_sdp_media_upgrade_rtpmaps(soa_session_t *ss,
sdp_media_t *sm,
sdp_media_t const *rm)
{
soa_static_session_t *sss = (soa_static_session_t *)ss;
char const *auxiliary = NULL;
int common_codecs;
common_codecs = soa_sdp_set_rtpmap_pt(sm, rm);
if (rm->m_type == sdp_media_audio)
auxiliary = sss->sss_audio_aux;
if (ss->ss_rtp_sort == SOA_RTP_SORT_REMOTE ||
(ss->ss_rtp_sort == SOA_RTP_SORT_DEFAULT &&
rm->m_mode == sdp_recvonly)) {
soa_sdp_sort_rtpmap(&sm->m_rtpmaps, rm->m_rtpmaps, auxiliary);
}
if (common_codecs == 0)
;
else if (ss->ss_rtp_select == SOA_RTP_SELECT_SINGLE) {
soa_sdp_select_rtpmap(&sm->m_rtpmaps, rm->m_rtpmaps, auxiliary, 1);
}
else if (ss->ss_rtp_select == SOA_RTP_SELECT_COMMON) {
soa_sdp_select_rtpmap(&sm->m_rtpmaps, rm->m_rtpmaps, auxiliary, 0);
}
return common_codecs;
}
/** Sort and select rtpmaps within session */
static
int soa_sdp_session_upgrade_rtpmaps(soa_session_t *ss,
sdp_session_t *session,
sdp_session_t const *remote)
{
sdp_media_t *sm;
sdp_media_t const *rm;
for (sm = session->sdp_media, rm = remote->sdp_media;
sm && rm;
sm = sm->m_next, rm = rm->m_next) {
if (!sm->m_rejected && sdp_media_uses_rtp(sm))
soa_sdp_media_upgrade_rtpmaps(ss, sm, rm);
}
return 0;
}
/** Upgrade m= lines within session */
static
int soa_sdp_upgrade(soa_session_t *ss,
su_home_t *home,
sdp_session_t *session,
sdp_session_t const *user,
sdp_session_t const *remote,
int **return_u2s,
int **return_s2u)
{
soa_static_session_t *sss = (soa_static_session_t *)ss;
int Ns, Nu, Nr, Nmax, n, i, j;
sdp_media_t *m, **mm, *um;
sdp_media_t **s_media, **o_media, **u_media;
sdp_media_t const *rm, **r_media;
int *u2s = NULL, *s2u = NULL;
if (session == NULL || user == NULL)
return (errno = EFAULT), -1;
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnon-literal-null-conversion"
#endif
Ns = sdp_media_count(session, sdp_media_any, (sdp_text_t)0, (sdp_proto_e)0, (sdp_text_t)0);
Nu = sdp_media_count(user, sdp_media_any, (sdp_text_t)0, (sdp_proto_e)0, (sdp_text_t)0);
Nr = sdp_media_count(remote, sdp_media_any, (sdp_text_t)0, (sdp_proto_e)0, (sdp_text_t)0);
#ifdef __clang__
#pragma clang diagnostic pop
#endif
if (remote == NULL)
Nmax = Ns + Nu;
else if (Ns < Nr)
Nmax = Nr;
else
Nmax = Ns;
s_media = su_zalloc(home, (Nmax + 1) * (sizeof *s_media));
o_media = su_zalloc(home, (Ns + 1) * (sizeof *o_media));
u_media = su_zalloc(home, (Nu + 1) * (sizeof *u_media));
r_media = su_zalloc(home, (Nr + 1) * (sizeof *r_media));
if (!s_media || !o_media || !u_media || !r_media)
return -1;
um = sdp_media_dup_all(home, user->sdp_media, session);
if (!um && user->sdp_media)
return -1;
u2s = su_alloc(home, (Nu + 1) * sizeof(*u2s));
s2u = su_alloc(home, (Nmax + 1) * sizeof(*s2u));
if (!u2s || !s2u)
return -1;
for (i = 0; i < Nu; i++)
u2s[i] = U2S_NOT_USED;
u2s[Nu] = U2S_SENTINEL;
for (i = 0; i < Nmax; i++)
s2u[i] = U2S_NOT_USED;
s2u[Nmax] = U2S_SENTINEL;
for (i = 0, m = session->sdp_media; m && i < Ns; m = m->m_next)
o_media[i++] = m;
assert(i == Ns);
for (i = 0, m = um; m && i < Nu; m = m->m_next)
u_media[i++] = m;
assert(i == Nu);
m = remote ? remote->sdp_media : NULL;
for (i = 0; m && i < Nr; m = m->m_next)
r_media[i++] = m;
assert(i == Nr);
if (sss->sss_ordered_user && sss->sss_u2s) { /* User SDP is ordered */
for (j = 0; sss->sss_u2s[j] != U2S_SENTINEL; j++) {
i = sss->sss_u2s[j];
if (i == U2S_NOT_USED)
continue;
if (j >= Nu) /* lines removed from user SDP */
continue;
if (i >= Ns) /* I should never be called but somehow i and Ns are 0 here sometimes */
continue;
s_media[i] = u_media[j], u_media[j] = SDP_MEDIA_NONE;
u2s[j] = i, s2u[i] = j;
}
}
if (remote) {
/* Update session according to remote */
for (i = 0; i < Nr; i++) {
rm = r_media[i];
m = s_media[i];
if (!m) {
int codec_mismatch = 0;
if (!rm->m_rejected)
j = soa_sdp_matching_mindex(ss, u_media, rm, &codec_mismatch);
else
j = -1;
if (j == -1) {
s_media[i] = soa_sdp_make_rejected_media(home, rm, session, 0);
continue;
}
else if (codec_mismatch && !ss->ss_rtp_mismatch) {
m = soa_sdp_make_rejected_media(home, u_media[j], session, 1);
soa_sdp_set_rtpmap_pt(s_media[i] = m, rm);
continue;
}
s_media[i] = m = u_media[j]; u_media[j] = SDP_MEDIA_NONE;
u2s[j] = i, s2u[i] = j;
}
if (sdp_media_uses_rtp(rm))
soa_sdp_media_upgrade_rtpmaps(ss, m, rm);
}
}
else {
if (sss->sss_ordered_user) {
/* Update session with unused media in u_media */
if (!sss->sss_reuse_rejected) {
/* Mark previously used slots */
for (i = 0; i < Ns; i++) {
if (s_media[i])
continue;
s_media[i] =
soa_sdp_make_rejected_media(home, o_media[i], session, 0);
}
}
for (j = 0; j < Nu; j++) {
if (u_media[j] == SDP_MEDIA_NONE)
continue;
for (i = 0; i < Nmax; i++) {
if (s_media[i] == NULL) {
s_media[i] = u_media[j], u_media[j] = SDP_MEDIA_NONE;
u2s[j] = i, s2u[i] = j;
break;
}
}
assert(i != Nmax);
}
}
/* Match unused user media by media types with the existing session */
for (i = 0; i < Ns; i++) {
if (s_media[i])
continue;
j = soa_sdp_matching_mindex(ss, u_media, o_media[i], NULL);
if (j == -1) {
s_media[i] = soa_sdp_make_rejected_media(home, o_media[i], session, 0);
continue;
}
s_media[i] = u_media[j], u_media[j] = SDP_MEDIA_NONE;
u2s[j] = i, s2u[i] = j;
}
/* Here we just append new media at the end */
for (j = 0; j < Nu; j++) {
if (u_media[j] != SDP_MEDIA_NONE) {
s_media[i] = u_media[j], u_media[j] = SDP_MEDIA_NONE;
u2s[j] = i, s2u[i] = j;
i++;
}
}
assert(i <= Nmax);
}
mm = &session->sdp_media;
for (i = 0; s_media[i]; i++) {
m = s_media[i]; *mm = m; mm = &m->m_next;
}
*mm = NULL;
s2u[n = i] = U2S_SENTINEL;
*return_u2s = u2s;
*return_s2u = s2u;
#ifndef NDEBUG /* X check */
for (j = 0; j < Nu; j++) {
i = u2s[j];
assert(i == U2S_NOT_USED || s2u[i] == j);
}
for (i = 0; i < n; i++) {
j = s2u[i];
assert(j == U2S_NOT_USED || u2s[j] == i);
}
#endif
return 0;
}
static
int *u2s_alloc(su_home_t *home, int const *u2s)
{
if (u2s) {
int i, *a;
for (i = 0; u2s[i] != U2S_SENTINEL; i++)
;
a = su_alloc(home, (i + 1) * (sizeof *u2s));
if (a)
memcpy(a, u2s, (i + 1) * (sizeof *u2s));
return a;
}
return NULL;
}
/** Check if @a session contains media that are rejected by @a remote. */
static
int soa_sdp_reject_is_needed(sdp_session_t const *session,
sdp_session_t const *remote)
{
sdp_media_t const *sm, *rm;
if (!remote)
return 1;
if (!session)
return 0;
for (sm = session->sdp_media, rm = remote->sdp_media;
sm && rm; sm = sm->m_next, rm = rm->m_next) {
if (rm->m_rejected) {
if (!sm->m_rejected)
return 1;
}
else {
/* Mode bits do not match */
if (((rm->m_mode & sdp_recvonly) == sdp_recvonly)
!= ((sm->m_mode & sdp_sendonly) == sdp_sendonly))
return 1;
}
}
if (sm)
return 1;
return 0;
}
/** If m= line is rejected by remote mark m= line rejected within session */
static
int soa_sdp_reject(su_home_t *home,
sdp_session_t *session,
sdp_session_t const *remote)
{
sdp_media_t *sm;
sdp_media_t const *rm;
if (!session || !session->sdp_media || !remote)
return 0;
rm = remote->sdp_media;
for (sm = session->sdp_media; sm; sm = sm->m_next) {
if (!rm || rm->m_rejected) {
sm->m_rejected = 1;