chonos stuff

git-svn-id: svn://elaine/murooma/trunk@297 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
(no author) 2014-09-25 21:21:32 +00:00
parent 6354d55f78
commit fe79a78efe
4 changed files with 32 additions and 68 deletions

View file

@ -36,7 +36,7 @@ void Stream::clearChunks()
void Stream::addChunk(PcmChunk* chunk)
{
while (chunks.size() * chunk->getDuration() > 10000)
while (chunks.size() * chunk->duration<chronos::msec>().count() > 10000)
chunks.pop();
chunks.push(shared_ptr<PcmChunk>(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<chronos::msec>().count())
{
chunk = chunks.pop();
ms -= min(ms, (long)chunk->getTimeLeft());
ms -= min(ms, (long)chunk->durationLeft<chronos::msec>().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<chronos::msec>().count() << ", " << chunk->duration<chronos::usec>().count() << ", " << chunk->duration<chronos::nsec>().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::milliseconds>(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<chronos::msec>().count())
{
cout << "sleep > chunk->getDuration(): " << sleep << " > " << chunk->duration<chronos::msec>().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<msec>(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<msec>(TimeProvider::serverNow() - getNextPlayerChunk(outputBuffer, framesPerBuffer, timeout, correction) - bufferMs + outputBufferDacTime).count();

View file

@ -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;
}

View file

@ -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)
}

View file

@ -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<typename T>
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<T>(chronos::nsec((chronos::nsec::rep)(1000000 * getFrameCount() / format.msRate())));
}
template<typename T>
inline T getAge() const
inline T durationLeft() const
{
return getAge<T>(timePoint());
return std::chrono::duration_cast<T>(chronos::nsec((chronos::nsec::rep)(1000000 * (getFrameCount() - idx) / format.msRate())));
}
inline long getAge() const
inline bool isEndOfChunk() const
{
return getAge<std::chrono::milliseconds>().count();
return idx >= getFrameCount();
}
inline static long getAge(const time_point_ms& time_point)
inline size_t getFrameCount() const
{
return getAge<std::chrono::milliseconds>(time_point).count();
return (payloadSize / format.frameSize);
}
template<typename T, typename U>
static inline T getAge(const std::chrono::time_point<U>& time_point)
{
return std::chrono::duration_cast<T>(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: