audio: Move StreamContext ownership out from StreamCommonImpl

Upcoming implementations of the streams of the primary module
will need to change the underlying stream type depending on
the current connected device. The stream context must persist,
thus its life time must be bound to the IStreamIn/Out implementation.
Move the StreamContext instance under ownership of StreamIn/Out.

Add StreamCommonImpl::onClose so that the owner of the context
may know when it is safe to reset it.

Re-arrange the order of the arguments when creating a stream
so that the context always comes first.

Bug: 264712385
Test: atest VtsHalAudioCoreTargetTest
Change-Id: Iaf13d4bc3a53cbfc27264d3abd1f6c417ece3941
diff --git a/audio/aidl/default/usb/ModuleUsb.cpp b/audio/aidl/default/usb/ModuleUsb.cpp
index a812e4d..f926e09 100644
--- a/audio/aidl/default/usb/ModuleUsb.cpp
+++ b/audio/aidl/default/usb/ModuleUsb.cpp
@@ -68,22 +68,22 @@
     return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
 }
 
-ndk::ScopedAStatus ModuleUsb::createInputStream(const SinkMetadata& sinkMetadata,
-                                                StreamContext&& context,
+ndk::ScopedAStatus ModuleUsb::createInputStream(StreamContext&& context,
+                                                const SinkMetadata& sinkMetadata,
                                                 const std::vector<MicrophoneInfo>& microphones,
                                                 std::shared_ptr<StreamIn>* result) {
-    return createStreamInstance<StreamInUsb>(result, sinkMetadata, std::move(context), microphones);
+    return createStreamInstance<StreamInUsb>(result, std::move(context), sinkMetadata, microphones);
 }
 
-ndk::ScopedAStatus ModuleUsb::createOutputStream(const SourceMetadata& sourceMetadata,
-                                                 StreamContext&& context,
+ndk::ScopedAStatus ModuleUsb::createOutputStream(StreamContext&& context,
+                                                 const SourceMetadata& sourceMetadata,
                                                  const std::optional<AudioOffloadInfo>& offloadInfo,
                                                  std::shared_ptr<StreamOut>* result) {
     if (offloadInfo.has_value()) {
         LOG(ERROR) << __func__ << ": offload is not supported";
         return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
     }
-    return createStreamInstance<StreamOutUsb>(result, sourceMetadata, std::move(context),
+    return createStreamInstance<StreamOutUsb>(result, std::move(context), sourceMetadata,
                                               offloadInfo);
 }
 
diff --git a/audio/aidl/default/usb/StreamUsb.cpp b/audio/aidl/default/usb/StreamUsb.cpp
index f664245..def12e0 100644
--- a/audio/aidl/default/usb/StreamUsb.cpp
+++ b/audio/aidl/default/usb/StreamUsb.cpp
@@ -35,8 +35,8 @@
 
 namespace aidl::android::hardware::audio::core {
 
-StreamUsb::StreamUsb(const Metadata& metadata, StreamContext&& context)
-    : StreamAlsa(metadata, std::move(context), 1 /*readWriteRetries*/) {}
+StreamUsb::StreamUsb(const StreamContext& context, const Metadata& metadata)
+    : StreamAlsa(context, metadata, 1 /*readWriteRetries*/) {}
 
 ndk::ScopedAStatus StreamUsb::setConnectedDevices(
         const std::vector<AudioDevice>& connectedDevices) {
@@ -55,7 +55,7 @@
         }
         connectedDeviceProfiles.push_back(*profile);
     }
-    RETURN_STATUS_IF_ERROR(StreamCommonImpl::setConnectedDevices(connectedDevices));
+    RETURN_STATUS_IF_ERROR(setConnectedDevices(connectedDevices));
     std::lock_guard guard(mLock);
     mConnectedDeviceProfiles = std::move(connectedDeviceProfiles);
     mConnectedDevicesUpdated.store(true, std::memory_order_release);
@@ -83,9 +83,9 @@
     return connectedDevices;
 }
 
-StreamInUsb::StreamInUsb(const SinkMetadata& sinkMetadata, StreamContext&& context,
+StreamInUsb::StreamInUsb(StreamContext&& context, const SinkMetadata& sinkMetadata,
                          const std::vector<MicrophoneInfo>& microphones)
-    : StreamUsb(sinkMetadata, std::move(context)), StreamIn(microphones) {}
+    : StreamIn(std::move(context), microphones), StreamUsb(StreamIn::mContext, sinkMetadata) {}
 
 ndk::ScopedAStatus StreamInUsb::getActiveMicrophones(
         std::vector<MicrophoneDynamicInfo>* _aidl_return __unused) {
@@ -93,10 +93,10 @@
     return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
 }
 
-StreamOutUsb::StreamOutUsb(const SourceMetadata& sourceMetadata, StreamContext&& context,
+StreamOutUsb::StreamOutUsb(StreamContext&& context, const SourceMetadata& sourceMetadata,
                            const std::optional<AudioOffloadInfo>& offloadInfo)
-    : StreamUsb(sourceMetadata, std::move(context)),
-      StreamOut(offloadInfo),
+    : StreamOut(std::move(context), offloadInfo),
+      StreamUsb(StreamOut::mContext, sourceMetadata),
       mChannelCount(getChannelCount(getContext().getChannelLayout())) {}
 
 ndk::ScopedAStatus StreamOutUsb::getHwVolume(std::vector<float>* _aidl_return) {