audio policy: clean up initial spatializer audio patch

During audio policy initialization, a spatializer stream is opened and
then closed but the audio patch cleanup normally done in closeOutput()
was omitted causing a stale audio patch to remain in audio flinger and
audio HAL.

Bug: 333829626
Test: repro steps in bug
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:a70bc37c1c5000ee161d84cf345fecedb7007baa)
Merged-In: I525c1c2fc91d6fc70bfd602c699562658e9bb7a1
Change-Id: I525c1c2fc91d6fc70bfd602c699562658e9bb7a1
diff --git a/services/audiopolicy/managerdefault/AudioPolicyManager.cpp b/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
index d427de4..5c4ab7b 100644
--- a/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
+++ b/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
@@ -6397,6 +6397,15 @@
         if ((desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0
                 && !isOutputOnlyAvailableRouteToSomeDevice(desc)) {
             outputsClosed.push_back(desc->mIoHandle);
+            nextAudioPortGeneration();
+            ssize_t index = mAudioPatches.indexOfKey(desc->getPatchHandle());
+            if (index >= 0) {
+                sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
+                (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(
+                            patchDesc->getAfHandle(), 0);
+                mAudioPatches.removeItemsAt(index);
+                mpClientInterface->onAudioPatchListUpdate();
+            }
             desc->close();
         }
     }