Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-codec/amr_sdp_match.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 <pjmedia-codec/amr_sdp_match.h>
20 #include <pjmedia/errno.h>
21 #include <pj/pool.h>
22 #include <pj/string.h>
23
24
25 #define GET_FMTP_IVAL_BASE(ival, base, fmtp, param, default_val) \
26 do { \
27 pj_str_t s; \
28 char *p; \
29 p = pj_stristr(&fmtp.fmt_param, &param); \
30 if (!p) { \
31 ival = default_val; \
32 break; \
33 } \
34 pj_strset(&s, p + param.slen, fmtp.fmt_param.slen - \
35 (p - fmtp.fmt_param.ptr) - param.slen); \
36 ival = pj_strtoul2(&s, NULL, base); \
37 } while (0)
38
39 #define GET_FMTP_IVAL(ival, fmtp, param, default_val) \
40 GET_FMTP_IVAL_BASE(ival, 10, fmtp, param, default_val)
41
42
43 /* Toggle AMR octet-align setting in the fmtp. */
44 static pj_status_t amr_toggle_octet_align(pj_pool_t *pool,
45 pjmedia_sdp_media *media,
46 unsigned fmt_idx)
47 {
48 pjmedia_sdp_attr *attr;
49 pjmedia_sdp_fmtp fmtp;
50 const pj_str_t STR_OCTET_ALIGN = {"octet-align=", 12};
51
52 enum { MAX_FMTP_STR_LEN = 160 };
53
54 attr = pjmedia_sdp_media_find_attr2(media, "fmtp",
55 &media->desc.fmt[fmt_idx]);
56 /* Check if the AMR media format has FMTP attribute */
57 if (attr) {
58 char *p;
59 pj_status_t status;
60
61 status = pjmedia_sdp_attr_get_fmtp(attr, &fmtp);
62 if (status != PJ_SUCCESS)
63 return status;
64
65 /* Check if the fmtp has octet-align field. */
66 p = pj_stristr(&fmtp.fmt_param, &STR_OCTET_ALIGN);
67 if (p) {
68 /* It has, just toggle the value */
69 unsigned octet_align;
70 pj_str_t s;
71
72 pj_strset(&s, p + STR_OCTET_ALIGN.slen, fmtp.fmt_param.slen -
73 (p - fmtp.fmt_param.ptr) - STR_OCTET_ALIGN.slen);
74 octet_align = pj_strtoul(&s);
75 *(p + STR_OCTET_ALIGN.slen) = (char)(octet_align? '0' : '1');
76 } else {
77 /* It doesn't, append octet-align field */
78 char buf[MAX_FMTP_STR_LEN];
79
80 pj_ansi_snprintf(buf, MAX_FMTP_STR_LEN, "%.*s;octet-align=1",
81 (int)fmtp.fmt_param.slen, fmtp.fmt_param.ptr);
82 attr->value = pj_strdup3(pool, buf);
83 }
84 } else {
85 /* Add new attribute for the AMR media format with octet-align
86 * field set.
87 */
88 char buf[MAX_FMTP_STR_LEN];
89
90 attr = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_attr);
91 attr->name = pj_str("fmtp");
92 pj_ansi_snprintf(buf, MAX_FMTP_STR_LEN, "%.*s octet-align=1",
93 (int)media->desc.fmt[fmt_idx].slen,
94 media->desc.fmt[fmt_idx].ptr);
95 attr->value = pj_strdup3(pool, buf);
96 media->attr[media->attr_count++] = attr;
97 }
98
99 return PJ_SUCCESS;
100 }
101
102
103 PJ_DEF(pj_status_t) pjmedia_codec_amr_match_sdp( pj_pool_t *pool,
104 pjmedia_sdp_media *offer,
105 unsigned o_fmt_idx,
106 pjmedia_sdp_media *answer,
107 unsigned a_fmt_idx,
108 unsigned option)
109 {
110 /* Negotiated format-param field-names constants. */
111 const pj_str_t STR_OCTET_ALIGN = {"octet-align=", 12};
112 const pj_str_t STR_CRC = {"crc=", 4};
113 const pj_str_t STR_ROBUST_SORTING = {"robust-sorting=", 15};
114 const pj_str_t STR_INTERLEAVING = {"interleaving=", 13};
115
116 /* Negotiated params and their default values. */
117 unsigned a_octet_align = 0, o_octet_align = 0;
118 unsigned a_crc = 0, o_crc = 0;
119 unsigned a_robust_sorting = 0, o_robust_sorting = 0;
120 unsigned a_interleaving = 0, o_interleaving = 0;
121
122 const pjmedia_sdp_attr *attr_ans;
123 const pjmedia_sdp_attr *attr_ofr;
124 pjmedia_sdp_fmtp fmtp;
125 pj_status_t status;
126
127 /* Parse offerer FMTP */
128 attr_ofr = pjmedia_sdp_media_find_attr2(offer, "fmtp",
129 &offer->desc.fmt[o_fmt_idx]);
130 if (attr_ofr) {
131 status = pjmedia_sdp_attr_get_fmtp(attr_ofr, &fmtp);
132 if (status != PJ_SUCCESS)
133 return status;
134
135 GET_FMTP_IVAL(o_octet_align, fmtp, STR_OCTET_ALIGN, 0);
136 GET_FMTP_IVAL(o_crc, fmtp, STR_CRC, 0);
137 GET_FMTP_IVAL(o_robust_sorting, fmtp, STR_ROBUST_SORTING, 0);
138 GET_FMTP_IVAL(o_interleaving, fmtp, STR_INTERLEAVING, 0);
139 }
140
141 /* Parse answerer FMTP */
142 attr_ans = pjmedia_sdp_media_find_attr2(answer, "fmtp",
143 &answer->desc.fmt[a_fmt_idx]);
144 if (attr_ans) {
145 status = pjmedia_sdp_attr_get_fmtp(attr_ans, &fmtp);
146 if (status != PJ_SUCCESS)
147 return status;
148
149 GET_FMTP_IVAL(a_octet_align, fmtp, STR_OCTET_ALIGN, 0);
150 GET_FMTP_IVAL(a_crc, fmtp, STR_CRC, 0);
151 GET_FMTP_IVAL(a_robust_sorting, fmtp, STR_ROBUST_SORTING, 0);
152 GET_FMTP_IVAL(a_interleaving, fmtp, STR_INTERLEAVING, 0);
153 }
154
155 /* First, match crc, robust-sorting, interleaving settings */
156 if (a_crc != o_crc ||
157 a_robust_sorting != o_robust_sorting ||
158 a_interleaving != o_interleaving)
159 {
160 return PJMEDIA_SDP_EFORMATNOTEQUAL;
161 }
162
163 /* Match octet-align setting */
164 if (a_octet_align != o_octet_align) {
165 /* Check if answer can be modified to match to the offer */
166 if (option & PJMEDIA_SDP_NEG_FMT_MATCH_ALLOW_MODIFY_ANSWER) {
167 status = amr_toggle_octet_align(pool, answer, a_fmt_idx);
168 return status;
169 } else {
170 return PJMEDIA_SDP_EFORMATNOTEQUAL;
171 }
172 }
173
174 return PJ_SUCCESS;
175 }