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