[asterisk-commits] sruffell: branch sruffell/transcoder r114711 - /team/sruffell/transcoder/codecs/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Apr 28 03:35:02 CDT 2008


Author: sruffell
Date: Mon Apr 28 03:35:02 2008
New Revision: 114711

URL: http://svn.digium.com/view/asterisk?view=rev&rev=114711
Log:
Updated codecs/codec_zap.c to use interface exported by: 
http://svn.digium.com/svn/zaptel/team/sruffell/zaptel-1.4-transcoder


Modified:
    team/sruffell/transcoder/codecs/codec_zap.c

Modified: team/sruffell/transcoder/codecs/codec_zap.c
URL: http://svn.digium.com/view/asterisk/team/sruffell/transcoder/codecs/codec_zap.c?view=diff&rev=114711&r1=114710&r2=114711
==============================================================================
--- team/sruffell/transcoder/codecs/codec_zap.c (original)
+++ team/sruffell/transcoder/codecs/codec_zap.c Mon Apr 28 03:35:02 2008
@@ -113,7 +113,7 @@
 	int totalms;
 	int lasttotalms;
 #endif
-	struct zt_transcode_header *hdr;
+	struct zt_transcoder_formats fmts;
 };
 
 static int transcoder_show(int fd, int argc, char **argv)
@@ -132,8 +132,9 @@
 
 static int zap_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
+	int res;
+	unsigned char *dst = (unsigned char *)pvt->outbuf + pvt->datalen;
 	struct pvt *ztp = pvt->pvt;
-	struct zt_transcode_header *hdr = ztp->hdr;
 
 	if (!f->subclass) {
 		/* Fake a return frame for calculation purposes */
@@ -142,104 +143,38 @@
 		return 0;
 	}
 
