Update of /usr/cvsroot/asterisk/res
In directory mongoose.digium.com:/tmp/cvs-serv13236/res

Modified Files:
        res_agi.c res_features.c res_musiconhold.c 
Log Message:
add a library of timeval manipulation functions, and change a large number of 
usses to use the new functions (bug #4504)


Index: res_agi.c
===================================================================
RCS file: /usr/cvsroot/asterisk/res/res_agi.c,v
retrieving revision 1.43
retrieving revision 1.44
diff -u -d -r1.43 -r1.44
--- res_agi.c   15 Jul 2005 22:06:15 -0000      1.43
+++ res_agi.c   15 Jul 2005 23:00:47 -0000      1.44
@@ -787,7 +787,7 @@
 {
        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 @@
 
        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 @@
                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 @@
                                break;
                        }
                        ast_frfree(f);
-                       gettimeofday(&tv, NULL);
                        if (gotsilence)
                                break;
                }
@@ -938,8 +938,7 @@
                }               
                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);

Index: res_features.c
===================================================================
RCS file: /usr/cvsroot/asterisk/res/res_features.c,v
retrieving revision 1.61
retrieving revision 1.62
diff -u -d -r1.61 -r1.62
--- res_features.c      11 Jul 2005 20:46:25 -0000      1.61
+++ res_features.c      15 Jul 2005 23:00:47 -0000      1.62
@@ -295,7 +295,7 @@
                                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 @@
                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 @@
                                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 @@
                                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 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 @@
        }
        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 @@
                                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 @@
                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();

Index: res_musiconhold.c
===================================================================
RCS file: /usr/cvsroot/asterisk/res/res_musiconhold.c,v
retrieving revision 1.64
retrieving revision 1.65
diff -u -d -r1.64 -r1.65
--- res_musiconhold.c   12 Jul 2005 18:57:20 -0000      1.64
+++ res_musiconhold.c   15 Jul 2005 23:00:47 -0000      1.65
@@ -444,17 +444,10 @@
        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 @@
                        /* 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;

_______________________________________________
Asterisk-Cvs mailing list
[email protected]
http://lists.digium.com/mailman/listinfo/asterisk-cvs

Reply via email to