From 44af00a28f4e02e035039805d82edc4677165cfa Mon Sep 17 00:00:00 2001
From: "Kevin P. Fleming" <kpfleming@digium.com>
Date: Fri, 10 Jul 2009 16:00:44 +0000
Subject: [PATCH] Eliminate extraneous LOG_DEBUG messages generated by app_fax.

The transmit_audio() and transmit_t38() functions in app_fax have processing
loops that are supposed to wait for frames to arrive on the channel and then
handle them, but they also have short timeouts so that the loops can have
watchdog timers and do other required processing. This commit changes the loops
to not actually call ast_read() and attempt to process the returned frame
unless a frame actually arrived, eliminating hundreds of LOG_DEBUG messages
and slightly improving performance.



git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@205780 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 apps/app_fax.c | 75 +++++++++++++++++++++++++++-----------------------
 1 file changed, 40 insertions(+), 35 deletions(-)

diff --git a/apps/app_fax.c b/apps/app_fax.c
index 83ae2cded5..6c132c1621 100644
--- a/apps/app_fax.c
+++ b/apps/app_fax.c
@@ -432,14 +432,28 @@ static int transmit_audio(fax_session *s)
 	ast_activate_generator(s->chan, &generator, &fax);
 
 	while (!s->finished) {
-		res = ast_waitfor(s->chan, 20);
-		if (res < 0)
+		inf = NULL;
+
+		if ((res = ast_waitfor(s->chan, 20)) < 0) {
 			break;
-		else if (res > 0)
-			res = 0;
+		}
+
+		/* if nothing arrived, check the watchdog timers */
+		if (res == 0) {
+			now = ast_tvnow();
+			if (ast_tvdiff_sec(now, start) > WATCHDOG_TOTAL_TIMEOUT || ast_tvdiff_sec(now, state_change) > WATCHDOG_STATE_TIMEOUT) {
+				ast_log(LOG_WARNING, "It looks like we hung. Aborting.\n");
+				res = -1;
+				break;
+			} else {
+				/* timers have not triggered, loop around to wait
+				 * again
+				 */
+				continue;
+			}
+		}
 
-		inf = ast_read(s->chan);
-		if (inf == NULL) {
+		if (!(inf = ast_read(s->chan))) {
 			ast_debug(1, "Channel hangup\n");
 			res = -1;
 			break;
@@ -473,7 +487,7 @@ static int transmit_audio(fax_session *s)
 
 
 		/* Check the frame type. Format also must be checked because there is a chance
-		   that a frame in old format was already queued before we set chanel format
+		   that a frame in old format was already queued before we set channel format
 		   to slinear so it will still be received by ast_read */
 		if (inf->frametype == AST_FRAME_VOICE && inf->subclass == AST_FORMAT_SLINEAR) {
 			if (fax_rx(&fax, inf->data.ptr, inf->samples) < 0) {
@@ -482,8 +496,6 @@ static int transmit_audio(fax_session *s)
 				res = -1;
 				break;
 			}
-
-			/* Watchdog */
 			if (last_state != t30state->state) {
 				state_change = ast_tvnow();
 				last_state = t30state->state;
@@ -517,15 +529,6 @@ static int transmit_audio(fax_session *s)
 		}
 
 		ast_frfree(inf);
-		inf = NULL;
-
-		/* Watchdog */
-		now = ast_tvnow();
-		if (ast_tvdiff_sec(now, start) > WATCHDOG_TOTAL_TIMEOUT || ast_tvdiff_sec(now, state_change) > WATCHDOG_STATE_TIMEOUT) {
-			ast_log(LOG_WARNING, "It looks like we hung. Aborting.\n");
-			res = -1;
-			break;
-		}
 	}
 
 	ast_debug(1, "Loop finished, res=%d\n", res);
@@ -617,19 +620,31 @@ static int transmit_t38(fax_session *s)
 	now = start = state_change = ast_tvnow();
 
 	while (!s->finished) {
-
-		res = ast_waitfor(s->chan, 20);
-		if (res < 0)
+		inf = NULL;
+		if ((res = ast_waitfor(s->chan, 20)) < 0) {
 			break;
-		else if (res > 0)
-			res = 0;
+		}
 
 		last_frame = now;
 		now = ast_tvnow();
+		/* if nothing arrived, check the watchdog timers */
+		if (res == 0) {
+			if (ast_tvdiff_sec(now, start) > WATCHDOG_TOTAL_TIMEOUT || ast_tvdiff_sec(now, state_change) > WATCHDOG_STATE_TIMEOUT) {
+				ast_log(LOG_WARNING, "It looks like we hung. Aborting.\n");
+				res = -1;
+				break;
+			} else {
+				/* timers have not triggered, loop around to wait
+				 * again
+				 */
+				t38_terminal_send_timeout(&t38, ast_tvdiff_us(now, last_frame) / (1000000 / 8000));
+				continue;
+			}
+		}
+
 		t38_terminal_send_timeout(&t38, ast_tvdiff_us(now, last_frame) / (1000000 / 8000));
 
-		inf = ast_read(s->chan);
-		if (inf == NULL) {
+		if (!(inf = ast_read(s->chan))) {
 			ast_debug(1, "Channel hangup\n");
 			res = -1;
 			break;
@@ -639,8 +654,6 @@ static int transmit_t38(fax_session *s)
 
 		if (inf->frametype == AST_FRAME_MODEM && inf->subclass == AST_MODEM_T38) {
 			t38_core_rx_ifp_packet(t38state, inf->data.ptr, inf->datalen, inf->seqno);
-
-			/* Watchdog */
 			if (last_state != t30state->state) {
 				state_change = ast_tvnow();
 				last_state = t30state->state;
@@ -654,14 +667,6 @@ static int transmit_t38(fax_session *s)
 		}
 
 		ast_frfree(inf);
-		inf = NULL;
-
-		/* Watchdog */
-		if (ast_tvdiff_sec(now, start) > WATCHDOG_TOTAL_TIMEOUT || ast_tvdiff_sec(now, state_change) > WATCHDOG_STATE_TIMEOUT) {
-			ast_log(LOG_WARNING, "It looks like we hung. Aborting.\n");
-			res = -1;
-			break;
-		}
 	}
 
 	ast_debug(1, "Loop finished, res=%d\n", res);
-- 
GitLab