Add r_submix to goldfish audio

r_submix is required by CDD.

Bug: 159945428
Test: g3-app-compat
Signed-off-by: Roman Kiryanov <rkir@google.com>
Change-Id: Ib22681ef11b1d48f9f92d7f1d0a5ed1622a0ccea
Merged-In: Ib22681ef11b1d48f9f92d7f1d0a5ed1622a0ccea
diff --git a/audio/device_factory.cpp b/audio/device_factory.cpp
index e0bbf1f..a822b79 100644
--- a/audio/device_factory.cpp
+++ b/audio/device_factory.cpp
@@ -27,13 +27,26 @@
 
 using ::android::hardware::Void;
 
+wp<PrimaryDevice> gPrimaryDevice;   // volume levels and the mic state are global
+
+template <class D> sp<D> getCachedDevice(wp<D>& cache) {
+    sp<D> result = cache.promote();
+    if (!result) {
+        result = new D;
+        cache = result;
+    }
+    return result;
+}
+
 Return<void> DevicesFactory::openDevice(const hidl_string& device,
                                         openDevice_cb _hidl_cb) {
     Result result = Result::OK;
-    std::unique_ptr<IDevice> dev;
+    sp<IDevice> dev;
 
     if (device == AUDIO_HARDWARE_MODULE_ID_PRIMARY) {
-        dev = std::make_unique<PrimaryDevice>();
+        dev = getCachedDevice(gPrimaryDevice);
+    } else if (device == AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) {
+        dev = getCachedDevice(gPrimaryDevice);
     } else {
         result = Result::INVALID_ARGUMENTS;
     }
@@ -43,12 +56,12 @@
               __func__, __LINE__, device.c_str(), toString(result).c_str());
     }
 
-    _hidl_cb(result, dev.release());
+    _hidl_cb(result, std::move(dev));
     return Void();
 }
 
 Return<void> DevicesFactory::openPrimaryDevice(openPrimaryDevice_cb _hidl_cb) {
-    _hidl_cb(Result::OK, new PrimaryDevice);
+    _hidl_cb(Result::OK, getCachedDevice(gPrimaryDevice));
     return Void();
 }
 
diff --git a/audio/policy/audio_policy_configuration.xml b/audio/policy/audio_policy_configuration.xml
index f631bb0..34e8aff 100644
--- a/audio/policy/audio_policy_configuration.xml
+++ b/audio/policy/audio_policy_configuration.xml
@@ -24,6 +24,9 @@
         <!-- Primary Audio HAL -->
         <xi:include href="primary_audio_policy_configuration.xml"/>
 
+        <!-- Remote Submix Audio HAL -->
+        <xi:include href="r_submix_audio_policy_configuration.xml"/>
+
     </modules>
     <!-- End of Modules section -->
 
diff --git a/audio/policy/r_submix_audio_policy_configuration.xml b/audio/policy/r_submix_audio_policy_configuration.xml
new file mode 100644
index 0000000..f74185e
--- /dev/null
+++ b/audio/policy/r_submix_audio_policy_configuration.xml
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Copyright (C) 2020 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<!-- Remote Submix Audio Policy Configuration file -->
+<module name="r_submix" halVersion="2.0">
+    <attachedDevices>
+        <item>Remote Submix In</item>
+    </attachedDevices>
+    <mixPorts>
+        <mixPort name="r_submix output" role="source">
+            <profile name="" format="AUDIO_FORMAT_PCM_16_BIT"
+                     samplingRates="44100" channelMasks="AUDIO_CHANNEL_OUT_STEREO"/>
+        </mixPort>
+        <mixPort name="r_submix input" role="sink">
+           <profile name="" format="AUDIO_FORMAT_PCM_16_BIT"
+                    samplingRates="44100" channelMasks="AUDIO_CHANNEL_IN_STEREO"/>
+        </mixPort>
+   </mixPorts>
+   <devicePorts>
+       <devicePort tagName="Remote Submix Out" type="AUDIO_DEVICE_OUT_REMOTE_SUBMIX" role="sink">
+           <profile name="" format="AUDIO_FORMAT_PCM_16_BIT"
+                    samplingRates="44100" channelMasks="AUDIO_CHANNEL_OUT_STEREO"/>
+       </devicePort>
+       <devicePort tagName="Remote Submix In" type="AUDIO_DEVICE_IN_REMOTE_SUBMIX" role="source">
+           <profile name="" format="AUDIO_FORMAT_PCM_16_BIT"
+                    samplingRates="44100" channelMasks="AUDIO_CHANNEL_IN_STEREO"/>
+        </devicePort>
+    </devicePorts>
+    <routes>
+        <route type="mix" sink="Remote Submix Out"
+               sources="r_submix output"/>
+        <route type="mix" sink="r_submix input"
+               sources="Remote Submix In"/>
+    </routes>
+</module>
diff --git a/vendor.mk b/vendor.mk
index 856d8e1..d37c0c3 100644
--- a/vendor.mk
+++ b/vendor.mk
@@ -162,7 +162,8 @@
 PRODUCT_PACKAGES += android.hardware.audio@6.0-impl.ranchu
 PRODUCT_COPY_FILES += \
     device/generic/goldfish/audio/policy/audio_policy_configuration.xml:$(TARGET_COPY_OUT_VENDOR)/etc/audio_policy_configuration.xml \
-    device/generic/goldfish/audio/policy/primary_audio_policy_configuration.xml:$(TARGET_COPY_OUT_VENDOR)/etc/primary_audio_policy_configuration.xml
+    device/generic/goldfish/audio/policy/primary_audio_policy_configuration.xml:$(TARGET_COPY_OUT_VENDOR)/etc/primary_audio_policy_configuration.xml \
+    device/generic/goldfish/audio/policy/r_submix_audio_policy_configuration.xml:$(TARGET_COPY_OUT_VENDOR)/etc/r_submix_audio_policy_configuration.xml
 endif
 
 PRODUCT_PACKAGES += \