Skip to content

Commit

Permalink
add isAvailabe() for AVOutput. If open init/failed, it's false
Browse files Browse the repository at this point in the history
TODO: DO NOT decode(or at least convert data) if the output is not
available
  • Loading branch information
wang-bin committed Jan 5, 2013
1 parent 9ea28ec commit 5335957
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
7 changes: 5 additions & 2 deletions src/AOPortAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,13 @@ bool AOPortAudio::open()
//
d.outputParameters->channelCount = d.channels;
PaError err = Pa_OpenStream(&d.stream, NULL, d.outputParameters, d.sample_rate, 0, paNoFlag, NULL, NULL);
if (err == paNoError)
if (err == paNoError) {
d.outputLatency = Pa_GetStreamInfo(d.stream)->outputLatency;
else
d.available = true;
} else {
qWarning("Open portaudio stream error: %s", Pa_GetErrorText(err));
d.available = false;
}
return err == paNoError;
}

Expand Down
5 changes: 5 additions & 0 deletions src/AVOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ bool AVOutput::writeData(const QByteArray &data)
return result;
}

bool AVOutput::isAvailable() const
{
return d_func().available;
}

void AVOutput::pause(bool p)
{
DPTR_D(AVOutput);
Expand Down
3 changes: 2 additions & 1 deletion src/QtAV/AVOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class Q_EXPORT AVOutput
AVOutput();
virtual ~AVOutput() = 0;
//Call tryPause() first to try to pause
bool writeData(const QByteArray& data);
bool writeData(const QByteArray& data);
bool isAvailable() const;
virtual bool open() = 0;
virtual bool close() = 0;
//Demuxer thread automatically paused because packets will be full
Expand Down
19 changes: 16 additions & 3 deletions src/QtAV/private/AOPortAudio_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,38 @@ class Q_EXPORT AOPortAudioPrivate : public AudioOutputPrivate
{
public:
AOPortAudioPrivate():outputParameters(new PaStreamParameters)
,stream(0)
,stream(0),initialized(true)
{
Pa_Initialize();
PaError err = paNoError;
if ((err = Pa_Initialize()) != paNoError) {
qWarning("Error when init portaudio: %s", Pa_GetErrorText(err));
available = false;
initialized = false;
return;
}

memset(outputParameters, 0, sizeof(PaStreamParameters));
outputParameters->device = Pa_GetDefaultOutputDevice();
if (outputParameters->device == paNoDevice) {
qWarning("PortAudio get device error!");
available = false;
return;
}
outputParameters->sampleFormat = paFloat32;
outputParameters->hostApiSpecificStreamInfo = NULL;
outputParameters->suggestedLatency = Pa_GetDeviceInfo(outputParameters->device)->defaultHighOutputLatency;

}
~AOPortAudioPrivate() {
Pa_Terminate();
if (initialized)
Pa_Terminate(); //Do NOT call this if init failed. See document
if (outputParameters) {
delete outputParameters;
outputParameters = 0;
}
}

bool initialized;
PaStreamParameters *outputParameters;
PaStream *stream;
#ifdef Q_OS_LINUX
Expand Down
3 changes: 2 additions & 1 deletion src/QtAV/private/AVOutput_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ class AVDecoder;
class Q_EXPORT AVOutputPrivate : public DPtrPrivate<AVOutput>
{
public:
AVOutputPrivate():paused(false) {}
AVOutputPrivate():paused(false),available(true) {}
virtual ~AVOutputPrivate() {
cond.wakeAll(); //WHY: failed to wake up
}

bool paused;
bool available;
QMutex mutex; //pause
QWaitCondition cond; //pause
};
Expand Down
4 changes: 2 additions & 2 deletions src/VideoThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ void VideoThread::run()
d.width = dec->width();
d.height = dec->height();
d.decoded_data = d.dec->data();
if (d.writer) {
if (d.writer && d.writer->isAvailable()) {
((VideoRenderer*)d.writer)->setSourceSize(dec->width(), dec->height());
d.writer->writeData(d.dec->data());
d.writer->writeData(d.dec->data());
}
//qApp->processEvents(QEventLoop::AllEvents);
}
Expand Down

0 comments on commit 5335957

Please sign in to comment.