[asterisk-commits] jrose: branch 10 r365990 - in /branches/10: ./ codecs/codec_dahdi.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed May 9 14:12:36 CDT 2012


Author: jrose
Date: Wed May  9 14:12:32 2012
New Revision: 365990

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=365990
Log:
Block on frameout if the hardware has enough samples to complete a frame.

Fixes some problems with skipping audio in elaborate scenarios involving
multiple codecs by making codec_dahdi operate in a more synchronous
fashion similar to codec_g729. This change also fixes the use of file
conversion tools from Asterisk's CLI. This change may cause the thread
responsible for transcoding audio to block briefly (Shaun Ruffell describes
this as 'several milliseconds') while waiting for the hardware transcoder.

(closes issue ASTERISK-19643)
reported by: Shaun Ruffell
Patches:
	0001-codec_dahdi-Block-on-frameout-the-hardware-has-enoug.patch
	uploaded by Shaun Ruffell (license 5417)
........

Merged revisions 365989 from http://svn.asterisk.org/svn/asterisk/branches/1.8

Modified:
    branches/10/   (props changed)
    branches/10/codecs/codec_dahdi.c

Propchange: branches/10/
------------------------------------------------------------------------------
Binary property 'branch-1.8-merged' - no diff available.

Modified: branches/10/codecs/codec_dahdi.c
URL: http://svnview.digium.com/svn/asterisk/branches/10/codecs/codec_dahdi.c?view=diff&rev=365990&r1=365989&r2=365990
==============================================================================
--- branches/10/codecs/codec_dahdi.c (original)
+++ branches/10/codecs/codec_dahdi.c Wed May  9 14:12:32 2012
@@ -56,6 +56,7 @@
 
 #define G723_SAMPLES 240
 #define G729_SAMPLES 160
+#define ULAW_SAMPLES 160
 
 #ifndef DAHDI_FORMAT_MAX_AUDIO
 #define DAHDI_FORMAT_G723_1    (1 << 0)
@@ -103,6 +104,7 @@
 	unsigned int fake:2;
 	uint16_t required_samples;
 	uint16_t samples_in_buffer;
+	uint16_t samples_written_to_hardware;
 	uint8_t ulaw_buffer[1024];
 };
 
@@ -174,7 +176,6 @@
 static void dahdi_write_frame(struct codec_dahdi_pvt *dahdip, const uint8_t *buffer, const ssize_t count)
 {
 	int res;
-	struct pollfd p = {0};
 	if (!count) return;
 	res = write(dahdip->fd, buffer, count);
 	if (option_verbose > 10) {
@@ -185,9 +186,6 @@
 			ast_log(LOG_ERROR, "Requested write of %zd bytes, but only wrote %d bytes.\n", count, res);
 		}
 	}
-	p.fd = dahdip->fd;
-	p.events = POLLOUT;
-	res = poll(&p, 1, 50);
 }
 
 static int dahdi_encoder_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
@@ -220,8 +218,9 @@
 		dahdip->samples_in_buffer += f->samples;
 	}
 
-	while (dahdip->samples_in_buffer > dahdip->required_samples) {
+	while (dahdip->samples_in_buffer >= dahdip->required_samples) {
 		dahdi_write_frame(dahdip, dahdip->ulaw_buffer, dahdip->required_samples);
+		dahdip->samples_written_to_hardware += dahdip->required_samples;
 		dahdip->samples_in_buffer -= dahdip->required_samples;
 		if (dahdip->samples_in_buffer) {
 			/* Shift any remaining bytes down. */
@@ -232,6 +231,14 @@
 	pvt->samples += f->samples;
 	pvt->datalen = 0;
 	return -1;
+}
+
+static void dahdi_wait_for_packet(int fd)
+{
+	struct pollfd p = {0};
+	p.fd = fd;
+	p.events = POLLIN;
+	poll(&p, 1, 10);
 }
 
 static struct ast_frame *dahdi_encoder_frameout(struct ast_trans_pvt *pvt)
@@ -257,6 +264,10 @@
 		return NULL;
 	}
 
+	if (dahdip->samples_written_to_hardware >= dahdip->required_samples) {
+		dahdi_wait_for_packet(dahdip->fd);
+	}
+
 	res = read(dahdip->fd, pvt->outbuf.c + pvt->datalen, pvt->t->buf_size - pvt->datalen);
 	if (-1 == res) {
 		if (EWOULDBLOCK == errno) {
@@ -276,6 +287,10 @@
 		pvt->f.data.ptr = pvt->outbuf.c;
 		pvt->f.samples = ast_codec_get_samples(&pvt->f);
 
+		dahdip->samples_written_to_hardware =
+		  (dahdip->samples_written_to_hardware >= pvt->f.samples) ?
+		     dahdip->samples_written_to_hardware - pvt->f.samples : 0;
+
 		pvt->samples = 0;
 		pvt->datalen = 0;
 		return ast_frisolate(&pvt->f);
@@ -302,6 +317,7 @@
 		}
 	}
 	dahdi_write_frame(dahdip, f->data.ptr, f->datalen);
+	dahdip->samples_written_to_hardware += f->samples;
 	pvt->samples += f->samples;
 	pvt->datalen = 0;
 	return -1;
@@ -329,6 +345,10 @@
 		return NULL;
 	}
 
+	if (dahdip->samples_written_to_hardware >= ULAW_SAMPLES) {
+		dahdi_wait_for_packet(dahdip->fd);
+	}
+
 	/* Let's check to see if there is a new frame for us.... */
 	if (dahdip->softslin) {
 		res = read(dahdip->fd, dahdip->ulaw_buffer, sizeof(dahdip->ulaw_buffer));
@@ -360,6 +380,9 @@
 		pvt->f.data.ptr = pvt->outbuf.c;
 		pvt->f.samples = res;
 		pvt->samples = 0;
+		dahdip->samples_written_to_hardware =
+			(dahdip->samples_written_to_hardware >= res) ?
+			        dahdip->samples_written_to_hardware - res : 0;
 
 		return ast_frisolate(&pvt->f);
 	}




More information about the asterisk-commits mailing list