auto import from //depot/cupcake/@132589
diff --git a/libs/audioflinger/A2dpAudioInterface.cpp b/libs/audioflinger/A2dpAudioInterface.cpp
index eb00f8c..d1b7af3 100644
--- a/libs/audioflinger/A2dpAudioInterface.cpp
+++ b/libs/audioflinger/A2dpAudioInterface.cpp
@@ -131,7 +131,8 @@
 // ----------------------------------------------------------------------------
 
 A2dpAudioInterface::A2dpAudioStreamOut::A2dpAudioStreamOut() :
-    mFd(-1), mStandby(true), mStartCount(0), mRetryCount(0), mData(NULL)
+    mFd(-1), mStandby(true), mStartCount(0), mRetryCount(0), mData(NULL),
+    mInitialized(false)
 {
     // use any address by default
     strncpy(mA2dpAddress, "00:00:00:00:00:00", sizeof(mA2dpAddress));
@@ -166,14 +167,13 @@
     status_t status = NO_INIT;
     size_t remaining = bytes;
 
-    if (!mData) {
-        status = a2dp_init(44100, 2, &mData);
+    if (!mInitialized) {
+        status = a2dp_init(mA2dpAddress, 44100, 2, &mData);
         if (status < 0) {
             LOGE("a2dp_init failed err: %d\n", status);
-            mData = NULL;
             goto Error;
         }
-        a2dp_set_sink(mData, mA2dpAddress);
+        mInitialized = true;
     }
     
     while (remaining > 0) {
@@ -191,6 +191,7 @@
     return bytes;
 
 Error:
+    close();
     // Simulate audio output timing in case of error
     usleep(bytes * 1000000 / frameSize() / sampleRate());
 
@@ -217,8 +218,7 @@
 
     if (strcmp(address, mA2dpAddress)) {
         strcpy(mA2dpAddress, address);
-        if (mData)
-            a2dp_set_sink(mData, mA2dpAddress);
+        close();
     }
     
     return NO_ERROR;
@@ -229,6 +229,7 @@
     if (mData) {
         a2dp_cleanup(mData);
         mData = NULL;
+        mInitialized = false;
     }
     return NO_ERROR;
 }
diff --git a/libs/audioflinger/A2dpAudioInterface.h b/libs/audioflinger/A2dpAudioInterface.h
index a56e8a0..5bef5da 100644
--- a/libs/audioflinger/A2dpAudioInterface.h
+++ b/libs/audioflinger/A2dpAudioInterface.h
@@ -96,6 +96,7 @@
                 int         mRetryCount;
                 char        mA2dpAddress[20];
                 void*       mData;
+                bool        mInitialized;
     };
 
     Mutex                   mLock;
diff --git a/libs/audioflinger/AudioFlinger.cpp b/libs/audioflinger/AudioFlinger.cpp
index 92c40e9..557d93b 100644
--- a/libs/audioflinger/AudioFlinger.cpp
+++ b/libs/audioflinger/AudioFlinger.cpp
@@ -171,6 +171,13 @@
      } else {
         LOGE("Couldn't even initialize the stubbed audio hardware!");
     }
+
+    char value[PROPERTY_VALUE_MAX];
+    property_get("ro.audio.silent", value, "0");
+    if (atoi(value)) {
+        LOGD("Silence is golden");
+        setMasterMute(true);
+    }
 }
 
 AudioFlinger::~AudioFlinger()
@@ -988,16 +995,6 @@
                 IPCThreadState::self()->flushCommands();
                 mWaitWorkCV.wait(mLock);
                 LOGV("Audio hardware exiting standby, output %d\n", mOutputType);
-                
-                if (mMasterMute == false) {
-                    char value[PROPERTY_VALUE_MAX];
-                    property_get("ro.audio.silent", value, "0");
-                    if (atoi(value)) {
-                        LOGD("Silence is golden");
-                        setMasterMute(true);
-                    }                    
-                }
-                
                 standbyTime = systemTime() + kStandbyTimeInNsecs;
                 continue;
             }
diff --git a/libs/audioflinger/AudioFlinger.h b/libs/audioflinger/AudioFlinger.h
index 77f064b..dfbb1e9 100644
--- a/libs/audioflinger/AudioFlinger.h
+++ b/libs/audioflinger/AudioFlinger.h
@@ -223,7 +223,10 @@
             enum track_flags {
                 STEPSERVER_FAILED = 0x01, //  StepServer could not acquire cblk->lock mutex
                 SYSTEM_FLAGS_MASK = 0x0000ffffUL,
-                // The upper 16 bits are used for track-specific flags.
+
+                AUDIO_IN_AGC_ENABLE = AudioSystem::AGC_ENABLE << 16,
+                AUDIO_IN_NS_ENABLE  = AudioSystem::NS_ENABLE << 16,
+                AUDIO_IN_IIR_ENABLE = AudioSystem::TX_IIR_ENABLE << 16
             };
 
                                 TrackBase(const sp<MixerThread>& mixerThread,
diff --git a/libs/audioflinger/AudioHardwareGeneric.h b/libs/audioflinger/AudioHardwareGeneric.h
index c949aa1..1d58389 100644
--- a/libs/audioflinger/AudioHardwareGeneric.h
+++ b/libs/audioflinger/AudioHardwareGeneric.h
@@ -47,7 +47,7 @@
     virtual size_t      bufferSize() const { return 4096; }
     virtual int         channelCount() const { return 2; }
     virtual int         format() const { return AudioSystem::PCM_16_BIT; }
-    virtual uint32_t    latency() const { return 20; }
+    virtual uint32_t    latency() const { return 0; }
     virtual status_t    setVolume(float volume) { return INVALID_OPERATION; }
     virtual ssize_t     write(const void* buffer, size_t bytes);
     virtual status_t    standby();
diff --git a/libs/ui/Camera.cpp b/libs/ui/Camera.cpp
index b3cbda1..6c60b85 100644
--- a/libs/ui/Camera.cpp
+++ b/libs/ui/Camera.cpp
@@ -110,8 +110,6 @@
     if (c->mCamera != 0) {
         c->mCamera->asBinder()->linkToDeath(c);
         c->mStatus = NO_ERROR;
-    } else {
-        c.clear();
     }
     return c;
 }
diff --git a/libs/utils/Threads.cpp b/libs/utils/Threads.cpp
index 5f407a9..74271ba 100644
--- a/libs/utils/Threads.cpp
+++ b/libs/utils/Threads.cpp
@@ -896,7 +896,6 @@
 {
     mLock.lock();
     if (mNumReaders == 0) {
-        mLock.unlock();
         LOG(LOG_WARN, "thread",
             "WARNING: unlockForRead requested, but not locked\n");
         return;
@@ -962,7 +961,6 @@
 {
     mLock.lock();
     if (mNumWriters == 0) {
-        mLock.unlock();
         LOG(LOG_WARN, "thread",
             "WARNING: unlockForWrite requested, but not locked\n");
         return;
@@ -974,7 +972,7 @@
     //printf(" wrlk held %.3f msec\n",
     //    (double) mDebugTimer.durationUsecs() / 1000.0);
 #endif
-    mWriteWaiter.signal();         // should other writers get first dibs?
+    // mWriteWaiter.signal();       // should other writers get first dibs?
     //printf("+++ signaling readers (if any)\n");
     mReadWaiter.broadcast();        // wake all readers (if any)
     mLock.unlock();