blob: f237e382ce71a336a1ad4ad841980fbcf522b965 [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,
92 };
93}
94
95static void codec_free(struct mgcp_rtp_codec *codec)
Philipp Maierbc0346e2018-06-07 09:52:16 +020096{
97 if (codec->subtype_name)
98 talloc_free(codec->subtype_name);
99 if (codec->audio_name)
100 talloc_free(codec->audio_name);
Neels Hofmeyr667fa592019-08-08 21:59:01 +0200101 *codec = (struct mgcp_rtp_codec){};
Philipp Maierbc0346e2018-06-07 09:52:16 +0200102}
103
104/*! Initalize or reset codec information with default data.
105 * \param[out] conn related rtp-connection. */
106void mgcp_codec_reset_all(struct mgcp_conn_rtp *conn)
107{
Neels Hofmeyrce64f182019-08-08 22:07:31 +0200108 int i;
109 for (i = 0; i < conn->end.codecs_assigned; i++)
110 codec_free(&conn->end.codecs[i]);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200111 conn->end.codecs_assigned = 0;
112 conn->end.codec = NULL;
113}
114
Neels Hofmeyr667fa592019-08-08 21:59:01 +0200115/*! Add codec configuration depending on payload type and/or codec name. This
116 * function uses the input parameters to extrapolate the full codec information.
117 * \param[out] codec configuration (caller provided memory).
118 * \param[out] conn related rtp-connection.
119 * \param[in] payload_type codec type id (e.g. 3 for GSM, -1 when undefined).
120 * \param[in] audio_name audio codec name, in uppercase (e.g. "GSM/8000/1").
121 * \param[in] param optional codec parameters (set to NULL when unused).
122 * \returns 0 on success, -EINVAL on failure. */
123int 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 +0200124{
125 int rate;
126 int channels;
127 char audio_codec[64];
Neels Hofmeyr667fa592019-08-08 21:59:01 +0200128 struct mgcp_rtp_codec *codec;
129 unsigned int pt_offset = conn->end.codecs_assigned;
130 void *ctx = conn->conn;
131
132 /* The amount of codecs we can store is limited, make sure we do not
133 * overrun this limit. */
134 if (conn->end.codecs_assigned >= MGCP_MAX_CODECS)
135 return -EINVAL;
136
137 /* First unused entry */
138 codec = &conn->end.codecs[conn->end.codecs_assigned];
Philipp Maierbc0346e2018-06-07 09:52:16 +0200139
140 /* Initalize the codec struct with some default data to begin with */
141 codec_init(codec);
142
143 if (payload_type != PTYPE_UNDEFINED) {
144 /* Make sure we do not get any reserved or undefined type numbers */
145 /* See also: https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200146 if ((payload_type == 1 || payload_type == 2 || payload_type == 19)
147 || (payload_type >= 72 && payload_type <= 76)
148 || (payload_type >= 127)) {
149 LOGP(DLMGCP, LOGL_ERROR, "Cannot add codec, payload type number %d is reserved\n",
150 payload_type);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200151 goto error;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200152 }
Philipp Maierbc0346e2018-06-07 09:52:16 +0200153
154 codec->payload_type = payload_type;
155 }
156
157 /* When no audio name is given, we are forced to use the payload
158 * type to generate the audio name. This is only possible for
159 * non dynamic payload types, which are statically defined */
160 if (!audio_name) {
161 switch (payload_type) {
162 case 0:
163 audio_name = talloc_strdup(ctx, "PCMU/8000/1");
164 break;
165 case 3:
166 audio_name = talloc_strdup(ctx, "GSM/8000/1");
167 break;
168 case 8:
169 audio_name = talloc_strdup(ctx, "PCMA/8000/1");
170 break;
171 case 18:
172 audio_name = talloc_strdup(ctx, "G729/8000/1");
173 break;
174 default:
175 /* The given payload type is not known to us, or it
176 * it is a dynamic payload type for which we do not
177 * know the audio name. We must give up here */
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200178 LOGP(DLMGCP, LOGL_ERROR, "No audio codec name given, and payload type %d unknown\n",
179 payload_type);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200180 goto error;
181 }
182 }
183
184 /* Now we extract the codec subtype name, rate and channels. The latter
185 * two are optional. If they are not present we use the safe defaults
186 * above. */
Neels Hofmeyra468b0f2019-08-28 04:56:49 +0200187 if (strlen(audio_name) >= sizeof(audio_codec)) {
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200188 LOGP(DLMGCP, LOGL_ERROR, "Audio codec too long: %s\n", osmo_quote_str(audio_name, -1));
Philipp Maierbc0346e2018-06-07 09:52:16 +0200189 goto error;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200190 }
Philipp Maierbc0346e2018-06-07 09:52:16 +0200191 channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS;
192 rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200193 if (sscanf(audio_name, "%63[^/]/%d/%d", audio_codec, &rate, &channels) < 1) {
194 LOGP(DLMGCP, LOGL_ERROR, "Invalid audio codec: %s\n", osmo_quote_str(audio_name, -1));
Philipp Maierbc0346e2018-06-07 09:52:16 +0200195 goto error;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200196 }
Philipp Maierbc0346e2018-06-07 09:52:16 +0200197
198 /* Note: We only accept configurations with one audio channel! */
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200199 if (channels != 1) {
200 LOGP(DLMGCP, LOGL_ERROR, "Cannot handle audio codec with more than one channel: %s\n",
201 osmo_quote_str(audio_name, -1));
Philipp Maierbc0346e2018-06-07 09:52:16 +0200202 goto error;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200203 }
Philipp Maierbc0346e2018-06-07 09:52:16 +0200204
205 codec->rate = rate;
206 codec->channels = channels;
207 codec->subtype_name = talloc_strdup(ctx, audio_codec);
208 codec->audio_name = talloc_strdup(ctx, audio_name);
209 codec->payload_type = payload_type;
210
211 if (!strcmp(audio_codec, "G729")) {
212 codec->frame_duration_num = 10;
213 codec->frame_duration_den = 1000;
214 } else {
215 codec->frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM;
216 codec->frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN;
217 }
218
219 /* Derive the payload type if it is unknown */
220 if (codec->payload_type == PTYPE_UNDEFINED) {
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200221 /* TODO: This is semi dead code, see OS#4150 */
Philipp Maierbc0346e2018-06-07 09:52:16 +0200222
223 /* For the known codecs from the static range we restore
224 * the IANA or 3GPP assigned payload type number */
225 if (codec->rate == 8000 && codec->channels == 1) {
226 /* See also: https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
227 if (!strcmp(codec->subtype_name, "GSM"))
228 codec->payload_type = 3;
229 else if (!strcmp(codec->subtype_name, "PCMA"))
230 codec->payload_type = 8;
231 else if (!strcmp(codec->subtype_name, "PCMU"))
232 codec->payload_type = 0;
233 else if (!strcmp(codec->subtype_name, "G729"))
234 codec->payload_type = 18;
235
236 /* See also: 3GPP TS 48.103, chapter 5.4.2.2 RTP Payload
237 * Note: These are not fixed payload types as the IANA
238 * defined once, they still remain dymanic payload
239 * types, but with a payload type number preference. */
240 else if (!strcmp(codec->subtype_name, "GSM-EFR"))
241 codec->payload_type = 110;
242 else if (!strcmp(codec->subtype_name, "GSM-HR-08"))
243 codec->payload_type = 111;
244 else if (!strcmp(codec->subtype_name, "AMR"))
245 codec->payload_type = 112;
246 else if (!strcmp(codec->subtype_name, "AMR-WB"))
247 codec->payload_type = 113;
248 }
249
250 /* If we could not determine a payload type we assume that
251 * we are dealing with a codec from the dynamic range. We
252 * choose a fixed identifier from 96-109. (Note: normally,
253 * the dynamic payload type rante is from 96-127, but from
254 * 110 onwards 3gpp defines prefered codec types, which are
255 * also fixed, see above) */
256 if (codec->payload_type < 0) {
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200257 /* FIXME: pt_offset is completely unrelated and useless here, any of those numbers may already
258 * have been added to the codecs. Instead, there should be an iterator checking for an actually
259 * unused dynamic payload type number. */
Philipp Maierbc0346e2018-06-07 09:52:16 +0200260 codec->payload_type = 96 + pt_offset;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200261 if (codec->payload_type > 109) {
262 LOGP(DLMGCP, LOGL_ERROR, "Ran out of payload type numbers to assign dynamically\n");
Philipp Maierbc0346e2018-06-07 09:52:16 +0200263 goto error;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200264 }
Philipp Maierbc0346e2018-06-07 09:52:16 +0200265 }
266 }
267
Philipp Maier228e5912019-03-05 13:56:59 +0100268 /* Copy over optional codec parameters */
269 if (param) {
270 codec->param = *param;
271 codec->param_present = true;
272 } else
273 codec->param_present = false;
274
Neels Hofmeyr667fa592019-08-08 21:59:01 +0200275 conn->end.codecs_assigned++;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200276 return 0;
277error:
278 /* Make sure we leave a clean codec entry on error. */
Neels Hofmeyr667fa592019-08-08 21:59:01 +0200279 codec_free(codec);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200280 return -EINVAL;
281}
282
Philipp Maierbc0346e2018-06-07 09:52:16 +0200283/* Check if the given codec is applicable on the specified endpoint
284 * Helper function for mgcp_codec_decide() */
285static bool is_codec_compatible(const struct mgcp_endpoint *endp, const struct mgcp_rtp_codec *codec)
286{
Philipp Maierbc0346e2018-06-07 09:52:16 +0200287 /* A codec name must be set, if not, this might mean that the codec
288 * (payload type) that was assigned is unknown to us so we must stop
289 * here. */
290 if (!codec->subtype_name)
291 return false;
292
Philipp Maier7f90ddb2020-06-02 21:52:53 +0200293 /* FIXME: implement meaningful checks to make sure that the given codec
294 * is compatible with the given endpoint */
Philipp Maierbc0346e2018-06-07 09:52:16 +0200295
Philipp Maier7f90ddb2020-06-02 21:52:53 +0200296 return true;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200297}
298
299/*! Decide for one suitable codec
300 * \param[in] conn related rtp-connection.
301 * \returns 0 on success, -EINVAL on failure. */
302int mgcp_codec_decide(struct mgcp_conn_rtp *conn)
303{
304 struct mgcp_rtp_end *rtp;
305 unsigned int i;
306 struct mgcp_endpoint *endp;
307 bool codec_assigned = false;
308
309 endp = conn->conn->endp;
310 rtp = &conn->end;
311
312 /* This function works on the results the SDP/LCO parser has extracted
313 * from the MGCP message. The goal is to select a suitable codec for
314 * the given connection. When transcoding is available, the first codec
315 * from the codec list is taken without further checking. When
316 * transcoding is not available, then the choice must be made more
317 * carefully. Each codec in the list is checked until one is found that
318 * is rated compatible. The rating is done by the helper function
319 * is_codec_compatible(), which does the actual checking. */
320 for (i = 0; i < rtp->codecs_assigned; i++) {
321 /* When no transcoding is available, avoid codecs that would
322 * require transcoding. */
Philipp Maier14b27a82020-06-02 20:15:30 +0200323 if (endp->trunk->no_audio_transcoding && !is_codec_compatible(endp, &rtp->codecs[i])) {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200324 LOGP(DLMGCP, LOGL_NOTICE, "transcoding not available, skipping codec: %d/%s\n",
325 rtp->codecs[i].payload_type, rtp->codecs[i].subtype_name);
326 continue;
327 }
328
329 rtp->codec = &rtp->codecs[i];
330 codec_assigned = true;
331 break;
332 }
333
334 /* FIXME: To the reviewes: This is problematic. I do not get why we
335 * need to reset the packet_duration_ms depending on the codec
336 * selection. I thought it were all 20ms? Is this to address some
337 * cornercase. (This piece of code was in the code path before,
338 * together with the note: "TODO/XXX: Store this per codec and derive
339 * it on use" */
340 if (codec_assigned) {
341 if (rtp->maximum_packet_time >= 0
342 && rtp->maximum_packet_time * rtp->codec->frame_duration_den >
343 rtp->codec->frame_duration_num * 1500)
344 rtp->packet_duration_ms = 0;
345
346 return 0;
347 }
348
349 return -EINVAL;
350}
Philipp Maier6931f9a2018-07-26 09:29:31 +0200351
Neels Hofmeyr16b637b2019-08-08 22:47:10 +0200352/* Return true if octet-aligned is set in the given codec. Default to octet-aligned=0, i.e. bandwidth-efficient mode.
353 * See RFC4867 "RTP Payload Format for AMR and AMR-WB" sections "8.1. AMR Media Type Registration" and "8.2. AMR-WB
354 * Media Type Registration":
355 *
356 * octet-align: Permissible values are 0 and 1. If 1, octet-aligned
357 * operation SHALL be used. If 0 or if not present,
358 * bandwidth-efficient operation is employed.
359 *
360 * https://tools.ietf.org/html/rfc4867
361 */
362static bool amr_is_octet_aligned(const struct mgcp_rtp_codec *codec)
363{
364 if (!codec->param_present)
365 return false;
366 if (!codec->param.amr_octet_aligned_present)
367 return false;
368 return codec->param.amr_octet_aligned;
369}
370
Philipp Maier6931f9a2018-07-26 09:29:31 +0200371/* Compare two codecs, all parameters must match up, except for the payload type
372 * number. */
Neels Hofmeyr782d6072019-08-09 00:51:21 +0200373static bool codecs_same(struct mgcp_rtp_codec *codec_a, struct mgcp_rtp_codec *codec_b)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200374{
375 if (codec_a->rate != codec_b->rate)
376 return false;
377 if (codec_a->channels != codec_b->channels)
378 return false;
379 if (codec_a->frame_duration_num != codec_b->frame_duration_num)
380 return false;
381 if (codec_a->frame_duration_den != codec_b->frame_duration_den)
382 return false;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200383 if (strcmp(codec_a->subtype_name, codec_b->subtype_name))
384 return false;
Neels Hofmeyr16b637b2019-08-08 22:47:10 +0200385 if (!strcmp(codec_a->subtype_name, "AMR")) {
386 if (amr_is_octet_aligned(codec_a) != amr_is_octet_aligned(codec_b))
387 return false;
388 }
Philipp Maier6931f9a2018-07-26 09:29:31 +0200389
390 return true;
391}
392
393/*! Translate a given payload type number that belongs to the packet of a
394 * source connection to the equivalent payload type number that matches the
395 * configuration of a destination connection.
396 * \param[in] conn_src related source rtp-connection.
397 * \param[in] conn_dst related destination rtp-connection.
398 * \param[in] payload_type number from the source packet or source connection.
399 * \returns translated payload type number on success, -EINVAL on failure. */
400int mgcp_codec_pt_translate(struct mgcp_conn_rtp *conn_src, struct mgcp_conn_rtp *conn_dst, int payload_type)
401{
402 struct mgcp_rtp_end *rtp_src;
403 struct mgcp_rtp_end *rtp_dst;
404 struct mgcp_rtp_codec *codec_src = NULL;
405 struct mgcp_rtp_codec *codec_dst = NULL;
406 unsigned int i;
407 unsigned int codecs_assigned;
408
409 rtp_src = &conn_src->end;
410 rtp_dst = &conn_dst->end;
411
412 /* Find the codec information that is used on the source side */
413 codecs_assigned = rtp_src->codecs_assigned;
414 OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
415 for (i = 0; i < codecs_assigned; i++) {
416 if (payload_type == rtp_src->codecs[i].payload_type) {
417 codec_src = &rtp_src->codecs[i];
418 break;
419 }
420 }
421 if (!codec_src)
422 return -EINVAL;
423
424 /* Use the codec infrmation from the source and try to find the
425 * equivalent of it on the destination side */
426 codecs_assigned = rtp_dst->codecs_assigned;
427 OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
428 for (i = 0; i < codecs_assigned; i++) {
Neels Hofmeyr782d6072019-08-09 00:51:21 +0200429 if (codecs_same(codec_src, &rtp_dst->codecs[i])) {
Philipp Maier6931f9a2018-07-26 09:29:31 +0200430 codec_dst = &rtp_dst->codecs[i];
431 break;
432 }
433 }
434 if (!codec_dst)
435 return -EINVAL;
436
437 return codec_dst->payload_type;
438}
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200439
440/* Find the payload type number configured for a specific codec by SDP.
441 * For example, IuUP gets assigned a payload type number, and the endpoint needs to translate that to the number
442 * assigned to "AMR" on the other conn (by a=rtpmap:N).
443 * \param conn The side of an endpoint to get the payload type number for (to translate the payload type number to).
444 * \param subtype_name SDP codec name without parameters (e.g. "AMR").
445 * \param match_nr Index for the match found, first being match_nr == 0. Iterate all matches by calling multiple times
446 * with incrementing match_nr.
447 * \return codec definition for that conn matching the subtype_name, or NULL if no such match_nr is found.
448 */
449const struct mgcp_rtp_codec *mgcp_codec_pt_find_by_subtype_name(struct mgcp_conn_rtp *conn,
450 const char *subtype_name, unsigned int match_nr)
451{
452 int i;
453 for (i = 0; i < conn->end.codecs_assigned; i++) {
454 if (!strcmp(conn->end.codecs[i].subtype_name, subtype_name)) {
455 if (match_nr) {
456 match_nr--;
457 continue;
458 }
459 return &conn->end.codecs[i];
460 }
461 }
462 return NULL;
463}