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

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon May 5 09:18:31 CDT 2008


Author: sruffell
Date: Mon May  5 09:18:31 2008
New Revision: 115294

URL: http://svn.digium.com/view/asterisk?view=rev&rev=115294
Log:
Adding back changes to codec_zap after creating a transcoder branch of the
head of the 1.4 branch.

Modified:
    team/sruffell/asterisk-1.4-transcoder/   (props changed)
    team/sruffell/asterisk-1.4-transcoder/codecs/codec_zap.c

Propchange: team/sruffell/asterisk-1.4-transcoder/
------------------------------------------------------------------------------
    automerge = yes

Propchange: team/sruffell/asterisk-1.4-transcoder/
------------------------------------------------------------------------------
    svnmerge-integrated = /branches/1.4:1-115292

Modified: team/sruffell/asterisk-1.4-transcoder/codecs/codec_zap.c
URL: http://svn.digium.com/view/asterisk/team/sruffell/asterisk-1.4-transcoder/codecs/codec_zap.c?view=diff&rev=115294&r1=115293&r2=115294
==============================================================================
--- team/sruffell/asterisk-1.4-transcoder/codecs/codec_zap.c (original)
+++ team/sruffell/asterisk-1.4-transcoder/codecs/codec_zap.c Mon May  5 09:18:31 2008
@@ -3,7 +3,7 @@
  *
  * Zaptel native transcoding support
  *
- * Copyright (C) 1999 - 2006, Digium, Inc.
+ * Copyright (C) 1999 - 2008, Digium, Inc.
  *
  * Mark Spencer <markster at digium.com>
  * Kevin P. Fleming <kpfleming at digium.com>
@@ -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,58 +132,62 @@
 
 static int zap_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
+	int res;
 	struct pvt *ztp = pvt->pvt;
-	struct zt_transcode_header *hdr = ztp->hdr;
-
-	if (!f->subclass) {
+
+	if (f->subclass) {
+		/* 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));
+		}
+		if (f->datalen != 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 */
 		ztp->fake = 2;
 		pvt->samples = f->samples;
-		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;
+		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.... */
+		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 /dev/transcode: %s\n", strerror(errno));
+				return NULL;
+			}
+		} else {
+			pvt->f.samples = res;
+			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;
+			ast_set_flag(&pvt->f, AST_FRFLAG_FROM_TRANSLATOR);
+
+			return &pvt->f;
 		}
-		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)
-{
-	struct pvt *ztp = pvt->pvt;
-	struct zt_transcode_header *hdr = ztp->hdr;
-	unsigned int x;
-
-	if (ztp->fake == 2) {
+
+	} else if (2 == ztp->fake) {
+
 		ztp->fake = 1;
 		pvt->f.frametype = AST_FRAME_VOICE;
 		pvt->f.subclass = 0;
@@ -194,52 +198,23 @@
 		pvt->f.mallocd = 0;
 		ast_set_flag(&pvt->f, AST_FRFLAG_FROM_TRANSLATOR);
 		pvt->samples = 0;
-	} else if (ztp->fake == 1) {
+
+		return &pvt->f;
+
+	} else if (1 == ztp->fake) {
+
 		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;
+
+	}
+	/* Shouldn't get here... */
+	return NULL;
 }
 
 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 +224,6 @@
 		break;
 	}
 
-	munmap(ztp->hdr, sizeof(*ztp->hdr));
 	close(ztp->fd);
 }
 
@@ -257,50 +231,33 @@
 {
 	/* 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;
+	
+	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;
+	}
+
 	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));
-		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);
@@ -441,7 +398,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 +408,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