Reject incomplete DB reads and clock voice progress from PCM outside paint

This commit is contained in:
evgeny committed 2026-10-28 00:59:00 +01:00
1 parent a17c4ae435
commit 1ab2d17c8e
2 files changed
+84 -119

No files matched your search

+18 -42
View File
@@ -26,6 +26,7 @@
#include <QDebug>
#include <QImageReader>
#include <QPersistentModelIndex>
#include <QPointer>
/* Один layout для размеров, отрисовки, выделения и hit-test ссылок. */
static qreal layoutMessageText(QTextLayout& layout, int width, const QString& compactLink = {}) {
@@ -92,7 +93,6 @@ struct VoicePlayState {
bool loading = false;
bool playing = false;
bool paused = false;
qint64 startMs = 0;
float durationSec = 0.0f;
float pausedPosSec = 0.0f;
float displayFrac = 0.0f;
@@ -111,19 +111,17 @@ static void ensureVoiceTimer() {
bool anyActive = false;
SoundManager* sm = SoundManager::instance();
for (auto it = s_voiceStates.begin(); it != s_voiceStates.end(); ++it) {
if (it->loading) { anyActive = true; continue; }
if (!it->playing && !it->paused) {
if (it->displayFrac > 0.001f || it->displayElapsed > 0.001f) anyActive = true;
continue;
}
if (it->paused) { anyActive = true; continue; }
if (!sm->isPcmPlaying(it->playbackId)) {
it->playing = false;
it->paused = false;
anyActive = true;
} else {
anyActive = true;
}
if (it->loading && !VoicePlayer::isPreparing()) it->loading = false;
if (it->playing && !sm->isPcmPlaying(it->playbackId)) it->playing = false;
float elapsed = it->paused ? it->pausedPosSec : 0;
const int rate = sm->pcmSampleRate(it->playbackId);
if (it->playing && rate > 0) elapsed = float(double(sm->pcmCursor(it->playbackId)) / rate);
elapsed = qBound(0.0f, elapsed, it->durationSec);
const float fraction = it->durationSec > 0 ? elapsed / it->durationSec : 0;
const bool moving = std::fabs(fraction - it->displayFrac) > 0.001f || std::fabs(elapsed - it->displayElapsed) > 0.001f;
it->displayFrac = moving ? it->displayFrac + (fraction - it->displayFrac) * 0.15f : fraction;
it->displayElapsed = moving ? it->displayElapsed + (elapsed - it->displayElapsed) * 0.15f : elapsed;
anyActive |= it->loading || it->playing || moving;
}
if (!anyActive) s_voiceTimer->stop();
if (s_voiceView) s_voiceView->viewport()->update();
@@ -288,7 +286,6 @@ bool MessageDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
float clickFrac = (float)(localPos.x() - L.voiceWaveformRect.x()) / L.voiceWaveformRect.width();
if (clickFrac < 0) clickFrac = 0; if (clickFrac > 1) clickFrac = 1;
float targetSec = clickFrac * dur;
qint64 now = QDateTime::currentMSecsSinceEpoch();
if (!st.playing && !st.paused) {
/* Start playback then seek */
@@ -300,7 +297,7 @@ bool MessageDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
MessageDelegate::startAutoPlay(filePath, actualDur, playbackId);
float position = clickFrac * actualDur;
auto& state = s_voiceStates[filePath];
state.startMs -= (qint64)(position * 1000); state.pausedPosSec = position;
state.pausedPosSec = position;
state.displayFrac = clickFrac; state.displayElapsed = position;
int rate = SoundManager::instance()->pcmSampleRate(playbackId);
if (rate > 0) SoundManager::instance()->seekPcm(playbackId, (unsigned long long)(position * rate));
@@ -309,7 +306,6 @@ bool MessageDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
s_voiceTimer->start();
} else {
/* Currently playing or paused — seek */
st.startMs = now - (qint64)(targetSec * 1000.0);
if (st.paused)
st.pausedPosSec = targetSec;
else if (!st.playing)
@@ -332,8 +328,8 @@ bool MessageDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
if (st.playing) {
/* Playing → Pause */
qint64 now = QDateTime::currentMSecsSinceEpoch();
st.pausedPosSec = (now - st.startMs) / 1000.0f;
const int rate = SoundManager::instance()->pcmSampleRate(st.playbackId);
st.pausedPosSec = rate > 0 ? float(double(SoundManager::instance()->pcmCursor(st.playbackId)) / rate) : 0;
st.playing = false;
st.paused = true;
SoundManager::instance()->pausePcm(st.playbackId);
@@ -345,8 +341,6 @@ bool MessageDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
/* Paused → Resume */
st.paused = false;
st.playing = true;
qint64 now = QDateTime::currentMSecsSinceEpoch();
st.startMs = now - (qint64)(st.pausedPosSec * 1000.0);
st.displayFrac = (dur > 0) ? (st.pausedPosSec / dur) : 0;
st.displayElapsed = st.pausedPosSec;
SoundManager::instance()->resumePcm(st.playbackId);
@@ -1108,29 +1102,11 @@ void MessageDelegate::paintBody(QPainter* painter, const QStyleOptionViewItem& o
bool isPaused = st->paused;
/* compute elapsed/frac for progress */
float elapsed = 0, targetFrac = 0, durSec = st->durationSec;
float durSec = st->durationSec;
if (durSec <= 0) {
QString ds = index.data(MsgVoiceDurationRole).toString();
ds.remove('s'); durSec = ds.toFloat();
}
if (isPaused) {
elapsed = st->pausedPosSec;
} else if (isPlaying) {
SoundManager* sm = SoundManager::instance();
if (sm->isPcmPlaying(st->playbackId)) {
int sr = sm->pcmSampleRate(st->playbackId);
if (sr > 0) elapsed = (float)((double)sm->pcmCursor(st->playbackId) / sr);
}
if (elapsed <= 0) {
qint64 now = QDateTime::currentMSecsSinceEpoch();
elapsed = (now - st->startMs) / 1000.0f;
}
if (elapsed > durSec) elapsed = durSec;
}
targetFrac = (durSec > 0) ? (elapsed / durSec) : 0;
if (targetFrac < 0) targetFrac = 0; if (targetFrac > 1) targetFrac = 1;
st->displayFrac += (targetFrac - st->displayFrac) * 0.15f;
st->displayElapsed += (elapsed - st->displayElapsed) * 0.15f;
float frac = st->displayFrac;
QRectF playR = L.voicePlayBtnRect;
@@ -1714,14 +1690,14 @@ void MessageDelegate::drawAvatar(QPainter *p, const Layout &L,
}
void MessageDelegate::startAutoPlay(const QString& filePath, float actualDurationSec, uint64_t playbackId) {
if (actualDurationSec <= 0) return;
if (actualDurationSec <= 0 || !playbackId) { GUI_WARN("voice delegate: invalid playback duration/id"); return; }
ensureVoiceTimer();
s_voiceStates.clear();
VoicePlayState& st = s_voiceStates[filePath];
st.playbackId = playbackId;
st.playing = true;
st.paused = false;
st.durationSec = (actualDurationSec > 0) ? actualDurationSec : 1.0f;
st.startMs = QDateTime::currentMSecsSinceEpoch();
st.pausedPosSec = 0.0f;
st.displayFrac = 0.0f;
st.displayElapsed = 0.0f;