mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-31 18:06:15 +02:00
sleep
git-svn-id: svn://elaine/murooma/trunk@202 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
parent
7691f6f67a
commit
f521230f41
3 changed files with 41 additions and 28 deletions
|
@ -63,9 +63,14 @@ void player(const std::string& ip, int port)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
cout << "connect\n";
|
|
||||||
tcp::socket s(io_service);
|
tcp::socket s(io_service);
|
||||||
s.connect(*iterator);
|
s.connect(*iterator);
|
||||||
|
struct timeval tv;
|
||||||
|
tv.tv_sec = 5;
|
||||||
|
tv.tv_usec = 0;
|
||||||
|
setsockopt(s.native(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||||
|
|
||||||
|
std::clog << kLogNotice << "connected to " << ip << ":" << port << std::endl;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
WireChunk* wireChunk = new WireChunk();
|
WireChunk* wireChunk = new WireChunk();
|
||||||
|
@ -89,16 +94,17 @@ cout << "connect\n";
|
||||||
stream->addChunk(new Chunk(sampleRate, channels, bps, wireChunk));
|
stream->addChunk(new Chunk(sampleRate, channels, bps, wireChunk));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (std::exception& e)
|
catch (const std::exception& e)
|
||||||
{
|
{
|
||||||
std::cerr << "Exception: " << e.what() << "\n";
|
std::clog << kLogNotice << "Exception: " << e.what() << ", trying to reconnect" << std::endl;
|
||||||
|
stream->clearChunks();
|
||||||
usleep(500*1000);
|
usleep(500*1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (std::exception& e)
|
catch (const std::exception& e)
|
||||||
{
|
{
|
||||||
std::cerr << "Exception: " << e.what() << "\n";
|
std::clog << kLogNotice << "Exception: " << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,7 +263,7 @@ int main (int argc, char *argv[])
|
||||||
std::thread playerThread(player, ip, port);
|
std::thread playerThread(player, ip, port);
|
||||||
|
|
||||||
std::string cmd;
|
std::string cmd;
|
||||||
/* while (true && (argc > 3))
|
while (true && (argc > 3))
|
||||||
{
|
{
|
||||||
std::cout << "> ";
|
std::cout << "> ";
|
||||||
std::getline(std::cin, cmd);
|
std::getline(std::cin, cmd);
|
||||||
|
@ -273,7 +279,7 @@ int main (int argc, char *argv[])
|
||||||
stream->setBufferLen(atoi(cmd.c_str()));
|
stream->setBufferLen(atoi(cmd.c_str()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
playerThread.join();
|
playerThread.join();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -25,6 +25,13 @@ void Stream::setBufferLen(size_t bufferLenMs)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void Stream::clearChunks()
|
||||||
|
{
|
||||||
|
while (chunks.size() > 0)
|
||||||
|
chunks.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void Stream::addChunk(Chunk* chunk)
|
void Stream::addChunk(Chunk* chunk)
|
||||||
{
|
{
|
||||||
while (chunks.size() * chunk->getDuration() > 10000)
|
while (chunks.size() * chunk->getDuration() > 10000)
|
||||||
|
@ -35,9 +42,13 @@ void Stream::addChunk(Chunk* chunk)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void Stream::getSilentPlayerChunk(void* outputBuffer, unsigned long framesPerBuffer)
|
time_point_ms Stream::getSilentPlayerChunk(void* outputBuffer, unsigned long framesPerBuffer)
|
||||||
{
|
{
|
||||||
memset(outputBuffer, 0, framesPerBuffer * channels_ * bytesPerSample_);//CHANNELS);
|
if (!chunk)
|
||||||
|
chunk = chunks.pop();
|
||||||
|
time_point_ms tp = chunk->timePoint();
|
||||||
|
memset(outputBuffer, 0, framesPerBuffer * frameSize_);
|
||||||
|
return tp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -112,8 +123,8 @@ void Stream::resetBuffers()
|
||||||
void Stream::getPlayerChunk(void* outputBuffer, double outputBufferDacTime, unsigned long framesPerBuffer)
|
void Stream::getPlayerChunk(void* outputBuffer, double outputBufferDacTime, unsigned long framesPerBuffer)
|
||||||
{
|
{
|
||||||
//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";
|
||||||
int msBuffer = floor(framesPerBuffer*2 / (hz_*channels_/1000));
|
int msBuffer = floor(framesPerBuffer / (hz_/1000));
|
||||||
|
//cout << msBuffer << " ms, " << framesPerBuffer << "\n";
|
||||||
int ticks = 0;
|
int ticks = 0;
|
||||||
long currentTick = getTickCount();
|
long currentTick = getTickCount();
|
||||||
if (lastTick == 0)
|
if (lastTick == 0)
|
||||||
|
@ -130,31 +141,26 @@ void Stream::getPlayerChunk(void* outputBuffer, double outputBufferDacTime, unsi
|
||||||
int correction = 0;
|
int correction = 0;
|
||||||
if (sleep != 0)
|
if (sleep != 0)
|
||||||
{
|
{
|
||||||
std::clog << kLogNotice << "sleep: " << sleep << std::endl;
|
|
||||||
resetBuffers();
|
resetBuffers();
|
||||||
if (sleep < -10)
|
if (sleep < -20)
|
||||||
{
|
{
|
||||||
// std::cerr << "Sleep: " << sleep << "\n";
|
sleep = Chunk::getAge(getSilentPlayerChunk(outputBuffer, framesPerBuffer)) - bufferMs + latencyMs;
|
||||||
sleep += msBuffer;
|
std::cerr << "Sleep: " << sleep << ", chunks: " << chunks.size() << "\n";
|
||||||
|
// std::clog << kLogNotice << "sleep: " << sleep << std::endl;
|
||||||
if (sleep > -msBuffer/2)
|
if (sleep > -msBuffer/2)
|
||||||
sleep = 0;
|
sleep = 0;
|
||||||
getSilentPlayerChunk(outputBuffer, framesPerBuffer);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (sleep > 10)
|
else if (sleep > 20)
|
||||||
{
|
{
|
||||||
// for (size_t i=0; i<chunks.size(); ++i)
|
// std::clog << kLogNotice << "sleep: " << sleep << std::endl;
|
||||||
// std::cerr << "Chunk " << i << ": " << chunks[i]->getAge() - bufferMs << "\n";
|
while (true)
|
||||||
while (true)// (int i=0; i<(int)(round((float)sleep / (float)PLAYER_CHUNK_MS)) + 1; ++i)
|
|
||||||
{
|
{
|
||||||
int age = Chunk::getAge(getNextPlayerChunk(outputBuffer, framesPerBuffer)) - bufferMs + latencyMs;
|
int age = Chunk::getAge(getNextPlayerChunk(outputBuffer, framesPerBuffer)) - bufferMs + latencyMs;
|
||||||
|
usleep(100);
|
||||||
|
// std::clog << kLogNotice << "age: " << age << std::endl;
|
||||||
if (age < 0)
|
if (age < 0)
|
||||||
break;
|
break;
|
||||||
// sleep -= msBuffer;
|
|
||||||
// age += 4*cardBuffer;
|
|
||||||
// cout << "age: " << age << ", msBuffer: " << msBuffer << "\n";
|
|
||||||
// if (sleep < msBuffer / 2)
|
|
||||||
// break;
|
|
||||||
}
|
}
|
||||||
sleep = 0;
|
sleep = 0;
|
||||||
return;
|
return;
|
||||||
|
@ -199,7 +205,7 @@ void Stream::getPlayerChunk(void* outputBuffer, double outputBufferDacTime, unsi
|
||||||
}
|
}
|
||||||
else if (pMiniBuffer->full() && (abs(pMiniBuffer->median()) > 50))
|
else if (pMiniBuffer->full() && (abs(pMiniBuffer->median()) > 50))
|
||||||
{
|
{
|
||||||
// cout << "pMiniBuffer->full() && (abs(pMiniBuffer->mean()) > 50): " << pMiniBuffer->median() << "\n";
|
cout << "pMiniBuffer->full() && (abs(pMiniBuffer->mean()) > 50): " << pMiniBuffer->median() << "\n";
|
||||||
sleep = pMiniBuffer->mean();
|
sleep = pMiniBuffer->mean();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,13 +18,14 @@ class Stream
|
||||||
public:
|
public:
|
||||||
Stream(size_t hz, size_t channels, size_t bps);
|
Stream(size_t hz, size_t channels, size_t bps);
|
||||||
void addChunk(Chunk* chunk);
|
void addChunk(Chunk* chunk);
|
||||||
|
void clearChunks();
|
||||||
void getPlayerChunk(void* outputBuffer, double outputBufferDacTime, unsigned long framesPerBuffer);
|
void getPlayerChunk(void* outputBuffer, double outputBufferDacTime, unsigned long framesPerBuffer);
|
||||||
void setBufferLen(size_t bufferLenMs);
|
void setBufferLen(size_t bufferLenMs);
|
||||||
void setLatency(size_t latency);
|
void setLatency(size_t latency);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
time_point_ms getNextPlayerChunk(void* outputBuffer, unsigned long framesPerBuffer, int correction = 0);
|
time_point_ms getNextPlayerChunk(void* outputBuffer, unsigned long framesPerBuffer, int correction = 0);
|
||||||
void getSilentPlayerChunk(void* outputBuffer, unsigned long framesPerBuffer);
|
time_point_ms getSilentPlayerChunk(void* outputBuffer, unsigned long framesPerBuffer);
|
||||||
void updateBuffers(int age);
|
void updateBuffers(int age);
|
||||||
void resetBuffers();
|
void resetBuffers();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue