Merge changes I2bb2cb29,Ie52ebe19

* changes:
  audio flinger: fix use of local variable after std:move
  audio flinger: report actual sink device for MSD playback threads
diff --git a/media/libeffects/preprocessing/tests/Android.bp b/media/libeffects/preprocessing/tests/Android.bp
index b439880..5e8255a 100644
--- a/media/libeffects/preprocessing/tests/Android.bp
+++ b/media/libeffects/preprocessing/tests/Android.bp
@@ -19,3 +19,14 @@
         "libhardware_headers",
     ],
 }
+
+cc_test {
+    name: "correlation",
+    host_supported: true,
+    srcs: ["correlation.cpp"],
+    cflags: [
+        "-Wall",
+        "-Werror",
+        "-Wextra",
+    ],
+}
diff --git a/media/libeffects/preprocessing/tests/correlation.cpp b/media/libeffects/preprocessing/tests/correlation.cpp
new file mode 100644
index 0000000..b13dcc7
--- /dev/null
+++ b/media/libeffects/preprocessing/tests/correlation.cpp
@@ -0,0 +1,166 @@
+/*
+ * Copyright (C) 2021 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.
+ */
+
+#include <iostream>
+#include <vector>
+
+constexpr int kMinLoopLimitValue = 1;
+constexpr int kNumPeaks = 3;
+
+/*!
+  \brief           Compute the length normalized correlation of two signals
+
+  \sigX            Pointer to signal 1
+  \sigY            Pointer to signal 2
+  \len             Length of signals
+  \enableCrossCorr Flag to be set to 1 if cross-correlation is needed
+
+  \return          First value is vector of correlation peak indices
+                   Second value is vector of correlation peak values
+*/
+
+static std::pair<std::vector<int>, std::vector<float>> correlation(const int16_t* sigX,
+                                                                   const int16_t* sigY, int len,
+                                                                   int16_t enableCrossCorr) {
+    float maxCorrVal = 0.f, prevCorrVal = 0.f;
+    int delay = 0, peakIndex = 0, flag = 0;
+    int loopLim = (1 == enableCrossCorr) ? len : kMinLoopLimitValue;
+    std::vector<int> peakIndexVect(kNumPeaks, 0);
+    std::vector<float> peakValueVect(kNumPeaks, 0.f);
+    for (int i = 0; i < loopLim; i++) {
+        float corrVal = 0.f;
+        for (int j = i; j < len; j++) {
+            corrVal += (float)(sigX[j] * sigY[j - i]);
+        }
+        corrVal /= len - i;
+        if (corrVal > maxCorrVal) {
+            delay = i;
+            maxCorrVal = corrVal;
+        }
+        // Correlation peaks are expected to be observed at equal intervals. The interval length is
+        // expected to match with wave period.
+        // The following block of code saves the first kNumPeaks number of peaks and the index at
+        // which they occur.
+        if (peakIndex < kNumPeaks) {
+            if (corrVal > prevCorrVal) {
+                peakIndexVect[peakIndex] = i;
+                peakValueVect[peakIndex] = corrVal;
+                flag = 0;
+            } else if (0 == flag) {
+                peakIndex++;
+                flag = 1;
+            }
+        }
+        if (peakIndex == kNumPeaks) break;
+        prevCorrVal = corrVal;
+    }
+    return {peakIndexVect, peakValueVect};
+}
+
+void printUsage() {
+    printf("\nUsage: ");
+    printf("\n     correlation <firstFile> <secondFile> [enableCrossCorr]\n");
+    printf("\nwhere, \n     <firstFile>       is the first file name");
+    printf("\n     <secondFile>      is the second file name");
+    printf("\n     [enableCrossCorr] is flag to set for cross-correlation (Default 1)\n\n");
+}
+
+int main(int argc, const char* argv[]) {
+    if (argc < 3) {
+        printUsage();
+        return EXIT_FAILURE;
+    }
+
+    std::unique_ptr<FILE, decltype(&fclose)> fInput1(fopen(argv[1], "rb"), &fclose);
+    if (fInput1.get() == NULL) {
+        printf("\nError: missing file %s\n", argv[1]);
+        return EXIT_FAILURE;
+    }
+    std::unique_ptr<FILE, decltype(&fclose)> fInput2(fopen(argv[2], "rb"), &fclose);
+    if (fInput2.get() == NULL) {
+        printf("\nError: missing file %s\n", argv[2]);
+        return EXIT_FAILURE;
+    }
+    int16_t enableCrossCorr = (4 == argc) ? atoi(argv[3]) : 1;
+
+    fseek(fInput1.get(), 0L, SEEK_END);
+    unsigned int fileSize1 = ftell(fInput1.get());
+    rewind(fInput1.get());
+    fseek(fInput2.get(), 0L, SEEK_END);
+    unsigned int fileSize2 = ftell(fInput2.get());
+    rewind(fInput2.get());
+    if (fileSize1 != fileSize2) {
+        printf("\nError: File sizes different\n");
+        return EXIT_FAILURE;
+    }
+
+    int numFrames = fileSize1 / sizeof(int16_t);
+    std::unique_ptr<int16_t[]> inBuffer1(new int16_t[numFrames]());
+    std::unique_ptr<int16_t[]> inBuffer2(new int16_t[numFrames]());
+
+    fread(inBuffer1.get(), sizeof(int16_t), numFrames, fInput1.get());
+    fread(inBuffer2.get(), sizeof(int16_t), numFrames, fInput2.get());
+
+    auto pairAutoCorr1 = correlation(inBuffer1.get(), inBuffer1.get(), numFrames, enableCrossCorr);
+    auto pairAutoCorr2 = correlation(inBuffer2.get(), inBuffer2.get(), numFrames, enableCrossCorr);
+
+    // Following code block checks pitch period difference between two input signals. They must
+    // match as AGC applies only gain, no frequency related computation is done.
+    bool pitchMatch = false;
+    for (unsigned i = 0; i < pairAutoCorr1.first.size() - 1; i++) {
+        if (pairAutoCorr1.first[i + 1] - pairAutoCorr1.first[i] !=
+            pairAutoCorr2.first[i + 1] - pairAutoCorr2.first[i]) {
+            pitchMatch = false;
+            break;
+        }
+        pitchMatch = true;
+    }
+    if (pitchMatch) {
+        printf("Auto-correlation  : Pitch matched\n");
+    } else {
+        printf("Auto-correlation  : Pitch mismatch\n");
+        return EXIT_FAILURE;
+    }
+
+    if (enableCrossCorr) {
+        auto pairCrossCorr =
+                correlation(inBuffer1.get(), inBuffer2.get(), numFrames, enableCrossCorr);
+
+        // Since AGC applies only gain, the pitch information obtained from cross correlation data
+        // of input and output is expected to be same as the input signal's pitch information.
+        pitchMatch = false;
+        for (unsigned i = 0; i < pairCrossCorr.first.size() - 1; i++) {
+            if (pairAutoCorr1.first[i + 1] - pairAutoCorr1.first[i] !=
+                pairCrossCorr.first[i + 1] - pairCrossCorr.first[i]) {
+                pitchMatch = false;
+                break;
+            }
+            pitchMatch = true;
+        }
+        if (pitchMatch) {
+            printf("Cross-correlation : Pitch matched for AGC\n");
+            if (pairAutoCorr1.second[0]) {
+                printf("Expected gain     : (maxCrossCorr / maxAutoCorr1) = %f\n",
+                       pairCrossCorr.second[0] / pairAutoCorr1.second[0]);
+            }
+        } else {
+            printf("Cross-correlation : Pitch mismatch\n");
+            return EXIT_FAILURE;
+        }
+    }
+
+    return EXIT_SUCCESS;
+}
diff --git a/services/audiopolicy/common/managerdefinitions/include/HwModule.h b/services/audiopolicy/common/managerdefinitions/include/HwModule.h
index b5b10f3..9ba745a 100644
--- a/services/audiopolicy/common/managerdefinitions/include/HwModule.h
+++ b/services/audiopolicy/common/managerdefinitions/include/HwModule.h
@@ -52,8 +52,12 @@
         devices.merge(mDynamicDevices);
         return devices;
     }
