From ba8811f5528404527b0cbad584a836f0b1807d26 Mon Sep 17 00:00:00 2001 From: Eric Laurent Date: Tue, 2 Mar 2010 18:38:06 -0800 Subject: [PATCH] Fix issue 2428563: Camera rendered inoperable by voice call interruption. The problem is that AudioRecord never exits read() when a timeout occurs while trying to get new PCM data from audio hardware input buffer: it just keeps waiting and retrying until stop() is called. In the same time, opencore AndroidAudioInput::audin_thread_func() loop cannot be exited when stuck in AudioRecord::read() because the iExitAudioThread flag can only be sampled when AudioRecord::read() returns. We remain stuck with the audio input thread running. The fix consists in modifying AudioRecord behavior in case of timeout when getting new PCM samples. We now wait only one timeout period and try to restart audio record, in case the problem is due to a media_server process crash. If this fails, we exit read() with a number of bytes read equals to 0 so that AndroidAudioInput::audin_thread_func() loop can exit. Also modified Audioflinger::RecordThread() loop so that we attempt to recover from HAL read errors. In case of read error, the input stream is forced to standby so that next read attempt does a reconfiguration and restart of the audio input device. --- libs/audioflinger/AudioFlinger.cpp | 10 ++++++++-- media/libmedia/AudioRecord.cpp | 10 +++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/libs/audioflinger/AudioFlinger.cpp b/libs/audioflinger/AudioFlinger.cpp index 2269352e5a323..8089389ffcd65 100644 --- a/libs/audioflinger/AudioFlinger.cpp +++ b/libs/audioflinger/AudioFlinger.cpp @@ -3247,7 +3247,10 @@ bool AudioFlinger::RecordThread::threadLoop() if (mBytesRead < 0) { LOGE("Error reading audio input"); if (mActiveTrack->mState == TrackBase::ACTIVE) { - sleep(1); + // Force input into standby so that it tries to + // recover at next read attempt + mInput->standby(); + usleep(5000); } mRsmpInIndex = mFrameCount; framesOut = 0; @@ -3429,7 +3432,10 @@ status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* if (mBytesRead < 0) { LOGE("RecordThread::getNextBuffer() Error reading audio input"); if (mActiveTrack->mState == TrackBase::ACTIVE) { - sleep(1); + // Force input into standby so that it tries to + // recover at next read attempt + mInput->standby(); + usleep(5000); } buffer->raw = 0; buffer->frameCount = 0; diff --git a/media/libmedia/AudioRecord.cpp b/media/libmedia/AudioRecord.cpp index bce33713f6ec0..ad037d6e42b40 100644 --- a/media/libmedia/AudioRecord.cpp +++ b/media/libmedia/AudioRecord.cpp @@ -552,13 +552,17 @@ ssize_t AudioRecord::read(void* buffer, size_t userSize) audioBuffer.frameCount = userSize/frameSize(); - // Calling obtainBuffer() with a negative wait count causes - // an (almost) infinite wait time. - status_t err = obtainBuffer(&audioBuffer, -1); + // By using a wait count corresponding to twice the timeout period in + // obtainBuffer() we give a chance to recover once for a read timeout + // (if media_server crashed for instance) before returning a length of + // 0 bytes read to the client + status_t err = obtainBuffer(&audioBuffer, ((2 * MAX_RUN_TIMEOUT_MS) / WAIT_PERIOD_MS)); if (err < 0) { // out of buffers, return #bytes written if (err == status_t(NO_MORE_BUFFERS)) break; + if (err == status_t(TIMED_OUT)) + err = 0; return ssize_t(err); }