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