Improve check for systime vs streamtime

This commit is contained in:
badaix 2019-12-01 22:56:49 +01:00
parent f35efe568a
commit eb94867b57
4 changed files with 37 additions and 5 deletions

View file

@ -225,7 +225,7 @@ void AlsaPlayer::worker()
adjustVolume(buff_, frames_);
if ((pcm = snd_pcm_writei(handle_, buff_, frames_)) == -EPIPE)
{
LOG(ERROR) << "XRUN\n";
LOG(ERROR) << "XRUN: " << snd_strerror(pcm) << "\n";
snd_pcm_prepare(handle_);
}
else if (pcm < 0)

View file

@ -51,6 +51,20 @@ inline static void systemtimeofday(struct timeval* tv)
// timeofday<std::chrono::system_clock>(tv);
}
template <class ToDuration>
inline ToDuration diff(const timeval& tv1, const timeval& tv2)
{
auto sec = tv1.tv_sec - tv2.tv_sec;
auto usec = tv1.tv_usec - tv2.tv_usec;
while (usec < 0)
{
sec -= 1;
usec += 1000000;
}
return std::chrono::duration_cast<ToDuration>(std::chrono::seconds(sec) + std::chrono::microseconds(usec));
}
inline static void addUs(timeval& tv, int us)
{
if (us < 0)
@ -115,7 +129,7 @@ inline void usleep(const int32_t& microseconds)
return;
sleep(usec(microseconds));
}
}
} // namespace chronos
#endif

View file

@ -95,6 +95,22 @@ static std::string uriDecode(const std::string& src)
}
static void split_left(const std::string& s, char delim, std::string& left, std::string& right)
{
auto pos = s.find(delim);
if (pos != std::string::npos)
{
left = s.substr(0, pos);
right = s.substr(pos + 1);
}
else
{
left = s;
right = "";
}
}
static std::vector<std::string>& split(const std::string& s, char delim, std::vector<std::string>& elems)
{

View file

@ -138,12 +138,14 @@ void AsioStream<ReadStream>::do_read()
// First read after connect. Set the initial read timestamp
// the timestamp will be incremented after encoding,
// since we do not know how much the encoder actually encoded
timeval now;
chronos::systemtimeofday(&now);
size_t stream2systime_diff = abs(now.tv_sec - tvEncodedChunk_.tv_sec);
if (stream2systime_diff > 5 + pcmReadMs_ / 1000)
auto stream2systime_diff = chronos::diff<std::chrono::milliseconds>(now, tvEncodedChunk_);
if (stream2systime_diff > chronos::sec(5) + chronos::msec(pcmReadMs_))
{
LOG(WARNING) << "Stream and system time out of sync: " << stream2systime_diff << "s, resetting stream time.\n";
LOG(WARNING) << "Stream and system time out of sync: " << stream2systime_diff.count() << "ms, resetting stream time.\n";
first_ = true;
}
if (first_)