Skip to content

Commit

Permalink
ao: apply backend features only when it's open. fix wang-bin#437
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed May 16, 2015
1 parent 487b7ac commit 7418cc3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/QtAV/AudioOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 90,7 @@ class Q_AV_EXPORT AudioOutput : public QObject, public AVOutput
QString backend() const;
bool open();
bool close();
bool isOpen() const;
/*!
* \brief play
* Play out the given audio data. It may block current thread until the data can be written to audio device
Expand All @@ -114,7 115,7 @@ class Q_AV_EXPORT AudioOutput : public QObject, public AVOutput
* Call this after open(), because it will call backend api if SetVolume feature is enabled
* \param volume linear. 1.0: original volume.
*/
void setVolume(qreal volume);
void setVolume(qreal value);
qreal volume() const;
/*!
* \brief setMute
Expand Down
65 changes: 45 additions & 20 deletions src/output/audio/AudioOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 170,8 @@ class AudioOutputPrivate : public AVOutputPrivate
}
/// call this if sample format or volume is changed
void updateSampleScaleFunc();
void tryVolume(qreal value);
void tryMute(bool value);

bool mute;
bool sw_volume, sw_mute;
Expand Down Expand Up @@ -225,6 227,33 @@ void AudioOutputPrivate::playInitialData()
backend->play();
}

void AudioOutputPrivate::tryVolume(qreal value)
{
// if not open, try later
if (!available)
return;
if (features & AudioOutput::SetVolume) {
sw_volume = !backend->setVolume(value);
//if (!qFuzzyCompare(backend->volume(), value))
// sw_volume = true;
if (sw_volume)
backend->setVolume(1.0); // TODO: partial software?
} else {
sw_volume = true;
}
}

void AudioOutputPrivate::tryMute(bool value)
{
// if not open, try later
if (!available)
return;
if ((features & AudioOutput::SetMute) && backend)
sw_mute = !backend->setMute(value);
else
sw_mute = true;
}

AudioOutput::AudioOutput(QObject* parent)
: QObject(parent)
, AVOutput(*new AudioOutputPrivate())
Expand All @@ -233,7 262,7 @@ AudioOutput::AudioOutput(QObject* parent)
d_func().format.setSampleFormat(AudioFormat::SampleFormat_Signed16);
d_func().format.setChannelLayout(AudioFormat::ChannelLayout_Stero);
static const QStringList all = QStringList()
#if QTAV_HAVE(PULSEAUDIO)
#if QTAV_HAVE(PULSEAUDIO)&& !defined(Q_OS_MAC)
<< "Pulse"
#endif
#if QTAV_HAVE(OPENAL)
Expand Down Expand Up @@ -331,6 360,8 @@ bool AudioOutput::open()
if (!d.backend->open())
return false;
d.available = true;
d.tryVolume(volume());
d.tryMute(isMute());
d.playInitialData();
return true;
}
Expand All @@ -345,6 376,11 @@ bool AudioOutput::close()
return d.backend->close();
}

bool AudioOutput::isOpen() const
{
return d_func().available;
}

bool AudioOutput::play(const QByteArray &data, qreal pts)
{
DPTR_D(AudioOutput);
Expand Down Expand Up @@ -426,25 462,17 @@ int AudioOutput::channels() const
return d_func().format.channels();
}

void AudioOutput::setVolume(qreal volume)
void AudioOutput::setVolume(qreal value)
{
DPTR_D(AudioOutput);
if (volume < 0.0)
if (value < 0.0)
return;
if (d.vol == volume) //fuzzy compare?
if (d.vol == value) //fuzzy compare?
return;
d.vol = volume;
emit volumeChanged(d.vol);
d.vol = value;
Q_EMIT volumeChanged(value);
d.updateSampleScaleFunc();
if (deviceFeatures() & SetVolume) {
d.sw_volume = !d.backend->setVolume(d.vol);
//if (!qFuzzyCompare(deviceGetVolume(), d.vol))
// d.sw_volume = true;
if (d.sw_volume)
d.backend->setVolume(1.0); // TODO: partial software?
} else {
d.sw_volume = true;
}
d.tryVolume(value);
}

qreal AudioOutput::volume() const
Expand All @@ -458,11 486,8 @@ void AudioOutput::setMute(bool value)
if (d.mute == value)
return;
d.mute = value;
emit muteChanged(value);
if ((deviceFeatures() & SetMute) && d.backend)
d.sw_mute = !d.backend->setMute(value);
else
d.sw_mute = true;
Q_EMIT muteChanged(value);
d.tryMute(value);
}

bool AudioOutput::isMute() const
Expand Down

0 comments on commit 7418cc3

Please sign in to comment.