+    std::string getTagForDevice(audio_devices_t device,
+                                const String8 &address = String8(),
+                                audio_format_t codec = AUDIO_FORMAT_DEFAULT);
     void addDynamicDevice(const sp<DeviceDescriptor> &device)
     {
+        device->setDynamic();
         mDynamicDevices.add(device);
     }
 
diff --git a/services/audiopolicy/common/managerdefinitions/src/DeviceDescriptor.cpp b/services/audiopolicy/common/managerdefinitions/src/DeviceDescriptor.cpp
index 8545f8e..f6859c7 100644
--- a/services/audiopolicy/common/managerdefinitions/src/DeviceDescriptor.cpp
+++ b/services/audiopolicy/common/managerdefinitions/src/DeviceDescriptor.cpp
@@ -49,11 +49,13 @@
 {
 }
 
+// Let DeviceDescriptorBase initialize the address since it handles specific cases like
+// legacy remote submix where "0" is added as default address.
 DeviceDescriptor::DeviceDescriptor(const AudioDeviceTypeAddr &deviceTypeAddr,
                                    const std::string &tagName,
                                    const FormatVector &encodedFormats) :
         DeviceDescriptorBase(deviceTypeAddr), mTagName(tagName), mEncodedFormats(encodedFormats),
-        mDeclaredAddress(deviceTypeAddr.getAddress())
+        mDeclaredAddress(DeviceDescriptorBase::address())
 {
     mCurrentEncodedFormat = AUDIO_FORMAT_DEFAULT;
     /* If framework runs against a pre 5.0 Audio HAL, encoded formats are absent from the config.
diff --git a/services/audiopolicy/common/managerdefinitions/src/HwModule.cpp b/services/audiopolicy/common/managerdefinitions/src/HwModule.cpp
index 2967014..3a143b0 100644
--- a/services/audiopolicy/common/managerdefinitions/src/HwModule.cpp
+++ b/services/audiopolicy/common/managerdefinitions/src/HwModule.cpp
@@ -41,6 +41,14 @@
     }
 }
 
+std::string HwModule::getTagForDevice(audio_devices_t device, const String8 &address,
+                                          audio_format_t codec)
+{
+    DeviceVector declaredDevices = getDeclaredDevices();
+    sp<DeviceDescriptor> deviceDesc = declaredDevices.getDevice(device, address, codec);
+    return deviceDesc ? deviceDesc->getTagName() : std::string{};
+}
+
 status_t HwModule::addOutputProfile(const std::string& name, const audio_config_t *config,
                                     audio_devices_t device, const String8& address)
 {
@@ -49,7 +57,8 @@
     profile->addAudioProfile(new AudioProfile(config->format, config->channel_mask,
                                               config->sample_rate));
 
-    sp<DeviceDescriptor> devDesc = new DeviceDescriptor(device, "" /*tagName*/, address.string());
+    sp<DeviceDescriptor> devDesc =
+            new DeviceDescriptor(device, getTagForDevice(device), address.string());
     addDynamicDevice(devDesc);
     // Reciprocally attach the device to the module
     devDesc->attach(this);
