Index: Makefile
===================================================================
RCS file: /usr/cvsroot/asterisk/Makefile,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- Makefile	17 Jun 2003 22:30:25 -0000	1.17
+++ Makefile	29 Jun 2003 20:32:26 -0000	1.18
@@ -91,7 +91,7 @@
 CFLAGS+=-pthread
 endif
 
-#CFLAGS+=$(shell if [ -f /usr/include/linux/zaptel.h ]; then echo "-DZAPTEL_OPTIMIZATIONS"; fi)
+CFLAGS+=$(shell if [ -f /usr/include/linux/zaptel.h ]; then echo "-DZAPTEL_OPTIMIZATIONS"; fi)
 
 LIBEDIT=editline/libedit.a
 
Index: channel.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channel.c,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- channel.c	28 Jun 2003 16:40:02 -0000	1.21
+++ channel.c	29 Jun 2003 20:32:26 -0000	1.22
@@ -938,13 +938,19 @@
 	return result;
 }
 
-int ast_settimeout(struct ast_channel *c, int ms)
+int ast_settimeout(struct ast_channel *c, int samples, int (*func)(void *data), void *data)
 {
 	int res = -1;
 #ifdef ZAPTEL_OPTIMIZATIONS
 	if (c->timingfd > -1) {
-		ms *= 8;
-		res = ioctl(c->timingfd, ZT_TIMERCONFIG, &ms);
+		if (!func) {
+			samples = 0;
+			data = 0;
+		}
+		ast_log(LOG_DEBUG, "Scheduling timer at %d sample intervals\n", samples);
+		res = ioctl(c->timingfd, ZT_TIMERCONFIG, &samples);
+		c->timingfunc = func;
+		c->timingdata = data;
 	}
 #endif	
 	return res;
@@ -983,6 +989,10 @@
 {
 	struct ast_frame *f = NULL;
 	int blah;
+#ifdef ZAPTEL_OPTIMIZATIONS
+	int (*func)(void *);
+	void *data;
+#endif
 	static struct ast_frame null_frame = 
 	{
 		AST_FRAME_NULL,
@@ -1027,10 +1037,18 @@
 		chan->exception = 0;
 		blah = -1;
 		ioctl(chan->timingfd, ZT_TIMERACK, &blah);
-		blah = 0;
-		ioctl(chan->timingfd, ZT_TIMERCONFIG, &blah);
-		f =  &null_frame;
+		func = chan->timingfunc;
+		data = chan->timingdata;
 		pthread_mutex_unlock(&chan->lock);
+		if (func) {
+			func(data);
+		} else {
+			blah = 0;
+			pthread_mutex_lock(&chan->lock);
+			ioctl(chan->timingfd, ZT_TIMERCONFIG, &blah);
+			pthread_mutex_unlock(&chan->lock);
+		}
+		f =  &null_frame;
 		return f;
 	}
 #endif
Index: file.c
===================================================================
RCS file: /usr/cvsroot/asterisk/file.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- file.c	29 Jun 2003 16:59:13 -0000	1.11
+++ file.c	29 Jun 2003 20:32:26 -0000	1.12
@@ -508,16 +508,26 @@
 			if (ast_write(s->owner, fr)) {
 				ast_log(LOG_WARNING, "Failed to write frame\n");
 				s->owner->streamid = -1;
+#ifdef ZAPTEL_OPTIMIZATIONS
+				ast_settimeout(s->owner, 0, NULL, NULL);
+#endif			
 				return 0;
 			}
 		} else {
 			/* Stream has finished */
 			s->owner->streamid = -1;
+#ifdef ZAPTEL_OPTIMIZATIONS
+			ast_settimeout(s->owner, 0, NULL, NULL);
+#endif			
 			return 0;
 		}
 	}
 	if (whennext != s->lasttimeout) {
+#ifdef ZAPTEL_OPTIMIZATIONS
+		ast_settimeout(s->owner, whennext, ast_readaudio_callback, s);
+#else
 		s->owner->streamid = ast_sched_add(s->owner->sched, whennext/8, ast_readaudio_callback, s);
+#endif		
 		s->lasttimeout = whennext;
 		return 0;
 	}
