Skip to content

Commit

Permalink
Merge pull request meetecho#14 from traud/asterisk-13.3
Browse files Browse the repository at this point in the history
allow iLBC transcoding, fixes meetecho#13
  • Loading branch information
traud committed Nov 26, 2015
2 parents bb2925f + 166b12f commit dc5ff5f
Showing 1 changed file with 46 additions and 29 deletions.
75 changes: 46 additions & 29 deletions codecs/codec_opus.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision: $")
#include "asterisk/cli.h"
#include "asterisk/config.h"
#include "asterisk/utils.h"
#include "asterisk/linkedlists.h"

#define BUFFER_SAMPLES 8000
#define OPUS_SAMPLES 160
Expand Down Expand Up @@ -183,41 +184,57 @@ static int lintoopus_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
static struct ast_frame *lintoopus_frameout(struct ast_trans_pvt *pvt)
{
struct opus_coder_pvt *opvt = pvt->pvt;
int datalen = 0; /* output bytes */
int samples = 0; /* output samples */

/* We can't work on anything less than a frame in size */
if (pvt->samples < opvt->framesize) {
return NULL;
}

/* Encode 160 samples (or more if it's not narrowband) */
ast_debug(3, "[Encoder #%d (%d)] %d samples, %d bytes\n",
opvt->id,
opvt->sampling_rate,
opvt->framesize,
opvt->framesize * 2);

if ((datalen = opus_encode(opvt->opus, opvt->buf, opvt->framesize, pvt->outbuf.uc, BUFFER_SAMPLES)) < 0) {
ast_log(LOG_ERROR, "Error encoding the Opus frame: %s\n", opus_strerror(datalen));
return NULL;
struct ast_frame *result = NULL;
struct ast_frame *last = NULL;
int samples = 0; /* output samples */

while (pvt->samples >= opvt->framesize) {
/* status is either error or output bytes */
const int status = opus_encode(opvt->opus,
opvt->buf + samples,
opvt->framesize,
pvt->outbuf.uc,
BUFFER_SAMPLES);

ast_debug(3, "[Encoder #%d (%d)] %d samples, %d bytes\n",
opvt->id,
opvt->sampling_rate,
opvt->framesize,
opvt->framesize * 2);

samples += opvt->framesize;
pvt->samples -= opvt->framesize;

if (status < 0) {
ast_log(LOG_ERROR, "Error encoding the Opus frame: %s\n", opus_strerror(status));
} else {
struct ast_frame *current = ast_trans_frameout(pvt,
status,
opvt->multiplier * opvt->framesize);

ast_debug(3, "[Encoder #%d (%d)] >> Got %d samples, %d bytes\n",
opvt->id,
opvt->sampling_rate,
opvt->multiplier * opvt->framesize,
status);

if (!current) {
continue;
} else if (last) {
AST_LIST_NEXT(last, frame_list) = current;
} else {
result = current;
}
last = current;
}
}

samples += opvt->framesize;
pvt->samples -= opvt->framesize;

/* Move the data at the end of the buffer to the front */
if (pvt->samples) {
if (samples) {
memmove(opvt->buf, opvt->buf + samples, pvt->samples * 2);
}

ast_debug(3, "[Encoder #%d (%d)] >> Got %d samples, %d bytes\n",
opvt->id,
opvt->sampling_rate,
opvt->multiplier * samples,
datalen);

return ast_trans_frameout(pvt, datalen, opvt->multiplier * samples);
return result;
}

static int opustolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
Expand Down

0 comments on commit dc5ff5f

Please sign in to comment.