Description: This patch ensures that JITTER in RTP packets is within acceptable limits (50 ms) Author: Nitesh Bansal --- a/res/res_rtp_asterisk.c +++ b/res/res_rtp_asterisk.c @@ -1684,6 +1684,26 @@ } } + +static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery) +{ + struct timeval t; + long ms; + + if (ast_tvzero(rtp->txcore)) { + rtp->txcore = ast_tvnow(); + rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000; + } + + t = (delivery && !ast_tvzero(*delivery)) ? *delivery : ast_tvnow(); + if ((ms = ast_tvdiff_ms(t, rtp->txcore)) < 0) { + ms = 0; + } + rtp->txcore = t; + + return (unsigned int) ms; +} + static int ast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data) @@ -1908,6 +1928,7 @@ rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000)); rtp->send_duration = 160; + rtp->lastts += calc_txstamp(rtp, NULL)*8; rtp->lastdigitts = rtp->lastts + rtp->send_duration; /* Create the actual packet that we will be sending */ @@ -1988,6 +2009,7 @@ /* And now we increment some values for the next time we swing by */ rtp->seqno++; rtp->send_duration += 160; + rtp->lastts += calc_txstamp(rtp, NULL)*8; return 0; } @@ -2064,7 +2086,7 @@ } /* Oh and we can't forget to turn off the stuff that says we are sending DTMF */ - rtp->lastts += rtp->send_duration; + rtp->lastts += calc_txstamp(rtp, NULL)*8; rtp->sending_digit = 0; rtp->send_digit = 0; @@ -2113,25 +2135,6 @@ return; } -static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery) -{ - struct timeval t; - long ms; - - if (ast_tvzero(rtp->txcore)) { - rtp->txcore = ast_tvnow(); - rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000; - } - - t = (delivery && !ast_tvzero(*delivery)) ? *delivery : ast_tvnow(); - if ((ms = ast_tvdiff_ms(t, rtp->txcore)) < 0) { - ms = 0; - } - rtp->txcore = t; - - return (unsigned int) ms; -} - static void timeval2ntp(struct timeval tv, unsigned int *msw, unsigned int *lsw) { unsigned int sec, usec, frac; --- a/main/translate.c +++ b/main/translate.c @@ -540,6 +540,11 @@ /* Predict next outgoing timestamp from samples in this frame. */ path->nextout = ast_tvadd(path->nextout, ast_samp2tv(out->samples, ast_format_rate(&out->subclass.format))); + if(f->samples != out->samples && ast_test_flag(out,AST_FRFLAG_HAS_TIMING_INFO)) + { + ast_log(LOG_DEBUG, "Sample size different %u vs %u\n", f->samples, out->samples); + ast_clear_flag(out,AST_FRFLAG_HAS_TIMING_INFO); + } } else { out->delivery = ast_tv(0, 0); ast_set2_flag(out, has_timing_info, AST_FRFLAG_HAS_TIMING_INFO);