Skip to content

Commit

Permalink
Set volume to QAVAudioOutput ASAP
Browse files Browse the repository at this point in the history
  • Loading branch information
valbok committed Jan 22, 2024
1 parent 3051db7 commit e2a4fcb
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/QtAVPlayer/qavaudiooutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit e2a4fcb

Please sign in to comment.