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