Index: main/rtp.c =================================================================== --- main/rtp.c (revision 191953) +++ main/rtp.c (working copy) @@ -73,7 +73,7 @@ #define RTP_MTU 1200 -#define DEFAULT_DTMF_TIMEOUT 3000 /*!< samples */ +#define DEFAULT_DTMF_TIMEOUT (150 * (8000 / 1000)) /*!< samples */ static int dtmftimeout = DEFAULT_DTMF_TIMEOUT; @@ -139,7 +139,9 @@ /* DTMF Reception Variables */ char resp; unsigned int lastevent; - int dtmfcount; + unsigned int dtmf_begin; + unsigned int dtmf_duration; + unsigned int dtmf_timeout; /* DTMF Transmission Variables */ unsigned int lastdigitts; char sending_digit; /*!< boolean - are we sending digits */ @@ -689,7 +691,7 @@ f = send_dtmf(rtp, AST_FRAME_DTMF_END); } rtp->resp = resp; - rtp->dtmfcount = dtmftimeout; + rtp->dtmf_timeout = 0; return f; } @@ -745,23 +747,53 @@ if (ast_test_flag(rtp, FLAG_DTMF_COMPENSATE)) { if ((rtp->lastevent != timestamp) || (rtp->resp && rtp->resp != resp)) { rtp->resp = resp; - rtp->dtmfcount = 0; + rtp->dtmf_timeout = 0; f = send_dtmf(rtp, AST_FRAME_DTMF_END); f->len = 0; rtp->lastevent = timestamp; } } else { - if ((!(rtp->resp) && (!(event_end & 0x80))) || (rtp->resp && rtp->resp != resp)) { - rtp->resp = resp; - f = send_dtmf(rtp, AST_FRAME_DTMF_BEGIN); - rtp->dtmfcount = dtmftimeout; - } else if ((event_end & 0x80) && (rtp->lastevent != seqno) && rtp->resp) { - f = send_dtmf(rtp, AST_FRAME_DTMF_END); - f->len = ast_tvdiff_ms(ast_samp2tv(samples, 8000), ast_tv(0, 0)); /* XXX hard coded 8kHz */ - rtp->resp = 0; - rtp->dtmfcount = 0; - rtp->lastevent = seqno; + /* The duration parameter measures the complete + duration of the event (from the beginning). RFC2833 */ + + if (event_end & 0x80) { + /* End event */ + if ((rtp->lastevent != seqno) && rtp->resp) { + f = send_dtmf(rtp, AST_FRAME_DTMF_END); + f->len = ast_tvdiff_ms(ast_samp2tv(samples, 8000), ast_tv(0, 0)); +ast_log(LOG_DEBUG, "normal end, dur=%ld\n", f->len); + rtp->resp = 0; + rtp->dtmf_begin = rtp->dtmf_duration = rtp->dtmf_timeout = 0; + } + } else { + /* Begin/continuation */ + + if (rtp->resp && rtp->resp != resp) { + /* Another digit already began. End it */ + f = send_dtmf(rtp, AST_FRAME_DTMF_END); + f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, 8000), ast_tv(0, 0)); +ast_log(LOG_DEBUG, "forced end, dur=%ld\n", f->len); + rtp->resp = 0; + rtp->dtmf_begin = rtp->dtmf_timeout = 0; + } else { + /* Either continuation of last digit or begin of a new one */ + rtp->dtmf_duration = samples; + rtp->dtmf_timeout = timestamp + rtp->dtmf_duration + dtmftimeout; + } + + + if (!rtp->resp) { + /*New digit begin */ + rtp->resp = resp; + f = send_dtmf(rtp, AST_FRAME_DTMF_BEGIN); + rtp->dtmf_begin = timestamp; +ast_log(LOG_DEBUG, "START, dur=%ld\n", f->len); + } + +ast_log(LOG_DEBUG, "..... begin=%u, timeout=%u, duration=%u\n", rtp->dtmf_begin,rtp->dtmf_timeout,rtp->dtmf_duration); } + + rtp->lastevent = seqno; } return f; @@ -1291,17 +1323,16 @@ rtp->rxseqno = seqno; - if (rtp->dtmfcount) { - rtp->dtmfcount -= (timestamp - rtp->lastrxts); + if (rtp->dtmf_timeout && rtp->dtmf_timeout < timestamp) { + rtp->dtmf_timeout = 0; - if (rtp->dtmfcount < 0) { - rtp->dtmfcount = 0; - } - - if (rtp->resp && !rtp->dtmfcount) { +ast_log(LOG_DEBUG, "TIMEOUT now=%u, begin=%u, timeout=%u\n", timestamp,rtp->dtmf_begin,rtp->dtmf_timeout); + if (rtp->resp) { struct ast_frame *f; f = send_dtmf(rtp, AST_FRAME_DTMF_END); + f->len = rtp->dtmf_duration; rtp->resp = 0; + rtp->dtmf_begin = rtp->dtmf_timeout = 0; return f; } } @@ -2085,7 +2116,8 @@ rtp->lastevent = 0; rtp->lasttxformat = 0; rtp->lastrxformat = 0; - rtp->dtmfcount = 0; + rtp->dtmf_begin = 0; + rtp->dtmf_timeout = 0; rtp->seqno = 0; rtp->rxseqno = 0; } @@ -3848,7 +3880,7 @@ } if ((s = ast_variable_retrieve(cfg, "general", "dtmftimeout"))) { dtmftimeout = atoi(s); - if ((dtmftimeout < 0) || (dtmftimeout > 20000)) { + if ((dtmftimeout < 0) || (dtmftimeout > 64000)) { ast_log(LOG_WARNING, "DTMF timeout of '%d' outside range, using default of '%d' instead\n", dtmftimeout, DEFAULT_DTMF_TIMEOUT); dtmftimeout = DEFAULT_DTMF_TIMEOUT;