From f521230f41d3df452fe3f9db3694f0fc7dd25774 Mon Sep 17 00:00:00 2001 From: "(no author)" <(no author)@d8a302eb-03bc-478d-80e4-98257eca68ef> Date: Thu, 21 Aug 2014 21:17:25 +0000 Subject: [PATCH] sleep git-svn-id: svn://elaine/murooma/trunk@202 d8a302eb-03bc-478d-80e4-98257eca68ef --- client/snapClient.cpp | 20 ++++++++++++------- client/stream.cpp | 46 ++++++++++++++++++++++++------------------- client/stream.h | 3 ++- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/client/snapClient.cpp b/client/snapClient.cpp index 48cded45..5c0effbe 100644 --- a/client/snapClient.cpp +++ b/client/snapClient.cpp @@ -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; diff --git a/client/stream.cpp b/client/stream.cpp index 191cfd3e..6a4b8560 100644 --- a/client/stream.cpp +++ b/client/stream.cpp @@ -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) @@ -126,35 +137,30 @@ void Stream::getPlayerChunk(void* outputBuffer, double outputBufferDacTime, unsi pCardBuffer->add(ticks); if (pCardBuffer->full()) cardBuffer = pCardBuffer->percentil(90); - + 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; igetAge() - 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(); } } diff --git a/client/stream.h b/client/stream.h index 60eda553..568e1f6f 100644 --- a/client/stream.h +++ b/client/stream.h @@ -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();