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 06e9aac
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/QtAVPlayer/qavaudiooutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class QAVAudioOutputPrivate : public QIODevice
bool isSequential() const override { return true; }
bool atEnd() const override { return false; }

void init(const QAudioFormat &fmt)
void init(const QAudioFormat &fmt, qreal v, int bsize)
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
auto audioDevice = QAudioDeviceInfo::defaultOutputDevice();
Expand Down Expand Up @@ -182,26 +182,26 @@ class QAVAudioOutputPrivate : public QIODevice
}
});

if (bufferSize > 0)
audioOutput->setBufferSize(bufferSize);
if (bsize > 0)
audioOutput->setBufferSize(bsize);
audioOutput->start(this);
}

audioOutput->setVolume(volume);
audioOutput->setVolume(v);
}

void doPlayAudio()
{
while (!quit) {
QMutexLocker locker(&mutex);
cond.wait(&mutex);
auto fmt = !frames.isEmpty() ? format(frames.first().format()) : QAudioFormat();
auto fmt = !frames.isEmpty() ? format(frames.first().format()) : QAudioFormat();
#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
fmt.setChannelConfig(channelConfig);
#endif
auto v = volume;
auto bsize = bufferSize;
locker.unlock();
if (fmt.isValid())
init(fmt);
init(fmt, v, bsize);
QCoreApplication::processEvents();
}
if (audioOutput) {
Expand All @@ -210,17 +210,23 @@ class QAVAudioOutputPrivate : public QIODevice
}
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,7 @@ void QAVAudioOutput::setVolume(qreal v)
Q_D(QAVAudioOutput);
QMutexLocker locker(&d->mutex);
d->volume = v;
d->cond.wakeAll();
}

qreal QAVAudioOutput::volume() const
Expand All @@ -250,6 +257,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 +292,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 06e9aac

Please sign in to comment.