Fix issues with synchronous record start.

- Added a timeout in case the trigger event is never fired.
- Extend AudioRecord obtainBuffer() timeout in case of
synchronous start to avoid spurious warning.
- Make sure that the event is triggered if the track is
destroyed.
- Reject event if the triggering track is in an incompatible state.

Also fix a problem when restoring a static AudioTrack after
a mediaserver crash.

Bug 6449468.

Change-Id: Ib36e11111fb88f73caa31dcb0622792737d57a4b
diff --git a/media/libmedia/AudioRecord.cpp b/media/libmedia/AudioRecord.cpp
index c21979b..0562f8e 100644
--- a/media/libmedia/AudioRecord.cpp
+++ b/media/libmedia/AudioRecord.cpp
@@ -334,7 +334,8 @@
         cblk->lock.unlock();
         if (ret == NO_ERROR) {
             mNewPosition = cblk->user + mUpdatePeriod;
-            cblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
+            cblk->bufferTimeoutMs = (event == AudioSystem::SYNC_EVENT_NONE) ? MAX_RUN_TIMEOUT_MS :
+                                            AudioSystem::kSyncRecordStartTimeOutMs;
             cblk->waitTimeMs = 0;
             if (t != 0) {
                 // thread unblocks in readyToRun() and returns NO_ERROR
@@ -569,6 +570,8 @@
     }
 
     cblk->waitTimeMs = 0;
+    // reset time out to running value after obtaining a buffer
+    cblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
 
     if (framesReq > framesReady) {
         framesReq = framesReady;
diff --git a/media/libmedia/AudioTrack.cpp b/media/libmedia/AudioTrack.cpp
index 6189be5..5e6cd51 100644
--- a/media/libmedia/AudioTrack.cpp
+++ b/media/libmedia/AudioTrack.cpp
@@ -1365,6 +1365,9 @@
                     mCblk->stepUser(frames);
                 }
             }
+            if (mSharedBuffer != 0) {
+                mCblk->stepUser(mCblk->frameCount);
+            }
             if (mActive) {
                 result = mAudioTrack->start();
                 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);