@@ -116,7 +125,8 @@
     profile->addAudioProfile(new AudioProfile(config->format, config->channel_mask,
                                               config->sample_rate));
 
-    sp<DeviceDescriptor> devDesc = new DeviceDescriptor(device, "" /*tagName*/, address.string());
+    sp<DeviceDescriptor> devDesc =
+            new DeviceDescriptor(device, getTagForDevice(device), address.string());
     addDynamicDevice(devDesc);
     // Reciprocally attach the device to the module
     devDesc->attach(this);
diff --git a/services/audiopolicy/config/audio_policy_configuration_bluetooth_legacy_hal.xml b/services/audiopolicy/config/audio_policy_configuration_bluetooth_legacy_hal.xml
index b4cc1d3..5f4e5f2 100644
--- a/services/audiopolicy/config/audio_policy_configuration_bluetooth_legacy_hal.xml
+++ b/services/audiopolicy/config/audio_policy_configuration_bluetooth_legacy_hal.xml
@@ -185,6 +185,9 @@
         <!-- Hearing aid Audio HAL -->
         <xi:include href="hearing_aid_audio_policy_configuration.xml"/>
 
+        <!-- Le Audio Audio HAL -->
+        <xi:include href="le_audio_policy_configuration.xml"/>
+
         <!-- MSD Audio HAL (optional) -->
         <xi:include href="msd_audio_policy_configuration.xml"/>
 
