blob: 4d4cec8a2d5ac7b66eb703c02f941132be4cc54d [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>
Jacob Erlbeck239a8532014-03-13 14:25:51 +010031
32#include <osmocom/core/talloc.h>
33
Jacob Erlbeck136a3192014-03-13 14:33:37 +010034int 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 Erlbeck239a8532014-03-13 14:25:51 +010047static 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
78static 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
86static 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
92static 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
98static 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
104static int processing_state_destructor(struct mgcp_process_rtp_state *state)
105{
106 switch (state->src_fmt) {
107 case AF_GSM:
Holger Hans Peter Freytherc5c239f2014-07-07 20:38:27 +0200108 if (state->src.gsm_handle)
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100109 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
137int 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 Erlbeck42a833e2014-04-14 10:31:47 +0200264 if (dst_end->force_output_ptime)
265 state->dst_packet_duration = mgcp_rtp_packet_duration(endp, dst_end);
266
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100267 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
277void 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 Erlbeck42a833e2014-04-14 10:31:47 +0200295static int decode_audio(struct mgcp_process_rtp_state *state,
296 uint8_t **src, size_t *nbytes)
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100297{
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200298 while (*nbytes >= state->src_frame_size) {
299 if (state->sample_cnt + state->src_samples_per_frame > ARRAY_SIZE(state->samples)) {
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100300 LOGP(DMGCP, LOGL_ERROR,
301 "Sample buffer too small: %d > %d.\n",
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200302 state->sample_cnt + state->src_samples_per_frame,
303 ARRAY_SIZE(state->samples));
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100304 return -ENOSPC;
305 }
306 switch (state->src_fmt) {
307 case AF_GSM:
308 if (gsm_decode(state->src.gsm_handle,
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200309 (gsm_byte *)*src, state->samples + state->sample_cnt) < 0) {
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100310 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 Erlbeck42a833e2014-04-14 10:31:47 +0200317 bcg729Decoder(state->src.g729_dec, *src, 0, state->samples + state->sample_cnt);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100318 break;
319#endif
320 case AF_PCMA:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200321 alaw_decode(*src, state->samples + state->sample_cnt,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100322 state->src_samples_per_frame);
323 break;
324 case AF_S16:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200325 memmove(state->samples + state->sample_cnt, *src,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100326 state->src_frame_size);
327 break;
328 case AF_L16:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200329 l16_decode(*src, state->samples + state->sample_cnt,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100330 state->src_samples_per_frame);
331 break;
332 default:
333 break;
334 }
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200335 *src += state->src_frame_size;
336 *nbytes -= state->src_frame_size;
337 state->sample_cnt += state->src_samples_per_frame;
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100338 }
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200339 return 0;
340}
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100341
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200342static 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 Erlbeck239a8532014-03-13 14:25:51 +0100347 /* Encode samples into dst */
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200348 while (nsamples + state->dst_samples_per_frame <= max_samples) {
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100349 if (nbytes + state->dst_frame_size > buf_size) {
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200350 if (nbytes > 0)
351 break;
352
353 /* Not even one frame fits into the buffer */
354 LOGP(DMGCP, LOGL_INFO,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100355 "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 Erlbeck42a833e2014-04-14 10:31:47 +0200362 state->samples + state->sample_offs, dst);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100363 break;
364#ifdef HAVE_BCG729
365 case AF_G729:
366 bcg729Encoder(state->dst.g729_enc,
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200367 state->samples + state->sample_offs, dst);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100368 break;
369#endif
370 case AF_PCMA:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200371 alaw_encode(state->samples + state->sample_offs, dst,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100372 state->src_samples_per_frame);
373 break;
374 case AF_S16:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200375 memmove(dst, state->samples + state->sample_offs,
376 state->dst_frame_size);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100377 break;
378 case AF_L16:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200379 l16_encode(state->samples + state->sample_offs, dst,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100380 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 Erlbeck42a833e2014-04-14 10:31:47 +0200387 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
394int 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 Erlbeck239a8532014-03-13 14:25:51 +0100418 }
419
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200420 /* 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 Erlbeck239a8532014-03-13 14:25:51 +0100426
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200427 /* TODO: check payload type (-> G.711 comfort noise) */
428
429 if (payload_len > 0) {
430 ts_no = ntohl(*(uint32_t*)(data+4));
Holger Hans Peter Freytherc8b29082014-07-02 22:02:15 +0200431 if (!state->is_running) {
Holger Hans Peter Freyther91eeeae2014-07-04 20:55:20 +0200432 state->next_seq = ntohs(*(uint16_t*)(data+2));
Holger Hans Peter Freytherc8b29082014-07-02 22:02:15 +0200433 state->next_time = ts_no;
434 state->is_running = 1;
435 }
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200436
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200437
438 if (state->sample_cnt > 0) {
439 int32_t delta = ts_no - state->next_time;
440 /* TODO: check sequence? reordering? packet loss? */
441
Holger Hans Peter Freythere52ca9a2014-07-02 21:56:26 +0200442 if (delta > state->sample_cnt) {
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200443 /* There is a time gap between the last packet
444 * and the current one. Just discard the
445 * partial data that is left in the buffer.
446 * TODO: This can be improved by adding silence
447 * instead if the delta is small enough.
448 */
Holger Hans Peter Freythere52ca9a2014-07-02 21:56:26 +0200449 LOGP(DMGCP, LOGL_NOTICE,
450 "0x%x dropping sample buffer due delta=%d sample_cnt=%d\n",
451 ENDPOINT_NUMBER(endp), delta, state->sample_cnt);
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200452 state->sample_cnt = 0;
Holger Hans Peter Freytherb9362782014-07-04 20:55:20 +0200453 state->next_time = ts_no;
Holger Hans Peter Freythere52ca9a2014-07-02 21:56:26 +0200454 } else if (delta < 0) {
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200455 LOGP(DMGCP, LOGL_NOTICE,
456 "RTP time jumps backwards, delta = %d, "
457 "discarding buffered samples\n",
458 delta);
459 state->sample_cnt = 0;
460 state->sample_offs = 0;
461 return -EAGAIN;
462 }
463
464 /* Make sure the samples start without offset */
465 if (state->sample_offs && state->sample_cnt)
466 memmove(&state->samples[0],
467 &state->samples[state->sample_offs],
468 state->sample_cnt *
469 sizeof(state->samples[0]));
470 }
471
472 state->sample_offs = 0;
473
474 /* Append decoded audio to samples */
475 decode_audio(state, &src, &nbytes);
476
477 if (nbytes > 0)
478 LOGP(DMGCP, LOGL_NOTICE,
479 "Skipped audio frame in RTP packet: %d octets\n",
480 nbytes);
481 } else
482 ts_no = state->next_time;
483
484 if (state->sample_cnt < state->dst_packet_duration)
485 return -EAGAIN;
486
487 max_samples =
488 state->dst_packet_duration ?
489 state->dst_packet_duration : state->sample_cnt;
490
491 nsamples = state->sample_cnt;
492
493 rc = encode_audio(state, dst, buf_size, max_samples);
Holger Hans Peter Freytherbd4109b2014-06-27 19:27:38 +0200494 /*
495 * There were no samples to encode?
496 * TODO: how does this work for comfort noise?
497 */
498 if (rc == 0)
499 return -ENOMSG;
500 /* Any other error during the encoding */
501 if (rc < 0)
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200502 return rc;
503
504 nsamples -= state->sample_cnt;
505
506 *len = rtp_hdr_size + rc;
Holger Hans Peter Freyther91eeeae2014-07-04 20:55:20 +0200507 *(uint16_t*)(data+2) = htons(state->next_seq);
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200508 *(uint32_t*)(data+4) = htonl(ts_no);
509
510 state->next_seq += 1;
511 state->next_time = ts_no + nsamples;
512
Holger Hans Peter Freyther77ceaaf2014-06-28 15:05:42 +0200513 /*
514 * XXX: At this point we should always have consumed
515 * samples. So doing OSMO_ASSERT(nsamples > 0) and returning
516 * rtp_hdr_size should be fine.
517 */
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200518 return nsamples ? rtp_hdr_size : 0;
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100519}