[svn-commits] sruffell: branch sruffell/asterisk-1.4-transcoder r160891 - in /team/sruffell...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Wed Dec 3 20:02:22 CST 2008


Author: sruffell
Date: Wed Dec  3 20:02:21 2008
New Revision: 160891

URL: http://svn.digium.com/view/asterisk?view=rev&rev=160891
Log:
- Splitting up the encoders from the decoders to simplify some of the logic in
  the framein and frameout routines.

- Allow 20ms buffers to be coalesced in order to provide 30ms buffers to the
  hardware when encoding into G723.


Modified:
    team/sruffell/asterisk-1.4-transcoder/codecs/codec_dahdi.c
    team/sruffell/asterisk-1.4-transcoder/main/translate.c

Modified: team/sruffell/asterisk-1.4-transcoder/codecs/codec_dahdi.c
URL: http://svn.digium.com/view/asterisk/team/sruffell/asterisk-1.4-transcoder/codecs/codec_dahdi.c?view=diff&rev=160891&r1=160890&r2=160891
==============================================================================
--- team/sruffell/asterisk-1.4-transcoder/codecs/codec_dahdi.c (original)
+++ team/sruffell/asterisk-1.4-transcoder/codecs/codec_dahdi.c Wed Dec  3 20:02:21 2008
@@ -44,6 +44,7 @@
 #include <sys/ioctl.h>
 #include <errno.h>
 #include <sys/mman.h>
+#include <sys/poll.h>
 
 #include "asterisk/lock.h"
 #include "asterisk/translate.h"
@@ -59,7 +60,10 @@
 #include "asterisk/frame.h"
 #include "asterisk/ulaw.h"
 
-#define BUFFER_SAMPLES	8000
+#define BUFFER_SIZE 8000
+
+#define G723_SAMPLES 240
+#define G729_SAMPLES 160
 
 static unsigned int global_useplc = 0;
 
@@ -107,43 +111,50 @@
 
 static AST_LIST_HEAD_STATIC(translators, translator);
 
-struct pvt {
+struct codec_dahdi_pvt {
 	int fd;
-	int fake;
-	int samples;
 	struct dahdi_transcoder_formats fmts;
-	enum {NONE=0, SLINTOULAW, ULAWTOSLIN} soft_slin;
-	uint8_t ulaw_buffer[160];
+	unsigned int softslin:1;
+	unsigned int fake:2;
+	uint16_t required_samples;
+	uint16_t samples_in_buffer;
+	uint8_t ulaw_buffer[1024];
 };
 
+/* Only used by a decoder */
 static int ulawtolin(struct ast_trans_pvt *pvt)
 {
-	struct pvt *ztp = pvt->pvt;
-	int i = ztp->samples;
+	struct codec_dahdi_pvt *ztp = pvt->pvt;
+	int i = ztp->required_samples;
 	uint8_t *src = &ztp->ulaw_buffer[0];
 	int16_t *dst = (int16_t *)pvt->outbuf + pvt->datalen;
 
 	/* convert and copy in outbuf */
-	while (i--)
+	while (i--) {
 		*dst++ = AST_MULAW(*src++);
+	}
 
 	return 0;
 }
 
+/* Only used by an encoder. */
 static int lintoulaw(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
-	struct pvt *ztp = pvt->pvt;
+	struct codec_dahdi_pvt *ztp = pvt->pvt;
 	int i = f->samples;
-	uint8_t *dst = &ztp->ulaw_buffer[0];
+	uint8_t *dst = &ztp->ulaw_buffer[ztp->samples_in_buffer];
 	int16_t *src = (int16_t*)f->data;
 
-	if (i > sizeof(ztp->ulaw_buffer)) {
-		ast_log(LOG_ERROR, "Cannot convert more that %d samples.\n", ztp->samples);
-		return 0;
-	}
-	while (i--)
+	if (ztp->samples_in_buffer + i > sizeof(ztp->ulaw_buffer)) {
+		ast_log(LOG_ERROR, "Out of buffer space!\n");
+		return -i;
+	}
+
+	while (i--) {
 		*dst++ = AST_LIN2MU(*src++);
-
+	}
+
+	ztp->samples_in_buffer += f->samples;
 	return 0;
 }
 
@@ -161,89 +172,79 @@
 	return RESULT_SUCCESS;
 }
 
