From e2a4fcbca41d24a0eb6fd8e89a5390e23e96df7a Mon Sep 17 00:00:00 2001 From: VaL Doroshchuk Date: Mon, 22 Jan 2024 18:09:00 +0100 Subject: [PATCH] Set volume to QAVAudioOutput ASAP --- src/QtAVPlayer/qavaudiooutput.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/QtAVPlayer/qavaudiooutput.cpp b/src/QtAVPlayer/qavaudiooutput.cpp index e7ac47c3..354c15f5 100644 --- a/src/QtAVPlayer/qavaudiooutput.cpp +++ b/src/QtAVPlayer/qavaudiooutput.cpp @@ -184,10 +184,9 @@ class QAVAudioOutputPrivate : public QIODevice if (bufferSize > 0) audioOutput->setBufferSize(bufferSize); + audioOutput->setVolume(volume); audioOutput->start(this); } - - audioOutput->setVolume(volume); } void doPlayAudio() @@ -199,28 +198,35 @@ class QAVAudioOutputPrivate : public QIODevice #if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0) fmt.setChannelConfig(channelConfig); #endif - locker.unlock(); if (fmt.isValid()) init(fmt); + locker.unlock(); QCoreApplication::processEvents(); } + QMutexLocker locker(&mutex); if (audioOutput) { audioOutput->stop(); audioOutput->deleteLater(); } audioOutput = nullptr; } + + void startThreadIfNeeded() + { + if (!audioPlayFuture.isRunning()) { +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + audioPlayFuture = QtConcurrent::run(&threadPool, this, &QAVAudioOutputPrivate::doPlayAudio); +#else + audioPlayFuture = QtConcurrent::run(&threadPool, &QAVAudioOutputPrivate::doPlayAudio, this); +#endif + } + } }; QAVAudioOutput::QAVAudioOutput(QObject *parent) : QObject(parent) , d_ptr(new QAVAudioOutputPrivate) { -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) - d_ptr->audioPlayFuture = QtConcurrent::run(&d_ptr->threadPool, d_ptr.get(), &QAVAudioOutputPrivate::doPlayAudio); -#else - d_ptr->audioPlayFuture = QtConcurrent::run(&d_ptr->threadPool, &QAVAudioOutputPrivate::doPlayAudio, d_ptr.get()); -#endif } QAVAudioOutput::~QAVAudioOutput() @@ -236,6 +242,8 @@ void QAVAudioOutput::setVolume(qreal v) Q_D(QAVAudioOutput); QMutexLocker locker(&d->mutex); d->volume = v; + if (d->audioOutput) + d->audioOutput->setVolume(v); } qreal QAVAudioOutput::volume() const @@ -250,6 +258,8 @@ void QAVAudioOutput::setBufferSize(int bytes) Q_D(QAVAudioOutput); QMutexLocker locker(&d->mutex); d->bufferSize = bytes; + if (d->bufferSize > 0 && d->audioOutput) + d->audioOutput->setBufferSize(d->bufferSize); } int QAVAudioOutput::bufferSize() const @@ -283,6 +293,7 @@ bool QAVAudioOutput::play(const QAVAudioFrame &frame) return false; QMutexLocker locker(&d->mutex); + d->startThreadIfNeeded(); d->frames.push_back(frame); d->cond.wakeAll();