diff --git a/core/AmMediaProcessor.cpp b/core/AmMediaProcessor.cpp index a35e6f06..97c60019 100644 --- a/core/AmMediaProcessor.cpp +++ b/core/AmMediaProcessor.cpp @@ -230,31 +230,27 @@ void AmMediaProcessorThread::on_stop() void AmMediaProcessorThread::run() { stop_requested = false; - struct timeval now,next_tick,diff,tick; // wallclock time unsigned long long ts = 0;//4294417296; - tick.tv_sec = 0; - tick.tv_usec = 1000*WC_INC_MS; + // everything else in microseconds + uint64_t tick = 1000*WC_INC_MS; - gettimeofday(&now,NULL); - timeradd(&tick,&now,&next_tick); + uint64_t now = gettimeofday_us(); + + uint64_t next_tick = now + tick; while(!stop_requested.get()){ - gettimeofday(&now,NULL); - - if(timercmp(&now,&next_tick,<)){ + now = gettimeofday_us(); - struct timespec sdiff,rem; - timersub(&next_tick,&now,&diff); + if(now < next_tick){ - sdiff.tv_sec = diff.tv_sec; - sdiff.tv_nsec = diff.tv_usec * 1000; + uint64_t diff = next_tick - now; - if(sdiff.tv_nsec > 2000000) // 2 ms - nanosleep(&sdiff,&rem); + if(diff > 2000) // 2 ms + usleep(diff); } processAudio(ts); @@ -262,7 +258,7 @@ void AmMediaProcessorThread::run() processDtmfEvents(); ts = (ts + WC_INC) & WALLCLOCK_MASK; - timeradd(&tick,&next_tick,&next_tick); + next_tick += tick; } } diff --git a/core/AmUtils.h b/core/AmUtils.h index a26444ad..9c5a8388 100644 --- a/core/AmUtils.h +++ b/core/AmUtils.h @@ -34,6 +34,8 @@ #include #include #include +#include +#include #include using std::string; @@ -290,6 +292,13 @@ void cvt_hex(HASH bin, HASHHEX hex); /** get an MD5 hash of a string */ string calculateMD5(const string& input); +/** return current time (unixtime / wall clock) as scalar microseconds */ +inline uint64_t gettimeofday_us() { + struct timeval now_tv; + gettimeofday(&now_tv,NULL); + return now_tv.tv_sec * 1000000LL + now_tv.tv_usec; +} + #endif // Local Variables: