From b8cc8e8a67027eda303694bfc7124c36828d2aaf Mon Sep 17 00:00:00 2001 From: "(no author)" <(no author)@d8a302eb-03bc-478d-80e4-98257eca68ef> Date: Mon, 4 Aug 2014 06:43:10 +0000 Subject: [PATCH] wireChunk git-svn-id: svn://elaine/murooma/trunk@134 d8a302eb-03bc-478d-80e4-98257eca68ef --- Makefile | 2 +- chunk.h | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- client.cpp | 2 +- server.cpp | 11 +++++------ stream.cpp | 40 +++++++++++++++------------------------- stream.h | 1 - timeUtils.h | 4 ++-- 7 files changed, 75 insertions(+), 38 deletions(-) diff --git a/Makefile b/Makefile index 6d08a586..39df447a 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ LDFLAGS = -lrt -lzmq -lpthread -lportaudio OBJ_SERVER = server.o BIN_SERVER = server -OBJ_CLIENT = client.o stream.o +OBJ_CLIENT = client.o stream.o chunk.o BIN_CLIENT = client OBJ = $(OBJ_SERVER) $(OBJ_CLIENT) diff --git a/chunk.h b/chunk.h index 106f8f3c..bd2020d6 100644 --- a/chunk.h +++ b/chunk.h @@ -1,6 +1,8 @@ #ifndef CHUNK_H #define CHUNK_H +#include + #define SAMPLE_RATE (48000) //#define SAMPLE_BIT (16) #define CHANNELS (2) @@ -16,15 +18,62 @@ #define FRAMES_PER_BUFFER ((SAMPLE_RATE*PLAYER_CHUNK_MS)/1000) -struct Chunk +typedef std::chrono::time_point time_point_ms; + + +struct WireChunk { int32_t tv_sec; int32_t tv_usec; - int32_t idx; int16_t payload[WIRE_CHUNK_SIZE]; }; +class Chunk +{ +public: + Chunk(WireChunk* _wireChunk); + ~Chunk(); + + int read(short* _outputBuffer, int _count); + bool isEndOfChunk(); + + inline time_point_ms timePoint() + { + time_point_ms tp; + return tp + std::chrono::seconds(wireChunk->tv_sec) + std::chrono::milliseconds(wireChunk->tv_usec / 1000) + std::chrono::milliseconds(idx / WIRE_CHUNK_MS_SIZE); + } + + template + inline T getAge() + { + return getAge(timePoint()); + } + + inline long getAge() + { + return getAge().count(); + } + + inline static long getAge(const time_point_ms& time_point) + { + return getAge(time_point).count(); + } + + template + static inline T getAge(const std::chrono::time_point& time_point) + { + return std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - time_point); + } + +private: + int32_t idx; + WireChunk* wireChunk; +}; + + + #endif + diff --git a/client.cpp b/client.cpp index c7065e5b..ec0c00d0 100644 --- a/client.cpp +++ b/client.cpp @@ -42,7 +42,7 @@ void player() while (1) { subscriber.recv(&update); - stream->addChunk((Chunk*)(update.data())); + stream->addChunk(new Chunk((WireChunk*)(update.data()))); } } diff --git a/server.cpp b/server.cpp index 88de55c7..86cc3f14 100644 --- a/server.cpp +++ b/server.cpp @@ -29,7 +29,7 @@ int main () { publisher.bind("tcp://0.0.0.0:123458"); char c[2]; - Chunk* chunk = new Chunk(); + WireChunk* chunk = new WireChunk(); timeval tvChunk; gettimeofday(&tvChunk, NULL); long nextTick = getTickCount(); @@ -50,9 +50,8 @@ int main () { chunk->tv_sec = tvChunk.tv_sec; chunk->tv_usec = tvChunk.tv_usec; - chunk->idx = 0; - zmq::message_t message(sizeof(Chunk)); - memcpy(message.data(), chunk, sizeof(Chunk)); + zmq::message_t message(sizeof(WireChunk)); + memcpy(message.data(), chunk, sizeof(WireChunk)); publisher.send(message); addMs(tvChunk, WIRE_CHUNK_MS); @@ -64,8 +63,8 @@ int main () { } else { - cin.sync(); - gettimeofday(&tvChunk, NULL); + cin.sync(); + gettimeofday(&tvChunk, NULL); nextTick = getTickCount(); } } diff --git a/stream.cpp b/stream.cpp index a3e8fb0e..f34af5f9 100644 --- a/stream.cpp +++ b/stream.cpp @@ -55,8 +55,8 @@ void Stream::getSilentPlayerChunk(short* outputBuffer) time_point_ms Stream::getNextPlayerChunk(short* outputBuffer, int correction) { Chunk* chunk = getNextChunk(); - time_point_ms tp = timePoint(chunk); - + time_point_ms tp = chunk->timePoint(); +/* if (correction != 0) { float idx(chunk->idx); @@ -80,7 +80,7 @@ time_point_ms Stream::getNextPlayerChunk(short* outputBuffer, int correction) // std::cerr << "Diff: " << diff_ms(getTimeval(chunk), tv) << "\t" << chunk->idx / PLAYER_CHUNK_MS_SIZE << "\n"; } else - { +*/ { /* int idx(chunk->idx); for (size_t n=0; nidx = idx; */ - size_t missing = PLAYER_CHUNK_SIZE;// + correction*PLAYER_CHUNK_MS_SIZE; - if (chunk->idx + PLAYER_CHUNK_SIZE > WIRE_CHUNK_SIZE) + int read = 0; + while (read < PLAYER_CHUNK_SIZE) { - if (outputBuffer != NULL) - memcpy(outputBuffer, &chunk->payload[chunk->idx], sizeof(int16_t)*(WIRE_CHUNK_SIZE - chunk->idx)); - missing = chunk->idx + PLAYER_CHUNK_SIZE - WIRE_CHUNK_SIZE; - chunks.pop_front(); - delete chunk; - chunk = getNextChunk(); + read += chunk->read(outputBuffer + read, PLAYER_CHUNK_SIZE - read); + if (chunk->isEndOfChunk()) + { + chunks.pop_front(); + delete chunk; + chunk = getNextChunk(); + } } - - if (outputBuffer != NULL) - memcpy((outputBuffer + PLAYER_CHUNK_SIZE - missing), &chunk->payload[chunk->idx], sizeof(int16_t)*missing); - - chunk->idx += missing; - if (chunk->idx >= WIRE_CHUNK_SIZE) - { - chunks.pop_front(); - delete chunk; - } - } return tp; @@ -147,11 +137,11 @@ void Stream::getChunk(short* outputBuffer, double outputBufferDacTime, unsigned else { 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::cerr << "Sleep: " << sleep << "\n"; - int age = getAge(getNextPlayerChunk(outputBuffer)) - bufferMs; + int age = Chunk::getAge(getNextPlayerChunk(outputBuffer)) - bufferMs; if (age < PLAYER_CHUNK_MS / 2) break; // std::cerr << getAge(getNextPlayerChunk(outputBuffer)) - bufferMs << "\t"; @@ -182,7 +172,7 @@ void Stream::getChunk(short* outputBuffer, double outputBufferDacTime, unsigned } } - int age = getAge(getNextPlayerChunk(outputBuffer, correction)) - bufferMs;// + outputBufferDacTime*1000; + int age = Chunk::getAge(getNextPlayerChunk(outputBuffer, correction)) - bufferMs;// + outputBufferDacTime*1000; if (outputBufferDacTime < 1) age += outputBufferDacTime*1000; pBuffer->add(age); diff --git a/stream.h b/stream.h index da4dbdcc..e0060357 100644 --- a/stream.h +++ b/stream.h @@ -23,7 +23,6 @@ public: void setBufferLen(size_t bufferLenMs); private: - void updateChunkTime(Chunk* chunk); void sleepMs(int ms); size_t currentSample; diff --git a/timeUtils.h b/timeUtils.h index a197be6e..82bc084d 100644 --- a/timeUtils.h +++ b/timeUtils.h @@ -5,7 +5,7 @@ #include #include - +/* typedef std::chrono::time_point time_point_ms; @@ -44,7 +44,7 @@ inline static long getAge(const time_point_ms& time_point) { return getAge(time_point).count(); } - +*/ static void addMs(timeval& tv, int ms)