-	if (!hdr->srclen)
-		/* Copy at front of buffer */
-		hdr->srcoffset = 0;
-
-	/* if we get handed a G.729 frame that is not a multiple of
-	   10 bytes (10 milliseconds), then it has a CNG frame and
-	   we need to avoid sending that to the transcoder
-	*/
-	if ((f->subclass == AST_FORMAT_G729A) && ((f->datalen % 10) != 0)) {
-		if (!ztp->g729b_warning) {
-			ast_log(LOG_WARNING, "G.729B CNG frame received but is not supported; dropping.\n");
-			ztp->g729b_warning = 1;
-		}
-		f->datalen -= f->datalen % 10;
-		f->samples = f->datalen * 8;
-	}
-
-	if (hdr->srclen + f->datalen > sizeof(hdr->srcdata)) {
-		ast_log(LOG_WARNING, "Out of space for codec translation!\n");
-		return -1;
-	}
-
-	if (hdr->srclen + f->datalen + hdr->srcoffset > sizeof(hdr->srcdata)) {
-		/* Very unlikely */
-		memmove(hdr->srcdata, hdr->srcdata + hdr->srcoffset, hdr->srclen);
-		hdr->srcoffset = 0;
-	}
-
-	memcpy(hdr->srcdata + hdr->srcoffset + hdr->srclen, f->data, f->datalen);
-	hdr->srclen += f->datalen;
-	pvt->samples += f->samples;
-
-	return -1;
-}
-
-static struct ast_frame *zap_frameout(struct ast_trans_pvt *pvt)
+	/* Give the frame to the hardware transcoder... */
+	res = write(ztp->fd,f->data, f->datalen); 
+	if (-1 == res) {
+		ast_log(LOG_ERROR, "Failed to write to /dev/transcode: %s\n", strerror(errno));
+		return -1;
+	}
+	if (f->datalen != res) {
+		ast_log(LOG_ERROR, "Requested write of %d bytes, but only wrote %d bytes.\n", f->datalen, res);
+	}
+
+	/* Wait for the results.... */
+	res = read(ztp->fd, dst, pvt->t->buf_size - pvt->datalen);
+	if (-1 == res) {
+		ast_log(LOG_ERROR, "Failed to read from /dev/transcode: %s\n", strerror(errno));
+		return -1;
+	}
+	if (0 == res) {
+		ast_log(LOG_NOTICE, "Timeout waiting for transcoder.\n");
+		return -1;
+	}
+
+	pvt->samples += res;
+	pvt->datalen += res;
+
+	return 0;
+}
+
+static void zap_destroy(struct ast_trans_pvt *pvt)
 {
 	struct pvt *ztp = pvt->pvt;
-	struct zt_transcode_header *hdr = ztp->hdr;
-	unsigned int x;
-
-	if (ztp->fake == 2) {
-		ztp->fake = 1;
-		pvt->f.frametype = AST_FRAME_VOICE;
-		pvt->f.subclass = 0;
-		pvt->f.samples = 160;
-		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;
-	} else if (ztp->fake == 1) {
-		return NULL;
-	} else {
-		if (hdr->dstlen) {
-#ifdef DEBUG_TRANSCODE
-			ztp->totalms += hdr->dstsamples;
-			if ((ztp->totalms - ztp->lasttotalms) > 8000) {
-				printf("Whee %p, %d (%d to %d)\n", ztp, hdr->dstlen, ztp->lasttotalms, ztp->totalms);
-				ztp->lasttotalms = ztp->totalms;
-			}
-#endif
-			pvt->f.frametype = AST_FRAME_VOICE;
-			pvt->f.subclass = hdr->dstfmt;
-			pvt->f.samples = hdr->dstsamples;
-			pvt->f.data = hdr->dstdata + hdr->dstoffset;
-			pvt->f.offset = hdr->dstoffset;
-			pvt->f.datalen = hdr->dstlen;
-			pvt->f.mallocd = 0;
-			ast_set_flag(&pvt->f, AST_FRFLAG_FROM_TRANSLATOR);
-			pvt->samples -= pvt->f.samples;
-			hdr->dstlen = 0;
-			
-		} else {
-			if (hdr->srclen) {
-				hdr->dstoffset = AST_FRIENDLY_OFFSET;
-				x = ZT_TCOP_TRANSCODE;
-				if (ioctl(ztp->fd, ZT_TRANSCODE_OP, &x))
-					ast_log(LOG_WARNING, "Failed to transcode: %s\n", strerror(errno));
-			}
-			return NULL;
-		}
-	}
-
-	return &pvt->f;
-}
-
-static void zap_destroy(struct ast_trans_pvt *pvt)
-{
-	struct pvt *ztp = pvt->pvt;
-	unsigned int x;
-
-	x = ZT_TCOP_RELEASE;
-	if (ioctl(ztp->fd, ZT_TRANSCODE_OP, &x))
-		ast_log(LOG_WARNING, "Failed to release transcoder channel: %s\n", strerror(errno));
-
-	switch (ztp->hdr->dstfmt) {
+
+	switch (ztp->fmts.dstfmt) {
 	case AST_FORMAT_G729A:
 	case AST_FORMAT_G723_1:
 		ast_atomic_fetchadd_int(&channels.encoders, -1);
@@ -249,7 +184,6 @@
 		break;
 	}
 
-	munmap(ztp->hdr, sizeof(*ztp->hdr));
 	close(ztp->fd);
 }
 
@@ -257,50 +191,26 @@
 {
 	/* Request translation through zap if possible */
 	int fd;
-	unsigned int x = ZT_TCOP_ALLOCATE;
 	struct pvt *ztp = pvt->pvt;
-	struct zt_transcode_header *hdr;
-	int flags;
 	
 	if ((fd = open("/dev/zap/transcode", O_RDWR)) < 0)
 		return -1;
-	flags = fcntl(fd, F_GETFL);
-	if (flags > - 1) {
-		if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
-			ast_log(LOG_WARNING, "Could not set non-block mode!\n");
-	}
 	
-
-	if ((hdr = mmap(NULL, sizeof(*hdr), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
-		ast_log(LOG_ERROR, "Memory Map failed for transcoding (%s)\n", strerror(errno));
+	ztp->fmts.srcfmt = (1 << source);
+	ztp->fmts.dstfmt = (1 << dest);
+
+	ast_log(LOG_VERBOSE, "Opening transcoder channel from %d to %d.\n", source, dest);
+
+	if (ioctl(fd, ZT_TC_ALLOCATE, &ztp->fmts)) {
+		ast_log(LOG_ERROR, "Unable to attach to transcoder: %s\n", strerror(errno));
 		close(fd);
 
 		return -1;
 	}
 
-	if (hdr->magic != ZT_TRANSCODE_MAGIC) {
-		ast_log(LOG_ERROR, "Transcoder header (%08x) wasn't magic.  Abandoning\n", hdr->magic);
-		munmap(hdr, sizeof(*hdr));
-		close(fd);
-
-		return -1;
-	}
-	
-	hdr->srcfmt = (1 << source);
-	hdr->dstfmt = (1 << dest);
-	if (ioctl(fd, ZT_TRANSCODE_OP, &x)) {
-		ast_log(LOG_ERROR, "Unable to attach transcoder: %s\n", strerror(errno));
-		munmap(hdr, sizeof(*hdr));
-		close(fd);
-
-		return -1;
-	}
-
-	ztp = pvt->pvt;
 	ztp->fd = fd;
-	ztp->hdr = hdr;
-
-	switch (hdr->dstfmt) {
+
+	switch (ztp->fmts.dstfmt) {
 	case AST_FORMAT_G729A:
 	case AST_FORMAT_G723_1:
 		ast_atomic_fetchadd_int(&channels.encoders, +1);
@@ -344,7 +254,6 @@
 	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.sample = fakesrc_sample;
 	zt->t.useplc = global_useplc;
@@ -441,7 +350,7 @@
 
 static int find_transcoders(void)
 {
-	struct zt_transcode_info info = { 0, };
+	struct zt_transcoder_info info = { 0, };
 	struct format_map map = { { { 0 } } };
 	int fd, res;
 	unsigned int x, y;
@@ -451,8 +360,7 @@
 		return 0;
 	}
 
-	info.op = ZT_TCOP_GETINFO;
-	for (info.tcnum = 0; !(res = ioctl(fd, ZT_TRANSCODE_OP, &info)); info.tcnum++) {
+	for (info.tcnum = 0; !(res = ioctl(fd, ZT_TC_GETINFO, &info)); info.tcnum++) {
 		if (option_verbose > 1)
 			ast_verbose(VERBOSE_PREFIX_2 "Found transcoder '%s'.\n", info.name);
 		build_translators(&map, info.dstfmts, info.srcfmts);




More information about the asterisk-commits mailing list