AudioPolicyManager: remove 5.1 channels for Surround Settings NEVER

This allows people with TOSLINK to hear the correct stereo PCM
when listening to movies.

Bug: 25144047
Change-Id: I88401c016dd5a3a469becb019540c04befe78bba
Signed-off-by: Phil Burk <philburk@google.com>
diff --git a/services/audiopolicy/managerdefault/AudioPolicyManager.cpp b/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
index 2405ced..5253f7e 100644
--- a/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
+++ b/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
@@ -5210,15 +5210,14 @@
 }
 
 // Modify the list of surround sound formats supported.
-void AudioPolicyManager::filterSurroundFormats(FormatVector &formats) {
-    // TODO Change the ALOGIs to ALOGVs in this function after the feature is verified.
-
+void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) {
+    FormatVector &formats = *formatsPtr;
     // TODO Set this based on Config properties.
     const bool alwaysForceAC3 = true;
 
     audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
             AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
-    ALOGI("%s: forced use = %d", __FUNCTION__, forceUse);
+    ALOGD("%s: forced use = %d", __FUNCTION__, forceUse);
 
     // Analyze original support for various formats.
     bool supportsAC3 = false;
@@ -5226,7 +5225,6 @@
     bool supportsIEC61937 = false;
     for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
         audio_format_t format = formats[formatIndex];
-        ALOGI("%s: original formats: 0x%08x", __FUNCTION__, format);
         switch (format) {
             case AUDIO_FORMAT_AC3:
                 supportsAC3 = true;
@@ -5243,8 +5241,6 @@
                 break;
         }
     }
-    ALOGI("%s: original, supportsAC3 = %d, supportsOtherSurround = %d, supportsIEC61937 = %d",
-            __FUNCTION__, supportsAC3, supportsOtherSurround, supportsIEC61937);
 
     // Modify formats based on surround preferences.
     // If NEVER, remove support for surround formats.
@@ -5259,7 +5255,6 @@
                     case AUDIO_FORMAT_DTS:
                     case AUDIO_FORMAT_DTS_HD:
                     case AUDIO_FORMAT_IEC61937:
-                        ALOGI("%s: remove format 0x%08x", __FUNCTION__, format);
                         formats.removeAt(formatIndex);
                         break;
                     default:
@@ -5297,13 +5292,42 @@
             supportsIEC61937 = true;
         }
     }
-    // Just for debugging.
-    for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
-        audio_format_t format = formats[formatIndex];
-        ALOGI("%s: final formats: 0x%08x", __FUNCTION__, format);
+}
+
+// Modify the list of channel masks supported.
+void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) {
+    ChannelsVector &channelMasks = *channelMasksPtr;
+    audio_policy_forced_cfg_t forceUse = mEngine->getForceUse(
+            AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND);
+
+    // If NEVER, then remove support for channelMasks > stereo.
+    if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) {
+        for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) {
+            audio_channel_mask_t channelMask = channelMasks[maskIndex];
+            if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) {
+                ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask);
+                channelMasks.removeAt(maskIndex);
+            } else {
+                maskIndex++;
+            }
+        }
+    // If ALWAYS, then make sure we at least support 5.1
+    } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
+        bool supports5dot1 = false;
+        // Are there any channel masks that can be considered "surround"?
+        for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) {
+            audio_channel_mask_t channelMask = channelMasks[maskIndex];
+            if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) {
+                supports5dot1 = true;
+                break;
+            }
+        }
+        // If not then add 5.1 support.
+        if (!supports5dot1) {
+            channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1);
+            ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__);
+        }
     }
-    ALOGI("%s: final, supportsAC3 = %d, supportsOtherSurround = %d, supportsIEC61937 = %d",
-            __FUNCTION__, supportsAC3, supportsOtherSurround, supportsIEC61937);
 }
 
 void AudioPolicyManager::updateAudioProfiles(audio_devices_t device,
@@ -5312,11 +5336,12 @@
 {
     String8 reply;
     char *value;
+
     // Format MUST be checked first to update the list of AudioProfile
     if (profiles.hasDynamicFormat()) {
         reply = mpClientInterface->getParameters(ioHandle,
                                                  String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
-        ALOGI("%s: supported formats %s", __FUNCTION__, reply.string());
+        ALOGV("%s: supported formats %s", __FUNCTION__, reply.string());
         AudioParameter repliedParameters(reply);
         if (repliedParameters.get(
                 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS), reply) != NO_ERROR) {
@@ -5325,7 +5350,7 @@
         }
         FormatVector formats = formatsFromString(reply.string());
         if (device == AUDIO_DEVICE_OUT_HDMI) {
-            filterSurroundFormats(formats);
+            filterSurroundFormats(&formats);
         }
         profiles.setFormats(formats);
     }
@@ -5358,6 +5383,9 @@
             if (repliedParameters.get(
                     String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS), reply) == NO_ERROR) {
                 channelMasks = channelMasksFromString(reply.string());
+                if (device == AUDIO_DEVICE_OUT_HDMI) {
+                    filterSurroundChannelMasks(&channelMasks);
+                }
             }
         }
         profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates));