policy: close voip output when new output is opened by same voip client

When USB headset is inserted, VoIP client will stop current output
and then open a new output. Due to voip usecase is not closed in time,
the new output is opened in handset with FLAG_NONE and fallback to
primary output.

Fix it by closing voip output when a new output is opened by same voip
client in different device.

CRs-Fixed: 2272627
Change-Id: I71672675f13f58ed818117d5d0abe0d93204227e
diff --git a/policy_hal/AudioPolicyManager.cpp b/policy_hal/AudioPolicyManager.cpp
index 9cf20c7..7712c67 100644
--- a/policy_hal/AudioPolicyManager.cpp
+++ b/policy_hal/AudioPolicyManager.cpp
@@ -1537,8 +1537,14 @@
         for (size_t i = 0; i < mOutputs.size(); i++) {
              sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
              if (desc->mFlags == (AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_DIRECT)) {
-                 voip_pcm_already_in_use = true;
-                 ALOGD("VoIP PCM already in use");
+                 //close voip output if currently open by the same client with different device
+                 if (desc->mDirectClientSession == session &&
+                     desc->device() != device) {
+                     closeOutput(desc->mIoHandle);
+                 } else {
+                     voip_pcm_already_in_use = true;
+                     ALOGD("VoIP PCM already in use");
+                 }
                  break;
              }
         }