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