@@ -606,6 +616,9 @@
 			if (f->owner->streamid > -1)
 				ast_sched_del(f->owner->sched, f->owner->streamid);
 			f->owner->streamid = -1;
+#ifdef ZAPTEL_OPTIMIZATIONS
+			ast_settimeout(f->owner, 0, NULL, NULL);
+#endif			
 		} else {
 			f->owner->vstream = NULL;
 			if (f->owner->vstreamid > -1)
@@ -766,15 +779,15 @@
 	struct ast_frame *fr;
 	while(c->stream) {
 		res = ast_sched_wait(c->sched);
-		if (res < 0) {
+		if ((res < 0) && !c->timingfunc) {
 			if (c->stream)
 				ast_closestream(c->stream);
 			if (c->vstream)
 				ast_closestream(c->vstream);
 			break;
 		}
-		/* Setup timeout if supported */
-		ast_settimeout(c, res);
+		if (res < 0)
+			res = 1000;
 		res = ast_waitfor(c, res);
 		if (res < 0) {
 			ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
@@ -832,7 +845,6 @@
 				ast_closestream(c->vstream);
 			break;
 		}
-		ast_settimeout(c, res);
 		res = ast_waitfor(c, res);
 		if (res < 0) {
 			ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
@@ -899,7 +911,6 @@
 				ast_closestream(c->vstream);
 			break;
 		}
-		ast_settimeout(c, ms);
 		rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
 		if (!rchan && (outfd < 0) && (ms)) {
 			ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
Index: formats/format_vox.c
===================================================================
RCS file: /usr/cvsroot/asterisk/formats/format_vox.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- formats/format_vox.c	28 Jun 2003 22:50:47 -0000	1.3
+++ formats/format_vox.c	29 Jun 2003 20:32:26 -0000	1.4
@@ -48,7 +48,6 @@
 };
 
 
-static struct ast_filestream *glist = NULL;
 static pthread_mutex_t vox_lock = AST_MUTEX_INITIALIZER;
 static int glistcnt = 0;
 
Index: include/asterisk/channel.h
===================================================================
RCS file: /usr/cvsroot/asterisk/include/asterisk/channel.h,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- include/asterisk/channel.h	29 Jun 2003 03:24:39 -0000	1.14
+++ include/asterisk/channel.h	29 Jun 2003 20:32:26 -0000	1.15
@@ -125,7 +125,8 @@
 	
 	/*! Timing fd */
 	int timingfd;
-
+	int (*timingfunc)(void *data);
+	void *timingdata;
 
 	/*! State of line -- Don't write directly, use ast_setstate */
 	int _state;				
@@ -695,8 +696,8 @@
 int ast_autoservice_stop(struct ast_channel *chan);
 
 /* If built with zaptel optimizations, force a scheduled expiration on the
-   timer fd */
-int ast_settimeout(struct ast_channel *c, int ms);
+   timer fd, at which point we call the callback function / data */
+int ast_settimeout(struct ast_channel *c, int samples, int (*func)(void *data), void *data);
 
 /* Transfer a channel (if supported).  Returns -1 on error, 0 if not supported
    and 1 if supported and requested */
Index: include/asterisk/file.h
===================================================================
RCS file: /usr/cvsroot/asterisk/include/asterisk/file.h,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- include/asterisk/file.h	29 Jun 2003 03:24:39 -0000	1.5
+++ include/asterisk/file.h	29 Jun 2003 20:32:26 -0000	1.6
@@ -259,7 +259,7 @@
  */
 long ast_tellstream(struct ast_filestream *fs);
 
-#define AST_RESERVED_POINTERS 12
+#define AST_RESERVED_POINTERS 20
 
 #if defined(__cplusplus) || defined(c_plusplus)
 }
