Merge "Fix issue 2428563: Camera rendered inoperable by voice call interruption."
diff --git a/libs/audioflinger/AudioFlinger.cpp b/libs/audioflinger/AudioFlinger.cpp
index 2269352..8089389 100644
--- a/libs/audioflinger/AudioFlinger.cpp
+++ b/libs/audioflinger/AudioFlinger.cpp
@@ -3247,7 +3247,10 @@
                             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 @@
         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 bce3371..ad037d6 100644
--- a/media/libmedia/AudioRecord.cpp
+++ b/media/libmedia/AudioRecord.cpp
@@ -552,13 +552,17 @@
 
         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);
         }