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