From a8771e39533d967734f31a3f7b0fd790592d4328 Mon Sep 17 00:00:00 2001
From: Richard Mudgett <rmudgett@digium.com>
Date: Thu, 27 Sep 2012 22:33:15 +0000
Subject: [PATCH] Cleanup ast_dtmf_stream()

* Made ast_dtmf_stream() wait after starting the silence generator rather
than before.

* Made ast_dtmf_stream() put the peer in autoservice for the whole time
things are being done to the chan.


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@373966 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 main/app.c | 37 +++++++++++++------------------------
 1 file changed, 13 insertions(+), 24 deletions(-)

diff --git a/main/app.c b/main/app.c
index c8cb29c903..8f90ddf7ee 100644
--- a/main/app.c
+++ b/main/app.c
@@ -731,32 +731,25 @@ int ast_vm_test_destroy_user(const char *context, const char *mailbox)
 int ast_dtmf_stream(struct ast_channel *chan, struct ast_channel *peer, const char *digits, int between, unsigned int duration)
 {
 	const char *ptr;
-	int res = 0;
+	int res;
 	struct ast_silence_generator *silgen = NULL;
 
 	if (!between) {
 		between = 100;
 	}
 
-	if (peer) {
-		res = ast_autoservice_start(peer);
-	}
-
-	if (!res) {
-		res = ast_waitfor(chan, 100);
-	}
-
-	/* ast_waitfor will return the number of remaining ms on success */
-	if (res < 0) {
-		if (peer) {
-			ast_autoservice_stop(peer);
-		}
-		return res;
+	if (peer && ast_autoservice_start(peer)) {
+		return -1;
 	}
 
+	/* Need a quiet time before sending digits. */
 	if (ast_opt_transmit_silence) {
 		silgen = ast_channel_start_silence_generator(chan);
 	}
+	res = ast_safe_sleep(chan, 100);
+	if (res) {
+		goto dtmf_stream_cleanup;
+	}
 
 	for (ptr = digits; *ptr; ptr++) {
 		if (*ptr == 'w') {
@@ -765,11 +758,11 @@ int ast_dtmf_stream(struct ast_channel *chan, struct ast_channel *peer, const ch
 				break;
 			}
 		} else if (strchr("0123456789*#abcdfABCDF", *ptr)) {
-			/* Character represents valid DTMF */
 			if (*ptr == 'f' || *ptr == 'F') {
 				/* ignore return values if not supported by channel */
 				ast_indicate(chan, AST_CONTROL_FLASH);
 			} else {
+				/* Character represents valid DTMF */
 				ast_senddigit(chan, *ptr, duration);
 			}
 			/* pause between digits */
@@ -781,17 +774,13 @@ int ast_dtmf_stream(struct ast_channel *chan, struct ast_channel *peer, const ch
 		}
 	}
 
-	if (peer) {
-		/* Stop autoservice on the peer channel, but don't overwrite any error condition
-		   that has occurred previously while acting on the primary channel */
-		if (ast_autoservice_stop(peer) && !res) {
-			res = -1;
-		}
-	}
-
+dtmf_stream_cleanup:
 	if (silgen) {
 		ast_channel_stop_silence_generator(chan, silgen);
 	}
+	if (peer && ast_autoservice_stop(peer)) {
+		res = -1;
+	}
 
 	return res;
 }
-- 
GitLab