blob: 2f1df7bd51c32bf52bab580300619f33e31cd8c0 [file] [log] [blame]
Philipp Maierbc0346e2018-06-07 09:52:16 +02001/*
2 * (C) 2009-2015 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2009-2014 by On-Waves
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
Philipp Maier993ea6b2020-08-04 18:26:50 +020020#include <osmocom/mgcp/mgcp.h>
21#include <osmocom/mgcp/osmux.h>
22#include <osmocom/mgcp/mgcp_conn.h>
23#include <osmocom/mgcp/mgcp_protocol.h>
Philipp Maierbc0346e2018-06-07 09:52:16 +020024#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maierc66ab2c2020-06-02 20:55:34 +020025#include <osmocom/mgcp/mgcp_trunk.h>
Philipp Maier993ea6b2020-08-04 18:26:50 +020026#include <osmocom/mgcp/mgcp_codec.h>
Philipp Maierbc0346e2018-06-07 09:52:16 +020027#include <errno.h>
28
29/* Helper function to dump codec information of a specified codec to a printable
30 * string, used by dump_codec_summary() */
31static char *dump_codec(struct mgcp_rtp_codec *codec)
32{
33 static char str[256];
34 char *pt_str;
35
36 if (codec->payload_type > 76)
37 pt_str = "DYNAMIC";
38 else if (codec->payload_type > 72)
39 pt_str = "RESERVED <!>";
40 else if (codec->payload_type != PTYPE_UNDEFINED)
41 pt_str = codec->subtype_name;
42 else
43 pt_str = "INVALID <!>";
44
45 snprintf(str, sizeof(str), "(pt:%i=%s, audio:%s subt=%s, rate=%u, ch=%i, t=%u/%u)", codec->payload_type, pt_str,
46 codec->audio_name, codec->subtype_name, codec->rate, codec->channels, codec->frame_duration_num,
47 codec->frame_duration_den);
48 return str;
49}
50
51/*! Dump a summary of all negotiated codecs to debug log
52 * \param[in] conn related rtp-connection. */
53void mgcp_codec_summary(struct mgcp_conn_rtp *conn)
54{
55 struct mgcp_rtp_end *rtp;
56 unsigned int i;
57 struct mgcp_rtp_codec *codec;
58 struct mgcp_endpoint *endp;
59
60 rtp = &conn->end;
61 endp = conn->conn->endp;
62
63 if (rtp->codecs_assigned == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +020064 LOGPENDP(endp, DLMGCP, LOGL_ERROR, "conn:%s no codecs available\n",
65 mgcp_conn_dump(conn->conn));
Philipp Maierbc0346e2018-06-07 09:52:16 +020066 return;
67 }
68
69 /* Store parsed codec information */
70 for (i = 0; i < rtp->codecs_assigned; i++) {
71 codec = &rtp->codecs[i];
72
Pau Espin Pedrol3239f622019-04-24 18:47:46 +020073 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "conn:%s codecs[%u]:%s",
74 mgcp_conn_dump(conn->conn), i, dump_codec(codec));
Philipp Maierbc0346e2018-06-07 09:52:16 +020075
76 if (codec == rtp->codec)
77 LOGPC(DLMGCP, LOGL_DEBUG, " [selected]");
78
79 LOGPC(DLMGCP, LOGL_DEBUG, "\n");
80 }
81}
82
83/* Initalize or reset codec information with default data. */
Neels Hofmeyr667fa592019-08-08 21:59:01 +020084static void codec_init(struct mgcp_rtp_codec *codec)
85{
86 *codec = (struct mgcp_rtp_codec){
87 .payload_type = -1,
88 .frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM,
89 .frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN,
90 .rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE,
91 .channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS,
Eric2ebcf5c2021-08-09 23:26:48 +020092 .subtype_name = "",
93 .audio_name = "",
Neels Hofmeyr667fa592019-08-08 21:59:01 +020094 };
95}
96
97static void codec_free(struct mgcp_rtp_codec *codec)
Philipp Maierbc0346e2018-06-07 09:52:16 +020098{
Neels Hofmeyr667fa592019-08-08 21:59:01 +020099 *codec = (struct mgcp_rtp_codec){};
Philipp Maierbc0346e2018-06-07 09:52:16 +0200100}
101
102/*! Initalize or reset codec information with default data.
103 * \param[out] conn related rtp-connection. */
104void mgcp_codec_reset_all(struct mgcp_conn_rtp *conn)
105{
Neels Hofmeyrce64f182019-08-08 22:07:31 +0200106 int i;
107 for (i = 0; i < conn->end.codecs_assigned; i++)
108 codec_free(&conn->end.codecs[i]);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200109 conn->end.codecs_assigned = 0;
110 conn->end.codec = NULL;
111}
112
Neels Hofmeyr667fa592019-08-08 21:59:01 +0200113/*! Add codec configuration depending on payload type and/or codec name. This
114 * function uses the input parameters to extrapolate the full codec information.
115 * \param[out] codec configuration (caller provided memory).
116 * \param[out] conn related rtp-connection.
117 * \param[in] payload_type codec type id (e.g. 3 for GSM, -1 when undefined).
118 * \param[in] audio_name audio codec name, in uppercase (e.g. "GSM/8000/1").
119 * \param[in] param optional codec parameters (set to NULL when unused).
120 * \returns 0 on success, -EINVAL on failure. */
121int mgcp_codec_add(struct mgcp_conn_rtp *conn, int payload_type, const char *audio_name, const struct mgcp_codec_param *param)
Philipp Maierbc0346e2018-06-07 09:52:16 +0200122{
123 int rate;
124 int channels;
Neels Hofmeyr667fa592019-08-08 21:59:01 +0200125 struct mgcp_rtp_codec *codec;
126 unsigned int pt_offset = conn->end.codecs_assigned;
Neels Hofmeyr667fa592019-08-08 21:59:01 +0200127
128 /* The amount of codecs we can store is limited, make sure we do not
129 * overrun this limit. */
130 if (conn->end.codecs_assigned >= MGCP_MAX_CODECS)
131 return -EINVAL;
132
133 /* First unused entry */
134 codec = &conn->end.codecs[conn->end.codecs_assigned];
Philipp Maierbc0346e2018-06-07 09:52:16 +0200135
136 /* Initalize the codec struct with some default data to begin with */
137 codec_init(codec);
138
139 if (payload_type != PTYPE_UNDEFINED) {
140 /* Make sure we do not get any reserved or undefined type numbers */
141 /* See also: https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200142 if ((payload_type == 1 || payload_type == 2 || payload_type == 19)
143 || (payload_type >= 72 && payload_type <= 76)
144 || (payload_type >= 127)) {
145 LOGP(DLMGCP, LOGL_ERROR, "Cannot add codec, payload type number %d is reserved\n",
146 payload_type);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200147 goto error;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200148 }
Philipp Maierbc0346e2018-06-07 09:52:16 +0200149
150 codec->payload_type = payload_type;
151 }
152
153 /* When no audio name is given, we are forced to use the payload
154 * type to generate the audio name. This is only possible for
155 * non dynamic payload types, which are statically defined */
156 if (!audio_name) {
157 switch (payload_type) {
158 case 0:
Eric2ebcf5c2021-08-09 23:26:48 +0200159 strcpy(codec->audio_name, "PCMU/8000/1");
Philipp Maierbc0346e2018-06-07 09:52:16 +0200160 break;
161 case 3:
Eric2ebcf5c2021-08-09 23:26:48 +0200162 strcpy(codec->audio_name, "GSM/8000/1");
Philipp Maierbc0346e2018-06-07 09:52:16 +0200163 break;
164 case 8:
Eric2ebcf5c2021-08-09 23:26:48 +0200165 strcpy(codec->audio_name, "PCMA/8000/1");
Philipp Maierbc0346e2018-06-07 09:52:16 +0200166 break;
167 case 18:
Eric2ebcf5c2021-08-09 23:26:48 +0200168 strcpy(codec->audio_name, "G729/8000/1");
Philipp Maierbc0346e2018-06-07 09:52:16 +0200169 break;
170 default:
171 /* The given payload type is not known to us, or it
172 * it is a dynamic payload type for which we do not
173 * know the audio name. We must give up here */
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200174 LOGP(DLMGCP, LOGL_ERROR, "No audio codec name given, and payload type %d unknown\n",
175 payload_type);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200176 goto error;
177 }
Eric2ebcf5c2021-08-09 23:26:48 +0200178 } else {
Vadim Yanitskiy0c045002021-10-21 00:07:00 +0300179 OSMO_STRLCPY_ARRAY(codec->audio_name, audio_name);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200180 }
181
182 /* Now we extract the codec subtype name, rate and channels. The latter
183 * two are optional. If they are not present we use the safe defaults
184 * above. */
Eric2ebcf5c2021-08-09 23:26:48 +0200185 if (strlen(codec->audio_name) >= sizeof(codec->subtype_name)) {
186 LOGP(DLMGCP, LOGL_ERROR, "Audio codec too long: %s\n", osmo_quote_str(codec->audio_name, -1));
Philipp Maierbc0346e2018-06-07 09:52:16 +0200187 goto error;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200188 }
Philipp Maierbc0346e2018-06-07 09:52:16 +0200189 channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS;
190 rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE;
Eric2ebcf5c2021-08-09 23:26:48 +0200191 if (sscanf(codec->audio_name, "%63[^/]/%d/%d", codec->subtype_name, &rate, &channels) < 1) {
192 LOGP(DLMGCP, LOGL_ERROR, "Invalid audio codec: %s\n", osmo_quote_str(codec->audio_name, -1));
Philipp Maierbc0346e2018-06-07 09:52:16 +0200193 goto error;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200194 }
Philipp Maierbc0346e2018-06-07 09:52:16 +0200195
196 /* Note: We only accept configurations with one audio channel! */
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200197 if (channels != 1) {
198 LOGP(DLMGCP, LOGL_ERROR, "Cannot handle audio codec with more than one channel: %s\n",
Eric2ebcf5c2021-08-09 23:26:48 +0200199 osmo_quote_str(codec->audio_name, -1));
Philipp Maierbc0346e2018-06-07 09:52:16 +0200200 goto error;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200201 }
Philipp Maierbc0346e2018-06-07 09:52:16 +0200202
203 codec->rate = rate;
204 codec->channels = channels;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200205 codec->payload_type = payload_type;
206
Eric2ebcf5c2021-08-09 23:26:48 +0200207 if (!strcmp(codec->subtype_name, "G729")) {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200208 codec->frame_duration_num = 10;
209 codec->frame_duration_den = 1000;
210 } else {
211 codec->frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM;
212 codec->frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN;
213 }
214
215 /* Derive the payload type if it is unknown */
216 if (codec->payload_type == PTYPE_UNDEFINED) {
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200217 /* TODO: This is semi dead code, see OS#4150 */
Philipp Maierbc0346e2018-06-07 09:52:16 +0200218
219 /* For the known codecs from the static range we restore
220 * the IANA or 3GPP assigned payload type number */
221 if (codec->rate == 8000 && codec->channels == 1) {
222 /* See also: https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
223 if (!strcmp(codec->subtype_name, "GSM"))
224 codec->payload_type = 3;
225 else if (!strcmp(codec->subtype_name, "PCMA"))
226 codec->payload_type = 8;
227 else if (!strcmp(codec->subtype_name, "PCMU"))
228 codec->payload_type = 0;
229 else if (!strcmp(codec->subtype_name, "G729"))
230 codec->payload_type = 18;
231
232 /* See also: 3GPP TS 48.103, chapter 5.4.2.2 RTP Payload
233 * Note: These are not fixed payload types as the IANA
234 * defined once, they still remain dymanic payload
235 * types, but with a payload type number preference. */
236 else if (!strcmp(codec->subtype_name, "GSM-EFR"))
237 codec->payload_type = 110;
238 else if (!strcmp(codec->subtype_name, "GSM-HR-08"))
239 codec->payload_type = 111;
240 else if (!strcmp(codec->subtype_name, "AMR"))
241 codec->payload_type = 112;
242 else if (!strcmp(codec->subtype_name, "AMR-WB"))
243 codec->payload_type = 113;
244 }
245
246 /* If we could not determine a payload type we assume that
247 * we are dealing with a codec from the dynamic range. We
248 * choose a fixed identifier from 96-109. (Note: normally,
249 * the dynamic payload type rante is from 96-127, but from
250 * 110 onwards 3gpp defines prefered codec types, which are
251 * also fixed, see above) */
252 if (codec->payload_type < 0) {
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200253 /* FIXME: pt_offset is completely unrelated and useless here, any of those numbers may already
254 * have been added to the codecs. Instead, there should be an iterator checking for an actually
255 * unused dynamic payload type number. */
Philipp Maierbc0346e2018-06-07 09:52:16 +0200256 codec->payload_type = 96 + pt_offset;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200257 if (codec->payload_type > 109) {
258 LOGP(DLMGCP, LOGL_ERROR, "Ran out of payload type numbers to assign dynamically\n");
Philipp Maierbc0346e2018-06-07 09:52:16 +0200259 goto error;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200260 }
Philipp Maierbc0346e2018-06-07 09:52:16 +0200261 }
262 }
263
Philipp Maier228e5912019-03-05 13:56:59 +0100264 /* Copy over optional codec parameters */
265 if (param) {
266 codec->param = *param;
267 codec->param_present = true;
268 } else
269 codec->param_present = false;
270
Neels Hofmeyr667fa592019-08-08 21:59:01 +0200271 conn->end.codecs_assigned++;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200272 return 0;
273error:
274 /* Make sure we leave a clean codec entry on error. */
Neels Hofmeyr667fa592019-08-08 21:59:01 +0200275 codec_free(codec);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200276 return -EINVAL;
277}
278
Neels Hofmeyr16b637b2019-08-08 22:47:10 +0200279/* Return true if octet-aligned is set in the given codec. Default to octet-aligned=0, i.e. bandwidth-efficient mode.
280 * See RFC4867 "RTP Payload Format for AMR and AMR-WB" sections "8.1. AMR Media Type Registration" and "8.2. AMR-WB
281 * Media Type Registration":
282 *
283 * octet-align: Permissible values are 0 and 1. If 1, octet-aligned
284 * operation SHALL be used. If 0 or if not present,
285 * bandwidth-efficient operation is employed.
286 *
287 * https://tools.ietf.org/html/rfc4867
288 */
Pau Espin Pedrol2799ff92022-01-03 15:24:59 +0100289bool mgcp_codec_amr_is_octet_aligned(const struct mgcp_rtp_codec *codec)
Neels Hofmeyr16b637b2019-08-08 22:47:10 +0200290{
291 if (!codec->param_present)
292 return false;
293 if (!codec->param.amr_octet_aligned_present)
294 return false;
295 return codec->param.amr_octet_aligned;
296}
297
Philipp Maier621e8662023-03-22 16:53:30 +0100298/* Compare two codecs, all parameters must match up */
Neels Hofmeyr782d6072019-08-09 00:51:21 +0200299static bool codecs_same(struct mgcp_rtp_codec *codec_a, struct mgcp_rtp_codec *codec_b)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200300{
Philipp Maier621e8662023-03-22 16:53:30 +0100301 /* All codec properties must match up, except the payload type number. Even though standardisd payload numbers
302 * exist for certain situations, the call agent may still assign them freely. Hence we must not insist on equal
303 * payload type numbers. Also the audio_name is not checked since it is already parsed into subtype_name, rate,
304 * and channels, which are checked. */
305 if (strcmp(codec_a->subtype_name, codec_b->subtype_name))
306 return false;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200307 if (codec_a->rate != codec_b->rate)
308 return false;
309 if (codec_a->channels != codec_b->channels)
310 return false;
311 if (codec_a->frame_duration_num != codec_b->frame_duration_num)
312 return false;
313 if (codec_a->frame_duration_den != codec_b->frame_duration_den)
314 return false;
Philipp Maier621e8662023-03-22 16:53:30 +0100315
316 /* AMR payload may be formatted in two different payload formats, it is still the same codec but since the
317 * formatting of the payload is different, conversation is required, so we must treat it as a different
318 * codec here. */
319 if (strcmp(codec_a->subtype_name, "AMR") == 0) {
Philipp Maierec967d72023-03-22 16:20:37 +0100320 if (mgcp_codec_amr_is_octet_aligned(codec_a) != mgcp_codec_amr_is_octet_aligned(codec_b))
321 return false;
322 }
Philipp Maier6931f9a2018-07-26 09:29:31 +0200323
324 return true;
325}
326
Philipp Maier621e8662023-03-22 16:53:30 +0100327/* Compare two codecs, all parameters must match up, except parameters related to payload formatting (not checked). */
328static bool codecs_convertible(struct mgcp_rtp_codec *codec_a, struct mgcp_rtp_codec *codec_b)
329{
330 /* OsmoMGW currently has no ability to transcode from one codec to another. However OsmoMGW is still able to
331 * translate between different payload formats as long as the encoded voice data itself does not change.
332 * Therefore we must insist on equal codecs but still allow different payload formatting. */
Philipp Maier2bfa7082023-05-23 13:58:38 +0200333
334 /* In 3G IuUP, AMR may be encapsulated in IuFP, this means even though the codec name and negotiated rate is
335 * different, the formatting can still be converted by OsmoMGW. Therefore we won't insist on equal
336 * subtype_name and rate if we detect IuFP and AMR is used on the same tandem. */
337 if (strcmp(codec_a->subtype_name, "AMR") == 0 && strcmp(codec_b->subtype_name, "VND.3GPP.IUFP") == 0)
338 goto iufp;
339 if (strcmp(codec_a->subtype_name, "VND.3GPP.IUFP") == 0 && strcmp(codec_b->subtype_name, "AMR") == 0)
340 goto iufp;
341
Philipp Maier621e8662023-03-22 16:53:30 +0100342 if (strcmp(codec_a->subtype_name, codec_b->subtype_name))
343 return false;
344 if (codec_a->rate != codec_b->rate)
345 return false;
Philipp Maier2bfa7082023-05-23 13:58:38 +0200346iufp:
Philipp Maier621e8662023-03-22 16:53:30 +0100347 if (codec_a->channels != codec_b->channels)
348 return false;
349 if (codec_a->frame_duration_num != codec_b->frame_duration_num)
350 return false;
351 if (codec_a->frame_duration_den != codec_b->frame_duration_den)
352 return false;
353
354 return true;
355}
356
Philipp Maier4c4d2272023-04-26 15:45:17 +0200357struct mgcp_rtp_codec *mgcp_codec_find_same(struct mgcp_conn_rtp *conn, struct mgcp_rtp_codec *codec)
358{
359 struct mgcp_rtp_end *rtp_end;
360 unsigned int i;
361 unsigned int codecs_assigned;
362
363 rtp_end = &conn->end;
364
365 /* Use the codec information from the source and try to find the equivalent of it on the destination side. In
366 * the first run we will look for an exact match. */
367 codecs_assigned = rtp_end->codecs_assigned;
368 OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
369 for (i = 0; i < codecs_assigned; i++) {
370 if (codecs_same(codec, &rtp_end->codecs[i])) {
371 return &rtp_end->codecs[i];
372 break;
373 }
374 }
375
376 return NULL;
377}
378
379/* For a given codec, find a convertible codec in the given connection. */
380static struct mgcp_rtp_codec *codec_find_convertible(struct mgcp_conn_rtp *conn, struct mgcp_rtp_codec *codec)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200381{
Philipp Maier9dd80ca2023-03-27 15:45:36 +0200382 struct mgcp_rtp_end *rtp_end;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200383 unsigned int i;
384 unsigned int codecs_assigned;
Philipp Maier9dd80ca2023-03-27 15:45:36 +0200385 struct mgcp_rtp_codec *codec_convertible = NULL;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200386
Philipp Maier9dd80ca2023-03-27 15:45:36 +0200387 rtp_end = &conn->end;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200388
Philipp Maier621e8662023-03-22 16:53:30 +0100389 /* Use the codec information from the source and try to find the equivalent of it on the destination side. In
390 * the first run we will look for an exact match. */
Philipp Maier4c4d2272023-04-26 15:45:17 +0200391 codec_convertible = mgcp_codec_find_same(conn, codec);
392 if (codec_convertible)
393 return codec_convertible;
Philipp Maier621e8662023-03-22 16:53:30 +0100394
395 /* In case we weren't able to find an exact match, we will try to find a match that is the same codec, but the
396 * payload format may be different. This alternative will require a frame format conversion (i.e. AMR bwe->oe) */
Philipp Maier4c4d2272023-04-26 15:45:17 +0200397 codecs_assigned = rtp_end->codecs_assigned;
398 OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
399 for (i = 0; i < codecs_assigned; i++) {
400 if (codecs_convertible(codec, &rtp_end->codecs[i])) {
401 codec_convertible = &rtp_end->codecs[i];
402 break;
Philipp Maier621e8662023-03-22 16:53:30 +0100403 }
404 }
405
Philipp Maier9dd80ca2023-03-27 15:45:36 +0200406 return codec_convertible;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200407}
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200408
Philipp Maier4c4d2272023-04-26 15:45:17 +0200409/*! Decide for one suitable codec on both of the given connections. In case a destination connection is not available,
410 * a tentative decision is made.
411 * \param[inout] conn_src related rtp-connection.
412 * \param[inout] conn_dst related destination rtp-connection (NULL if not present).
Philipp Maier0e8310f2023-04-26 15:23:05 +0200413 * \returns 0 on success, -EINVAL on failure. */
Philipp Maier4c4d2272023-04-26 15:45:17 +0200414int mgcp_codec_decide(struct mgcp_conn_rtp *conn_src, struct mgcp_conn_rtp *conn_dst)
Philipp Maier0e8310f2023-04-26 15:23:05 +0200415{
Philipp Maier0e8310f2023-04-26 15:23:05 +0200416 unsigned int i;
Philipp Maier0e8310f2023-04-26 15:23:05 +0200417
Philipp Maier4c4d2272023-04-26 15:45:17 +0200418 /* In case no destination connection is available (yet), or in case the destination connection exists but has
419 * no codecs assigned, we are forced to make a simple tentative decision:
420 * We just use the first codec of the source connection (conn_src) */
421 OSMO_ASSERT(conn_src->end.codecs_assigned <= MGCP_MAX_CODECS);
422 if (!conn_dst || conn_dst->end.codecs_assigned == 0) {
423 if (conn_src->end.codecs_assigned >= 1) {
424 conn_src->end.codec = &conn_src->end.codecs[0];
425 return 0;
426 } else
427 return -EINVAL;
Philipp Maier0e8310f2023-04-26 15:23:05 +0200428 }
429
Philipp Maier4c4d2272023-04-26 15:45:17 +0200430 /* Compare all codecs of the source connection (conn_src) to the codecs of the destination connection (conn_dst). In case
431 * of a match set this codec on both connections. This would be an ideal selection since no codec conversion would be
432 * required. */
433 for (i = 0; i < conn_src->end.codecs_assigned; i++) {
Neels Hofmeyrfd57bd52023-12-10 06:58:56 +0100434 struct mgcp_rtp_codec *codec_conn_src = &conn_src->end.codecs[i];
435 struct mgcp_rtp_codec *codec_conn_dst = mgcp_codec_find_same(conn_dst, codec_conn_src);
Philipp Maier4c4d2272023-04-26 15:45:17 +0200436 if (codec_conn_dst) {
437 /* We found the a codec that is exactly the same (same codec, same payload format etc.) on both
438 * sides. We now set this codec on both connections. */
439 conn_dst->end.codec = codec_conn_dst;
Neels Hofmeyrfd57bd52023-12-10 06:58:56 +0100440 conn_src->end.codec = codec_conn_src;
Philipp Maier4c4d2272023-04-26 15:45:17 +0200441 return 0;
442 }
443 }
Philipp Maier0e8310f2023-04-26 15:23:05 +0200444
Philipp Maier4c4d2272023-04-26 15:45:17 +0200445 /* In case we could not find a codec that is exactly the same, let's at least try to find a codec that we are able
446 * to convert. */
447 for (i = 0; i < conn_src->end.codecs_assigned; i++) {
Neels Hofmeyrfd57bd52023-12-10 06:58:56 +0100448 struct mgcp_rtp_codec *codec_conn_src = &conn_src->end.codecs[i];
449 struct mgcp_rtp_codec *codec_conn_dst = codec_find_convertible(conn_dst, codec_conn_src);
Philipp Maier4c4d2272023-04-26 15:45:17 +0200450 if (codec_conn_dst) {
Neels Hofmeyrfd57bd52023-12-10 06:58:56 +0100451 /* We found the a codec that we can convert to. Set each side to its codec. */
Philipp Maier4c4d2272023-04-26 15:45:17 +0200452 conn_dst->end.codec = codec_conn_dst;
Neels Hofmeyrfd57bd52023-12-10 06:58:56 +0100453 conn_src->end.codec = codec_conn_src;
Philipp Maier4c4d2272023-04-26 15:45:17 +0200454 return 0;
455 }
Philipp Maier0e8310f2023-04-26 15:23:05 +0200456 }
457
Neels Hofmeyrc4b90352023-12-10 07:02:37 +0100458 if (conn_dst->end.codecs_assigned)
459 conn_dst->end.codec = &conn_dst->end.codecs[0];
460 else
461 return -EINVAL;
462
463 if (conn_src->end.codecs_assigned)
464 conn_src->end.codec = &conn_src->end.codecs[0];
465 else
466 return -EINVAL;
467
468 return 0;
Philipp Maier0e8310f2023-04-26 15:23:05 +0200469}
470
471/* Check if the codec has a specific AMR mode (octet-aligned or bandwith-efficient) set. */
472bool mgcp_codec_amr_align_mode_is_indicated(const struct mgcp_rtp_codec *codec)
473{
474 if (codec->param_present == false)
475 return false;
476 if (!codec->param.amr_octet_aligned_present)
477 return false;
478 if (strcmp(codec->subtype_name, "AMR") != 0)
479 return false;
480 return true;
481}
482
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200483/* Find the payload type number configured for a specific codec by SDP.
484 * For example, IuUP gets assigned a payload type number, and the endpoint needs to translate that to the number
485 * assigned to "AMR" on the other conn (by a=rtpmap:N).
486 * \param conn The side of an endpoint to get the payload type number for (to translate the payload type number to).
487 * \param subtype_name SDP codec name without parameters (e.g. "AMR").
488 * \param match_nr Index for the match found, first being match_nr == 0. Iterate all matches by calling multiple times
489 * with incrementing match_nr.
Philipp Maier3699ef02023-03-27 15:54:22 +0200490 * \return codec definition for that conn matching the subtype_name, or NULL if no such match_nr is found. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200491const struct mgcp_rtp_codec *mgcp_codec_pt_find_by_subtype_name(struct mgcp_conn_rtp *conn,
492 const char *subtype_name, unsigned int match_nr)
493{
494 int i;
495 for (i = 0; i < conn->end.codecs_assigned; i++) {
496 if (!strcmp(conn->end.codecs[i].subtype_name, subtype_name)) {
497 if (match_nr) {
498 match_nr--;
499 continue;
500 }
501 return &conn->end.codecs[i];
502 }
503 }
504 return NULL;
505}
Philipp Maier9dd80ca2023-03-27 15:45:36 +0200506
507/*! Lookup a codec that is assigned to a connection by its payload type number.
508 * \param[in] conn related rtp-connection.
509 * \param[in] payload_type number of the codec to look up.
510 * \returns pointer to codec struct on success, NULL on failure. */
511struct mgcp_rtp_codec *mgcp_codec_from_pt(struct mgcp_conn_rtp *conn, int payload_type)
512{
513 struct mgcp_rtp_end *rtp_end = &conn->end;
514 unsigned int codecs_assigned = rtp_end->codecs_assigned;
515 struct mgcp_rtp_codec *codec = NULL;
516 size_t i;
517
518 OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
519
520 for (i = 0; i < codecs_assigned; i++) {
521 if (payload_type == rtp_end->codecs[i].payload_type) {
522 codec = &rtp_end->codecs[i];
523 break;
524 }
525 }
526
527 return codec;
528}