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> |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 22 | #include <osmocom/mgcp/mgcp_trunk.h> |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | |
| 25 | /* Helper function to dump codec information of a specified codec to a printable |
| 26 | * string, used by dump_codec_summary() */ |
| 27 | static char *dump_codec(struct mgcp_rtp_codec *codec) |
| 28 | { |
| 29 | static char str[256]; |
| 30 | char *pt_str; |
| 31 | |
| 32 | if (codec->payload_type > 76) |
| 33 | pt_str = "DYNAMIC"; |
| 34 | else if (codec->payload_type > 72) |
| 35 | pt_str = "RESERVED <!>"; |
| 36 | else if (codec->payload_type != PTYPE_UNDEFINED) |
| 37 | pt_str = codec->subtype_name; |
| 38 | else |
| 39 | pt_str = "INVALID <!>"; |
| 40 | |
| 41 | snprintf(str, sizeof(str), "(pt:%i=%s, audio:%s subt=%s, rate=%u, ch=%i, t=%u/%u)", codec->payload_type, pt_str, |
| 42 | codec->audio_name, codec->subtype_name, codec->rate, codec->channels, codec->frame_duration_num, |
| 43 | codec->frame_duration_den); |
| 44 | return str; |
| 45 | } |
| 46 | |
| 47 | /*! Dump a summary of all negotiated codecs to debug log |
| 48 | * \param[in] conn related rtp-connection. */ |
| 49 | void mgcp_codec_summary(struct mgcp_conn_rtp *conn) |
| 50 | { |
| 51 | struct mgcp_rtp_end *rtp; |
| 52 | unsigned int i; |
| 53 | struct mgcp_rtp_codec *codec; |
| 54 | struct mgcp_endpoint *endp; |
| 55 | |
| 56 | rtp = &conn->end; |
| 57 | endp = conn->conn->endp; |
| 58 | |
| 59 | if (rtp->codecs_assigned == 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 60 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, "conn:%s no codecs available\n", |
| 61 | mgcp_conn_dump(conn->conn)); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 62 | return; |
| 63 | } |
| 64 | |
| 65 | /* Store parsed codec information */ |
| 66 | for (i = 0; i < rtp->codecs_assigned; i++) { |
| 67 | codec = &rtp->codecs[i]; |
| 68 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 69 | LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "conn:%s codecs[%u]:%s", |
| 70 | mgcp_conn_dump(conn->conn), i, dump_codec(codec)); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 71 | |
| 72 | if (codec == rtp->codec) |
| 73 | LOGPC(DLMGCP, LOGL_DEBUG, " [selected]"); |
| 74 | |
| 75 | LOGPC(DLMGCP, LOGL_DEBUG, "\n"); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /* Initalize or reset codec information with default data. */ |
Neels Hofmeyr | 667fa59 | 2019-08-08 21:59:01 +0200 | [diff] [blame] | 80 | static void codec_init(struct mgcp_rtp_codec *codec) |
| 81 | { |
| 82 | *codec = (struct mgcp_rtp_codec){ |
| 83 | .payload_type = -1, |
| 84 | .frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM, |
| 85 | .frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN, |
| 86 | .rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE, |
| 87 | .channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS, |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | static void codec_free(struct mgcp_rtp_codec *codec) |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 92 | { |
| 93 | if (codec->subtype_name) |
| 94 | talloc_free(codec->subtype_name); |
| 95 | if (codec->audio_name) |
| 96 | talloc_free(codec->audio_name); |
Neels Hofmeyr | 667fa59 | 2019-08-08 21:59:01 +0200 | [diff] [blame] | 97 | *codec = (struct mgcp_rtp_codec){}; |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /*! Initalize or reset codec information with default data. |
| 101 | * \param[out] conn related rtp-connection. */ |
| 102 | void mgcp_codec_reset_all(struct mgcp_conn_rtp *conn) |
| 103 | { |
Neels Hofmeyr | ce64f18 | 2019-08-08 22:07:31 +0200 | [diff] [blame] | 104 | int i; |
| 105 | for (i = 0; i < conn->end.codecs_assigned; i++) |
| 106 | codec_free(&conn->end.codecs[i]); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 107 | conn->end.codecs_assigned = 0; |
| 108 | conn->end.codec = NULL; |
| 109 | } |
| 110 | |
Neels Hofmeyr | 667fa59 | 2019-08-08 21:59:01 +0200 | [diff] [blame] | 111 | /*! Add codec configuration depending on payload type and/or codec name. This |
| 112 | * function uses the input parameters to extrapolate the full codec information. |
| 113 | * \param[out] codec configuration (caller provided memory). |
| 114 | * \param[out] conn related rtp-connection. |
| 115 | * \param[in] payload_type codec type id (e.g. 3 for GSM, -1 when undefined). |
| 116 | * \param[in] audio_name audio codec name, in uppercase (e.g. "GSM/8000/1"). |
| 117 | * \param[in] param optional codec parameters (set to NULL when unused). |
| 118 | * \returns 0 on success, -EINVAL on failure. */ |
| 119 | int mgcp_codec_add(struct mgcp_conn_rtp *conn, int payload_type, const char *audio_name, const struct mgcp_codec_param *param) |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 120 | { |
| 121 | int rate; |
| 122 | int channels; |
| 123 | char audio_codec[64]; |
Neels Hofmeyr | 667fa59 | 2019-08-08 21:59:01 +0200 | [diff] [blame] | 124 | struct mgcp_rtp_codec *codec; |
| 125 | unsigned int pt_offset = conn->end.codecs_assigned; |
| 126 | void *ctx = conn->conn; |
| 127 | |
| 128 | /* The amount of codecs we can store is limited, make sure we do not |
| 129 | * overrun this limit. */ |
| 130 | if (conn->end.codecs_assigned >= MGCP_MAX_CODECS) |
| 131 | return -EINVAL; |
| 132 | |
| 133 | /* First unused entry */ |
| 134 | codec = &conn->end.codecs[conn->end.codecs_assigned]; |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 135 | |
| 136 | /* Initalize the codec struct with some default data to begin with */ |
| 137 | codec_init(codec); |
| 138 | |
| 139 | if (payload_type != PTYPE_UNDEFINED) { |
| 140 | /* Make sure we do not get any reserved or undefined type numbers */ |
| 141 | /* See also: https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */ |
Neels Hofmeyr | b0cfa72 | 2019-08-08 23:39:56 +0200 | [diff] [blame] | 142 | if ((payload_type == 1 || payload_type == 2 || payload_type == 19) |
| 143 | || (payload_type >= 72 && payload_type <= 76) |
| 144 | || (payload_type >= 127)) { |
| 145 | LOGP(DLMGCP, LOGL_ERROR, "Cannot add codec, payload type number %d is reserved\n", |
| 146 | payload_type); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 147 | goto error; |
Neels Hofmeyr | b0cfa72 | 2019-08-08 23:39:56 +0200 | [diff] [blame] | 148 | } |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 149 | |
| 150 | codec->payload_type = payload_type; |
| 151 | } |
| 152 | |
| 153 | /* When no audio name is given, we are forced to use the payload |
| 154 | * type to generate the audio name. This is only possible for |
| 155 | * non dynamic payload types, which are statically defined */ |
| 156 | if (!audio_name) { |
| 157 | switch (payload_type) { |
| 158 | case 0: |
| 159 | audio_name = talloc_strdup(ctx, "PCMU/8000/1"); |
| 160 | break; |
| 161 | case 3: |
| 162 | audio_name = talloc_strdup(ctx, "GSM/8000/1"); |
| 163 | break; |
| 164 | case 8: |
| 165 | audio_name = talloc_strdup(ctx, "PCMA/8000/1"); |
| 166 | break; |
| 167 | case 18: |
| 168 | audio_name = talloc_strdup(ctx, "G729/8000/1"); |
| 169 | break; |
| 170 | default: |
| 171 | /* The given payload type is not known to us, or it |
| 172 | * it is a dynamic payload type for which we do not |
| 173 | * know the audio name. We must give up here */ |
Neels Hofmeyr | b0cfa72 | 2019-08-08 23:39:56 +0200 | [diff] [blame] | 174 | LOGP(DLMGCP, LOGL_ERROR, "No audio codec name given, and payload type %d unknown\n", |
| 175 | payload_type); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 176 | goto error; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /* Now we extract the codec subtype name, rate and channels. The latter |
| 181 | * two are optional. If they are not present we use the safe defaults |
| 182 | * above. */ |
Neels Hofmeyr | a468b0f | 2019-08-28 04:56:49 +0200 | [diff] [blame] | 183 | if (strlen(audio_name) >= sizeof(audio_codec)) { |
Neels Hofmeyr | b0cfa72 | 2019-08-08 23:39:56 +0200 | [diff] [blame] | 184 | LOGP(DLMGCP, LOGL_ERROR, "Audio codec too long: %s\n", osmo_quote_str(audio_name, -1)); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 185 | goto error; |
Neels Hofmeyr | b0cfa72 | 2019-08-08 23:39:56 +0200 | [diff] [blame] | 186 | } |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 187 | channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS; |
| 188 | rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE; |
Neels Hofmeyr | b0cfa72 | 2019-08-08 23:39:56 +0200 | [diff] [blame] | 189 | if (sscanf(audio_name, "%63[^/]/%d/%d", audio_codec, &rate, &channels) < 1) { |
| 190 | LOGP(DLMGCP, LOGL_ERROR, "Invalid audio codec: %s\n", osmo_quote_str(audio_name, -1)); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 191 | goto error; |
Neels Hofmeyr | b0cfa72 | 2019-08-08 23:39:56 +0200 | [diff] [blame] | 192 | } |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 193 | |
| 194 | /* Note: We only accept configurations with one audio channel! */ |
Neels Hofmeyr | b0cfa72 | 2019-08-08 23:39:56 +0200 | [diff] [blame] | 195 | if (channels != 1) { |
| 196 | LOGP(DLMGCP, LOGL_ERROR, "Cannot handle audio codec with more than one channel: %s\n", |
| 197 | osmo_quote_str(audio_name, -1)); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 198 | goto error; |
Neels Hofmeyr | b0cfa72 | 2019-08-08 23:39:56 +0200 | [diff] [blame] | 199 | } |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 200 | |
| 201 | codec->rate = rate; |
| 202 | codec->channels = channels; |
| 203 | codec->subtype_name = talloc_strdup(ctx, audio_codec); |
| 204 | codec->audio_name = talloc_strdup(ctx, audio_name); |
| 205 | codec->payload_type = payload_type; |
| 206 | |
| 207 | if (!strcmp(audio_codec, "G729")) { |
| 208 | codec->frame_duration_num = 10; |
| 209 | codec->frame_duration_den = 1000; |
| 210 | } else { |
| 211 | codec->frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM; |
| 212 | codec->frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN; |
| 213 | } |
| 214 | |
| 215 | /* Derive the payload type if it is unknown */ |
| 216 | if (codec->payload_type == PTYPE_UNDEFINED) { |
Neels Hofmeyr | b0cfa72 | 2019-08-08 23:39:56 +0200 | [diff] [blame] | 217 | /* TODO: This is semi dead code, see OS#4150 */ |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 218 | |
| 219 | /* For the known codecs from the static range we restore |
| 220 | * the IANA or 3GPP assigned payload type number */ |
| 221 | if (codec->rate == 8000 && codec->channels == 1) { |
| 222 | /* See also: https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */ |
| 223 | if (!strcmp(codec->subtype_name, "GSM")) |
| 224 | codec->payload_type = 3; |
| 225 | else if (!strcmp(codec->subtype_name, "PCMA")) |
| 226 | codec->payload_type = 8; |
| 227 | else if (!strcmp(codec->subtype_name, "PCMU")) |
| 228 | codec->payload_type = 0; |
| 229 | else if (!strcmp(codec->subtype_name, "G729")) |
| 230 | codec->payload_type = 18; |
| 231 | |
| 232 | /* See also: 3GPP TS 48.103, chapter 5.4.2.2 RTP Payload |
| 233 | * Note: These are not fixed payload types as the IANA |
| 234 | * defined once, they still remain dymanic payload |
| 235 | * types, but with a payload type number preference. */ |
| 236 | else if (!strcmp(codec->subtype_name, "GSM-EFR")) |
| 237 | codec->payload_type = 110; |
| 238 | else if (!strcmp(codec->subtype_name, "GSM-HR-08")) |
| 239 | codec->payload_type = 111; |
| 240 | else if (!strcmp(codec->subtype_name, "AMR")) |
| 241 | codec->payload_type = 112; |
| 242 | else if (!strcmp(codec->subtype_name, "AMR-WB")) |
| 243 | codec->payload_type = 113; |
| 244 | } |
| 245 | |
| 246 | /* If we could not determine a payload type we assume that |
| 247 | * we are dealing with a codec from the dynamic range. We |
| 248 | * choose a fixed identifier from 96-109. (Note: normally, |
| 249 | * the dynamic payload type rante is from 96-127, but from |
| 250 | * 110 onwards 3gpp defines prefered codec types, which are |
| 251 | * also fixed, see above) */ |
| 252 | if (codec->payload_type < 0) { |
Neels Hofmeyr | b0cfa72 | 2019-08-08 23:39:56 +0200 | [diff] [blame] | 253 | /* FIXME: pt_offset is completely unrelated and useless here, any of those numbers may already |
| 254 | * have been added to the codecs. Instead, there should be an iterator checking for an actually |
| 255 | * unused dynamic payload type number. */ |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 256 | codec->payload_type = 96 + pt_offset; |
Neels Hofmeyr | b0cfa72 | 2019-08-08 23:39:56 +0200 | [diff] [blame] | 257 | if (codec->payload_type > 109) { |
| 258 | LOGP(DLMGCP, LOGL_ERROR, "Ran out of payload type numbers to assign dynamically\n"); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 259 | goto error; |
Neels Hofmeyr | b0cfa72 | 2019-08-08 23:39:56 +0200 | [diff] [blame] | 260 | } |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 264 | /* Copy over optional codec parameters */ |
| 265 | if (param) { |
| 266 | codec->param = *param; |
| 267 | codec->param_present = true; |
| 268 | } else |
| 269 | codec->param_present = false; |
| 270 | |
Neels Hofmeyr | 667fa59 | 2019-08-08 21:59:01 +0200 | [diff] [blame] | 271 | conn->end.codecs_assigned++; |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 272 | return 0; |
| 273 | error: |
| 274 | /* Make sure we leave a clean codec entry on error. */ |
Neels Hofmeyr | 667fa59 | 2019-08-08 21:59:01 +0200 | [diff] [blame] | 275 | codec_free(codec); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 276 | return -EINVAL; |
| 277 | } |
| 278 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 279 | /* Check if the given codec is applicable on the specified endpoint |
| 280 | * Helper function for mgcp_codec_decide() */ |
| 281 | static bool is_codec_compatible(const struct mgcp_endpoint *endp, const struct mgcp_rtp_codec *codec) |
| 282 | { |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 283 | /* A codec name must be set, if not, this might mean that the codec |
| 284 | * (payload type) that was assigned is unknown to us so we must stop |
| 285 | * here. */ |
| 286 | if (!codec->subtype_name) |
| 287 | return false; |
| 288 | |
Philipp Maier | 7f90ddb | 2020-06-02 21:52:53 +0200 | [diff] [blame] | 289 | /* FIXME: implement meaningful checks to make sure that the given codec |
| 290 | * is compatible with the given endpoint */ |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 291 | |
Philipp Maier | 7f90ddb | 2020-06-02 21:52:53 +0200 | [diff] [blame] | 292 | return true; |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /*! Decide for one suitable codec |
| 296 | * \param[in] conn related rtp-connection. |
| 297 | * \returns 0 on success, -EINVAL on failure. */ |
| 298 | int mgcp_codec_decide(struct mgcp_conn_rtp *conn) |
| 299 | { |
| 300 | struct mgcp_rtp_end *rtp; |
| 301 | unsigned int i; |
| 302 | struct mgcp_endpoint *endp; |
| 303 | bool codec_assigned = false; |
| 304 | |
| 305 | endp = conn->conn->endp; |
| 306 | rtp = &conn->end; |
| 307 | |
| 308 | /* This function works on the results the SDP/LCO parser has extracted |
| 309 | * from the MGCP message. The goal is to select a suitable codec for |
| 310 | * the given connection. When transcoding is available, the first codec |
| 311 | * from the codec list is taken without further checking. When |
| 312 | * transcoding is not available, then the choice must be made more |
| 313 | * carefully. Each codec in the list is checked until one is found that |
| 314 | * is rated compatible. The rating is done by the helper function |
| 315 | * is_codec_compatible(), which does the actual checking. */ |
| 316 | for (i = 0; i < rtp->codecs_assigned; i++) { |
| 317 | /* When no transcoding is available, avoid codecs that would |
| 318 | * require transcoding. */ |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 319 | if (endp->trunk->no_audio_transcoding && !is_codec_compatible(endp, &rtp->codecs[i])) { |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 320 | LOGP(DLMGCP, LOGL_NOTICE, "transcoding not available, skipping codec: %d/%s\n", |
| 321 | rtp->codecs[i].payload_type, rtp->codecs[i].subtype_name); |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | rtp->codec = &rtp->codecs[i]; |
| 326 | codec_assigned = true; |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | /* FIXME: To the reviewes: This is problematic. I do not get why we |
| 331 | * need to reset the packet_duration_ms depending on the codec |
| 332 | * selection. I thought it were all 20ms? Is this to address some |
| 333 | * cornercase. (This piece of code was in the code path before, |
| 334 | * together with the note: "TODO/XXX: Store this per codec and derive |
| 335 | * it on use" */ |
| 336 | if (codec_assigned) { |
| 337 | if (rtp->maximum_packet_time >= 0 |
| 338 | && rtp->maximum_packet_time * rtp->codec->frame_duration_den > |
| 339 | rtp->codec->frame_duration_num * 1500) |
| 340 | rtp->packet_duration_ms = 0; |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | return -EINVAL; |
| 346 | } |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 347 | |
Neels Hofmeyr | 16b637b | 2019-08-08 22:47:10 +0200 | [diff] [blame] | 348 | /* Return true if octet-aligned is set in the given codec. Default to octet-aligned=0, i.e. bandwidth-efficient mode. |
| 349 | * See RFC4867 "RTP Payload Format for AMR and AMR-WB" sections "8.1. AMR Media Type Registration" and "8.2. AMR-WB |
| 350 | * Media Type Registration": |
| 351 | * |
| 352 | * octet-align: Permissible values are 0 and 1. If 1, octet-aligned |
| 353 | * operation SHALL be used. If 0 or if not present, |
| 354 | * bandwidth-efficient operation is employed. |
| 355 | * |
| 356 | * https://tools.ietf.org/html/rfc4867 |
| 357 | */ |
| 358 | static bool amr_is_octet_aligned(const struct mgcp_rtp_codec *codec) |
| 359 | { |
| 360 | if (!codec->param_present) |
| 361 | return false; |
| 362 | if (!codec->param.amr_octet_aligned_present) |
| 363 | return false; |
| 364 | return codec->param.amr_octet_aligned; |
| 365 | } |
| 366 | |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 367 | /* Compare two codecs, all parameters must match up, except for the payload type |
| 368 | * number. */ |
Neels Hofmeyr | 782d607 | 2019-08-09 00:51:21 +0200 | [diff] [blame] | 369 | static bool codecs_same(struct mgcp_rtp_codec *codec_a, struct mgcp_rtp_codec *codec_b) |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 370 | { |
| 371 | if (codec_a->rate != codec_b->rate) |
| 372 | return false; |
| 373 | if (codec_a->channels != codec_b->channels) |
| 374 | return false; |
| 375 | if (codec_a->frame_duration_num != codec_b->frame_duration_num) |
| 376 | return false; |
| 377 | if (codec_a->frame_duration_den != codec_b->frame_duration_den) |
| 378 | return false; |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 379 | if (strcmp(codec_a->subtype_name, codec_b->subtype_name)) |
| 380 | return false; |
Neels Hofmeyr | 16b637b | 2019-08-08 22:47:10 +0200 | [diff] [blame] | 381 | if (!strcmp(codec_a->subtype_name, "AMR")) { |
| 382 | if (amr_is_octet_aligned(codec_a) != amr_is_octet_aligned(codec_b)) |
| 383 | return false; |
| 384 | } |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 385 | |
| 386 | return true; |
| 387 | } |
| 388 | |
| 389 | /*! Translate a given payload type number that belongs to the packet of a |
| 390 | * source connection to the equivalent payload type number that matches the |
| 391 | * configuration of a destination connection. |
| 392 | * \param[in] conn_src related source rtp-connection. |
| 393 | * \param[in] conn_dst related destination rtp-connection. |
| 394 | * \param[in] payload_type number from the source packet or source connection. |
| 395 | * \returns translated payload type number on success, -EINVAL on failure. */ |
| 396 | int mgcp_codec_pt_translate(struct mgcp_conn_rtp *conn_src, struct mgcp_conn_rtp *conn_dst, int payload_type) |
| 397 | { |
| 398 | struct mgcp_rtp_end *rtp_src; |
| 399 | struct mgcp_rtp_end *rtp_dst; |
| 400 | struct mgcp_rtp_codec *codec_src = NULL; |
| 401 | struct mgcp_rtp_codec *codec_dst = NULL; |
| 402 | unsigned int i; |
| 403 | unsigned int codecs_assigned; |
| 404 | |
| 405 | rtp_src = &conn_src->end; |
| 406 | rtp_dst = &conn_dst->end; |
| 407 | |
| 408 | /* Find the codec information that is used on the source side */ |
| 409 | codecs_assigned = rtp_src->codecs_assigned; |
| 410 | OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS); |
| 411 | for (i = 0; i < codecs_assigned; i++) { |
| 412 | if (payload_type == rtp_src->codecs[i].payload_type) { |
| 413 | codec_src = &rtp_src->codecs[i]; |
| 414 | break; |
| 415 | } |
| 416 | } |
| 417 | if (!codec_src) |
| 418 | return -EINVAL; |
| 419 | |
| 420 | /* Use the codec infrmation from the source and try to find the |
| 421 | * equivalent of it on the destination side */ |
| 422 | codecs_assigned = rtp_dst->codecs_assigned; |
| 423 | OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS); |
| 424 | for (i = 0; i < codecs_assigned; i++) { |
Neels Hofmeyr | 782d607 | 2019-08-09 00:51:21 +0200 | [diff] [blame] | 425 | if (codecs_same(codec_src, &rtp_dst->codecs[i])) { |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 426 | codec_dst = &rtp_dst->codecs[i]; |
| 427 | break; |
| 428 | } |
| 429 | } |
| 430 | if (!codec_dst) |
| 431 | return -EINVAL; |
| 432 | |
| 433 | return codec_dst->payload_type; |
| 434 | } |