Index: channels/chan_agent.c
===================================================================
--- channels/chan_agent.c	(revision 7269)
+++ channels/chan_agent.c	(working copy)
@@ -51,6 +51,7 @@
 #include "asterisk/module.h"
 #include "asterisk/pbx.h"
 #include "asterisk/options.h"
+#include "asterisk/translate.h"
 #include "asterisk/lock.h"
 #include "asterisk/sched.h"
 #include "asterisk/io.h"
@@ -154,6 +155,8 @@
 static int autologoff;
 static int wrapuptime;
 static int ackcall;
+static char acksound[AST_MAX_BUF] = "";
+static int ackwait = 0;
 
 static int maxlogintries = 3;
 static char agentgoodbye[AST_MAX_FILENAME_LEN] = "vm-goodbye";
@@ -202,6 +205,8 @@
 	char loginchan[80];            /**< channel they logged in from */
 	char logincallerid[80];        /**< Caller ID they had when they logged in */
 	struct ast_channel *chan;      /**< Channel we use */
+        struct ast_trans_pvt *trans;
+        int framecount;
 	struct agent_pvt *next;        /**< Next Agent in the linked list. */
 };
 
@@ -484,6 +489,14 @@
 			/* Note that we don't hangup if it's not a callback because Asterisk will do it
 			   for us when the PBX instance that called login finishes */
 			if (!ast_strlen_zero(p->loginchan)) {
+			  if(p && p->chan && p->chan->stream) {
+			    ast_closestream(p->chan->stream);
+			    p->chan->stream = NULL;
+			  }
+			  if(p && p->trans) {
+			    ast_translator_free_path(p->trans);
+			    p->trans = NULL;
+			  }
 				if (p->chan)
 					ast_log(LOG_DEBUG, "Bridge on '%s' being cleared (2)\n", p->chan->name);
 				ast_hangup(p->chan);
@@ -504,6 +517,7 @@
  				if (p->ackcall) {
  					if (option_verbose > 2)
  						ast_verbose(VERBOSE_PREFIX_3 "%s answered, waiting for '#' to acknowledge\n", p->chan->name);
+					p->framecount = 1;
  					/* Don't pass answer along */
  					ast_frfree(f);
  					f = &null_frame;
@@ -518,6 +532,14 @@
  			break;
  		case AST_FRAME_DTMF:
  			if (!p->acknowledged && (f->subclass == '#')) {
+			  if(p && p->chan && p->chan->stream) {
+			    ast_closestream(p->chan->stream);
+			    p->chan->stream = NULL;
+			  }
+			  if(p && p->trans) {
+			    ast_translator_free_path(p->trans);
+			    p->trans = NULL;
+			  }
  				if (option_verbose > 2)
  					ast_verbose(VERBOSE_PREFIX_3 "%s acknowledged\n", p->chan->name);
  				p->acknowledged = 1;
@@ -639,6 +661,7 @@
 static int agent_call(struct ast_channel *ast, char *dest, int timeout)
 {
 	struct agent_pvt *p = ast->tech_pvt;
+	struct ast_frame *f2 = NULL;
 	int res = -1;
 	int newstate=0;
 	ast_mutex_lock(&p->lock);
@@ -716,6 +739,46 @@
 		}
 		res = 0;
 	}
+
+	/* Turn this code into poor man's generator since we are reading frames at the perfect speed 
+	   just open a stream and pass along the frames from the file while we are waiting for 
+	   the agent to press # or the timeout to occur.
+	*/
+	if(p && ! p->acknowledged) {
+		if(p && ! p->acknowledged && p->chan && !ast_strlen_zero(acksound) && p && p->framecount++ > ackwait && 
+		   ! p->trans && ! p->acknowledged && (ast_openstream(p->chan,acksound, p->chan->language))) {
+			if((f2 = ast_readframe(p->chan->stream))) {
+				if(p->chan->readformat && p->chan->readformat != f2->subclass) 
+					p->trans = ast_translator_build_path(p->chan->readformat, f2->subclass);
+			}
+		}
+		if(p && p->framecount > ackwait && ! p->acknowledged && p->chan && p->chan->stream) {
+			if(!f2) {
+				if(!(f2 = ast_readframe(p->chan->stream))) {
+					/* Reset the stream and the ackwait and play the file again if the time comes. */
+					if(p && p->chan && p->chan->stream) {
+						ast_closestream(p->chan->stream);
+						p->chan->stream = NULL;
+					}
+					if(p && p->trans) {
+						ast_translator_free_path(p->trans);
+						p->trans = NULL;
+					}
+					p->framecount = 0;
+				}
+			}
+			if(f2) {
+				if(p && p->trans && f2->subclass != p->chan->readformat)
+					f2 = ast_translate(p->trans, f2, 1);
+				if(f2) {
+					ast_write(p->chan, f2);
+					ast_frfree(f2);
+					f2 = NULL;
+				}
+			}
+		}
+	}
+
 	CLEANUP(ast,p);
 	ast_mutex_unlock(&p->lock);
 	if (newstate)
@@ -746,6 +809,15 @@
 	p->app_sleep_cond = 1;
 	p->acknowledged = 0;
 
+	if(p && p->chan && p->chan->stream) {
+	  ast_stopstream(p->chan);
+	  p->chan->stream = NULL;
+	}
+	if(p && p->trans) {
+	  ast_translator_free_path(p->trans);
+	  p->trans = NULL;
+	}
+
 	/* if they really are hung up then set start to 0 so the test
 	 * later if we're called on an already downed channel
 	 * doesn't cause an agent to be logged out like when
@@ -975,6 +1047,8 @@
 		 * can safely use signals for this purpose. The pselect() needs to be
 		 * implemented in the kernel for this.
 		 */
+		p->trans = NULL;
+		p->framecount = 0;
 		p->app_sleep_cond = 0;
 		if( ast_mutex_trylock(&p->app_lock) )
 		{
@@ -1057,6 +1131,10 @@
 		/* Create the interface list */
 		if (!strcasecmp(v->name, "agent")) {
 			add_agent(v->value, 0);
+		} else if (!strcasecmp(v->name, "acksound")) {
+			strncpy(acksound,v->value,AST_MAX_BUF);
+		} else if (!strcasecmp(v->name, "ackwait")) {
+			ackwait = atoi(v->value);
 		} else if (!strcasecmp(v->name, "group")) {
 			group = ast_get_group(v->value);
 		} else if (!strcasecmp(v->name, "autologoff")) {
