fix underflow for agent audio timer

This commit is contained in:
Zach Pomerantz 2016-10-27 13:33:57 -07:00
parent 745f6546dd
commit 68f05bee87

View file

@ -15,19 +15,23 @@
// this should send a signal every 10ms, with pretty good precision. Hardcoding // this should send a signal every 10ms, with pretty good precision. Hardcoding
// to 10ms since that's what you'd want for audio. // to 10ms since that's what you'd want for audio.
void AvatarAudioTimer::start() { void AvatarAudioTimer::start() {
qDebug() << "AvatarAudioTimer::start called"; qDebug() << __FUNCTION__;
auto startTime = usecTimestampNow(); auto startTime = usecTimestampNow();
quint64 frameCounter = 0; quint64 frameCounter = 0;
const int TARGET_INTERVAL_USEC = 10000; // 10ms const int TARGET_INTERVAL_USEC = 10000; // 10ms
while (!_quit) { while (!_quit) {
frameCounter++; ++frameCounter;
// simplest possible timer
// tick every 10ms from startTime
quint64 targetTime = startTime + frameCounter * TARGET_INTERVAL_USEC; quint64 targetTime = startTime + frameCounter * TARGET_INTERVAL_USEC;
quint64 interval = std::max((quint64)0, targetTime - usecTimestampNow()); quint64 now = usecTimestampNow();
usleep(interval);
// avoid quint64 underflow
if (now < targetTime) {
usleep(targetTime - now);
}
emit avatarTick(); emit avatarTick();
} }
qDebug() << "AvatarAudioTimer is finished"; qDebug() << "AvatarAudioTimer is finished";
} }