--- asterisk-13.19.0.orig/apps/app_amd.c 2018-01-11 11:44:54.000000000 -0500 +++ asterisk-13.19.0/apps/app_amd.c 2018-02-06 12:56:02.112771181 -0500 @@ -174,6 +174,7 @@ int silenceDuration = 0; int iTotalTime = 0; int iWordsCount = 0; + int ms = 0; int currentState = STATE_IN_WORD; int consecutiveVoiceDuration = 0; char amdCause[256] = "", amdStatus[256] = ""; @@ -279,6 +280,10 @@ /* Now we go into a loop waiting for frames from the channel */ while ((res = ast_waitfor(chan, 2 * maxWaitTimeForFrame)) > -1) { + /* Figure out how long we waited */ + if ( res > 0 ) { + ms = 2 * maxWaitTimeForFrame - res; + } /* If we fail to read in a frame, that means they hung up */ if (!(f = ast_read(chan))) { @@ -289,15 +294,22 @@ break; } - if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_NULL || f->frametype == AST_FRAME_CNG) { - /* If the total time exceeds the analysis time then give up as we are not too sure */ + if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_CNG) { + /* figure out how long the frame is in ms */ if (f->frametype == AST_FRAME_VOICE) { framelength = (ast_codec_samples_count(f) / DEFAULT_SAMPLES_PER_MS); } else { - framelength = 2 * maxWaitTimeForFrame; + framelength = ms; } - iTotalTime += framelength; + + if ( f->frametype == AST_FRAME_VOICE ) { + ast_debug(1, "AMD: Channel [%s] frametype [AST_FRAME_VOICE] iTotalTime [%d] framelength [%d] totalAnalysisTime [%d]\n", ast_channel_name(chan), iTotalTime, framelength, totalAnalysisTime ); + } else { + ast_debug(1, "AMD: Channel [%s] frametype [AST_FRAME_CNG] iTotalTime [%d] framelength [%d] totalAnalysisTime [%d]\n", ast_channel_name(chan), iTotalTime, framelength, totalAnalysisTime ); + } + + /* If the total time exceeds the analysis time then give up as we are not too sure */ if (iTotalTime >= totalAnalysisTime) { ast_verb(3, "AMD: Channel [%s]. Too long...\n", ast_channel_name(chan)); ast_frfree(f); @@ -308,7 +320,7 @@ /* Feed the frame of audio into the silence detector and see if we get a result */ if (f->frametype != AST_FRAME_VOICE) - dspsilence += 2 * maxWaitTimeForFrame; + dspsilence += framelength; else { dspsilence = 0; ast_dsp_silence(silenceDetector, f, &dspsilence);