blob: 905795c71d13f9717e873bbe509c9f405c8ab301 [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. */
333 if (strcmp(codec_a->subtype_name, codec_b->subtype_name))
334 return false;
335 if (codec_a->rate != codec_b->rate)
336 return false;
337 if (codec_a->channels != codec_b->channels)
338 return false;
339 if (codec_a->frame_duration_num != codec_b->frame_duration_num)
340 return false;
341 if (codec_a->frame_duration_den != codec_b->frame_duration_den)
342 return false;
343
344 return true;
345}
346
Philipp Maier4c4d2272023-04-26 15:45:17 +0200347struct mgcp_rtp_codec *mgcp_codec_find_same(struct mgcp_conn_rtp *conn, struct mgcp_rtp_codec *codec)
348{
349 struct mgcp_rtp_end *rtp_end;
350 unsigned int i;
351 unsigned int codecs_assigned;
352
353 rtp_end = &conn->end;
354
355 /* Use the codec information from the source and try to find the equivalent of it on the destination side. In
356 * the first run we will look for an exact match. */
357 codecs_assigned = rtp_end->codecs_assigned;
358 OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
359 for (i = 0; i < codecs_assigned; i++) {
360 if (codecs_same(codec, &rtp_end->codecs[i])) {
361 return &rtp_end->codecs[i];
362 break;
363 }
364 }
365
366 return NULL;
367}
368
369/* For a given codec, find a convertible codec in the given connection. */
370static struct mgcp_rtp_codec *codec_find_convertible(struct mgcp_conn_rtp *conn, struct mgcp_rtp_codec *codec)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200371{
Philipp Maier9dd80ca2023-03-27 15:45:36 +0200372 struct mgcp_rtp_end *rtp_end;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200373 unsigned int i;
374 unsigned int codecs_assigned;
Philipp Maier9dd80ca2023-03-27 15:45:36 +0200375 struct mgcp_rtp_codec *codec_convertible = NULL;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200376
Philipp Maier9dd80ca2023-03-27 15:45:36 +0200377 rtp_end = &conn->end;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200378
Philipp Maier621e8662023-03-22 16:53:30 +0100379 /* Use the codec information from the source and try to find the equivalent of it on the destination side. In
380 * the first run we will look for an exact match. */
Philipp Maier4c4d2272023-04-26 15:45:17 +0200381 codec_convertible = mgcp_codec_find_same(conn, codec);
382 if (codec_convertible)
383 return codec_convertible;
Philipp Maier621e8662023-03-22 16:53:30 +0100384
385 /* 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
386 * 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 +0200387 codecs_assigned = rtp_end->codecs_assigned;
388 OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
389 for (i = 0; i < codecs_assigned; i++) {
390 if (codecs_convertible(codec, &rtp_end->codecs[i])) {
391 codec_convertible = &rtp_end->codecs[i];
392 break;
Philipp Maier621e8662023-03-22 16:53:30 +0100393 }
394 }
395
Philipp Maier9dd80ca2023-03-27 15:45:36 +0200396 return codec_convertible;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200397}
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200398
Philipp Maier4c4d2272023-04-26 15:45:17 +0200399/*! Decide for one suitable codec on both of the given connections. In case a destination connection is not available,
400 * a tentative decision is made.
401 * \param[inout] conn_src related rtp-connection.
402 * \param[inout] conn_dst related destination rtp-connection (NULL if not present).
Philipp Maier0e8310f2023-04-26 15:23:05 +0200403 * \returns 0 on success, -EINVAL on failure. */
Philipp Maier4c4d2272023-04-26 15:45:17 +0200404int mgcp_codec_decide(struct mgcp_conn_rtp *conn_src, struct mgcp_conn_rtp *conn_dst)
Philipp Maier0e8310f2023-04-26 15:23:05 +0200405{
Philipp Maier0e8310f2023-04-26 15:23:05 +0200406 unsigned int i;
Philipp Maier0e8310f2023-04-26 15:23:05 +0200407
Philipp Maier4c4d2272023-04-26 15:45:17 +0200408 /* In case no destination connection is available (yet), or in case the destination connection exists but has
409 * no codecs assigned, we are forced to make a simple tentative decision:
410 * We just use the first codec of the source connection (conn_src) */
411 OSMO_ASSERT(conn_src->end.codecs_assigned <= MGCP_MAX_CODECS);
412 if (!conn_dst || conn_dst->end.codecs_assigned == 0) {
413 if (conn_src->end.codecs_assigned >= 1) {
414 conn_src->end.codec = &conn_src->end.codecs[0];
415 return 0;
416 } else
417 return -EINVAL;
Philipp Maier0e8310f2023-04-26 15:23:05 +0200418 }
419
Philipp Maier4c4d2272023-04-26 15:45:17 +0200420 /* Compare all codecs of the source connection (conn_src) to the codecs of the destination connection (conn_dst). In case
421 * of a match set this codec on both connections. This would be an ideal selection since no codec conversion would be
422 * required. */
423 for (i = 0; i < conn_src->end.codecs_assigned; i++) {
424 struct mgcp_rtp_codec *codec_conn_dst = mgcp_codec_find_same(conn_dst, &conn_src->end.codecs[i]);
425 if (codec_conn_dst) {
426 /* We found the a codec that is exactly the same (same codec, same payload format etc.) on both
427 * sides. We now set this codec on both connections. */
428 conn_dst->end.codec = codec_conn_dst;
429 conn_src->end.codec = mgcp_codec_find_same(conn_src, codec_conn_dst);
430 OSMO_ASSERT(conn_src->end.codec);
431 return 0;
432 }
433 }
Philipp Maier0e8310f2023-04-26 15:23:05 +0200434
Philipp Maier4c4d2272023-04-26 15:45:17 +0200435 /* 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
436 * to convert. */
437 for (i = 0; i < conn_src->end.codecs_assigned; i++) {
438 struct mgcp_rtp_codec *codec_conn_dst = codec_find_convertible(conn_dst, &conn_src->end.codecs[i]);
439 if (codec_conn_dst) {
440 /* We found the a codec that we are able to convert on both sides. We now set this codec on both
441 * connections. */
442 conn_dst->end.codec = codec_conn_dst;
443 conn_src->end.codec = codec_find_convertible(conn_src, codec_conn_dst);
444 OSMO_ASSERT(conn_src->end.codec);
445 return 0;
446 }
Philipp Maier0e8310f2023-04-26 15:23:05 +0200447 }
448
449 return -EINVAL;
450}
451
452/* Check if the codec has a specific AMR mode (octet-aligned or bandwith-efficient) set. */
453bool mgcp_codec_amr_align_mode_is_indicated(const struct mgcp_rtp_codec *codec)
454{
455 if (codec->param_present == false)
456 return false;
457 if (!codec->param.amr_octet_aligned_present)
458 return false;
459 if (strcmp(codec->subtype_name, "AMR") != 0)
460 return false;
461 return true;
462}
463
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200464/* Find the payload type number configured for a specific codec by SDP.
465 * For example, IuUP gets assigned a payload type number, and the endpoint needs to translate that to the number
466 * assigned to "AMR" on the other conn (by a=rtpmap:N).
467 * \param conn The side of an endpoint to get the payload type number for (to translate the payload type number to).
468 * \param subtype_name SDP codec name without parameters (e.g. "AMR").
469 * \param match_nr Index for the match found, first being match_nr == 0. Iterate all matches by calling multiple times
470 * with incrementing match_nr.
Philipp Maier3699ef02023-03-27 15:54:22 +0200471 * \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 +0200472const struct mgcp_rtp_codec *mgcp_codec_pt_find_by_subtype_name(struct mgcp_conn_rtp *conn,
473 const char *subtype_name, unsigned int match_nr)
474{
475 int i;
476 for (i = 0; i < conn->end.codecs_assigned; i++) {
477 if (!strcmp(conn->end.codecs[i].subtype_name, subtype_name)) {
478 if (match_nr) {
479 match_nr--;
480 continue;
481 }
482 return &conn->end.codecs[i];
483 }
484 }
485 return NULL;
486}
Philipp Maier9dd80ca2023-03-27 15:45:36 +0200487
488/*! Lookup a codec that is assigned to a connection by its payload type number.
489 * \param[in] conn related rtp-connection.
490 * \param[in] payload_type number of the codec to look up.
491 * \returns pointer to codec struct on success, NULL on failure. */
492struct mgcp_rtp_codec *mgcp_codec_from_pt(struct mgcp_conn_rtp *conn, int payload_type)
493{
494 struct mgcp_rtp_end *rtp_end = &conn->end;
495 unsigned int codecs_assigned = rtp_end->codecs_assigned;
496 struct mgcp_rtp_codec *codec = NULL;
497 size_t i;
498
499 OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
500
501 for (i = 0; i < codecs_assigned; i++) {
502 if (payload_type == rtp_end->codecs[i].payload_type) {
503 codec = &rtp_end->codecs[i];
504 break;
505 }
506 }
507
508 return codec;
509}