diff --git a/services/audiopolicy/config/bluetooth_audio_policy_configuration.xml b/services/audiopolicy/config/bluetooth_audio_policy_configuration.xml
index ce78eb0..7238317 100644
--- a/services/audiopolicy/config/bluetooth_audio_policy_configuration.xml
+++ b/services/audiopolicy/config/bluetooth_audio_policy_configuration.xml
@@ -10,6 +10,12 @@
                      samplingRates="24000,16000"
                      channelMasks="AUDIO_CHANNEL_OUT_STEREO"/>
         </mixPort>
+        <!-- Le Audio Audio Ports -->
+        <mixPort name="le audio output" role="source">
+            <profile name="" format="AUDIO_FORMAT_PCM_16_BIT,AUDIO_FORMAT_PCM_24_BIT,AUDIO_FORMAT_PCM_32_BIT"
+                     samplingRates="8000,16000,24000,32000,44100,48000"
+                     channelMasks="AUDIO_CHANNEL_OUT_MONO,AUDIO_CHANNEL_OUT_STEREO"/>
+        </mixPort>
     </mixPorts>
     <devicePorts>
         <!-- A2DP Audio Ports -->
@@ -30,6 +36,9 @@
         </devicePort>
         <!-- Hearing AIDs Audio Ports -->
         <devicePort tagName="BT Hearing Aid Out" type="AUDIO_DEVICE_OUT_HEARING_AID" role="sink"/>
+        <!-- BLE Audio Ports -->
+        <devicePort tagName="BLE Headset Out" type="AUDIO_DEVICE_OUT_BLE_HEADSET" role="sink"/>
+        <devicePort tagName="BLE Speaker Out" type="AUDIO_DEVICE_OUT_BLE_SPEAKER" role="sink"/>
     </devicePorts>
     <routes>
         <route type="mix" sink="BT A2DP Out"
@@ -40,5 +49,9 @@
                sources="a2dp output"/>
         <route type="mix" sink="BT Hearing Aid Out"
                sources="hearing aid output"/>
+        <route type="mix" sink="BLE Headset Out"
+               sources="le audio output"/>
+        <route type="mix" sink="BLE Speaker Out"
+               sources="le audio output"/>
     </routes>
 </module>
diff --git a/services/audiopolicy/config/le_audio_policy_configuration.xml b/services/audiopolicy/config/le_audio_policy_configuration.xml
new file mode 100644
index 0000000..a3dc72b
--- /dev/null
+++ b/services/audiopolicy/config/le_audio_policy_configuration.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Le Audio HAL Audio Policy Configuration file -->
+<module name="bluetooth" halVersion="2.1">
+    <mixPorts>
+        <mixPort name="le audio output" role="source">
+            <profile name="" format="AUDIO_FORMAT_PCM_16_BIT,AUDIO_FORMAT_PCM_24_BIT,AUDIO_FORMAT_PCM_32_BIT"
+                     samplingRates="8000,16000,24000,32000,44100,48000"
+                     channelMasks="AUDIO_CHANNEL_OUT_MONO,AUDIO_CHANNEL_OUT_STEREO"/>
+        </mixPort>
+    </mixPorts>
+    <devicePorts>
+        <devicePort tagName="BLE Headset Out" type="AUDIO_DEVICE_OUT_BLE_HEADSET" role="sink"/>
+        <devicePort tagName="BLE Speaker Out" type="AUDIO_DEVICE_OUT_BLE_SPEAKER" role="sink"/>
+    </devicePorts>
+    <routes>
+        <route type="mix" sink="BLE Headset Out" sources="le audio output"/>
+        <route type="mix" sink="BLE Speaker Out" sources="le audio output"/>
+    </routes>
+</module>