blob: cd721d395db5e9a3868cb8b61b5e159fdbc7a402 [file] [log] [blame]
Neels Hofmeyreef45782019-10-21 03:24:04 +02001/* Minimalistic SDP parse/compose implementation, focused on GSM audio codecs */
2/*
Vadim Yanitskiy999a5932023-05-18 17:22:26 +07003 * (C) 2019 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
Neels Hofmeyreef45782019-10-21 03:24:04 +02004 * All Rights Reserved
5 *
6 * SPDX-License-Identifier: AGPL-3.0+
7 *
8 * Author: Neels Hofmeyr
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#include <string.h>
25#include <errno.h>
26
27#include <osmocom/core/utils.h>
28#include <osmocom/core/logging.h>
29
30#include <osmocom/msc/debug.h>
31#include <osmocom/msc/sdp_msg.h>
32
Neels Hofmeyr2d116822022-08-06 17:39:04 +020033bool sdp_audio_codec_is_set(const struct sdp_audio_codec *a)
34{
35 return a && a->subtype_name[0];
36}
37
Neels Hofmeyreef45782019-10-21 03:24:04 +020038/* Compare name, rate and fmtp, returning typical cmp result: 0 on match, and -1 / 1 on mismatch.
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +010039 * If cmp_fmtp is false, do *not* compare the fmtp string; if true, compare fmtp 1:1 as strings.
40 * If cmp_payload_type is false, do *not* compare the payload_type number.
Neels Hofmeyreef45782019-10-21 03:24:04 +020041 * The fmtp is only string-compared -- e.g. if AMR parameters appear in a different order, it amounts to a mismatch even
42 * though all parameters are the same. */
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +010043int sdp_audio_codec_cmp(const struct sdp_audio_codec *a, const struct sdp_audio_codec *b,
44 bool cmp_fmtp, bool cmp_payload_type)
Neels Hofmeyreef45782019-10-21 03:24:04 +020045{
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +010046 int cmp;
Neels Hofmeyreef45782019-10-21 03:24:04 +020047 if (a == b)
48 return 0;
49 if (!a)
50 return -1;
51 if (!b)
52 return 1;
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +010053 cmp = strncmp(a->subtype_name, b->subtype_name, sizeof(a->subtype_name));
54 if (cmp)
55 return cmp;
56 cmp = OSMO_CMP(a->rate, b->rate);
57 if (cmp)
58 return cmp;
59 if (cmp_fmtp) {
60 cmp = strncmp(a->fmtp, b->fmtp, sizeof(a->fmtp));
61 if (cmp)
62 return cmp;
63 }
64 if (cmp_payload_type) {
65 cmp = OSMO_CMP(a->payload_type, b->payload_type);
66 if (cmp)
67 return cmp;
68 }
69 return 0;
70}
Neels Hofmeyreef45782019-10-21 03:24:04 +020071
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +010072/* Compare two lists of audio codecs, returning typical cmp result: 0 on match, and -1 / 1 on mismatch.
73 * The ordering in the two lists may differ, except that the first codec in 'a' must also be the first codec in 'b'.
74 * This is because the first codec typically expresses the preferred codec to use.
75 * If cmp_fmtp is false, do *not* compare the fmtp strings; if true, compare fmtp 1:1 as strings.
76 * If cmp_payload_type is false, do *not* compare the payload_type numbers.
77 * The fmtp is only string-compared -- e.g. if AMR parameters appear in a different order, it amounts to a mismatch even
78 * though all parameters are the same. */
79int sdp_audio_codecs_cmp(const struct sdp_audio_codecs *a, const struct sdp_audio_codecs *b,
80 bool cmp_fmtp, bool cmp_payload_type)
81{
82 const struct sdp_audio_codec *codec_a;
83 const struct sdp_audio_codec *codec_b;
84 int cmp;
85 if (a == b)
86 return 0;
87 if (!a)
Neels Hofmeyreef45782019-10-21 03:24:04 +020088 return -1;
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +010089 if (!b)
Neels Hofmeyreef45782019-10-21 03:24:04 +020090 return 1;
91
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +010092 cmp = OSMO_CMP(a->count, b->count);
93 if (cmp)
94 return cmp;
95
96 if (!a->count)
97 return 0;
98
99 /* The first codec is the "chosen" codec and should match. The others may appear in different order. */
100 cmp = sdp_audio_codec_cmp(&a->codec[0], &b->codec[0], cmp_fmtp, cmp_payload_type);
101 if (cmp)
102 return cmp;
103
104 /* See if each codec in a is also present in b */
105 foreach_sdp_audio_codec(codec_a, a) {
106 bool match_found = false;
107 foreach_sdp_audio_codec(codec_b, b) {
108 if (!sdp_audio_codec_cmp(codec_a, codec_b, cmp_fmtp, cmp_payload_type)) {
109 match_found = true;
110 break;
111 }
112 }
113 if (!match_found)
114 return -1;
115 }
Neels Hofmeyreef45782019-10-21 03:24:04 +0200116
117 return 0;
118}
119
120/* Given a predefined fixed payload_type number, add an SDP audio codec entry, if not present yet.
121 * The payload_type must exist in sdp_msg_payload_type_names.
122 * Return the audio codec created or already existing for this payload type number.
123 */
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100124struct sdp_audio_codec *sdp_audio_codecs_add(struct sdp_audio_codecs *ac, unsigned int payload_type,
125 const char *subtype_name, unsigned int rate, const char *fmtp)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200126{
127 struct sdp_audio_codec *codec;
128
129 /* Does an entry already exist? */
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100130 codec = sdp_audio_codecs_by_payload_type(ac, payload_type, false);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200131 if (codec) {
132 /* Already exists, sanity check */
133 if (!codec->subtype_name[0])
134 OSMO_STRLCPY_ARRAY(codec->subtype_name, subtype_name);
135 else if (strcmp(codec->subtype_name, subtype_name)) {
136 /* There already is an entry with this payload_type number but a mismatching subtype_name. That is
137 * weird, rather abort. */
138 return NULL;
139 }
140 if (codec->rate != rate
141 || (fmtp && strcmp(fmtp, codec->fmtp))) {
142 /* Mismatching details. Rather abort */
143 return NULL;
144 }
145 return codec;
146 }
147
148 /* None exists, create codec entry for this payload type number */
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100149 codec = sdp_audio_codecs_by_payload_type(ac, payload_type, true);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200150 /* NULL means unable to add an entry */
151 if (!codec)
152 return NULL;
153
154 OSMO_STRLCPY_ARRAY(codec->subtype_name, subtype_name);
155 if (fmtp)
156 OSMO_STRLCPY_ARRAY(codec->fmtp, fmtp);
Neels Hofmeyr8ff00e72023-03-04 03:56:46 +0100157 codec->rate = rate;
Neels Hofmeyreef45782019-10-21 03:24:04 +0200158 return codec;
159}
160
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100161struct sdp_audio_codec *sdp_audio_codecs_add_copy(struct sdp_audio_codecs *ac, const struct sdp_audio_codec *codec)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200162{
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100163 return sdp_audio_codecs_add(ac, codec->payload_type, codec->subtype_name, codec->rate,
164 codec->fmtp[0] ? codec->fmtp : NULL);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200165}
166
Neels Hofmeyr40d34972022-07-19 12:15:27 +0200167/* Find or create an entry for the given payload_type number in the given list of codecs.
168 * If the given payload_type number is already present in ac, return the first matching entry.
169 * If no such payload_type number is present: a) return NULL if create == false;
170 * b) If create == true, add a mostly empty codec entry to the end of ac with the given payload_type number, and return
171 * the created entry.
172 * If create == true, a NULL return value means that there was no unused entry left in ac to add this payload_type.
173 */
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100174struct sdp_audio_codec *sdp_audio_codecs_by_payload_type(struct sdp_audio_codecs *ac, unsigned int payload_type,
175 bool create)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200176{
177 struct sdp_audio_codec *codec;
178 foreach_sdp_audio_codec(codec, ac) {
179 if (codec->payload_type == payload_type)
180 return codec;
181 }
182
183 if (!create)
184 return NULL;
185
186 /* Not found; codec points after the last entry now. */
187 if ((codec - ac->codec) >= ARRAY_SIZE(ac->codec))
188 return NULL;
189
190 *codec = (struct sdp_audio_codec){
191 .payload_type = payload_type,
192 .rate = 8000,
193 };
194
195 ac->count = (codec - ac->codec) + 1;
196 return codec;
197}
198
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +0100199/* Return a given sdp_msg's codec entry that matches the subtype_name and rate of the given codec, or NULL if no
200 * match is found. Comparison is made by sdp_audio_codec_cmp(cmp_payload_type=false). */
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100201struct sdp_audio_codec *sdp_audio_codecs_by_descr(struct sdp_audio_codecs *ac, const struct sdp_audio_codec *codec)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200202{
203 struct sdp_audio_codec *i;
204 foreach_sdp_audio_codec(i, ac) {
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +0100205 if (!sdp_audio_codec_cmp(i, codec, false, false))
Neels Hofmeyreef45782019-10-21 03:24:04 +0200206 return i;
207 }
208 return NULL;
209}
210
211/* Remove the codec entry pointed at by 'codec'. 'codec' must point at an entry of 'sdp' (to use an external codec
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100212 * instance, use sdp_audio_codecs_by_descr()).
Neels Hofmeyreef45782019-10-21 03:24:04 +0200213 * Return 0 on success, -ENOENT if codec does not point at the sdp->codec array. */
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100214int sdp_audio_codecs_remove(struct sdp_audio_codecs *ac, const struct sdp_audio_codec *codec)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200215{
216 struct sdp_audio_codec *i;
217 if ((codec < ac->codec)
218 || ((codec - ac->codec) >= OSMO_MIN(ac->count, ARRAY_SIZE(ac->codec))))
219 return -ENOENT;
220
221 /* Move all following entries one up */
222 ac->count--;
223 foreach_sdp_audio_codec(i, ac) {
224 if (i < codec)
225 continue;
226 *i = *(i+1);
227 }
228 return 0;
229}
230
Neels Hofmeyr01431082022-07-19 12:16:02 +0200231static const char * const sdp_mode_str[] = {
232 [SDP_MODE_UNSET] = "-",
233 [SDP_MODE_SENDONLY] = "sendonly",
234 [SDP_MODE_RECVONLY] = "recvonly",
235 [SDP_MODE_SENDRECV] = "sendrecv",
236 [SDP_MODE_INACTIVE] = "inactive",
237};
238
Neels Hofmeyreef45782019-10-21 03:24:04 +0200239/* Convert struct sdp_msg to the actual SDP protocol representation */
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100240int sdp_msg_to_sdp_str_buf(char *dst, size_t dst_size, const struct sdp_msg *sdp)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200241{
242 const struct sdp_audio_codec *codec;
243 struct osmo_strbuf sb = { .buf = dst, .len = dst_size };
Neels Hofmeyr6d9b4852022-09-05 16:02:50 +0200244 const char *ip;
245 char ipv;
246
247 if (!sdp) {
248 OSMO_STRBUF_PRINTF(sb, "%s", "");
249 return sb.chars_needed;
250 }
251
252 ip = sdp->rtp.ip[0] ? sdp->rtp.ip : "0.0.0.0";
253 ipv = (osmo_ip_str_type(ip) == AF_INET6) ? '6' : '4';
Neels Hofmeyreef45782019-10-21 03:24:04 +0200254
255 OSMO_STRBUF_PRINTF(sb,
256 "v=0\r\n"
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +0200257 "o=OsmoMSC 0 0 IN IP%c %s\r\n"
Neels Hofmeyreef45782019-10-21 03:24:04 +0200258 "s=GSM Call\r\n"
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +0200259 "c=IN IP%c %s\r\n"
Neels Hofmeyreef45782019-10-21 03:24:04 +0200260 "t=0 0\r\n"
261 "m=audio %d RTP/AVP",
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +0200262 ipv, ip, ipv, ip,
Neels Hofmeyreef45782019-10-21 03:24:04 +0200263 sdp->rtp.port);
264
265 /* Append all payload type numbers to 'm=audio <port> RTP/AVP 3 4 112' line */
266 foreach_sdp_audio_codec(codec, &sdp->audio_codecs)
267 OSMO_STRBUF_PRINTF(sb, " %d", codec->payload_type);
268 OSMO_STRBUF_PRINTF(sb, "\r\n");
269
270 /* Add details for all codecs */
271 foreach_sdp_audio_codec(codec, &sdp->audio_codecs) {
Neels Hofmeyr5b1f9a22022-08-06 17:40:52 +0200272 if (!sdp_audio_codec_is_set(codec))
273 continue;
274 OSMO_STRBUF_PRINTF(sb, "a=rtpmap:%d %s/%d\r\n", codec->payload_type, codec->subtype_name,
275 codec->rate > 0 ? codec->rate : 8000);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200276 if (codec->fmtp[0])
277 OSMO_STRBUF_PRINTF(sb, "a=fmtp:%d %s\r\n", codec->payload_type, codec->fmtp);
278 }
279
280 OSMO_STRBUF_PRINTF(sb, "a=ptime:%d\r\n", sdp->ptime > 0? sdp->ptime : 20);
281
Neels Hofmeyr01431082022-07-19 12:16:02 +0200282 if (sdp->mode != SDP_MODE_UNSET && sdp->mode < ARRAY_SIZE(sdp_mode_str))
283 OSMO_STRBUF_PRINTF(sb, "a=%s\r\n", sdp_mode_str[sdp->mode]);
284
Neels Hofmeyreef45782019-10-21 03:24:04 +0200285 return sb.chars_needed;
286}
287
288/* Return the first line ending (or the end of the string) at or after the given string position. */
289const char *sdp_msg_line_end(const char *src)
290{
291 const char *line_end = strchr(src, '\r');
292 if (!line_end)
293 line_end = strchr(src, '\n');
294 if (!line_end)
295 line_end = src + strlen(src);
296 return line_end;
297}
298
299/* parse a line like 'a=rtpmap:0 PCMU/8000', 'a=fmtp:112 octet-align=1; mode-set=4', 'a=ptime:20'.
300 * The src should point at the character after 'a=', e.g. at the start of 'rtpmap', 'fmtp', 'ptime'
301 */
302int sdp_parse_attrib(struct sdp_msg *sdp, const char *src)
303{
304 unsigned int payload_type;
305 struct sdp_audio_codec *codec;
306#define A_RTPMAP "rtpmap:"
307#define A_FMTP "fmtp:"
308#define A_PTIME "ptime:"
309#define A_RTCP "rtcp:"
Neels Hofmeyreef45782019-10-21 03:24:04 +0200310
311 if (osmo_str_startswith(src, A_RTPMAP)) {
Neels Hofmeyr40d34972022-07-19 12:15:27 +0200312 /* "a=rtpmap:3 GSM/8000" */
Neels Hofmeyreef45782019-10-21 03:24:04 +0200313 char *audio_name;
314 unsigned int channels = 1;
315 if (sscanf(src, A_RTPMAP "%u", &payload_type) != 1)
316 return -EINVAL;
317
318 audio_name = strchr(src, ' ');
319 if (!audio_name || audio_name >= sdp_msg_line_end(src))
320 return -EINVAL;
321
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100322 codec = sdp_audio_codecs_by_payload_type(&sdp->audio_codecs, payload_type, true);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200323 if (!codec)
324 return -ENOSPC;
325
326 if (sscanf(audio_name, " %31[^/]/%u/%u", codec->subtype_name, &codec->rate, &channels) < 1)
327 return -EINVAL;
328
329 if (channels != 1)
330 return -ENOTSUP;
331 }
332
333 else if (osmo_str_startswith(src, A_FMTP)) {
Neels Hofmeyr40d34972022-07-19 12:15:27 +0200334 /* "a=fmtp:112 octet-align=1;mode-set=0,1,2,3" */
Neels Hofmeyreef45782019-10-21 03:24:04 +0200335 char *fmtp_str;
336 const char *line_end = sdp_msg_line_end(src);
337 if (sscanf(src, A_FMTP "%u", &payload_type) != 1)
338 return -EINVAL;
339
340 fmtp_str = strchr(src, ' ');
341 if (!fmtp_str)
342 return -EINVAL;
343 fmtp_str++;
344 if (fmtp_str >= line_end)
345 return -EINVAL;
346
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100347 codec = sdp_audio_codecs_by_payload_type(&sdp->audio_codecs, payload_type, true);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200348 if (!codec)
349 return -ENOSPC;
350
351 /* (+1 because osmo_strlcpy() interprets it as size including the '\0') */
352 osmo_strlcpy(codec->fmtp, fmtp_str, line_end - fmtp_str + 1);
353 }
354
355 else if (osmo_str_startswith(src, A_PTIME)) {
Neels Hofmeyr40d34972022-07-19 12:15:27 +0200356 /* "a=ptime:20" */
Neels Hofmeyreef45782019-10-21 03:24:04 +0200357 if (sscanf(src, A_PTIME "%u", &sdp->ptime) != 1)
358 return -EINVAL;
359
360 }
361
362 else if (osmo_str_startswith(src, A_RTCP)) {
363 /* TODO? */
364 }
365
Neels Hofmeyr01431082022-07-19 12:16:02 +0200366 else if (osmo_str_startswith(src, sdp_mode_str[SDP_MODE_SENDRECV])) {
Neels Hofmeyr40d34972022-07-19 12:15:27 +0200367 /* "a=sendrecv" */
Neels Hofmeyr01431082022-07-19 12:16:02 +0200368 sdp->mode = SDP_MODE_SENDRECV;
Neels Hofmeyreef45782019-10-21 03:24:04 +0200369 }
370
Neels Hofmeyr01431082022-07-19 12:16:02 +0200371 else if (osmo_str_startswith(src, sdp_mode_str[SDP_MODE_SENDONLY])) {
Neels Hofmeyr40d34972022-07-19 12:15:27 +0200372 /* "a=sendonly" */
Neels Hofmeyr01431082022-07-19 12:16:02 +0200373 sdp->mode = SDP_MODE_SENDONLY;
Neels Hofmeyreef45782019-10-21 03:24:04 +0200374 }
375
Neels Hofmeyr01431082022-07-19 12:16:02 +0200376 else if (osmo_str_startswith(src, sdp_mode_str[SDP_MODE_RECVONLY])) {
Neels Hofmeyr40d34972022-07-19 12:15:27 +0200377 /* "a=recvonly" */
Neels Hofmeyr01431082022-07-19 12:16:02 +0200378 sdp->mode = SDP_MODE_RECVONLY;
379 }
380
381 else if (osmo_str_startswith(src, sdp_mode_str[SDP_MODE_INACTIVE])) {
382 /* "a=inactive" */
383 sdp->mode = SDP_MODE_INACTIVE;
Neels Hofmeyreef45782019-10-21 03:24:04 +0200384 }
385
386 return 0;
387}
388
389const struct value_string sdp_msg_payload_type_names[] = {
390 { 0, "PCMU" },
391 { 3, "GSM" },
392 { 8, "PCMA" },
393 { 18, "G729" },
394 { 110, "GSM-EFR" },
395 { 111, "GSM-HR-08" },
396 { 112, "AMR" },
397 { 113, "AMR-WB" },
398 {}
399};
400
401/* Return payload type number matching given string ("AMR", "GSM", ...) or negative if not found. */
402int sdp_subtype_name_to_payload_type(const char *subtype_name)
403{
404 return get_string_value(sdp_msg_payload_type_names, subtype_name);
405}
406
407/* Parse a line like 'm=audio 16398 RTP/AVP 0 3 8 96 112', starting after the '=' */
408static int sdp_parse_media_description(struct sdp_msg *sdp, const char *src)
409{
410 unsigned int port;
411 int i;
412 const char *payload_type_str;
413 const char *line_end = sdp_msg_line_end(src);
414 if (sscanf(src, "audio %u RTP/AVP", &port) < 1)
415 return -ENOTSUP;
416
Vadim Yanitskiy40b11c92020-02-09 04:04:54 +0700417 if (port > 0xffff)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200418 return -EINVAL;
419
420 sdp->rtp.port = port;
421
422 /* skip "audio 12345 RTP/AVP ", i.e. 3 spaces on */
423 payload_type_str = src;
424 for (i = 0; i < 3; i++) {
425 payload_type_str = strchr(payload_type_str, ' ');
426 if (!payload_type_str)
427 return -EINVAL;
428 while (*payload_type_str == ' ')
429 payload_type_str++;
430 if (payload_type_str >= line_end)
431 return -EINVAL;
432 }
433
434 /* Parse listing of payload type numbers after "RTP/AVP" */
435 while (payload_type_str < line_end) {
436 unsigned int payload_type;
437 struct sdp_audio_codec *codec;
438 const char *subtype_name;
439 if (sscanf(payload_type_str, "%u", &payload_type) < 1)
440 return -EINVAL;
441
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100442 codec = sdp_audio_codecs_by_payload_type(&sdp->audio_codecs, payload_type, true);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200443 if (!codec)
444 return -ENOSPC;
445
446 /* Fill in subtype name for fixed payload types */
447 subtype_name = get_value_string_or_null(sdp_msg_payload_type_names, codec->payload_type);
448 if (subtype_name)
449 OSMO_STRLCPY_ARRAY(codec->subtype_name, subtype_name);
450
451 payload_type_str = strchr(payload_type_str, ' ');
452 if (!payload_type_str)
453 payload_type_str = line_end;
454 while (*payload_type_str == ' ')
455 payload_type_str++;
456 }
457
458 return 0;
459}
460
461/* parse a line like 'c=IN IP4 192.168.11.151' starting after the '=' */
462static int sdp_parse_connection_info(struct sdp_msg *sdp, const char *src)
463{
464 char ipv[10];
465 char addr_str[INET6_ADDRSTRLEN];
466 if (sscanf(src, "IN %s %s", ipv, addr_str) < 2)
467 return -EINVAL;
468
469 /* supporting only IPv4 */
470 if (strcmp(ipv, "IP4"))
471 return -ENOTSUP;
472
473 osmo_sockaddr_str_from_str(&sdp->rtp, addr_str, sdp->rtp.port);
474 return 0;
475}
476
477/* Parse SDP string into struct sdp_msg. Return 0 on success, negative on error. */
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100478int sdp_msg_from_sdp_str(struct sdp_msg *sdp, const char *src)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200479{
480 const char *pos;
481 *sdp = (struct sdp_msg){};
482
483 for (pos = src; pos && *pos; pos++) {
484 char attrib;
485 int rc = 0;
486
487 if (*pos == '\r' || *pos == '\n')
488 continue;
489
490 /* Expecting only lines starting with 'X='. Not being too strict about it is probably alright. */
491 if (pos[1] != '=')
492 goto next_line;
493
494 attrib = *pos;
495 pos += 2;
496 switch (attrib) {
497 /* a=... */
498 case 'a':
499 rc = sdp_parse_attrib(sdp, pos);
500 break;
501 case 'm':
502 rc = sdp_parse_media_description(sdp, pos);
503 break;
504 case 'c':
505 rc = sdp_parse_connection_info(sdp, pos);
506 break;
507 default:
508 /* ignore any other parameters */
509 break;
510 }
511
512 if (rc) {
513 size_t line_len;
514 const char *line_end = sdp_msg_line_end(pos);
515 pos -= 2;
516 line_len = line_end - pos;
517 switch (rc) {
518 case -EINVAL:
519 LOGP(DMNCC, LOGL_ERROR,
520 "Failed to parse SDP: invalid line: %s\n", osmo_quote_str(pos, line_len));
521 break;
522 case -ENOSPC:
523 LOGP(DMNCC, LOGL_ERROR,
524 "Failed to parse SDP: no more space for: %s\n", osmo_quote_str(pos, line_len));
525 break;
526 case -ENOTSUP:
527 LOGP(DMNCC, LOGL_ERROR,
528 "Failed to parse SDP: not supported: %s\n", osmo_quote_str(pos, line_len));
529 break;
530 default:
531 LOGP(DMNCC, LOGL_ERROR,
532 "Failed to parse SDP: %s\n", osmo_quote_str(pos, line_len));
533 break;
534 }
535 return rc;
536 }
537next_line:
538 pos = strstr(pos, "\r\n");
539 if (!pos)
540 break;
541 }
542
543 return 0;
544}
545
546/* Leave only those codecs in 'ac_dest' that are also present in 'ac_other'.
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +0100547 * The matching is made by sdp_audio_codec_cmp(cmp_payload_type=false), i.e. payload_type numbers are not compared and
548 * fmtp parameters are compared 1:1 as plain strings.
Neels Hofmeyreef45782019-10-21 03:24:04 +0200549 * If translate_payload_type_numbers has an effect if ac_dest and ac_other have mismatching payload_type numbers for the
550 * same SDP codec descriptions. If translate_payload_type_numbers is true, take the payload_type numbers from ac_other.
551 * If false, keep payload_type numbers in ac_dest unchanged. */
552void sdp_audio_codecs_intersection(struct sdp_audio_codecs *ac_dest, const struct sdp_audio_codecs *ac_other,
553 bool translate_payload_type_numbers)
554{
555 int i;
556 for (i = 0; i < ac_dest->count; i++) {
557 struct sdp_audio_codec *codec = &ac_dest->codec[i];
558 struct sdp_audio_codec *other;
559 OSMO_ASSERT(i < ARRAY_SIZE(ac_dest->codec));
560
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100561 other = sdp_audio_codecs_by_descr((struct sdp_audio_codecs *)ac_other, codec);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200562
563 if (!other) {
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100564 OSMO_ASSERT(sdp_audio_codecs_remove(ac_dest, codec) == 0);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200565 i--;
566 continue;
567 }
568
569 /* Doing payload_type number translation of part of the intersection because it makes the algorithm
570 * simpler: we already know ac_dest is a subset of ac_other, and there is no need to resolve payload
571 * type number conflicts. */
572 if (translate_payload_type_numbers)
573 codec->payload_type = other->payload_type;
574 }
575}
576
577/* Make sure the given codec is listed as the first codec. 'codec' must be an actual codec entry of the given audio
578 * codecs list. */
579void sdp_audio_codecs_select(struct sdp_audio_codecs *ac, struct sdp_audio_codec *codec)
580{
581 struct sdp_audio_codec tmp;
582 struct sdp_audio_codec *pos;
583 OSMO_ASSERT((codec >= ac->codec)
584 && ((codec - ac->codec) < OSMO_MIN(ac->count, ARRAY_SIZE(ac->codec))));
585
586 /* Already the first? */
587 if (codec == ac->codec)
588 return;
589
590 tmp = *codec;
591 for (pos = codec - 1; pos >= ac->codec; pos--)
592 pos[1] = pos[0];
593
594 ac->codec[0] = tmp;
595 return;
596}
597
598/* Short single-line representation of an SDP audio codec, convenient for logging.
599 * Like "AMR/8000:octet-align=1#122" */
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100600int sdp_audio_codec_to_str_buf(char *buf, size_t buflen, const struct sdp_audio_codec *codec)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200601{
602 struct osmo_strbuf sb = { .buf = buf, .len = buflen };
603 OSMO_STRBUF_PRINTF(sb, "%s", codec->subtype_name);
Neels Hofmeyr99ab7c52022-01-13 19:04:59 +0100604 if (codec->rate != 8000)
605 OSMO_STRBUF_PRINTF(sb, "/%u", codec->rate);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200606 if (codec->fmtp[0])
607 OSMO_STRBUF_PRINTF(sb, ":%s", codec->fmtp);
Neels Hofmeyr99ab7c52022-01-13 19:04:59 +0100608 OSMO_STRBUF_PRINTF(sb, "#%d", codec->payload_type);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200609 return sb.chars_needed;
610}
611
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100612char *sdp_audio_codec_to_str_c(void *ctx, const struct sdp_audio_codec *codec)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200613{
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100614 OSMO_NAME_C_IMPL(ctx, 32, "sdp_audio_codec_to_str_c-ERROR", sdp_audio_codec_to_str_buf, codec)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200615}
616
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100617const char *sdp_audio_codec_to_str(const struct sdp_audio_codec *codec)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200618{
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100619 return sdp_audio_codec_to_str_c(OTC_SELECT, codec);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200620}
621
622/* Short single-line representation of a list of SDP audio codecs, convenient for logging */
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100623int sdp_audio_codecs_to_str_buf(char *buf, size_t buflen, const struct sdp_audio_codecs *ac)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200624{
625 struct osmo_strbuf sb = { .buf = buf, .len = buflen };
626 const struct sdp_audio_codec *codec;
627 if (!ac->count)
628 OSMO_STRBUF_PRINTF(sb, "(no-codecs)");
629 foreach_sdp_audio_codec(codec, ac) {
630 bool first = (codec == ac->codec);
631 if (!first)
632 OSMO_STRBUF_PRINTF(sb, ",");
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100633 OSMO_STRBUF_APPEND(sb, sdp_audio_codec_to_str_buf, codec);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200634 }
635 return sb.chars_needed;
636}
637
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100638char *sdp_audio_codecs_to_str_c(void *ctx, const struct sdp_audio_codecs *ac)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200639{
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100640 OSMO_NAME_C_IMPL(ctx, 128, "sdp_audio_codecs_to_str_c-ERROR", sdp_audio_codecs_to_str_buf, ac)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200641}
642
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100643const char *sdp_audio_codecs_to_str(const struct sdp_audio_codecs *ac)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200644{
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100645 return sdp_audio_codecs_to_str_c(OTC_SELECT, ac);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200646}
647
648/* Short single-line representation of an SDP message, convenient for logging */
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100649int sdp_msg_to_str_buf(char *buf, size_t buflen, const struct sdp_msg *sdp)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200650{
651 struct osmo_strbuf sb = { .buf = buf, .len = buflen };
652 if (!sdp) {
653 OSMO_STRBUF_PRINTF(sb, "NULL");
654 return sb.chars_needed;
655 }
656
657 OSMO_STRBUF_PRINTF(sb, OSMO_SOCKADDR_STR_FMT, OSMO_SOCKADDR_STR_FMT_ARGS(&sdp->rtp));
658 OSMO_STRBUF_PRINTF(sb, "{");
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100659 OSMO_STRBUF_APPEND(sb, sdp_audio_codecs_to_str_buf, &sdp->audio_codecs);
Oliver Smith10632132023-05-12 12:14:22 +0200660 if (sdp->bearer_services.count) {
661 OSMO_STRBUF_PRINTF(sb, ",");
662 OSMO_STRBUF_APPEND(sb, csd_bs_list_to_str_buf, &sdp->bearer_services);
663 }
Neels Hofmeyreef45782019-10-21 03:24:04 +0200664 OSMO_STRBUF_PRINTF(sb, "}");
665 return sb.chars_needed;
666}
667
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100668char *sdp_msg_to_str_c(void *ctx, const struct sdp_msg *sdp)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200669{
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100670 OSMO_NAME_C_IMPL(ctx, 128, "sdp_msg_to_str_c-ERROR", sdp_msg_to_str_buf, sdp)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200671}
672
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100673const char *sdp_msg_to_str(const struct sdp_msg *sdp)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200674{
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100675 return sdp_msg_to_str_c(OTC_SELECT, sdp);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200676}
Oliver Smith10632132023-05-12 12:14:22 +0200677
678void sdp_audio_codecs_set_csd(struct sdp_audio_codecs *ac)
679{
680 *ac = (struct sdp_audio_codecs){
681 .count = 1,
682 .codec = {{
683 .payload_type = 120,
684 .subtype_name = "CLEARMODE",
685 .rate = 8000,
686 }},
687 };
688}