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 | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 23 | #include <osmocom/core/msgb.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 24 | #include <osmocom/mgcp/mgcp.h> |
| 25 | #include <osmocom/mgcp/mgcp_internal.h> |
| 26 | #include <osmocom/mgcp/mgcp_msg.h> |
Philipp Maier | 37d11c8 | 2018-02-01 14:38:12 +0100 | [diff] [blame] | 27 | #include <osmocom/mgcp/mgcp_endp.h> |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 28 | #include <osmocom/mgcp/mgcp_codec.h> |
Philipp Maier | 7e37ce6 | 2019-03-05 14:00:11 +0100 | [diff] [blame] | 29 | #include <osmocom/mgcp/mgcp_sdp.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 30 | |
| 31 | #include <errno.h> |
| 32 | |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 33 | /* Two structs to store intermediate parsing results. The function |
| 34 | * mgcp_parse_sdp_data() is using the following two structs as temporary |
| 35 | * storage for parsing the SDP codec information. */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 36 | struct sdp_rtp_map { |
| 37 | /* the type */ |
| 38 | int payload_type; |
| 39 | /* null, static or later dynamic codec name */ |
| 40 | char *codec_name; |
| 41 | /* A pointer to the original line for later parsing */ |
| 42 | char *map_line; |
| 43 | |
| 44 | int rate; |
| 45 | int channels; |
| 46 | }; |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 47 | struct sdp_fmtp_param { |
| 48 | int payload_type; |
| 49 | struct mgcp_codec_param param; |
| 50 | }; |
| 51 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 52 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 53 | /* Helper function to extrapolate missing codec parameters in a codec mao from |
| 54 | * an already filled in payload_type, called from: mgcp_parse_sdp_data() */ |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 55 | static void codecs_initialize(void *ctx, struct sdp_rtp_map *codecs, int used) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 56 | { |
| 57 | int i; |
| 58 | |
| 59 | for (i = 0; i < used; ++i) { |
| 60 | switch (codecs[i].payload_type) { |
| 61 | case 0: |
| 62 | codecs[i].codec_name = "PCMU"; |
| 63 | codecs[i].rate = 8000; |
| 64 | codecs[i].channels = 1; |
| 65 | break; |
| 66 | case 3: |
| 67 | codecs[i].codec_name = "GSM"; |
| 68 | codecs[i].rate = 8000; |
| 69 | codecs[i].channels = 1; |
| 70 | break; |
| 71 | case 8: |
| 72 | codecs[i].codec_name = "PCMA"; |
| 73 | codecs[i].rate = 8000; |
| 74 | codecs[i].channels = 1; |
| 75 | break; |
| 76 | case 18: |
| 77 | codecs[i].codec_name = "G729"; |
| 78 | codecs[i].rate = 8000; |
| 79 | codecs[i].channels = 1; |
| 80 | break; |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 81 | default: |
| 82 | codecs[i].codec_name = NULL; |
| 83 | codecs[i].rate = 0; |
| 84 | codecs[i].channels = 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 89 | /* Helper function to update codec map information with additional data from |
| 90 | * SDP, called from: mgcp_parse_sdp_data() */ |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 91 | static void codecs_update(void *ctx, struct sdp_rtp_map *codecs, int used, |
| 92 | int payload, const char *audio_name) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 93 | { |
| 94 | int i; |
| 95 | |
| 96 | for (i = 0; i < used; ++i) { |
| 97 | char audio_codec[64]; |
| 98 | int rate = -1; |
| 99 | int channels = -1; |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 100 | |
| 101 | /* Note: We can only update payload codecs that already exist |
| 102 | * in our codec list. If we get an unexpected payload type, |
| 103 | * we just drop it */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 104 | if (codecs[i].payload_type != payload) |
| 105 | continue; |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 106 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 107 | if (sscanf(audio_name, "%63[^/]/%d/%d", |
Philipp Maier | d8d7b4b | 2017-10-18 11:45:35 +0200 | [diff] [blame] | 108 | audio_codec, &rate, &channels) < 1) { |
| 109 | LOGP(DLMGCP, LOGL_ERROR, "Failed to parse '%s'\n", |
| 110 | audio_name); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 111 | continue; |
| 112 | } |
| 113 | |
| 114 | codecs[i].map_line = talloc_strdup(ctx, audio_name); |
| 115 | codecs[i].codec_name = talloc_strdup(ctx, audio_codec); |
| 116 | codecs[i].rate = rate; |
| 117 | codecs[i].channels = channels; |
| 118 | return; |
| 119 | } |
| 120 | |
Philipp Maier | d8d7b4b | 2017-10-18 11:45:35 +0200 | [diff] [blame] | 121 | LOGP(DLMGCP, LOGL_ERROR, "Unconfigured PT(%d) with %s\n", payload, |
| 122 | audio_name); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 125 | /* Extract payload types from SDP, also check for duplicates */ |
| 126 | static int pt_from_sdp(void *ctx, struct sdp_rtp_map *codecs, |
| 127 | unsigned int codecs_len, char *sdp) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 128 | { |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 129 | char *str; |
| 130 | char *str_ptr; |
| 131 | char *pt_str; |
| 132 | unsigned int pt; |
| 133 | unsigned int count = 0; |
| 134 | unsigned int i; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 135 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 136 | str = talloc_zero_size(ctx, strlen(sdp) + 1); |
| 137 | str_ptr = str; |
| 138 | strcpy(str_ptr, sdp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 139 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 140 | str_ptr = strstr(str_ptr, "RTP/AVP "); |
| 141 | if (!str_ptr) |
| 142 | goto exit; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 143 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 144 | pt_str = strtok(str_ptr, " "); |
| 145 | if (!pt_str) |
| 146 | goto exit; |
| 147 | |
| 148 | while (1) { |
| 149 | /* Do not allow excessive payload types */ |
| 150 | if (count > codecs_len) |
| 151 | goto error; |
| 152 | |
| 153 | pt_str = strtok(NULL, " "); |
| 154 | if (!pt_str) |
| 155 | break; |
| 156 | |
| 157 | pt = atoi(pt_str); |
| 158 | |
| 159 | /* Do not allow duplicate payload types */ |
| 160 | for (i = 0; i < count; i++) |
| 161 | if (codecs[i].payload_type == pt) |
| 162 | goto error; |
| 163 | |
| 164 | codecs[count].payload_type = pt; |
| 165 | count++; |
| 166 | } |
| 167 | |
| 168 | exit: |
| 169 | talloc_free(str); |
| 170 | return count; |
| 171 | error: |
| 172 | talloc_free(str); |
| 173 | return -EINVAL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 176 | /* Extract fmtp parameters from SDP, called from: mgcp_parse_sdp_data() */ |
| 177 | static int fmtp_from_sdp(void *ctx, struct sdp_fmtp_param *fmtp_param, char *sdp) |
| 178 | { |
| 179 | char *str; |
| 180 | char *str_ptr; |
| 181 | char *param_str; |
| 182 | unsigned int pt; |
| 183 | unsigned int count = 0; |
| 184 | char delimiter; |
| 185 | unsigned int amr_octet_aligned; |
| 186 | |
| 187 | memset(fmtp_param, 0, sizeof(*fmtp_param)); |
| 188 | |
| 189 | str = talloc_zero_size(ctx, strlen(sdp) + 1); |
| 190 | str_ptr = str; |
| 191 | strcpy(str_ptr, sdp); |
| 192 | |
| 193 | /* Check if the input string begins with an fmtp token */ |
| 194 | str_ptr = strstr(str_ptr, "fmtp:"); |
| 195 | if (!str_ptr) |
| 196 | goto exit; |
| 197 | str_ptr += 5; |
| 198 | |
| 199 | /* Extract payload type */ |
| 200 | if (sscanf(str_ptr, "%u ", &pt) != 1) |
| 201 | goto error; |
| 202 | fmtp_param->payload_type = pt; |
| 203 | |
| 204 | /* Advance pointer to the beginning of the parameter section and |
| 205 | * tokenize string */ |
| 206 | str_ptr = strstr(str_ptr, " "); |
| 207 | if (!str_ptr) |
| 208 | goto error; |
| 209 | str_ptr++; |
| 210 | |
| 211 | param_str = strtok(str_ptr, " "); |
| 212 | if (!param_str) |
| 213 | goto exit; |
| 214 | |
| 215 | while (1) { |
| 216 | /* Make sure that we don't get trapped in an endless loop */ |
| 217 | if (count > 256) |
| 218 | goto error; |
| 219 | |
| 220 | /* Chop off delimiters ';' at the end */ |
| 221 | delimiter = str_ptr[strlen(str_ptr) - 1]; |
| 222 | if (delimiter == ';' || delimiter == ',') |
| 223 | str_ptr[strlen(str_ptr) - 1] = '\0'; |
| 224 | |
| 225 | /* AMR octet aligned parameter */ |
| 226 | if (sscanf(param_str, "octet-align=%d", &amr_octet_aligned) == 1) { |
| 227 | fmtp_param->param.amr_octet_aligned_present = true; |
| 228 | fmtp_param->param.amr_octet_aligned = false; |
| 229 | if (amr_octet_aligned == 1) |
| 230 | fmtp_param->param.amr_octet_aligned = true; |
| 231 | |
| 232 | } |
| 233 | |
| 234 | param_str = strtok(NULL, " "); |
| 235 | if (!param_str) |
| 236 | break; |
| 237 | count++; |
| 238 | } |
| 239 | |
| 240 | exit: |
| 241 | talloc_free(str); |
| 242 | return 0; |
| 243 | error: |
| 244 | talloc_free(str); |
| 245 | return -EINVAL; |
| 246 | } |
| 247 | |
| 248 | /* Pick optional fmtp parameters by payload type, if there are no fmtp |
| 249 | * parameters, a nullpointer is returned */ |
| 250 | static struct mgcp_codec_param *param_by_pt(int pt, struct sdp_fmtp_param *fmtp_params, unsigned int fmtp_params_len) |
| 251 | { |
| 252 | unsigned int i; |
| 253 | |
| 254 | for (i = 0; i < fmtp_params_len; i++) { |
| 255 | if (fmtp_params[i].payload_type == pt) |
| 256 | return &fmtp_params[i].param; |
| 257 | } |
| 258 | |
| 259 | return NULL; |
| 260 | } |
| 261 | |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 262 | /*! Analyze SDP input string. |
| 263 | * \param[in] endp trunk endpoint. |
| 264 | * \param[out] conn associated rtp connection. |
| 265 | * \param[out] caller provided memory to store the parsing results. |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 266 | * |
| 267 | * Note: In conn (conn->end) the function returns the packet duration, |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 268 | * rtp port, rtcp port and the codec information. |
| 269 | * \returns 0 on success, -1 on failure. */ |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 270 | int mgcp_parse_sdp_data(const struct mgcp_endpoint *endp, |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 271 | struct mgcp_conn_rtp *conn, struct mgcp_parse_data *p) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 272 | { |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 273 | struct sdp_rtp_map codecs[MGCP_MAX_CODECS]; |
| 274 | unsigned int codecs_used = 0; |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 275 | struct sdp_fmtp_param fmtp_params[MGCP_MAX_CODECS]; |
| 276 | unsigned int fmtp_used = 0; |
| 277 | struct mgcp_codec_param *codec_param; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 278 | char *line; |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 279 | unsigned int i; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 280 | void *tmp_ctx = talloc_new(NULL); |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 281 | struct mgcp_rtp_end *rtp; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 282 | |
Philipp Maier | c7a228a | 2017-10-18 11:42:25 +0200 | [diff] [blame] | 283 | int payload; |
| 284 | int ptime, ptime2 = 0; |
| 285 | char audio_name[64]; |
| 286 | int port, rc; |
| 287 | char ipv4[16]; |
| 288 | |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 289 | OSMO_ASSERT(endp); |
| 290 | OSMO_ASSERT(conn); |
| 291 | OSMO_ASSERT(p); |
| 292 | |
| 293 | rtp = &conn->end; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 294 | memset(&codecs, 0, sizeof(codecs)); |
| 295 | |
| 296 | for_each_line(line, p->save) { |
| 297 | switch (line[0]) { |
| 298 | case 'o': |
| 299 | case 's': |
| 300 | case 't': |
| 301 | case 'v': |
| 302 | /* skip these SDP attributes */ |
| 303 | break; |
Philipp Maier | c7a228a | 2017-10-18 11:42:25 +0200 | [diff] [blame] | 304 | case 'a': |
Philipp Maier | e7ae69a | 2019-03-05 15:17:36 +0100 | [diff] [blame] | 305 | if (sscanf(line, "a=rtpmap:%d %63s", &payload, audio_name) == 2) { |
| 306 | codecs_update(tmp_ctx, codecs, codecs_used, payload, audio_name); |
| 307 | break; |
| 308 | } |
| 309 | |
| 310 | if (sscanf(line, "a=ptime:%d-%d", &ptime, &ptime2) >= 1) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 311 | if (ptime2 > 0 && ptime2 != ptime) |
| 312 | rtp->packet_duration_ms = 0; |
| 313 | else |
| 314 | rtp->packet_duration_ms = ptime; |
Philipp Maier | e7ae69a | 2019-03-05 15:17:36 +0100 | [diff] [blame] | 315 | break; |
| 316 | } |
| 317 | |
| 318 | if (sscanf(line, "a=maxptime:%d", &ptime2) == 1) { |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 319 | rtp->maximum_packet_time = ptime2; |
Philipp Maier | e7ae69a | 2019-03-05 15:17:36 +0100 | [diff] [blame] | 320 | break; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 321 | } |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 322 | |
| 323 | if (strncmp("a=fmtp:", line, 6) == 0) { |
| 324 | rc = fmtp_from_sdp(conn->conn, &fmtp_params[fmtp_used], line); |
| 325 | if (rc >= 0) |
| 326 | fmtp_used++; |
| 327 | break; |
| 328 | } |
| 329 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 330 | break; |
Philipp Maier | c7a228a | 2017-10-18 11:42:25 +0200 | [diff] [blame] | 331 | case 'm': |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 332 | rc = sscanf(line, "m=audio %d RTP/AVP", &port); |
| 333 | if (rc == 1) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 334 | rtp->rtp_port = htons(port); |
| 335 | rtp->rtcp_port = htons(port + 1); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 336 | } |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 337 | |
| 338 | rc = pt_from_sdp(conn->conn, codecs, |
| 339 | ARRAY_SIZE(codecs), line); |
| 340 | if (rc > 0) |
| 341 | codecs_used = rc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 342 | break; |
Philipp Maier | c7a228a | 2017-10-18 11:42:25 +0200 | [diff] [blame] | 343 | case 'c': |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 344 | |
| 345 | if (sscanf(line, "c=IN IP4 %15s", ipv4) == 1) { |
| 346 | inet_aton(ipv4, &rtp->addr); |
| 347 | } |
| 348 | break; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 349 | default: |
| 350 | if (p->endp) |
| 351 | LOGP(DLMGCP, LOGL_NOTICE, |
| 352 | "Unhandled SDP option: '%c'/%d on 0x%x\n", |
Philipp Maier | c7a228a | 2017-10-18 11:42:25 +0200 | [diff] [blame] | 353 | line[0], line[0], |
| 354 | ENDPOINT_NUMBER(p->endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 355 | else |
| 356 | LOGP(DLMGCP, LOGL_NOTICE, |
| 357 | "Unhandled SDP option: '%c'/%d\n", |
| 358 | line[0], line[0]); |
| 359 | break; |
| 360 | } |
| 361 | } |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 362 | OSMO_ASSERT(codecs_used <= MGCP_MAX_CODECS); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 363 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 364 | /* So far we have only set the payload type in the codec struct. Now we |
| 365 | * fill up the remaining fields of the codec description with some default |
| 366 | * information */ |
| 367 | codecs_initialize(tmp_ctx, codecs, codecs_used); |
| 368 | |
| 369 | /* Store parsed codec information */ |
Philipp Maier | dbd70c7 | 2018-05-25 11:07:31 +0200 | [diff] [blame] | 370 | for (i = 0; i < codecs_used; i++) { |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 371 | codec_param = param_by_pt(codecs[i].payload_type, fmtp_params, fmtp_used); |
| 372 | rc = mgcp_codec_add(conn, codecs[i].payload_type, codecs[i].map_line, codec_param); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 373 | if (rc < 0) |
| 374 | LOGP(DLMGCP, LOGL_NOTICE, "endpoint:0x%x, failed to add codec\n", ENDPOINT_NUMBER(p->endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | talloc_free(tmp_ctx); |
Philipp Maier | dbd70c7 | 2018-05-25 11:07:31 +0200 | [diff] [blame] | 378 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 379 | LOGPCONN(conn->conn, DLMGCP, LOGL_NOTICE, |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 380 | "Got media info via SDP: port:%d, addr:%s, duration:%d, payload-types:", |
| 381 | ntohs(rtp->rtp_port), inet_ntoa(rtp->addr), |
| 382 | rtp->packet_duration_ms); |
| 383 | if (codecs_used == 0) |
| 384 | LOGPC(DLMGCP, LOGL_NOTICE, "none"); |
| 385 | for (i = 0; i < codecs_used; i++) { |
| 386 | LOGPC(DLMGCP, LOGL_NOTICE, "%d=%s", |
| 387 | rtp->codecs[i].payload_type, |
| 388 | rtp->codecs[i].subtype_name ? rtp-> codecs[i].subtype_name : "unknown"); |
| 389 | LOGPC(DLMGCP, LOGL_NOTICE, " "); |
| 390 | } |
| 391 | LOGPC(DLMGCP, LOGL_NOTICE, "\n"); |
| 392 | |
Philipp Maier | dbd70c7 | 2018-05-25 11:07:31 +0200 | [diff] [blame] | 393 | return 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Philipp Maier | 8482e83 | 2018-08-02 17:22:40 +0200 | [diff] [blame] | 396 | |
| 397 | /* Add rtpmap string to the sdp payload, but only when the payload type falls |
| 398 | * into the dynamic payload type range */ |
| 399 | static int add_rtpmap(struct msgb *sdp, int payload_type, const char *audio_name) |
| 400 | { |
| 401 | int rc; |
| 402 | |
| 403 | if (payload_type >= 96 && payload_type <= 127) { |
| 404 | if (!audio_name) |
| 405 | return -EINVAL; |
| 406 | rc = msgb_printf(sdp, "a=rtpmap:%d %s\r\n", payload_type, audio_name); |
| 407 | if (rc < 0) |
| 408 | return -EINVAL; |
| 409 | } |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
Philipp Maier | 217d31d | 2019-03-05 14:48:56 +0100 | [diff] [blame] | 414 | /* Add audio strings to sdp payload */ |
Philipp Maier | 910189d | 2018-08-02 17:45:42 +0200 | [diff] [blame] | 415 | static int add_audio(struct msgb *sdp, int *payload_types, unsigned int payload_types_len, int local_port) |
| 416 | { |
| 417 | int rc; |
| 418 | unsigned int i; |
| 419 | |
| 420 | if (payload_types_len < 0) |
| 421 | return -EINVAL; |
| 422 | |
| 423 | rc = msgb_printf(sdp, "m=audio %d RTP/AVP", local_port); |
| 424 | if (rc < 0) |
| 425 | return -EINVAL; |
| 426 | |
| 427 | for (i = 0; i < payload_types_len; i++) { |
| 428 | rc = msgb_printf(sdp, " %d", payload_types[i]); |
| 429 | if (rc < 0) |
| 430 | return -EINVAL; |
| 431 | } |
| 432 | |
| 433 | rc = msgb_printf(sdp, "\r\n"); |
| 434 | if (rc < 0) |
| 435 | return -EINVAL; |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 440 | /* Add fmtp strings to sdp payload */ |
| 441 | static int add_fmtp(struct msgb *sdp, struct sdp_fmtp_param *fmtp_params, unsigned int fmtp_params_len, |
| 442 | const char *fmtp_extra) |
| 443 | { |
| 444 | unsigned int i; |
| 445 | int rc; |
| 446 | int fmtp_extra_pt = -1; |
| 447 | char *fmtp_extra_pars = ""; |
| 448 | |
| 449 | /* When no fmtp parameters ara available but an fmtp extra string |
| 450 | * is configured, just add the fmtp extra string */ |
| 451 | if (fmtp_params_len == 0 && fmtp_extra) { |
| 452 | return msgb_printf(sdp, "%s\r\n", fmtp_extra); |
| 453 | } |
| 454 | |
| 455 | /* When there is fmtp extra configured we dissect it in order to drop |
| 456 | * in the configured extra parameters at the right place when |
| 457 | * generating the fmtp strings. */ |
| 458 | if (fmtp_extra) { |
| 459 | if (sscanf(fmtp_extra, "a=fmtp:%d ", &fmtp_extra_pt) != 1) |
| 460 | fmtp_extra_pt = -1; |
| 461 | |
| 462 | fmtp_extra_pars = strstr(fmtp_extra, " "); |
| 463 | |
| 464 | if (!fmtp_extra_pars) |
| 465 | fmtp_extra_pars = ""; |
| 466 | else |
| 467 | fmtp_extra_pars++; |
| 468 | } |
| 469 | |
| 470 | for (i = 0; i < fmtp_params_len; i++) { |
| 471 | rc = msgb_printf(sdp, "a=fmtp:%u", fmtp_params[i].payload_type); |
| 472 | |
| 473 | /* Add amr octet align parameter */ |
| 474 | if (fmtp_params[i].param.amr_octet_aligned_present) { |
| 475 | if (fmtp_params[i].param.amr_octet_aligned) |
| 476 | rc = msgb_printf(sdp, " octet-align=1"); |
| 477 | else |
| 478 | rc = msgb_printf(sdp, " octet-align=0"); |
| 479 | if (rc < 0) |
| 480 | return -EINVAL; |
| 481 | } |
| 482 | |
| 483 | /* Append extra parameters from fmtp extra */ |
| 484 | if (fmtp_params[i].payload_type == fmtp_extra_pt) { |
| 485 | rc = msgb_printf(sdp, " %s", fmtp_extra_pars); |
| 486 | if (rc < 0) |
| 487 | return -EINVAL; |
| 488 | } |
| 489 | |
| 490 | rc = msgb_printf(sdp, "\r\n", fmtp_params[i].payload_type); |
| 491 | if (rc < 0) |
| 492 | return -EINVAL; |
| 493 | } |
| 494 | |
| 495 | return 0; |
| 496 | } |
| 497 | |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 498 | /*! Generate SDP response string. |
| 499 | * \param[in] endp trunk endpoint. |
| 500 | * \param[in] conn associated rtp connection. |
| 501 | * \param[out] sdp msg buffer to append resulting SDP string data. |
| 502 | * \param[in] addr IPV4 address string (e.g. 192.168.100.1). |
| 503 | * \returns 0 on success, -1 on failure. */ |
| 504 | int mgcp_write_response_sdp(const struct mgcp_endpoint *endp, |
| 505 | const struct mgcp_conn_rtp *conn, struct msgb *sdp, |
| 506 | const char *addr) |
| 507 | { |
Philipp Maier | 5812825 | 2019-03-06 11:28:18 +0100 | [diff] [blame] | 508 | const struct mgcp_rtp_codec *codec; |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 509 | const char *fmtp_extra; |
| 510 | const char *audio_name; |
| 511 | int payload_type; |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 512 | struct sdp_fmtp_param fmtp_param; |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 513 | int rc; |
Philipp Maier | 910189d | 2018-08-02 17:45:42 +0200 | [diff] [blame] | 514 | int payload_types[1]; |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 515 | struct sdp_fmtp_param fmtp_params[1]; |
| 516 | unsigned int fmtp_params_len = 0; |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 517 | |
| 518 | OSMO_ASSERT(endp); |
| 519 | OSMO_ASSERT(conn); |
| 520 | OSMO_ASSERT(sdp); |
| 521 | OSMO_ASSERT(addr); |
| 522 | |
| 523 | /* FIXME: constify endp and conn args in get_net_donwlink_format_cb() */ |
| 524 | endp->cfg->get_net_downlink_format_cb((struct mgcp_endpoint *)endp, |
Philipp Maier | 5812825 | 2019-03-06 11:28:18 +0100 | [diff] [blame] | 525 | &codec, &fmtp_extra, |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 526 | (struct mgcp_conn_rtp *)conn); |
| 527 | |
Philipp Maier | 5812825 | 2019-03-06 11:28:18 +0100 | [diff] [blame] | 528 | audio_name = codec->audio_name; |
| 529 | payload_type = codec->payload_type; |
| 530 | |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 531 | rc = msgb_printf(sdp, |
| 532 | "v=0\r\n" |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 533 | "o=- %s 23 IN IP4 %s\r\n" |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 534 | "s=-\r\n" |
| 535 | "c=IN IP4 %s\r\n" |
| 536 | "t=0 0\r\n", conn->conn->id, addr, addr); |
| 537 | |
| 538 | if (rc < 0) |
| 539 | goto buffer_too_small; |
| 540 | |
| 541 | if (payload_type >= 0) { |
Philipp Maier | 910189d | 2018-08-02 17:45:42 +0200 | [diff] [blame] | 542 | |
| 543 | payload_types[0] = payload_type; |
| 544 | rc = add_audio(sdp, payload_types, 1, conn->end.local_port); |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 545 | if (rc < 0) |
| 546 | goto buffer_too_small; |
| 547 | |
Philipp Maier | 8482e83 | 2018-08-02 17:22:40 +0200 | [diff] [blame] | 548 | if (endp->tcfg->audio_send_name) { |
| 549 | rc = add_rtpmap(sdp, payload_type, audio_name); |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 550 | if (rc < 0) |
| 551 | goto buffer_too_small; |
| 552 | } |
| 553 | |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 554 | if (codec->param_present) { |
| 555 | fmtp_param.payload_type = payload_type; |
| 556 | fmtp_param.param = codec->param; |
| 557 | fmtp_params[0] = fmtp_param; |
| 558 | fmtp_params_len = 1; |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 559 | } |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 560 | rc = add_fmtp(sdp, fmtp_params, fmtp_params_len, fmtp_extra); |
| 561 | if (rc < 0) |
| 562 | goto buffer_too_small; |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 563 | } |
| 564 | if (conn->end.packet_duration_ms > 0 && endp->tcfg->audio_send_ptime) { |
| 565 | rc = msgb_printf(sdp, "a=ptime:%u\r\n", |
| 566 | conn->end.packet_duration_ms); |
| 567 | if (rc < 0) |
| 568 | goto buffer_too_small; |
| 569 | } |
| 570 | |
| 571 | return 0; |
| 572 | |
| 573 | buffer_too_small: |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 574 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, "SDP messagebuffer too small\n"); |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 575 | return -1; |
| 576 | } |