blob: e6bf9c209dec1ed144fe743e24eb49004051fd4b [file] [log] [blame]
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001/*
2 * Some SDP file parsing...
3 *
4 * (C) 2009-2015 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2009-2014 by On-Waves
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
Philipp Maier8970c492017-10-11 13:33:42 +020023#include <osmocom/core/msgb.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020024#include <osmocom/mgcp/mgcp.h>
25#include <osmocom/mgcp/mgcp_internal.h>
26#include <osmocom/mgcp/mgcp_msg.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020027
28#include <errno.h>
29
30struct sdp_rtp_map {
31 /* the type */
32 int payload_type;
33 /* null, static or later dynamic codec name */
34 char *codec_name;
35 /* A pointer to the original line for later parsing */
36 char *map_line;
37
38 int rate;
39 int channels;
40};
41
Philipp Maier8970c492017-10-11 13:33:42 +020042/*! Set codec configuration depending on payload type and codec name.
43 * \param[in] ctx talloc context.
44 * \param[out] codec configuration (caller provided memory).
45 * \param[in] payload_type codec type id (e.g. 3 for GSM, -1 when undefined).
46 * \param[in] audio_name audio codec name (e.g. "GSM/8000/1").
47 * \returns 0 on success, -1 on failure. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020048int mgcp_set_audio_info(void *ctx, struct mgcp_rtp_codec *codec,
49 int payload_type, const char *audio_name)
50{
51 int rate = codec->rate;
52 int channels = codec->channels;
53 char audio_codec[64];
54
55 talloc_free(codec->subtype_name);
56 codec->subtype_name = NULL;
57 talloc_free(codec->audio_name);
58 codec->audio_name = NULL;
59
60 if (payload_type != PTYPE_UNDEFINED)
61 codec->payload_type = payload_type;
62
63 if (!audio_name) {
64 switch (payload_type) {
65 case 0: audio_name = "PCMU/8000/1"; break;
66 case 3: audio_name = "GSM/8000/1"; break;
67 case 8: audio_name = "PCMA/8000/1"; break;
68 case 18: audio_name = "G729/8000/1"; break;
69 default:
70 /* Payload type is unknown, don't change rate and
71 * channels. */
72 /* TODO: return value? */
73 return 0;
74 }
75 }
76
77 if (sscanf(audio_name, "%63[^/]/%d/%d",
78 audio_codec, &rate, &channels) < 1)
79 return -EINVAL;
80
81 codec->rate = rate;
82 codec->channels = channels;
83 codec->subtype_name = talloc_strdup(ctx, audio_codec);
84 codec->audio_name = talloc_strdup(ctx, audio_name);
85
86 if (!strcmp(audio_codec, "G729")) {
87 codec->frame_duration_num = 10;
88 codec->frame_duration_den = 1000;
89 } else {
90 codec->frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM;
91 codec->frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN;
92 }
93
94 if (payload_type < 0) {
95 payload_type = 96;
96 if (rate == 8000 && channels == 1) {
97 if (!strcmp(audio_codec, "GSM"))
98 payload_type = 3;
99 else if (!strcmp(audio_codec, "PCMA"))
100 payload_type = 8;
101 else if (!strcmp(audio_codec, "PCMU"))
102 payload_type = 0;
103 else if (!strcmp(audio_codec, "G729"))
104 payload_type = 18;
105 }
106
107 codec->payload_type = payload_type;
108 }
109
110 if (channels != 1)
111 LOGP(DLMGCP, LOGL_NOTICE,
112 "Channels != 1 in SDP: '%s'\n", audio_name);
113
114 return 0;
115}
116
Philipp Maier8970c492017-10-11 13:33:42 +0200117static void codecs_initialize(void *ctx, struct sdp_rtp_map *codecs, int used)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200118{
119 int i;
120
121 for (i = 0; i < used; ++i) {
122 switch (codecs[i].payload_type) {
123 case 0:
124 codecs[i].codec_name = "PCMU";
125 codecs[i].rate = 8000;
126 codecs[i].channels = 1;
127 break;
128 case 3:
129 codecs[i].codec_name = "GSM";
130 codecs[i].rate = 8000;
131 codecs[i].channels = 1;
132 break;
133 case 8:
134 codecs[i].codec_name = "PCMA";
135 codecs[i].rate = 8000;
136 codecs[i].channels = 1;
137 break;
138 case 18:
139 codecs[i].codec_name = "G729";
140 codecs[i].rate = 8000;
141 codecs[i].channels = 1;
142 break;
143 }
144 }
145}
146
Philipp Maier8970c492017-10-11 13:33:42 +0200147static void codecs_update(void *ctx, struct sdp_rtp_map *codecs, int used,
148 int payload, const char *audio_name)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200149{
150 int i;
151
152 for (i = 0; i < used; ++i) {
153 char audio_codec[64];
154 int rate = -1;
155 int channels = -1;
156 if (codecs[i].payload_type != payload)
157 continue;
158 if (sscanf(audio_name, "%63[^/]/%d/%d",
159 audio_codec, &rate, &channels) < 1) {
160 LOGP(DLMGCP, LOGL_ERROR, "Failed to parse '%s'\n", audio_name);
161 continue;
162 }
163
164 codecs[i].map_line = talloc_strdup(ctx, audio_name);
165 codecs[i].codec_name = talloc_strdup(ctx, audio_codec);
166 codecs[i].rate = rate;
167 codecs[i].channels = channels;
168 return;
169 }
170
171 LOGP(DLMGCP, LOGL_ERROR, "Unconfigured PT(%d) with %s\n", payload, audio_name);
172}
173
Philipp Maier8970c492017-10-11 13:33:42 +0200174/* Check if the codec matches what is set up in the trunk config */
175static int is_codec_compatible(const struct mgcp_endpoint *endp,
176 const struct sdp_rtp_map *codec)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200177{
178 char *bts_codec;
179 char audio_codec[64];
180
181 if (!codec->codec_name)
182 return 0;
183
184 /*
185 * GSM, GSM/8000 and GSM/8000/1 should all be compatible.. let's go
186 * by name first.
187 */
188 bts_codec = endp->tcfg->audio_name;
189 if (sscanf(bts_codec, "%63[^/]/%*d/%*d", audio_codec) < 1)
190 return 0;
191
192 return strcasecmp(audio_codec, codec->codec_name) == 0;
193}
194
Philipp Maier8970c492017-10-11 13:33:42 +0200195/*! Analyze SDP input string.
196 * \param[in] endp trunk endpoint.
197 * \param[out] conn associated rtp connection.
198 * \param[out] caller provided memory to store the parsing results.
199 * \returns 0 on success, -1 on failure.
200 *
201 * Note: In conn (conn->end) the function returns the packet duration,
202 * the rtp port and the rtcp port */
203int mgcp_parse_sdp_data(const struct mgcp_endpoint *endp,
204 struct mgcp_conn_rtp *conn,
205 struct mgcp_parse_data *p)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200206{
207 struct sdp_rtp_map codecs[10];
208 int codecs_used = 0;
209 char *line;
210 int maxptime = -1;
211 int i;
212 int codecs_assigned = 0;
213 void *tmp_ctx = talloc_new(NULL);
Philipp Maier8970c492017-10-11 13:33:42 +0200214 struct mgcp_rtp_end *rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200215
Philipp Maier8970c492017-10-11 13:33:42 +0200216 OSMO_ASSERT(endp);
217 OSMO_ASSERT(conn);
218 OSMO_ASSERT(p);
219
220 rtp = &conn->end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200221 memset(&codecs, 0, sizeof(codecs));
222
223 for_each_line(line, p->save) {
224 switch (line[0]) {
225 case 'o':
226 case 's':
227 case 't':
228 case 'v':
229 /* skip these SDP attributes */
230 break;
231 case 'a': {
232 int payload;
233 int ptime, ptime2 = 0;
234 char audio_name[64];
235
236
237 if (sscanf(line, "a=rtpmap:%d %63s",
238 &payload, audio_name) == 2) {
239 codecs_update(tmp_ctx, codecs, codecs_used, payload, audio_name);
240 } else if (sscanf(line, "a=ptime:%d-%d",
241 &ptime, &ptime2) >= 1) {
242 if (ptime2 > 0 && ptime2 != ptime)
243 rtp->packet_duration_ms = 0;
244 else
245 rtp->packet_duration_ms = ptime;
246 } else if (sscanf(line, "a=maxptime:%d", &ptime2) == 1) {
247 maxptime = ptime2;
248 }
249 break;
250 }
251 case 'm': {
252 int port, rc;
253
254 rc = sscanf(line, "m=audio %d RTP/AVP %d %d %d %d %d %d %d %d %d %d",
255 &port,
256 &codecs[0].payload_type,
257 &codecs[1].payload_type,
258 &codecs[2].payload_type,
259 &codecs[3].payload_type,
260 &codecs[4].payload_type,
261 &codecs[5].payload_type,
262 &codecs[6].payload_type,
263 &codecs[7].payload_type,
264 &codecs[8].payload_type,
265 &codecs[9].payload_type);
266 if (rc >= 2) {
267 rtp->rtp_port = htons(port);
268 rtp->rtcp_port = htons(port + 1);
269 codecs_used = rc - 1;
270 codecs_initialize(tmp_ctx, codecs, codecs_used);
271 }
272 break;
273 }
274 case 'c': {
275 char ipv4[16];
276
277 if (sscanf(line, "c=IN IP4 %15s", ipv4) == 1) {
278 inet_aton(ipv4, &rtp->addr);
279 }
280 break;
281 }
282 default:
283 if (p->endp)
284 LOGP(DLMGCP, LOGL_NOTICE,
285 "Unhandled SDP option: '%c'/%d on 0x%x\n",
286 line[0], line[0], ENDPOINT_NUMBER(p->endp));
287 else
288 LOGP(DLMGCP, LOGL_NOTICE,
289 "Unhandled SDP option: '%c'/%d\n",
290 line[0], line[0]);
291 break;
292 }
293 }
294
295 /* Now select the primary and alt_codec */
296 for (i = 0; i < codecs_used && codecs_assigned < 2; ++i) {
297 struct mgcp_rtp_codec *codec = codecs_assigned == 0 ?
298 &rtp->codec : &rtp->alt_codec;
299
300 if (endp->tcfg->no_audio_transcoding &&
301 !is_codec_compatible(endp, &codecs[i])) {
302 LOGP(DLMGCP, LOGL_NOTICE, "Skipping codec %s\n",
303 codecs[i].codec_name);
304 continue;
305 }
306
307 mgcp_set_audio_info(p->cfg, codec,
308 codecs[i].payload_type,
309 codecs[i].map_line);
310 codecs_assigned += 1;
311 }
312
313 if (codecs_assigned > 0) {
314 /* TODO/XXX: Store this per codec and derive it on use */
315 if (maxptime >= 0 && maxptime * rtp->codec.frame_duration_den >
316 rtp->codec.frame_duration_num * 1500) {
317 /* more than 1 frame */
318 rtp->packet_duration_ms = 0;
319 }
320
321 LOGP(DLMGCP, LOGL_NOTICE,
322 "Got media info via SDP: port %d, payload %d (%s), "
323 "duration %d, addr %s\n",
324 ntohs(rtp->rtp_port), rtp->codec.payload_type,
325 rtp->codec.subtype_name ? rtp->codec.subtype_name : "unknown",
326 rtp->packet_duration_ms, inet_ntoa(rtp->addr));
327 }
328
329 talloc_free(tmp_ctx);
330 return codecs_assigned > 0;
331}
332
Philipp Maier8970c492017-10-11 13:33:42 +0200333/*! Generate SDP response string.
334 * \param[in] endp trunk endpoint.
335 * \param[in] conn associated rtp connection.
336 * \param[out] sdp msg buffer to append resulting SDP string data.
337 * \param[in] addr IPV4 address string (e.g. 192.168.100.1).
338 * \returns 0 on success, -1 on failure. */
339int mgcp_write_response_sdp(const struct mgcp_endpoint *endp,
340 const struct mgcp_conn_rtp *conn, struct msgb *sdp,
341 const char *addr)
342{
343 const char *fmtp_extra;
344 const char *audio_name;
345 int payload_type;
346 int rc;
347
348 OSMO_ASSERT(endp);
349 OSMO_ASSERT(conn);
350 OSMO_ASSERT(sdp);
351 OSMO_ASSERT(addr);
352
353 /* FIXME: constify endp and conn args in get_net_donwlink_format_cb() */
354 endp->cfg->get_net_downlink_format_cb((struct mgcp_endpoint *)endp,
355 &payload_type, &audio_name,
356 &fmtp_extra,
357 (struct mgcp_conn_rtp *)conn);
358
359 rc = msgb_printf(sdp,
360 "v=0\r\n"
361 "o=- %u 23 IN IP4 %s\r\n"
362 "s=-\r\n"
363 "c=IN IP4 %s\r\n"
364 "t=0 0\r\n", conn->conn->id, addr, addr);
365
366 if (rc < 0)
367 goto buffer_too_small;
368
369 if (payload_type >= 0) {
370 rc = msgb_printf(sdp, "m=audio %d RTP/AVP %d\r\n",
371 conn->end.local_port, payload_type);
372 if (rc < 0)
373 goto buffer_too_small;
374
375 if (audio_name && endp->tcfg->audio_send_name) {
376 rc = msgb_printf(sdp, "a=rtpmap:%d %s\r\n",
377 payload_type, audio_name);
378
379 if (rc < 0)
380 goto buffer_too_small;
381 }
382
383 if (fmtp_extra) {
384 rc = msgb_printf(sdp, "%s\r\n", fmtp_extra);
385
386 if (rc < 0)
387 goto buffer_too_small;
388 }
389 }
390 if (conn->end.packet_duration_ms > 0 && endp->tcfg->audio_send_ptime) {
391 rc = msgb_printf(sdp, "a=ptime:%u\r\n",
392 conn->end.packet_duration_ms);
393 if (rc < 0)
394 goto buffer_too_small;
395 }
396
397 return 0;
398
399buffer_too_small:
400 LOGP(DLMGCP, LOGL_ERROR, "SDP messagebuffer too small\n");
401 return -1;
402}