Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2009-2015 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 3 | * (C) 2009-2014 by On-Waves |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | #include <osmocom/mgcp/mgcp_internal.h> |
| 21 | #include <osmocom/mgcp/mgcp_endp.h> |
| 22 | #include <errno.h> |
| 23 | |
| 24 | /* Helper function to dump codec information of a specified codec to a printable |
| 25 | * string, used by dump_codec_summary() */ |
| 26 | static char *dump_codec(struct mgcp_rtp_codec *codec) |
| 27 | { |
| 28 | static char str[256]; |
| 29 | char *pt_str; |
| 30 | |
| 31 | if (codec->payload_type > 76) |
| 32 | pt_str = "DYNAMIC"; |
| 33 | else if (codec->payload_type > 72) |
| 34 | pt_str = "RESERVED <!>"; |
| 35 | else if (codec->payload_type != PTYPE_UNDEFINED) |
| 36 | pt_str = codec->subtype_name; |
| 37 | else |
| 38 | pt_str = "INVALID <!>"; |
| 39 | |
| 40 | snprintf(str, sizeof(str), "(pt:%i=%s, audio:%s subt=%s, rate=%u, ch=%i, t=%u/%u)", codec->payload_type, pt_str, |
| 41 | codec->audio_name, codec->subtype_name, codec->rate, codec->channels, codec->frame_duration_num, |
| 42 | codec->frame_duration_den); |
| 43 | return str; |
| 44 | } |
| 45 | |
| 46 | /*! Dump a summary of all negotiated codecs to debug log |
| 47 | * \param[in] conn related rtp-connection. */ |
| 48 | void mgcp_codec_summary(struct mgcp_conn_rtp *conn) |
| 49 | { |
| 50 | struct mgcp_rtp_end *rtp; |
| 51 | unsigned int i; |
| 52 | struct mgcp_rtp_codec *codec; |
| 53 | struct mgcp_endpoint *endp; |
| 54 | |
| 55 | rtp = &conn->end; |
| 56 | endp = conn->conn->endp; |
| 57 | |
| 58 | if (rtp->codecs_assigned == 0) { |
| 59 | LOGP(DLMGCP, LOGL_ERROR, "endpoint:0x%x conn:%s no codecs available\n", ENDPOINT_NUMBER(endp), |
| 60 | mgcp_conn_dump(conn->conn)); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | /* Store parsed codec information */ |
| 65 | for (i = 0; i < rtp->codecs_assigned; i++) { |
| 66 | codec = &rtp->codecs[i]; |
| 67 | |
| 68 | LOGP(DLMGCP, LOGL_DEBUG, "endpoint:0x%x conn:%s codecs[%u]:%s", ENDPOINT_NUMBER(endp), |
| 69 | mgcp_conn_dump(conn->conn), i, dump_codec(codec)); |
| 70 | |
| 71 | if (codec == rtp->codec) |
| 72 | LOGPC(DLMGCP, LOGL_DEBUG, " [selected]"); |
| 73 | |
| 74 | LOGPC(DLMGCP, LOGL_DEBUG, "\n"); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /* Initalize or reset codec information with default data. */ |
| 79 | void codec_init(struct mgcp_rtp_codec *codec) |
| 80 | { |
| 81 | if (codec->subtype_name) |
| 82 | talloc_free(codec->subtype_name); |
| 83 | if (codec->audio_name) |
| 84 | talloc_free(codec->audio_name); |
| 85 | memset(codec, 0, sizeof(*codec)); |
| 86 | codec->payload_type = -1; |
| 87 | codec->frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM; |
| 88 | codec->frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN; |
| 89 | codec->rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE; |
| 90 | codec->channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS; |
| 91 | } |
| 92 | |
| 93 | /*! Initalize or reset codec information with default data. |
| 94 | * \param[out] conn related rtp-connection. */ |
| 95 | void mgcp_codec_reset_all(struct mgcp_conn_rtp *conn) |
| 96 | { |
| 97 | memset(conn->end.codecs, 0, sizeof(conn->end.codecs)); |
| 98 | conn->end.codecs_assigned = 0; |
| 99 | conn->end.codec = NULL; |
| 100 | } |
| 101 | |
| 102 | /* Set members of struct mgcp_rtp_codec, extrapolate in missing information */ |
| 103 | static int codec_set(void *ctx, struct mgcp_rtp_codec *codec, |
| 104 | int payload_type, const char *audio_name, unsigned int pt_offset) |
| 105 | { |
| 106 | int rate; |
| 107 | int channels; |
| 108 | char audio_codec[64]; |
| 109 | |
| 110 | /* Initalize the codec struct with some default data to begin with */ |
| 111 | codec_init(codec); |
| 112 | |
| 113 | if (payload_type != PTYPE_UNDEFINED) { |
| 114 | /* Make sure we do not get any reserved or undefined type numbers */ |
| 115 | /* See also: https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */ |
| 116 | if (payload_type == 1 || payload_type == 2 || payload_type == 19) |
| 117 | goto error; |
| 118 | if (payload_type >= 72 && payload_type <= 76) |
| 119 | goto error; |
| 120 | if (payload_type >= 127) |
| 121 | goto error; |
| 122 | |
| 123 | codec->payload_type = payload_type; |
| 124 | } |
| 125 | |
| 126 | /* When no audio name is given, we are forced to use the payload |
| 127 | * type to generate the audio name. This is only possible for |
| 128 | * non dynamic payload types, which are statically defined */ |
| 129 | if (!audio_name) { |
| 130 | switch (payload_type) { |
| 131 | case 0: |
| 132 | audio_name = talloc_strdup(ctx, "PCMU/8000/1"); |
| 133 | break; |
| 134 | case 3: |
| 135 | audio_name = talloc_strdup(ctx, "GSM/8000/1"); |
| 136 | break; |
| 137 | case 8: |
| 138 | audio_name = talloc_strdup(ctx, "PCMA/8000/1"); |
| 139 | break; |
| 140 | case 18: |
| 141 | audio_name = talloc_strdup(ctx, "G729/8000/1"); |
| 142 | break; |
| 143 | default: |
| 144 | /* The given payload type is not known to us, or it |
| 145 | * it is a dynamic payload type for which we do not |
| 146 | * know the audio name. We must give up here */ |
| 147 | goto error; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /* Now we extract the codec subtype name, rate and channels. The latter |
| 152 | * two are optional. If they are not present we use the safe defaults |
| 153 | * above. */ |
| 154 | if (strlen(audio_name) > sizeof(audio_codec)) |
| 155 | goto error; |
| 156 | channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS; |
| 157 | rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE; |
| 158 | if (sscanf(audio_name, "%63[^/]/%d/%d", audio_codec, &rate, &channels) < 1) |
| 159 | goto error; |
| 160 | |
| 161 | /* Note: We only accept configurations with one audio channel! */ |
| 162 | if (channels != 1) |
| 163 | goto error; |
| 164 | |
| 165 | codec->rate = rate; |
| 166 | codec->channels = channels; |
| 167 | codec->subtype_name = talloc_strdup(ctx, audio_codec); |
| 168 | codec->audio_name = talloc_strdup(ctx, audio_name); |
| 169 | codec->payload_type = payload_type; |
| 170 | |
| 171 | if (!strcmp(audio_codec, "G729")) { |
| 172 | codec->frame_duration_num = 10; |
| 173 | codec->frame_duration_den = 1000; |
| 174 | } else { |
| 175 | codec->frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM; |
| 176 | codec->frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN; |
| 177 | } |
| 178 | |
| 179 | /* Derive the payload type if it is unknown */ |
| 180 | if (codec->payload_type == PTYPE_UNDEFINED) { |
| 181 | |
| 182 | /* For the known codecs from the static range we restore |
| 183 | * the IANA or 3GPP assigned payload type number */ |
| 184 | if (codec->rate == 8000 && codec->channels == 1) { |
| 185 | /* See also: https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */ |
| 186 | if (!strcmp(codec->subtype_name, "GSM")) |
| 187 | codec->payload_type = 3; |
| 188 | else if (!strcmp(codec->subtype_name, "PCMA")) |
| 189 | codec->payload_type = 8; |
| 190 | else if (!strcmp(codec->subtype_name, "PCMU")) |
| 191 | codec->payload_type = 0; |
| 192 | else if (!strcmp(codec->subtype_name, "G729")) |
| 193 | codec->payload_type = 18; |
| 194 | |
| 195 | /* See also: 3GPP TS 48.103, chapter 5.4.2.2 RTP Payload |
| 196 | * Note: These are not fixed payload types as the IANA |
| 197 | * defined once, they still remain dymanic payload |
| 198 | * types, but with a payload type number preference. */ |
| 199 | else if (!strcmp(codec->subtype_name, "GSM-EFR")) |
| 200 | codec->payload_type = 110; |
| 201 | else if (!strcmp(codec->subtype_name, "GSM-HR-08")) |
| 202 | codec->payload_type = 111; |
| 203 | else if (!strcmp(codec->subtype_name, "AMR")) |
| 204 | codec->payload_type = 112; |
| 205 | else if (!strcmp(codec->subtype_name, "AMR-WB")) |
| 206 | codec->payload_type = 113; |
| 207 | } |
| 208 | |
| 209 | /* If we could not determine a payload type we assume that |
| 210 | * we are dealing with a codec from the dynamic range. We |
| 211 | * choose a fixed identifier from 96-109. (Note: normally, |
| 212 | * the dynamic payload type rante is from 96-127, but from |
| 213 | * 110 onwards 3gpp defines prefered codec types, which are |
| 214 | * also fixed, see above) */ |
| 215 | if (codec->payload_type < 0) { |
| 216 | codec->payload_type = 96 + pt_offset; |
| 217 | if (codec->payload_type > 109) |
| 218 | goto error; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | return 0; |
| 223 | error: |
| 224 | /* Make sure we leave a clean codec entry on error. */ |
| 225 | codec_init(codec); |
| 226 | memset(codec, 0, sizeof(*codec)); |
| 227 | return -EINVAL; |
| 228 | } |
| 229 | |
| 230 | /*! Add codec configuration depending on payload type and/or codec name. This |
| 231 | * function uses the input parameters to extrapolate the full codec information. |
| 232 | * \param[out] codec configuration (caller provided memory). |
| 233 | * \param[out] conn related rtp-connection. |
| 234 | * \param[in] payload_type codec type id (e.g. 3 for GSM, -1 when undefined). |
| 235 | * \param[in] audio_name audio codec name (e.g. "GSM/8000/1"). |
| 236 | * \returns 0 on success, -EINVAL on failure. */ |
| 237 | int mgcp_codec_add(struct mgcp_conn_rtp *conn, int payload_type, const char *audio_name) |
| 238 | { |
| 239 | int rc; |
| 240 | |
| 241 | /* The amount of codecs we can store is limited, make sure we do not |
| 242 | * overrun this limit. */ |
| 243 | if (conn->end.codecs_assigned >= MGCP_MAX_CODECS) |
| 244 | return -EINVAL; |
| 245 | |
| 246 | rc = codec_set(conn->conn, &conn->end.codecs[conn->end.codecs_assigned], payload_type, audio_name, |
| 247 | conn->end.codecs_assigned); |
| 248 | if (rc != 0) |
| 249 | return -EINVAL; |
| 250 | |
| 251 | conn->end.codecs_assigned++; |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | /* Check if the given codec is applicable on the specified endpoint |
| 257 | * Helper function for mgcp_codec_decide() */ |
| 258 | static bool is_codec_compatible(const struct mgcp_endpoint *endp, const struct mgcp_rtp_codec *codec) |
| 259 | { |
| 260 | char codec_name[64]; |
| 261 | |
| 262 | /* A codec name must be set, if not, this might mean that the codec |
| 263 | * (payload type) that was assigned is unknown to us so we must stop |
| 264 | * here. */ |
| 265 | if (!codec->subtype_name) |
| 266 | return false; |
| 267 | |
| 268 | /* We now extract the codec_name (letters before the /, e.g. "GSM" |
| 269 | * from the audio name that is stored in the trunk configuration. |
| 270 | * We do not compare to the full audio_name because we expect that |
| 271 | * "GSM", "GSM/8000" and "GSM/8000/1" are all compatible when the |
| 272 | * audio name of the codec is set to "GSM" */ |
| 273 | if (sscanf(endp->tcfg->audio_name, "%63[^/]/%*d/%*d", codec_name) < 1) |
| 274 | return false; |
| 275 | |
| 276 | /* Finally we check if the subtype_name we have generated from the |
| 277 | * audio_name in the trunc struct patches the codec_name of the |
| 278 | * given codec */ |
| 279 | if (strcasecmp(codec_name, codec->subtype_name) == 0) |
| 280 | return true; |
| 281 | |
| 282 | /* FIXME: It is questinable that the method to pick a compatible |
| 283 | * codec can work properly. Since this useses tcfg->audio_name, as |
| 284 | * a reference, which is set to "AMR/8000" permanently. |
| 285 | * tcfg->audio_name must be updated by the first connection that |
| 286 | * has been made on an endpoint, so that the second connection |
| 287 | * can make a meaningful decision here */ |
| 288 | |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | /*! Decide for one suitable codec |
| 293 | * \param[in] conn related rtp-connection. |
| 294 | * \returns 0 on success, -EINVAL on failure. */ |
| 295 | int mgcp_codec_decide(struct mgcp_conn_rtp *conn) |
| 296 | { |
| 297 | struct mgcp_rtp_end *rtp; |
| 298 | unsigned int i; |
| 299 | struct mgcp_endpoint *endp; |
| 300 | bool codec_assigned = false; |
| 301 | |
| 302 | endp = conn->conn->endp; |
| 303 | rtp = &conn->end; |
| 304 | |
| 305 | /* This function works on the results the SDP/LCO parser has extracted |
| 306 | * from the MGCP message. The goal is to select a suitable codec for |
| 307 | * the given connection. When transcoding is available, the first codec |
| 308 | * from the codec list is taken without further checking. When |
| 309 | * transcoding is not available, then the choice must be made more |
| 310 | * carefully. Each codec in the list is checked until one is found that |
| 311 | * is rated compatible. The rating is done by the helper function |
| 312 | * is_codec_compatible(), which does the actual checking. */ |
| 313 | for (i = 0; i < rtp->codecs_assigned; i++) { |
| 314 | /* When no transcoding is available, avoid codecs that would |
| 315 | * require transcoding. */ |
| 316 | if (endp->tcfg->no_audio_transcoding && !is_codec_compatible(endp, &rtp->codecs[i])) { |
| 317 | LOGP(DLMGCP, LOGL_NOTICE, "transcoding not available, skipping codec: %d/%s\n", |
| 318 | rtp->codecs[i].payload_type, rtp->codecs[i].subtype_name); |
| 319 | continue; |
| 320 | } |
| 321 | |
| 322 | rtp->codec = &rtp->codecs[i]; |
| 323 | codec_assigned = true; |
| 324 | break; |
| 325 | } |
| 326 | |
| 327 | /* FIXME: To the reviewes: This is problematic. I do not get why we |
| 328 | * need to reset the packet_duration_ms depending on the codec |
| 329 | * selection. I thought it were all 20ms? Is this to address some |
| 330 | * cornercase. (This piece of code was in the code path before, |
| 331 | * together with the note: "TODO/XXX: Store this per codec and derive |
| 332 | * it on use" */ |
| 333 | if (codec_assigned) { |
| 334 | if (rtp->maximum_packet_time >= 0 |
| 335 | && rtp->maximum_packet_time * rtp->codec->frame_duration_den > |
| 336 | rtp->codec->frame_duration_num * 1500) |
| 337 | rtp->packet_duration_ms = 0; |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | return -EINVAL; |
| 343 | } |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 344 | |
| 345 | /* Compare two codecs, all parameters must match up, except for the payload type |
| 346 | * number. */ |
| 347 | static bool codecs_cmp(struct mgcp_rtp_codec *codec_a, struct mgcp_rtp_codec *codec_b) |
| 348 | { |
| 349 | if (codec_a->rate != codec_b->rate) |
| 350 | return false; |
| 351 | if (codec_a->channels != codec_b->channels) |
| 352 | return false; |
| 353 | if (codec_a->frame_duration_num != codec_b->frame_duration_num) |
| 354 | return false; |
| 355 | if (codec_a->frame_duration_den != codec_b->frame_duration_den) |
| 356 | return false; |
| 357 | if (strcmp(codec_a->audio_name, codec_b->audio_name)) |
| 358 | return false; |
| 359 | if (strcmp(codec_a->subtype_name, codec_b->subtype_name)) |
| 360 | return false; |
| 361 | |
| 362 | return true; |
| 363 | } |
| 364 | |
| 365 | /*! Translate a given payload type number that belongs to the packet of a |
| 366 | * source connection to the equivalent payload type number that matches the |
| 367 | * configuration of a destination connection. |
| 368 | * \param[in] conn_src related source rtp-connection. |
| 369 | * \param[in] conn_dst related destination rtp-connection. |
| 370 | * \param[in] payload_type number from the source packet or source connection. |
| 371 | * \returns translated payload type number on success, -EINVAL on failure. */ |
| 372 | int mgcp_codec_pt_translate(struct mgcp_conn_rtp *conn_src, struct mgcp_conn_rtp *conn_dst, int payload_type) |
| 373 | { |
| 374 | struct mgcp_rtp_end *rtp_src; |
| 375 | struct mgcp_rtp_end *rtp_dst; |
| 376 | struct mgcp_rtp_codec *codec_src = NULL; |
| 377 | struct mgcp_rtp_codec *codec_dst = NULL; |
| 378 | unsigned int i; |
| 379 | unsigned int codecs_assigned; |
| 380 | |
| 381 | rtp_src = &conn_src->end; |
| 382 | rtp_dst = &conn_dst->end; |
| 383 | |
| 384 | /* Find the codec information that is used on the source side */ |
| 385 | codecs_assigned = rtp_src->codecs_assigned; |
| 386 | OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS); |
| 387 | for (i = 0; i < codecs_assigned; i++) { |
| 388 | if (payload_type == rtp_src->codecs[i].payload_type) { |
| 389 | codec_src = &rtp_src->codecs[i]; |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | if (!codec_src) |
| 394 | return -EINVAL; |
| 395 | |
| 396 | /* Use the codec infrmation from the source and try to find the |
| 397 | * equivalent of it on the destination side */ |
| 398 | codecs_assigned = rtp_dst->codecs_assigned; |
| 399 | OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS); |
| 400 | for (i = 0; i < codecs_assigned; i++) { |
| 401 | if (codecs_cmp(codec_src, &rtp_dst->codecs[i])) { |
| 402 | codec_dst = &rtp_dst->codecs[i]; |
| 403 | break; |
| 404 | } |
| 405 | } |
| 406 | if (!codec_dst) |
| 407 | return -EINVAL; |
| 408 | |
| 409 | return codec_dst->payload_type; |
| 410 | } |