Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 1 | /* |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 2 | * (C) 2014 by On-Waves |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU Affero General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU Affero General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Affero General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <errno.h> |
| 23 | |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 24 | |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 25 | #include "g711common.h" |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 26 | |
| 27 | #include <openbsc/debug.h> |
| 28 | #include <openbsc/mgcp.h> |
| 29 | #include <openbsc/mgcp_internal.h> |
Holger Hans Peter Freyther | dd1f815 | 2014-07-22 13:05:31 +0200 | [diff] [blame] | 30 | #include <openbsc/mgcp_transcode.h> |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 31 | |
| 32 | #include <osmocom/core/talloc.h> |
Holger Hans Peter Freyther | 7c7358e | 2015-03-22 13:56:30 +0100 | [diff] [blame] | 33 | #include <osmocom/netif/rtp.h> |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 34 | |
Jacob Erlbeck | 136a319 | 2014-03-13 14:33:37 +0100 | [diff] [blame] | 35 | int mgcp_transcoding_get_frame_size(void *state_, int nsamples, int dst) |
| 36 | { |
| 37 | struct mgcp_process_rtp_state *state = state_; |
| 38 | if (dst) |
| 39 | return (nsamples >= 0 ? |
| 40 | nsamples / state->dst_samples_per_frame : |
| 41 | 1) * state->dst_frame_size; |
| 42 | else |
| 43 | return (nsamples >= 0 ? |
| 44 | nsamples / state->src_samples_per_frame : |
| 45 | 1) * state->src_frame_size; |
| 46 | } |
| 47 | |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 48 | static enum audio_format get_audio_format(const struct mgcp_rtp_codec *codec) |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 49 | { |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 50 | if (codec->subtype_name) { |
Holger Hans Peter Freyther | c57b550 | 2015-04-24 15:07:20 -0400 | [diff] [blame] | 51 | if (!strcasecmp("GSM", codec->subtype_name)) |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 52 | return AF_GSM; |
Holger Hans Peter Freyther | c57b550 | 2015-04-24 15:07:20 -0400 | [diff] [blame] | 53 | if (!strcasecmp("PCMA", codec->subtype_name)) |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 54 | return AF_PCMA; |
Holger Hans Peter Freyther | ceef936 | 2015-08-19 14:43:35 +0200 | [diff] [blame] | 55 | if (!strcasecmp("PCMU", codec->subtype_name)) |
| 56 | return AF_PCMU; |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 57 | #ifdef HAVE_BCG729 |
Holger Hans Peter Freyther | c57b550 | 2015-04-24 15:07:20 -0400 | [diff] [blame] | 58 | if (!strcasecmp("G729", codec->subtype_name)) |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 59 | return AF_G729; |
| 60 | #endif |
Holger Hans Peter Freyther | c57b550 | 2015-04-24 15:07:20 -0400 | [diff] [blame] | 61 | if (!strcasecmp("L16", codec->subtype_name)) |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 62 | return AF_L16; |
| 63 | } |
| 64 | |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 65 | switch (codec->payload_type) { |
Holger Hans Peter Freyther | ceef936 | 2015-08-19 14:43:35 +0200 | [diff] [blame] | 66 | case 0 /* PCMU */: |
| 67 | return AF_PCMU; |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 68 | case 3 /* GSM */: |
| 69 | return AF_GSM; |
| 70 | case 8 /* PCMA */: |
| 71 | return AF_PCMA; |
| 72 | #ifdef HAVE_BCG729 |
| 73 | case 18 /* G.729 */: |
| 74 | return AF_G729; |
| 75 | #endif |
| 76 | case 11 /* L16 */: |
| 77 | return AF_L16; |
| 78 | default: |
| 79 | return AF_INVALID; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static void l16_encode(short *sample, unsigned char *buf, size_t n) |
| 84 | { |
| 85 | for (; n > 0; --n, ++sample, buf += 2) { |
| 86 | buf[0] = sample[0] >> 8; |
| 87 | buf[1] = sample[0] & 0xff; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | static void l16_decode(unsigned char *buf, short *sample, size_t n) |
| 92 | { |
| 93 | for (; n > 0; --n, ++sample, buf += 2) |
| 94 | sample[0] = ((short)buf[0] << 8) | buf[1]; |
| 95 | } |
| 96 | |
| 97 | static void alaw_encode(short *sample, unsigned char *buf, size_t n) |
| 98 | { |
| 99 | for (; n > 0; --n) |
| 100 | *(buf++) = s16_to_alaw(*(sample++)); |
| 101 | } |
| 102 | |
| 103 | static void alaw_decode(unsigned char *buf, short *sample, size_t n) |
| 104 | { |
| 105 | for (; n > 0; --n) |
| 106 | *(sample++) = alaw_to_s16(*(buf++)); |
| 107 | } |
| 108 | |
Holger Hans Peter Freyther | ceef936 | 2015-08-19 14:43:35 +0200 | [diff] [blame] | 109 | static void ulaw_encode(short *sample, unsigned char *buf, size_t n) |
| 110 | { |
| 111 | for (; n > 0; --n) |
| 112 | *(buf++) = s16_to_ulaw(*(sample++)); |
| 113 | } |
| 114 | |
| 115 | static void ulaw_decode(unsigned char *buf, short *sample, size_t n) |
| 116 | { |
| 117 | for (; n > 0; --n) |
| 118 | *(sample++) = ulaw_to_s16(*(buf++)); |
| 119 | } |
| 120 | |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 121 | static int processing_state_destructor(struct mgcp_process_rtp_state *state) |
| 122 | { |
| 123 | switch (state->src_fmt) { |
| 124 | case AF_GSM: |
Holger Hans Peter Freyther | c5c239f | 2014-07-07 20:38:27 +0200 | [diff] [blame] | 125 | if (state->src.gsm_handle) |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 126 | gsm_destroy(state->src.gsm_handle); |
| 127 | break; |
| 128 | #ifdef HAVE_BCG729 |
| 129 | case AF_G729: |
| 130 | if (state->src.g729_dec) |
| 131 | closeBcg729DecoderChannel(state->src.g729_dec); |
| 132 | break; |
| 133 | #endif |
| 134 | default: |
| 135 | break; |
| 136 | } |
| 137 | switch (state->dst_fmt) { |
| 138 | case AF_GSM: |
| 139 | if (state->dst.gsm_handle) |
| 140 | gsm_destroy(state->dst.gsm_handle); |
| 141 | break; |
| 142 | #ifdef HAVE_BCG729 |
| 143 | case AF_G729: |
| 144 | if (state->dst.g729_enc) |
| 145 | closeBcg729EncoderChannel(state->dst.g729_enc); |
| 146 | break; |
| 147 | #endif |
| 148 | default: |
| 149 | break; |
| 150 | } |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | int mgcp_transcoding_setup(struct mgcp_endpoint *endp, |
| 155 | struct mgcp_rtp_end *dst_end, |
| 156 | struct mgcp_rtp_end *src_end) |
| 157 | { |
| 158 | struct mgcp_process_rtp_state *state; |
| 159 | enum audio_format src_fmt, dst_fmt; |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 160 | const struct mgcp_rtp_codec *dst_codec = &dst_end->codec; |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 161 | |
| 162 | /* cleanup first */ |
| 163 | if (dst_end->rtp_process_data) { |
| 164 | talloc_free(dst_end->rtp_process_data); |
| 165 | dst_end->rtp_process_data = NULL; |
| 166 | } |
| 167 | |
| 168 | if (!src_end) |
| 169 | return 0; |
| 170 | |
Jacob Erlbeck | 5a2484b | 2015-04-28 09:28:18 +0200 | [diff] [blame] | 171 | const struct mgcp_rtp_codec *src_codec = &src_end->codec; |
| 172 | |
Holger Hans Peter Freyther | cb43a9a | 2015-04-24 16:03:55 -0400 | [diff] [blame] | 173 | if (endp->tcfg->no_audio_transcoding) { |
| 174 | LOGP(DMGCP, LOGL_NOTICE, |
| 175 | "Transcoding disabled on endpoint 0x%x\n", |
| 176 | ENDPOINT_NUMBER(endp)); |
| 177 | return 0; |
| 178 | } |
| 179 | |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 180 | src_fmt = get_audio_format(src_codec); |
| 181 | dst_fmt = get_audio_format(dst_codec); |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 182 | |
| 183 | LOGP(DMGCP, LOGL_ERROR, |
| 184 | "Checking transcoding: %s (%d) -> %s (%d)\n", |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 185 | src_codec->subtype_name, src_codec->payload_type, |
| 186 | dst_codec->subtype_name, dst_codec->payload_type); |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 187 | |
| 188 | if (src_fmt == AF_INVALID || dst_fmt == AF_INVALID) { |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 189 | if (!src_codec->subtype_name || !dst_codec->subtype_name) |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 190 | /* Not enough info, do nothing */ |
| 191 | return 0; |
| 192 | |
Holger Hans Peter Freyther | c57b550 | 2015-04-24 15:07:20 -0400 | [diff] [blame] | 193 | if (strcasecmp(src_codec->subtype_name, dst_codec->subtype_name) == 0) |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 194 | /* Nothing to do */ |
| 195 | return 0; |
| 196 | |
| 197 | LOGP(DMGCP, LOGL_ERROR, |
| 198 | "Cannot transcode: %s codec not supported (%s -> %s).\n", |
| 199 | src_fmt != AF_INVALID ? "destination" : "source", |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 200 | src_codec->audio_name, dst_codec->audio_name); |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 201 | return -EINVAL; |
| 202 | } |
| 203 | |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 204 | if (src_codec->rate && dst_codec->rate && src_codec->rate != dst_codec->rate) { |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 205 | LOGP(DMGCP, LOGL_ERROR, |
| 206 | "Cannot transcode: rate conversion (%d -> %d) not supported.\n", |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 207 | src_codec->rate, dst_codec->rate); |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 208 | return -EINVAL; |
| 209 | } |
| 210 | |
| 211 | state = talloc_zero(endp->tcfg->cfg, struct mgcp_process_rtp_state); |
| 212 | talloc_set_destructor(state, processing_state_destructor); |
| 213 | dst_end->rtp_process_data = state; |
| 214 | |
| 215 | state->src_fmt = src_fmt; |
| 216 | |
| 217 | switch (state->src_fmt) { |
| 218 | case AF_L16: |
| 219 | case AF_S16: |
| 220 | state->src_frame_size = 80 * sizeof(short); |
| 221 | state->src_samples_per_frame = 80; |
| 222 | break; |
| 223 | case AF_GSM: |
| 224 | state->src_frame_size = sizeof(gsm_frame); |
| 225 | state->src_samples_per_frame = 160; |
| 226 | state->src.gsm_handle = gsm_create(); |
| 227 | if (!state->src.gsm_handle) { |
| 228 | LOGP(DMGCP, LOGL_ERROR, |
| 229 | "Failed to initialize GSM decoder.\n"); |
| 230 | return -EINVAL; |
| 231 | } |
| 232 | break; |
| 233 | #ifdef HAVE_BCG729 |
| 234 | case AF_G729: |
| 235 | state->src_frame_size = 10; |
| 236 | state->src_samples_per_frame = 80; |
| 237 | state->src.g729_dec = initBcg729DecoderChannel(); |
| 238 | if (!state->src.g729_dec) { |
| 239 | LOGP(DMGCP, LOGL_ERROR, |
| 240 | "Failed to initialize G.729 decoder.\n"); |
| 241 | return -EINVAL; |
| 242 | } |
| 243 | break; |
| 244 | #endif |
Holger Hans Peter Freyther | ceef936 | 2015-08-19 14:43:35 +0200 | [diff] [blame] | 245 | case AF_PCMU: |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 246 | case AF_PCMA: |
| 247 | state->src_frame_size = 80; |
| 248 | state->src_samples_per_frame = 80; |
| 249 | break; |
| 250 | default: |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | state->dst_fmt = dst_fmt; |
| 255 | |
| 256 | switch (state->dst_fmt) { |
| 257 | case AF_L16: |
| 258 | case AF_S16: |
| 259 | state->dst_frame_size = 80*sizeof(short); |
| 260 | state->dst_samples_per_frame = 80; |
| 261 | break; |
| 262 | case AF_GSM: |
| 263 | state->dst_frame_size = sizeof(gsm_frame); |
| 264 | state->dst_samples_per_frame = 160; |
| 265 | state->dst.gsm_handle = gsm_create(); |
| 266 | if (!state->dst.gsm_handle) { |
| 267 | LOGP(DMGCP, LOGL_ERROR, |
| 268 | "Failed to initialize GSM encoder.\n"); |
| 269 | return -EINVAL; |
| 270 | } |
| 271 | break; |
| 272 | #ifdef HAVE_BCG729 |
| 273 | case AF_G729: |
| 274 | state->dst_frame_size = 10; |
| 275 | state->dst_samples_per_frame = 80; |
| 276 | state->dst.g729_enc = initBcg729EncoderChannel(); |
| 277 | if (!state->dst.g729_enc) { |
| 278 | LOGP(DMGCP, LOGL_ERROR, |
| 279 | "Failed to initialize G.729 decoder.\n"); |
| 280 | return -EINVAL; |
| 281 | } |
| 282 | break; |
| 283 | #endif |
Holger Hans Peter Freyther | ceef936 | 2015-08-19 14:43:35 +0200 | [diff] [blame] | 284 | case AF_PCMU: |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 285 | case AF_PCMA: |
| 286 | state->dst_frame_size = 80; |
| 287 | state->dst_samples_per_frame = 80; |
| 288 | break; |
| 289 | default: |
| 290 | break; |
| 291 | } |
| 292 | |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 293 | if (dst_end->force_output_ptime) |
| 294 | state->dst_packet_duration = mgcp_rtp_packet_duration(endp, dst_end); |
| 295 | |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 296 | LOGP(DMGCP, LOGL_INFO, |
| 297 | "Initialized RTP processing on: 0x%x " |
| 298 | "conv: %d (%d, %d, %s) -> %d (%d, %d, %s)\n", |
| 299 | ENDPOINT_NUMBER(endp), |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 300 | src_fmt, src_codec->payload_type, src_codec->rate, src_end->fmtp_extra, |
| 301 | dst_fmt, dst_codec->payload_type, dst_codec->rate, dst_end->fmtp_extra); |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | void mgcp_transcoding_net_downlink_format(struct mgcp_endpoint *endp, |
| 307 | int *payload_type, |
| 308 | const char**audio_name, |
| 309 | const char**fmtp_extra) |
| 310 | { |
| 311 | struct mgcp_process_rtp_state *state = endp->net_end.rtp_process_data; |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 312 | struct mgcp_rtp_codec *net_codec = &endp->net_end.codec; |
| 313 | struct mgcp_rtp_codec *bts_codec = &endp->bts_end.codec; |
| 314 | |
| 315 | if (!state || net_codec->payload_type < 0) { |
| 316 | *payload_type = bts_codec->payload_type; |
| 317 | *audio_name = bts_codec->audio_name; |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 318 | *fmtp_extra = endp->bts_end.fmtp_extra; |
| 319 | return; |
| 320 | } |
| 321 | |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 322 | *payload_type = net_codec->payload_type; |
| 323 | *audio_name = net_codec->audio_name; |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 324 | *fmtp_extra = endp->net_end.fmtp_extra; |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 325 | } |
| 326 | |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 327 | static int decode_audio(struct mgcp_process_rtp_state *state, |
| 328 | uint8_t **src, size_t *nbytes) |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 329 | { |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 330 | while (*nbytes >= state->src_frame_size) { |
| 331 | if (state->sample_cnt + state->src_samples_per_frame > ARRAY_SIZE(state->samples)) { |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 332 | LOGP(DMGCP, LOGL_ERROR, |
| 333 | "Sample buffer too small: %d > %d.\n", |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 334 | state->sample_cnt + state->src_samples_per_frame, |
| 335 | ARRAY_SIZE(state->samples)); |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 336 | return -ENOSPC; |
| 337 | } |
| 338 | switch (state->src_fmt) { |
| 339 | case AF_GSM: |
| 340 | if (gsm_decode(state->src.gsm_handle, |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 341 | (gsm_byte *)*src, state->samples + state->sample_cnt) < 0) { |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 342 | LOGP(DMGCP, LOGL_ERROR, |
| 343 | "Failed to decode GSM.\n"); |
| 344 | return -EINVAL; |
| 345 | } |
| 346 | break; |
| 347 | #ifdef HAVE_BCG729 |
| 348 | case AF_G729: |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 349 | bcg729Decoder(state->src.g729_dec, *src, 0, state->samples + state->sample_cnt); |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 350 | break; |
| 351 | #endif |
Holger Hans Peter Freyther | ceef936 | 2015-08-19 14:43:35 +0200 | [diff] [blame] | 352 | case AF_PCMU: |
| 353 | ulaw_decode(*src, state->samples + state->sample_cnt, |
| 354 | state->src_samples_per_frame); |
| 355 | break; |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 356 | case AF_PCMA: |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 357 | alaw_decode(*src, state->samples + state->sample_cnt, |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 358 | state->src_samples_per_frame); |
| 359 | break; |
| 360 | case AF_S16: |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 361 | memmove(state->samples + state->sample_cnt, *src, |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 362 | state->src_frame_size); |
| 363 | break; |
| 364 | case AF_L16: |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 365 | l16_decode(*src, state->samples + state->sample_cnt, |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 366 | state->src_samples_per_frame); |
| 367 | break; |
| 368 | default: |
| 369 | break; |
| 370 | } |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 371 | *src += state->src_frame_size; |
| 372 | *nbytes -= state->src_frame_size; |
| 373 | state->sample_cnt += state->src_samples_per_frame; |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 374 | } |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 375 | return 0; |
| 376 | } |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 377 | |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 378 | static int encode_audio(struct mgcp_process_rtp_state *state, |
| 379 | uint8_t *dst, size_t buf_size, size_t max_samples) |
| 380 | { |
| 381 | int nbytes = 0; |
| 382 | size_t nsamples = 0; |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 383 | /* Encode samples into dst */ |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 384 | while (nsamples + state->dst_samples_per_frame <= max_samples) { |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 385 | if (nbytes + state->dst_frame_size > buf_size) { |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 386 | if (nbytes > 0) |
| 387 | break; |
| 388 | |
| 389 | /* Not even one frame fits into the buffer */ |
| 390 | LOGP(DMGCP, LOGL_INFO, |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 391 | "Encoding (RTP) buffer too small: %d > %d.\n", |
| 392 | nbytes + state->dst_frame_size, buf_size); |
| 393 | return -ENOSPC; |
| 394 | } |
| 395 | switch (state->dst_fmt) { |
| 396 | case AF_GSM: |
| 397 | gsm_encode(state->dst.gsm_handle, |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 398 | state->samples + state->sample_offs, dst); |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 399 | break; |
| 400 | #ifdef HAVE_BCG729 |
| 401 | case AF_G729: |
| 402 | bcg729Encoder(state->dst.g729_enc, |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 403 | state->samples + state->sample_offs, dst); |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 404 | break; |
| 405 | #endif |
Holger Hans Peter Freyther | ceef936 | 2015-08-19 14:43:35 +0200 | [diff] [blame] | 406 | case AF_PCMU: |
| 407 | ulaw_encode(state->samples + state->sample_offs, dst, |
| 408 | state->src_samples_per_frame); |
| 409 | break; |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 410 | case AF_PCMA: |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 411 | alaw_encode(state->samples + state->sample_offs, dst, |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 412 | state->src_samples_per_frame); |
| 413 | break; |
| 414 | case AF_S16: |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 415 | memmove(dst, state->samples + state->sample_offs, |
| 416 | state->dst_frame_size); |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 417 | break; |
| 418 | case AF_L16: |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 419 | l16_encode(state->samples + state->sample_offs, dst, |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 420 | state->src_samples_per_frame); |
| 421 | break; |
| 422 | default: |
| 423 | break; |
| 424 | } |
| 425 | dst += state->dst_frame_size; |
| 426 | nbytes += state->dst_frame_size; |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 427 | state->sample_offs += state->dst_samples_per_frame; |
| 428 | nsamples += state->dst_samples_per_frame; |
| 429 | } |
| 430 | state->sample_cnt -= nsamples; |
| 431 | return nbytes; |
| 432 | } |
| 433 | |
Holger Hans Peter Freyther | 4680121 | 2014-09-01 22:20:57 +0200 | [diff] [blame] | 434 | static struct mgcp_rtp_end *source_for_dest(struct mgcp_endpoint *endp, |
| 435 | struct mgcp_rtp_end *dst_end) |
| 436 | { |
| 437 | if (&endp->bts_end == dst_end) |
| 438 | return &endp->net_end; |
| 439 | else if (&endp->net_end == dst_end) |
| 440 | return &endp->bts_end; |
| 441 | OSMO_ASSERT(0); |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * With some modems we get offered multiple codecs |
| 446 | * and we have selected one of them. It might not |
| 447 | * be the right one and we need to detect this with |
| 448 | * the first audio packets. One difficulty is that |
| 449 | * we patch the rtp payload type in place, so we |
| 450 | * need to discuss this. |
| 451 | */ |
| 452 | struct mgcp_process_rtp_state *check_transcode_state( |
| 453 | struct mgcp_endpoint *endp, |
| 454 | struct mgcp_rtp_end *dst_end, |
| 455 | struct rtp_hdr *rtp_hdr) |
| 456 | { |
| 457 | struct mgcp_rtp_end *src_end; |
| 458 | |
| 459 | /* Only deal with messages from net to bts */ |
| 460 | if (&endp->bts_end != dst_end) |
| 461 | goto done; |
| 462 | |
| 463 | src_end = source_for_dest(endp, dst_end); |
| 464 | |
| 465 | /* Already patched */ |
| 466 | if (rtp_hdr->payload_type == dst_end->codec.payload_type) |
| 467 | goto done; |
| 468 | /* The payload we expect */ |
| 469 | if (rtp_hdr->payload_type == src_end->codec.payload_type) |
| 470 | goto done; |
| 471 | /* The matching alternate payload type? Then switch */ |
| 472 | if (rtp_hdr->payload_type == src_end->alt_codec.payload_type) { |
| 473 | struct mgcp_config *cfg = endp->cfg; |
| 474 | struct mgcp_rtp_codec tmp_codec = src_end->alt_codec; |
| 475 | src_end->alt_codec = src_end->codec; |
| 476 | src_end->codec = tmp_codec; |
| 477 | cfg->setup_rtp_processing_cb(endp, &endp->net_end, &endp->bts_end); |
| 478 | cfg->setup_rtp_processing_cb(endp, &endp->bts_end, &endp->net_end); |
| 479 | } |
| 480 | |
| 481 | done: |
| 482 | return dst_end->rtp_process_data; |
| 483 | } |
| 484 | |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 485 | int mgcp_transcoding_process_rtp(struct mgcp_endpoint *endp, |
| 486 | struct mgcp_rtp_end *dst_end, |
| 487 | char *data, int *len, int buf_size) |
| 488 | { |
Holger Hans Peter Freyther | 4680121 | 2014-09-01 22:20:57 +0200 | [diff] [blame] | 489 | struct mgcp_process_rtp_state *state; |
Holger Hans Peter Freyther | 3713f78 | 2014-09-01 11:02:05 +0200 | [diff] [blame] | 490 | const size_t rtp_hdr_size = sizeof(struct rtp_hdr); |
| 491 | struct rtp_hdr *rtp_hdr = (struct rtp_hdr *) data; |
| 492 | char *payload_data = (char *) &rtp_hdr->data[0]; |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 493 | int payload_len = *len - rtp_hdr_size; |
| 494 | uint8_t *src = (uint8_t *)payload_data; |
| 495 | uint8_t *dst = (uint8_t *)payload_data; |
| 496 | size_t nbytes = payload_len; |
| 497 | size_t nsamples; |
| 498 | size_t max_samples; |
| 499 | uint32_t ts_no; |
| 500 | int rc; |
| 501 | |
Holger Hans Peter Freyther | 4680121 | 2014-09-01 22:20:57 +0200 | [diff] [blame] | 502 | state = check_transcode_state(endp, dst_end, rtp_hdr); |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 503 | if (!state) |
| 504 | return 0; |
| 505 | |
| 506 | if (state->src_fmt == state->dst_fmt) { |
| 507 | if (!state->dst_packet_duration) |
| 508 | return 0; |
| 509 | |
| 510 | /* TODO: repackage without transcoding */ |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 511 | } |
| 512 | |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 513 | /* If the remaining samples do not fit into a fixed ptime, |
| 514 | * a) discard them, if the next packet is much later |
| 515 | * b) add silence and * send it, if the current packet is not |
| 516 | * yet too late |
| 517 | * c) append the sample data, if the timestamp matches exactly |
| 518 | */ |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 519 | |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 520 | /* TODO: check payload type (-> G.711 comfort noise) */ |
| 521 | |
| 522 | if (payload_len > 0) { |
Holger Hans Peter Freyther | 3713f78 | 2014-09-01 11:02:05 +0200 | [diff] [blame] | 523 | ts_no = ntohl(rtp_hdr->timestamp); |
Holger Hans Peter Freyther | c8b2908 | 2014-07-02 22:02:15 +0200 | [diff] [blame] | 524 | if (!state->is_running) { |
Holger Hans Peter Freyther | 3713f78 | 2014-09-01 11:02:05 +0200 | [diff] [blame] | 525 | state->next_seq = ntohs(rtp_hdr->sequence); |
Holger Hans Peter Freyther | c8b2908 | 2014-07-02 22:02:15 +0200 | [diff] [blame] | 526 | state->next_time = ts_no; |
| 527 | state->is_running = 1; |
| 528 | } |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 529 | |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 530 | |
| 531 | if (state->sample_cnt > 0) { |
| 532 | int32_t delta = ts_no - state->next_time; |
| 533 | /* TODO: check sequence? reordering? packet loss? */ |
| 534 | |
Holger Hans Peter Freyther | e52ca9a | 2014-07-02 21:56:26 +0200 | [diff] [blame] | 535 | if (delta > state->sample_cnt) { |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 536 | /* There is a time gap between the last packet |
| 537 | * and the current one. Just discard the |
| 538 | * partial data that is left in the buffer. |
| 539 | * TODO: This can be improved by adding silence |
| 540 | * instead if the delta is small enough. |
| 541 | */ |
Holger Hans Peter Freyther | e52ca9a | 2014-07-02 21:56:26 +0200 | [diff] [blame] | 542 | LOGP(DMGCP, LOGL_NOTICE, |
| 543 | "0x%x dropping sample buffer due delta=%d sample_cnt=%d\n", |
| 544 | ENDPOINT_NUMBER(endp), delta, state->sample_cnt); |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 545 | state->sample_cnt = 0; |
Holger Hans Peter Freyther | b936278 | 2014-07-04 20:55:20 +0200 | [diff] [blame] | 546 | state->next_time = ts_no; |
Holger Hans Peter Freyther | e52ca9a | 2014-07-02 21:56:26 +0200 | [diff] [blame] | 547 | } else if (delta < 0) { |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 548 | LOGP(DMGCP, LOGL_NOTICE, |
| 549 | "RTP time jumps backwards, delta = %d, " |
| 550 | "discarding buffered samples\n", |
| 551 | delta); |
| 552 | state->sample_cnt = 0; |
| 553 | state->sample_offs = 0; |
| 554 | return -EAGAIN; |
| 555 | } |
| 556 | |
| 557 | /* Make sure the samples start without offset */ |
| 558 | if (state->sample_offs && state->sample_cnt) |
| 559 | memmove(&state->samples[0], |
| 560 | &state->samples[state->sample_offs], |
| 561 | state->sample_cnt * |
| 562 | sizeof(state->samples[0])); |
| 563 | } |
| 564 | |
| 565 | state->sample_offs = 0; |
| 566 | |
| 567 | /* Append decoded audio to samples */ |
| 568 | decode_audio(state, &src, &nbytes); |
| 569 | |
| 570 | if (nbytes > 0) |
| 571 | LOGP(DMGCP, LOGL_NOTICE, |
| 572 | "Skipped audio frame in RTP packet: %d octets\n", |
| 573 | nbytes); |
| 574 | } else |
| 575 | ts_no = state->next_time; |
| 576 | |
| 577 | if (state->sample_cnt < state->dst_packet_duration) |
| 578 | return -EAGAIN; |
| 579 | |
| 580 | max_samples = |
| 581 | state->dst_packet_duration ? |
| 582 | state->dst_packet_duration : state->sample_cnt; |
| 583 | |
| 584 | nsamples = state->sample_cnt; |
| 585 | |
| 586 | rc = encode_audio(state, dst, buf_size, max_samples); |
Holger Hans Peter Freyther | bd4109b | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 587 | /* |
| 588 | * There were no samples to encode? |
| 589 | * TODO: how does this work for comfort noise? |
| 590 | */ |
| 591 | if (rc == 0) |
| 592 | return -ENOMSG; |
| 593 | /* Any other error during the encoding */ |
| 594 | if (rc < 0) |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 595 | return rc; |
| 596 | |
| 597 | nsamples -= state->sample_cnt; |
| 598 | |
| 599 | *len = rtp_hdr_size + rc; |
Holger Hans Peter Freyther | 3713f78 | 2014-09-01 11:02:05 +0200 | [diff] [blame] | 600 | rtp_hdr->sequence = htons(state->next_seq); |
| 601 | rtp_hdr->timestamp = htonl(ts_no); |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 602 | |
| 603 | state->next_seq += 1; |
| 604 | state->next_time = ts_no + nsamples; |
| 605 | |
Holger Hans Peter Freyther | 77ceaaf | 2014-06-28 15:05:42 +0200 | [diff] [blame] | 606 | /* |
| 607 | * XXX: At this point we should always have consumed |
| 608 | * samples. So doing OSMO_ASSERT(nsamples > 0) and returning |
| 609 | * rtp_hdr_size should be fine. |
| 610 | */ |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 611 | return nsamples ? rtp_hdr_size : 0; |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 612 | } |