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> |
Holger Hans Peter Freyther | 3713f78 | 2014-09-01 11:02:05 +0200 | [diff] [blame] | 31 | #include <openbsc/rtp.h> |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 32 | |
| 33 | #include <osmocom/core/talloc.h> |
| 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) { |
| 51 | if (!strcmp("GSM", codec->subtype_name)) |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 52 | return AF_GSM; |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame^] | 53 | if (!strcmp("PCMA", codec->subtype_name)) |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 54 | return AF_PCMA; |
| 55 | #ifdef HAVE_BCG729 |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame^] | 56 | if (!strcmp("G729", codec->subtype_name)) |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 57 | return AF_G729; |
| 58 | #endif |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame^] | 59 | if (!strcmp("L16", codec->subtype_name)) |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 60 | return AF_L16; |
| 61 | } |
| 62 | |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame^] | 63 | switch (codec->payload_type) { |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 64 | case 3 /* GSM */: |
| 65 | return AF_GSM; |
| 66 | case 8 /* PCMA */: |
| 67 | return AF_PCMA; |
| 68 | #ifdef HAVE_BCG729 |
| 69 | case 18 /* G.729 */: |
| 70 | return AF_G729; |
| 71 | #endif |
| 72 | case 11 /* L16 */: |
| 73 | return AF_L16; |
| 74 | default: |
| 75 | return AF_INVALID; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static void l16_encode(short *sample, unsigned char *buf, size_t n) |
| 80 | { |
| 81 | for (; n > 0; --n, ++sample, buf += 2) { |
| 82 | buf[0] = sample[0] >> 8; |
| 83 | buf[1] = sample[0] & 0xff; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static void l16_decode(unsigned char *buf, short *sample, size_t n) |
| 88 | { |
| 89 | for (; n > 0; --n, ++sample, buf += 2) |
| 90 | sample[0] = ((short)buf[0] << 8) | buf[1]; |
| 91 | } |
| 92 | |
| 93 | static void alaw_encode(short *sample, unsigned char *buf, size_t n) |
| 94 | { |
| 95 | for (; n > 0; --n) |
| 96 | *(buf++) = s16_to_alaw(*(sample++)); |
| 97 | } |
| 98 | |
| 99 | static void alaw_decode(unsigned char *buf, short *sample, size_t n) |
| 100 | { |
| 101 | for (; n > 0; --n) |
| 102 | *(sample++) = alaw_to_s16(*(buf++)); |
| 103 | } |
| 104 | |
| 105 | static int processing_state_destructor(struct mgcp_process_rtp_state *state) |
| 106 | { |
| 107 | switch (state->src_fmt) { |
| 108 | case AF_GSM: |
Holger Hans Peter Freyther | c5c239f | 2014-07-07 20:38:27 +0200 | [diff] [blame] | 109 | if (state->src.gsm_handle) |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 110 | gsm_destroy(state->src.gsm_handle); |
| 111 | break; |
| 112 | #ifdef HAVE_BCG729 |
| 113 | case AF_G729: |
| 114 | if (state->src.g729_dec) |
| 115 | closeBcg729DecoderChannel(state->src.g729_dec); |
| 116 | break; |
| 117 | #endif |
| 118 | default: |
| 119 | break; |
| 120 | } |
| 121 | switch (state->dst_fmt) { |
| 122 | case AF_GSM: |
| 123 | if (state->dst.gsm_handle) |
| 124 | gsm_destroy(state->dst.gsm_handle); |
| 125 | break; |
| 126 | #ifdef HAVE_BCG729 |
| 127 | case AF_G729: |
| 128 | if (state->dst.g729_enc) |
| 129 | closeBcg729EncoderChannel(state->dst.g729_enc); |
| 130 | break; |
| 131 | #endif |
| 132 | default: |
| 133 | break; |
| 134 | } |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | int mgcp_transcoding_setup(struct mgcp_endpoint *endp, |
| 139 | struct mgcp_rtp_end *dst_end, |
| 140 | struct mgcp_rtp_end *src_end) |
| 141 | { |
| 142 | struct mgcp_process_rtp_state *state; |
| 143 | enum audio_format src_fmt, dst_fmt; |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame^] | 144 | const struct mgcp_rtp_codec *src_codec = &src_end->codec; |
| 145 | const struct mgcp_rtp_codec *dst_codec = &dst_end->codec; |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 146 | |
| 147 | /* cleanup first */ |
| 148 | if (dst_end->rtp_process_data) { |
| 149 | talloc_free(dst_end->rtp_process_data); |
| 150 | dst_end->rtp_process_data = NULL; |
| 151 | } |
| 152 | |
| 153 | if (!src_end) |
| 154 | return 0; |
| 155 | |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame^] | 156 | src_fmt = get_audio_format(src_codec); |
| 157 | dst_fmt = get_audio_format(dst_codec); |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 158 | |
| 159 | LOGP(DMGCP, LOGL_ERROR, |
| 160 | "Checking transcoding: %s (%d) -> %s (%d)\n", |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame^] | 161 | src_codec->subtype_name, src_codec->payload_type, |
| 162 | dst_codec->subtype_name, dst_codec->payload_type); |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 163 | |
| 164 | if (src_fmt == AF_INVALID || dst_fmt == AF_INVALID) { |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame^] | 165 | if (!src_codec->subtype_name || !dst_codec->subtype_name) |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 166 | /* Not enough info, do nothing */ |
| 167 | return 0; |
| 168 | |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame^] | 169 | if (strcmp(src_codec->subtype_name, dst_codec->subtype_name) == 0) |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 170 | /* Nothing to do */ |
| 171 | return 0; |
| 172 | |
| 173 | LOGP(DMGCP, LOGL_ERROR, |
| 174 | "Cannot transcode: %s codec not supported (%s -> %s).\n", |
| 175 | src_fmt != AF_INVALID ? "destination" : "source", |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame^] | 176 | src_codec->audio_name, dst_codec->audio_name); |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 177 | return -EINVAL; |
| 178 | } |
| 179 | |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame^] | 180 | 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] | 181 | LOGP(DMGCP, LOGL_ERROR, |
| 182 | "Cannot transcode: rate conversion (%d -> %d) not supported.\n", |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame^] | 183 | src_codec->rate, dst_codec->rate); |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 184 | return -EINVAL; |
| 185 | } |
| 186 | |
| 187 | state = talloc_zero(endp->tcfg->cfg, struct mgcp_process_rtp_state); |
| 188 | talloc_set_destructor(state, processing_state_destructor); |
| 189 | dst_end->rtp_process_data = state; |
| 190 | |
| 191 | state->src_fmt = src_fmt; |
| 192 | |
| 193 | switch (state->src_fmt) { |
| 194 | case AF_L16: |
| 195 | case AF_S16: |
| 196 | state->src_frame_size = 80 * sizeof(short); |
| 197 | state->src_samples_per_frame = 80; |
| 198 | break; |
| 199 | case AF_GSM: |
| 200 | state->src_frame_size = sizeof(gsm_frame); |
| 201 | state->src_samples_per_frame = 160; |
| 202 | state->src.gsm_handle = gsm_create(); |
| 203 | if (!state->src.gsm_handle) { |
| 204 | LOGP(DMGCP, LOGL_ERROR, |
| 205 | "Failed to initialize GSM decoder.\n"); |
| 206 | return -EINVAL; |
| 207 | } |
| 208 | break; |
| 209 | #ifdef HAVE_BCG729 |
| 210 | case AF_G729: |
| 211 | state->src_frame_size = 10; |
| 212 | state->src_samples_per_frame = 80; |
| 213 | state->src.g729_dec = initBcg729DecoderChannel(); |
| 214 | if (!state->src.g729_dec) { |
| 215 | LOGP(DMGCP, LOGL_ERROR, |
| 216 | "Failed to initialize G.729 decoder.\n"); |
| 217 | return -EINVAL; |
| 218 | } |
| 219 | break; |
| 220 | #endif |
| 221 | case AF_PCMA: |
| 222 | state->src_frame_size = 80; |
| 223 | state->src_samples_per_frame = 80; |
| 224 | break; |
| 225 | default: |
| 226 | break; |
| 227 | } |
| 228 | |
| 229 | state->dst_fmt = dst_fmt; |
| 230 | |
| 231 | switch (state->dst_fmt) { |
| 232 | case AF_L16: |
| 233 | case AF_S16: |
| 234 | state->dst_frame_size = 80*sizeof(short); |
| 235 | state->dst_samples_per_frame = 80; |
| 236 | break; |
| 237 | case AF_GSM: |
| 238 | state->dst_frame_size = sizeof(gsm_frame); |
| 239 | state->dst_samples_per_frame = 160; |
| 240 | state->dst.gsm_handle = gsm_create(); |
| 241 | if (!state->dst.gsm_handle) { |
| 242 | LOGP(DMGCP, LOGL_ERROR, |
| 243 | "Failed to initialize GSM encoder.\n"); |
| 244 | return -EINVAL; |
| 245 | } |
| 246 | break; |
| 247 | #ifdef HAVE_BCG729 |
| 248 | case AF_G729: |
| 249 | state->dst_frame_size = 10; |
| 250 | state->dst_samples_per_frame = 80; |
| 251 | state->dst.g729_enc = initBcg729EncoderChannel(); |
| 252 | if (!state->dst.g729_enc) { |
| 253 | LOGP(DMGCP, LOGL_ERROR, |
| 254 | "Failed to initialize G.729 decoder.\n"); |
| 255 | return -EINVAL; |
| 256 | } |
| 257 | break; |
| 258 | #endif |
| 259 | case AF_PCMA: |
| 260 | state->dst_frame_size = 80; |
| 261 | state->dst_samples_per_frame = 80; |
| 262 | break; |
| 263 | default: |
| 264 | break; |
| 265 | } |
| 266 | |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 267 | if (dst_end->force_output_ptime) |
| 268 | state->dst_packet_duration = mgcp_rtp_packet_duration(endp, dst_end); |
| 269 | |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 270 | LOGP(DMGCP, LOGL_INFO, |
| 271 | "Initialized RTP processing on: 0x%x " |
| 272 | "conv: %d (%d, %d, %s) -> %d (%d, %d, %s)\n", |
| 273 | ENDPOINT_NUMBER(endp), |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame^] | 274 | src_fmt, src_codec->payload_type, src_codec->rate, src_end->fmtp_extra, |
| 275 | 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] | 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | void mgcp_transcoding_net_downlink_format(struct mgcp_endpoint *endp, |
| 281 | int *payload_type, |
| 282 | const char**audio_name, |
| 283 | const char**fmtp_extra) |
| 284 | { |
| 285 | 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^] | 286 | struct mgcp_rtp_codec *net_codec = &endp->net_end.codec; |
| 287 | struct mgcp_rtp_codec *bts_codec = &endp->bts_end.codec; |
| 288 | |
| 289 | if (!state || net_codec->payload_type < 0) { |
| 290 | *payload_type = bts_codec->payload_type; |
| 291 | *audio_name = bts_codec->audio_name; |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 292 | *fmtp_extra = endp->bts_end.fmtp_extra; |
| 293 | return; |
| 294 | } |
| 295 | |
Holger Hans Peter Freyther | cac2438 | 2014-09-01 10:35:55 +0200 | [diff] [blame^] | 296 | *payload_type = net_codec->payload_type; |
| 297 | *audio_name = net_codec->audio_name; |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 298 | *fmtp_extra = endp->net_end.fmtp_extra; |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 299 | } |
| 300 | |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 301 | static int decode_audio(struct mgcp_process_rtp_state *state, |
| 302 | uint8_t **src, size_t *nbytes) |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 303 | { |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 304 | while (*nbytes >= state->src_frame_size) { |
| 305 | 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] | 306 | LOGP(DMGCP, LOGL_ERROR, |
| 307 | "Sample buffer too small: %d > %d.\n", |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 308 | state->sample_cnt + state->src_samples_per_frame, |
| 309 | ARRAY_SIZE(state->samples)); |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 310 | return -ENOSPC; |
| 311 | } |
| 312 | switch (state->src_fmt) { |
| 313 | case AF_GSM: |
| 314 | if (gsm_decode(state->src.gsm_handle, |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 315 | (gsm_byte *)*src, state->samples + state->sample_cnt) < 0) { |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 316 | LOGP(DMGCP, LOGL_ERROR, |
| 317 | "Failed to decode GSM.\n"); |
| 318 | return -EINVAL; |
| 319 | } |
| 320 | break; |
| 321 | #ifdef HAVE_BCG729 |
| 322 | case AF_G729: |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 323 | bcg729Decoder(state->src.g729_dec, *src, 0, state->samples + state->sample_cnt); |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 324 | break; |
| 325 | #endif |
| 326 | case AF_PCMA: |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 327 | alaw_decode(*src, state->samples + state->sample_cnt, |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 328 | state->src_samples_per_frame); |
| 329 | break; |
| 330 | case AF_S16: |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 331 | memmove(state->samples + state->sample_cnt, *src, |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 332 | state->src_frame_size); |
| 333 | break; |
| 334 | case AF_L16: |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 335 | l16_decode(*src, state->samples + state->sample_cnt, |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 336 | state->src_samples_per_frame); |
| 337 | break; |
| 338 | default: |
| 339 | break; |
| 340 | } |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 341 | *src += state->src_frame_size; |
| 342 | *nbytes -= state->src_frame_size; |
| 343 | state->sample_cnt += state->src_samples_per_frame; |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 344 | } |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 345 | return 0; |
| 346 | } |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 347 | |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 348 | static int encode_audio(struct mgcp_process_rtp_state *state, |
| 349 | uint8_t *dst, size_t buf_size, size_t max_samples) |
| 350 | { |
| 351 | int nbytes = 0; |
| 352 | size_t nsamples = 0; |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 353 | /* Encode samples into dst */ |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 354 | while (nsamples + state->dst_samples_per_frame <= max_samples) { |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 355 | if (nbytes + state->dst_frame_size > buf_size) { |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 356 | if (nbytes > 0) |
| 357 | break; |
| 358 | |
| 359 | /* Not even one frame fits into the buffer */ |
| 360 | LOGP(DMGCP, LOGL_INFO, |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 361 | "Encoding (RTP) buffer too small: %d > %d.\n", |
| 362 | nbytes + state->dst_frame_size, buf_size); |
| 363 | return -ENOSPC; |
| 364 | } |
| 365 | switch (state->dst_fmt) { |
| 366 | case AF_GSM: |
| 367 | gsm_encode(state->dst.gsm_handle, |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 368 | state->samples + state->sample_offs, dst); |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 369 | break; |
| 370 | #ifdef HAVE_BCG729 |
| 371 | case AF_G729: |
| 372 | bcg729Encoder(state->dst.g729_enc, |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 373 | state->samples + state->sample_offs, dst); |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 374 | break; |
| 375 | #endif |
| 376 | case AF_PCMA: |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 377 | alaw_encode(state->samples + state->sample_offs, dst, |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 378 | state->src_samples_per_frame); |
| 379 | break; |
| 380 | case AF_S16: |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 381 | memmove(dst, state->samples + state->sample_offs, |
| 382 | state->dst_frame_size); |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 383 | break; |
| 384 | case AF_L16: |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 385 | l16_encode(state->samples + state->sample_offs, dst, |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 386 | state->src_samples_per_frame); |
| 387 | break; |
| 388 | default: |
| 389 | break; |
| 390 | } |
| 391 | dst += state->dst_frame_size; |
| 392 | nbytes += state->dst_frame_size; |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 393 | state->sample_offs += state->dst_samples_per_frame; |
| 394 | nsamples += state->dst_samples_per_frame; |
| 395 | } |
| 396 | state->sample_cnt -= nsamples; |
| 397 | return nbytes; |
| 398 | } |
| 399 | |
| 400 | int mgcp_transcoding_process_rtp(struct mgcp_endpoint *endp, |
| 401 | struct mgcp_rtp_end *dst_end, |
| 402 | char *data, int *len, int buf_size) |
| 403 | { |
| 404 | struct mgcp_process_rtp_state *state = dst_end->rtp_process_data; |
Holger Hans Peter Freyther | 3713f78 | 2014-09-01 11:02:05 +0200 | [diff] [blame] | 405 | const size_t rtp_hdr_size = sizeof(struct rtp_hdr); |
| 406 | struct rtp_hdr *rtp_hdr = (struct rtp_hdr *) data; |
| 407 | char *payload_data = (char *) &rtp_hdr->data[0]; |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 408 | int payload_len = *len - rtp_hdr_size; |
| 409 | uint8_t *src = (uint8_t *)payload_data; |
| 410 | uint8_t *dst = (uint8_t *)payload_data; |
| 411 | size_t nbytes = payload_len; |
| 412 | size_t nsamples; |
| 413 | size_t max_samples; |
| 414 | uint32_t ts_no; |
| 415 | int rc; |
| 416 | |
| 417 | if (!state) |
| 418 | return 0; |
| 419 | |
| 420 | if (state->src_fmt == state->dst_fmt) { |
| 421 | if (!state->dst_packet_duration) |
| 422 | return 0; |
| 423 | |
| 424 | /* TODO: repackage without transcoding */ |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 425 | } |
| 426 | |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 427 | /* If the remaining samples do not fit into a fixed ptime, |
| 428 | * a) discard them, if the next packet is much later |
| 429 | * b) add silence and * send it, if the current packet is not |
| 430 | * yet too late |
| 431 | * c) append the sample data, if the timestamp matches exactly |
| 432 | */ |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 433 | |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 434 | /* TODO: check payload type (-> G.711 comfort noise) */ |
| 435 | |
| 436 | if (payload_len > 0) { |
Holger Hans Peter Freyther | 3713f78 | 2014-09-01 11:02:05 +0200 | [diff] [blame] | 437 | ts_no = ntohl(rtp_hdr->timestamp); |
Holger Hans Peter Freyther | c8b2908 | 2014-07-02 22:02:15 +0200 | [diff] [blame] | 438 | if (!state->is_running) { |
Holger Hans Peter Freyther | 3713f78 | 2014-09-01 11:02:05 +0200 | [diff] [blame] | 439 | state->next_seq = ntohs(rtp_hdr->sequence); |
Holger Hans Peter Freyther | c8b2908 | 2014-07-02 22:02:15 +0200 | [diff] [blame] | 440 | state->next_time = ts_no; |
| 441 | state->is_running = 1; |
| 442 | } |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 443 | |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 444 | |
| 445 | if (state->sample_cnt > 0) { |
| 446 | int32_t delta = ts_no - state->next_time; |
| 447 | /* TODO: check sequence? reordering? packet loss? */ |
| 448 | |
Holger Hans Peter Freyther | e52ca9a | 2014-07-02 21:56:26 +0200 | [diff] [blame] | 449 | if (delta > state->sample_cnt) { |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 450 | /* There is a time gap between the last packet |
| 451 | * and the current one. Just discard the |
| 452 | * partial data that is left in the buffer. |
| 453 | * TODO: This can be improved by adding silence |
| 454 | * instead if the delta is small enough. |
| 455 | */ |
Holger Hans Peter Freyther | e52ca9a | 2014-07-02 21:56:26 +0200 | [diff] [blame] | 456 | LOGP(DMGCP, LOGL_NOTICE, |
| 457 | "0x%x dropping sample buffer due delta=%d sample_cnt=%d\n", |
| 458 | ENDPOINT_NUMBER(endp), delta, state->sample_cnt); |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 459 | state->sample_cnt = 0; |
Holger Hans Peter Freyther | b936278 | 2014-07-04 20:55:20 +0200 | [diff] [blame] | 460 | state->next_time = ts_no; |
Holger Hans Peter Freyther | e52ca9a | 2014-07-02 21:56:26 +0200 | [diff] [blame] | 461 | } else if (delta < 0) { |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 462 | LOGP(DMGCP, LOGL_NOTICE, |
| 463 | "RTP time jumps backwards, delta = %d, " |
| 464 | "discarding buffered samples\n", |
| 465 | delta); |
| 466 | state->sample_cnt = 0; |
| 467 | state->sample_offs = 0; |
| 468 | return -EAGAIN; |
| 469 | } |
| 470 | |
| 471 | /* Make sure the samples start without offset */ |
| 472 | if (state->sample_offs && state->sample_cnt) |
| 473 | memmove(&state->samples[0], |
| 474 | &state->samples[state->sample_offs], |
| 475 | state->sample_cnt * |
| 476 | sizeof(state->samples[0])); |
| 477 | } |
| 478 | |
| 479 | state->sample_offs = 0; |
| 480 | |
| 481 | /* Append decoded audio to samples */ |
| 482 | decode_audio(state, &src, &nbytes); |
| 483 | |
| 484 | if (nbytes > 0) |
| 485 | LOGP(DMGCP, LOGL_NOTICE, |
| 486 | "Skipped audio frame in RTP packet: %d octets\n", |
| 487 | nbytes); |
| 488 | } else |
| 489 | ts_no = state->next_time; |
| 490 | |
| 491 | if (state->sample_cnt < state->dst_packet_duration) |
| 492 | return -EAGAIN; |
| 493 | |
| 494 | max_samples = |
| 495 | state->dst_packet_duration ? |
| 496 | state->dst_packet_duration : state->sample_cnt; |
| 497 | |
| 498 | nsamples = state->sample_cnt; |
| 499 | |
| 500 | rc = encode_audio(state, dst, buf_size, max_samples); |
Holger Hans Peter Freyther | bd4109b | 2014-06-27 19:27:38 +0200 | [diff] [blame] | 501 | /* |
| 502 | * There were no samples to encode? |
| 503 | * TODO: how does this work for comfort noise? |
| 504 | */ |
| 505 | if (rc == 0) |
| 506 | return -ENOMSG; |
| 507 | /* Any other error during the encoding */ |
| 508 | if (rc < 0) |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 509 | return rc; |
| 510 | |
| 511 | nsamples -= state->sample_cnt; |
| 512 | |
| 513 | *len = rtp_hdr_size + rc; |
Holger Hans Peter Freyther | 3713f78 | 2014-09-01 11:02:05 +0200 | [diff] [blame] | 514 | rtp_hdr->sequence = htons(state->next_seq); |
| 515 | rtp_hdr->timestamp = htonl(ts_no); |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 516 | |
| 517 | state->next_seq += 1; |
| 518 | state->next_time = ts_no + nsamples; |
| 519 | |
Holger Hans Peter Freyther | 77ceaaf | 2014-06-28 15:05:42 +0200 | [diff] [blame] | 520 | /* |
| 521 | * XXX: At this point we should always have consumed |
| 522 | * samples. So doing OSMO_ASSERT(nsamples > 0) and returning |
| 523 | * rtp_hdr_size should be fine. |
| 524 | */ |
Jacob Erlbeck | 42a833e | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 525 | return nsamples ? rtp_hdr_size : 0; |
Jacob Erlbeck | 239a853 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 526 | } |