auto import from //branches/cupcake_rel/...@141571
diff --git a/include/media/AudioRecord.h b/include/media/AudioRecord.h
index ff64855..7164b78 100644
--- a/include/media/AudioRecord.h
+++ b/include/media/AudioRecord.h
@@ -335,6 +335,7 @@
     uint32_t                mNotificationFrames;
     uint32_t                mRemainingFrames;
     uint32_t                mMarkerPosition;
+    bool                    mMarkerReached;
     uint32_t                mNewPosition;
     uint32_t                mUpdatePeriod;
 };
diff --git a/include/media/AudioTrack.h b/include/media/AudioTrack.h
index 659f5f8..7645978 100644
--- a/include/media/AudioTrack.h
+++ b/include/media/AudioTrack.h
@@ -409,6 +409,7 @@
     int                     mLoopCount;
     uint32_t                mRemainingFrames;
     uint32_t                mMarkerPosition;
+    bool                    mMarkerReached;
     uint32_t                mNewPosition;
     uint32_t                mUpdatePeriod;
 };
diff --git a/include/media/mediaplayer.h b/include/media/mediaplayer.h
index 58906d1..255a67b 100644
--- a/include/media/mediaplayer.h
+++ b/include/media/mediaplayer.h
@@ -173,6 +173,7 @@
     };
 
     sp<IMediaPlayer>            mPlayer;
+    thread_id_t                 mLockThreadId;
     Mutex                       mLock;
     Mutex                       mNotifyLock;
     Condition                   mSignal;
diff --git a/media/libmedia/AudioRecord.cpp b/media/libmedia/AudioRecord.cpp
index 7594ff0..5c800c3 100644
--- a/media/libmedia/AudioRecord.cpp
+++ b/media/libmedia/AudioRecord.cpp
@@ -200,6 +200,7 @@
     // TODO: add audio hardware input latency here
     mLatency = (1000*mFrameCount) / mSampleRate;
     mMarkerPosition = 0;
+    mMarkerReached = false;
     mNewPosition = 0;
     mUpdatePeriod = 0;
 
@@ -293,6 +294,9 @@
 
     if (android_atomic_and(~1, &mActive) == 1) {
         mAudioRecord->stop();
+        // the record head position will reset to 0, so if a marker is set, we need
+        // to activate it again
+        mMarkerReached = false;
         if (t != 0) {
             t->requestExit();
         } else {
@@ -317,6 +321,7 @@
     if (mCbf == 0) return INVALID_OPERATION;
 
     mMarkerPosition = marker;
+    mMarkerReached = false;
 
     return NO_ERROR;
 }
@@ -492,10 +497,10 @@
     size_t readSize;
 
     // Manage marker callback
-    if (mMarkerPosition > 0) {
+    if (!mMarkerReached && (mMarkerPosition > 0)) {
         if (mCblk->user >= mMarkerPosition) {
             mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
-            mMarkerPosition = 0;
+            mMarkerReached = true;
         }
     }
 
diff --git a/media/libmedia/AudioTrack.cpp b/media/libmedia/AudioTrack.cpp
index 7537ddf..24f7281 100644
--- a/media/libmedia/AudioTrack.cpp
+++ b/media/libmedia/AudioTrack.cpp
@@ -259,6 +259,7 @@
     mLatency = afLatency + (1000*mFrameCount) / mSampleRate;
     mLoopCount = 0;
     mMarkerPosition = 0;
+    mMarkerReached = false;
     mNewPosition = 0;
     mUpdatePeriod = 0;
 
@@ -360,6 +361,9 @@
         // Cancel loops (If we are in the middle of a loop, playback
         // would not stop until loopCount reaches 0).
         setLoop(0, 0, 0);
+        // the playback head position will reset to 0, so if a marker is set, we need
+        // to activate it again
+        mMarkerReached = false;
         // Force flush if a shared buffer is used otherwise audioflinger
         // will not stop before end of buffer is reached.
         if (mSharedBuffer != 0) {
@@ -385,6 +389,12 @@
 void AudioTrack::flush()
 {
     LOGV("flush");
+    
+    // clear playback marker and periodic update counter
+    mMarkerPosition = 0;
+    mMarkerReached = false;
+    mUpdatePeriod = 0;
+    
 
     if (!mActive) {
         mAudioTrack->flush();
@@ -508,6 +518,7 @@
     if (mCbf == 0) return INVALID_OPERATION;
 
     mMarkerPosition = marker;
+    mMarkerReached = false;
 
     return NO_ERROR;
 }
@@ -755,10 +766,10 @@
     }
 
     // Manage marker callback
-    if(mMarkerPosition > 0) {
+    if (!mMarkerReached && (mMarkerPosition > 0)) {
         if (mCblk->server >= mMarkerPosition) {
             mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
-            mMarkerPosition = 0;
+            mMarkerReached = true;
         }
     }
 
diff --git a/media/libmedia/JetPlayer.cpp b/media/libmedia/JetPlayer.cpp
index 77a9013..586aacb 100644
--- a/media/libmedia/JetPlayer.cpp
+++ b/media/libmedia/JetPlayer.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#define LOG_NDEBUG 0
+//#define LOG_NDEBUG 0
 #define LOG_TAG "JetPlayer-C"
 
 #include <utils/Log.h>
diff --git a/media/libmedia/mediaplayer.cpp b/media/libmedia/mediaplayer.cpp
index 6b40412..5841922 100644
--- a/media/libmedia/mediaplayer.cpp
+++ b/media/libmedia/mediaplayer.cpp
@@ -91,6 +91,7 @@
     mLoop = false;
     mLeftVolume = mRightVolume = 1.0;
     mVideoWidth = mVideoHeight = 0;
+    mLockThreadId = 0;
 }
 
 void MediaPlayer::onFirstRef()
@@ -223,16 +224,24 @@
 {
     LOGV("prepare");
     Mutex::Autolock _l(mLock);
-    if (mPrepareSync) return -EALREADY;
+    mLockThreadId = getThreadId();
+    if (mPrepareSync) {
+        mLockThreadId = 0;
+        return -EALREADY;
+    }
     mPrepareSync = true;
     status_t ret = prepareAsync_l();
-    if (ret != NO_ERROR) return ret;
+    if (ret != NO_ERROR) {
+        mLockThreadId = 0;
+        return ret;
+    }
 
     if (mPrepareSync) {
         mSignal.wait(mLock);  // wait for prepare done
         mPrepareSync = false;
     }
     LOGV("prepare complete - status=%d", mPrepareStatus);
+    mLockThreadId = 0;
     return mPrepareStatus;
 }
 
@@ -485,14 +494,23 @@
 {
     LOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
     bool send = true;
+    bool locked = false;
 
     // TODO: In the future, we might be on the same thread if the app is
     // running in the same process as the media server. In that case,
     // this will deadlock.
-    mLock.lock();
+    // 
+    // The threadId hack below works around this for the care of prepare
+    // within the same process.
+
+     if (mLockThreadId != getThreadId()) {
+        mLock.lock();
+        locked = true;
+    } 
+
     if (mPlayer == 0) {
         LOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
-        mLock.unlock();   // release the lock when done.
+        if (locked) mLock.unlock();   // release the lock when done.
         return;
     }
 
@@ -561,7 +579,7 @@
     }
 
     sp<MediaPlayerListener> listener = mListener;
-    mLock.unlock();
+    if (locked) mLock.unlock();
 
     // this prevents re-entrant calls into client code
     if ((listener != 0) && send) {