blob: 09e1277832172192c6f30be95c89a06872a4a4e1 [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
33/* Compare name, rate and fmtp, returning typical cmp result: 0 on match, and -1 / 1 on mismatch.
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +010034 * If cmp_fmtp is false, do *not* compare the fmtp string; if true, compare fmtp 1:1 as strings.
35 * If cmp_payload_type is false, do *not* compare the payload_type number.
Neels Hofmeyreef45782019-10-21 03:24:04 +020036 * The fmtp is only string-compared -- e.g. if AMR parameters appear in a different order, it amounts to a mismatch even
37 * though all parameters are the same. */
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +010038int sdp_audio_codec_cmp(const struct sdp_audio_codec *a, const struct sdp_audio_codec *b,
39 bool cmp_fmtp, bool cmp_payload_type)
Neels Hofmeyreef45782019-10-21 03:24:04 +020040{
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +010041 int cmp;
Neels Hofmeyreef45782019-10-21 03:24:04 +020042 if (a == b)
43 return 0;
44 if (!a)
45 return -1;
46 if (!b)
47 return 1;
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +010048 cmp = strncmp(a->subtype_name, b->subtype_name, sizeof(a->subtype_name));
49 if (cmp)
50 return cmp;
51 cmp = OSMO_CMP(a->rate, b->rate);
52 if (cmp)
53 return cmp;
54 if (cmp_fmtp) {
55 cmp = strncmp(a->fmtp, b->fmtp, sizeof(a->fmtp));
56 if (cmp)
57 return cmp;
58 }
59 if (cmp_payload_type) {
60 cmp = OSMO_CMP(a->payload_type, b->payload_type);
61 if (cmp)
62 return cmp;
63 }
64 return 0;
65}
Neels Hofmeyreef45782019-10-21 03:24:04 +020066
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +010067/* Compare two lists of audio codecs, returning typical cmp result: 0 on match, and -1 / 1 on mismatch.
68 * The ordering in the two lists may differ, except that the first codec in 'a' must also be the first codec in 'b'.
69 * This is because the first codec typically expresses the preferred codec to use.
70 * If cmp_fmtp is false, do *not* compare the fmtp strings; if true, compare fmtp 1:1 as strings.
71 * If cmp_payload_type is false, do *not* compare the payload_type numbers.
72 * The fmtp is only string-compared -- e.g. if AMR parameters appear in a different order, it amounts to a mismatch even
73 * though all parameters are the same. */
74int sdp_audio_codecs_cmp(const struct sdp_audio_codecs *a, const struct sdp_audio_codecs *b,
75 bool cmp_fmtp, bool cmp_payload_type)
76{
77 const struct sdp_audio_codec *codec_a;
78 const struct sdp_audio_codec *codec_b;
79 int cmp;
80 if (a == b)
81 return 0;
82 if (!a)
Neels Hofmeyreef45782019-10-21 03:24:04 +020083 return -1;
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +010084 if (!b)
Neels Hofmeyreef45782019-10-21 03:24:04 +020085 return 1;
86
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +010087 cmp = OSMO_CMP(a->count, b->count);
88 if (cmp)
89 return cmp;
90
91 if (!a->count)
92 return 0;
93
94 /* The first codec is the "chosen" codec and should match. The others may appear in different order. */
95 cmp = sdp_audio_codec_cmp(&a->codec[0], &b->codec[0], cmp_fmtp, cmp_payload_type);
96 if (cmp)
97 return cmp;
98
99 /* See if each codec in a is also present in b */
100 foreach_sdp_audio_codec(codec_a, a) {
101 bool match_found = false;
102 foreach_sdp_audio_codec(codec_b, b) {
103 if (!sdp_audio_codec_cmp(codec_a, codec_b, cmp_fmtp, cmp_payload_type)) {
104 match_found = true;
105 break;
106 }
107 }
108 if (!match_found)
109 return -1;
110 }
Neels Hofmeyreef45782019-10-21 03:24:04 +0200111
112 return 0;
113}
114
115/* Given a predefined fixed payload_type number, add an SDP audio codec entry, if not present yet.
116 * The payload_type must exist in sdp_msg_payload_type_names.
117 * Return the audio codec created or already existing for this payload type number.
118 */
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100119struct sdp_audio_codec *sdp_audio_codecs_add(struct sdp_audio_codecs *ac, unsigned int payload_type,
120 const char *subtype_name, unsigned int rate, const char *fmtp)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200121{
122 struct sdp_audio_codec *codec;
123
124 /* Does an entry already exist? */
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100125 codec = sdp_audio_codecs_by_payload_type(ac, payload_type, false);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200126 if (codec) {
127 /* Already exists, sanity check */
128 if (!codec->subtype_name[0])
129 OSMO_STRLCPY_ARRAY(codec->subtype_name, subtype_name);
130 else if (strcmp(codec->subtype_name, subtype_name)) {
131 /* There already is an entry with this payload_type number but a mismatching subtype_name. That is
132 * weird, rather abort. */
133 return NULL;
134 }
135 if (codec->rate != rate
136 || (fmtp && strcmp(fmtp, codec->fmtp))) {
137 /* Mismatching details. Rather abort */
138 return NULL;
139 }
140 return codec;
141 }
142
143 /* None exists, create codec entry for this payload type number */
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100144 codec = sdp_audio_codecs_by_payload_type(ac, payload_type, true);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200145 /* NULL means unable to add an entry */
146 if (!codec)
147 return NULL;
148
149 OSMO_STRLCPY_ARRAY(codec->subtype_name, subtype_name);
150 if (fmtp)
151 OSMO_STRLCPY_ARRAY(codec->fmtp, fmtp);
152 return codec;
153}
154
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100155struct 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 +0200156{
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100157 return sdp_audio_codecs_add(ac, codec->payload_type, codec->subtype_name, codec->rate,
158 codec->fmtp[0] ? codec->fmtp : NULL);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200159}
160
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100161struct sdp_audio_codec *sdp_audio_codecs_by_payload_type(struct sdp_audio_codecs *ac, unsigned int payload_type,
162 bool create)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200163{
164 struct sdp_audio_codec *codec;
165 foreach_sdp_audio_codec(codec, ac) {
166 if (codec->payload_type == payload_type)
167 return codec;
168 }
169
170 if (!create)
171 return NULL;
172
173 /* Not found; codec points after the last entry now. */
174 if ((codec - ac->codec) >= ARRAY_SIZE(ac->codec))
175 return NULL;
176
177 *codec = (struct sdp_audio_codec){
178 .payload_type = payload_type,
179 .rate = 8000,
180 };
181
182 ac->count = (codec - ac->codec) + 1;
183 return codec;
184}
185
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +0100186/* Return a given sdp_msg's codec entry that matches the subtype_name and rate of the given codec, or NULL if no
187 * match is found. Comparison is made by sdp_audio_codec_cmp(cmp_payload_type=false). */
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100188struct 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 +0200189{
190 struct sdp_audio_codec *i;
191 foreach_sdp_audio_codec(i, ac) {
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +0100192 if (!sdp_audio_codec_cmp(i, codec, false, false))
Neels Hofmeyreef45782019-10-21 03:24:04 +0200193 return i;
194 }
195 return NULL;
196}
197
198/* 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 +0100199 * instance, use sdp_audio_codecs_by_descr()).
Neels Hofmeyreef45782019-10-21 03:24:04 +0200200 * Return 0 on success, -ENOENT if codec does not point at the sdp->codec array. */
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100201int sdp_audio_codecs_remove(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 if ((codec < ac->codec)
205 || ((codec - ac->codec) >= OSMO_MIN(ac->count, ARRAY_SIZE(ac->codec))))
206 return -ENOENT;
207
208 /* Move all following entries one up */
209 ac->count--;
210 foreach_sdp_audio_codec(i, ac) {
211 if (i < codec)
212 continue;
213 *i = *(i+1);
214 }
215 return 0;
216}
217
218/* Convert struct sdp_msg to the actual SDP protocol representation */
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100219int sdp_msg_to_sdp_str_buf(char *dst, size_t dst_size, const struct sdp_msg *sdp)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200220{
221 const struct sdp_audio_codec *codec;
222 struct osmo_strbuf sb = { .buf = dst, .len = dst_size };
Neels Hofmeyr6d9b4852022-09-05 16:02:50 +0200223 const char *ip;
224 char ipv;
225
226 if (!sdp) {
227 OSMO_STRBUF_PRINTF(sb, "%s", "");
228 return sb.chars_needed;
229 }
230
231 ip = sdp->rtp.ip[0] ? sdp->rtp.ip : "0.0.0.0";
232 ipv = (osmo_ip_str_type(ip) == AF_INET6) ? '6' : '4';
Neels Hofmeyreef45782019-10-21 03:24:04 +0200233
234 OSMO_STRBUF_PRINTF(sb,
235 "v=0\r\n"
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +0200236 "o=OsmoMSC 0 0 IN IP%c %s\r\n"
Neels Hofmeyreef45782019-10-21 03:24:04 +0200237 "s=GSM Call\r\n"
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +0200238 "c=IN IP%c %s\r\n"
Neels Hofmeyreef45782019-10-21 03:24:04 +0200239 "t=0 0\r\n"
240 "m=audio %d RTP/AVP",
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +0200241 ipv, ip, ipv, ip,
Neels Hofmeyreef45782019-10-21 03:24:04 +0200242 sdp->rtp.port);
243
244 /* Append all payload type numbers to 'm=audio <port> RTP/AVP 3 4 112' line */
245 foreach_sdp_audio_codec(codec, &sdp->audio_codecs)
246 OSMO_STRBUF_PRINTF(sb, " %d", codec->payload_type);
247 OSMO_STRBUF_PRINTF(sb, "\r\n");
248
249 /* Add details for all codecs */
250 foreach_sdp_audio_codec(codec, &sdp->audio_codecs) {
251 if (codec->subtype_name[0]) {
252 OSMO_STRBUF_PRINTF(sb, "a=rtpmap:%d %s/%d\r\n", codec->payload_type, codec->subtype_name,
253 codec->rate > 0? codec->rate : 8000);
254 }
255
256 if (codec->fmtp[0])
257 OSMO_STRBUF_PRINTF(sb, "a=fmtp:%d %s\r\n", codec->payload_type, codec->fmtp);
258 }
259
260 OSMO_STRBUF_PRINTF(sb, "a=ptime:%d\r\n", sdp->ptime > 0? sdp->ptime : 20);
261
262 return sb.chars_needed;
263}
264
265/* Return the first line ending (or the end of the string) at or after the given string position. */
266const char *sdp_msg_line_end(const char *src)
267{
268 const char *line_end = strchr(src, '\r');
269 if (!line_end)
270 line_end = strchr(src, '\n');
271 if (!line_end)
272 line_end = src + strlen(src);
273 return line_end;
274}
275
276/* parse a line like 'a=rtpmap:0 PCMU/8000', 'a=fmtp:112 octet-align=1; mode-set=4', 'a=ptime:20'.
277 * The src should point at the character after 'a=', e.g. at the start of 'rtpmap', 'fmtp', 'ptime'
278 */
279int sdp_parse_attrib(struct sdp_msg *sdp, const char *src)
280{
281 unsigned int payload_type;
282 struct sdp_audio_codec *codec;
283#define A_RTPMAP "rtpmap:"
284#define A_FMTP "fmtp:"
285#define A_PTIME "ptime:"
286#define A_RTCP "rtcp:"
287#define A_SENDRECV "sendrecv"
288#define A_SENDONLY "sendonly"
289#define A_RECVONLY "recvonly"
290
291 if (osmo_str_startswith(src, A_RTPMAP)) {
292 char *audio_name;
293 unsigned int channels = 1;
294 if (sscanf(src, A_RTPMAP "%u", &payload_type) != 1)
295 return -EINVAL;
296
297 audio_name = strchr(src, ' ');
298 if (!audio_name || audio_name >= sdp_msg_line_end(src))
299 return -EINVAL;
300
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100301 codec = sdp_audio_codecs_by_payload_type(&sdp->audio_codecs, payload_type, true);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200302 if (!codec)
303 return -ENOSPC;
304
305 if (sscanf(audio_name, " %31[^/]/%u/%u", codec->subtype_name, &codec->rate, &channels) < 1)
306 return -EINVAL;
307
308 if (channels != 1)
309 return -ENOTSUP;
310 }
311
312 else if (osmo_str_startswith(src, A_FMTP)) {
313 char *fmtp_str;
314 const char *line_end = sdp_msg_line_end(src);
315 if (sscanf(src, A_FMTP "%u", &payload_type) != 1)
316 return -EINVAL;
317
318 fmtp_str = strchr(src, ' ');
319 if (!fmtp_str)
320 return -EINVAL;
321 fmtp_str++;
322 if (fmtp_str >= line_end)
323 return -EINVAL;
324
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100325 codec = sdp_audio_codecs_by_payload_type(&sdp->audio_codecs, payload_type, true);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200326 if (!codec)
327 return -ENOSPC;
328
329 /* (+1 because osmo_strlcpy() interprets it as size including the '\0') */
330 osmo_strlcpy(codec->fmtp, fmtp_str, line_end - fmtp_str + 1);
331 }
332
333 else if (osmo_str_startswith(src, A_PTIME)) {
334 if (sscanf(src, A_PTIME "%u", &sdp->ptime) != 1)
335 return -EINVAL;
336
337 }
338
339 else if (osmo_str_startswith(src, A_RTCP)) {
340 /* TODO? */
341 }
342
343 else if (osmo_str_startswith(src, A_SENDRECV)) {
344 /* TODO? */
345 }
346
347 else if (osmo_str_startswith(src, A_SENDONLY)) {
348 /* TODO? */
349 }
350
351 else if (osmo_str_startswith(src, A_RECVONLY)) {
352 /* TODO? */
353 }
354
355 return 0;
356}
357
358const struct value_string sdp_msg_payload_type_names[] = {
359 { 0, "PCMU" },
360 { 3, "GSM" },
361 { 8, "PCMA" },
362 { 18, "G729" },
363 { 110, "GSM-EFR" },
364 { 111, "GSM-HR-08" },
365 { 112, "AMR" },
366 { 113, "AMR-WB" },
367 {}
368};
369
370/* Return payload type number matching given string ("AMR", "GSM", ...) or negative if not found. */
371int sdp_subtype_name_to_payload_type(const char *subtype_name)
372{
373 return get_string_value(sdp_msg_payload_type_names, subtype_name);
374}
375
376/* Parse a line like 'm=audio 16398 RTP/AVP 0 3 8 96 112', starting after the '=' */
377static int sdp_parse_media_description(struct sdp_msg *sdp, const char *src)
378{
379 unsigned int port;
380 int i;
381 const char *payload_type_str;
382 const char *line_end = sdp_msg_line_end(src);
383 if (sscanf(src, "audio %u RTP/AVP", &port) < 1)
384 return -ENOTSUP;
385
Vadim Yanitskiy40b11c92020-02-09 04:04:54 +0700386 if (port > 0xffff)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200387 return -EINVAL;
388
389 sdp->rtp.port = port;
390
391 /* skip "audio 12345 RTP/AVP ", i.e. 3 spaces on */
392 payload_type_str = src;
393 for (i = 0; i < 3; i++) {
394 payload_type_str = strchr(payload_type_str, ' ');
395 if (!payload_type_str)
396 return -EINVAL;
397 while (*payload_type_str == ' ')
398 payload_type_str++;
399 if (payload_type_str >= line_end)
400 return -EINVAL;
401 }
402
403 /* Parse listing of payload type numbers after "RTP/AVP" */
404 while (payload_type_str < line_end) {
405 unsigned int payload_type;
406 struct sdp_audio_codec *codec;
407 const char *subtype_name;
408 if (sscanf(payload_type_str, "%u", &payload_type) < 1)
409 return -EINVAL;
410
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100411 codec = sdp_audio_codecs_by_payload_type(&sdp->audio_codecs, payload_type, true);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200412 if (!codec)
413 return -ENOSPC;
414
415 /* Fill in subtype name for fixed payload types */
416 subtype_name = get_value_string_or_null(sdp_msg_payload_type_names, codec->payload_type);
417 if (subtype_name)
418 OSMO_STRLCPY_ARRAY(codec->subtype_name, subtype_name);
419
420 payload_type_str = strchr(payload_type_str, ' ');
421 if (!payload_type_str)
422 payload_type_str = line_end;
423 while (*payload_type_str == ' ')
424 payload_type_str++;
425 }
426
427 return 0;
428}
429
430/* parse a line like 'c=IN IP4 192.168.11.151' starting after the '=' */
431static int sdp_parse_connection_info(struct sdp_msg *sdp, const char *src)
432{
433 char ipv[10];
434 char addr_str[INET6_ADDRSTRLEN];
435 if (sscanf(src, "IN %s %s", ipv, addr_str) < 2)
436 return -EINVAL;
437
438 /* supporting only IPv4 */
439 if (strcmp(ipv, "IP4"))
440 return -ENOTSUP;
441
442 osmo_sockaddr_str_from_str(&sdp->rtp, addr_str, sdp->rtp.port);
443 return 0;
444}
445
446/* Parse SDP string into struct sdp_msg. Return 0 on success, negative on error. */
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100447int sdp_msg_from_sdp_str(struct sdp_msg *sdp, const char *src)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200448{
449 const char *pos;
450 *sdp = (struct sdp_msg){};
451
452 for (pos = src; pos && *pos; pos++) {
453 char attrib;
454 int rc = 0;
455
456 if (*pos == '\r' || *pos == '\n')
457 continue;
458
459 /* Expecting only lines starting with 'X='. Not being too strict about it is probably alright. */
460 if (pos[1] != '=')
461 goto next_line;
462
463 attrib = *pos;
464 pos += 2;
465 switch (attrib) {
466 /* a=... */
467 case 'a':
468 rc = sdp_parse_attrib(sdp, pos);
469 break;
470 case 'm':
471 rc = sdp_parse_media_description(sdp, pos);
472 break;
473 case 'c':
474 rc = sdp_parse_connection_info(sdp, pos);
475 break;
476 default:
477 /* ignore any other parameters */
478 break;
479 }
480
481 if (rc) {
482 size_t line_len;
483 const char *line_end = sdp_msg_line_end(pos);
484 pos -= 2;
485 line_len = line_end - pos;
486 switch (rc) {
487 case -EINVAL:
488 LOGP(DMNCC, LOGL_ERROR,
489 "Failed to parse SDP: invalid line: %s\n", osmo_quote_str(pos, line_len));
490 break;
491 case -ENOSPC:
492 LOGP(DMNCC, LOGL_ERROR,
493 "Failed to parse SDP: no more space for: %s\n", osmo_quote_str(pos, line_len));
494 break;
495 case -ENOTSUP:
496 LOGP(DMNCC, LOGL_ERROR,
497 "Failed to parse SDP: not supported: %s\n", osmo_quote_str(pos, line_len));
498 break;
499 default:
500 LOGP(DMNCC, LOGL_ERROR,
501 "Failed to parse SDP: %s\n", osmo_quote_str(pos, line_len));
502 break;
503 }
504 return rc;
505 }
506next_line:
507 pos = strstr(pos, "\r\n");
508 if (!pos)
509 break;
510 }
511
512 return 0;
513}
514
515/* Leave only those codecs in 'ac_dest' that are also present in 'ac_other'.
Neels Hofmeyrc9e0ca32022-01-13 18:28:44 +0100516 * The matching is made by sdp_audio_codec_cmp(cmp_payload_type=false), i.e. payload_type numbers are not compared and
517 * fmtp parameters are compared 1:1 as plain strings.
Neels Hofmeyreef45782019-10-21 03:24:04 +0200518 * If translate_payload_type_numbers has an effect if ac_dest and ac_other have mismatching payload_type numbers for the
519 * same SDP codec descriptions. If translate_payload_type_numbers is true, take the payload_type numbers from ac_other.
520 * If false, keep payload_type numbers in ac_dest unchanged. */
521void sdp_audio_codecs_intersection(struct sdp_audio_codecs *ac_dest, const struct sdp_audio_codecs *ac_other,
522 bool translate_payload_type_numbers)
523{
524 int i;
525 for (i = 0; i < ac_dest->count; i++) {
526 struct sdp_audio_codec *codec = &ac_dest->codec[i];
527 struct sdp_audio_codec *other;
528 OSMO_ASSERT(i < ARRAY_SIZE(ac_dest->codec));
529
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100530 other = sdp_audio_codecs_by_descr((struct sdp_audio_codecs *)ac_other, codec);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200531
532 if (!other) {
Neels Hofmeyrd20dd222022-01-14 00:38:42 +0100533 OSMO_ASSERT(sdp_audio_codecs_remove(ac_dest, codec) == 0);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200534 i--;
535 continue;
536 }
537
538 /* Doing payload_type number translation of part of the intersection because it makes the algorithm
539 * simpler: we already know ac_dest is a subset of ac_other, and there is no need to resolve payload
540 * type number conflicts. */
541 if (translate_payload_type_numbers)
542 codec->payload_type = other->payload_type;
543 }
544}
545
546/* Make sure the given codec is listed as the first codec. 'codec' must be an actual codec entry of the given audio
547 * codecs list. */
548void sdp_audio_codecs_select(struct sdp_audio_codecs *ac, struct sdp_audio_codec *codec)
549{
550 struct sdp_audio_codec tmp;
551 struct sdp_audio_codec *pos;
552 OSMO_ASSERT((codec >= ac->codec)
553 && ((codec - ac->codec) < OSMO_MIN(ac->count, ARRAY_SIZE(ac->codec))));
554
555 /* Already the first? */
556 if (codec == ac->codec)
557 return;
558
559 tmp = *codec;
560 for (pos = codec - 1; pos >= ac->codec; pos--)
561 pos[1] = pos[0];
562
563 ac->codec[0] = tmp;
564 return;
565}
566
567/* Short single-line representation of an SDP audio codec, convenient for logging.
568 * Like "AMR/8000:octet-align=1#122" */
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100569int sdp_audio_codec_to_str_buf(char *buf, size_t buflen, const struct sdp_audio_codec *codec)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200570{
571 struct osmo_strbuf sb = { .buf = buf, .len = buflen };
572 OSMO_STRBUF_PRINTF(sb, "%s", codec->subtype_name);
Neels Hofmeyr99ab7c52022-01-13 19:04:59 +0100573 if (codec->rate != 8000)
574 OSMO_STRBUF_PRINTF(sb, "/%u", codec->rate);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200575 if (codec->fmtp[0])
576 OSMO_STRBUF_PRINTF(sb, ":%s", codec->fmtp);
Neels Hofmeyr99ab7c52022-01-13 19:04:59 +0100577 OSMO_STRBUF_PRINTF(sb, "#%d", codec->payload_type);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200578 return sb.chars_needed;
579}
580
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100581char *sdp_audio_codec_to_str_c(void *ctx, const struct sdp_audio_codec *codec)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200582{
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100583 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 +0200584}
585
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100586const char *sdp_audio_codec_to_str(const struct sdp_audio_codec *codec)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200587{
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100588 return sdp_audio_codec_to_str_c(OTC_SELECT, codec);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200589}
590
591/* Short single-line representation of a list of SDP audio codecs, convenient for logging */
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100592int sdp_audio_codecs_to_str_buf(char *buf, size_t buflen, const struct sdp_audio_codecs *ac)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200593{
594 struct osmo_strbuf sb = { .buf = buf, .len = buflen };
595 const struct sdp_audio_codec *codec;
596 if (!ac->count)
597 OSMO_STRBUF_PRINTF(sb, "(no-codecs)");
598 foreach_sdp_audio_codec(codec, ac) {
599 bool first = (codec == ac->codec);
600 if (!first)
601 OSMO_STRBUF_PRINTF(sb, ",");
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100602 OSMO_STRBUF_APPEND(sb, sdp_audio_codec_to_str_buf, codec);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200603 }
604 return sb.chars_needed;
605}
606
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100607char *sdp_audio_codecs_to_str_c(void *ctx, const struct sdp_audio_codecs *ac)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200608{
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100609 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 +0200610}
611
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100612const char *sdp_audio_codecs_to_str(const struct sdp_audio_codecs *ac)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200613{
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100614 return sdp_audio_codecs_to_str_c(OTC_SELECT, ac);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200615}
616
617/* Short single-line representation of an SDP message, convenient for logging */
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100618int sdp_msg_to_str_buf(char *buf, size_t buflen, const struct sdp_msg *sdp)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200619{
620 struct osmo_strbuf sb = { .buf = buf, .len = buflen };
621 if (!sdp) {
622 OSMO_STRBUF_PRINTF(sb, "NULL");
623 return sb.chars_needed;
624 }
625
626 OSMO_STRBUF_PRINTF(sb, OSMO_SOCKADDR_STR_FMT, OSMO_SOCKADDR_STR_FMT_ARGS(&sdp->rtp));
627 OSMO_STRBUF_PRINTF(sb, "{");
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100628 OSMO_STRBUF_APPEND(sb, sdp_audio_codecs_to_str_buf, &sdp->audio_codecs);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200629 OSMO_STRBUF_PRINTF(sb, "}");
630 return sb.chars_needed;
631}
632
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100633char *sdp_msg_to_str_c(void *ctx, const struct sdp_msg *sdp)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200634{
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100635 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 +0200636}
637
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100638const char *sdp_msg_to_str(const struct sdp_msg *sdp)
Neels Hofmeyreef45782019-10-21 03:24:04 +0200639{
Neels Hofmeyr9a515e52022-01-13 20:40:12 +0100640 return sdp_msg_to_str_c(OTC_SELECT, sdp);
Neels Hofmeyreef45782019-10-21 03:24:04 +0200641}