blob: 3cea495f3150243687843cebbc5ae71aad127828 [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 */
20#include <osmocom/mgcp/mgcp_internal.h>
21#include <osmocom/mgcp/mgcp_endp.h>
22#include <errno.h>
23
24/* Helper function to dump codec information of a specified codec to a printable
25 * string, used by dump_codec_summary() */
26static char *dump_codec(struct mgcp_rtp_codec *codec)
27{
28 static char str[256];
29 char *pt_str;
30
31 if (codec->payload_type > 76)
32 pt_str = "DYNAMIC";
33 else if (codec->payload_type > 72)
34 pt_str = "RESERVED <!>";
35 else if (codec->payload_type != PTYPE_UNDEFINED)
36 pt_str = codec->subtype_name;
37 else
38 pt_str = "INVALID <!>";
39
40 snprintf(str, sizeof(str), "(pt:%i=%s, audio:%s subt=%s, rate=%u, ch=%i, t=%u/%u)", codec->payload_type, pt_str,
41 codec->audio_name, codec->subtype_name, codec->rate, codec->channels, codec->frame_duration_num,
42 codec->frame_duration_den);
43 return str;
44}
45
46/*! Dump a summary of all negotiated codecs to debug log
47 * \param[in] conn related rtp-connection. */
48void mgcp_codec_summary(struct mgcp_conn_rtp *conn)
49{
50 struct mgcp_rtp_end *rtp;
51 unsigned int i;
52 struct mgcp_rtp_codec *codec;
53 struct mgcp_endpoint *endp;
54
55 rtp = &conn->end;
56 endp = conn->conn->endp;
57
58 if (rtp->codecs_assigned == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +020059 LOGPENDP(endp, DLMGCP, LOGL_ERROR, "conn:%s no codecs available\n",
60 mgcp_conn_dump(conn->conn));
Philipp Maierbc0346e2018-06-07 09:52:16 +020061 return;
62 }
63
64 /* Store parsed codec information */
65 for (i = 0; i < rtp->codecs_assigned; i++) {
66 codec = &rtp->codecs[i];
67
Pau Espin Pedrol3239f622019-04-24 18:47:46 +020068 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "conn:%s codecs[%u]:%s",
69 mgcp_conn_dump(conn->conn), i, dump_codec(codec));
Philipp Maierbc0346e2018-06-07 09:52:16 +020070
71 if (codec == rtp->codec)
72 LOGPC(DLMGCP, LOGL_DEBUG, " [selected]");
73
74 LOGPC(DLMGCP, LOGL_DEBUG, "\n");
75 }
76}
77
78/* Initalize or reset codec information with default data. */
Neels Hofmeyr667fa592019-08-08 21:59:01 +020079static void codec_init(struct mgcp_rtp_codec *codec)
80{
81 *codec = (struct mgcp_rtp_codec){
82 .payload_type = -1,
83 .frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM,
84 .frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN,
85 .rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE,
86 .channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS,
87 };
88}
89
90static void codec_free(struct mgcp_rtp_codec *codec)
Philipp Maierbc0346e2018-06-07 09:52:16 +020091{
92 if (codec->subtype_name)
93 talloc_free(codec->subtype_name);
94 if (codec->audio_name)
95 talloc_free(codec->audio_name);
Neels Hofmeyr667fa592019-08-08 21:59:01 +020096 *codec = (struct mgcp_rtp_codec){};
Philipp Maierbc0346e2018-06-07 09:52:16 +020097}
98
99/*! Initalize or reset codec information with default data.
100 * \param[out] conn related rtp-connection. */
101void mgcp_codec_reset_all(struct mgcp_conn_rtp *conn)
102{
Neels Hofmeyrce64f182019-08-08 22:07:31 +0200103 int i;
104 for (i = 0; i < conn->end.codecs_assigned; i++)
105 codec_free(&conn->end.codecs[i]);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200106 conn->end.codecs_assigned = 0;
107 conn->end.codec = NULL;
108}
109
Neels Hofmeyr667fa592019-08-08 21:59:01 +0200110/*! Add codec configuration depending on payload type and/or codec name. This
111 * function uses the input parameters to extrapolate the full codec information.
112 * \param[out] codec configuration (caller provided memory).
113 * \param[out] conn related rtp-connection.
114 * \param[in] payload_type codec type id (e.g. 3 for GSM, -1 when undefined).
115 * \param[in] audio_name audio codec name, in uppercase (e.g. "GSM/8000/1").
116 * \param[in] param optional codec parameters (set to NULL when unused).
117 * \returns 0 on success, -EINVAL on failure. */
118int 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 +0200119{
120 int rate;
121 int channels;
122 char audio_codec[64];
Neels Hofmeyr667fa592019-08-08 21:59:01 +0200123 struct mgcp_rtp_codec *codec;
124 unsigned int pt_offset = conn->end.codecs_assigned;
125 void *ctx = conn->conn;
126
127 /* The amount of codecs we can store is limited, make sure we do not
128 * overrun this limit. */
129 if (conn->end.codecs_assigned >= MGCP_MAX_CODECS)
130 return -EINVAL;
131
132 /* First unused entry */
133 codec = &conn->end.codecs[conn->end.codecs_assigned];
Philipp Maierbc0346e2018-06-07 09:52:16 +0200134
135 /* Initalize the codec struct with some default data to begin with */
136 codec_init(codec);
137
138 if (payload_type != PTYPE_UNDEFINED) {
139 /* Make sure we do not get any reserved or undefined type numbers */
140 /* See also: https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200141 if ((payload_type == 1 || payload_type == 2 || payload_type == 19)
142 || (payload_type >= 72 && payload_type <= 76)
143 || (payload_type >= 127)) {
144 LOGP(DLMGCP, LOGL_ERROR, "Cannot add codec, payload type number %d is reserved\n",
145 payload_type);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200146 goto error;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200147 }
Philipp Maierbc0346e2018-06-07 09:52:16 +0200148
149 codec->payload_type = payload_type;
150 }
151
152 /* When no audio name is given, we are forced to use the payload
153 * type to generate the audio name. This is only possible for
154 * non dynamic payload types, which are statically defined */
155 if (!audio_name) {
156 switch (payload_type) {
157 case 0:
158 audio_name = talloc_strdup(ctx, "PCMU/8000/1");
159 break;
160 case 3:
161 audio_name = talloc_strdup(ctx, "GSM/8000/1");
162 break;
163 case 8:
164 audio_name = talloc_strdup(ctx, "PCMA/8000/1");
165 break;
166 case 18:
167 audio_name = talloc_strdup(ctx, "G729/8000/1");
168 break;
169 default:
170 /* The given payload type is not known to us, or it
171 * it is a dynamic payload type for which we do not
172 * know the audio name. We must give up here */
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200173 LOGP(DLMGCP, LOGL_ERROR, "No audio codec name given, and payload type %d unknown\n",
174 payload_type);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200175 goto error;
176 }
177 }
178
179 /* Now we extract the codec subtype name, rate and channels. The latter
180 * two are optional. If they are not present we use the safe defaults
181 * above. */
Neels Hofmeyra468b0f2019-08-28 04:56:49 +0200182 if (strlen(audio_name) >= sizeof(audio_codec)) {
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200183 LOGP(DLMGCP, LOGL_ERROR, "Audio codec too long: %s\n", osmo_quote_str(audio_name, -1));
Philipp Maierbc0346e2018-06-07 09:52:16 +0200184 goto error;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200185 }
Philipp Maierbc0346e2018-06-07 09:52:16 +0200186 channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS;
187 rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200188 if (sscanf(audio_name, "%63[^/]/%d/%d", audio_codec, &rate, &channels) < 1) {
189 LOGP(DLMGCP, LOGL_ERROR, "Invalid audio codec: %s\n", osmo_quote_str(audio_name, -1));
Philipp Maierbc0346e2018-06-07 09:52:16 +0200190 goto error;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200191 }
Philipp Maierbc0346e2018-06-07 09:52:16 +0200192
193 /* Note: We only accept configurations with one audio channel! */
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200194 if (channels != 1) {
195 LOGP(DLMGCP, LOGL_ERROR, "Cannot handle audio codec with more than one channel: %s\n",
196 osmo_quote_str(audio_name, -1));
Philipp Maierbc0346e2018-06-07 09:52:16 +0200197 goto error;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200198 }
Philipp Maierbc0346e2018-06-07 09:52:16 +0200199
200 codec->rate = rate;
201 codec->channels = channels;
202 codec->subtype_name = talloc_strdup(ctx, audio_codec);
203 codec->audio_name = talloc_strdup(ctx, audio_name);
204 codec->payload_type = payload_type;
205
206 if (!strcmp(audio_codec, "G729")) {
207 codec->frame_duration_num = 10;
208 codec->frame_duration_den = 1000;
209 } else {
210 codec->frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM;
211 codec->frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN;
212 }
213
214 /* Derive the payload type if it is unknown */
215 if (codec->payload_type == PTYPE_UNDEFINED) {
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200216 /* TODO: This is semi dead code, see OS#4150 */
Philipp Maierbc0346e2018-06-07 09:52:16 +0200217
218 /* For the known codecs from the static range we restore
219 * the IANA or 3GPP assigned payload type number */
220 if (codec->rate == 8000 && codec->channels == 1) {
221 /* See also: https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
222 if (!strcmp(codec->subtype_name, "GSM"))
223 codec->payload_type = 3;
224 else if (!strcmp(codec->subtype_name, "PCMA"))
225 codec->payload_type = 8;
226 else if (!strcmp(codec->subtype_name, "PCMU"))
227 codec->payload_type = 0;
228 else if (!strcmp(codec->subtype_name, "G729"))
229 codec->payload_type = 18;
230
231 /* See also: 3GPP TS 48.103, chapter 5.4.2.2 RTP Payload
232 * Note: These are not fixed payload types as the IANA
233 * defined once, they still remain dymanic payload
234 * types, but with a payload type number preference. */
235 else if (!strcmp(codec->subtype_name, "GSM-EFR"))
236 codec->payload_type = 110;
237 else if (!strcmp(codec->subtype_name, "GSM-HR-08"))
238 codec->payload_type = 111;
239 else if (!strcmp(codec->subtype_name, "AMR"))
240 codec->payload_type = 112;
241 else if (!strcmp(codec->subtype_name, "AMR-WB"))
242 codec->payload_type = 113;
243 }
244
245 /* If we could not determine a payload type we assume that
246 * we are dealing with a codec from the dynamic range. We
247 * choose a fixed identifier from 96-109. (Note: normally,
248 * the dynamic payload type rante is from 96-127, but from
249 * 110 onwards 3gpp defines prefered codec types, which are
250 * also fixed, see above) */
251 if (codec->payload_type < 0) {
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200252 /* FIXME: pt_offset is completely unrelated and useless here, any of those numbers may already
253 * have been added to the codecs. Instead, there should be an iterator checking for an actually
254 * unused dynamic payload type number. */
Philipp Maierbc0346e2018-06-07 09:52:16 +0200255 codec->payload_type = 96 + pt_offset;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200256 if (codec->payload_type > 109) {
257 LOGP(DLMGCP, LOGL_ERROR, "Ran out of payload type numbers to assign dynamically\n");
Philipp Maierbc0346e2018-06-07 09:52:16 +0200258 goto error;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200259 }
Philipp Maierbc0346e2018-06-07 09:52:16 +0200260 }
261 }
262
Philipp Maier228e5912019-03-05 13:56:59 +0100263 /* Copy over optional codec parameters */
264 if (param) {
265 codec->param = *param;
266 codec->param_present = true;
267 } else
268 codec->param_present = false;
269
Neels Hofmeyr667fa592019-08-08 21:59:01 +0200270 conn->end.codecs_assigned++;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200271 return 0;
272error:
273 /* Make sure we leave a clean codec entry on error. */
Neels Hofmeyr667fa592019-08-08 21:59:01 +0200274 codec_free(codec);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200275 return -EINVAL;
276}
277
Philipp Maierbc0346e2018-06-07 09:52:16 +0200278/* Check if the given codec is applicable on the specified endpoint
279 * Helper function for mgcp_codec_decide() */
280static bool is_codec_compatible(const struct mgcp_endpoint *endp, const struct mgcp_rtp_codec *codec)
281{
282 char codec_name[64];
283
284 /* A codec name must be set, if not, this might mean that the codec
285 * (payload type) that was assigned is unknown to us so we must stop
286 * here. */
287 if (!codec->subtype_name)
288 return false;
289
290 /* We now extract the codec_name (letters before the /, e.g. "GSM"
291 * from the audio name that is stored in the trunk configuration.
292 * We do not compare to the full audio_name because we expect that
293 * "GSM", "GSM/8000" and "GSM/8000/1" are all compatible when the
294 * audio name of the codec is set to "GSM" */
Philipp Maier14b27a82020-06-02 20:15:30 +0200295 if (sscanf(endp->trunk->audio_name, "%63[^/]/%*d/%*d", codec_name) < 1)
Philipp Maierbc0346e2018-06-07 09:52:16 +0200296 return false;
297
298 /* Finally we check if the subtype_name we have generated from the
299 * audio_name in the trunc struct patches the codec_name of the
300 * given codec */
301 if (strcasecmp(codec_name, codec->subtype_name) == 0)
302 return true;
303
304 /* FIXME: It is questinable that the method to pick a compatible
Philipp Maier14b27a82020-06-02 20:15:30 +0200305 * codec can work properly. Since this useses trunk->audio_name, as
Philipp Maierbc0346e2018-06-07 09:52:16 +0200306 * a reference, which is set to "AMR/8000" permanently.
Philipp Maier14b27a82020-06-02 20:15:30 +0200307 * trunk->audio_name must be updated by the first connection that
Philipp Maierbc0346e2018-06-07 09:52:16 +0200308 * has been made on an endpoint, so that the second connection
309 * can make a meaningful decision here */
310
311 return false;
312}
313
314/*! Decide for one suitable codec
315 * \param[in] conn related rtp-connection.
316 * \returns 0 on success, -EINVAL on failure. */
317int mgcp_codec_decide(struct mgcp_conn_rtp *conn)
318{
319 struct mgcp_rtp_end *rtp;
320 unsigned int i;
321 struct mgcp_endpoint *endp;
322 bool codec_assigned = false;
323
324 endp = conn->conn->endp;
325 rtp = &conn->end;
326
327 /* This function works on the results the SDP/LCO parser has extracted
328 * from the MGCP message. The goal is to select a suitable codec for
329 * the given connection. When transcoding is available, the first codec
330 * from the codec list is taken without further checking. When
331 * transcoding is not available, then the choice must be made more
332 * carefully. Each codec in the list is checked until one is found that
333 * is rated compatible. The rating is done by the helper function
334 * is_codec_compatible(), which does the actual checking. */
335 for (i = 0; i < rtp->codecs_assigned; i++) {
336 /* When no transcoding is available, avoid codecs that would
337 * require transcoding. */
Philipp Maier14b27a82020-06-02 20:15:30 +0200338 if (endp->trunk->no_audio_transcoding && !is_codec_compatible(endp, &rtp->codecs[i])) {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200339 LOGP(DLMGCP, LOGL_NOTICE, "transcoding not available, skipping codec: %d/%s\n",
340 rtp->codecs[i].payload_type, rtp->codecs[i].subtype_name);
341 continue;
342 }
343
344 rtp->codec = &rtp->codecs[i];
345 codec_assigned = true;
346 break;
347 }
348
349 /* FIXME: To the reviewes: This is problematic. I do not get why we
350 * need to reset the packet_duration_ms depending on the codec
351 * selection. I thought it were all 20ms? Is this to address some
352 * cornercase. (This piece of code was in the code path before,
353 * together with the note: "TODO/XXX: Store this per codec and derive
354 * it on use" */
355 if (codec_assigned) {
356 if (rtp->maximum_packet_time >= 0
357 && rtp->maximum_packet_time * rtp->codec->frame_duration_den >
358 rtp->codec->frame_duration_num * 1500)
359 rtp->packet_duration_ms = 0;
360
361 return 0;
362 }
363
364 return -EINVAL;
365}
Philipp Maier6931f9a2018-07-26 09:29:31 +0200366
Neels Hofmeyr16b637b2019-08-08 22:47:10 +0200367/* Return true if octet-aligned is set in the given codec. Default to octet-aligned=0, i.e. bandwidth-efficient mode.
368 * See RFC4867 "RTP Payload Format for AMR and AMR-WB" sections "8.1. AMR Media Type Registration" and "8.2. AMR-WB
369 * Media Type Registration":
370 *
371 * octet-align: Permissible values are 0 and 1. If 1, octet-aligned
372 * operation SHALL be used. If 0 or if not present,
373 * bandwidth-efficient operation is employed.
374 *
375 * https://tools.ietf.org/html/rfc4867
376 */
377static bool amr_is_octet_aligned(const struct mgcp_rtp_codec *codec)
378{
379 if (!codec->param_present)
380 return false;
381 if (!codec->param.amr_octet_aligned_present)
382 return false;
383 return codec->param.amr_octet_aligned;
384}
385
Philipp Maier6931f9a2018-07-26 09:29:31 +0200386/* Compare two codecs, all parameters must match up, except for the payload type
387 * number. */
Neels Hofmeyr782d6072019-08-09 00:51:21 +0200388static bool codecs_same(struct mgcp_rtp_codec *codec_a, struct mgcp_rtp_codec *codec_b)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200389{
390 if (codec_a->rate != codec_b->rate)
391 return false;
392 if (codec_a->channels != codec_b->channels)
393 return false;
394 if (codec_a->frame_duration_num != codec_b->frame_duration_num)
395 return false;
396 if (codec_a->frame_duration_den != codec_b->frame_duration_den)
397 return false;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200398 if (strcmp(codec_a->subtype_name, codec_b->subtype_name))
399 return false;
Neels Hofmeyr16b637b2019-08-08 22:47:10 +0200400 if (!strcmp(codec_a->subtype_name, "AMR")) {
401 if (amr_is_octet_aligned(codec_a) != amr_is_octet_aligned(codec_b))
402 return false;
403 }
Philipp Maier6931f9a2018-07-26 09:29:31 +0200404
405 return true;
406}
407
408/*! Translate a given payload type number that belongs to the packet of a
409 * source connection to the equivalent payload type number that matches the
410 * configuration of a destination connection.
411 * \param[in] conn_src related source rtp-connection.
412 * \param[in] conn_dst related destination rtp-connection.
413 * \param[in] payload_type number from the source packet or source connection.
414 * \returns translated payload type number on success, -EINVAL on failure. */
415int mgcp_codec_pt_translate(struct mgcp_conn_rtp *conn_src, struct mgcp_conn_rtp *conn_dst, int payload_type)
416{
417 struct mgcp_rtp_end *rtp_src;
418 struct mgcp_rtp_end *rtp_dst;
419 struct mgcp_rtp_codec *codec_src = NULL;
420 struct mgcp_rtp_codec *codec_dst = NULL;
421 unsigned int i;
422 unsigned int codecs_assigned;
423
424 rtp_src = &conn_src->end;
425 rtp_dst = &conn_dst->end;
426
427 /* Find the codec information that is used on the source side */
428 codecs_assigned = rtp_src->codecs_assigned;
429 OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
430 for (i = 0; i < codecs_assigned; i++) {
431 if (payload_type == rtp_src->codecs[i].payload_type) {
432 codec_src = &rtp_src->codecs[i];
433 break;
434 }
435 }
436 if (!codec_src)
437 return -EINVAL;
438
439 /* Use the codec infrmation from the source and try to find the
440 * equivalent of it on the destination side */
441 codecs_assigned = rtp_dst->codecs_assigned;
442 OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
443 for (i = 0; i < codecs_assigned; i++) {
Neels Hofmeyr782d6072019-08-09 00:51:21 +0200444 if (codecs_same(codec_src, &rtp_dst->codecs[i])) {
Philipp Maier6931f9a2018-07-26 09:29:31 +0200445 codec_dst = &rtp_dst->codecs[i];
446 break;
447 }
448 }
449 if (!codec_dst)
450 return -EINVAL;
451
452 return codec_dst->payload_type;
453}