audio policy: fix delayed command insertion

When inserting a delayed command in the audio policy service
command thread queue, later similar commands are filtered out.
In case of volume commands and delayed unmuting, this can cause
a problem where a new volume command can cause the timing of a
delayed unmuting command to not be honored.

This change makes that a volume command gets the time stamp of
any delayed command it replaces.

Bug 6720482.

Change-Id: I216f85ac0ea46e4a046d3483e9e9b7ffe6c34a08
diff --git a/services/audioflinger/AudioPolicyService.cpp b/services/audioflinger/AudioPolicyService.cpp
index 02b531c..8b644ab 100644
--- a/services/audioflinger/AudioPolicyService.cpp
+++ b/services/audioflinger/AudioPolicyService.cpp
@@ -780,7 +780,6 @@
     data->mType = type;
     data->mStream = stream;
     command->mParam = (void *)data;
-    command->mWaitStatus = false;
     Mutex::Autolock _l(mLock);
     insertCommand_l(command);
     ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
@@ -792,7 +791,6 @@
     AudioCommand *command = new AudioCommand();
     command->mCommand = STOP_TONE;
     command->mParam = NULL;
-    command->mWaitStatus = false;
     Mutex::Autolock _l(mLock);
     insertCommand_l(command);
     ALOGV("AudioCommandThread() adding tone stop");
@@ -813,11 +811,6 @@
     data->mVolume = volume;
     data->mIO = output;
     command->mParam = data;
-    if (delayMs == 0) {
-        command->mWaitStatus = true;
-    } else {
-        command->mWaitStatus = false;
-    }
     Mutex::Autolock _l(mLock);
     insertCommand_l(command, delayMs);
     ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
@@ -843,11 +836,6 @@
     data->mIO = ioHandle;
     data->mKeyValuePairs = String8(keyValuePairs);
     command->mParam = data;
-    if (delayMs == 0) {
-        command->mWaitStatus = true;
-    } else {
-        command->mWaitStatus = false;
-    }
     Mutex::Autolock _l(mLock);
     insertCommand_l(command, delayMs);
     ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
@@ -870,11 +858,6 @@
     VoiceVolumeData *data = new VoiceVolumeData();
     data->mVolume = volume;
     command->mParam = data;
-    if (delayMs == 0) {
-        command->mWaitStatus = true;
-    } else {
-        command->mWaitStatus = false;
-    }
     Mutex::Autolock _l(mLock);
     insertCommand_l(command, delayMs);
     ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
@@ -893,6 +876,7 @@
     ssize_t i;  // not size_t because i will count down to -1
     Vector <AudioCommand *> removedCommands;
 
+    nsecs_t time = 0;
     command->mTime = systemTime() + milliseconds(delayMs);
 
     // acquire wake lock to make sure delayed commands are processed
@@ -938,6 +922,7 @@
             } else {
                 data2->mKeyValuePairs = param2.toString();
             }
+            time = command2->mTime;
         } break;
 
         case SET_VOLUME: {
@@ -948,6 +933,7 @@
             ALOGV("Filtering out volume command on output %d for stream %d",
                     data->mIO, data->mStream);
             removedCommands.add(command2);
+            time = command2->mTime;
         } break;
         case START_TONE:
         case STOP_TONE:
@@ -969,6 +955,17 @@
     }
     removedCommands.clear();
 
+    // wait for status only if delay is 0 and command time was not modified above
+    if (delayMs == 0 && time == 0) {
+        command->mWaitStatus = true;
+    } else {
+        command->mWaitStatus = false;
+    }
+    // update command time if modified above
+    if (time != 0) {
+        command->mTime = time;
+    }
+
     // insert command at the right place according to its time stamp
     ALOGV("inserting command: %d at index %d, num commands %d",
             command->mCommand, (int)i+1, mAudioCommands.size());