blob: 1a8d26a619d8b5858d2be12cee3a5168a4b805d0 [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
Philipp Maierbc0346e2018-06-07 09:52:16 +0200279/* Check if the given codec is applicable on the specified endpoint
280 * Helper function for mgcp_codec_decide() */
281static bool is_codec_compatible(const struct mgcp_endpoint *endp, const struct mgcp_rtp_codec *codec)
282{
Philipp Maierbc0346e2018-06-07 09:52:16 +0200283 /* A codec name must be set, if not, this might mean that the codec
284 * (payload type) that was assigned is unknown to us so we must stop
285 * here. */
Eric2ebcf5c2021-08-09 23:26:48 +0200286 if (!strlen(codec->subtype_name))
Philipp Maierbc0346e2018-06-07 09:52:16 +0200287 return false;
288
Philipp Maier7f90ddb2020-06-02 21:52:53 +0200289 /* FIXME: implement meaningful checks to make sure that the given codec
290 * is compatible with the given endpoint */
Philipp Maierbc0346e2018-06-07 09:52:16 +0200291
Philipp Maier7f90ddb2020-06-02 21:52:53 +0200292 return true;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200293}
294
295/*! Decide for one suitable codec
296 * \param[in] conn related rtp-connection.
297 * \returns 0 on success, -EINVAL on failure. */
298int mgcp_codec_decide(struct mgcp_conn_rtp *conn)
299{
300 struct mgcp_rtp_end *rtp;
301 unsigned int i;
302 struct mgcp_endpoint *endp;
303 bool codec_assigned = false;
304
305 endp = conn->conn->endp;
306 rtp = &conn->end;
307
308 /* This function works on the results the SDP/LCO parser has extracted
309 * from the MGCP message. The goal is to select a suitable codec for
310 * the given connection. When transcoding is available, the first codec
311 * from the codec list is taken without further checking. When
312 * transcoding is not available, then the choice must be made more
313 * carefully. Each codec in the list is checked until one is found that
314 * is rated compatible. The rating is done by the helper function
315 * is_codec_compatible(), which does the actual checking. */
316 for (i = 0; i < rtp->codecs_assigned; i++) {
317 /* When no transcoding is available, avoid codecs that would
318 * require transcoding. */
Philipp Maier14b27a82020-06-02 20:15:30 +0200319 if (endp->trunk->no_audio_transcoding && !is_codec_compatible(endp, &rtp->codecs[i])) {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200320 LOGP(DLMGCP, LOGL_NOTICE, "transcoding not available, skipping codec: %d/%s\n",
321 rtp->codecs[i].payload_type, rtp->codecs[i].subtype_name);
322 continue;
323 }
324
325 rtp->codec = &rtp->codecs[i];
326 codec_assigned = true;
327 break;
328 }
329
330 /* FIXME: To the reviewes: This is problematic. I do not get why we
331 * need to reset the packet_duration_ms depending on the codec
332 * selection. I thought it were all 20ms? Is this to address some
333 * cornercase. (This piece of code was in the code path before,
334 * together with the note: "TODO/XXX: Store this per codec and derive
335 * it on use" */
336 if (codec_assigned) {
337 if (rtp->maximum_packet_time >= 0
338 && rtp->maximum_packet_time * rtp->codec->frame_duration_den >
339 rtp->codec->frame_duration_num * 1500)
340 rtp->packet_duration_ms = 0;
341
342 return 0;
343 }
344
345 return -EINVAL;
346}
Philipp Maier6931f9a2018-07-26 09:29:31 +0200347
Pau Espin Pedrolcca55242022-10-24 17:14:33 +0200348/* Check if the codec has a specific AMR mode (octet-aligned or bandwith-efficient) set. */
349bool mgcp_codec_amr_align_mode_is_indicated(const struct mgcp_rtp_codec *codec)
350{
351 if (codec->param_present == false)
352 return false;
353 if (!codec->param.amr_octet_aligned_present)
354 return false;
355 if (strcmp(codec->subtype_name, "AMR") != 0)
356 return false;
357 return true;
358}
359
Neels Hofmeyr16b637b2019-08-08 22:47:10 +0200360/* Return true if octet-aligned is set in the given codec. Default to octet-aligned=0, i.e. bandwidth-efficient mode.
361 * See RFC4867 "RTP Payload Format for AMR and AMR-WB" sections "8.1. AMR Media Type Registration" and "8.2. AMR-WB
362 * Media Type Registration":
363 *
364 * octet-align: Permissible values are 0 and 1. If 1, octet-aligned
365 * operation SHALL be used. If 0 or if not present,
366 * bandwidth-efficient operation is employed.
367 *
368 * https://tools.ietf.org/html/rfc4867
369 */
Pau Espin Pedrol2799ff92022-01-03 15:24:59 +0100370bool mgcp_codec_amr_is_octet_aligned(const struct mgcp_rtp_codec *codec)
Neels Hofmeyr16b637b2019-08-08 22:47:10 +0200371{
372 if (!codec->param_present)
373 return false;
374 if (!codec->param.amr_octet_aligned_present)
375 return false;
376 return codec->param.amr_octet_aligned;
377}
378
Philipp Maier6931f9a2018-07-26 09:29:31 +0200379/* Compare two codecs, all parameters must match up, except for the payload type
380 * number. */
Neels Hofmeyr782d6072019-08-09 00:51:21 +0200381static bool codecs_same(struct mgcp_rtp_codec *codec_a, struct mgcp_rtp_codec *codec_b)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200382{
383 if (codec_a->rate != codec_b->rate)
384 return false;
385 if (codec_a->channels != codec_b->channels)
386 return false;
387 if (codec_a->frame_duration_num != codec_b->frame_duration_num)
388 return false;
389 if (codec_a->frame_duration_den != codec_b->frame_duration_den)
390 return false;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200391 if (strcmp(codec_a->subtype_name, codec_b->subtype_name))
392 return false;
Philipp Maiere0058b72022-02-16 12:21:15 +0100393
394 /* Note: AMR allows to set the RTP payload format to octet-aligned or bandwith-efficient (octet-aligned=0)
395 * via SDP. This difference concerns payload format only, but not the actual codec. It is not a difference
396 * within the meaning of this function. */
Philipp Maier6931f9a2018-07-26 09:29:31 +0200397
398 return true;
399}
400
401/*! Translate a given payload type number that belongs to the packet of a
402 * source connection to the equivalent payload type number that matches the
403 * configuration of a destination connection.
404 * \param[in] conn_src related source rtp-connection.
405 * \param[in] conn_dst related destination rtp-connection.
406 * \param[in] payload_type number from the source packet or source connection.
407 * \returns translated payload type number on success, -EINVAL on failure. */
408int mgcp_codec_pt_translate(struct mgcp_conn_rtp *conn_src, struct mgcp_conn_rtp *conn_dst, int payload_type)
409{
410 struct mgcp_rtp_end *rtp_src;
411 struct mgcp_rtp_end *rtp_dst;
412 struct mgcp_rtp_codec *codec_src = NULL;
413 struct mgcp_rtp_codec *codec_dst = NULL;
414 unsigned int i;
415 unsigned int codecs_assigned;
416
417 rtp_src = &conn_src->end;
418 rtp_dst = &conn_dst->end;
419
420 /* Find the codec information that is used on the source side */
421 codecs_assigned = rtp_src->codecs_assigned;
422 OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
423 for (i = 0; i < codecs_assigned; i++) {
424 if (payload_type == rtp_src->codecs[i].payload_type) {
425 codec_src = &rtp_src->codecs[i];
426 break;
427 }
428 }
429 if (!codec_src)
430 return -EINVAL;
431
Pau Espin Pedrol80751d82022-01-03 15:26:08 +0100432 /* Use the codec information from the source and try to find the
Philipp Maier6931f9a2018-07-26 09:29:31 +0200433 * equivalent of it on the destination side */
434 codecs_assigned = rtp_dst->codecs_assigned;
435 OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
436 for (i = 0; i < codecs_assigned; i++) {
Neels Hofmeyr782d6072019-08-09 00:51:21 +0200437 if (codecs_same(codec_src, &rtp_dst->codecs[i])) {
Philipp Maier6931f9a2018-07-26 09:29:31 +0200438 codec_dst = &rtp_dst->codecs[i];
439 break;
440 }
441 }
442 if (!codec_dst)
443 return -EINVAL;
444
445 return codec_dst->payload_type;
446}
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200447
448/* Find the payload type number configured for a specific codec by SDP.
449 * For example, IuUP gets assigned a payload type number, and the endpoint needs to translate that to the number
450 * assigned to "AMR" on the other conn (by a=rtpmap:N).
451 * \param conn The side of an endpoint to get the payload type number for (to translate the payload type number to).
452 * \param subtype_name SDP codec name without parameters (e.g. "AMR").
453 * \param match_nr Index for the match found, first being match_nr == 0. Iterate all matches by calling multiple times
454 * with incrementing match_nr.
455 * \return codec definition for that conn matching the subtype_name, or NULL if no such match_nr is found.
456 */
457const struct mgcp_rtp_codec *mgcp_codec_pt_find_by_subtype_name(struct mgcp_conn_rtp *conn,
458 const char *subtype_name, unsigned int match_nr)
459{
460 int i;
461 for (i = 0; i < conn->end.codecs_assigned; i++) {
462 if (!strcmp(conn->end.codecs[i].subtype_name, subtype_name)) {
463 if (match_nr) {
464 match_nr--;
465 continue;
466 }
467 return &conn->end.codecs[i];
468 }
469 }
470 return NULL;
471}