Skip to content

Commit

Permalink
Merge pull request #12516 from daschuer/visualplayposclamp_2
Browse files Browse the repository at this point in the history
Improved loging in case of  clamping visual position
  • Loading branch information
JoergAtGithub authored Jan 20, 2024
2 parents dc22fd5 + 12f0bab commit 85f3f06
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
25 changes: 21 additions & 4 deletions src/waveform/visualplayposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,46 @@ double VisualPlayPosition::calcOffsetAtNextVSync(
const int refToVSync = pVSyncThread->fromTimerToNextSyncMicros(data.m_referenceTime);
const int syncIntervalTimeMicros = pVSyncThread->getSyncIntervalTimeMicros();
#endif
// The offset is limited to the audio buffer + 2 x waveform sync interval
// The positive offset is limited to the audio buffer + 2 x waveform sync interval
// This should be sufficient to compensate jitter, but does not continue
// in case of underflows.
const int maxOffset = static_cast<int>(
data.m_audioBufferMicroS + 2 * syncIntervalTimeMicros);

// The minimum offset is limited to -data.m_callbackEntrytoDac to avoid a more
// negative value indicating an outdated request that is no longer valid anyway.
// This is probably caused by a vsync problem.
const int minOffset = -data.m_callbackEntrytoDac;

// Calculate the offset in micros for the position of the sample that will be transferred
// to the DAC when the next display frame is displayed
int offset = refToVSync - data.m_callbackEntrytoDac;
if (offset < -maxOffset) {
offset = -maxOffset;
if (offset < minOffset) {
offset = minOffset;
if (!m_noTransport) {
qWarning() << "VisualPlayPosition::calcOffsetAtNextVSync"
<< m_key << "no transport (offset < -maxOffset)";
<< m_key << "outdated position request (offset < minOffset)";
qDebug() << m_key << "refToVSync:" << refToVSync
<< "data.m_callbackEntrytoDac:"
<< data.m_callbackEntrytoDac;
m_noTransport = true;
}
} else if (offset > maxOffset) {
offset = maxOffset;
if (!m_noTransport) {
qWarning() << "VisualPlayPosition::calcOffsetAtNextVSync"
<< m_key << "no transport (offset > maxOffset)";
qDebug() << m_key << "refToVSync:" << refToVSync
<< "data.m_callbackEntrytoDac:"
<< data.m_callbackEntrytoDac;
m_noTransport = true;
}
} else {
if (m_noTransport) {
qDebug() << m_key << "refToVSync:" << refToVSync
<< "data.m_callbackEntrytoDac:"
<< data.m_callbackEntrytoDac;
}
m_noTransport = false;
}
// Apply the offset proportional to m_positionStep
Expand Down
21 changes: 15 additions & 6 deletions src/waveform/vsyncthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ void VSyncThread::run() {
//qDebug() << "VSyncThread::run()";
switch (m_vSyncMode) {
case ST_FREE:
m_syncIntervalTimeMicros = 1000;
m_waitToSwapMicros = m_syncIntervalTimeMicros;
runFree();
break;
case ST_PLL:
Expand Down Expand Up @@ -65,8 +67,7 @@ void VSyncThread::runFree() {
m_semaVsyncSlot.acquire();

m_sinceLastSwap = m_timer.restart();
m_waitToSwapMicros = 1000;
usleep(1000);
usleep(m_waitToSwapMicros);
}
}

Expand Down Expand Up @@ -168,9 +169,11 @@ int VSyncThread::elapsed() {
}

void VSyncThread::setSyncIntervalTimeMicros(int syncTime) {
m_syncIntervalTimeMicros = syncTime;
m_vSyncPerRendering = static_cast<int>(
round(m_displayFrameRate * m_syncIntervalTimeMicros / 1000));
if (m_vSyncMode != ST_FREE) {
m_syncIntervalTimeMicros = syncTime;
m_vSyncPerRendering = static_cast<int>(
round(m_displayFrameRate * m_syncIntervalTimeMicros / 1000));
}
}

void VSyncThread::setVSyncType(int type) {
Expand All @@ -187,7 +190,13 @@ void VSyncThread::setVSyncType(int type) {
int VSyncThread::fromTimerToNextSyncMicros(const PerformanceTimer& timer) {
int difference = static_cast<int>(m_timer.difference(timer).toIntegerMicros());
// int math is fine here, because we do not expect times > 4.2 s
return difference + m_waitToSwapMicros;
int toNextSync = difference + m_waitToSwapMicros;
while (toNextSync < 0) {
// this function is called during rendering. A negative value indicates
// an attempt to render an outdated frame. Render the next frame instead
toNextSync += m_syncIntervalTimeMicros;
}
return toNextSync;
}

int VSyncThread::droppedFrames() {
Expand Down

0 comments on commit 85f3f06

Please sign in to comment.