Merge "policy_hal: use primary output for voice playback if voip bypassed"
diff --git a/policy_hal/AudioPolicyManager.cpp b/policy_hal/AudioPolicyManager.cpp
index 7638c62..412038a 100644
--- a/policy_hal/AudioPolicyManager.cpp
+++ b/policy_hal/AudioPolicyManager.cpp
@@ -1489,40 +1489,39 @@
        }
 
 #ifdef COMPRESS_VOIP_ENABLED
-    if ((stream == AUDIO_STREAM_VOICE_CALL) &&
-        (channelMask == 1) &&
-        (samplingRate == 8000 || samplingRate == 16000 ||
-         samplingRate == 32000 || samplingRate == 48000)) {
-        // Allow Voip direct output only if:
-        // audio mode is MODE_IN_COMMUNCATION; AND
-        // voip output is not opened already; AND
-        // requested sample rate matches with that of voip input stream (if opened already)
-        int value = 0;
-        uint32_t mode = 0, voipOutCount = 1, voipSampleRate = 1;
-        String8 valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
-                                                           String8("audio_mode"));
-        AudioParameter result = AudioParameter(valueStr);
-        if (result.getInt(String8("audio_mode"), value) == NO_ERROR) {
-            mode = value;
-        }
+    if ((mEngine->getPhoneState() == AUDIO_MODE_IN_COMMUNICATION) &&
+        (stream == AUDIO_STREAM_VOICE_CALL) &&
+        audio_is_linear_pcm(format)) {
+        // let voice stream to go with primary output by default
+        // in case direct voip is bypassed
+        bool use_primary_out = true;
 
-        valueStr =  mpClientInterface->getParameters((audio_io_handle_t)0,
+        if ((channelMask == 1) &&
+                (samplingRate == 8000 || samplingRate == 16000 ||
+                samplingRate == 32000 || samplingRate == 48000)) {
+            // Allow Voip direct output only if:
+            // audio mode is MODE_IN_COMMUNCATION; AND
+            // voip output is not opened already; AND
+            // requested sample rate matches with that of voip input stream (if opened already)
+            int value = 0;
+            uint32_t voipOutCount = 1, voipSampleRate = 1;
+
+            valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
                                               String8("voip_out_stream_count"));
-        result = AudioParameter(valueStr);
-        if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
-            voipOutCount = value;
-        }
+            result = AudioParameter(valueStr);
+            if (result.getInt(String8("voip_out_stream_count"), value) == NO_ERROR) {
+                voipOutCount = value;
+            }
 
-        valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
+            valueStr = mpClientInterface->getParameters((audio_io_handle_t)0,
                                               String8("voip_sample_rate"));
-        result = AudioParameter(valueStr);
-        if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
-            voipSampleRate = value;
-        }
+            result = AudioParameter(valueStr);
+            if (result.getInt(String8("voip_sample_rate"), value) == NO_ERROR) {
+                voipSampleRate = value;
+            }
 
-        if ((mode == AUDIO_MODE_IN_COMMUNICATION) && (voipOutCount == 0) &&
-            ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
-            if (audio_is_linear_pcm(format)) {
+            if ((voipOutCount == 0) &&
+                ((voipSampleRate == 0) || (voipSampleRate == samplingRate))) {
                 char propValue[PROPERTY_VALUE_MAX] = {0};
                 property_get("vendor.voice.path.for.pcm.voip", propValue, "0");
                 bool voipPcmSysPropEnabled = !strncmp("true", propValue, sizeof("true"));
@@ -1530,12 +1529,14 @@
                     flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX |
                                                  AUDIO_OUTPUT_FLAG_DIRECT);
                     ALOGD("Set VoIP and Direct output flags for PCM format");
-                } else {
-                   //If VoIP is going in audio path, make VoIP use primary output
-                   flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_FAST|AUDIO_OUTPUT_FLAG_PRIMARY);
+                    use_primary_out = false;
                 }
             }
         }
+
+        if (use_primary_out) {
+            flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_FAST|AUDIO_OUTPUT_FLAG_PRIMARY);
+        }
 #else
     if (mEngine->getPhoneState() == AUDIO_MODE_IN_COMMUNICATION &&
         stream == AUDIO_STREAM_VOICE_CALL &&