From fe79a78efe929c42cb10c83e407b7e8969d5ba6f Mon Sep 17 00:00:00 2001 From: "(no author)" <(no author)@d8a302eb-03bc-478d-80e4-98257eca68ef> Date: Thu, 25 Sep 2014 21:21:32 +0000 Subject: [PATCH] chonos stuff git-svn-id: svn://elaine/murooma/trunk@297 d8a302eb-03bc-478d-80e4-98257eca68ef --- client/stream.cpp | 32 ++++++++++++++++++-------------- common/timeDefs.h | 1 + message/pcmChunk.cpp | 27 +-------------------------- message/pcmChunk.h | 40 ++++++++++++---------------------------- 4 files changed, 32 insertions(+), 68 deletions(-) diff --git a/client/stream.cpp b/client/stream.cpp index 8885031b..6fe324df 100644 --- a/client/stream.cpp +++ b/client/stream.cpp @@ -36,7 +36,7 @@ void Stream::clearChunks() void Stream::addChunk(PcmChunk* chunk) { - while (chunks.size() * chunk->getDuration() > 10000) + while (chunks.size() * chunk->duration().count() > 10000) chunks.pop(); chunks.push(shared_ptr(chunk)); // cout << "new chunk: " << chunk->getDuration() << ", Chunks: " << chunks.size() << "\n"; @@ -81,10 +81,10 @@ time_point_hrc Stream::seek(long ms) return chunk->timePoint(); // time_point_ms tp = chunk->timePoint(); - while (ms > chunk->getTimeLeft()) + while (ms > chunk->duration().count()) { chunk = chunks.pop(); - ms -= min(ms, (long)chunk->getTimeLeft()); + ms -= min(ms, (long)chunk->durationLeft().count()); } chunk->seek(ms * format.msRate()); return chunk->timePoint(); @@ -97,6 +97,7 @@ time_point_hrc Stream::getNextPlayerChunk(void* outputBuffer, unsigned long fram if (!chunks.try_pop(chunk, chronos::msec(timeout))) throw 0; +//cout << "duration: " << chunk->duration().count() << ", " << chunk->duration().count() << ", " << chunk->duration().count() << "\n"; time_point_hrc tp = chunk->timePoint(); int read = 0; int toRead = framesPerBuffer + correction*format.msRate(); @@ -168,7 +169,18 @@ bool Stream::getPlayerChunk(void* outputBuffer, chronos::usec outputBufferDacTim lastTick = currentTick; ticks = currentTick - lastTick; lastTick = currentTick; - +/* +pShortBuffer->full() && (abs(shortMedian) > 5): 25 +Sleep: 25 +Chunk: 25 25 25 25 23 105941 +Chunk: 25 25 25 25 55 107210 +Chunk: 25 25 25 25 88 104671 +pShortBuffer->full() && (abs(shortMedian) > 5): 25 +Sleep: 25 +Chunk: 25 25 25 25 20 99614 +Chunk: 25 25 25 25 53 96802 +Chunk: 25 25 25 25 86 117732 +*/ int correction = 0; if (sleep != 0) { @@ -188,23 +200,16 @@ bool Stream::getPlayerChunk(void* outputBuffer, chronos::usec outputBufferDacTim } else if (sleep > msBuffer/2) { - /* cout << "Sleep " << sleep; - time_point_ms ms(std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch())); - ms -= std::chrono::milliseconds((long int)(bufferMs - outputBufferDacTime)); - cout << "\nms: " << Chunk::getAge(ms) << "\t chunk: " << chunk->getAge() << "\n"; - sleep = Chunk::getAge(seekTo(ms)) - bufferMs + outputBufferDacTime; - cout << " after: " << sleep << "\n"; - */ if (!chunk) if (!chunks.try_pop(chunk, chronos::msec(timeout))) throw 0; - while (sleep > chunk->getDuration()) + while (sleep > chunk->duration().count()) { + cout << "sleep > chunk->getDuration(): " << sleep << " > " << chunk->duration().count() << ", chunks: " << chunks.size() << ", out: " << outputBufferDacTime.count() << ", needed: " << msBuffer << "\n"; if (!chunks.try_pop(chunk, chronos::msec(timeout))) throw 0; msec age = std::chrono::duration_cast(TimeProvider::serverNow() - chunk->timePoint() - bufferMs + outputBufferDacTime); sleep = age.count(); -// cout << "chunk->getAge() > chunk->getDuration(): " << chunk->getAge() - bufferMs + outputBufferDacTime << " > " << chunk->getDuration() << ", chunks: " << chunks.size() << ", out: " << outputBufferDacTime << ", needed: " << msBuffer << ", sleep: " << sleep << "\n"; usleep(1000); } // cout << "seek: " << PcmChunk::getAge(seek(sleep)) - bufferMs + outputBufferDacTime << "\n"; @@ -222,7 +227,6 @@ bool Stream::getPlayerChunk(void* outputBuffer, chronos::usec outputBufferDacTim } } - long age(0); age = std::chrono::duration_cast(TimeProvider::serverNow() - getNextPlayerChunk(outputBuffer, framesPerBuffer, timeout, correction) - bufferMs + outputBufferDacTime).count(); diff --git a/common/timeDefs.h b/common/timeDefs.h index a04e5435..5f8978cf 100644 --- a/common/timeDefs.h +++ b/common/timeDefs.h @@ -10,6 +10,7 @@ namespace chronos typedef std::chrono::seconds sec; typedef std::chrono::milliseconds msec; typedef std::chrono::microseconds usec; + typedef std::chrono::nanoseconds nsec; } diff --git a/message/pcmChunk.cpp b/message/pcmChunk.cpp index f92c2ea3..e4c953b5 100644 --- a/message/pcmChunk.cpp +++ b/message/pcmChunk.cpp @@ -22,32 +22,6 @@ PcmChunk::~PcmChunk() } -bool PcmChunk::isEndOfChunk() const -{ - return idx >= getFrameCount(); -} - - -double PcmChunk::getFrameCount() const -{ - return (payloadSize / format.frameSize); -} - - - -double PcmChunk::getDuration() const -{ - return getFrameCount() / format.msRate(); -} - - - -double PcmChunk::getTimeLeft() const -{ - return (getFrameCount() - idx) / format.msRate(); -} - - int PcmChunk::seek(int frames) { @@ -77,3 +51,4 @@ int PcmChunk::readFrames(void* outputBuffer, size_t frameCount) } + diff --git a/message/pcmChunk.h b/message/pcmChunk.h index 04df7c87..40e57192 100644 --- a/message/pcmChunk.h +++ b/message/pcmChunk.h @@ -17,7 +17,6 @@ public: ~PcmChunk(); int readFrames(void* outputBuffer, size_t frameCount); - bool isEndOfChunk() const; inline chronos::time_point_hrc timePoint() const { @@ -27,46 +26,31 @@ public: chronos::usec((chronos::usec::rep)(1000000. * ((double)idx / (double)format.rate))) ); } -/* - inline time_point_ms timePoint() const + + int seek(int frames); + + template + inline T duration() const { - time_point_ms tp; - std::chrono::milliseconds::rep relativeIdxTp = ((double)idx / ((double)format.rate/1000.)); - return - tp + - std::chrono::seconds(timestamp.sec) + - std::chrono::milliseconds(timestamp.usec / 1000) + - std::chrono::milliseconds(relativeIdxTp); + return std::chrono::duration_cast(chronos::nsec((chronos::nsec::rep)(1000000 * getFrameCount() / format.msRate()))); } template - inline T getAge() const + inline T durationLeft() const { - return getAge(timePoint()); + return std::chrono::duration_cast(chronos::nsec((chronos::nsec::rep)(1000000 * (getFrameCount() - idx) / format.msRate()))); } - inline long getAge() const + inline bool isEndOfChunk() const { - return getAge().count(); + return idx >= getFrameCount(); } - inline static long getAge(const time_point_ms& time_point) + inline size_t getFrameCount() const { - return getAge(time_point).count(); + return (payloadSize / format.frameSize); } - 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); - } -*/ - int seek(int frames); - double getDuration() const; - double getDurationUs() const; - double getTimeLeft() const; - double getFrameCount() const; - SampleFormat format; private: