blob: e9e2c62e0bbfd97a9e338f5e9884ce524caf241d [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. */
79void codec_init(struct mgcp_rtp_codec *codec)
80{
81 if (codec->subtype_name)
82 talloc_free(codec->subtype_name);
83 if (codec->audio_name)
84 talloc_free(codec->audio_name);
85 memset(codec, 0, sizeof(*codec));
86 codec->payload_type = -1;
87 codec->frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM;
88 codec->frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN;
89 codec->rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE;
90 codec->channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS;
91}
92
93/*! Initalize or reset codec information with default data.
94 * \param[out] conn related rtp-connection. */
95void mgcp_codec_reset_all(struct mgcp_conn_rtp *conn)
96{
97 memset(conn->end.codecs, 0, sizeof(conn->end.codecs));
98 conn->end.codecs_assigned = 0;
99 conn->end.codec = NULL;
100}
101
Pau Espin Pedrol83fd8a52019-06-26 12:55:26 +0200102/* Set members of struct mgcp_rtp_codec, extrapolate in missing information. Param audio_name is expected in uppercase. */
Philipp Maier228e5912019-03-05 13:56:59 +0100103static int codec_set(void *ctx, struct mgcp_rtp_codec *codec, int payload_type, const char *audio_name,
104 unsigned int pt_offset, struct mgcp_codec_param *param)
Philipp Maierbc0346e2018-06-07 09:52:16 +0200105{
106 int rate;
107 int channels;
108 char audio_codec[64];
109
110 /* Initalize the codec struct with some default data to begin with */
111 codec_init(codec);
112
113 if (payload_type != PTYPE_UNDEFINED) {
114 /* Make sure we do not get any reserved or undefined type numbers */
115 /* See also: https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
116 if (payload_type == 1 || payload_type == 2 || payload_type == 19)
117 goto error;
118 if (payload_type >= 72 && payload_type <= 76)
119 goto error;
120 if (payload_type >= 127)
121 goto error;
122
123 codec->payload_type = payload_type;
124 }
125
126 /* When no audio name is given, we are forced to use the payload
127 * type to generate the audio name. This is only possible for
128 * non dynamic payload types, which are statically defined */
129 if (!audio_name) {
130 switch (payload_type) {
131 case 0:
132 audio_name = talloc_strdup(ctx, "PCMU/8000/1");
133 break;
134 case 3:
135 audio_name = talloc_strdup(ctx, "GSM/8000/1");
136 break;
137 case 8:
138 audio_name = talloc_strdup(ctx, "PCMA/8000/1");
139 break;
140 case 18:
141 audio_name = talloc_strdup(ctx, "G729/8000/1");
142 break;
143 default:
144 /* The given payload type is not known to us, or it
145 * it is a dynamic payload type for which we do not
146 * know the audio name. We must give up here */
147 goto error;
148 }
149 }
150
151 /* Now we extract the codec subtype name, rate and channels. The latter
152 * two are optional. If they are not present we use the safe defaults
153 * above. */
154 if (strlen(audio_name) > sizeof(audio_codec))
155 goto error;
156 channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS;
157 rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE;
158 if (sscanf(audio_name, "%63[^/]/%d/%d", audio_codec, &rate, &channels) < 1)
159 goto error;
160
161 /* Note: We only accept configurations with one audio channel! */
162 if (channels != 1)
163 goto error;
164
165 codec->rate = rate;
166 codec->channels = channels;
167 codec->subtype_name = talloc_strdup(ctx, audio_codec);
168 codec->audio_name = talloc_strdup(ctx, audio_name);
169 codec->payload_type = payload_type;
170
171 if (!strcmp(audio_codec, "G729")) {
172 codec->frame_duration_num = 10;
173 codec->frame_duration_den = 1000;
174 } else {
175 codec->frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM;
176 codec->frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN;
177 }
178
179 /* Derive the payload type if it is unknown */
180 if (codec->payload_type == PTYPE_UNDEFINED) {
181
182 /* For the known codecs from the static range we restore
183 * the IANA or 3GPP assigned payload type number */
184 if (codec->rate == 8000 && codec->channels == 1) {
185 /* See also: https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
186 if (!strcmp(codec->subtype_name, "GSM"))
187 codec->payload_type = 3;
188 else if (!strcmp(codec->subtype_name, "PCMA"))
189 codec->payload_type = 8;
190 else if (!strcmp(codec->subtype_name, "PCMU"))
191 codec->payload_type = 0;
192 else if (!strcmp(codec->subtype_name, "G729"))
193 codec->payload_type = 18;
194
195 /* See also: 3GPP TS 48.103, chapter 5.4.2.2 RTP Payload
196 * Note: These are not fixed payload types as the IANA
197 * defined once, they still remain dymanic payload
198 * types, but with a payload type number preference. */
199 else if (!strcmp(codec->subtype_name, "GSM-EFR"))
200 codec->payload_type = 110;
201 else if (!strcmp(codec->subtype_name, "GSM-HR-08"))
202 codec->payload_type = 111;
203 else if (!strcmp(codec->subtype_name, "AMR"))
204 codec->payload_type = 112;
205 else if (!strcmp(codec->subtype_name, "AMR-WB"))
206 codec->payload_type = 113;
207 }
208
209 /* If we could not determine a payload type we assume that
210 * we are dealing with a codec from the dynamic range. We
211 * choose a fixed identifier from 96-109. (Note: normally,
212 * the dynamic payload type rante is from 96-127, but from
213 * 110 onwards 3gpp defines prefered codec types, which are
214 * also fixed, see above) */
215 if (codec->payload_type < 0) {
216 codec->payload_type = 96 + pt_offset;
217 if (codec->payload_type > 109)
218 goto error;
219 }
220 }
221
Philipp Maier228e5912019-03-05 13:56:59 +0100222 /* Copy over optional codec parameters */
223 if (param) {
224 codec->param = *param;
225 codec->param_present = true;
226 } else
227 codec->param_present = false;
228
Philipp Maierbc0346e2018-06-07 09:52:16 +0200229 return 0;
230error:
231 /* Make sure we leave a clean codec entry on error. */
232 codec_init(codec);
233 memset(codec, 0, sizeof(*codec));
234 return -EINVAL;
235}
236
237/*! Add codec configuration depending on payload type and/or codec name. This
238 * function uses the input parameters to extrapolate the full codec information.
239 * \param[out] codec configuration (caller provided memory).
240 * \param[out] conn related rtp-connection.
241 * \param[in] payload_type codec type id (e.g. 3 for GSM, -1 when undefined).
Pau Espin Pedrol83fd8a52019-06-26 12:55:26 +0200242 * \param[in] audio_name audio codec name, in uppercase (e.g. "GSM/8000/1").
Philipp Maier228e5912019-03-05 13:56:59 +0100243 * \param[in] param optional codec parameters (set to NULL when unused).
Philipp Maierbc0346e2018-06-07 09:52:16 +0200244 * \returns 0 on success, -EINVAL on failure. */
Philipp Maier228e5912019-03-05 13:56:59 +0100245int mgcp_codec_add(struct mgcp_conn_rtp *conn, int payload_type, const char *audio_name, struct mgcp_codec_param *param)
Philipp Maierbc0346e2018-06-07 09:52:16 +0200246{
247 int rc;
248
249 /* The amount of codecs we can store is limited, make sure we do not
250 * overrun this limit. */
251 if (conn->end.codecs_assigned >= MGCP_MAX_CODECS)
252 return -EINVAL;
253
254 rc = codec_set(conn->conn, &conn->end.codecs[conn->end.codecs_assigned], payload_type, audio_name,
Philipp Maier228e5912019-03-05 13:56:59 +0100255 conn->end.codecs_assigned, param);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200256 if (rc != 0)
257 return -EINVAL;
258
259 conn->end.codecs_assigned++;
260
261 return 0;
262}
263
264/* Check if the given codec is applicable on the specified endpoint
265 * Helper function for mgcp_codec_decide() */
266static bool is_codec_compatible(const struct mgcp_endpoint *endp, const struct mgcp_rtp_codec *codec)
267{
268 char codec_name[64];
269
270 /* A codec name must be set, if not, this might mean that the codec
271 * (payload type) that was assigned is unknown to us so we must stop
272 * here. */
273 if (!codec->subtype_name)
274 return false;
275
276 /* We now extract the codec_name (letters before the /, e.g. "GSM"
277 * from the audio name that is stored in the trunk configuration.
278 * We do not compare to the full audio_name because we expect that
279 * "GSM", "GSM/8000" and "GSM/8000/1" are all compatible when the
280 * audio name of the codec is set to "GSM" */
281 if (sscanf(endp->tcfg->audio_name, "%63[^/]/%*d/%*d", codec_name) < 1)
282 return false;
283
284 /* Finally we check if the subtype_name we have generated from the
285 * audio_name in the trunc struct patches the codec_name of the
286 * given codec */
287 if (strcasecmp(codec_name, codec->subtype_name) == 0)
288 return true;
289
290 /* FIXME: It is questinable that the method to pick a compatible
291 * codec can work properly. Since this useses tcfg->audio_name, as
292 * a reference, which is set to "AMR/8000" permanently.
293 * tcfg->audio_name must be updated by the first connection that
294 * has been made on an endpoint, so that the second connection
295 * can make a meaningful decision here */
296
297 return false;
298}
299
300/*! Decide for one suitable codec
301 * \param[in] conn related rtp-connection.
302 * \returns 0 on success, -EINVAL on failure. */
303int mgcp_codec_decide(struct mgcp_conn_rtp *conn)
304{
305 struct mgcp_rtp_end *rtp;
306 unsigned int i;
307 struct mgcp_endpoint *endp;
308 bool codec_assigned = false;
309
310 endp = conn->conn->endp;
311 rtp = &conn->end;
312
313 /* This function works on the results the SDP/LCO parser has extracted
314 * from the MGCP message. The goal is to select a suitable codec for
315 * the given connection. When transcoding is available, the first codec
316 * from the codec list is taken without further checking. When
317 * transcoding is not available, then the choice must be made more
318 * carefully. Each codec in the list is checked until one is found that
319 * is rated compatible. The rating is done by the helper function
320 * is_codec_compatible(), which does the actual checking. */
321 for (i = 0; i < rtp->codecs_assigned; i++) {
322 /* When no transcoding is available, avoid codecs that would
323 * require transcoding. */
324 if (endp->tcfg->no_audio_transcoding && !is_codec_compatible(endp, &rtp->codecs[i])) {
325 LOGP(DLMGCP, LOGL_NOTICE, "transcoding not available, skipping codec: %d/%s\n",
326 rtp->codecs[i].payload_type, rtp->codecs[i].subtype_name);
327 continue;
328 }
329
330 rtp->codec = &rtp->codecs[i];
331 codec_assigned = true;
332 break;
333 }
334
335 /* FIXME: To the reviewes: This is problematic. I do not get why we
336 * need to reset the packet_duration_ms depending on the codec
337 * selection. I thought it were all 20ms? Is this to address some
338 * cornercase. (This piece of code was in the code path before,
339 * together with the note: "TODO/XXX: Store this per codec and derive
340 * it on use" */
341 if (codec_assigned) {
342 if (rtp->maximum_packet_time >= 0
343 && rtp->maximum_packet_time * rtp->codec->frame_duration_den >
344 rtp->codec->frame_duration_num * 1500)
345 rtp->packet_duration_ms = 0;
346
347 return 0;
348 }
349
350 return -EINVAL;
351}
Philipp Maier6931f9a2018-07-26 09:29:31 +0200352
353/* Compare two codecs, all parameters must match up, except for the payload type
354 * number. */
Neels Hofmeyr782d6072019-08-09 00:51:21 +0200355static bool codecs_same(struct mgcp_rtp_codec *codec_a, struct mgcp_rtp_codec *codec_b)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200356{
357 if (codec_a->rate != codec_b->rate)
358 return false;
359 if (codec_a->channels != codec_b->channels)
360 return false;
361 if (codec_a->frame_duration_num != codec_b->frame_duration_num)
362 return false;
363 if (codec_a->frame_duration_den != codec_b->frame_duration_den)
364 return false;
365 if (strcmp(codec_a->audio_name, codec_b->audio_name))
366 return false;
367 if (strcmp(codec_a->subtype_name, codec_b->subtype_name))
368 return false;
369
370 return true;
371}
372
373/*! Translate a given payload type number that belongs to the packet of a
374 * source connection to the equivalent payload type number that matches the
375 * configuration of a destination connection.
376 * \param[in] conn_src related source rtp-connection.
377 * \param[in] conn_dst related destination rtp-connection.
378 * \param[in] payload_type number from the source packet or source connection.
379 * \returns translated payload type number on success, -EINVAL on failure. */
380int mgcp_codec_pt_translate(struct mgcp_conn_rtp *conn_src, struct mgcp_conn_rtp *conn_dst, int payload_type)
381{
382 struct mgcp_rtp_end *rtp_src;
383 struct mgcp_rtp_end *rtp_dst;
384 struct mgcp_rtp_codec *codec_src = NULL;
385 struct mgcp_rtp_codec *codec_dst = NULL;
386 unsigned int i;
387 unsigned int codecs_assigned;
388
389 rtp_src = &conn_src->end;
390 rtp_dst = &conn_dst->end;
391
392 /* Find the codec information that is used on the source side */
393 codecs_assigned = rtp_src->codecs_assigned;
394 OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
395 for (i = 0; i < codecs_assigned; i++) {
396 if (payload_type == rtp_src->codecs[i].payload_type) {
397 codec_src = &rtp_src->codecs[i];
398 break;
399 }
400 }
401 if (!codec_src)
402 return -EINVAL;
403
404 /* Use the codec infrmation from the source and try to find the
405 * equivalent of it on the destination side */
406 codecs_assigned = rtp_dst->codecs_assigned;
407 OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
408 for (i = 0; i < codecs_assigned; i++) {
Neels Hofmeyr782d6072019-08-09 00:51:21 +0200409 if (codecs_same(codec_src, &rtp_dst->codecs[i])) {
Philipp Maier6931f9a2018-07-26 09:29:31 +0200410 codec_dst = &rtp_dst->codecs[i];
411 break;
412 }
413 }
414 if (!codec_dst)
415 return -EINVAL;
416
417 return codec_dst->payload_type;
418}