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