mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-12 08:36:43 +02:00
optimizations
git-svn-id: svn://elaine/murooma/trunk@51 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
parent
8244ce81d6
commit
7228f5565b
2 changed files with 16 additions and 54 deletions
66
client.cpp
66
client.cpp
|
@ -119,6 +119,7 @@ void sleepMs(int ms)
|
||||||
|
|
||||||
|
|
||||||
int skip(0);
|
int skip(0);
|
||||||
|
time_t lastUpdate(0);
|
||||||
|
|
||||||
|
|
||||||
/* This routine will be called by the PortAudio engine when audio is needed.
|
/* This routine will be called by the PortAudio engine when audio is needed.
|
||||||
|
@ -142,6 +143,8 @@ static int patestCallback( const void *inputBuffer, void *outputBuffer,
|
||||||
|
|
||||||
std::unique_lock<std::mutex> lck(mtx);
|
std::unique_lock<std::mutex> lck(mtx);
|
||||||
int age = 0;
|
int age = 0;
|
||||||
|
int median = 0;
|
||||||
|
int shortMedian = 0;
|
||||||
PlayerChunk* chunk = NULL;
|
PlayerChunk* chunk = NULL;
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
|
@ -154,11 +157,16 @@ static int patestCallback( const void *inputBuffer, void *outputBuffer,
|
||||||
age = getAge(*chunk) + timeInfo->outputBufferDacTime*1000 - bufferMs;
|
age = getAge(*chunk) + timeInfo->outputBufferDacTime*1000 - bufferMs;
|
||||||
buffer.add(age);
|
buffer.add(age);
|
||||||
shortBuffer.add(age);
|
shortBuffer.add(age);
|
||||||
int median = buffer.median();
|
time_t now = time(NULL);
|
||||||
int shortMedian = shortBuffer.median();
|
|
||||||
|
|
||||||
if (skip == 0)
|
if (skip == 0)
|
||||||
{
|
{
|
||||||
|
if (now != lastUpdate)
|
||||||
|
{
|
||||||
|
lastUpdate = now;
|
||||||
|
median = buffer.median();
|
||||||
|
shortMedian = shortBuffer.median();
|
||||||
|
}
|
||||||
if ((age > 500) || (age < -500))
|
if ((age > 500) || (age < -500))
|
||||||
skip = age / PLAYER_CHUNK_MS;
|
skip = age / PLAYER_CHUNK_MS;
|
||||||
else if (shortBuffer.full() && ((shortMedian > 100) || (shortMedian < -100)))
|
else if (shortBuffer.full() && ((shortMedian > 100) || (shortMedian < -100)))
|
||||||
|
@ -194,49 +202,6 @@ static int patestCallback( const void *inputBuffer, void *outputBuffer,
|
||||||
usleep(100);
|
usleep(100);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* int maxDiff = 10;
|
|
||||||
if ((shortMedian > bufferMs + std::max(100, 3*WIRE_CHUNK_MS)))
|
|
||||||
{
|
|
||||||
chunks->pop_front();
|
|
||||||
delete chunk;
|
|
||||||
std::cerr << "packe too old, dropping\n";
|
|
||||||
usleep(100);
|
|
||||||
}
|
|
||||||
else if ((shortMedian < bufferMs - std::max(100, 3*WIRE_CHUNK_MS)))
|
|
||||||
{
|
|
||||||
chunk = new PlayerChunk();
|
|
||||||
memset(&(chunk->payload[0]), 0, PLAYER_CHUNK_SIZE);
|
|
||||||
std::cerr << "age < bufferMs (" << age << " < " << bufferMs << "), playing silence\n";
|
|
||||||
usleep(10 * 1000);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if (buffer.full() && (median > bufferMs + maxDiff))
|
|
||||||
{
|
|
||||||
std::cerr << "median > bufferMs + PLAYER_CHUNK_MS (" << median << " > " << bufferMs + maxDiff << "), dropping chunk\n";
|
|
||||||
// buffer.clear();
|
|
||||||
chunks->pop_front();
|
|
||||||
delete chunk;
|
|
||||||
sleepMs(median - bufferMs);
|
|
||||||
}
|
|
||||||
else if (buffer.full() && (median + maxDiff < bufferMs))
|
|
||||||
{
|
|
||||||
std::cerr << "median + PLAYER_CHUNK_MS < bufferMs (" << median + maxDiff << " < " << bufferMs << "), playing silence\n";
|
|
||||||
// buffer.clear();
|
|
||||||
if (bufferMs - median > PLAYER_CHUNK_MS)
|
|
||||||
{
|
|
||||||
chunk = new PlayerChunk();
|
|
||||||
memset(&(chunk->payload[0]), 0, PLAYER_CHUNK_SIZE);
|
|
||||||
sleepMs(bufferMs - median - PLAYER_CHUNK_MS + 10);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
sleepMs(bufferMs - median + 10);
|
|
||||||
}
|
|
||||||
// delete chunk;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
chunks->pop_front();
|
chunks->pop_front();
|
||||||
|
@ -246,10 +211,8 @@ static int patestCallback( const void *inputBuffer, void *outputBuffer,
|
||||||
|
|
||||||
for( i=0; i<framesPerBuffer; i++)
|
for( i=0; i<framesPerBuffer; i++)
|
||||||
{
|
{
|
||||||
//std::cerr << (int)chunk->payload[i] << "\t" << (int)chunk->payload[i+1] << "\t" << (int)chunk->payload[i+2] << "\t" << (int)chunk->payload[i+3] << "\n";
|
*out++ = (chunk->payload[4*i+1]*256) + chunk->payload[4*i+0];
|
||||||
//std::cerr << i << "\t" << 4*i+1 << "\t" << 4*i << "\n";
|
*out++ = (chunk->payload[4*i+3]*256) + chunk->payload[4*i+2];
|
||||||
*out++ = (int)chunk->payload[4*i+1]*256 + (int)chunk->payload[4*i+0];
|
|
||||||
*out++ = (int)chunk->payload[4*i+3]*256 + (int)chunk->payload[4*i+2];
|
|
||||||
}
|
}
|
||||||
delete chunk;
|
delete chunk;
|
||||||
|
|
||||||
|
@ -345,16 +308,15 @@ int main (int argc, char *argv[])
|
||||||
std::cerr << "Unsuccessful in setting thread realtime prio" << std::endl;
|
std::cerr << "Unsuccessful in setting thread realtime prio" << std::endl;
|
||||||
*/
|
*/
|
||||||
initAudio();
|
initAudio();
|
||||||
|
Chunk* chunk = new Chunk();
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
zmq::message_t update;
|
zmq::message_t update;
|
||||||
subscriber.recv(&update);
|
subscriber.recv(&update);
|
||||||
Chunk* chunk = new Chunk();
|
|
||||||
memcpy(chunk, update.data(), sizeof(Chunk));
|
memcpy(chunk, update.data(), sizeof(Chunk));
|
||||||
for (size_t n=0; n<WIRE_CHUNK_MS/PLAYER_CHUNK_MS; ++n)
|
for (size_t n=0; n<WIRE_CHUNK_MS/PLAYER_CHUNK_MS; ++n)
|
||||||
{
|
{
|
||||||
PlayerChunk* playerChunk = new PlayerChunk();
|
PlayerChunk* playerChunk = new PlayerChunk();
|
||||||
// for (size_t m=0; m<PLAYER_CHUNK_SIZE; ++m)
|
|
||||||
playerChunk->tv_sec = chunk->tv_sec;
|
playerChunk->tv_sec = chunk->tv_sec;
|
||||||
playerChunk->tv_usec = chunk->tv_usec;
|
playerChunk->tv_usec = chunk->tv_usec;
|
||||||
addMs(*playerChunk, n*PLAYER_CHUNK_MS);
|
addMs(*playerChunk, n*PLAYER_CHUNK_MS);
|
||||||
|
@ -364,13 +326,13 @@ int main (int argc, char *argv[])
|
||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
cv.notify_all();
|
cv.notify_all();
|
||||||
}
|
}
|
||||||
delete chunk;
|
|
||||||
|
|
||||||
// timeval now;
|
// timeval now;
|
||||||
// gettimeofday(&now, NULL);
|
// gettimeofday(&now, NULL);
|
||||||
// std::cerr << "New chunk: " << chunkTime(*chunk) << "\t" << timeToStr(now) << "\t" << getAge(*chunk) << "\n";
|
// std::cerr << "New chunk: " << chunkTime(*chunk) << "\t" << timeToStr(now) << "\t" << getAge(*chunk) << "\n";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
delete chunk;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ int getAge(const T& chunk)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
long getTickCount()
|
inline long getTickCount()
|
||||||
{
|
{
|
||||||
struct timespec now;
|
struct timespec now;
|
||||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||||
|
@ -53,7 +53,7 @@ long getTickCount()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void addMs(timeval& tv, int ms)
|
inline void addMs(timeval& tv, int ms)
|
||||||
{
|
{
|
||||||
tv.tv_usec += ms*1000;
|
tv.tv_usec += ms*1000;
|
||||||
tv.tv_sec += (tv.tv_usec / 1000000);
|
tv.tv_sec += (tv.tv_usec / 1000000);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue