[svn-commits] sruffell: branch sruffell/asterisk-1.4-transcoder r131793 - /team/sruffell/as...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Thu Jul 17 16:15:27 CDT 2008


Author: sruffell
Date: Thu Jul 17 16:15:27 2008
New Revision: 131793

URL: http://svn.digium.com/view/asterisk?view=rev&rev=131793
Log:
Re-updating to support the new transcoder interface in:
http://svn.digium.com/svn/dahdi/linux/team/sruffell/dahdi-transcoder

Modified:
    team/sruffell/asterisk-1.4-transcoder/codecs/codec_dahdi.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=131793&r1=131792&r2=131793
==============================================================================
--- team/sruffell/asterisk-1.4-transcoder/codecs/codec_dahdi.c (original)
+++ team/sruffell/asterisk-1.4-transcoder/codecs/codec_dahdi.c Thu Jul 17 16:15:27 2008
@@ -1,9 +1,9 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Zaptel native transcoding support
- *
- * Copyright (C) 1999 - 2006, Digium, Inc.
+ * DAHDI native transcoding support
+ *
+ * Copyright (C) 1999 - 2008, Digium, Inc.
  *
  * Mark Spencer <markster at digium.com>
  * Kevin P. Fleming <kpfleming at digium.com>
@@ -21,7 +21,7 @@
 
 /*! \file
  *
- * \brief Translate between various formats natively through Zaptel transcoding
+ * \brief Translate between various formats natively through DAHDI transcoding
  *
  * \ingroup codecs
  */
@@ -44,6 +44,7 @@
 #include <sys/ioctl.h>
 #include <errno.h>
 #include <sys/mman.h>
+#include <dahdi/user.h>
 
 #include "asterisk/lock.h"
 #include "asterisk/translate.h"
@@ -56,9 +57,14 @@
 #include "asterisk/utils.h"
 #include "asterisk/linkedlists.h"
 
-#include "asterisk/dahdi_compat.h"
-
 #define BUFFER_SAMPLES	8000
+
+
+#ifdef HAVE_ZAPTEL
+const char *device_file = "/dev/zap/transcode";
+#else
+const char *device_file = "/dev/dahdi/transcode";
+#endif
 
 static unsigned int global_useplc = 0;
 
@@ -70,25 +76,25 @@
 
 static char show_transcoder_usage[] =
 "Usage: show transcoder\n"
-"       Displays channel utilization of Zaptel transcoder(s).\n";
+"       Displays channel utilization of DAHDI transcoder(s).\n";
 
 static char transcoder_show_usage[] =
 "Usage: transcoder show\n"
-"       Displays channel utilization of Zaptel transcoder(s).\n";
+"       Displays channel utilization of DAHDI transcoder(s).\n";
 
 static int transcoder_show(int fd, int argc, char **argv);
 
 static struct ast_cli_entry cli_deprecated[] = {
 	{ { "show", "transcoder", NULL },
 	  transcoder_show,
-	  "Display Zaptel transcoder utilization.",
+	  "Display DAHDI transcoder utilization.",
 	  show_transcoder_usage}
 };
 
 static struct ast_cli_entry cli[] = {
 	{ { "transcoder", "show", NULL },
 	  transcoder_show,
-	  "Display Zaptel transcoder utilization.",
+	  "Display DAHDI transcoder utilization.",
 	  transcoder_show_usage, NULL,
 	  &cli_deprecated[0]}
 };
@@ -114,7 +120,7 @@
 	int totalms;
 	int lasttotalms;
 #endif
-	DAHDI_TRANSCODE_HEADER *hdr;
+	struct dahdi_transcoder_formats fmts;
 };
 
 static int transcoder_show(int fd, int argc, char **argv)
@@ -124,17 +130,18 @@
 	copy = channels;
 
 	if (copy.total == 0)
-		ast_cli(fd, "No Zaptel transcoders found.\n");
+		ast_cli(fd, "No DAHDI transcoders found.\n");
 	else
 		ast_cli(fd, "%d/%d encoders/decoders of %d channels are in use.\n", copy.encoders, copy.decoders, copy.total);
 
 	return RESULT_SUCCESS;
 }
 
-static int zap_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
-{
+static int dahdi_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;
-	DAHDI_TRANSCODE_HEADER *hdr = ztp->hdr;
 
 	if (!f->subclass) {
 		/* Fake a return frame for calculation purposes */
@@ -143,104 +150,40 @@
 		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... */
+	ast_log(LOG_ERROR, "Requesting to write %d bytes.\n", f->datalen);
+	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.... */
+	ast_log(LOG_ERROR, "Requesting to read %d bytes.\n", pvt->t->buf_size - pvt->datalen);
+	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 dahdi_destroy(struct ast_trans_pvt *pvt)
 {
 	struct pvt *ztp = pvt->pvt;
-	DAHDI_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 = DAHDI_TCOP_TRANSCODE;
-				if (ioctl(ztp->fd, DAHDI_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 = DAHDI_TCOP_RELEASE;
-	if (ioctl(ztp->fd, DAHDI_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);
@@ -250,58 +193,35 @@
 		break;
 	}
 
-	munmap(ztp->hdr, sizeof(*ztp->hdr));
 	close(ztp->fd);
 }
 
-static int zap_translate(struct ast_trans_pvt *pvt, int dest, int source)
-{
-	/* Request translation through zap if possible */
+static int dahdi_translate(struct ast_trans_pvt *pvt, int dest, int source)
+{
+	/* Request translation through DAHDI if possible */
 	int fd;
-	unsigned int x = DAHDI_TCOP_ALLOCATE;
 	struct pvt *ztp = pvt->pvt;
-	DAHDI_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 ((fd = open(device_file, O_RDWR)) < 0) {
+		ast_log(LOG_VERBOSE, "Unable to open %s\n", device_file);
+		return -1;
 	}
 	
-
-	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, DAHDI_TC_ALLOCATE, &ztp->fmts)) {
+		ast_log(LOG_ERROR, "Unable to attach to transcoder: %s\n", strerror(errno));
 		close(fd);
 
 		return -1;
 	}
 
-	if (hdr->magic != DAHDI_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, DAHDI_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);
@@ -314,9 +234,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)
@@ -339,14 +259,13 @@
 	if (!(zt = ast_calloc(1, sizeof(*zt))))
 		return -1;
 
-	snprintf((char *) (zt->t.name), sizeof(zt->t.name), "zap%sto%s", 
+	snprintf((char *) (zt->t.name), sizeof(zt->t.name), "dahdi%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.newpvt = dahdi_new;
+	zt->t.framein = dahdi_framein;
+	zt->t.destroy = dahdi_destroy;
 	zt->t.sample = fakesrc_sample;
 	zt->t.useplc = global_useplc;
 	zt->t.buf_size = BUFFER_SAMPLES * 2;
@@ -408,12 +327,13 @@
 		return;
 
 	for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
-	       if (!strcasecmp(var->name, "genericplc")) {
-		       global_useplc = ast_true(var->value);
-		       if (option_verbose > 2)
-			       ast_verbose(VERBOSE_PREFIX_3 "codec_zap: %susing generic PLC\n",
+		if (!strcasecmp(var->name, "genericplc")) {
+			global_useplc = ast_true(var->value);
+			if (option_verbose > 2)
+				ast_verbose(VERBOSE_PREFIX_3 "codec_dahdi: %susing generic PLC\n",
 					   global_useplc ? "" : "not ");
 	       }
+
 	}
 
 	ast_config_destroy(cfg);
@@ -442,18 +362,20 @@
 
 static int find_transcoders(void)
 {
-	DAHDI_TRANSCODE_INFO info = { 0, };
-	struct format_map map = { { { 0 } } };
+	struct dahdi_transcoder_info info;
+	struct format_map map;
 	int fd, res;
 	unsigned int x, y;
 
-	if ((fd = open("/dev/zap/transcode", O_RDWR)) < 0) {
+	memset(&info, 0, sizeof(info));
+	memset(&map, 0, sizeof(map));
+
+	if ((fd = open(device_file, O_RDWR)) < 0) {
 		ast_verbose(VERBOSE_PREFIX_2 "No hardware transcoders found.\n");
 		return 0;
 	}
 
-	info.op = DAHDI_TCOP_GETINFO;
-	for (info.tcnum = 0; !(res = ioctl(fd, DAHDI_TRANSCODE_OP, &info)); info.tcnum++) {
+	for (info.tcnum = 0; !(res = ioctl(fd, DAHDI_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);
@@ -506,7 +428,7 @@
 	return 0;
 }
 
-AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Generic Zaptel Transcoder Codec Translator",
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Generic DAHDI Transcoder Codec Translator",
 		.load = load_module,
 		.unload = unload_module,
 		.reload = reload,




More information about the svn-commits mailing list