summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/multimedia/audio/qaudioinput_alsa_p.cpp54
-rw-r--r--src/multimedia/audio/qaudioinput_alsa_p.h1
2 files changed, 40 insertions, 15 deletions
diff --git a/src/multimedia/audio/qaudioinput_alsa_p.cpp b/src/multimedia/audio/qaudioinput_alsa_p.cpp
index c9a8b71..58669b3 100644
--- a/src/multimedia/audio/qaudioinput_alsa_p.cpp
+++ b/src/multimedia/audio/qaudioinput_alsa_p.cpp
@@ -120,7 +120,7 @@ int QAudioInputPrivate::xrun_recovery(int err)
if(err < 0)
reset = true;
else {
- bytesAvailable = bytesReady();
+ bytesAvailable = checkBytesReady();
if (bytesAvailable <= 0)
reset = true;
}
@@ -408,7 +408,7 @@ bool QAudioInputPrivate::open()
snd_pcm_start(handle);
// Step 5: Setup timer
- bytesAvailable = bytesReady();
+ bytesAvailable = checkBytesReady();
if(pullMode)
connect(audioSource,SIGNAL(readyRead()),this,SLOT(userFeed()));
@@ -437,19 +437,29 @@ void QAudioInputPrivate::close()
}
}
-int QAudioInputPrivate::bytesReady() const
+int QAudioInputPrivate::checkBytesReady()
{
if(resuming)
- return period_size;
-
- if(deviceState != QAudio::ActiveState && deviceState != QAudio::IdleState)
- return 0;
- int frames = snd_pcm_avail_update(handle);
- if (frames < 0) return frames;
- if((int)frames > (int)buffer_frames)
- frames = buffer_frames;
+ bytesAvailable = period_size;
+ else if(deviceState != QAudio::ActiveState
+ && deviceState != QAudio::IdleState)
+ bytesAvailable = 0;
+ else {
+ int frames = snd_pcm_avail_update(handle);
+ if (frames < 0) {
+ bytesAvailable = frames;
+ } else {
+ if((int)frames > (int)buffer_frames)
+ frames = buffer_frames;
+ bytesAvailable = snd_pcm_frames_to_bytes(handle, frames);
+ }
+ }
+ return bytesAvailable;
+}
- return snd_pcm_frames_to_bytes(handle, frames);
+int QAudioInputPrivate::bytesReady() const
+{
+ return qMax(bytesAvailable, 0);
}
qint64 QAudioInputPrivate::read(char* data, qint64 len)
@@ -460,12 +470,12 @@ qint64 QAudioInputPrivate::read(char* data, qint64 len)
if ( !handle )
return 0;
- bytesAvailable = bytesReady();
+ bytesAvailable = checkBytesReady();
if (bytesAvailable < 0) {
// bytesAvailable as negative is error code, try to recover from it.
xrun_recovery(bytesAvailable);
- bytesAvailable = bytesReady();
+ bytesAvailable = checkBytesReady();
if (bytesAvailable < 0) {
// recovery failed must stop and set error.
close();
@@ -639,11 +649,25 @@ bool QAudioInputPrivate::deviceReady()
InputPrivate* a = qobject_cast<InputPrivate*>(audioSource);
a->trigger();
}
- bytesAvailable = bytesReady();
+ bytesAvailable = checkBytesReady();
if(deviceState != QAudio::ActiveState)
return true;
+ if (bytesAvailable < 0) {
+ // bytesAvailable as negative is error code, try to recover from it.
+ xrun_recovery(bytesAvailable);
+ bytesAvailable = checkBytesReady();
+ if (bytesAvailable < 0) {
+ // recovery failed must stop and set error.
+ close();
+ errorState = QAudio::IOError;
+ deviceState = QAudio::StoppedState;
+ emit stateChanged(deviceState);
+ return 0;
+ }
+ }
+
if(intervalTime && (timeStamp.elapsed() + elapsedTimeOffset) > intervalTime) {
emit notify();
elapsedTimeOffset = timeStamp.elapsed() + elapsedTimeOffset - intervalTime;
diff --git a/src/multimedia/audio/qaudioinput_alsa_p.h b/src/multimedia/audio/qaudioinput_alsa_p.h
index 92cef83..191235e 100644
--- a/src/multimedia/audio/qaudioinput_alsa_p.h
+++ b/src/multimedia/audio/qaudioinput_alsa_p.h
@@ -109,6 +109,7 @@ private slots:
bool deviceReady();
private:
+ int checkBytesReady();
int xrun_recovery(int err);
int setFormat();
bool open();