diff options
author | kpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b> | 2005-07-15 23:00:47 +0000 |
---|---|---|
committer | kpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b> | 2005-07-15 23:00:47 +0000 |
commit | ff15e0fa53156b9994da3f726bfe91232de6195a (patch) | |
tree | 896015cada90b49858e1bf6503d9583704a56179 /res/res_musiconhold.c | |
parent | c05cf07847cb384ba66aa29d5eda580eda413ffb (diff) |
add a library of timeval manipulation functions, and change a large number of usses to use the new functions (bug #4504)
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@6146 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'res/res_musiconhold.c')
-rwxr-xr-x | res/res_musiconhold.c | 49 |
1 files changed, 11 insertions, 38 deletions
diff --git a/res/res_musiconhold.c b/res/res_musiconhold.c index 07ba5fa08..351081f46 100755 --- a/res/res_musiconhold.c +++ b/res/res_musiconhold.c @@ -444,17 +444,10 @@ static void *monmp3thread(void *data) char buf[8192]; short sbuf[8192]; int res, res2; - struct timeval tv; - struct timeval tv_tmp; - long error_sec, error_usec; - long delay; + struct timeval tv, tv_tmp; - tv_tmp.tv_sec = 0; - tv_tmp.tv_usec = 0; tv.tv_sec = 0; tv.tv_usec = 0; - error_sec = 0; - error_usec = 0; for(;/* ever */;) { /* Spawn mp3 player if it's not there */ if (class->srcfd < 0) { @@ -468,40 +461,20 @@ static void *monmp3thread(void *data) /* Pause some amount of time */ res = read(class->pseudofd, buf, sizeof(buf)); } else { + long delta; /* Reliable sleep */ - if (gettimeofday(&tv_tmp, NULL) < 0) { - ast_log(LOG_NOTICE, "gettimeofday() failed!\n"); - return NULL; - } - if (((unsigned long)(tv.tv_sec) > 0)&&((unsigned long)(tv.tv_usec) > 0)) { - if ((unsigned long)(tv_tmp.tv_usec) < (unsigned long)(tv.tv_usec)) { - tv_tmp.tv_usec += 1000000; - tv_tmp.tv_sec -= 1; - } - error_sec = (unsigned long)(tv_tmp.tv_sec) - (unsigned long)(tv.tv_sec); - error_usec = (unsigned long)(tv_tmp.tv_usec) - (unsigned long)(tv.tv_usec); - } else { - error_sec = 0; - error_usec = 0; - } - if (error_sec * 1000 + error_usec / 1000 < MOH_MS_INTERVAL) { - tv.tv_sec = tv_tmp.tv_sec + (MOH_MS_INTERVAL/1000 - error_sec); - tv.tv_usec = tv_tmp.tv_usec + ((MOH_MS_INTERVAL % 1000) * 1000 - error_usec); - delay = (MOH_MS_INTERVAL/1000 - error_sec) * 1000 + - ((MOH_MS_INTERVAL % 1000) * 1000 - error_usec) / 1000; + tv_tmp = ast_tvnow(); + if (ast_tvzero(tv)) + tv = tv_tmp; + delta = ast_tvdiff_ms(tv_tmp, tv); + if (delta < MOH_MS_INTERVAL) { /* too early */ + tv = ast_tvadd(tv, ast_samp2tv(MOH_MS_INTERVAL, 1000)); /* next deadline */ + usleep(1000 * (MOH_MS_INTERVAL - delta)); } else { ast_log(LOG_NOTICE, "Request to schedule in the past?!?!\n"); - tv.tv_sec = tv_tmp.tv_sec; - tv.tv_usec = tv_tmp.tv_usec; - delay = 0; - } - if (tv.tv_usec > 1000000) { - tv.tv_sec++; - tv.tv_usec-= 1000000; + tv = tv_tmp; } - if (delay > 0) - usleep(delay * 1000); - res = 800; /* 800 samples */ + res = 8 * MOH_MS_INTERVAL; /* 8 samples per millisecond */ } if (!class->members) continue; |