Policy_HAL: avoid setting direct VoIP flag multiple

Block setting direct VoIP flag to normal playback output.
Fallback to primary output if a VoIP output is already
opened and if there is a request for other.

CRs-Fixed: 2145224
Change-Id: I33c553f1e8e3fcb15a9810a0bd9c35780ce7fa34
diff --git a/policy_hal/AudioPolicyManager.cpp b/policy_hal/AudioPolicyManager.cpp
index 5d7f9c1..8771f31 100644
--- a/policy_hal/AudioPolicyManager.cpp
+++ b/policy_hal/AudioPolicyManager.cpp
@@ -1517,11 +1517,25 @@
             }
         }
 #else
-    if (stream == AUDIO_STREAM_VOICE_CALL &&
+    if (mEngine->getPhoneState() == AUDIO_MODE_IN_COMMUNICATION &&
+        stream == AUDIO_STREAM_VOICE_CALL &&
         audio_is_linear_pcm(format)) {
-        flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
-                                       AUDIO_OUTPUT_FLAG_DIRECT);
-        ALOGV("Set VoIP and Direct output flags for PCM format");
+        //check if VoIP output is not opened already
+        bool voip_pcm_already_in_use = false;
+        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");
+                 break;
+             }
+        }
+
+        if (!voip_pcm_already_in_use) {
+            flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
+                                           AUDIO_OUTPUT_FLAG_DIRECT);
+            ALOGV("Set VoIP and Direct output flags for PCM format");
+        }
 #endif
         //IF VOIP is going to be started at the same time as when
         //vr is enabled, get VOIP to fallback to low latency