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
This commit is contained in:
Kevin P. Fleming
2005-07-15 23:00:47 +00:00
parent 60cd1fa57d
commit 22b0f5d306
39 changed files with 420 additions and 635 deletions

View File

@@ -787,7 +787,7 @@ static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, char
{
struct ast_filestream *fs;
struct ast_frame *f;
struct timeval tv, start;
struct timeval start;
long sample_offset = 0;
int res = 0;
int ms;
@@ -854,7 +854,9 @@ static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, char
if (!res)
res = ast_waitstream(chan, argv[4]);
if (!res) {
if (res) {
fdprintf(agi->fd, "200 result=%d (randomerror) endpos=%ld\n", res, sample_offset);
} else {
fs = ast_writefile(argv[2], argv[3], NULL, O_CREAT | O_WRONLY | (sample_offset ? O_APPEND : 0), 0, 0644);
if (!fs) {
res = -1;
@@ -870,9 +872,8 @@ static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, char
ast_seekstream(fs, sample_offset, SEEK_SET);
ast_truncstream(fs);
gettimeofday(&start, NULL);
gettimeofday(&tv, NULL);
while ((ms < 0) || (((tv.tv_sec - start.tv_sec) * 1000 + (tv.tv_usec - start.tv_usec)/1000) < ms)) {
start = ast_tvnow();
while ((ms < 0) || ast_tvdiff_ms(ast_tvnow(), start) < ms) {
res = ast_waitfor(chan, -1);
if (res < 0) {
ast_closestream(fs);
@@ -926,7 +927,6 @@ static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, char
break;
}
ast_frfree(f);
gettimeofday(&tv, NULL);
if (gotsilence)
break;
}
@@ -938,8 +938,7 @@ static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, char
}
fdprintf(agi->fd, "200 result=%d (timeout) endpos=%ld\n", res, sample_offset);
ast_closestream(fs);
} else
fdprintf(agi->fd, "200 result=%d (randomerror) endpos=%ld\n", res, sample_offset);
}
if (silence > 0) {
res = ast_set_read_format(chan, rfmt);

View File

@@ -295,7 +295,7 @@ int ast_park_call(struct ast_channel *chan, struct ast_channel *peer, int timeou
ast_indicate(pu->chan, AST_CONTROL_HOLD);
ast_moh_start(pu->chan, NULL);
}
gettimeofday(&pu->start, NULL);
pu->start = ast_tvnow();
pu->parkingnum = x;
if (timeout > 0)
pu->parkingtime = timeout;
@@ -938,7 +938,7 @@ static struct ast_channel *ast_feature_request_and_dial(struct ast_channel *call
ast_set_callerid(chan, cid_num, cid_name, cid_num);
if (!ast_call(chan, data, timeout)) {
struct timeval started, ended;
struct timeval started;
int x, len = 0;
char *disconnect_code = NULL, *dialed_code = NULL;
@@ -955,7 +955,7 @@ static struct ast_channel *ast_feature_request_and_dial(struct ast_channel *call
break;
}
x = 0;
gettimeofday(&started, NULL);
started = ast_tvnow();
to = timeout;
while (!ast_check_hangup(caller) && timeout && (chan->_state != AST_STATE_UP)) {
monitor_chans[0] = caller;
@@ -963,8 +963,7 @@ static struct ast_channel *ast_feature_request_and_dial(struct ast_channel *call
active_channel = ast_waitfor_n(monitor_chans, 2, &to);
/* see if the timeout has been violated */
gettimeofday(&ended,NULL);
if(ast_tvdiff_ms(&ended, &started) > timeout) {
if(ast_tvdiff_ms(ast_tvnow(), started) > timeout) {
state = AST_CONTROL_UNHOLD;
ast_log(LOG_NOTICE, "We exceeded our AT-timeout\n");
break; /*doh! timeout*/
@@ -1107,7 +1106,7 @@ int ast_bridge_call(struct ast_channel *chan,struct ast_channel *peer,struct ast
int hasfeatures=0;
int hadfeatures=0;
struct ast_option_header *aoh;
struct timeval start, end;
struct timeval start;
struct ast_bridge_config backup_config;
int allowdisconnect_in,allowdisconnect_out,allowredirect_in,allowredirect_out;
char *monitor_exec;
@@ -1157,13 +1156,11 @@ int ast_bridge_call(struct ast_channel *chan,struct ast_channel *peer,struct ast
}
for (;;) {
if (config->timelimit)
gettimeofday(&start, NULL);
start = ast_tvnow();
res = ast_channel_bridge(chan,peer,config,&f, &who);
if (config->timelimit) {
/* Update time limit for next pass */
gettimeofday(&end, NULL);
diff = (end.tv_sec - start.tv_sec) * 1000;
diff += (end.tv_usec - start.tv_usec) / 1000;
diff = ast_tvdiff_ms(ast_tvnow(), start);
config->timelimit -= diff;
if (hasfeatures) {
/* Running on backup config, meaning a feature might be being
@@ -1344,8 +1341,7 @@ static void *do_parking_thread(void *ignore)
pu = pu->next;
continue;
}
gettimeofday(&tv, NULL);
tms = (tv.tv_sec - pu->start.tv_sec) * 1000 + (tv.tv_usec - pu->start.tv_usec) / 1000;
tms = ast_tvdiff_ms(ast_tvnow(), pu->start);
if (tms > pu->parkingtime) {
/* Stop music on hold */
ast_moh_stop(pu->chan);
@@ -1486,8 +1482,7 @@ std: for (x=0; x<AST_MAX_FDS; x++) {
ast_mutex_unlock(&parking_lock);
rfds = nrfds;
efds = nefds;
tv.tv_sec = ms / 1000;
tv.tv_usec = (ms % 1000) * 1000;
tv = ast_samp2tv(ms, 1000);
/* Wait for something to happen */
ast_select(max + 1, &rfds, NULL, &efds, (ms > -1) ? &tv : NULL);
pthread_testcancel();

View File

@@ -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;
tv = tv_tmp;
}
if (tv.tv_usec > 1000000) {
tv.tv_sec++;
tv.tv_usec-= 1000000;
}
if (delay > 0)
usleep(delay * 1000);
res = 800; /* 800 samples */
res = 8 * MOH_MS_INTERVAL; /* 8 samples per millisecond */
}
if (!class->members)
continue;