blob: 040d32d4a8f7496846937a2e47fd55a07ef4b291 [file] [log] [blame]
Neels Hofmeyreef45782019-10-21 03:24:04 +02001/* Minimalistic SDP parse/compose implementation, focused on GSM audio codecs */
2/*
3 * (C) 2019 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de>
4 * 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);
157 return codec;
158}
159
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100160struct 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 +0200161{
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100162 return sdp_audio_codecs_add(ac, codec->payload_type, codec->subtype_name, codec->rate,
163 codec->fmtp[0] ? codec->fmtp : NULL);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200164}
165
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100166struct sdp_audio_codec *sdp_audio_codecs_by_payload_type(struct sdp_audio_codecs *ac, unsigned int payload_type,
167 bool create)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200168{
169 struct sdp_audio_codec *codec;
170 foreach_sdp_audio_codec(codec, ac) {
171 if (codec->payload_type == payload_type)
172 return codec;
173 }
174
175 if (!create)
176 return NULL;
177
178 /* Not found; codec points after the last entry now. */
179 if ((codec - ac->codec) >= ARRAY_SIZE(ac->codec))
180 return NULL;
181
182 *codec = (struct sdp_audio_codec){
183 .payload_type = payload_type,
184 .rate = 8000,
185 };
186
187 ac->count = (codec - ac->codec) + 1;
188 return codec;
189}
190
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +0100191/* Return a given sdp_msg's codec entry that matches the subtype_name and rate of the given codec, or NULL if no
192 * match is found. Comparison is made by sdp_audio_codec_cmp(cmp_payload_type=false). */
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100193struct 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 +0200194{
195 struct sdp_audio_codec *i;
196 foreach_sdp_audio_codec(i, ac) {
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +0100197 if (!sdp_audio_codec_cmp(i, codec, false, false))
Neels Hofmeyreef45782019-10-21 03:24:04 +0200198 return i;
199 }
200 return NULL;
201}
202
203/* 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 +0100204 * instance, use sdp_audio_codecs_by_descr()).
Neels Hofmeyreef45782019-10-21 03:24:04 +0200205 * Return 0 on success, -ENOENT if codec does not point at the sdp->codec array. */
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100206int sdp_audio_codecs_remove(struct sdp_audio_codecs *ac, const struct sdp_audio_codec *codec)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200207{
208 struct sdp_audio_codec *i;
209 if ((codec < ac->codec)
210 || ((codec - ac->codec) >= OSMO_MIN(ac->count, ARRAY_SIZE(ac->codec))))
211 return -ENOENT;
212
213 /* Move all following entries one up */
214 ac->count--;
215 foreach_sdp_audio_codec(i, ac) {
216 if (i < codec)
217 continue;
218 *i = *(i+1);
219 }
220 return 0;
221}
222
223/* Convert struct sdp_msg to the actual SDP protocol representation */
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100224int sdp_msg_to_sdp_str_buf(char *dst, size_t dst_size, const struct sdp_msg *sdp)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200225{
226 const struct sdp_audio_codec *codec;
227 struct osmo_strbuf sb = { .buf = dst, .len = dst_size };
Neels Hofmeyr6d9b4852022-09-05 16:02:50 +0200228 const char *ip;
229 char ipv;
230
231 if (!sdp) {
232 OSMO_STRBUF_PRINTF(sb, "%s", "");
233 return sb.chars_needed;
234 }
235
236 ip = sdp->rtp.ip[0] ? sdp->rtp.ip : "0.0.0.0";
237 ipv = (osmo_ip_str_type(ip) == AF_INET6) ? '6' : '4';
Neels Hofmeyreef45782019-10-21 03:24:04 +0200238
239 OSMO_STRBUF_PRINTF(sb,
240 "v=0\r\n"
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +0200241 "o=OsmoMSC 0 0 IN IP%c %s\r\n"
Neels Hofmeyreef45782019-10-21 03:24:04 +0200242 "s=GSM Call\r\n"
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +0200243 "c=IN IP%c %s\r\n"
Neels Hofmeyreef45782019-10-21 03:24:04 +0200244 "t=0 0\r\n"
245 "m=audio %d RTP/AVP",
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +0200246 ipv, ip, ipv, ip,
Neels Hofmeyreef45782019-10-21 03:24:04 +0200247 sdp->rtp.port);
248
249 /* Append all payload type numbers to 'm=audio <port> RTP/AVP 3 4 112' line */
250 foreach_sdp_audio_codec(codec, &sdp->audio_codecs)
251 OSMO_STRBUF_PRINTF(sb, " %d", codec->payload_type);
252 OSMO_STRBUF_PRINTF(sb, "\r\n");
253
254 /* Add details for all codecs */
255 foreach_sdp_audio_codec(codec, &sdp->audio_codecs) {
256 if (codec->subtype_name[0]) {
257 OSMO_STRBUF_PRINTF(sb, "a=rtpmap:%d %s/%d\r\n", codec->payload_type, codec->subtype_name,
258 codec->rate > 0? codec->rate : 8000);
259 }
260
261 if (codec->fmtp[0])
262 OSMO_STRBUF_PRINTF(sb, "a=fmtp:%d %s\r\n", codec->payload_type, codec->fmtp);
263 }
264
265 OSMO_STRBUF_PRINTF(sb, "a=ptime:%d\r\n", sdp->ptime > 0? sdp->ptime : 20);
266
267 return sb.chars_needed;
268}
269
270/* Return the first line ending (or the end of the string) at or after the given string position. */
271const char *sdp_msg_line_end(const char *src)
272{
273 const char *line_end = strchr(src, '\r');
274 if (!line_end)
275 line_end = strchr(src, '\n');
276 if (!line_end)
277 line_end = src + strlen(src);
278 return line_end;
279}
280
281/* parse a line like 'a=rtpmap:0 PCMU/8000', 'a=fmtp:112 octet-align=1; mode-set=4', 'a=ptime:20'.
282 * The src should point at the character after 'a=', e.g. at the start of 'rtpmap', 'fmtp', 'ptime'
283 */
284int sdp_parse_attrib(struct sdp_msg *sdp, const char *src)
285{
286 unsigned int payload_type;
287 struct sdp_audio_codec *codec;
288#define A_RTPMAP "rtpmap:"
289#define A_FMTP "fmtp:"
290#define A_PTIME "ptime:"
291#define A_RTCP "rtcp:"
292#define A_SENDRECV "sendrecv"
293#define A_SENDONLY "sendonly"
294#define A_RECVONLY "recvonly"
295
296 if (osmo_str_startswith(src, A_RTPMAP)) {
297 char *audio_name;
298 unsigned int channels = 1;
299 if (sscanf(src, A_RTPMAP "%u", &payload_type) != 1)
300 return -EINVAL;
301
302 audio_name = strchr(src, ' ');
303 if (!audio_name || audio_name >= sdp_msg_line_end(src))
304 return -EINVAL;
305
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100306 codec = sdp_audio_codecs_by_payload_type(&sdp->audio_codecs, payload_type, true);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200307 if (!codec)
308 return -ENOSPC;
309
310 if (sscanf(audio_name, " %31[^/]/%u/%u", codec->subtype_name, &codec->rate, &channels) < 1)
311 return -EINVAL;
312
313 if (channels != 1)
314 return -ENOTSUP;
315 }
316
317 else if (osmo_str_startswith(src, A_FMTP)) {
318 char *fmtp_str;
319 const char *line_end = sdp_msg_line_end(src);
320 if (sscanf(src, A_FMTP "%u", &payload_type) != 1)
321 return -EINVAL;
322
323 fmtp_str = strchr(src, ' ');
324 if (!fmtp_str)
325 return -EINVAL;
326 fmtp_str++;
327 if (fmtp_str >= line_end)
328 return -EINVAL;
329
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100330 codec = sdp_audio_codecs_by_payload_type(&sdp->audio_codecs, payload_type, true);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200331 if (!codec)
332 return -ENOSPC;
333
334 /* (+1 because osmo_strlcpy() interprets it as size including the '\0') */
335 osmo_strlcpy(codec->fmtp, fmtp_str, line_end - fmtp_str + 1);
336 }
337
338 else if (osmo_str_startswith(src, A_PTIME)) {
339 if (sscanf(src, A_PTIME "%u", &sdp->ptime) != 1)
340 return -EINVAL;
341
342 }
343
344 else if (osmo_str_startswith(src, A_RTCP)) {
345 /* TODO? */
346 }
347
348 else if (osmo_str_startswith(src, A_SENDRECV)) {
349 /* TODO? */
350 }
351
352 else if (osmo_str_startswith(src, A_SENDONLY)) {
353 /* TODO? */
354 }
355
356 else if (osmo_str_startswith(src, A_RECVONLY)) {
357 /* TODO? */
358 }
359
360 return 0;
361}
362
363const struct value_string sdp_msg_payload_type_names[] = {
364 { 0, "PCMU" },
365 { 3, "GSM" },
366 { 8, "PCMA" },
367 { 18, "G729" },
368 { 110, "GSM-EFR" },
369 { 111, "GSM-HR-08" },
370 { 112, "AMR" },
371 { 113, "AMR-WB" },
372 {}
373};
374
375/* Return payload type number matching given string ("AMR", "GSM", ...) or negative if not found. */
376int sdp_subtype_name_to_payload_type(const char *subtype_name)
377{
378 return get_string_value(sdp_msg_payload_type_names, subtype_name);
379}
380
381/* Parse a line like 'm=audio 16398 RTP/AVP 0 3 8 96 112', starting after the '=' */
382static int sdp_parse_media_description(struct sdp_msg *sdp, const char *src)
383{
384 unsigned int port;
385 int i;
386 const char *payload_type_str;
387 const char *line_end = sdp_msg_line_end(src);
388 if (sscanf(src, "audio %u RTP/AVP", &port) < 1)
389 return -ENOTSUP;
390
Vadim Yanitskiy40b11c92020-02-09 04:04:54 +0700391 if (port > 0xffff)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200392 return -EINVAL;
393
394 sdp->rtp.port = port;
395
396 /* skip "audio 12345 RTP/AVP ", i.e. 3 spaces on */
397 payload_type_str = src;
398 for (i = 0; i < 3; i++) {
399 payload_type_str = strchr(payload_type_str, ' ');
400 if (!payload_type_str)
401 return -EINVAL;
402 while (*payload_type_str == ' ')
403 payload_type_str++;
404 if (payload_type_str >= line_end)
405 return -EINVAL;
406 }
407
408 /* Parse listing of payload type numbers after "RTP/AVP" */
409 while (payload_type_str < line_end) {
410 unsigned int payload_type;
411 struct sdp_audio_codec *codec;
412 const char *subtype_name;
413 if (sscanf(payload_type_str, "%u", &payload_type) < 1)
414 return -EINVAL;
415
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100416 codec = sdp_audio_codecs_by_payload_type(&sdp->audio_codecs, payload_type, true);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200417 if (!codec)
418 return -ENOSPC;
419
420 /* Fill in subtype name for fixed payload types */
421 subtype_name = get_value_string_or_null(sdp_msg_payload_type_names, codec->payload_type);
422 if (subtype_name)
423 OSMO_STRLCPY_ARRAY(codec->subtype_name, subtype_name);
424
425 payload_type_str = strchr(payload_type_str, ' ');
426 if (!payload_type_str)
427 payload_type_str = line_end;
428 while (*payload_type_str == ' ')
429 payload_type_str++;
430 }
431
432 return 0;
433}
434
435/* parse a line like 'c=IN IP4 192.168.11.151' starting after the '=' */
436static int sdp_parse_connection_info(struct sdp_msg *sdp, const char *src)
437{
438 char ipv[10];
439 char addr_str[INET6_ADDRSTRLEN];
440 if (sscanf(src, "IN %s %s", ipv, addr_str) < 2)
441 return -EINVAL;
442
443 /* supporting only IPv4 */
444 if (strcmp(ipv, "IP4"))
445 return -ENOTSUP;
446
447 osmo_sockaddr_str_from_str(&sdp->rtp, addr_str, sdp->rtp.port);
448 return 0;
449}
450
451/* Parse SDP string into struct sdp_msg. Return 0 on success, negative on error. */
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100452int sdp_msg_from_sdp_str(struct sdp_msg *sdp, const char *src)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200453{
454 const char *pos;
455 *sdp = (struct sdp_msg){};
456
457 for (pos = src; pos && *pos; pos++) {
458 char attrib;
459 int rc = 0;
460
461 if (*pos == '\r' || *pos == '\n')
462 continue;
463
464 /* Expecting only lines starting with 'X='. Not being too strict about it is probably alright. */
465 if (pos[1] != '=')
466 goto next_line;
467
468 attrib = *pos;
469 pos += 2;
470 switch (attrib) {
471 /* a=... */
472 case 'a':
473 rc = sdp_parse_attrib(sdp, pos);
474 break;
475 case 'm':
476 rc = sdp_parse_media_description(sdp, pos);
477 break;
478 case 'c':
479 rc = sdp_parse_connection_info(sdp, pos);
480 break;
481 default:
482 /* ignore any other parameters */
483 break;
484 }
485
486 if (rc) {
487 size_t line_len;
488 const char *line_end = sdp_msg_line_end(pos);
489 pos -= 2;
490 line_len = line_end - pos;
491 switch (rc) {
492 case -EINVAL:
493 LOGP(DMNCC, LOGL_ERROR,
494 "Failed to parse SDP: invalid line: %s\n", osmo_quote_str(pos, line_len));
495 break;
496 case -ENOSPC:
497 LOGP(DMNCC, LOGL_ERROR,
498 "Failed to parse SDP: no more space for: %s\n", osmo_quote_str(pos, line_len));
499 break;
500 case -ENOTSUP:
501 LOGP(DMNCC, LOGL_ERROR,
502 "Failed to parse SDP: not supported: %s\n", osmo_quote_str(pos, line_len));
503 break;
504 default:
505 LOGP(DMNCC, LOGL_ERROR,
506 "Failed to parse SDP: %s\n", osmo_quote_str(pos, line_len));
507 break;
508 }
509 return rc;
510 }
511next_line:
512 pos = strstr(pos, "\r\n");
513 if (!pos)
514 break;
515 }
516
517 return 0;
518}
519
520/* Leave only those codecs in 'ac_dest' that are also present in 'ac_other'.
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +0100521 * The matching is made by sdp_audio_codec_cmp(cmp_payload_type=false), i.e. payload_type numbers are not compared and
522 * fmtp parameters are compared 1:1 as plain strings.
Neels Hofmeyreef45782019-10-21 03:24:04 +0200523 * If translate_payload_type_numbers has an effect if ac_dest and ac_other have mismatching payload_type numbers for the
524 * same SDP codec descriptions. If translate_payload_type_numbers is true, take the payload_type numbers from ac_other.
525 * If false, keep payload_type numbers in ac_dest unchanged. */
526void sdp_audio_codecs_intersection(struct sdp_audio_codecs *ac_dest, const struct sdp_audio_codecs *ac_other,
527 bool translate_payload_type_numbers)
528{
529 int i;
530 for (i = 0; i < ac_dest->count; i++) {
531 struct sdp_audio_codec *codec = &ac_dest->codec[i];
532 struct sdp_audio_codec *other;
533 OSMO_ASSERT(i < ARRAY_SIZE(ac_dest->codec));
534
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100535 other = sdp_audio_codecs_by_descr((struct sdp_audio_codecs *)ac_other, codec);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200536
537 if (!other) {
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100538 OSMO_ASSERT(sdp_audio_codecs_remove(ac_dest, codec) == 0);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200539 i--;
540 continue;
541 }
542
543 /* Doing payload_type number translation of part of the intersection because it makes the algorithm
544 * simpler: we already know ac_dest is a subset of ac_other, and there is no need to resolve payload
545 * type number conflicts. */
546 if (translate_payload_type_numbers)
547 codec->payload_type = other->payload_type;
548 }
549}
550
551/* Make sure the given codec is listed as the first codec. 'codec' must be an actual codec entry of the given audio
552 * codecs list. */
553void sdp_audio_codecs_select(struct sdp_audio_codecs *ac, struct sdp_audio_codec *codec)
554{
555 struct sdp_audio_codec tmp;
556 struct sdp_audio_codec *pos;
557 OSMO_ASSERT((codec >= ac->codec)
558 && ((codec - ac->codec) < OSMO_MIN(ac->count, ARRAY_SIZE(ac->codec))));
559
560 /* Already the first? */
561 if (codec == ac->codec)
562 return;
563
564 tmp = *codec;
565 for (pos = codec - 1; pos >= ac->codec; pos--)
566 pos[1] = pos[0];
567
568 ac->codec[0] = tmp;
569 return;
570}
571
572/* Short single-line representation of an SDP audio codec, convenient for logging.
573 * Like "AMR/8000:octet-align=1#122" */
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100574int sdp_audio_codec_to_str_buf(char *buf, size_t buflen, const struct sdp_audio_codec *codec)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200575{
576 struct osmo_strbuf sb = { .buf = buf, .len = buflen };
577 OSMO_STRBUF_PRINTF(sb, "%s", codec->subtype_name);
Neels Hofmeyr99ab7c52022-01-13 19:04:59 +0100578 if (codec->rate != 8000)
579 OSMO_STRBUF_PRINTF(sb, "/%u", codec->rate);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200580 if (codec->fmtp[0])
581 OSMO_STRBUF_PRINTF(sb, ":%s", codec->fmtp);
Neels Hofmeyr99ab7c52022-01-13 19:04:59 +0100582 OSMO_STRBUF_PRINTF(sb, "#%d", codec->payload_type);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200583 return sb.chars_needed;
584}
585
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100586char *sdp_audio_codec_to_str_c(void *ctx, const struct sdp_audio_codec *codec)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200587{
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100588 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 +0200589}
590
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100591const char *sdp_audio_codec_to_str(const struct sdp_audio_codec *codec)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200592{
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100593 return sdp_audio_codec_to_str_c(OTC_SELECT, codec);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200594}
595
596/* Short single-line representation of a list of SDP audio codecs, convenient for logging */
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100597int sdp_audio_codecs_to_str_buf(char *buf, size_t buflen, const struct sdp_audio_codecs *ac)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200598{
599 struct osmo_strbuf sb = { .buf = buf, .len = buflen };
600 const struct sdp_audio_codec *codec;
601 if (!ac->count)
602 OSMO_STRBUF_PRINTF(sb, "(no-codecs)");
603 foreach_sdp_audio_codec(codec, ac) {
604 bool first = (codec == ac->codec);
605 if (!first)
606 OSMO_STRBUF_PRINTF(sb, ",");
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100607 OSMO_STRBUF_APPEND(sb, sdp_audio_codec_to_str_buf, codec);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200608 }
609 return sb.chars_needed;
610}
611
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100612char *sdp_audio_codecs_to_str_c(void *ctx, const struct sdp_audio_codecs *ac)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200613{
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100614 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 +0200615}
616
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100617const char *sdp_audio_codecs_to_str(const struct sdp_audio_codecs *ac)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200618{
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100619 return sdp_audio_codecs_to_str_c(OTC_SELECT, ac);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200620}
621
622/* Short single-line representation of an SDP message, convenient for logging */
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100623int sdp_msg_to_str_buf(char *buf, size_t buflen, const struct sdp_msg *sdp)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200624{
625 struct osmo_strbuf sb = { .buf = buf, .len = buflen };
626 if (!sdp) {
627 OSMO_STRBUF_PRINTF(sb, "NULL");
628 return sb.chars_needed;
629 }
630
631 OSMO_STRBUF_PRINTF(sb, OSMO_SOCKADDR_STR_FMT, OSMO_SOCKADDR_STR_FMT_ARGS(&sdp->rtp));
632 OSMO_STRBUF_PRINTF(sb, "{");
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100633 OSMO_STRBUF_APPEND(sb, sdp_audio_codecs_to_str_buf, &sdp->audio_codecs);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200634 OSMO_STRBUF_PRINTF(sb, "}");
635 return sb.chars_needed;
636}
637
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100638char *sdp_msg_to_str_c(void *ctx, const struct sdp_msg *sdp)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200639{
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100640 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 +0200641}
642
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100643const char *sdp_msg_to_str(const struct sdp_msg *sdp)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200644{
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100645 return sdp_msg_to_str_c(OTC_SELECT, sdp);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200646}