mirror of
https://github.com/badaix/snapcast.git
synced 2025-07-30 06:39:35 +02:00
sleep
git-svn-id: svn://elaine/murooma/trunk@300 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
parent
f54ea7564c
commit
1759aeb160
5 changed files with 39 additions and 38 deletions
|
@ -128,7 +128,7 @@ void Player::worker()
|
||||||
{
|
{
|
||||||
snd_pcm_avail_delay(pcm_handle, &avail, &delay);
|
snd_pcm_avail_delay(pcm_handle, &avail, &delay);
|
||||||
|
|
||||||
if (stream_->getPlayerChunk(buff, chronos::usec((chronos::usec::rep)(1000 * (double)delay / stream_->format.msRate())), frames, 500))
|
if (stream_->getPlayerChunk(buff, chronos::usec((chronos::usec::rep)(1000 * (double)delay / stream_->format.msRate())), frames))
|
||||||
{
|
{
|
||||||
if ((pcm = snd_pcm_writei(pcm_handle, buff, frames)) == -EPIPE)
|
if ((pcm = snd_pcm_writei(pcm_handle, buff, frames)) == -EPIPE)
|
||||||
{
|
{
|
||||||
|
|
|
@ -71,7 +71,7 @@ time_point_ms Stream::seekTo(const time_point_ms& to)
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
time_point_hrc Stream::seek(long ms)
|
time_point_hrc Stream::seek(long ms)
|
||||||
{
|
{
|
||||||
if (!chunk)
|
if (!chunk)
|
||||||
|
@ -89,13 +89,13 @@ time_point_hrc Stream::seek(long ms)
|
||||||
chunk->seek(ms * format.msRate());
|
chunk->seek(ms * format.msRate());
|
||||||
return chunk->start();
|
return chunk->start();
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
time_point_hrc Stream::getNextPlayerChunk(void* outputBuffer, unsigned long framesPerBuffer, size_t timeout, const chronos::usec& correction)
|
time_point_hrc Stream::getNextPlayerChunk(void* outputBuffer, const chronos::usec& timeout, unsigned long framesPerBuffer, const chronos::usec& correction)
|
||||||
{
|
{
|
||||||
if (!chunk)
|
if (!chunk && !chunks.try_pop(chunk, timeout))
|
||||||
if (!chunks.try_pop(chunk, chronos::msec(timeout)))
|
throw 0;
|
||||||
throw 0;
|
|
||||||
|
|
||||||
//cout << "duration: " << chunk->duration<chronos::msec>().count() << ", " << chunk->duration<chronos::usec>().count() << ", " << chunk->duration<chronos::nsec>().count() << "\n";
|
//cout << "duration: " << chunk->duration<chronos::msec>().count() << ", " << chunk->duration<chronos::usec>().count() << ", " << chunk->duration<chronos::nsec>().count() << "\n";
|
||||||
time_point_hrc tp = chunk->start();
|
time_point_hrc tp = chunk->start();
|
||||||
|
@ -116,9 +116,8 @@ time_point_hrc Stream::getNextPlayerChunk(void* outputBuffer, unsigned long fram
|
||||||
while (read < toRead)
|
while (read < toRead)
|
||||||
{
|
{
|
||||||
read += chunk->readFrames(buffer + read*format.frameSize, toRead - read);
|
read += chunk->readFrames(buffer + read*format.frameSize, toRead - read);
|
||||||
if (chunk->isEndOfChunk())
|
if (chunk->isEndOfChunk() && !chunks.try_pop(chunk, timeout))
|
||||||
if (!chunks.try_pop(chunk, std::chrono::milliseconds(timeout)))
|
throw 0;
|
||||||
throw 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (correction.count() != 0)
|
if (correction.count() != 0)
|
||||||
|
@ -157,10 +156,21 @@ void Stream::resetBuffers()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool Stream::getPlayerChunk(void* outputBuffer, chronos::usec outputBufferDacTime, unsigned long framesPerBuffer, size_t timeout)
|
bool Stream::getPlayerChunk(void* outputBuffer, const chronos::usec& outputBufferDacTime, unsigned long framesPerBuffer)
|
||||||
{
|
{
|
||||||
|
if (!chunk && !chunks.try_pop(chunk, outputBufferDacTime))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
chronos::usec age = std::chrono::duration_cast<usec>(TimeProvider::serverNow() - chunk->start() - bufferMs + outputBufferDacTime);
|
||||||
|
if ((sleep.count() == 0) && (chronos::abs(age) > chronos::msec(200)))
|
||||||
|
{
|
||||||
|
cout << "age > 200: " << age.count() << "\n";
|
||||||
|
sleep = age;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
||||||
//cout << "framesPerBuffer: " << framesPerBuffer << "\tms: " << framesPerBuffer*2 / PLAYER_CHUNK_MS_SIZE << "\t" << PLAYER_CHUNK_SIZE << "\n";
|
//cout << "framesPerBuffer: " << framesPerBuffer << "\tms: " << framesPerBuffer*2 / PLAYER_CHUNK_MS_SIZE << "\t" << PLAYER_CHUNK_SIZE << "\n";
|
||||||
chronos::nsec bufferDuration = chronos::nsec(chronos::usec::rep(framesPerBuffer / format_.nsRate()));
|
chronos::nsec bufferDuration = chronos::nsec(chronos::usec::rep(framesPerBuffer / format_.nsRate()));
|
||||||
// cout << "buffer duration: " << bufferDuration.count() << "\n";
|
// cout << "buffer duration: " << bufferDuration.count() << "\n";
|
||||||
|
@ -172,34 +182,25 @@ bool Stream::getPlayerChunk(void* outputBuffer, chronos::usec outputBufferDacTim
|
||||||
resetBuffers();
|
resetBuffers();
|
||||||
if (sleep < -bufferDuration/2)
|
if (sleep < -bufferDuration/2)
|
||||||
{
|
{
|
||||||
usec age = chrono::duration_cast<usec>(TimeProvider::serverNow() - getSilentPlayerChunk(outputBuffer, framesPerBuffer) - bufferMs + outputBufferDacTime);
|
sleep = chrono::duration_cast<usec>(TimeProvider::serverNow() - getSilentPlayerChunk(outputBuffer, framesPerBuffer) - bufferMs + outputBufferDacTime);
|
||||||
sleep = age;
|
//cout << "-sleep: " << sleep.count() << " " << -bufferDuration.count() / 2000 << "\n";
|
||||||
// sleep = PcmChunk::getAge(getSilentPlayerChunk(outputBuffer, framesPerBuffer)) - bufferMs + outputBufferDacTime + TimeProvider::getInstance().getDiffToServerMs();
|
|
||||||
std::cerr << " after: " << sleep.count() << ", chunks: " << chunks.size() << "\n";
|
|
||||||
// std::clog << kLogNotice << "sleep: " << sleep << std::endl;
|
|
||||||
// if (sleep > -msBuffer/2)
|
|
||||||
// sleep = 0;
|
|
||||||
if (sleep < -bufferDuration/2)
|
if (sleep < -bufferDuration/2)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else if (sleep > bufferDuration/2)
|
else if (sleep > bufferDuration/2)
|
||||||
{
|
{
|
||||||
if (!chunk)
|
while (sleep > chunk->duration<chronos::usec>())
|
||||||
if (!chunks.try_pop(chunk, chronos::msec(timeout)))
|
|
||||||
throw 0;
|
|
||||||
while (sleep > chunk->duration<chronos::msec>())
|
|
||||||
{
|
{
|
||||||
cout << "sleep > chunk->getDuration(): " << sleep.count() << " > " << chunk->duration<chronos::msec>().count() << ", chunks: " << chunks.size() << ", out: " << outputBufferDacTime.count() << ", needed: " << bufferDuration.count() << "\n";
|
cout << "sleep > chunk->getDuration(): " << sleep.count() << " > " << chunk->duration<chronos::msec>().count() << ", chunks: " << chunks.size() << ", out: " << outputBufferDacTime.count() << ", needed: " << bufferDuration.count() << "\n";
|
||||||
if (!chunks.try_pop(chunk, chronos::msec(timeout)))
|
if (!chunks.try_pop(chunk, outputBufferDacTime))
|
||||||
throw 0;
|
return false;
|
||||||
msec age = std::chrono::duration_cast<msec>(TimeProvider::serverNow() - chunk->start() - bufferMs + outputBufferDacTime);
|
|
||||||
sleep = age;
|
sleep = std::chrono::duration_cast<usec>(TimeProvider::serverNow() - chunk->start() - bufferMs + outputBufferDacTime);
|
||||||
usleep(1000);
|
usleep(1000);
|
||||||
}
|
}
|
||||||
// cout << "seek: " << PcmChunk::getAge(seek(sleep)) - bufferMs + outputBufferDacTime << "\n";
|
|
||||||
sleep = chronos::usec(0);
|
|
||||||
}
|
}
|
||||||
else if (sleep < -chronos::usec(100))
|
|
||||||
|
if (sleep < -chronos::usec(100))
|
||||||
{
|
{
|
||||||
sleep += chronos::usec(100);
|
sleep += chronos::usec(100);
|
||||||
correction = -chronos::usec(100);
|
correction = -chronos::usec(100);
|
||||||
|
@ -217,7 +218,7 @@ bool Stream::getPlayerChunk(void* outputBuffer, chronos::usec outputBufferDacTim
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
chronos::usec age = std::chrono::duration_cast<usec>(TimeProvider::serverNow() - getNextPlayerChunk(outputBuffer, framesPerBuffer, timeout, correction) - bufferMs + outputBufferDacTime);
|
age = std::chrono::duration_cast<usec>(TimeProvider::serverNow() - getNextPlayerChunk(outputBuffer, outputBufferDacTime, framesPerBuffer, correction) - bufferMs + outputBufferDacTime);
|
||||||
|
|
||||||
if (sleep.count() == 0)
|
if (sleep.count() == 0)
|
||||||
{
|
{
|
||||||
|
@ -236,11 +237,6 @@ bool Stream::getPlayerChunk(void* outputBuffer, chronos::usec outputBufferDacTim
|
||||||
cout << "pMiniBuffer->full() && (abs(pMiniBuffer->mean()) > 50): " << miniBuffer.median() << "\n";
|
cout << "pMiniBuffer->full() && (abs(pMiniBuffer->mean()) > 50): " << miniBuffer.median() << "\n";
|
||||||
sleep = chronos::usec((chronos::msec::rep)miniBuffer.mean());
|
sleep = chronos::usec((chronos::msec::rep)miniBuffer.mean());
|
||||||
}
|
}
|
||||||
else if (chronos::abs(age) > chronos::msec(200))
|
|
||||||
{
|
|
||||||
cout << "age > 50: " << age.count() << "\n";
|
|
||||||
sleep = age;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sleep.count() != 0)
|
if (sleep.count() != 0)
|
||||||
|
|
|
@ -22,12 +22,12 @@ public:
|
||||||
Stream(const SampleFormat& format);
|
Stream(const SampleFormat& format);
|
||||||
void addChunk(PcmChunk* chunk);
|
void addChunk(PcmChunk* chunk);
|
||||||
void clearChunks();
|
void clearChunks();
|
||||||
bool getPlayerChunk(void* outputBuffer, chronos::usec outputBufferDacTime, unsigned long framesPerBuffer, size_t timeout);
|
bool getPlayerChunk(void* outputBuffer, const chronos::usec& outputBufferDacTime, unsigned long framesPerBuffer);
|
||||||
void setBufferLen(size_t bufferLenMs);
|
void setBufferLen(size_t bufferLenMs);
|
||||||
const SampleFormat& format;
|
const SampleFormat& format;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
chronos::time_point_hrc getNextPlayerChunk(void* outputBuffer, unsigned long framesPerBuffer, size_t timeout, const chronos::usec& correction = chronos::usec(0));
|
chronos::time_point_hrc getNextPlayerChunk(void* outputBuffer, const chronos::usec& timeout, unsigned long framesPerBuffer, const chronos::usec& correction = chronos::usec(0));
|
||||||
chronos::time_point_hrc getSilentPlayerChunk(void* outputBuffer, unsigned long framesPerBuffer);
|
chronos::time_point_hrc getSilentPlayerChunk(void* outputBuffer, unsigned long framesPerBuffer);
|
||||||
chronos::time_point_hrc seek(long ms);
|
chronos::time_point_hrc seek(long ms);
|
||||||
// time_point_ms seekTo(const time_point_ms& to);
|
// time_point_ms seekTo(const time_point_ms& to);
|
||||||
|
|
|
@ -32,7 +32,7 @@ public:
|
||||||
return queue_.front();
|
return queue_.front();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool try_pop(T& item, std::chrono::milliseconds timeout)
|
bool try_pop(T& item, std::chrono::microseconds timeout)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> mlock(mutex_);
|
std::unique_lock<std::mutex> mlock(mutex_);
|
||||||
|
|
||||||
|
@ -45,6 +45,11 @@ public:
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool try_pop(T& item, std::chrono::milliseconds timeout)
|
||||||
|
{
|
||||||
|
return try_pop(item, std::chrono::duration_cast<std::chrono::microseconds>(timeout));
|
||||||
|
}
|
||||||
|
|
||||||
void pop(T& item)
|
void pop(T& item)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> mlock(mutex_);
|
std::unique_lock<std::mutex> mlock(mutex_);
|
||||||
|
|
|
@ -12,7 +12,7 @@ double PcmEncoder::encode(PcmChunk* chunk)
|
||||||
for (size_t n=0; n<wireChunk->length; ++n)
|
for (size_t n=0; n<wireChunk->length; ++n)
|
||||||
wireChunk->payload[n] *= 1;
|
wireChunk->payload[n] *= 1;
|
||||||
*/
|
*/
|
||||||
return chunk->getDuration();
|
return chunk->duration<chronos::msec>().count();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue