Fix issue 2304701: Media streams can remain muted on A2DP output.

The problem comes from the fact that when a notification is played on both headsets + speaker,
the media strategy is muted. It is only unmuted when a new device is selected on hardwate output
(for instance headset only when music starts).
If an A2DP output is created while music is muted, AudioFlinger with use the last value received
for music volume to initialize the music stream volume on the newly created A2DP output, which in
this case is 0. The code in audio policy manager that applies stream volumes after the A2DP output
has been created is inefficient here, because as music stream is muted, the request to change the
volume is ignored.
As next time music starts it is now played over A2DP output and not on HW output,
no device modification is done on HW output and the music streams remains muted.
This is also applicatble to SYSTEM and TTS streams.

The fix consists in keeping a stream mute count on each output separately instead of a global stream mute count.
Thus when the music volume is re applied after A2DP output creation, the request is not ignored as the music stream is not
muted on A2DP output.
diff --git a/libaudio-qsd8k/AudioPolicyManager.cpp b/libaudio-qsd8k/AudioPolicyManager.cpp
index 965a30a..9660854 100644
--- a/libaudio-qsd8k/AudioPolicyManager.cpp
+++ b/libaudio-qsd8k/AudioPolicyManager.cpp
@@ -1181,7 +1181,7 @@
 
     snprintf(buffer, SIZE, "\nStreams dump:\n");
     write(fd, buffer, strlen(buffer));
-    snprintf(buffer, SIZE, " Stream  Index Min  Index Max  Index Cur  Mute Count  Can be muted\n");
+    snprintf(buffer, SIZE, " Stream  Index Min  Index Max  Index Cur  Can be muted\n");
     write(fd, buffer, strlen(buffer));
     for (size_t i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
         snprintf(buffer, SIZE, " %02d", i);
@@ -1580,8 +1580,8 @@
 {
 
     // do not change actual stream volume if the stream is muted
-    if (mStreams[stream].mMuteCount != 0) {
-        LOGV("checkAndSetVolume() stream %d muted count %d", stream, mStreams[stream].mMuteCount);
+    if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
+        LOGV("checkAndSetVolume() stream %d muted count %d", stream, mOutputs.valueFor(output)->mMuteCount[stream]);
         return NO_ERROR;
     }
 
@@ -1654,25 +1654,25 @@
 void AudioPolicyManager::setStreamMute(int stream, bool on, audio_io_handle_t output, int delayMs)
 {
     StreamDescriptor &streamDesc = mStreams[stream];
-    uint32_t device = mOutputs.valueFor(output)->mDevice;
+    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
 
-    LOGV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d", stream, on, output, streamDesc.mMuteCount);
+    LOGV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d", stream, on, output, outputDesc->mMuteCount[stream]);
 
     if (on) {
-        if (streamDesc.mMuteCount == 0) {
+        if (outputDesc->mMuteCount[stream] == 0) {
             if (streamDesc.mCanBeMuted) {
-                checkAndSetVolume(stream, 0, output, device, delayMs);
+                checkAndSetVolume(stream, 0, output, outputDesc->device(), delayMs);
             }
         }
         // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
-        streamDesc.mMuteCount++;
+        outputDesc->mMuteCount[stream]++;
     } else {
-        if (streamDesc.mMuteCount == 0) {
+        if (outputDesc->mMuteCount[stream] == 0) {
             LOGW("setStreamMute() unmuting non muted stream!");
             return;
         }
-        if (--streamDesc.mMuteCount == 0) {
-            checkAndSetVolume(stream, streamDesc.mIndexCur, output, device, delayMs);
+        if (--outputDesc->mMuteCount[stream] == 0) {
+            checkAndSetVolume(stream, streamDesc.mIndexCur, output, outputDesc->device(), delayMs);
         }
     }
 }
@@ -1729,6 +1729,7 @@
     for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
         mRefCount[i] = 0;
         mCurVolume[i] = -1.0;
+        mMuteCount[i] = 0;
     }
 }
 
@@ -1788,10 +1789,10 @@
     result.append(buffer);
     snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
     result.append(buffer);
-    snprintf(buffer, SIZE, " Stream volume refCount\n");
+    snprintf(buffer, SIZE, " Stream volume    refCount muteCount\n");
     result.append(buffer);
     for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
-        snprintf(buffer, SIZE, " %02d     %.03f  %d\n", i, mCurVolume[i], mRefCount[i]);
+        snprintf(buffer, SIZE, " %02d     %.03f     %02d       %02d\n", i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
         result.append(buffer);
     }
     write(fd, result.string(), result.size());
@@ -1834,11 +1835,10 @@
 
 void AudioPolicyManager::StreamDescriptor::dump(char* buffer, size_t size)
 {
-    snprintf(buffer, size, "      %02d         %02d         %02d         %02d          %d\n",
+    snprintf(buffer, size, "      %02d         %02d         %02d         %d\n",
             mIndexMin,
             mIndexMax,
             mIndexCur,
-            mMuteCount,
             mCanBeMuted);
 }
 
diff --git a/libaudio-qsd8k/AudioPolicyManager.h b/libaudio-qsd8k/AudioPolicyManager.h
index 662b8ac..2c6d20b 100644
--- a/libaudio-qsd8k/AudioPolicyManager.h
+++ b/libaudio-qsd8k/AudioPolicyManager.h
@@ -115,6 +115,7 @@
             AudioOutputDescriptor *mOutput1;    // used by duplicated outputs: first output
             AudioOutputDescriptor *mOutput2;    // used by duplicated outputs: second output
             float mCurVolume[AudioSystem::NUM_STREAM_TYPES];   // current stream volume
+            int mMuteCount[AudioSystem::NUM_STREAM_TYPES];     // mute request counter
         };
 
         // descriptor for audio inputs. Used to maintain current configuration of each opened audio input
@@ -140,14 +141,13 @@
         {
         public:
             StreamDescriptor()
-            :   mIndexMin(0), mIndexMax(1), mIndexCur(1), mMuteCount(0), mCanBeMuted(true) {}
+            :   mIndexMin(0), mIndexMax(1), mIndexCur(1), mCanBeMuted(true) {}
 
             void dump(char* buffer, size_t size);
 
             int mIndexMin;      // min volume index
             int mIndexMax;      // max volume index
             int mIndexCur;      // current volume index
-            int mMuteCount;     // mute request counter
             bool mCanBeMuted;   // true is the stream can be muted
         };