summaryrefslogtreecommitdiff
path: root/res/res_musiconhold.c
diff options
context:
space:
mode:
authorKevin P. Fleming <kpfleming@digium.com>2005-07-15 23:00:47 +0000
committerKevin P. Fleming <kpfleming@digium.com>2005-07-15 23:00:47 +0000
commit22b0f5d30691e94b18e152ec44ed90a23f55b266 (patch)
tree896015cada90b49858e1bf6503d9583704a56179 /res/res_musiconhold.c
parent60cd1fa57deeb80b7a564fd81194797b503d4846 (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: https://origsvn.digium.com/svn/asterisk/trunk@6146 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'res/res_musiconhold.c')
-rwxr-xr-xres/res_musiconhold.c49
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;