auto import from //branches/cupcake_rel/...@138607
diff --git a/libs/audioflinger/AudioFlinger.cpp b/libs/audioflinger/AudioFlinger.cpp
index 57a53bd..5652b28 100644
--- a/libs/audioflinger/AudioFlinger.cpp
+++ b/libs/audioflinger/AudioFlinger.cpp
@@ -837,12 +837,12 @@
 }
 
 #ifdef WITH_A2DP
-void AudioFlinger::handleStreamDisablesA2dp(int command)
+// handleStreamDisablesA2dp_l() must be called with AudioFlinger::mLock held
+void AudioFlinger::handleStreamDisablesA2dp_l(int command)
 {
     switch(command) {
     case ACTIVE_TRACK_ADDED:
         {
-            AutoMutex lock(mHardwareLock);
             if (mA2dpDisableCount++ == 0) {
                 if (mA2dpEnabled) {
                     setA2dpEnabled_l(false);
@@ -854,8 +854,7 @@
         break;
     case ACTIVE_TRACK_REMOVED:
         {
-            AutoMutex lock(mHardwareLock);
-            if (mA2dpDisableCount > 0){
+            if (mA2dpDisableCount > 0) {
                 if (--mA2dpDisableCount == 0) {
                     if (mA2dpSuppressed) {
                         setA2dpEnabled_l(true);
@@ -1502,8 +1501,10 @@
             mAudioFlinger->handleForcedSpeakerRoute(ACTIVE_TRACK_ADDED);
         }        
 #ifdef WITH_A2DP
+        // AudioFlinger::mLock must be locked before calling
+        // handleStreamDisablesA2dp_l because it calls setA2dpEnabled_l().
         if (streamDisablesA2dp(track->type())) {
-            mAudioFlinger->handleStreamDisablesA2dp(ACTIVE_TRACK_ADDED);
+            mAudioFlinger->handleStreamDisablesA2dp_l(ACTIVE_TRACK_ADDED);
         }
 #endif
     }
@@ -1524,8 +1525,10 @@
             mAudioFlinger->handleForcedSpeakerRoute(ACTIVE_TRACK_REMOVED);
         }
 #ifdef WITH_A2DP
+        // AudioFlinger::mLock must be locked before calling
+        // handleStreamDisablesA2dp_l because it calls setA2dpEnabled_l().
         if (streamDisablesA2dp(track->type())) {
-            mAudioFlinger->handleStreamDisablesA2dp(ACTIVE_TRACK_REMOVED);
+            mAudioFlinger->handleStreamDisablesA2dp_l(ACTIVE_TRACK_REMOVED);
         }
 #endif
     }
@@ -2476,8 +2479,15 @@
     mRecordTrack = recordTrack;
 
 #ifdef WITH_A2DP
-    if (streamDisablesA2dp(recordTrack->type())) {
-        mAudioFlinger->handleStreamDisablesA2dp(ACTIVE_TRACK_ADDED);
+    { // scope for lock2
+
+        // AudioFlinger::mLock must be locked before calling
+        // handleStreamDisablesA2dp_l because it calls setA2dpEnabled_l().
+        AutoMutex lock2(&mAudioFlinger->mLock);
+
+        // Currently there is no way to detect if we are recording over SCO,
+        // so we disable A2DP during any recording.
+        mAudioFlinger->handleStreamDisablesA2dp_l(ACTIVE_TRACK_ADDED);
     }
 #endif
 
@@ -2494,8 +2504,15 @@
     AutoMutex lock(&mLock);
     if (mActive && (recordTrack == mRecordTrack.get())) {
 #ifdef WITH_A2DP
-        if (streamDisablesA2dp(recordTrack->type())) {
-            mAudioFlinger->handleStreamDisablesA2dp(ACTIVE_TRACK_REMOVED);
+        { // scope for lock2
+    
+            // AudioFlinger::mLock must be locked before calling
+            // handleStreamDisablesA2dp_l because it calls setA2dpEnabled_l().
+            AutoMutex lock2(&mAudioFlinger->mLock);
+
+            // Currently there is no way to detect if we are recording over SCO,
+            // so we disable A2DP during any recording.
+            mAudioFlinger->handleStreamDisablesA2dp_l(ACTIVE_TRACK_REMOVED);
         }
 #endif
         mActive = false;