blob: 055e1af6c3db815271ae95e8ebcba5aeb9294454 [file] [log] [blame]
Neels Hofmeyre9920f22017-07-10 15:07:22 +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
23#include <osmocom/legacy_mgcp/mgcp.h>
24#include <osmocom/legacy_mgcp/mgcp_internal.h>
25
26#include <errno.h>
27
28struct sdp_rtp_map {
29 /* the type */
30 int payload_type;
31 /* null, static or later dynamic codec name */
32 char *codec_name;
33 /* A pointer to the original line for later parsing */
34 char *map_line;
35
36 int rate;
37 int channels;
38};
39
40int mgcp_set_audio_info(void *ctx, struct mgcp_rtp_codec *codec,
41 int payload_type, const char *audio_name)
42{
43 int rate = codec->rate;
44 int channels = codec->channels;
45 char audio_codec[64];
46
47 talloc_free(codec->subtype_name);
48 codec->subtype_name = NULL;
49 talloc_free(codec->audio_name);
50 codec->audio_name = NULL;
51
52 if (payload_type != PTYPE_UNDEFINED)
53 codec->payload_type = payload_type;
54
55 if (!audio_name) {
56 switch (payload_type) {
57 case 0: audio_name = "PCMU/8000/1"; break;
58 case 3: audio_name = "GSM/8000/1"; break;
59 case 8: audio_name = "PCMA/8000/1"; break;
60 case 18: audio_name = "G729/8000/1"; break;
61 default:
62 /* Payload type is unknown, don't change rate and
63 * channels. */
64 /* TODO: return value? */
65 return 0;
66 }
67 }
68
69 if (sscanf(audio_name, "%63[^/]/%d/%d",
70 audio_codec, &rate, &channels) < 1)
71 return -EINVAL;
72
73 codec->rate = rate;
74 codec->channels = channels;
75 codec->subtype_name = talloc_strdup(ctx, audio_codec);
76 codec->audio_name = talloc_strdup(ctx, audio_name);
77
78 if (!strcmp(audio_codec, "G729")) {
79 codec->frame_duration_num = 10;
80 codec->frame_duration_den = 1000;
81 } else {
82 codec->frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM;
83 codec->frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN;
84 }
85
86 if (payload_type < 0) {
87 payload_type = 96;
88 if (rate == 8000 && channels == 1) {
89 if (!strcmp(audio_codec, "GSM"))
90 payload_type = 3;
91 else if (!strcmp(audio_codec, "PCMA"))
92 payload_type = 8;
93 else if (!strcmp(audio_codec, "PCMU"))
94 payload_type = 0;
95 else if (!strcmp(audio_codec, "G729"))
96 payload_type = 18;
97 }
98
99 codec->payload_type = payload_type;
100 }
101
102 if (channels != 1)
103 LOGP(DLMGCP, LOGL_NOTICE,
104 "Channels != 1 in SDP: '%s'\n", audio_name);
105
106 return 0;
107}
108
109void codecs_initialize(void *ctx, struct sdp_rtp_map *codecs, int used)
110{
111 int i;
112
113 for (i = 0; i < used; ++i) {
114 switch (codecs[i].payload_type) {
115 case 0:
116 codecs[i].codec_name = "PCMU";
117 codecs[i].rate = 8000;
118 codecs[i].channels = 1;
119 break;
120 case 3:
121 codecs[i].codec_name = "GSM";
122 codecs[i].rate = 8000;
123 codecs[i].channels = 1;
124 break;
125 case 8:
126 codecs[i].codec_name = "PCMA";
127 codecs[i].rate = 8000;
128 codecs[i].channels = 1;
129 break;
130 case 18:
131 codecs[i].codec_name = "G729";
132 codecs[i].rate = 8000;
133 codecs[i].channels = 1;
134 break;
135 }
136 }
137}
138
139void codecs_update(void *ctx, struct sdp_rtp_map *codecs, int used, int payload, char *audio_name)
140{
141 int i;
142
143 for (i = 0; i < used; ++i) {
144 char audio_codec[64];
145 int rate = -1;
146 int channels = -1;
147 if (codecs[i].payload_type != payload)
148 continue;
149 if (sscanf(audio_name, "%63[^/]/%d/%d",
150 audio_codec, &rate, &channels) < 1) {
151 LOGP(DLMGCP, LOGL_ERROR, "Failed to parse '%s'\n", audio_name);
152 continue;
153 }
154
155 codecs[i].map_line = talloc_strdup(ctx, audio_name);
156 codecs[i].codec_name = talloc_strdup(ctx, audio_codec);
157 codecs[i].rate = rate;
158 codecs[i].channels = channels;
159 return;
160 }
161
162 LOGP(DLMGCP, LOGL_ERROR, "Unconfigured PT(%d) with %s\n", payload, audio_name);
163}
164
165int is_codec_compatible(struct mgcp_endpoint *endp, struct sdp_rtp_map *codec)
166{
167 char *bts_codec;
168 char audio_codec[64];
169
170 if (!codec->codec_name)
171 return 0;
172
173 /*
174 * GSM, GSM/8000 and GSM/8000/1 should all be compatible.. let's go
175 * by name first.
176 */
177 bts_codec = endp->tcfg->audio_name;
178 if (sscanf(bts_codec, "%63[^/]/%*d/%*d", audio_codec) < 1)
179 return 0;
180
181 return strcasecmp(audio_codec, codec->codec_name) == 0;
182}
183
184int mgcp_parse_sdp_data(struct mgcp_endpoint *endp, struct mgcp_rtp_end *rtp, struct mgcp_parse_data *p)
185{
186 struct sdp_rtp_map codecs[10];
187 int codecs_used = 0;
188 char *line;
189 int maxptime = -1;
190 int i;
191 int codecs_assigned = 0;
192 void *tmp_ctx = talloc_new(NULL);
193
194 memset(&codecs, 0, sizeof(codecs));
195
196 for_each_line(line, p->save) {
197 switch (line[0]) {
198 case 'o':
199 case 's':
200 case 't':
201 case 'v':
202 /* skip these SDP attributes */
203 break;
204 case 'a': {
205 int payload;
206 int ptime, ptime2 = 0;
207 char audio_name[64];
208
209
210 if (sscanf(line, "a=rtpmap:%d %63s",
211 &payload, audio_name) == 2) {
212 codecs_update(tmp_ctx, codecs, codecs_used, payload, audio_name);
213 } else if (sscanf(line, "a=ptime:%d-%d",
214 &ptime, &ptime2) >= 1) {
215 if (ptime2 > 0 && ptime2 != ptime)
216 rtp->packet_duration_ms = 0;
217 else
218 rtp->packet_duration_ms = ptime;
219 } else if (sscanf(line, "a=maxptime:%d", &ptime2) == 1) {
220 maxptime = ptime2;
221 }
222 break;
223 }
224 case 'm': {
225 int port, rc;
226
227 rc = sscanf(line, "m=audio %d RTP/AVP %d %d %d %d %d %d %d %d %d %d",
228 &port,
229 &codecs[0].payload_type,
230 &codecs[1].payload_type,
231 &codecs[2].payload_type,
232 &codecs[3].payload_type,
233 &codecs[4].payload_type,
234 &codecs[5].payload_type,
235 &codecs[6].payload_type,
236 &codecs[7].payload_type,
237 &codecs[8].payload_type,
238 &codecs[9].payload_type);
239 if (rc >= 2) {
240 rtp->rtp_port = htons(port);
241 rtp->rtcp_port = htons(port + 1);
242 codecs_used = rc - 1;
243 codecs_initialize(tmp_ctx, codecs, codecs_used);
244 }
245 break;
246 }
247 case 'c': {
248 char ipv4[16];
249
250 if (sscanf(line, "c=IN IP4 %15s", ipv4) == 1) {
251 inet_aton(ipv4, &rtp->addr);
252 }
253 break;
254 }
255 default:
256 if (p->endp)
257 LOGP(DLMGCP, LOGL_NOTICE,
258 "Unhandled SDP option: '%c'/%d on 0x%x\n",
259 line[0], line[0], ENDPOINT_NUMBER(p->endp));
260 else
261 LOGP(DLMGCP, LOGL_NOTICE,
262 "Unhandled SDP option: '%c'/%d\n",
263 line[0], line[0]);
264 break;
265 }
266 }
267
268 /* Now select the primary and alt_codec */
269 for (i = 0; i < codecs_used && codecs_assigned < 2; ++i) {
270 struct mgcp_rtp_codec *codec = codecs_assigned == 0 ?
271 &rtp->codec : &rtp->alt_codec;
272
273 if (endp->tcfg->no_audio_transcoding &&
274 !is_codec_compatible(endp, &codecs[i])) {
275 LOGP(DLMGCP, LOGL_NOTICE, "Skipping codec %s\n",
276 codecs[i].codec_name);
277 continue;
278 }
279
280 mgcp_set_audio_info(p->cfg, codec,
281 codecs[i].payload_type,
282 codecs[i].map_line);
283 codecs_assigned += 1;
284 }
285
286 if (codecs_assigned > 0) {
287 /* TODO/XXX: Store this per codec and derive it on use */
288 if (maxptime >= 0 && maxptime * rtp->codec.frame_duration_den >
289 rtp->codec.frame_duration_num * 1500) {
290 /* more than 1 frame */
291 rtp->packet_duration_ms = 0;
292 }
293
294 LOGP(DLMGCP, LOGL_NOTICE,
295 "Got media info via SDP: port %d, payload %d (%s), "
296 "duration %d, addr %s\n",
297 ntohs(rtp->rtp_port), rtp->codec.payload_type,
298 rtp->codec.subtype_name ? rtp->codec.subtype_name : "unknown",
299 rtp->packet_duration_ms, inet_ntoa(rtp->addr));
300 }
301
302 talloc_free(tmp_ctx);
303 return codecs_assigned > 0;
304}
305