git-svn-id: svn://elaine/murooma/trunk@202 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
(no author) 2014-08-21 21:17:25 +00:00
parent 7691f6f67a
commit f521230f41
3 changed files with 41 additions and 28 deletions

View file

@ -63,9 +63,14 @@ void player(const std::string& ip, int port)
{
try
{
cout << "connect\n";
tcp::socket s(io_service);
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)
{
WireChunk* wireChunk = new WireChunk();
@ -89,16 +94,17 @@ cout << "connect\n";
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);
}
}
}
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::string cmd;
/* while (true && (argc > 3))
while (true && (argc > 3))
{
std::cout << "> ";
std::getline(std::cin, cmd);
@ -273,7 +279,7 @@ int main (int argc, char *argv[])
stream->setBufferLen(atoi(cmd.c_str()));
}
}
*/
playerThread.join();
return 0;

View file

@ -25,6 +25,13 @@ void Stream::setBufferLen(size_t bufferLenMs)
void Stream::clearChunks()
{
while (chunks.size() > 0)
chunks.pop();
}
void Stream::addChunk(Chunk* chunk)
{
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)
{
//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;
long currentTick = getTickCount();
if (lastTick == 0)
@ -130,31 +141,26 @@ void Stream::getPlayerChunk(void* outputBuffer, double outputBufferDacTime, unsi
int correction = 0;
if (sleep != 0)
{
std::clog << kLogNotice << "sleep: " << sleep << std::endl;
resetBuffers();
if (sleep < -10)
if (sleep < -20)
{
// std::cerr << "Sleep: " << sleep << "\n";
sleep += msBuffer;
sleep = Chunk::getAge(getSilentPlayerChunk(outputBuffer, framesPerBuffer)) - bufferMs + latencyMs;
std::cerr << "Sleep: " << sleep << ", chunks: " << chunks.size() << "\n";
// std::clog << kLogNotice << "sleep: " << sleep << std::endl;
if (sleep > -msBuffer/2)
sleep = 0;
getSilentPlayerChunk(outputBuffer, framesPerBuffer);
return;
}
else if (sleep > 10)
else if (sleep > 20)
{
// for (size_t i=0; i<chunks.size(); ++i)
// std::cerr << "Chunk " << i << ": " << chunks[i]->getAge() - bufferMs << "\n";
while (true)// (int i=0; i<(int)(round((float)sleep / (float)PLAYER_CHUNK_MS)) + 1; ++i)
// std::clog << kLogNotice << "sleep: " << sleep << std::endl;
while (true)
{
int age = Chunk::getAge(getNextPlayerChunk(outputBuffer, framesPerBuffer)) - bufferMs + latencyMs;
usleep(100);
// std::clog << kLogNotice << "age: " << age << std::endl;
if (age < 0)
break;
// sleep -= msBuffer;
// age += 4*cardBuffer;
// cout << "age: " << age << ", msBuffer: " << msBuffer << "\n";
// if (sleep < msBuffer / 2)
// break;
}
sleep = 0;
return;
@ -199,7 +205,7 @@ void Stream::getPlayerChunk(void* outputBuffer, double outputBufferDacTime, unsi
}
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();
}
}

View file

@ -18,13 +18,14 @@ class Stream
public:
Stream(size_t hz, size_t channels, size_t bps);
void addChunk(Chunk* chunk);
void clearChunks();
void getPlayerChunk(void* outputBuffer, double outputBufferDacTime, unsigned long framesPerBuffer);
void setBufferLen(size_t bufferLenMs);
void setLatency(size_t latency);
private:
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 resetBuffers();