diff options
author | Axel Spoerl <axel.spoerl@qt.io> | 2022-05-10 17:41:23 +0200 |
---|---|---|
committer | Axel Spoerl <axel.spoerl@qt.io> | 2022-05-12 14:28:37 +0200 |
commit | 1296351d60ee6f64888bc2181b46d864f51d2201 (patch) | |
tree | 2f2825bff9bfdf6cb6848a4123fd0d061f824e4d | |
parent | fd8b8107e333330d03d7b354503203a6c3ffe722 (diff) |
Fix chunk sizing in QPulseAudioSink
QPulseAudioSink:write() takes a length parameter which is set to the
qMin of itself and the return value of pa_stream_writable_size().
This method returns the number of bytes requested by the server,
but not yet written. Unless data is buffered, the value is 0.
That leads to length being set to 0 and no data being sent.
In PulseOutputPrivate::writeData(), a loop was implemented calling
QPulseAudioSink::write() up to 10 times for each data chunk.
That forced data buffering and subsequently the return value of
pa_stream_writable_size() to be > 0.
This patch replaces the query of pa_stream_writable_size() with the
nbytes value modified by pa_stream_begin_write(). It represents the
maximum amount of bytes the pulseaudio server is ready to accept.
The patch also removes the loop calling write up to 10 times as it
becomes unnecessary.
Fixes: QTBUG-60575
Change-Id: I316a5ac610c8540ab8f7fff4a3b0b1ee3d7e7ad5
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
(cherry picked from commit f1829a24d4c9a393f95d55c415daa80275a90b93)
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
-rw-r--r-- | src/plugins/pulseaudio/qaudiooutput_pulse.cpp | 36 |
1 files changed, 18 insertions, 18 deletions
diff --git a/src/plugins/pulseaudio/qaudiooutput_pulse.cpp b/src/plugins/pulseaudio/qaudiooutput_pulse.cpp index 9526c7d67..f5edafd41 100644 --- a/src/plugins/pulseaudio/qaudiooutput_pulse.cpp +++ b/src/plugins/pulseaudio/qaudiooutput_pulse.cpp @@ -506,27 +506,30 @@ qint64 QPulseAudioOutput::write(const char *data, qint64 len) pulseEngine->lock(); - len = qMin(len, static_cast<qint64>(pa_stream_writable_size(m_stream))); + size_t nbytes = len; + void *dest = nullptr; + + if (pa_stream_begin_write(m_stream, &dest, &nbytes) < 0) { + qWarning("QAudioSink(pulseaudio): pa_stream_begin_write, error = %s", + pa_strerror(pa_context_errno(pulseEngine->context()))); + setError(QAudio::IOError); + return 0; + } + + len = qMin(len, qint64(nbytes)); if (m_volume < 1.0f) { // Don't use PulseAudio volume, as it might affect all other streams of the same category // or even affect the system volume if flat volumes are enabled - void *dest = NULL; - size_t nbytes = len; - if (pa_stream_begin_write(m_stream, &dest, &nbytes) < 0) { - qWarning("QAudioOutput(pulseaudio): pa_stream_begin_write, error = %s", - pa_strerror(pa_context_errno(pulseEngine->context()))); - setError(QAudio::IOError); - return 0; - } - - len = int(nbytes); QAudioHelperInternal::qMultiplySamples(m_volume, m_format, data, dest, len); - data = reinterpret_cast<char *>(dest); + } else { + memcpy(dest, data, len); } - if (pa_stream_write(m_stream, data, len, NULL, 0, PA_SEEK_RELATIVE) < 0) { - qWarning("QAudioOutput(pulseaudio): pa_stream_write, error = %s", + data = reinterpret_cast<char *>(dest); + + if ((pa_stream_write(m_stream, data, len, nullptr, 0, PA_SEEK_RELATIVE)) < 0) { + qWarning("QAudioSink(pulseaudio): pa_stream_write, error = %s", pa_strerror(pa_context_errno(pulseEngine->context()))); setError(QAudio::IOError); return 0; @@ -683,7 +686,6 @@ qint64 PulseOutputPrivate::readData(char *data, qint64 len) qint64 PulseOutputPrivate::writeData(const char *data, qint64 len) { - int retry = 0; qint64 written = 0; if ((m_audioDevice->m_deviceState == QAudio::ActiveState @@ -691,10 +693,8 @@ qint64 PulseOutputPrivate::writeData(const char *data, qint64 len) while(written < len) { int chunk = m_audioDevice->write(data+written, (len-written)); if (chunk <= 0) - retry++; - written+=chunk; - if (retry > 10) return written; + written+=chunk; } } |