blob: 7fada78eece5b6915859099eb69c86b299cf75bc [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 Maier9dd80ca2023-03-27 15:45:36 +0200347/*! For a given codec, find a convertible codec in the given connection.
348 * \param[in] conn connection to search for a convertible codec
349 * \param[in] codec for which a convertible codec shall be found.
350 * \returns codec on success, -NULL on failure. */
351struct mgcp_rtp_codec *mgcp_codec_find_convertible(struct mgcp_conn_rtp *conn, struct mgcp_rtp_codec *codec)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200352{
Philipp Maier9dd80ca2023-03-27 15:45:36 +0200353 struct mgcp_rtp_end *rtp_end;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200354 unsigned int i;
355 unsigned int codecs_assigned;
Philipp Maier9dd80ca2023-03-27 15:45:36 +0200356 struct mgcp_rtp_codec *codec_convertible = NULL;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200357
Philipp Maier9dd80ca2023-03-27 15:45:36 +0200358 rtp_end = &conn->end;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200359
Philipp Maier621e8662023-03-22 16:53:30 +0100360 /* Use the codec information from the source and try to find the equivalent of it on the destination side. In
361 * the first run we will look for an exact match. */
Philipp Maier9dd80ca2023-03-27 15:45:36 +0200362 codecs_assigned = rtp_end->codecs_assigned;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200363 OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
364 for (i = 0; i < codecs_assigned; i++) {
Philipp Maier9dd80ca2023-03-27 15:45:36 +0200365 if (codecs_same(codec, &rtp_end->codecs[i])) {
366 codec_convertible = &rtp_end->codecs[i];
Philipp Maier6931f9a2018-07-26 09:29:31 +0200367 break;
368 }
369 }
Philipp Maier621e8662023-03-22 16:53:30 +0100370
371 /* 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
372 * payload format may be different. This alternative will require a frame format conversion (i.e. AMR bwe->oe) */
Philipp Maier9dd80ca2023-03-27 15:45:36 +0200373 if (!codec_convertible) {
Philipp Maier621e8662023-03-22 16:53:30 +0100374 for (i = 0; i < codecs_assigned; i++) {
Philipp Maier9dd80ca2023-03-27 15:45:36 +0200375 if (codecs_convertible(codec, &rtp_end->codecs[i])) {
376 codec_convertible = &rtp_end->codecs[i];
Philipp Maier621e8662023-03-22 16:53:30 +0100377 break;
378 }
379 }
380 }
381
Philipp Maier9dd80ca2023-03-27 15:45:36 +0200382 return codec_convertible;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200383}
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200384
Philipp Maier0e8310f2023-04-26 15:23:05 +0200385/* Check if the given codec is applicable on the specified endpoint
386 * Helper function for mgcp_codec_decide() */
387static bool is_codec_compatible(const struct mgcp_endpoint *endp, const struct mgcp_rtp_codec *codec)
388{
389 /* A codec name must be set, if not, this might mean that the codec
390 * (payload type) that was assigned is unknown to us so we must stop
391 * here. */
392 if (!strlen(codec->subtype_name))
393 return false;
394
395 /* FIXME: implement meaningful checks to make sure that the given codec
396 * is compatible with the given endpoint */
397
398 return true;
399}
400
401/*! Decide for one suitable codec
402 * \param[in] conn related rtp-connection.
403 * \returns 0 on success, -EINVAL on failure. */
404int mgcp_codec_decide(struct mgcp_conn_rtp *conn)
405{
406 struct mgcp_rtp_end *rtp;
407 unsigned int i;
408 struct mgcp_endpoint *endp;
409 bool codec_assigned = false;
410
411 endp = conn->conn->endp;
412 rtp = &conn->end;
413
414 /* This function works on the results the SDP/LCO parser has extracted
415 * from the MGCP message. The goal is to select a suitable codec for
416 * the given connection. When transcoding is available, the first codec
417 * from the codec list is taken without further checking. When
418 * transcoding is not available, then the choice must be made more
419 * carefully. Each codec in the list is checked until one is found that
420 * is rated compatible. The rating is done by the helper function
421 * is_codec_compatible(), which does the actual checking. */
422 for (i = 0; i < rtp->codecs_assigned; i++) {
423 /* When no transcoding is available, avoid codecs that would
424 * require transcoding. */
425 if (endp->trunk->no_audio_transcoding && !is_codec_compatible(endp, &rtp->codecs[i])) {
426 LOGP(DLMGCP, LOGL_NOTICE, "transcoding not available, skipping codec: %d/%s\n",
427 rtp->codecs[i].payload_type, rtp->codecs[i].subtype_name);
428 continue;
429 }
430
431 rtp->codec = &rtp->codecs[i];
432 codec_assigned = true;
433 break;
434 }
435
436 /* FIXME: To the reviewes: This is problematic. I do not get why we
437 * need to reset the packet_duration_ms depending on the codec
438 * selection. I thought it were all 20ms? Is this to address some
439 * cornercase. (This piece of code was in the code path before,
440 * together with the note: "TODO/XXX: Store this per codec and derive
441 * it on use" */
442 if (codec_assigned) {
443 if (rtp->maximum_packet_time >= 0
444 && rtp->maximum_packet_time * rtp->codec->frame_duration_den >
445 rtp->codec->frame_duration_num * 1500)
446 rtp->packet_duration_ms = 0;
447
448 return 0;
449 }
450
451 return -EINVAL;
452}
453
454/* Check if the codec has a specific AMR mode (octet-aligned or bandwith-efficient) set. */
455bool mgcp_codec_amr_align_mode_is_indicated(const struct mgcp_rtp_codec *codec)
456{
457 if (codec->param_present == false)
458 return false;
459 if (!codec->param.amr_octet_aligned_present)
460 return false;
461 if (strcmp(codec->subtype_name, "AMR") != 0)
462 return false;
463 return true;
464}
465
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200466/* Find the payload type number configured for a specific codec by SDP.
467 * For example, IuUP gets assigned a payload type number, and the endpoint needs to translate that to the number
468 * assigned to "AMR" on the other conn (by a=rtpmap:N).
469 * \param conn The side of an endpoint to get the payload type number for (to translate the payload type number to).
470 * \param subtype_name SDP codec name without parameters (e.g. "AMR").
471 * \param match_nr Index for the match found, first being match_nr == 0. Iterate all matches by calling multiple times
472 * with incrementing match_nr.
Philipp Maier3699ef02023-03-27 15:54:22 +0200473 * \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 +0200474const struct mgcp_rtp_codec *mgcp_codec_pt_find_by_subtype_name(struct mgcp_conn_rtp *conn,
475 const char *subtype_name, unsigned int match_nr)
476{
477 int i;
478 for (i = 0; i < conn->end.codecs_assigned; i++) {
479 if (!strcmp(conn->end.codecs[i].subtype_name, subtype_name)) {
480 if (match_nr) {
481 match_nr--;
482 continue;
483 }
484 return &conn->end.codecs[i];
485 }
486 }
487 return NULL;
488}
Philipp Maier9dd80ca2023-03-27 15:45:36 +0200489
490/*! Lookup a codec that is assigned to a connection by its payload type number.
491 * \param[in] conn related rtp-connection.
492 * \param[in] payload_type number of the codec to look up.
493 * \returns pointer to codec struct on success, NULL on failure. */
494struct mgcp_rtp_codec *mgcp_codec_from_pt(struct mgcp_conn_rtp *conn, int payload_type)
495{
496 struct mgcp_rtp_end *rtp_end = &conn->end;
497 unsigned int codecs_assigned = rtp_end->codecs_assigned;
498 struct mgcp_rtp_codec *codec = NULL;
499 size_t i;
500
501 OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
502
503 for (i = 0; i < codecs_assigned; i++) {
504 if (payload_type == rtp_end->codecs[i].payload_type) {
505 codec = &rtp_end->codecs[i];
506 break;
507 }
508 }
509
510 return codec;
511}