-static int zap_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
+static void dahdi_write_frame(struct codec_dahdi_pvt *ztp, const uint8_t *buffer, const ssize_t count)
 {
 	int res;
-	struct pvt *ztp = pvt->pvt;
-
-	if (f->subclass) {
-		int write_size;
-		/* If we received a frame in signed linear format, and the
-		 * hardware doesn't support signed linear format, we need to
-		 * convert it before sending it to the hardware. */
-		if (SLINTOULAW == ztp->soft_slin) {
-			lintoulaw(pvt, f);
-			write_size = sizeof(ztp->ulaw_buffer);
-			res = write(ztp->fd, ztp->ulaw_buffer, sizeof(ztp->ulaw_buffer)); 
-		} else {
-			write_size = f->datalen;
-			res = write(ztp->fd, f->data, f->datalen); 
-		}
-		if (option_verbose > 3) {
-			if (-1 == res) {
-				ast_log(LOG_ERROR, "Failed to write to transcoder: %s\n", strerror(errno));
-			} 
-			if (write_size != res) {
-				ast_log(LOG_ERROR, "Requested write of %d bytes, but only wrote %d bytes.\n", f->datalen, res);
-			}
-		}
-		res = -1;
-		pvt->samples += f->samples;
-	} else {
-		/* Fake a return frame for calculation purposes */
+	struct pollfd p = {0};
+	if (!count) return;
+	res = write(ztp->fd, buffer, count); 
+	if (option_verbose > 3) {
+		if (-1 == res) {
+			ast_log(LOG_ERROR, "Failed to write to transcoder: %s\n", strerror(errno));
+		} 
+		if (count != res) {
+			ast_log(LOG_ERROR, "Requested write of %zd bytes, but only wrote %d bytes.\n", count, res);
+		}
+	}
+	p.fd = ztp->fd;
+	p.events = POLLOUT;
+	res = poll(&p, 1, 50);
+}
+
+static int dahdi_encoder_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
+{
+	struct codec_dahdi_pvt *ztp = pvt->pvt;
+
+	if (!f->subclass) {
+		/* We're just faking a return for calculation purposes. */
 		ztp->fake = 2;
 		pvt->samples = f->samples;
-		res = 0;
-	}
-	return res;
-}
-
-static struct ast_frame *zap_frameout(struct ast_trans_pvt *pvt)
-{
-	struct pvt *ztp = pvt->pvt;
-
-	if (0 == ztp->fake) {
-		int res;
-		/* Let's check to see if there is a new frame for us.... */
-		if (ULAWTOSLIN == ztp->soft_slin) {
-			res = read(ztp->fd, ztp->ulaw_buffer, sizeof(ztp->ulaw_buffer));
-		} else {
-			res = read(ztp->fd, pvt->outbuf + pvt->datalen, pvt->t->buf_size - pvt->datalen);
-		}
-		if (-1 == res) {
-			if (EWOULDBLOCK == errno) {
-				/* Nothing waiting... */
-				return NULL;
-			} else {
-				ast_log(LOG_ERROR, "Failed to read from transcoder: %s\n", strerror(errno));
-				return NULL;
-			}
-		} else {
-			if (ULAWTOSLIN == ztp->soft_slin) {
-				ulawtolin(pvt);
-				pvt->f.datalen = res * 2;
-			} else {
-				pvt->f.datalen = res;
-			}
-			pvt->f.samples = ztp->samples;
-			pvt->datalen = 0;
-			pvt->f.frametype = AST_FRAME_VOICE;
-			pvt->f.subclass = 1 <<  (pvt->t->dstfmt);
-			pvt->f.mallocd = 0;
-			pvt->f.offset = AST_FRIENDLY_OFFSET;
-			pvt->f.src = pvt->t->name;
-			pvt->f.data = pvt->outbuf;
-			ast_set_flag(&pvt->f, AST_FRFLAG_FROM_TRANSLATOR);
-
-			return &pvt->f;
-		}
-
-	} else if (2 == ztp->fake) {
-
+		return 0;
+	}
+
+	/* Buffer up the packets and send them to the hardware if we
+	 * have enough samples set up. */
+	if (ztp->softslin) {
+		if (lintoulaw(pvt, f)) {
+			 return -1;
+		}
+	} else {
+		/* NOTE:  If softslin support is not needed, and the sample
+		 * size is equal to the required sample size, we wouldn't
+		 * need this copy operation.  But at the time this was
+		 * written, only softslin is supported. */
+		if (ztp->samples_in_buffer + f->samples > sizeof(ztp->ulaw_buffer)) {
+			ast_log(LOG_ERROR, "Out of buffer space.\n");
+			return -1;
+		}
+		memcpy(&ztp->ulaw_buffer[ztp->samples_in_buffer], f->data, f->samples);
+		ztp->samples_in_buffer += f->samples;
+	}
+
+	while (ztp->samples_in_buffer > ztp->required_samples) {
+		dahdi_write_frame(ztp, ztp->ulaw_buffer, ztp->required_samples);
+		ztp->samples_in_buffer -= ztp->required_samples;
+		if (ztp->samples_in_buffer) {
+			/* Shift any remaining bytes down. */
+			memmove(ztp->ulaw_buffer, &ztp->ulaw_buffer[ztp->required_samples],
+				ztp->samples_in_buffer);
+		}
+	}
+	pvt->samples = 0;
+	pvt->datalen = 0;
+	return -1;
+}
+
+static struct ast_frame *dahdi_encoder_frameout(struct ast_trans_pvt *pvt)
+{
+	struct codec_dahdi_pvt *ztp = pvt->pvt;
+	int res;
+
+	if (2 == ztp->fake) {
 		ztp->fake = 1;
 		pvt->f.frametype = AST_FRAME_VOICE;
 		pvt->f.subclass = 0;
-		pvt->f.samples = 160;
+		pvt->f.samples = ztp->required_samples;
 		pvt->f.data = NULL;
 		pvt->f.offset = 0;
 		pvt->f.datalen = 0;
@@ -254,17 +255,129 @@
 		return &pvt->f;
 
 	} else if (1 == ztp->fake) {
-
+		ztp->fake = 0;
 		return NULL;
-
-	}
+	}
+
+	res = read(ztp->fd, pvt->outbuf + pvt->datalen, pvt->t->buf_size - pvt->datalen);
+	if (-1 == res) {
+		if (EWOULDBLOCK == errno) {
+			/* Nothing waiting... */
+			return NULL;
+		} else {
+			ast_log(LOG_ERROR, "Failed to read from transcoder: %s\n", strerror(errno));
+			return NULL;
+		}
+	} else {
+		pvt->f.datalen = res;
+		pvt->f.samples = ztp->required_samples;
+		pvt->f.frametype = AST_FRAME_VOICE;
+		pvt->f.subclass = 1 <<  (pvt->t->dstfmt);
+		pvt->f.mallocd = 0;
+		pvt->f.offset = AST_FRIENDLY_OFFSET;
+		pvt->f.src = pvt->t->name;
+		pvt->f.data = pvt->outbuf;
+		ast_set_flag(&pvt->f, AST_FRFLAG_FROM_TRANSLATOR);
+
+		pvt->samples = ztp->required_samples;
+		pvt->datalen = 0;
+		return &pvt->f;
+	}
+
 	/* Shouldn't get here... */
 	return NULL;
 }
 
