mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-21 04:56:13 +02:00
switched to chrono
git-svn-id: svn://elaine/murooma/trunk@130 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
parent
06a7909b1a
commit
a76b4fb953
3 changed files with 47 additions and 73 deletions
14
stream.cpp
14
stream.cpp
|
@ -1,5 +1,4 @@
|
|||
#include "stream.h"
|
||||
#include "timeUtils.h"
|
||||
#include <iostream>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
@ -53,10 +52,10 @@ void Stream::getSilentPlayerChunk(short* outputBuffer)
|
|||
|
||||
|
||||
|
||||
timeval Stream::getNextPlayerChunk(short* outputBuffer, int correction)
|
||||
time_point_ms Stream::getNextPlayerChunk(short* outputBuffer, int correction)
|
||||
{
|
||||
Chunk* chunk = getNextChunk();
|
||||
timeval tv = getTimeval(chunk);
|
||||
time_point_ms tp = timePoint(chunk);
|
||||
|
||||
if (correction != 0)
|
||||
{
|
||||
|
@ -77,8 +76,8 @@ timeval Stream::getNextPlayerChunk(short* outputBuffer, int correction)
|
|||
}
|
||||
}
|
||||
chunk->idx = idx;
|
||||
if (correction != 0)
|
||||
std::cerr << "Diff: " << diff_ms(getTimeval(chunk), tv) << "\t" << chunk->idx / PLAYER_CHUNK_MS_SIZE << "\n";
|
||||
// if (correction != 0)
|
||||
// std::cerr << "Diff: " << diff_ms(getTimeval(chunk), tv) << "\t" << chunk->idx / PLAYER_CHUNK_MS_SIZE << "\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -126,7 +125,7 @@ timeval Stream::getNextPlayerChunk(short* outputBuffer, int correction)
|
|||
|
||||
}
|
||||
|
||||
return tv;
|
||||
return tp;
|
||||
}
|
||||
|
||||
|
||||
|
@ -180,8 +179,7 @@ void Stream::getChunk(short* outputBuffer, double outputBufferDacTime, unsigned
|
|||
}
|
||||
}
|
||||
|
||||
timeval tv = getNextPlayerChunk(outputBuffer, correction);
|
||||
int age = getAge(tv) - bufferMs;// + outputBufferDacTime*1000;
|
||||
int age = getAge(getNextPlayerChunk(outputBuffer, correction)) - bufferMs;// + outputBufferDacTime*1000;
|
||||
if (outputBufferDacTime < 1)
|
||||
age += outputBufferDacTime*1000;
|
||||
pBuffer->add(age);
|
||||
|
|
3
stream.h
3
stream.h
|
@ -8,6 +8,7 @@
|
|||
#include <vector>
|
||||
#include "doubleBuffer.h"
|
||||
#include "chunk.h"
|
||||
#include "timeUtils.h"
|
||||
|
||||
|
||||
class Stream
|
||||
|
@ -16,7 +17,7 @@ public:
|
|||
Stream();
|
||||
void addChunk(Chunk* chunk);
|
||||
Chunk* getNextChunk();
|
||||
timeval getNextPlayerChunk(short* outputBuffer, int correction = 0);
|
||||
time_point_ms getNextPlayerChunk(short* outputBuffer, int correction = 0);
|
||||
void getSilentPlayerChunk(short* outputBuffer);
|
||||
void getChunk(short* outputBuffer, double outputBufferDacTime, unsigned long framesPerBuffer);
|
||||
void setBufferLen(size_t bufferLenMs);
|
||||
|
|
103
timeUtils.h
103
timeUtils.h
|
@ -3,20 +3,50 @@
|
|||
|
||||
#include "chunk.h"
|
||||
#include <sys/time.h>
|
||||
#include <chrono>
|
||||
|
||||
static std::string timeToStr(const timeval& timestamp)
|
||||
|
||||
typedef std::chrono::time_point<std::chrono::high_resolution_clock, std::chrono::milliseconds> time_point_ms;
|
||||
|
||||
|
||||
static inline time_point_ms timePoint(const Chunk* chunk)
|
||||
{
|
||||
char tmbuf[64], buf[64];
|
||||
struct tm *nowtm;
|
||||
time_t nowtime;
|
||||
nowtime = timestamp.tv_sec;
|
||||
nowtm = localtime(&nowtime);
|
||||
strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S", nowtm);
|
||||
snprintf(buf, sizeof buf, "%s.%06d", tmbuf, (int)timestamp.tv_usec);
|
||||
return buf;
|
||||
time_point_ms tp;
|
||||
return tp + std::chrono::seconds(chunk->tv_sec) + std::chrono::milliseconds(chunk->tv_usec / 1000) + std::chrono::milliseconds(chunk->idx / WIRE_CHUNK_MS_SIZE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<typename T, typename U>
|
||||
static inline T getAge(const std::chrono::time_point<U>& time_point)
|
||||
{
|
||||
return std::chrono::duration_cast<T>(std::chrono::high_resolution_clock::now() - time_point);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<typename T>
|
||||
static inline T getAge(const Chunk* chunk)
|
||||
{
|
||||
return getAge<T>(timePoint(chunk));
|
||||
}
|
||||
|
||||
|
||||
|
||||
inline static long getAge(const Chunk* chunk)
|
||||
{
|
||||
return getAge<std::chrono::milliseconds>(chunk).count();
|
||||
}
|
||||
|
||||
|
||||
|
||||
inline static long getAge(const time_point_ms& time_point)
|
||||
{
|
||||
return getAge<std::chrono::milliseconds>(time_point).count();
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void addMs(timeval& tv, int ms)
|
||||
{
|
||||
if (ms < 0)
|
||||
|
@ -33,51 +63,6 @@ static void addMs(timeval& tv, int ms)
|
|||
}
|
||||
|
||||
|
||||
static timeval getTimeval(const Chunk* chunk)
|
||||
{
|
||||
timeval ts;
|
||||
ts.tv_sec = chunk->tv_sec;
|
||||
ts.tv_usec = chunk->tv_usec;
|
||||
addMs(ts, chunk->idx / WIRE_CHUNK_MS_SIZE);
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
static void setTimeval(Chunk* chunk, timeval tv)
|
||||
{
|
||||
chunk->tv_sec = tv.tv_sec;
|
||||
chunk->tv_usec = tv.tv_usec;
|
||||
}
|
||||
|
||||
|
||||
static std::string chunkTime(const Chunk* chunk)
|
||||
{
|
||||
return timeToStr(getTimeval(chunk));
|
||||
}
|
||||
|
||||
|
||||
static long diff_ms(const timeval& t1, const timeval& t2)
|
||||
{
|
||||
return (((t1.tv_sec - t2.tv_sec) * 1000000) +
|
||||
(t1.tv_usec - t2.tv_usec))/1000;
|
||||
}
|
||||
|
||||
|
||||
static long getAge(const Chunk* chunk)
|
||||
{
|
||||
timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
return diff_ms(now, getTimeval(chunk));
|
||||
}
|
||||
|
||||
|
||||
static long getAge(const timeval& tv)
|
||||
{
|
||||
timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
return diff_ms(now, tv);
|
||||
}
|
||||
|
||||
|
||||
static long getTickCount()
|
||||
{
|
||||
|
@ -88,16 +73,6 @@ static long getTickCount()
|
|||
|
||||
|
||||
|
||||
/*
|
||||
static void addMs(Chunk* chunk, int ms)
|
||||
{
|
||||
timeval tv = getTimeval(chunk);
|
||||
addMs(tv, ms);
|
||||
chunk->tv_sec = tv.tv_sec;
|
||||
chunk->tv_usec = tv.tv_usec;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue