From 1e1f6b6c1c5df9e7d4b7aaa0c94cbe5027226593 Mon Sep 17 00:00:00 2001 From: Joni Poikelin Date: Wed, 31 Jan 2024 07:53:05 +0200 Subject: alsa: Prevent buffer underrun errors when QIODevice has no data to read Fixes: QTBUG-114900 Pick-to: 6.6 6.5 Change-Id: I093538ea4f1b4f9fdcf22d38343b4c8aff55a39d Reviewed-by: Artem Dyomin Reviewed-by: Lars Knoll (cherry picked from commit c39a9a4f26fda6560de4ffb0cd4451c7482a9b6e) Reviewed-by: Qt Cherry-pick Bot --- src/multimedia/alsa/qalsaaudiosink.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/multimedia/alsa/qalsaaudiosink.cpp b/src/multimedia/alsa/qalsaaudiosink.cpp index 2417d6051..98a68861f 100644 --- a/src/multimedia/alsa/qalsaaudiosink.cpp +++ b/src/multimedia/alsa/qalsaaudiosink.cpp @@ -157,6 +157,11 @@ void QAlsaAudioSink::start(QIODevice* device) pullMode = true; audioSource = device; + connect(audioSource, &QIODevice::readyRead, timer, [this] { + if (!timer->isActive()) { + timer->start(period_time / 1000); + } + }); deviceState = QAudio::ActiveState; open(); @@ -606,11 +611,13 @@ bool QAlsaAudioSink::deviceReady() } else if(l == 0) { // Did not get any data to output + timer->stop(); + snd_pcm_drain(handle); bytesAvailable = bytesFree(); if(bytesAvailable > snd_pcm_frames_to_bytes(handle, buffer_frames-period_frames)) { // Underrun if (deviceState != QAudio::IdleState) { - errorState = QAudio::UnderrunError; + errorState = audioSource->atEnd() ? QAudio::NoError : QAudio::UnderrunError; emit errorChanged(errorState); deviceState = QAudio::IdleState; emit stateChanged(deviceState); -- cgit v1.2.3