-static void zap_destroy(struct ast_trans_pvt *pvt)
-{
-	struct pvt *ztp = pvt->pvt;
+static int dahdi_decoder_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
+{
+	struct codec_dahdi_pvt *ztp = pvt->pvt;
+
+	if (pvt->samples) ast_log(LOG_WARNING, " -- %d\n", pvt->samples);
+	if (!f->subclass) {
+		/* We're just faking a return for calculation purposes. */
+		ztp->fake = 2;
+		pvt->samples = f->samples;
+		return 0;
+	}
+
+	if (!f->datalen) {
+		if (f->samples != ztp->required_samples) {
+			ast_log(LOG_ERROR, "%d != %d %d\n", f->samples, ztp->required_samples, f->datalen);
+		}
+	}
+	dahdi_write_frame(ztp, f->data, f->datalen);
+	pvt->samples = 0;
+	pvt->datalen = 0;
+	return -1;
+}
+
+static struct ast_frame *dahdi_decoder_frameout(struct ast_trans_pvt *pvt)
+{
+	int res;
+	struct codec_dahdi_pvt *ztp = pvt->pvt;
+
+	if (2 == ztp->fake) {
+		ztp->fake = 1;
+		pvt->f.frametype = AST_FRAME_VOICE;
+		pvt->f.subclass = 0;
+		pvt->f.samples = ztp->required_samples;
+		pvt->f.data = NULL;
+		pvt->f.offset = 0;
+		pvt->f.datalen = 0;
+		pvt->f.mallocd = 0;
+		ast_set_flag(&pvt->f, AST_FRFLAG_FROM_TRANSLATOR);
+		pvt->samples = 0;
+		return &pvt->f;
+	} else if (1 == ztp->fake) {
+		pvt->samples = 0;
+		ztp->fake = 0;
+		return NULL;
+	}
+
+	/* Let's check to see if there is a new frame for us.... */
+	if (ztp->softslin) {
+		res = read(ztp->fd, ztp->ulaw_buffer, sizeof(ztp->ulaw_buffer));
+	} else {
+		res = read(ztp->fd, pvt->outbuf + pvt->datalen, pvt->t->buf_size - pvt->datalen);
+	}
+
+	if (-1 == res) {
+		if (EWOULDBLOCK == errno) {
+			/* Nothing waiting... */
+			return NULL;
+		} else {
+			ast_log(LOG_ERROR, "Failed to read from transcoder: %s\n", strerror(errno));
+			return NULL;
+		}
+	} else {
+		if (ztp->softslin) {
+			ulawtolin(pvt);
+			pvt->f.datalen = res * 2;
+		} else {
+			pvt->f.datalen = res;
+		}
+		pvt->datalen = 0;
+		pvt->f.frametype = AST_FRAME_VOICE;
+		pvt->f.subclass = 1 <<  (pvt->t->dstfmt);
+		pvt->f.mallocd = 0;
+		pvt->f.offset = AST_FRIENDLY_OFFSET;
+		pvt->f.src = pvt->t->name;
+		pvt->f.data = pvt->outbuf;
+		pvt->f.samples = ztp->required_samples;
+		ast_set_flag(&pvt->f, AST_FRFLAG_FROM_TRANSLATOR);
+		pvt->samples = ztp->required_samples;
+
+		return &pvt->f;
+	}
+
+	/* Shouldn't get here... */
+	return NULL;
+}
+
+
+static void dahdi_destroy(struct ast_trans_pvt *pvt)
+{
+	struct codec_dahdi_pvt *ztp = pvt->pvt;
 
 	switch (ztp->fmts.dstfmt) {
 	case AST_FORMAT_G729A:
@@ -279,25 +392,23 @@
 	close(ztp->fd);
 }
 
-static int zap_translate(struct ast_trans_pvt *pvt, int dest, int source)
+static int dahdi_translate(struct ast_trans_pvt *pvt, int dest, int source)
 {
 	/* Request translation through zap if possible */
 	int fd;
-	struct pvt *ztp = pvt->pvt;
+	struct codec_dahdi_pvt *ztp = pvt->pvt;
 	int flags;
 	int tried_once = 0;
+#ifdef HAVE_ZAPTEL
+	const char *dev_filename = "/dev/zap/transcode";
+#else
+	const char *dev_filename = "/dev/dahdi/transcode";
+#endif
 	
-#ifdef HAVE_ZAPTEL
-	if ((fd = open("/dev/zap/transcode", O_RDWR)) < 0) {
-		ast_log(LOG_ERROR, "Failed to open /dev/zap/transcode: %s\n", strerror(errno));
+	if ((fd = open(dev_filename, O_RDWR)) < 0) {
+		ast_log(LOG_ERROR, "Failed to open %s: %s\n", dev_filename, strerror(errno));
 		return -1;
 	}
-#else
-	if ((fd = open("/dev/dahdi/transcode", O_RDWR)) < 0) {
-		ast_log(LOG_ERROR, "Failed to open /dev/dahdi/transcode: %s\n", strerror(errno));
-		return -1;
-	}
-#endif
 	
 	ztp->fmts.srcfmt = (1 << source);
 	ztp->fmts.dstfmt = (1 << dest);
@@ -317,11 +428,11 @@
 			 * software. */
 			if (AST_FORMAT_SLINEAR == ztp->fmts.srcfmt) {
 				ast_log(LOG_DEBUG, "Using soft_slin support on source\n");
-				ztp->soft_slin = SLINTOULAW;
+				ztp->softslin = 1;
 				ztp->fmts.srcfmt = AST_FORMAT_ULAW;
 			} else if (AST_FORMAT_SLINEAR == ztp->fmts.dstfmt) {
 				ast_log(LOG_DEBUG, "Using soft_slin support on destination\n");
-				ztp->soft_slin = ULAWTOSLIN;
+				ztp->softslin = 1;
 				ztp->fmts.dstfmt = AST_FORMAT_ULAW;
 			}
 			tried_once = 1;
@@ -341,17 +452,7 @@
 
 	ztp->fd = fd;
 
-	switch (ztp->fmts.dstfmt) {
-	case AST_FORMAT_G729A:
-		ztp->samples = 160;
-		break;
-	case AST_FORMAT_G723_1:
-		ztp->samples = 240;
-		break;
-	default:
-		ztp->samples = 160;
-		break;
-	};
+	ztp->required_samples = ((ztp->fmts.dstfmt|ztp->fmts.srcfmt)&AST_FORMAT_G723_1) ? G723_SAMPLES : G729_SAMPLES;
 
 	switch (ztp->fmts.dstfmt) {
 	case AST_FORMAT_G729A:
@@ -368,9 +469,9 @@
 	return 0;
 }
 
-static int zap_new(struct ast_trans_pvt *pvt)
-{
-	return zap_translate(pvt, pvt->t->dstfmt, pvt->t->srcfmt);
+static int dahdi_new(struct ast_trans_pvt *pvt)
+{
+	return dahdi_translate(pvt, pvt->t->dstfmt, pvt->t->srcfmt);
 }
 
 static struct ast_frame *fakesrc_sample(void)
@@ -385,26 +486,49 @@
 	return &f;
 }
 
+static int is_encoder(struct translator *zt)
+{
+	if (zt->t.srcfmt&(AST_FORMAT_ULAW|AST_FORMAT_ALAW|AST_FORMAT_SLINEAR)) {
+		return 1;
+	} else {
+		return 0;
+	}
+}
+
 static int register_translator(int dst, int src)
 {
 	struct translator *zt;
 	int res;
 
-	if (!(zt = ast_calloc(1, sizeof(*zt))))
+	if (!(zt = ast_calloc(1, sizeof(*zt)))) {
 		return -1;
+	}
 
 	snprintf((char *) (zt->t.name), sizeof(zt->t.name), "zap%sto%s", 
 		 ast_getformatname((1 << src)), ast_getformatname((1 << dst)));
 	zt->t.srcfmt = (1 << src);
 	zt->t.dstfmt = (1 << dst);
-	zt->t.newpvt = zap_new;
-	zt->t.framein = zap_framein;
-	zt->t.frameout = zap_frameout;
-	zt->t.destroy = zap_destroy;
+	zt->t.buf_size = BUFFER_SIZE;
+	if (is_encoder(zt)) {
+		zt->t.framein = dahdi_encoder_framein;
+		zt->t.frameout = dahdi_encoder_frameout;
+		zt->t.buffer_samples = 0;
+	} else {
+		zt->t.framein = dahdi_decoder_framein;
+		zt->t.frameout = dahdi_decoder_frameout;
+		if (AST_FORMAT_G723_1 == zt->t.srcfmt) {
+			zt->t.plc_samples = G723_SAMPLES;
+		} else {
+			zt->t.plc_samples = G729_SAMPLES;
+		}
+		zt->t.buffer_samples = zt->t.plc_samples * 8;
+	}
+	zt->t.destroy = dahdi_destroy;
+	zt->t.newpvt = dahdi_new;
 	zt->t.sample = fakesrc_sample;
 	zt->t.useplc = global_useplc;
-	zt->t.buf_size = BUFFER_SAMPLES * 2;
-	zt->t.desc_size = sizeof(struct pvt);
+	zt->t.native_plc = 1;
+	zt->t.desc_size = sizeof(struct codec_dahdi_pvt);
 	if ((res = ast_register_translator(&zt->t))) {
 		free(zt);
 		return -1;

Modified: team/sruffell/asterisk-1.4-transcoder/main/translate.c
URL: http://svn.digium.com/view/asterisk/team/sruffell/asterisk-1.4-transcoder/main/translate.c?view=diff&rev=160891&r1=160890&r2=160891
==============================================================================
--- team/sruffell/asterisk-1.4-transcoder/main/translate.c (original)
+++ team/sruffell/asterisk-1.4-transcoder/main/translate.c Wed Dec  3 20:02:21 2008
@@ -179,7 +179,10 @@
 			if (pvt->plc) {
 				int l = pvt->t->plc_samples;
 				if (pvt->samples + l > pvt->t->buffer_samples) {
-					ast_log(LOG_WARNING, "Out of buffer space\n");
+					ast_log(LOG_WARNING, "Out of buffer space %d %d\n",
+						pvt->samples + l,
+						pvt->t->buffer_samples);
+					exit(-1);
 					return -1;
 				}
 				l = plc_fillin(pvt->plc, dst + pvt->samples, l);
@@ -187,8 +190,10 @@
 				pvt->datalen = pvt->samples * 2;	/* SLIN has 2bytes for 1sample */
 			}
 			/* We don't want generic PLC. If the codec has native PLC, then do that */
-			if (!pvt->t->native_plc)
+			if (!pvt->t->native_plc) {
+				ast_log(LOG_WARNING, "native_plc\n");
 				return 0;
+			}
 		}
 		if (pvt->samples + f->samples > pvt->t->buffer_samples) {
 			ast_log(LOG_WARNING, "Out of buffer space\n");




More information about the svn-commits mailing list