blob: b76c5d9ac2c87518e2368f8f7817b0bbd821d562 [file] [log] [blame]
Jacob Erlbeck239a8532014-03-13 14:25:51 +01001/*
Jacob Erlbeck239a8532014-03-13 14:25:51 +01002 * (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 Erlbeck42a833e2014-04-14 10:31:47 +020024
25#include "../../bscconfig.h"
Jacob Erlbeck239a8532014-03-13 14:25:51 +010026
27#include "g711common.h"
28#include <gsm.h>
29#ifdef HAVE_BCG729
30#include <bcg729/decoder.h>
31#include <bcg729/encoder.h>
32#endif
33
34#include <openbsc/debug.h>
35#include <openbsc/mgcp.h>
36#include <openbsc/mgcp_internal.h>
37
38#include <osmocom/core/talloc.h>
39
40enum audio_format {
41 AF_INVALID,
42 AF_S16,
43 AF_L16,
44 AF_GSM,
45 AF_G729,
46 AF_PCMA
47};
48
49struct mgcp_process_rtp_state {
50 /* decoding */
51 enum audio_format src_fmt;
52 union {
53 gsm gsm_handle;
54#ifdef HAVE_BCG729
55 bcg729DecoderChannelContextStruct *g729_dec;
56#endif
57 } src;
58 size_t src_frame_size;
59 size_t src_samples_per_frame;
60
61 /* processing */
62
63 /* encoding */
64 enum audio_format dst_fmt;
65 union {
66 gsm gsm_handle;
67#ifdef HAVE_BCG729
68 bcg729EncoderChannelContextStruct *g729_enc;
69#endif
70 } dst;
71 size_t dst_frame_size;
72 size_t dst_samples_per_frame;
Jacob Erlbeck42a833e2014-04-14 10:31:47 +020073 int dst_packet_duration;
74
75 int is_running;
76 uint16_t next_seq;
77 uint32_t next_time;
78 int16_t samples[10*160];
79 size_t sample_cnt;
80 size_t sample_offs;
Jacob Erlbeck239a8532014-03-13 14:25:51 +010081};
82
Jacob Erlbeck136a3192014-03-13 14:33:37 +010083int mgcp_transcoding_get_frame_size(void *state_, int nsamples, int dst)
84{
85 struct mgcp_process_rtp_state *state = state_;
86 if (dst)
87 return (nsamples >= 0 ?
88 nsamples / state->dst_samples_per_frame :
89 1) * state->dst_frame_size;
90 else
91 return (nsamples >= 0 ?
92 nsamples / state->src_samples_per_frame :
93 1) * state->src_frame_size;
94}
95
Jacob Erlbeck239a8532014-03-13 14:25:51 +010096static enum audio_format get_audio_format(const struct mgcp_rtp_end *rtp_end)
97{
98 if (rtp_end->subtype_name) {
99 if (!strcmp("GSM", rtp_end->subtype_name))
100 return AF_GSM;
101 if (!strcmp("PCMA", rtp_end->subtype_name))
102 return AF_PCMA;
103#ifdef HAVE_BCG729
104 if (!strcmp("G729", rtp_end->subtype_name))
105 return AF_G729;
106#endif
107 if (!strcmp("L16", rtp_end->subtype_name))
108 return AF_L16;
109 }
110
111 switch (rtp_end->payload_type) {
112 case 3 /* GSM */:
113 return AF_GSM;
114 case 8 /* PCMA */:
115 return AF_PCMA;
116#ifdef HAVE_BCG729
117 case 18 /* G.729 */:
118 return AF_G729;
119#endif
120 case 11 /* L16 */:
121 return AF_L16;
122 default:
123 return AF_INVALID;
124 }
125}
126
127static void l16_encode(short *sample, unsigned char *buf, size_t n)
128{
129 for (; n > 0; --n, ++sample, buf += 2) {
130 buf[0] = sample[0] >> 8;
131 buf[1] = sample[0] & 0xff;
132 }
133}
134
135static void l16_decode(unsigned char *buf, short *sample, size_t n)
136{
137 for (; n > 0; --n, ++sample, buf += 2)
138 sample[0] = ((short)buf[0] << 8) | buf[1];
139}
140
141static void alaw_encode(short *sample, unsigned char *buf, size_t n)
142{
143 for (; n > 0; --n)
144 *(buf++) = s16_to_alaw(*(sample++));
145}
146
147static void alaw_decode(unsigned char *buf, short *sample, size_t n)
148{
149 for (; n > 0; --n)
150 *(sample++) = alaw_to_s16(*(buf++));
151}
152
153static int processing_state_destructor(struct mgcp_process_rtp_state *state)
154{
155 switch (state->src_fmt) {
156 case AF_GSM:
Holger Hans Peter Freytherc5c239f2014-07-07 20:38:27 +0200157 if (state->src.gsm_handle)
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100158 gsm_destroy(state->src.gsm_handle);
159 break;
160#ifdef HAVE_BCG729
161 case AF_G729:
162 if (state->src.g729_dec)
163 closeBcg729DecoderChannel(state->src.g729_dec);
164 break;
165#endif
166 default:
167 break;
168 }
169 switch (state->dst_fmt) {
170 case AF_GSM:
171 if (state->dst.gsm_handle)
172 gsm_destroy(state->dst.gsm_handle);
173 break;
174#ifdef HAVE_BCG729
175 case AF_G729:
176 if (state->dst.g729_enc)
177 closeBcg729EncoderChannel(state->dst.g729_enc);
178 break;
179#endif
180 default:
181 break;
182 }
183 return 0;
184}
185
186int mgcp_transcoding_setup(struct mgcp_endpoint *endp,
187 struct mgcp_rtp_end *dst_end,
188 struct mgcp_rtp_end *src_end)
189{
190 struct mgcp_process_rtp_state *state;
191 enum audio_format src_fmt, dst_fmt;
192
193 /* cleanup first */
194 if (dst_end->rtp_process_data) {
195 talloc_free(dst_end->rtp_process_data);
196 dst_end->rtp_process_data = NULL;
197 }
198
199 if (!src_end)
200 return 0;
201
202 src_fmt = get_audio_format(src_end);
203 dst_fmt = get_audio_format(dst_end);
204
205 LOGP(DMGCP, LOGL_ERROR,
206 "Checking transcoding: %s (%d) -> %s (%d)\n",
207 src_end->subtype_name, src_end->payload_type,
208 dst_end->subtype_name, dst_end->payload_type);
209
210 if (src_fmt == AF_INVALID || dst_fmt == AF_INVALID) {
211 if (!src_end->subtype_name || !dst_end->subtype_name)
212 /* Not enough info, do nothing */
213 return 0;
214
215 if (strcmp(src_end->subtype_name, dst_end->subtype_name) == 0)
216 /* Nothing to do */
217 return 0;
218
219 LOGP(DMGCP, LOGL_ERROR,
220 "Cannot transcode: %s codec not supported (%s -> %s).\n",
221 src_fmt != AF_INVALID ? "destination" : "source",
222 src_end->audio_name, dst_end->audio_name);
223 return -EINVAL;
224 }
225
226 if (src_end->rate && dst_end->rate && src_end->rate != dst_end->rate) {
227 LOGP(DMGCP, LOGL_ERROR,
228 "Cannot transcode: rate conversion (%d -> %d) not supported.\n",
229 src_end->rate, dst_end->rate);
230 return -EINVAL;
231 }
232
233 state = talloc_zero(endp->tcfg->cfg, struct mgcp_process_rtp_state);
234 talloc_set_destructor(state, processing_state_destructor);
235 dst_end->rtp_process_data = state;
236
237 state->src_fmt = src_fmt;
238
239 switch (state->src_fmt) {
240 case AF_L16:
241 case AF_S16:
242 state->src_frame_size = 80 * sizeof(short);
243 state->src_samples_per_frame = 80;
244 break;
245 case AF_GSM:
246 state->src_frame_size = sizeof(gsm_frame);
247 state->src_samples_per_frame = 160;
248 state->src.gsm_handle = gsm_create();
249 if (!state->src.gsm_handle) {
250 LOGP(DMGCP, LOGL_ERROR,
251 "Failed to initialize GSM decoder.\n");
252 return -EINVAL;
253 }
254 break;
255#ifdef HAVE_BCG729
256 case AF_G729:
257 state->src_frame_size = 10;
258 state->src_samples_per_frame = 80;
259 state->src.g729_dec = initBcg729DecoderChannel();
260 if (!state->src.g729_dec) {
261 LOGP(DMGCP, LOGL_ERROR,
262 "Failed to initialize G.729 decoder.\n");
263 return -EINVAL;
264 }
265 break;
266#endif
267 case AF_PCMA:
268 state->src_frame_size = 80;
269 state->src_samples_per_frame = 80;
270 break;
271 default:
272 break;
273 }
274
275 state->dst_fmt = dst_fmt;
276
277 switch (state->dst_fmt) {
278 case AF_L16:
279 case AF_S16:
280 state->dst_frame_size = 80*sizeof(short);
281 state->dst_samples_per_frame = 80;
282 break;
283 case AF_GSM:
284 state->dst_frame_size = sizeof(gsm_frame);
285 state->dst_samples_per_frame = 160;
286 state->dst.gsm_handle = gsm_create();
287 if (!state->dst.gsm_handle) {
288 LOGP(DMGCP, LOGL_ERROR,
289 "Failed to initialize GSM encoder.\n");
290 return -EINVAL;
291 }
292 break;
293#ifdef HAVE_BCG729
294 case AF_G729:
295 state->dst_frame_size = 10;
296 state->dst_samples_per_frame = 80;
297 state->dst.g729_enc = initBcg729EncoderChannel();
298 if (!state->dst.g729_enc) {
299 LOGP(DMGCP, LOGL_ERROR,
300 "Failed to initialize G.729 decoder.\n");
301 return -EINVAL;
302 }
303 break;
304#endif
305 case AF_PCMA:
306 state->dst_frame_size = 80;
307 state->dst_samples_per_frame = 80;
308 break;
309 default:
310 break;
311 }
312
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200313 if (dst_end->force_output_ptime)
314 state->dst_packet_duration = mgcp_rtp_packet_duration(endp, dst_end);
315
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100316 LOGP(DMGCP, LOGL_INFO,
317 "Initialized RTP processing on: 0x%x "
318 "conv: %d (%d, %d, %s) -> %d (%d, %d, %s)\n",
319 ENDPOINT_NUMBER(endp),
320 src_fmt, src_end->payload_type, src_end->rate, src_end->fmtp_extra,
321 dst_fmt, dst_end->payload_type, dst_end->rate, dst_end->fmtp_extra);
322
323 return 0;
324}
325
326void mgcp_transcoding_net_downlink_format(struct mgcp_endpoint *endp,
327 int *payload_type,
328 const char**audio_name,
329 const char**fmtp_extra)
330{
331 struct mgcp_process_rtp_state *state = endp->net_end.rtp_process_data;
332 if (!state || endp->net_end.payload_type < 0) {
333 *payload_type = endp->bts_end.payload_type;
334 *audio_name = endp->bts_end.audio_name;
335 *fmtp_extra = endp->bts_end.fmtp_extra;
336 return;
337 }
338
339 *payload_type = endp->net_end.payload_type;
340 *fmtp_extra = endp->net_end.fmtp_extra;
341 *audio_name = endp->net_end.audio_name;
342}
343
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200344static int decode_audio(struct mgcp_process_rtp_state *state,
345 uint8_t **src, size_t *nbytes)
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100346{
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200347 while (*nbytes >= state->src_frame_size) {
348 if (state->sample_cnt + state->src_samples_per_frame > ARRAY_SIZE(state->samples)) {
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100349 LOGP(DMGCP, LOGL_ERROR,
350 "Sample buffer too small: %d > %d.\n",
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200351 state->sample_cnt + state->src_samples_per_frame,
352 ARRAY_SIZE(state->samples));
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100353 return -ENOSPC;
354 }
355 switch (state->src_fmt) {
356 case AF_GSM:
357 if (gsm_decode(state->src.gsm_handle,
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200358 (gsm_byte *)*src, state->samples + state->sample_cnt) < 0) {
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100359 LOGP(DMGCP, LOGL_ERROR,
360 "Failed to decode GSM.\n");
361 return -EINVAL;
362 }
363 break;
364#ifdef HAVE_BCG729
365 case AF_G729:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200366 bcg729Decoder(state->src.g729_dec, *src, 0, state->samples + state->sample_cnt);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100367 break;
368#endif
369 case AF_PCMA:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200370 alaw_decode(*src, state->samples + state->sample_cnt,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100371 state->src_samples_per_frame);
372 break;
373 case AF_S16:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200374 memmove(state->samples + state->sample_cnt, *src,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100375 state->src_frame_size);
376 break;
377 case AF_L16:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200378 l16_decode(*src, state->samples + state->sample_cnt,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100379 state->src_samples_per_frame);
380 break;
381 default:
382 break;
383 }
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200384 *src += state->src_frame_size;
385 *nbytes -= state->src_frame_size;
386 state->sample_cnt += state->src_samples_per_frame;
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100387 }
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200388 return 0;
389}
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100390
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200391static int encode_audio(struct mgcp_process_rtp_state *state,
392 uint8_t *dst, size_t buf_size, size_t max_samples)
393{
394 int nbytes = 0;
395 size_t nsamples = 0;
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100396 /* Encode samples into dst */
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200397 while (nsamples + state->dst_samples_per_frame <= max_samples) {
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100398 if (nbytes + state->dst_frame_size > buf_size) {
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200399 if (nbytes > 0)
400 break;
401
402 /* Not even one frame fits into the buffer */
403 LOGP(DMGCP, LOGL_INFO,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100404 "Encoding (RTP) buffer too small: %d > %d.\n",
405 nbytes + state->dst_frame_size, buf_size);
406 return -ENOSPC;
407 }
408 switch (state->dst_fmt) {
409 case AF_GSM:
410 gsm_encode(state->dst.gsm_handle,
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200411 state->samples + state->sample_offs, dst);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100412 break;
413#ifdef HAVE_BCG729
414 case AF_G729:
415 bcg729Encoder(state->dst.g729_enc,
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200416 state->samples + state->sample_offs, dst);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100417 break;
418#endif
419 case AF_PCMA:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200420 alaw_encode(state->samples + state->sample_offs, dst,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100421 state->src_samples_per_frame);
422 break;
423 case AF_S16:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200424 memmove(dst, state->samples + state->sample_offs,
425 state->dst_frame_size);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100426 break;
427 case AF_L16:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200428 l16_encode(state->samples + state->sample_offs, dst,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100429 state->src_samples_per_frame);
430 break;
431 default:
432 break;
433 }
434 dst += state->dst_frame_size;
435 nbytes += state->dst_frame_size;
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200436 state->sample_offs += state->dst_samples_per_frame;
437 nsamples += state->dst_samples_per_frame;
438 }
439 state->sample_cnt -= nsamples;
440 return nbytes;
441}
442
443int mgcp_transcoding_process_rtp(struct mgcp_endpoint *endp,
444 struct mgcp_rtp_end *dst_end,
445 char *data, int *len, int buf_size)
446{
447 struct mgcp_process_rtp_state *state = dst_end->rtp_process_data;
448 size_t rtp_hdr_size = 12;
449 char *payload_data = data + rtp_hdr_size;
450 int payload_len = *len - rtp_hdr_size;
451 uint8_t *src = (uint8_t *)payload_data;
452 uint8_t *dst = (uint8_t *)payload_data;
453 size_t nbytes = payload_len;
454 size_t nsamples;
455 size_t max_samples;
456 uint32_t ts_no;
457 int rc;
458
459 if (!state)
460 return 0;
461
462 if (state->src_fmt == state->dst_fmt) {
463 if (!state->dst_packet_duration)
464 return 0;
465
466 /* TODO: repackage without transcoding */
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100467 }
468
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200469 /* If the remaining samples do not fit into a fixed ptime,
470 * a) discard them, if the next packet is much later
471 * b) add silence and * send it, if the current packet is not
472 * yet too late
473 * c) append the sample data, if the timestamp matches exactly
474 */
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100475
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200476 /* TODO: check payload type (-> G.711 comfort noise) */
477
478 if (payload_len > 0) {
479 ts_no = ntohl(*(uint32_t*)(data+4));
480 if (!state->is_running)
481 state->next_seq = ntohs(*(uint32_t*)(data+4));
482
483 state->is_running = 1;
484
485 if (state->sample_cnt > 0) {
486 int32_t delta = ts_no - state->next_time;
487 /* TODO: check sequence? reordering? packet loss? */
488
489 if (delta > state->sample_cnt)
490 /* There is a time gap between the last packet
491 * and the current one. Just discard the
492 * partial data that is left in the buffer.
493 * TODO: This can be improved by adding silence
494 * instead if the delta is small enough.
495 */
496 state->sample_cnt = 0;
497 else if (delta < 0) {
498 LOGP(DMGCP, LOGL_NOTICE,
499 "RTP time jumps backwards, delta = %d, "
500 "discarding buffered samples\n",
501 delta);
502 state->sample_cnt = 0;
503 state->sample_offs = 0;
504 return -EAGAIN;
505 }
506
507 /* Make sure the samples start without offset */
508 if (state->sample_offs && state->sample_cnt)
509 memmove(&state->samples[0],
510 &state->samples[state->sample_offs],
511 state->sample_cnt *
512 sizeof(state->samples[0]));
513 }
514
515 state->sample_offs = 0;
516
517 /* Append decoded audio to samples */
518 decode_audio(state, &src, &nbytes);
519
520 if (nbytes > 0)
521 LOGP(DMGCP, LOGL_NOTICE,
522 "Skipped audio frame in RTP packet: %d octets\n",
523 nbytes);
524 } else
525 ts_no = state->next_time;
526
527 if (state->sample_cnt < state->dst_packet_duration)
528 return -EAGAIN;
529
530 max_samples =
531 state->dst_packet_duration ?
532 state->dst_packet_duration : state->sample_cnt;
533
534 nsamples = state->sample_cnt;
535
536 rc = encode_audio(state, dst, buf_size, max_samples);
537 if (rc <= 0)
538 return rc;
539
540 nsamples -= state->sample_cnt;
541
542 *len = rtp_hdr_size + rc;
543 *(uint16_t*)(data+2) = htonl(state->next_seq);
544 *(uint32_t*)(data+4) = htonl(ts_no);
545
546 state->next_seq += 1;
547 state->next_time = ts_no + nsamples;
548
549 return nsamples ? rtp_hdr_size : 0;
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100550}