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