blob: e961ba6ccdea5ce8df2845a776722402f079a700 [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
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +020048static enum audio_format get_audio_format(const struct mgcp_rtp_codec *codec)
Jacob Erlbeck239a8532014-03-13 14:25:51 +010049{
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +020050 if (codec->subtype_name) {
51 if (!strcmp("GSM", codec->subtype_name))
Jacob Erlbeck239a8532014-03-13 14:25:51 +010052 return AF_GSM;
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +020053 if (!strcmp("PCMA", codec->subtype_name))
Jacob Erlbeck239a8532014-03-13 14:25:51 +010054 return AF_PCMA;
55#ifdef HAVE_BCG729
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +020056 if (!strcmp("G729", codec->subtype_name))
Jacob Erlbeck239a8532014-03-13 14:25:51 +010057 return AF_G729;
58#endif
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +020059 if (!strcmp("L16", codec->subtype_name))
Jacob Erlbeck239a8532014-03-13 14:25:51 +010060 return AF_L16;
61 }
62
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +020063 switch (codec->payload_type) {
Jacob Erlbeck239a8532014-03-13 14:25:51 +010064 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;
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +0200144 const struct mgcp_rtp_codec *src_codec = &src_end->codec;
145 const struct mgcp_rtp_codec *dst_codec = &dst_end->codec;
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100146
147 /* cleanup first */
148 if (dst_end->rtp_process_data) {
149 talloc_free(dst_end->rtp_process_data);
150 dst_end->rtp_process_data = NULL;
151 }
152
153 if (!src_end)
154 return 0;
155
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +0200156 src_fmt = get_audio_format(src_codec);
157 dst_fmt = get_audio_format(dst_codec);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100158
159 LOGP(DMGCP, LOGL_ERROR,
160 "Checking transcoding: %s (%d) -> %s (%d)\n",
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +0200161 src_codec->subtype_name, src_codec->payload_type,
162 dst_codec->subtype_name, dst_codec->payload_type);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100163
164 if (src_fmt == AF_INVALID || dst_fmt == AF_INVALID) {
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +0200165 if (!src_codec->subtype_name || !dst_codec->subtype_name)
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100166 /* Not enough info, do nothing */
167 return 0;
168
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +0200169 if (strcmp(src_codec->subtype_name, dst_codec->subtype_name) == 0)
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100170 /* Nothing to do */
171 return 0;
172
173 LOGP(DMGCP, LOGL_ERROR,
174 "Cannot transcode: %s codec not supported (%s -> %s).\n",
175 src_fmt != AF_INVALID ? "destination" : "source",
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +0200176 src_codec->audio_name, dst_codec->audio_name);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100177 return -EINVAL;
178 }
179
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +0200180 if (src_codec->rate && dst_codec->rate && src_codec->rate != dst_codec->rate) {
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100181 LOGP(DMGCP, LOGL_ERROR,
182 "Cannot transcode: rate conversion (%d -> %d) not supported.\n",
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +0200183 src_codec->rate, dst_codec->rate);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100184 return -EINVAL;
185 }
186
187 state = talloc_zero(endp->tcfg->cfg, struct mgcp_process_rtp_state);
188 talloc_set_destructor(state, processing_state_destructor);
189 dst_end->rtp_process_data = state;
190
191 state->src_fmt = src_fmt;
192
193 switch (state->src_fmt) {
194 case AF_L16:
195 case AF_S16:
196 state->src_frame_size = 80 * sizeof(short);
197 state->src_samples_per_frame = 80;
198 break;
199 case AF_GSM:
200 state->src_frame_size = sizeof(gsm_frame);
201 state->src_samples_per_frame = 160;
202 state->src.gsm_handle = gsm_create();
203 if (!state->src.gsm_handle) {
204 LOGP(DMGCP, LOGL_ERROR,
205 "Failed to initialize GSM decoder.\n");
206 return -EINVAL;
207 }
208 break;
209#ifdef HAVE_BCG729
210 case AF_G729:
211 state->src_frame_size = 10;
212 state->src_samples_per_frame = 80;
213 state->src.g729_dec = initBcg729DecoderChannel();
214 if (!state->src.g729_dec) {
215 LOGP(DMGCP, LOGL_ERROR,
216 "Failed to initialize G.729 decoder.\n");
217 return -EINVAL;
218 }
219 break;
220#endif
221 case AF_PCMA:
222 state->src_frame_size = 80;
223 state->src_samples_per_frame = 80;
224 break;
225 default:
226 break;
227 }
228
229 state->dst_fmt = dst_fmt;
230
231 switch (state->dst_fmt) {
232 case AF_L16:
233 case AF_S16:
234 state->dst_frame_size = 80*sizeof(short);
235 state->dst_samples_per_frame = 80;
236 break;
237 case AF_GSM:
238 state->dst_frame_size = sizeof(gsm_frame);
239 state->dst_samples_per_frame = 160;
240 state->dst.gsm_handle = gsm_create();
241 if (!state->dst.gsm_handle) {
242 LOGP(DMGCP, LOGL_ERROR,
243 "Failed to initialize GSM encoder.\n");
244 return -EINVAL;
245 }
246 break;
247#ifdef HAVE_BCG729
248 case AF_G729:
249 state->dst_frame_size = 10;
250 state->dst_samples_per_frame = 80;
251 state->dst.g729_enc = initBcg729EncoderChannel();
252 if (!state->dst.g729_enc) {
253 LOGP(DMGCP, LOGL_ERROR,
254 "Failed to initialize G.729 decoder.\n");
255 return -EINVAL;
256 }
257 break;
258#endif
259 case AF_PCMA:
260 state->dst_frame_size = 80;
261 state->dst_samples_per_frame = 80;
262 break;
263 default:
264 break;
265 }
266
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200267 if (dst_end->force_output_ptime)
268 state->dst_packet_duration = mgcp_rtp_packet_duration(endp, dst_end);
269
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100270 LOGP(DMGCP, LOGL_INFO,
271 "Initialized RTP processing on: 0x%x "
272 "conv: %d (%d, %d, %s) -> %d (%d, %d, %s)\n",
273 ENDPOINT_NUMBER(endp),
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +0200274 src_fmt, src_codec->payload_type, src_codec->rate, src_end->fmtp_extra,
275 dst_fmt, dst_codec->payload_type, dst_codec->rate, dst_end->fmtp_extra);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100276
277 return 0;
278}
279
280void mgcp_transcoding_net_downlink_format(struct mgcp_endpoint *endp,
281 int *payload_type,
282 const char**audio_name,
283 const char**fmtp_extra)
284{
285 struct mgcp_process_rtp_state *state = endp->net_end.rtp_process_data;
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +0200286 struct mgcp_rtp_codec *net_codec = &endp->net_end.codec;
287 struct mgcp_rtp_codec *bts_codec = &endp->bts_end.codec;
288
289 if (!state || net_codec->payload_type < 0) {
290 *payload_type = bts_codec->payload_type;
291 *audio_name = bts_codec->audio_name;
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100292 *fmtp_extra = endp->bts_end.fmtp_extra;
293 return;
294 }
295
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +0200296 *payload_type = net_codec->payload_type;
297 *audio_name = net_codec->audio_name;
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100298 *fmtp_extra = endp->net_end.fmtp_extra;
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100299}
300
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200301static int decode_audio(struct mgcp_process_rtp_state *state,
302 uint8_t **src, size_t *nbytes)
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100303{
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200304 while (*nbytes >= state->src_frame_size) {
305 if (state->sample_cnt + state->src_samples_per_frame > ARRAY_SIZE(state->samples)) {
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100306 LOGP(DMGCP, LOGL_ERROR,
307 "Sample buffer too small: %d > %d.\n",
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200308 state->sample_cnt + state->src_samples_per_frame,
309 ARRAY_SIZE(state->samples));
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100310 return -ENOSPC;
311 }
312 switch (state->src_fmt) {
313 case AF_GSM:
314 if (gsm_decode(state->src.gsm_handle,
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200315 (gsm_byte *)*src, state->samples + state->sample_cnt) < 0) {
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100316 LOGP(DMGCP, LOGL_ERROR,
317 "Failed to decode GSM.\n");
318 return -EINVAL;
319 }
320 break;
321#ifdef HAVE_BCG729
322 case AF_G729:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200323 bcg729Decoder(state->src.g729_dec, *src, 0, state->samples + state->sample_cnt);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100324 break;
325#endif
326 case AF_PCMA:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200327 alaw_decode(*src, state->samples + state->sample_cnt,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100328 state->src_samples_per_frame);
329 break;
330 case AF_S16:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200331 memmove(state->samples + state->sample_cnt, *src,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100332 state->src_frame_size);
333 break;
334 case AF_L16:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200335 l16_decode(*src, state->samples + state->sample_cnt,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100336 state->src_samples_per_frame);
337 break;
338 default:
339 break;
340 }
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200341 *src += state->src_frame_size;
342 *nbytes -= state->src_frame_size;
343 state->sample_cnt += state->src_samples_per_frame;
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100344 }
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200345 return 0;
346}
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100347
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200348static int encode_audio(struct mgcp_process_rtp_state *state,
349 uint8_t *dst, size_t buf_size, size_t max_samples)
350{
351 int nbytes = 0;
352 size_t nsamples = 0;
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100353 /* Encode samples into dst */
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200354 while (nsamples + state->dst_samples_per_frame <= max_samples) {
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100355 if (nbytes + state->dst_frame_size > buf_size) {
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200356 if (nbytes > 0)
357 break;
358
359 /* Not even one frame fits into the buffer */
360 LOGP(DMGCP, LOGL_INFO,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100361 "Encoding (RTP) buffer too small: %d > %d.\n",
362 nbytes + state->dst_frame_size, buf_size);
363 return -ENOSPC;
364 }
365 switch (state->dst_fmt) {
366 case AF_GSM:
367 gsm_encode(state->dst.gsm_handle,
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200368 state->samples + state->sample_offs, dst);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100369 break;
370#ifdef HAVE_BCG729
371 case AF_G729:
372 bcg729Encoder(state->dst.g729_enc,
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200373 state->samples + state->sample_offs, dst);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100374 break;
375#endif
376 case AF_PCMA:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200377 alaw_encode(state->samples + state->sample_offs, dst,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100378 state->src_samples_per_frame);
379 break;
380 case AF_S16:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200381 memmove(dst, state->samples + state->sample_offs,
382 state->dst_frame_size);
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100383 break;
384 case AF_L16:
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200385 l16_encode(state->samples + state->sample_offs, dst,
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100386 state->src_samples_per_frame);
387 break;
388 default:
389 break;
390 }
391 dst += state->dst_frame_size;
392 nbytes += state->dst_frame_size;
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200393 state->sample_offs += state->dst_samples_per_frame;
394 nsamples += state->dst_samples_per_frame;
395 }
396 state->sample_cnt -= nsamples;
397 return nbytes;
398}
399
400int mgcp_transcoding_process_rtp(struct mgcp_endpoint *endp,
401 struct mgcp_rtp_end *dst_end,
402 char *data, int *len, int buf_size)
403{
404 struct mgcp_process_rtp_state *state = dst_end->rtp_process_data;
Holger Hans Peter Freyther3713f782014-09-01 11:02:05 +0200405 const size_t rtp_hdr_size = sizeof(struct rtp_hdr);
406 struct rtp_hdr *rtp_hdr = (struct rtp_hdr *) data;
407 char *payload_data = (char *) &rtp_hdr->data[0];
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200408 int payload_len = *len - rtp_hdr_size;
409 uint8_t *src = (uint8_t *)payload_data;
410 uint8_t *dst = (uint8_t *)payload_data;
411 size_t nbytes = payload_len;
412 size_t nsamples;
413 size_t max_samples;
414 uint32_t ts_no;
415 int rc;
416
417 if (!state)
418 return 0;
419
420 if (state->src_fmt == state->dst_fmt) {
421 if (!state->dst_packet_duration)
422 return 0;
423
424 /* TODO: repackage without transcoding */
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100425 }
426
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200427 /* If the remaining samples do not fit into a fixed ptime,
428 * a) discard them, if the next packet is much later
429 * b) add silence and * send it, if the current packet is not
430 * yet too late
431 * c) append the sample data, if the timestamp matches exactly
432 */
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100433
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200434 /* TODO: check payload type (-> G.711 comfort noise) */
435
436 if (payload_len > 0) {
Holger Hans Peter Freyther3713f782014-09-01 11:02:05 +0200437 ts_no = ntohl(rtp_hdr->timestamp);
Holger Hans Peter Freytherc8b29082014-07-02 22:02:15 +0200438 if (!state->is_running) {
Holger Hans Peter Freyther3713f782014-09-01 11:02:05 +0200439 state->next_seq = ntohs(rtp_hdr->sequence);
Holger Hans Peter Freytherc8b29082014-07-02 22:02:15 +0200440 state->next_time = ts_no;
441 state->is_running = 1;
442 }
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200443
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200444
445 if (state->sample_cnt > 0) {
446 int32_t delta = ts_no - state->next_time;
447 /* TODO: check sequence? reordering? packet loss? */
448
Holger Hans Peter Freythere52ca9a2014-07-02 21:56:26 +0200449 if (delta > state->sample_cnt) {
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200450 /* There is a time gap between the last packet
451 * and the current one. Just discard the
452 * partial data that is left in the buffer.
453 * TODO: This can be improved by adding silence
454 * instead if the delta is small enough.
455 */
Holger Hans Peter Freythere52ca9a2014-07-02 21:56:26 +0200456 LOGP(DMGCP, LOGL_NOTICE,
457 "0x%x dropping sample buffer due delta=%d sample_cnt=%d\n",
458 ENDPOINT_NUMBER(endp), delta, state->sample_cnt);
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200459 state->sample_cnt = 0;
Holger Hans Peter Freytherb9362782014-07-04 20:55:20 +0200460 state->next_time = ts_no;
Holger Hans Peter Freythere52ca9a2014-07-02 21:56:26 +0200461 } else if (delta < 0) {
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200462 LOGP(DMGCP, LOGL_NOTICE,
463 "RTP time jumps backwards, delta = %d, "
464 "discarding buffered samples\n",
465 delta);
466 state->sample_cnt = 0;
467 state->sample_offs = 0;
468 return -EAGAIN;
469 }
470
471 /* Make sure the samples start without offset */
472 if (state->sample_offs && state->sample_cnt)
473 memmove(&state->samples[0],
474 &state->samples[state->sample_offs],
475 state->sample_cnt *
476 sizeof(state->samples[0]));
477 }
478
479 state->sample_offs = 0;
480
481 /* Append decoded audio to samples */
482 decode_audio(state, &src, &nbytes);
483
484 if (nbytes > 0)
485 LOGP(DMGCP, LOGL_NOTICE,
486 "Skipped audio frame in RTP packet: %d octets\n",
487 nbytes);
488 } else
489 ts_no = state->next_time;
490
491 if (state->sample_cnt < state->dst_packet_duration)
492 return -EAGAIN;
493
494 max_samples =
495 state->dst_packet_duration ?
496 state->dst_packet_duration : state->sample_cnt;
497
498 nsamples = state->sample_cnt;
499
500 rc = encode_audio(state, dst, buf_size, max_samples);
Holger Hans Peter Freytherbd4109b2014-06-27 19:27:38 +0200501 /*
502 * There were no samples to encode?
503 * TODO: how does this work for comfort noise?
504 */
505 if (rc == 0)
506 return -ENOMSG;
507 /* Any other error during the encoding */
508 if (rc < 0)
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200509 return rc;
510
511 nsamples -= state->sample_cnt;
512
513 *len = rtp_hdr_size + rc;
Holger Hans Peter Freyther3713f782014-09-01 11:02:05 +0200514 rtp_hdr->sequence = htons(state->next_seq);
515 rtp_hdr->timestamp = htonl(ts_no);
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200516
517 state->next_seq += 1;
518 state->next_time = ts_no + nsamples;
519
Holger Hans Peter Freyther77ceaaf2014-06-28 15:05:42 +0200520 /*
521 * XXX: At this point we should always have consumed
522 * samples. So doing OSMO_ASSERT(nsamples > 0) and returning
523 * rtp_hdr_size should be fine.
524 */
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200525 return nsamples ? rtp_hdr_size : 0;
Jacob Erlbeck239a8532014-03-13 14:25:51 +0100526}