blob: c2513170504c6433cc080da9c7217f8a136535cf [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>
Philipp Maierc66ab2c2020-06-02 20:55:34 +020022#include <osmocom/mgcp/mgcp_trunk.h>
Philipp Maierbc0346e2018-06-07 09:52:16 +020023#include <errno.h>
24
25/* Helper function to dump codec information of a specified codec to a printable
26 * string, used by dump_codec_summary() */
27static char *dump_codec(struct mgcp_rtp_codec *codec)
28{
29 static char str[256];
30 char *pt_str;
31
32 if (codec->payload_type > 76)
33 pt_str = "DYNAMIC";
34 else if (codec->payload_type > 72)
35 pt_str = "RESERVED <!>";
36 else if (codec->payload_type != PTYPE_UNDEFINED)
37 pt_str = codec->subtype_name;
38 else
39 pt_str = "INVALID <!>";
40
41 snprintf(str, sizeof(str), "(pt:%i=%s, audio:%s subt=%s, rate=%u, ch=%i, t=%u/%u)", codec->payload_type, pt_str,
42 codec->audio_name, codec->subtype_name, codec->rate, codec->channels, codec->frame_duration_num,
43 codec->frame_duration_den);
44 return str;
45}
46
47/*! Dump a summary of all negotiated codecs to debug log
48 * \param[in] conn related rtp-connection. */
49void mgcp_codec_summary(struct mgcp_conn_rtp *conn)
50{
51 struct mgcp_rtp_end *rtp;
52 unsigned int i;
53 struct mgcp_rtp_codec *codec;
54 struct mgcp_endpoint *endp;
55
56 rtp = &conn->end;
57 endp = conn->conn->endp;
58
59 if (rtp->codecs_assigned == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +020060 LOGPENDP(endp, DLMGCP, LOGL_ERROR, "conn:%s no codecs available\n",
61 mgcp_conn_dump(conn->conn));
Philipp Maierbc0346e2018-06-07 09:52:16 +020062 return;
63 }
64
65 /* Store parsed codec information */
66 for (i = 0; i < rtp->codecs_assigned; i++) {
67 codec = &rtp->codecs[i];
68
Pau Espin Pedrol3239f622019-04-24 18:47:46 +020069 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "conn:%s codecs[%u]:%s",
70 mgcp_conn_dump(conn->conn), i, dump_codec(codec));
Philipp Maierbc0346e2018-06-07 09:52:16 +020071
72 if (codec == rtp->codec)
73 LOGPC(DLMGCP, LOGL_DEBUG, " [selected]");
74
75 LOGPC(DLMGCP, LOGL_DEBUG, "\n");
76 }
77}
78
79/* Initalize or reset codec information with default data. */
Neels Hofmeyr667fa592019-08-08 21:59:01 +020080static void codec_init(struct mgcp_rtp_codec *codec)
81{
82 *codec = (struct mgcp_rtp_codec){
83 .payload_type = -1,
84 .frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM,
85 .frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN,
86 .rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE,
87 .channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS,
88 };
89}
90
91static void codec_free(struct mgcp_rtp_codec *codec)
Philipp Maierbc0346e2018-06-07 09:52:16 +020092{
93 if (codec->subtype_name)
94 talloc_free(codec->subtype_name);
95 if (codec->audio_name)
96 talloc_free(codec->audio_name);
Neels Hofmeyr667fa592019-08-08 21:59:01 +020097 *codec = (struct mgcp_rtp_codec){};
Philipp Maierbc0346e2018-06-07 09:52:16 +020098}
99
100/*! Initalize or reset codec information with default data.
101 * \param[out] conn related rtp-connection. */
102void mgcp_codec_reset_all(struct mgcp_conn_rtp *conn)
103{
Neels Hofmeyrce64f182019-08-08 22:07:31 +0200104 int i;
105 for (i = 0; i < conn->end.codecs_assigned; i++)
106 codec_free(&conn->end.codecs[i]);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200107 conn->end.codecs_assigned = 0;
108 conn->end.codec = NULL;
109}
110
Neels Hofmeyr667fa592019-08-08 21:59:01 +0200111/*! Add codec configuration depending on payload type and/or codec name. This
112 * function uses the input parameters to extrapolate the full codec information.
113 * \param[out] codec configuration (caller provided memory).
114 * \param[out] conn related rtp-connection.
115 * \param[in] payload_type codec type id (e.g. 3 for GSM, -1 when undefined).
116 * \param[in] audio_name audio codec name, in uppercase (e.g. "GSM/8000/1").
117 * \param[in] param optional codec parameters (set to NULL when unused).
118 * \returns 0 on success, -EINVAL on failure. */
119int 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 +0200120{
121 int rate;
122 int channels;
123 char audio_codec[64];
Neels Hofmeyr667fa592019-08-08 21:59:01 +0200124 struct mgcp_rtp_codec *codec;
125 unsigned int pt_offset = conn->end.codecs_assigned;
126 void *ctx = conn->conn;
127
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:
159 audio_name = talloc_strdup(ctx, "PCMU/8000/1");
160 break;
161 case 3:
162 audio_name = talloc_strdup(ctx, "GSM/8000/1");
163 break;
164 case 8:
165 audio_name = talloc_strdup(ctx, "PCMA/8000/1");
166 break;
167 case 18:
168 audio_name = talloc_strdup(ctx, "G729/8000/1");
169 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 }
178 }
179
180 /* Now we extract the codec subtype name, rate and channels. The latter
181 * two are optional. If they are not present we use the safe defaults
182 * above. */
Neels Hofmeyra468b0f2019-08-28 04:56:49 +0200183 if (strlen(audio_name) >= sizeof(audio_codec)) {
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200184 LOGP(DLMGCP, LOGL_ERROR, "Audio codec too long: %s\n", osmo_quote_str(audio_name, -1));
Philipp Maierbc0346e2018-06-07 09:52:16 +0200185 goto error;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200186 }
Philipp Maierbc0346e2018-06-07 09:52:16 +0200187 channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS;
188 rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200189 if (sscanf(audio_name, "%63[^/]/%d/%d", audio_codec, &rate, &channels) < 1) {
190 LOGP(DLMGCP, LOGL_ERROR, "Invalid audio codec: %s\n", osmo_quote_str(audio_name, -1));
Philipp Maierbc0346e2018-06-07 09:52:16 +0200191 goto error;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200192 }
Philipp Maierbc0346e2018-06-07 09:52:16 +0200193
194 /* Note: We only accept configurations with one audio channel! */
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200195 if (channels != 1) {
196 LOGP(DLMGCP, LOGL_ERROR, "Cannot handle audio codec with more than one channel: %s\n",
197 osmo_quote_str(audio_name, -1));
Philipp Maierbc0346e2018-06-07 09:52:16 +0200198 goto error;
Neels Hofmeyrb0cfa722019-08-08 23:39:56 +0200199 }
Philipp Maierbc0346e2018-06-07 09:52:16 +0200200
201 codec->rate = rate;
202 codec->channels = channels;
203 codec->subtype_name = talloc_strdup(ctx, audio_codec);
204 codec->audio_name = talloc_strdup(ctx, audio_name);
205 codec->payload_type = payload_type;
206
207 if (!strcmp(audio_codec, "G729")) {
208 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{
283 char codec_name[64];
284
285 /* A codec name must be set, if not, this might mean that the codec
286 * (payload type) that was assigned is unknown to us so we must stop
287 * here. */
288 if (!codec->subtype_name)
289 return false;
290
291 /* We now extract the codec_name (letters before the /, e.g. "GSM"
292 * from the audio name that is stored in the trunk configuration.
293 * We do not compare to the full audio_name because we expect that
294 * "GSM", "GSM/8000" and "GSM/8000/1" are all compatible when the
295 * audio name of the codec is set to "GSM" */
Philipp Maier14b27a82020-06-02 20:15:30 +0200296 if (sscanf(endp->trunk->audio_name, "%63[^/]/%*d/%*d", codec_name) < 1)
Philipp Maierbc0346e2018-06-07 09:52:16 +0200297 return false;
298
299 /* Finally we check if the subtype_name we have generated from the
300 * audio_name in the trunc struct patches the codec_name of the
301 * given codec */
302 if (strcasecmp(codec_name, codec->subtype_name) == 0)
303 return true;
304
305 /* FIXME: It is questinable that the method to pick a compatible
Philipp Maier14b27a82020-06-02 20:15:30 +0200306 * codec can work properly. Since this useses trunk->audio_name, as
Philipp Maierbc0346e2018-06-07 09:52:16 +0200307 * a reference, which is set to "AMR/8000" permanently.
Philipp Maier14b27a82020-06-02 20:15:30 +0200308 * trunk->audio_name must be updated by the first connection that
Philipp Maierbc0346e2018-06-07 09:52:16 +0200309 * has been made on an endpoint, so that the second connection
310 * can make a meaningful decision here */
311
312 return false;
313}
314
315/*! Decide for one suitable codec
316 * \param[in] conn related rtp-connection.
317 * \returns 0 on success, -EINVAL on failure. */
318int mgcp_codec_decide(struct mgcp_conn_rtp *conn)
319{
320 struct mgcp_rtp_end *rtp;
321 unsigned int i;
322 struct mgcp_endpoint *endp;
323 bool codec_assigned = false;
324
325 endp = conn->conn->endp;
326 rtp = &conn->end;
327
328 /* This function works on the results the SDP/LCO parser has extracted
329 * from the MGCP message. The goal is to select a suitable codec for
330 * the given connection. When transcoding is available, the first codec
331 * from the codec list is taken without further checking. When
332 * transcoding is not available, then the choice must be made more
333 * carefully. Each codec in the list is checked until one is found that
334 * is rated compatible. The rating is done by the helper function
335 * is_codec_compatible(), which does the actual checking. */
336 for (i = 0; i < rtp->codecs_assigned; i++) {
337 /* When no transcoding is available, avoid codecs that would
338 * require transcoding. */
Philipp Maier14b27a82020-06-02 20:15:30 +0200339 if (endp->trunk->no_audio_transcoding && !is_codec_compatible(endp, &rtp->codecs[i])) {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200340 LOGP(DLMGCP, LOGL_NOTICE, "transcoding not available, skipping codec: %d/%s\n",
341 rtp->codecs[i].payload_type, rtp->codecs[i].subtype_name);
342 continue;
343 }
344
345 rtp->codec = &rtp->codecs[i];
346 codec_assigned = true;
347 break;
348 }
349
350 /* FIXME: To the reviewes: This is problematic. I do not get why we
351 * need to reset the packet_duration_ms depending on the codec
352 * selection. I thought it were all 20ms? Is this to address some
353 * cornercase. (This piece of code was in the code path before,
354 * together with the note: "TODO/XXX: Store this per codec and derive
355 * it on use" */
356 if (codec_assigned) {
357 if (rtp->maximum_packet_time >= 0
358 && rtp->maximum_packet_time * rtp->codec->frame_duration_den >
359 rtp->codec->frame_duration_num * 1500)
360 rtp->packet_duration_ms = 0;
361
362 return 0;
363 }
364
365 return -EINVAL;
366}
Philipp Maier6931f9a2018-07-26 09:29:31 +0200367
Neels Hofmeyr16b637b2019-08-08 22:47:10 +0200368/* Return true if octet-aligned is set in the given codec. Default to octet-aligned=0, i.e. bandwidth-efficient mode.
369 * See RFC4867 "RTP Payload Format for AMR and AMR-WB" sections "8.1. AMR Media Type Registration" and "8.2. AMR-WB
370 * Media Type Registration":
371 *
372 * octet-align: Permissible values are 0 and 1. If 1, octet-aligned
373 * operation SHALL be used. If 0 or if not present,
374 * bandwidth-efficient operation is employed.
375 *
376 * https://tools.ietf.org/html/rfc4867
377 */
378static bool amr_is_octet_aligned(const struct mgcp_rtp_codec *codec)
379{
380 if (!codec->param_present)
381 return false;
382 if (!codec->param.amr_octet_aligned_present)
383 return false;
384 return codec->param.amr_octet_aligned;
385}
386
Philipp Maier6931f9a2018-07-26 09:29:31 +0200387/* Compare two codecs, all parameters must match up, except for the payload type
388 * number. */
Neels Hofmeyr782d6072019-08-09 00:51:21 +0200389static bool codecs_same(struct mgcp_rtp_codec *codec_a, struct mgcp_rtp_codec *codec_b)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200390{
391 if (codec_a->rate != codec_b->rate)
392 return false;
393 if (codec_a->channels != codec_b->channels)
394 return false;
395 if (codec_a->frame_duration_num != codec_b->frame_duration_num)
396 return false;
397 if (codec_a->frame_duration_den != codec_b->frame_duration_den)
398 return false;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200399 if (strcmp(codec_a->subtype_name, codec_b->subtype_name))
400 return false;
Neels Hofmeyr16b637b2019-08-08 22:47:10 +0200401 if (!strcmp(codec_a->subtype_name, "AMR")) {
402 if (amr_is_octet_aligned(codec_a) != amr_is_octet_aligned(codec_b))
403 return false;
404 }
Philipp Maier6931f9a2018-07-26 09:29:31 +0200405
406 return true;
407}
408
409/*! Translate a given payload type number that belongs to the packet of a
410 * source connection to the equivalent payload type number that matches the
411 * configuration of a destination connection.
412 * \param[in] conn_src related source rtp-connection.
413 * \param[in] conn_dst related destination rtp-connection.
414 * \param[in] payload_type number from the source packet or source connection.
415 * \returns translated payload type number on success, -EINVAL on failure. */
416int mgcp_codec_pt_translate(struct mgcp_conn_rtp *conn_src, struct mgcp_conn_rtp *conn_dst, int payload_type)
417{
418 struct mgcp_rtp_end *rtp_src;
419 struct mgcp_rtp_end *rtp_dst;
420 struct mgcp_rtp_codec *codec_src = NULL;
421 struct mgcp_rtp_codec *codec_dst = NULL;
422 unsigned int i;
423 unsigned int codecs_assigned;
424
425 rtp_src = &conn_src->end;
426 rtp_dst = &conn_dst->end;
427
428 /* Find the codec information that is used on the source side */
429 codecs_assigned = rtp_src->codecs_assigned;
430 OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
431 for (i = 0; i < codecs_assigned; i++) {
432 if (payload_type == rtp_src->codecs[i].payload_type) {
433 codec_src = &rtp_src->codecs[i];
434 break;
435 }
436 }
437 if (!codec_src)
438 return -EINVAL;
439
440 /* Use the codec infrmation from the source and try to find the
441 * equivalent of it on the destination side */
442 codecs_assigned = rtp_dst->codecs_assigned;
443 OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
444 for (i = 0; i < codecs_assigned; i++) {
Neels Hofmeyr782d6072019-08-09 00:51:21 +0200445 if (codecs_same(codec_src, &rtp_dst->codecs[i])) {
Philipp Maier6931f9a2018-07-26 09:29:31 +0200446 codec_dst = &rtp_dst->codecs[i];
447 break;
448 }
449 }
450 if (!codec_dst)
451 return -EINVAL;
452
453 return codec_dst->payload_type;
454}