blob: 88de7d2b2ab258913a500a23e2fa4108b52decc4 [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
Jacob Erlbeck239a8532014-03-13 14:25:51 +010025#include "g711common.h"
Jacob Erlbeck239a8532014-03-13 14:25:51 +010026
27#include <openbsc/debug.h>
28#include <openbsc/mgcp.h>
29#include <openbsc/mgcp_internal.h>
Holger Hans Peter Freytherdd1f8152014-07-22 13:05:31 +020030#include <openbsc/mgcp_transcode.h>
Holger Hans Peter Freyther3713f782014-09-01 11:02:05 +020031#include <openbsc/rtp.h>
Jacob Erlbeck239a8532014-03-13 14:25:51 +010032
33#include <osmocom/core/talloc.h>
34
Jacob Erlbeck136a3192014-03-13 14:33:37 +010035int 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
Jacob Erlbeck239a8532014-03-13 14:25:51 +010048static enum audio_format get_audio_format(const struct mgcp_rtp_end *rtp_end)
49{
50 if (rtp_end->subtype_name) {
51 if (!strcmp("GSM", rtp_end->subtype_name))
52 return AF_GSM;
53 if (!strcmp("PCMA", rtp_end->subtype_name))
54 return AF_PCMA;
55#ifdef HAVE_BCG729
56 if (!strcmp("G729", rtp_end->subtype_name))
57 return AF_G729;
58#endif
59 if (!strcmp("L16", rtp_end->subtype_name))
60 return AF_L16;
61 }
62
63 switch (rtp_end->payload_type) {
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
79static 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
87static 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
93static 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
99static 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
105static int processing_state_destructor(struct mgcp_process_rtp_state *state)
106{
107 switch (state->src_fmt) {
108 case AF_GSM:
Holger Hans Peter Freytherc5c239f2014-07-07 20:38:27 +0200109 if (state->src.gsm_handle)
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100110 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
138int 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;
144
145 /* cleanup first */
146 if (dst_end->rtp_process_data) {
147 talloc_free(dst_end->rtp_process_data);
148 dst_end->rtp_process_data = NULL;
149 }
150
151 if (!src_end)
152 return 0;
153
154 src_fmt = get_audio_format(src_end);
155 dst_fmt = get_audio_format(dst_end);
156
157 LOGP(DMGCP, LOGL_ERROR,
158 "Checking transcoding: %s (%d) -> %s (%d)\n",
159 src_end->subtype_name, src_end->payload_type,
160 dst_end->subtype_name, dst_end->payload_type);
161
162 if (src_fmt == AF_INVALID || dst_fmt == AF_INVALID) {
163 if (!src_end->subtype_name || !dst_end->subtype_name)
164 /* Not enough info, do nothing */
165 return 0;
166
167 if (strcmp(src_end->subtype_name, dst_end->subtype_name) == 0)
168 /* Nothing to do */
169 return 0;
170
171 LOGP(DMGCP, LOGL_ERROR,
172 "Cannot transcode: %s codec not supported (%s -> %s).\n",
173 src_fmt != AF_INVALID ? "destination" : "source",
174 src_end->audio_name, dst_end->audio_name);
175 return -EINVAL;
176 }
177
178 if (src_end->rate && dst_end->rate && src_end->rate != dst_end->rate) {
179 LOGP(DMGCP, LOGL_ERROR,
180 "Cannot transcode: rate conversion (%d -> %d) not supported.\n",
181 src_end->rate, dst_end->rate);
182 return -EINVAL;
183 }
184
185 state = talloc_zero(endp->tcfg->cfg, struct mgcp_process_rtp_state);
186 talloc_set_destructor(state, processing_state_destructor);
187 dst_end->rtp_process_data = state;
188
189 state->src_fmt = src_fmt;
190
191 switch (state->src_fmt) {
192 case AF_L16:
193 case AF_S16:
194 state->src_frame_size = 80 * sizeof(short);
195 state->src_samples_per_frame = 80;
196 break;
197 case AF_GSM:
198 state->src_frame_size = sizeof(gsm_frame);
199 state->src_samples_per_frame = 160;
200 state->src.gsm_handle = gsm_create();
201 if (!state->src.gsm_handle) {
202 LOGP(DMGCP, LOGL_ERROR,
203 "Failed to initialize GSM decoder.\n");
204 return -EINVAL;
205 }
206 break;
207#ifdef HAVE_BCG729
208 case AF_G729:
209 state->src_frame_size = 10;
210 state->src_samples_per_frame = 80;
211 state->src.g729_dec = initBcg729DecoderChannel();
212 if (!state->src.g729_dec) {
213 LOGP(DMGCP, LOGL_ERROR,
214 "Failed to initialize G.729 decoder.\n");
215 return -EINVAL;
216 }
217 break;
218#endif
219 case AF_PCMA:
220 state->src_frame_size = 80;
221 state->src_samples_per_frame = 80;
222 break;
223 default:
224 break;
225 }
226
227 state->dst_fmt = dst_fmt;
228
229 switch (state->dst_fmt) {
230 case AF_L16:
231 case AF_S16:
232 state->dst_frame_size = 80*sizeof(short);
233 state->dst_samples_per_frame = 80;
234 break;
235 case AF_GSM:
236 state->dst_frame_size = sizeof(gsm_frame);
237 state->dst_samples_per_frame = 160;
238 state->dst.gsm_handle = gsm_create();
239 if (!state->dst.gsm_handle) {
240 LOGP(DMGCP, LOGL_ERROR,
241 "Failed to initialize GSM encoder.\n");
242 return -EINVAL;
243 }
244 break;
245#ifdef HAVE_BCG729
246 case AF_G729:
247 state->dst_frame_size = 10;
248 state->dst_samples_per_frame = 80;
249 state->dst.g729_enc = initBcg729EncoderChannel();
250 if (!state->dst.g729_enc) {
251 LOGP(DMGCP, LOGL_ERROR,
252 "Failed to initialize G.729 decoder.\n");
253 return -EINVAL;
254 }
255 break;
256#endif
257 case AF_PCMA:
258 state->dst_frame_size = 80;
259 state->dst_samples_per_frame = 80;
260 break;
261 default:
262 break;
263 }
264
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200265 if (dst_end->force_output_ptime)
266 state->dst_packet_duration = mgcp_rtp_packet_duration(endp, dst_end);
267
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100268 LOGP(DMGCP, LOGL_INFO,
269 "Initialized RTP processing on: 0x%x "
270 "conv: %d (%d, %d, %s) -> %d (%d, %d, %s)\n",
271 ENDPOINT_NUMBER(endp),
272 src_fmt, src_end->payload_type, src_end->rate, src_end->fmtp_extra,
273 dst_fmt, dst_end->payload_type, dst_end->rate, dst_end->fmtp_extra);
274
275 return 0;
276}
277
278void mgcp_transcoding_net_downlink_format(struct mgcp_endpoint *endp,
279 int *payload_type,
280 const char**audio_name,
281 const char**fmtp_extra)
282{
283 struct mgcp_process_rtp_state *state = endp->net_end.rtp_process_data;
284 if (!state || endp->net_end.payload_type < 0) {
285 *payload_type = endp->bts_end.payload_type;
286 *audio_name = endp->bts_end.audio_name;
287 *fmtp_extra = endp->bts_end.fmtp_extra;
288 return;
289 }
290
291 *payload_type = endp->net_end.payload_type;
292 *fmtp_extra = endp->net_end.fmtp_extra;
293 *audio_name = endp->net_end.audio_name;
294}
295
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200296static int decode_audio(struct mgcp_process_rtp_state *state,
297 uint8_t **src, size_t *nbytes)
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100298{
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200299 while (*nbytes >= state->src_frame_size) {
300 if (state->sample_cnt + state->src_samples_per_frame > ARRAY_SIZE(state->samples)) {
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100301 LOGP(DMGCP, LOGL_ERROR,
302 "Sample buffer too small: %d > %d.\n",
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200303 state->sample_cnt + state->src_samples_per_frame,
304 ARRAY_SIZE(state->samples));
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100305 return -ENOSPC;
306 }
307 switch (state->src_fmt) {
308 case AF_GSM:
309 if (gsm_decode(state->src.gsm_handle,
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200310 (gsm_byte *)*src, state->samples + state->sample_cnt) < 0) {
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100311 LOGP(DMGCP, LOGL_ERROR,
312 "Failed to decode GSM.\n");
313 return -EINVAL;
314 }
315 break;
316#ifdef HAVE_BCG729
317 case AF_G729:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200318 bcg729Decoder(state->src.g729_dec, *src, 0, state->samples + state->sample_cnt);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100319 break;
320#endif
321 case AF_PCMA:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200322 alaw_decode(*src, state->samples + state->sample_cnt,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100323 state->src_samples_per_frame);
324 break;
325 case AF_S16:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200326 memmove(state->samples + state->sample_cnt, *src,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100327 state->src_frame_size);
328 break;
329 case AF_L16:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200330 l16_decode(*src, state->samples + state->sample_cnt,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100331 state->src_samples_per_frame);
332 break;
333 default:
334 break;
335 }
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200336 *src += state->src_frame_size;
337 *nbytes -= state->src_frame_size;
338 state->sample_cnt += state->src_samples_per_frame;
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100339 }
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200340 return 0;
341}
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100342
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200343static int encode_audio(struct mgcp_process_rtp_state *state,
344 uint8_t *dst, size_t buf_size, size_t max_samples)
345{
346 int nbytes = 0;
347 size_t nsamples = 0;
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100348 /* Encode samples into dst */
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200349 while (nsamples + state->dst_samples_per_frame <= max_samples) {
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100350 if (nbytes + state->dst_frame_size > buf_size) {
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200351 if (nbytes > 0)
352 break;
353
354 /* Not even one frame fits into the buffer */
355 LOGP(DMGCP, LOGL_INFO,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100356 "Encoding (RTP) buffer too small: %d > %d.\n",
357 nbytes + state->dst_frame_size, buf_size);
358 return -ENOSPC;
359 }
360 switch (state->dst_fmt) {
361 case AF_GSM:
362 gsm_encode(state->dst.gsm_handle,
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200363 state->samples + state->sample_offs, dst);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100364 break;
365#ifdef HAVE_BCG729
366 case AF_G729:
367 bcg729Encoder(state->dst.g729_enc,
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200368 state->samples + state->sample_offs, dst);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100369 break;
370#endif
371 case AF_PCMA:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200372 alaw_encode(state->samples + state->sample_offs, dst,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100373 state->src_samples_per_frame);
374 break;
375 case AF_S16:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200376 memmove(dst, state->samples + state->sample_offs,
377 state->dst_frame_size);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100378 break;
379 case AF_L16:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200380 l16_encode(state->samples + state->sample_offs, dst,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100381 state->src_samples_per_frame);
382 break;
383 default:
384 break;
385 }
386 dst += state->dst_frame_size;
387 nbytes += state->dst_frame_size;
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200388 state->sample_offs += state->dst_samples_per_frame;
389 nsamples += state->dst_samples_per_frame;
390 }
391 state->sample_cnt -= nsamples;
392 return nbytes;
393}
394
395int mgcp_transcoding_process_rtp(struct mgcp_endpoint *endp,
396 struct mgcp_rtp_end *dst_end,
397 char *data, int *len, int buf_size)
398{
399 struct mgcp_process_rtp_state *state = dst_end->rtp_process_data;
Holger Hans Peter Freyther3713f782014-09-01 11:02:05 +0200400 const size_t rtp_hdr_size = sizeof(struct rtp_hdr);
401 struct rtp_hdr *rtp_hdr = (struct rtp_hdr *) data;
402 char *payload_data = (char *) &rtp_hdr->data[0];
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200403 int payload_len = *len - rtp_hdr_size;
404 uint8_t *src = (uint8_t *)payload_data;
405 uint8_t *dst = (uint8_t *)payload_data;
406 size_t nbytes = payload_len;
407 size_t nsamples;
408 size_t max_samples;
409 uint32_t ts_no;
410 int rc;
411
412 if (!state)
413 return 0;
414
415 if (state->src_fmt == state->dst_fmt) {
416 if (!state->dst_packet_duration)
417 return 0;
418
419 /* TODO: repackage without transcoding */
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100420 }
421
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200422 /* If the remaining samples do not fit into a fixed ptime,
423 * a) discard them, if the next packet is much later
424 * b) add silence and * send it, if the current packet is not
425 * yet too late
426 * c) append the sample data, if the timestamp matches exactly
427 */
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100428
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200429 /* TODO: check payload type (-> G.711 comfort noise) */
430
431 if (payload_len > 0) {
Holger Hans Peter Freyther3713f782014-09-01 11:02:05 +0200432 ts_no = ntohl(rtp_hdr->timestamp);
Holger Hans Peter Freytherc8b29082014-07-02 22:02:15 +0200433 if (!state->is_running) {
Holger Hans Peter Freyther3713f782014-09-01 11:02:05 +0200434 state->next_seq = ntohs(rtp_hdr->sequence);
Holger Hans Peter Freytherc8b29082014-07-02 22:02:15 +0200435 state->next_time = ts_no;
436 state->is_running = 1;
437 }
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200438
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200439
440 if (state->sample_cnt > 0) {
441 int32_t delta = ts_no - state->next_time;
442 /* TODO: check sequence? reordering? packet loss? */
443
Holger Hans Peter Freythere52ca9a2014-07-02 21:56:26 +0200444 if (delta > state->sample_cnt) {
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200445 /* There is a time gap between the last packet
446 * and the current one. Just discard the
447 * partial data that is left in the buffer.
448 * TODO: This can be improved by adding silence
449 * instead if the delta is small enough.
450 */
Holger Hans Peter Freythere52ca9a2014-07-02 21:56:26 +0200451 LOGP(DMGCP, LOGL_NOTICE,
452 "0x%x dropping sample buffer due delta=%d sample_cnt=%d\n",
453 ENDPOINT_NUMBER(endp), delta, state->sample_cnt);
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200454 state->sample_cnt = 0;
Holger Hans Peter Freytherb9362782014-07-04 20:55:20 +0200455 state->next_time = ts_no;
Holger Hans Peter Freythere52ca9a2014-07-02 21:56:26 +0200456 } else if (delta < 0) {
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200457 LOGP(DMGCP, LOGL_NOTICE,
458 "RTP time jumps backwards, delta = %d, "
459 "discarding buffered samples\n",
460 delta);
461 state->sample_cnt = 0;
462 state->sample_offs = 0;
463 return -EAGAIN;
464 }
465
466 /* Make sure the samples start without offset */
467 if (state->sample_offs && state->sample_cnt)
468 memmove(&state->samples[0],
469 &state->samples[state->sample_offs],
470 state->sample_cnt *
471 sizeof(state->samples[0]));
472 }
473
474 state->sample_offs = 0;
475
476 /* Append decoded audio to samples */
477 decode_audio(state, &src, &nbytes);
478
479 if (nbytes > 0)
480 LOGP(DMGCP, LOGL_NOTICE,
481 "Skipped audio frame in RTP packet: %d octets\n",
482 nbytes);
483 } else
484 ts_no = state->next_time;
485
486 if (state->sample_cnt < state->dst_packet_duration)
487 return -EAGAIN;
488
489 max_samples =
490 state->dst_packet_duration ?
491 state->dst_packet_duration : state->sample_cnt;
492
493 nsamples = state->sample_cnt;
494
495 rc = encode_audio(state, dst, buf_size, max_samples);
Holger Hans Peter Freytherbd4109b2014-06-27 19:27:38 +0200496 /*
497 * There were no samples to encode?
498 * TODO: how does this work for comfort noise?
499 */
500 if (rc == 0)
501 return -ENOMSG;
502 /* Any other error during the encoding */
503 if (rc < 0)
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200504 return rc;
505
506 nsamples -= state->sample_cnt;
507
508 *len = rtp_hdr_size + rc;
Holger Hans Peter Freyther3713f782014-09-01 11:02:05 +0200509 rtp_hdr->sequence = htons(state->next_seq);
510 rtp_hdr->timestamp = htonl(ts_no);
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200511
512 state->next_seq += 1;
513 state->next_time = ts_no + nsamples;
514
Holger Hans Peter Freyther77ceaaf2014-06-28 15:05:42 +0200515 /*
516 * XXX: At this point we should always have consumed
517 * samples. So doing OSMO_ASSERT(nsamples > 0) and returning
518 * rtp_hdr_size should be fine.
519 */
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200520 return nsamples ? rtp_hdr_size : 0;
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100521}