From a76b4fb95336895c975f9c097edb91d12f0c4f92 Mon Sep 17 00:00:00 2001 From: "(no author)" <(no author)@d8a302eb-03bc-478d-80e4-98257eca68ef> Date: Sun, 3 Aug 2014 11:33:54 +0000 Subject: [PATCH] switched to chrono git-svn-id: svn://elaine/murooma/trunk@130 d8a302eb-03bc-478d-80e4-98257eca68ef --- stream.cpp | 14 +++---- stream.h | 3 +- timeUtils.h | 103 ++++++++++++++++++++-------------------------------- 3 files changed, 47 insertions(+), 73 deletions(-) diff --git a/stream.cpp b/stream.cpp index 4d7eaba9..2fd9a427 100644 --- a/stream.cpp +++ b/stream.cpp @@ -1,5 +1,4 @@ #include "stream.h" -#include "timeUtils.h" #include #include #include @@ -53,10 +52,10 @@ void Stream::getSilentPlayerChunk(short* outputBuffer) -timeval Stream::getNextPlayerChunk(short* outputBuffer, int correction) +time_point_ms Stream::getNextPlayerChunk(short* outputBuffer, int correction) { Chunk* chunk = getNextChunk(); - timeval tv = getTimeval(chunk); + time_point_ms tp = timePoint(chunk); if (correction != 0) { @@ -77,8 +76,8 @@ timeval Stream::getNextPlayerChunk(short* outputBuffer, int correction) } } chunk->idx = idx; - if (correction != 0) - std::cerr << "Diff: " << diff_ms(getTimeval(chunk), tv) << "\t" << chunk->idx / PLAYER_CHUNK_MS_SIZE << "\n"; +// if (correction != 0) +// std::cerr << "Diff: " << diff_ms(getTimeval(chunk), tv) << "\t" << chunk->idx / PLAYER_CHUNK_MS_SIZE << "\n"; } else { @@ -126,7 +125,7 @@ timeval Stream::getNextPlayerChunk(short* outputBuffer, int correction) } - return tv; + return tp; } @@ -180,8 +179,7 @@ void Stream::getChunk(short* outputBuffer, double outputBufferDacTime, unsigned } } - timeval tv = getNextPlayerChunk(outputBuffer, correction); - int age = getAge(tv) - bufferMs;// + outputBufferDacTime*1000; + int age = getAge(getNextPlayerChunk(outputBuffer, correction)) - bufferMs;// + outputBufferDacTime*1000; if (outputBufferDacTime < 1) age += outputBufferDacTime*1000; pBuffer->add(age); diff --git a/stream.h b/stream.h index 0f0f4036..da4dbdcc 100644 --- a/stream.h +++ b/stream.h @@ -8,6 +8,7 @@ #include #include "doubleBuffer.h" #include "chunk.h" +#include "timeUtils.h" class Stream @@ -16,7 +17,7 @@ public: Stream(); void addChunk(Chunk* chunk); Chunk* getNextChunk(); - timeval getNextPlayerChunk(short* outputBuffer, int correction = 0); + time_point_ms getNextPlayerChunk(short* outputBuffer, int correction = 0); void getSilentPlayerChunk(short* outputBuffer); void getChunk(short* outputBuffer, double outputBufferDacTime, unsigned long framesPerBuffer); void setBufferLen(size_t bufferLenMs); diff --git a/timeUtils.h b/timeUtils.h index 93c1a340..a197be6e 100644 --- a/timeUtils.h +++ b/timeUtils.h @@ -3,20 +3,50 @@ #include "chunk.h" #include +#include -static std::string timeToStr(const timeval& timestamp) + +typedef std::chrono::time_point time_point_ms; + + +static inline time_point_ms timePoint(const Chunk* chunk) { - char tmbuf[64], buf[64]; - struct tm *nowtm; - time_t nowtime; - nowtime = timestamp.tv_sec; - nowtm = localtime(&nowtime); - strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S", nowtm); - snprintf(buf, sizeof buf, "%s.%06d", tmbuf, (int)timestamp.tv_usec); - return buf; + time_point_ms tp; + return tp + std::chrono::seconds(chunk->tv_sec) + std::chrono::milliseconds(chunk->tv_usec / 1000) + std::chrono::milliseconds(chunk->idx / WIRE_CHUNK_MS_SIZE); } + +template +static inline T getAge(const std::chrono::time_point& time_point) +{ + return std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - time_point); +} + + + +template +static inline T getAge(const Chunk* chunk) +{ + return getAge(timePoint(chunk)); +} + + + +inline static long getAge(const Chunk* chunk) +{ + return getAge(chunk).count(); +} + + + +inline static long getAge(const time_point_ms& time_point) +{ + return getAge(time_point).count(); +} + + + static void addMs(timeval& tv, int ms) { if (ms < 0) @@ -33,51 +63,6 @@ static void addMs(timeval& tv, int ms) } -static timeval getTimeval(const Chunk* chunk) -{ - timeval ts; - ts.tv_sec = chunk->tv_sec; - ts.tv_usec = chunk->tv_usec; - addMs(ts, chunk->idx / WIRE_CHUNK_MS_SIZE); - return ts; -} - - -static void setTimeval(Chunk* chunk, timeval tv) -{ - chunk->tv_sec = tv.tv_sec; - chunk->tv_usec = tv.tv_usec; -} - - -static std::string chunkTime(const Chunk* chunk) -{ - return timeToStr(getTimeval(chunk)); -} - - -static long diff_ms(const timeval& t1, const timeval& t2) -{ - return (((t1.tv_sec - t2.tv_sec) * 1000000) + - (t1.tv_usec - t2.tv_usec))/1000; -} - - -static long getAge(const Chunk* chunk) -{ - timeval now; - gettimeofday(&now, NULL); - return diff_ms(now, getTimeval(chunk)); -} - - -static long getAge(const timeval& tv) -{ - timeval now; - gettimeofday(&now, NULL); - return diff_ms(now, tv); -} - static long getTickCount() { @@ -88,16 +73,6 @@ static long getTickCount() -/* -static void addMs(Chunk* chunk, int ms) -{ - timeval tv = getTimeval(chunk); - addMs(tv, ms); - chunk->tv_sec = tv.tv_sec; - chunk->tv_usec = tv.tv_usec; -} -*/ - #endif