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 | */ |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 20 | #include <osmocom/mgcp/mgcp.h> |
| 21 | #include <osmocom/mgcp/osmux.h> |
| 22 | #include <osmocom/mgcp/mgcp_conn.h> |
| 23 | #include <osmocom/mgcp/mgcp_protocol.h> |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 24 | #include <osmocom/mgcp/mgcp_endp.h> |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 25 | #include <osmocom/mgcp/mgcp_trunk.h> |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 26 | #include <osmocom/mgcp/mgcp_codec.h> |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 27 | #include <errno.h> |
| 28 | |
| 29 | /* Helper function to dump codec information of a specified codec to a printable |
| 30 | * string, used by dump_codec_summary() */ |
| 31 | static char *dump_codec(struct mgcp_rtp_codec *codec) |
| 32 | { |
| 33 | static char str[256]; |
| 34 | char *pt_str; |
| 35 | |
| 36 | if (codec->payload_type > 76) |
| 37 | pt_str = "DYNAMIC"; |
| 38 | else if (codec->payload_type > 72) |
| 39 | pt_str = "RESERVED <!>"; |
| 40 | else if (codec->payload_type != PTYPE_UNDEFINED) |
| 41 | pt_str = codec->subtype_name; |
| 42 | else |
| 43 | pt_str = "INVALID <!>"; |
| 44 | |
| 45 | snprintf(str, sizeof(str), "(pt:%i=%s, audio:%s subt=%s, rate=%u, ch=%i, t=%u/%u)", codec->payload_type, pt_str, |
| 46 | codec->audio_name, codec->subtype_name, codec->rate, codec->channels, codec->frame_duration_num, |
| 47 | codec->frame_duration_den); |
| 48 | return str; |
| 49 | } |
| 50 | |
| 51 | /*! Dump a summary of all negotiated codecs to debug log |
| 52 | * \param[in] conn related rtp-connection. */ |
| 53 | void mgcp_codec_summary(struct mgcp_conn_rtp *conn) |
| 54 | { |
| 55 | struct mgcp_rtp_end *rtp; |
| 56 | unsigned int i; |
| 57 | struct mgcp_rtp_codec *codec; |
| 58 | struct mgcp_endpoint *endp; |
| 59 | |
| 60 | rtp = &conn->end; |
| 61 | endp = conn->conn->endp; |
| 62 | |
| 63 | if (rtp->codecs_assigned == 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 64 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, "conn:%s no codecs available\n", |
| 65 | mgcp_conn_dump(conn->conn)); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 66 | return; |
| 67 | } |
| 68 | |
| 69 | /* Store parsed codec information */ |
| 70 | for (i = 0; i < rtp->codecs_assigned; i++) { |
| 71 | codec = &rtp->codecs[i]; |
| 72 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 73 | LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "conn:%s codecs[%u]:%s", |
| 74 | mgcp_conn_dump(conn->conn), i, dump_codec(codec)); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 75 | |
| 76 | if (codec == rtp->codec) |
| 77 | LOGPC(DLMGCP, LOGL_DEBUG, " [selected]"); |
| 78 | |
| 79 | LOGPC(DLMGCP, LOGL_DEBUG, "\n"); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /* Initalize or reset codec information with default data. */ |
Neels Hofmeyr | 667fa59 | 2019-08-08 21:59:01 +0200 | [diff] [blame] | 84 | static void codec_init(struct mgcp_rtp_codec *codec) |
| 85 | { |
| 86 | *codec = (struct mgcp_rtp_codec){ |
| 87 | .payload_type = -1, |
| 88 | .frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM, |
| 89 | .frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN, |
| 90 | .rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE, |
| 91 | .channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS, |
Eric | 2ebcf5c | 2021-08-09 23:26:48 +0200 | [diff] [blame] | 92 | .subtype_name = "", |
| 93 | .audio_name = "", |
Neels Hofmeyr | 667fa59 | 2019-08-08 21:59:01 +0200 | [diff] [blame] | 94 | }; |
| 95 | } |
| 96 | |
| 97 | static void codec_free(struct mgcp_rtp_codec *codec) |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 98 | { |
Neels Hofmeyr | 667fa59 | 2019-08-08 21:59:01 +0200 | [diff] [blame] | 99 | *codec = (struct mgcp_rtp_codec){}; |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /*! Initalize or reset codec information with default data. |
| 103 | * \param[out] conn related rtp-connection. */ |
| 104 | void mgcp_codec_reset_all(struct mgcp_conn_rtp *conn) |
| 105 | { |
Neels Hofmeyr | ce64f18 | 2019-08-08 22:07:31 +0200 | [diff] [blame] | 106 | int i; |
| 107 | for (i = 0; i < conn->end.codecs_assigned; i++) |
| 108 | codec_free(&conn->end.codecs[i]); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 109 | conn->end.codecs_assigned = 0; |
| 110 | conn->end.codec = NULL; |
| 111 | } |
| 112 | |
Neels Hofmeyr | 667fa59 | 2019-08-08 21:59:01 +0200 | [diff] [blame] | 113 | /*! Add codec configuration depending on payload type and/or codec name. This |
| 114 | * function uses the input parameters to extrapolate the full codec information. |
| 115 | * \param[out] codec configuration (caller provided memory). |
| 116 | * \param[out] conn related rtp-connection. |
| 117 | * \param[in] payload_type codec type id (e.g. 3 for GSM, -1 when undefined). |
| 118 | * \param[in] audio_name audio codec name, in uppercase (e.g. "GSM/8000/1"). |
| 119 | * \param[in] param optional codec parameters (set to NULL when unused). |
| 120 | * \returns 0 on success, -EINVAL on failure. */ |
| 121 | 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] | 122 | { |
| 123 | int rate; |
| 124 | int channels; |
Neels Hofmeyr | 667fa59 | 2019-08-08 21:59:01 +0200 | [diff] [blame] | 125 | struct mgcp_rtp_codec *codec; |
| 126 | unsigned int pt_offset = conn->end.codecs_assigned; |
Neels Hofmeyr | 667fa59 | 2019-08-08 21:59:01 +0200 | [diff] [blame] | 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: |
Eric | 2ebcf5c | 2021-08-09 23:26:48 +0200 | [diff] [blame] | 159 | strcpy(codec->audio_name, "PCMU/8000/1"); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 160 | break; |
| 161 | case 3: |
Eric | 2ebcf5c | 2021-08-09 23:26:48 +0200 | [diff] [blame] | 162 | strcpy(codec->audio_name, "GSM/8000/1"); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 163 | break; |
| 164 | case 8: |
Eric | 2ebcf5c | 2021-08-09 23:26:48 +0200 | [diff] [blame] | 165 | strcpy(codec->audio_name, "PCMA/8000/1"); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 166 | break; |
| 167 | case 18: |
Eric | 2ebcf5c | 2021-08-09 23:26:48 +0200 | [diff] [blame] | 168 | strcpy(codec->audio_name, "G729/8000/1"); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 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 | } |
Eric | 2ebcf5c | 2021-08-09 23:26:48 +0200 | [diff] [blame] | 178 | } else { |
Vadim Yanitskiy | 0c04500 | 2021-10-21 00:07:00 +0300 | [diff] [blame] | 179 | OSMO_STRLCPY_ARRAY(codec->audio_name, audio_name); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* Now we extract the codec subtype name, rate and channels. The latter |
| 183 | * two are optional. If they are not present we use the safe defaults |
| 184 | * above. */ |
Eric | 2ebcf5c | 2021-08-09 23:26:48 +0200 | [diff] [blame] | 185 | if (strlen(codec->audio_name) >= sizeof(codec->subtype_name)) { |
| 186 | LOGP(DLMGCP, LOGL_ERROR, "Audio codec too long: %s\n", osmo_quote_str(codec->audio_name, -1)); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 187 | goto error; |
Neels Hofmeyr | b0cfa72 | 2019-08-08 23:39:56 +0200 | [diff] [blame] | 188 | } |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 189 | channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS; |
| 190 | rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE; |
Eric | 2ebcf5c | 2021-08-09 23:26:48 +0200 | [diff] [blame] | 191 | if (sscanf(codec->audio_name, "%63[^/]/%d/%d", codec->subtype_name, &rate, &channels) < 1) { |
| 192 | LOGP(DLMGCP, LOGL_ERROR, "Invalid audio codec: %s\n", osmo_quote_str(codec->audio_name, -1)); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 193 | goto error; |
Neels Hofmeyr | b0cfa72 | 2019-08-08 23:39:56 +0200 | [diff] [blame] | 194 | } |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 195 | |
| 196 | /* Note: We only accept configurations with one audio channel! */ |
Neels Hofmeyr | b0cfa72 | 2019-08-08 23:39:56 +0200 | [diff] [blame] | 197 | if (channels != 1) { |
| 198 | LOGP(DLMGCP, LOGL_ERROR, "Cannot handle audio codec with more than one channel: %s\n", |
Eric | 2ebcf5c | 2021-08-09 23:26:48 +0200 | [diff] [blame] | 199 | osmo_quote_str(codec->audio_name, -1)); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 200 | goto error; |
Neels Hofmeyr | b0cfa72 | 2019-08-08 23:39:56 +0200 | [diff] [blame] | 201 | } |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 202 | |
| 203 | codec->rate = rate; |
| 204 | codec->channels = channels; |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 205 | codec->payload_type = payload_type; |
| 206 | |
Eric | 2ebcf5c | 2021-08-09 23:26:48 +0200 | [diff] [blame] | 207 | if (!strcmp(codec->subtype_name, "G729")) { |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 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 | |
Neels Hofmeyr | 16b637b | 2019-08-08 22:47:10 +0200 | [diff] [blame] | 279 | /* Return true if octet-aligned is set in the given codec. Default to octet-aligned=0, i.e. bandwidth-efficient mode. |
| 280 | * See RFC4867 "RTP Payload Format for AMR and AMR-WB" sections "8.1. AMR Media Type Registration" and "8.2. AMR-WB |
| 281 | * Media Type Registration": |
| 282 | * |
| 283 | * octet-align: Permissible values are 0 and 1. If 1, octet-aligned |
| 284 | * operation SHALL be used. If 0 or if not present, |
| 285 | * bandwidth-efficient operation is employed. |
| 286 | * |
| 287 | * https://tools.ietf.org/html/rfc4867 |
| 288 | */ |
Pau Espin Pedrol | 2799ff9 | 2022-01-03 15:24:59 +0100 | [diff] [blame] | 289 | bool mgcp_codec_amr_is_octet_aligned(const struct mgcp_rtp_codec *codec) |
Neels Hofmeyr | 16b637b | 2019-08-08 22:47:10 +0200 | [diff] [blame] | 290 | { |
| 291 | if (!codec->param_present) |
| 292 | return false; |
| 293 | if (!codec->param.amr_octet_aligned_present) |
| 294 | return false; |
| 295 | return codec->param.amr_octet_aligned; |
| 296 | } |
| 297 | |
Philipp Maier | 621e866 | 2023-03-22 16:53:30 +0100 | [diff] [blame] | 298 | /* Compare two codecs, all parameters must match up */ |
Neels Hofmeyr | 782d607 | 2019-08-09 00:51:21 +0200 | [diff] [blame] | 299 | 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] | 300 | { |
Philipp Maier | 621e866 | 2023-03-22 16:53:30 +0100 | [diff] [blame] | 301 | /* All codec properties must match up, except the payload type number. Even though standardisd payload numbers |
| 302 | * exist for certain situations, the call agent may still assign them freely. Hence we must not insist on equal |
| 303 | * payload type numbers. Also the audio_name is not checked since it is already parsed into subtype_name, rate, |
| 304 | * and channels, which are checked. */ |
| 305 | if (strcmp(codec_a->subtype_name, codec_b->subtype_name)) |
| 306 | return false; |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 307 | if (codec_a->rate != codec_b->rate) |
| 308 | return false; |
| 309 | if (codec_a->channels != codec_b->channels) |
| 310 | return false; |
| 311 | if (codec_a->frame_duration_num != codec_b->frame_duration_num) |
| 312 | return false; |
| 313 | if (codec_a->frame_duration_den != codec_b->frame_duration_den) |
| 314 | return false; |
Philipp Maier | 621e866 | 2023-03-22 16:53:30 +0100 | [diff] [blame] | 315 | |
| 316 | /* AMR payload may be formatted in two different payload formats, it is still the same codec but since the |
| 317 | * formatting of the payload is different, conversation is required, so we must treat it as a different |
| 318 | * codec here. */ |
| 319 | if (strcmp(codec_a->subtype_name, "AMR") == 0) { |
Philipp Maier | ec967d7 | 2023-03-22 16:20:37 +0100 | [diff] [blame] | 320 | if (mgcp_codec_amr_is_octet_aligned(codec_a) != mgcp_codec_amr_is_octet_aligned(codec_b)) |
| 321 | return false; |
| 322 | } |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 323 | |
| 324 | return true; |
| 325 | } |
| 326 | |
Philipp Maier | 621e866 | 2023-03-22 16:53:30 +0100 | [diff] [blame] | 327 | /* Compare two codecs, all parameters must match up, except parameters related to payload formatting (not checked). */ |
| 328 | static bool codecs_convertible(struct mgcp_rtp_codec *codec_a, struct mgcp_rtp_codec *codec_b) |
| 329 | { |
| 330 | /* OsmoMGW currently has no ability to transcode from one codec to another. However OsmoMGW is still able to |
| 331 | * translate between different payload formats as long as the encoded voice data itself does not change. |
| 332 | * Therefore we must insist on equal codecs but still allow different payload formatting. */ |
Philipp Maier | 2bfa708 | 2023-05-23 13:58:38 +0200 | [diff] [blame] | 333 | |
| 334 | /* In 3G IuUP, AMR may be encapsulated in IuFP, this means even though the codec name and negotiated rate is |
| 335 | * different, the formatting can still be converted by OsmoMGW. Therefore we won't insist on equal |
| 336 | * subtype_name and rate if we detect IuFP and AMR is used on the same tandem. */ |
| 337 | if (strcmp(codec_a->subtype_name, "AMR") == 0 && strcmp(codec_b->subtype_name, "VND.3GPP.IUFP") == 0) |
| 338 | goto iufp; |
| 339 | if (strcmp(codec_a->subtype_name, "VND.3GPP.IUFP") == 0 && strcmp(codec_b->subtype_name, "AMR") == 0) |
| 340 | goto iufp; |
| 341 | |
Philipp Maier | 621e866 | 2023-03-22 16:53:30 +0100 | [diff] [blame] | 342 | if (strcmp(codec_a->subtype_name, codec_b->subtype_name)) |
| 343 | return false; |
| 344 | if (codec_a->rate != codec_b->rate) |
| 345 | return false; |
Philipp Maier | 2bfa708 | 2023-05-23 13:58:38 +0200 | [diff] [blame] | 346 | iufp: |
Philipp Maier | 621e866 | 2023-03-22 16:53:30 +0100 | [diff] [blame] | 347 | if (codec_a->channels != codec_b->channels) |
| 348 | return false; |
| 349 | if (codec_a->frame_duration_num != codec_b->frame_duration_num) |
| 350 | return false; |
| 351 | if (codec_a->frame_duration_den != codec_b->frame_duration_den) |
| 352 | return false; |
| 353 | |
| 354 | return true; |
| 355 | } |
| 356 | |
Philipp Maier | 4c4d227 | 2023-04-26 15:45:17 +0200 | [diff] [blame] | 357 | struct mgcp_rtp_codec *mgcp_codec_find_same(struct mgcp_conn_rtp *conn, struct mgcp_rtp_codec *codec) |
| 358 | { |
| 359 | struct mgcp_rtp_end *rtp_end; |
| 360 | unsigned int i; |
| 361 | unsigned int codecs_assigned; |
| 362 | |
| 363 | rtp_end = &conn->end; |
| 364 | |
| 365 | /* Use the codec information from the source and try to find the equivalent of it on the destination side. In |
| 366 | * the first run we will look for an exact match. */ |
| 367 | codecs_assigned = rtp_end->codecs_assigned; |
| 368 | OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS); |
| 369 | for (i = 0; i < codecs_assigned; i++) { |
| 370 | if (codecs_same(codec, &rtp_end->codecs[i])) { |
| 371 | return &rtp_end->codecs[i]; |
| 372 | break; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | return NULL; |
| 377 | } |
| 378 | |
| 379 | /* For a given codec, find a convertible codec in the given connection. */ |
| 380 | static struct mgcp_rtp_codec *codec_find_convertible(struct mgcp_conn_rtp *conn, struct mgcp_rtp_codec *codec) |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 381 | { |
Philipp Maier | 9dd80ca | 2023-03-27 15:45:36 +0200 | [diff] [blame] | 382 | struct mgcp_rtp_end *rtp_end; |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 383 | unsigned int i; |
| 384 | unsigned int codecs_assigned; |
Philipp Maier | 9dd80ca | 2023-03-27 15:45:36 +0200 | [diff] [blame] | 385 | struct mgcp_rtp_codec *codec_convertible = NULL; |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 386 | |
Philipp Maier | 9dd80ca | 2023-03-27 15:45:36 +0200 | [diff] [blame] | 387 | rtp_end = &conn->end; |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 388 | |
Philipp Maier | 621e866 | 2023-03-22 16:53:30 +0100 | [diff] [blame] | 389 | /* Use the codec information from the source and try to find the equivalent of it on the destination side. In |
| 390 | * the first run we will look for an exact match. */ |
Philipp Maier | 4c4d227 | 2023-04-26 15:45:17 +0200 | [diff] [blame] | 391 | codec_convertible = mgcp_codec_find_same(conn, codec); |
| 392 | if (codec_convertible) |
| 393 | return codec_convertible; |
Philipp Maier | 621e866 | 2023-03-22 16:53:30 +0100 | [diff] [blame] | 394 | |
| 395 | /* In case we weren't able to find an exact match, we will try to find a match that is the same codec, but the |
| 396 | * payload format may be different. This alternative will require a frame format conversion (i.e. AMR bwe->oe) */ |
Philipp Maier | 4c4d227 | 2023-04-26 15:45:17 +0200 | [diff] [blame] | 397 | codecs_assigned = rtp_end->codecs_assigned; |
| 398 | OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS); |
| 399 | for (i = 0; i < codecs_assigned; i++) { |
| 400 | if (codecs_convertible(codec, &rtp_end->codecs[i])) { |
| 401 | codec_convertible = &rtp_end->codecs[i]; |
| 402 | break; |
Philipp Maier | 621e866 | 2023-03-22 16:53:30 +0100 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
Philipp Maier | 9dd80ca | 2023-03-27 15:45:36 +0200 | [diff] [blame] | 406 | return codec_convertible; |
Philipp Maier | 6931f9a | 2018-07-26 09:29:31 +0200 | [diff] [blame] | 407 | } |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 408 | |
Philipp Maier | 4c4d227 | 2023-04-26 15:45:17 +0200 | [diff] [blame] | 409 | /*! Decide for one suitable codec on both of the given connections. In case a destination connection is not available, |
| 410 | * a tentative decision is made. |
| 411 | * \param[inout] conn_src related rtp-connection. |
| 412 | * \param[inout] conn_dst related destination rtp-connection (NULL if not present). |
Philipp Maier | 0e8310f | 2023-04-26 15:23:05 +0200 | [diff] [blame] | 413 | * \returns 0 on success, -EINVAL on failure. */ |
Philipp Maier | 4c4d227 | 2023-04-26 15:45:17 +0200 | [diff] [blame] | 414 | int mgcp_codec_decide(struct mgcp_conn_rtp *conn_src, struct mgcp_conn_rtp *conn_dst) |
Philipp Maier | 0e8310f | 2023-04-26 15:23:05 +0200 | [diff] [blame] | 415 | { |
Philipp Maier | 0e8310f | 2023-04-26 15:23:05 +0200 | [diff] [blame] | 416 | unsigned int i; |
Philipp Maier | 0e8310f | 2023-04-26 15:23:05 +0200 | [diff] [blame] | 417 | |
Philipp Maier | 4c4d227 | 2023-04-26 15:45:17 +0200 | [diff] [blame] | 418 | /* In case no destination connection is available (yet), or in case the destination connection exists but has |
| 419 | * no codecs assigned, we are forced to make a simple tentative decision: |
| 420 | * We just use the first codec of the source connection (conn_src) */ |
| 421 | OSMO_ASSERT(conn_src->end.codecs_assigned <= MGCP_MAX_CODECS); |
| 422 | if (!conn_dst || conn_dst->end.codecs_assigned == 0) { |
| 423 | if (conn_src->end.codecs_assigned >= 1) { |
| 424 | conn_src->end.codec = &conn_src->end.codecs[0]; |
| 425 | return 0; |
| 426 | } else |
| 427 | return -EINVAL; |
Philipp Maier | 0e8310f | 2023-04-26 15:23:05 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Philipp Maier | 4c4d227 | 2023-04-26 15:45:17 +0200 | [diff] [blame] | 430 | /* Compare all codecs of the source connection (conn_src) to the codecs of the destination connection (conn_dst). In case |
| 431 | * of a match set this codec on both connections. This would be an ideal selection since no codec conversion would be |
| 432 | * required. */ |
| 433 | for (i = 0; i < conn_src->end.codecs_assigned; i++) { |
| 434 | struct mgcp_rtp_codec *codec_conn_dst = mgcp_codec_find_same(conn_dst, &conn_src->end.codecs[i]); |
| 435 | if (codec_conn_dst) { |
| 436 | /* We found the a codec that is exactly the same (same codec, same payload format etc.) on both |
| 437 | * sides. We now set this codec on both connections. */ |
| 438 | conn_dst->end.codec = codec_conn_dst; |
| 439 | conn_src->end.codec = mgcp_codec_find_same(conn_src, codec_conn_dst); |
| 440 | OSMO_ASSERT(conn_src->end.codec); |
| 441 | return 0; |
| 442 | } |
| 443 | } |
Philipp Maier | 0e8310f | 2023-04-26 15:23:05 +0200 | [diff] [blame] | 444 | |
Philipp Maier | 4c4d227 | 2023-04-26 15:45:17 +0200 | [diff] [blame] | 445 | /* In case we could not find a codec that is exactly the same, let's at least try to find a codec that we are able |
| 446 | * to convert. */ |
| 447 | for (i = 0; i < conn_src->end.codecs_assigned; i++) { |
| 448 | struct mgcp_rtp_codec *codec_conn_dst = codec_find_convertible(conn_dst, &conn_src->end.codecs[i]); |
| 449 | if (codec_conn_dst) { |
| 450 | /* We found the a codec that we are able to convert on both sides. We now set this codec on both |
| 451 | * connections. */ |
| 452 | conn_dst->end.codec = codec_conn_dst; |
| 453 | conn_src->end.codec = codec_find_convertible(conn_src, codec_conn_dst); |
| 454 | OSMO_ASSERT(conn_src->end.codec); |
| 455 | return 0; |
| 456 | } |
Philipp Maier | 0e8310f | 2023-04-26 15:23:05 +0200 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | return -EINVAL; |
| 460 | } |
| 461 | |
| 462 | /* Check if the codec has a specific AMR mode (octet-aligned or bandwith-efficient) set. */ |
| 463 | bool mgcp_codec_amr_align_mode_is_indicated(const struct mgcp_rtp_codec *codec) |
| 464 | { |
| 465 | if (codec->param_present == false) |
| 466 | return false; |
| 467 | if (!codec->param.amr_octet_aligned_present) |
| 468 | return false; |
| 469 | if (strcmp(codec->subtype_name, "AMR") != 0) |
| 470 | return false; |
| 471 | return true; |
| 472 | } |
| 473 | |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 474 | /* Find the payload type number configured for a specific codec by SDP. |
| 475 | * For example, IuUP gets assigned a payload type number, and the endpoint needs to translate that to the number |
| 476 | * assigned to "AMR" on the other conn (by a=rtpmap:N). |
| 477 | * \param conn The side of an endpoint to get the payload type number for (to translate the payload type number to). |
| 478 | * \param subtype_name SDP codec name without parameters (e.g. "AMR"). |
| 479 | * \param match_nr Index for the match found, first being match_nr == 0. Iterate all matches by calling multiple times |
| 480 | * with incrementing match_nr. |
Philipp Maier | 3699ef0 | 2023-03-27 15:54:22 +0200 | [diff] [blame] | 481 | * \return codec definition for that conn matching the subtype_name, or NULL if no such match_nr is found. */ |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 482 | const struct mgcp_rtp_codec *mgcp_codec_pt_find_by_subtype_name(struct mgcp_conn_rtp *conn, |
| 483 | const char *subtype_name, unsigned int match_nr) |
| 484 | { |
| 485 | int i; |
| 486 | for (i = 0; i < conn->end.codecs_assigned; i++) { |
| 487 | if (!strcmp(conn->end.codecs[i].subtype_name, subtype_name)) { |
| 488 | if (match_nr) { |
| 489 | match_nr--; |
| 490 | continue; |
| 491 | } |
| 492 | return &conn->end.codecs[i]; |
| 493 | } |
| 494 | } |
| 495 | return NULL; |
| 496 | } |
Philipp Maier | 9dd80ca | 2023-03-27 15:45:36 +0200 | [diff] [blame] | 497 | |
| 498 | /*! Lookup a codec that is assigned to a connection by its payload type number. |
| 499 | * \param[in] conn related rtp-connection. |
| 500 | * \param[in] payload_type number of the codec to look up. |
| 501 | * \returns pointer to codec struct on success, NULL on failure. */ |
| 502 | struct mgcp_rtp_codec *mgcp_codec_from_pt(struct mgcp_conn_rtp *conn, int payload_type) |
| 503 | { |
| 504 | struct mgcp_rtp_end *rtp_end = &conn->end; |
| 505 | unsigned int codecs_assigned = rtp_end->codecs_assigned; |
| 506 | struct mgcp_rtp_codec *codec = NULL; |
| 507 | size_t i; |
| 508 | |
| 509 | OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS); |
| 510 | |
| 511 | for (i = 0; i < codecs_assigned; i++) { |
| 512 | if (payload_type == rtp_end->codecs[i].payload_type) { |
| 513 | codec = &rtp_end->codecs[i]; |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | return codec; |
| 519 | } |