blob: 8575e86bc5fd74668cf1ee5be38cb74c6fbebf1c [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{
Philipp Maiere472b4e2017-10-18 11:38:17 +0200178 char *codec_str;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200179 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 */
Philipp Maiere472b4e2017-10-18 11:38:17 +0200188 codec_str = endp->tcfg->audio_name;
189 if (sscanf(codec_str, "%63[^/]/%*d/%*d", audio_codec) < 1)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200190 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 Maierc7a228a2017-10-18 11:42:25 +0200216 int payload;
217 int ptime, ptime2 = 0;
218 char audio_name[64];
219 int port, rc;
220 char ipv4[16];
221
Philipp Maier8970c492017-10-11 13:33:42 +0200222 OSMO_ASSERT(endp);
223 OSMO_ASSERT(conn);
224 OSMO_ASSERT(p);
225
226 rtp = &conn->end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200227 memset(&codecs, 0, sizeof(codecs));
228
229 for_each_line(line, p->save) {
230 switch (line[0]) {
231 case 'o':
232 case 's':
233 case 't':
234 case 'v':
235 /* skip these SDP attributes */
236 break;
Philipp Maierc7a228a2017-10-18 11:42:25 +0200237 case 'a':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200238 if (sscanf(line, "a=rtpmap:%d %63s",
239 &payload, audio_name) == 2) {
Philipp Maierc7a228a2017-10-18 11:42:25 +0200240 codecs_update(tmp_ctx, codecs,
241 codecs_used, payload, audio_name);
242 } else
243 if (sscanf
244 (line, "a=ptime:%d-%d", &ptime, &ptime2) >= 1) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200245 if (ptime2 > 0 && ptime2 != ptime)
246 rtp->packet_duration_ms = 0;
247 else
248 rtp->packet_duration_ms = ptime;
Philipp Maierc7a228a2017-10-18 11:42:25 +0200249 } else if (sscanf(line, "a=maxptime:%d", &ptime2)
250 == 1) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200251 maxptime = ptime2;
252 }
253 break;
Philipp Maierc7a228a2017-10-18 11:42:25 +0200254 case 'm':
255 rc = sscanf(line,
256 "m=audio %d RTP/AVP %d %d %d %d %d %d %d %d %d %d",
257 &port, &codecs[0].payload_type,
258 &codecs[1].payload_type,
259 &codecs[2].payload_type,
260 &codecs[3].payload_type,
261 &codecs[4].payload_type,
262 &codecs[5].payload_type,
263 &codecs[6].payload_type,
264 &codecs[7].payload_type,
265 &codecs[8].payload_type,
266 &codecs[9].payload_type);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200267 if (rc >= 2) {
268 rtp->rtp_port = htons(port);
269 rtp->rtcp_port = htons(port + 1);
270 codecs_used = rc - 1;
271 codecs_initialize(tmp_ctx, codecs, codecs_used);
272 }
273 break;
Philipp Maierc7a228a2017-10-18 11:42:25 +0200274 case 'c':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200275
276 if (sscanf(line, "c=IN IP4 %15s", ipv4) == 1) {
277 inet_aton(ipv4, &rtp->addr);
278 }
279 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200280 default:
281 if (p->endp)
282 LOGP(DLMGCP, LOGL_NOTICE,
283 "Unhandled SDP option: '%c'/%d on 0x%x\n",
Philipp Maierc7a228a2017-10-18 11:42:25 +0200284 line[0], line[0],
285 ENDPOINT_NUMBER(p->endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200286 else
287 LOGP(DLMGCP, LOGL_NOTICE,
288 "Unhandled SDP option: '%c'/%d\n",
289 line[0], line[0]);
290 break;
291 }
292 }
293
294 /* Now select the primary and alt_codec */
295 for (i = 0; i < codecs_used && codecs_assigned < 2; ++i) {
296 struct mgcp_rtp_codec *codec = codecs_assigned == 0 ?
Philipp Maierc7a228a2017-10-18 11:42:25 +0200297 &rtp->codec : &rtp->alt_codec;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200298
299 if (endp->tcfg->no_audio_transcoding &&
Philipp Maierc7a228a2017-10-18 11:42:25 +0200300 !is_codec_compatible(endp, &codecs[i])) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200301 LOGP(DLMGCP, LOGL_NOTICE, "Skipping codec %s\n",
Philipp Maierc7a228a2017-10-18 11:42:25 +0200302 codecs[i].codec_name);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200303 continue;
304 }
305
306 mgcp_set_audio_info(p->cfg, codec,
Philipp Maierc7a228a2017-10-18 11:42:25 +0200307 codecs[i].payload_type, codecs[i].map_line);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200308 codecs_assigned += 1;
309 }
310
311 if (codecs_assigned > 0) {
312 /* TODO/XXX: Store this per codec and derive it on use */
313 if (maxptime >= 0 && maxptime * rtp->codec.frame_duration_den >
Philipp Maierc7a228a2017-10-18 11:42:25 +0200314 rtp->codec.frame_duration_num * 1500) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200315 /* more than 1 frame */
316 rtp->packet_duration_ms = 0;
317 }
318
319 LOGP(DLMGCP, LOGL_NOTICE,
320 "Got media info via SDP: port %d, payload %d (%s), "
321 "duration %d, addr %s\n",
322 ntohs(rtp->rtp_port), rtp->codec.payload_type,
Philipp Maierc7a228a2017-10-18 11:42:25 +0200323 rtp->codec.subtype_name ? rtp->
324 codec.subtype_name : "unknown", rtp->packet_duration_ms,
325 inet_ntoa(rtp->addr));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200326 }
327
328 talloc_free(tmp_ctx);
329 return codecs_assigned > 0;
330}
331
Philipp Maier8970c492017-10-11 13:33:42 +0200332/*! Generate SDP response string.
333 * \param[in] endp trunk endpoint.
334 * \param[in] conn associated rtp connection.
335 * \param[out] sdp msg buffer to append resulting SDP string data.
336 * \param[in] addr IPV4 address string (e.g. 192.168.100.1).
337 * \returns 0 on success, -1 on failure. */
338int mgcp_write_response_sdp(const struct mgcp_endpoint *endp,
339 const struct mgcp_conn_rtp *conn, struct msgb *sdp,
340 const char *addr)
341{
342 const char *fmtp_extra;
343 const char *audio_name;
344 int payload_type;
345 int rc;
346
347 OSMO_ASSERT(endp);
348 OSMO_ASSERT(conn);
349 OSMO_ASSERT(sdp);
350 OSMO_ASSERT(addr);
351
352 /* FIXME: constify endp and conn args in get_net_donwlink_format_cb() */
353 endp->cfg->get_net_downlink_format_cb((struct mgcp_endpoint *)endp,
354 &payload_type, &audio_name,
355 &fmtp_extra,
356 (struct mgcp_conn_rtp *)conn);
357
358 rc = msgb_printf(sdp,
359 "v=0\r\n"
360 "o=- %u 23 IN IP4 %s\r\n"
361 "s=-\r\n"
362 "c=IN IP4 %s\r\n"
363 "t=0 0\r\n", conn->conn->id, addr, addr);
364
365 if (rc < 0)
366 goto buffer_too_small;
367
368 if (payload_type >= 0) {
369 rc = msgb_printf(sdp, "m=audio %d RTP/AVP %d\r\n",
370 conn->end.local_port, payload_type);
371 if (rc < 0)
372 goto buffer_too_small;
373
374 if (audio_name && endp->tcfg->audio_send_name) {
375 rc = msgb_printf(sdp, "a=rtpmap:%d %s\r\n",
376 payload_type, audio_name);
377
378 if (rc < 0)
379 goto buffer_too_small;
380 }
381
382 if (fmtp_extra) {
383 rc = msgb_printf(sdp, "%s\r\n", fmtp_extra);
384
385 if (rc < 0)
386 goto buffer_too_small;
387 }
388 }
389 if (conn->end.packet_duration_ms > 0 && endp->tcfg->audio_send_ptime) {
390 rc = msgb_printf(sdp, "a=ptime:%u\r\n",
391 conn->end.packet_duration_ms);
392 if (rc < 0)
393 goto buffer_too_small;
394 }
395
396 return 0;
397
398buffer_too_small:
399 LOGP(DLMGCP, LOGL_ERROR, "SDP messagebuffer too small\n");
400 return -1;
401}