libaudiohal@aidl: Fix callbacks handling

- Ensure that 'StreamOutHalInterface' slice pointer is used
  by all parties.
- Handle state changes in StreamHalAidl.
- Pace stream state queries by the buffer duration (for sync
  modes) or by a fixed amount (for async) and allow it in
  any state and from any thread.

Bug: 300416194
Bug: 331300943
Test: mp3 and ogg offloaded playback (bug repro steps)
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:8065bfd2de9b7c7c8e6cb344866c69c0d77538ca)
Merged-In: I953cfc8862aa466d17f7992df052b7b68fb97e0a
Change-Id: I953cfc8862aa466d17f7992df052b7b68fb97e0a
diff --git a/media/libaudiohal/impl/DeviceHalAidl.cpp b/media/libaudiohal/impl/DeviceHalAidl.cpp
index f865603..2447b18 100644
--- a/media/libaudiohal/impl/DeviceHalAidl.cpp
+++ b/media/libaudiohal/impl/DeviceHalAidl.cpp
@@ -478,15 +478,21 @@
                 __func__, ret.desc.toString().c_str());
         return NO_INIT;
     }
-    *outStream = sp<StreamOutHalAidl>::make(*config, std::move(context), aidlPatch.latenciesMs[0],
+    auto stream = sp<StreamOutHalAidl>::make(*config, std::move(context), aidlPatch.latenciesMs[0],
             std::move(ret.stream), mVendorExt, this /*callbackBroker*/);
-    void* cbCookie = (*outStream).get();
+    *outStream = stream;
+    /* StreamOutHalInterface* */ void* cbCookie = (*outStream).get();
     {
         std::lock_guard l(mLock);
         mCallbacks.emplace(cbCookie, Callbacks{});
         mMapper.addStream(*outStream, mixPortConfig.id, aidlPatch.id);
     }
-    if (streamCb) streamCb->setCookie(cbCookie);
+    if (streamCb) {
+        streamCb->setCookie(cbCookie);
+        // Although StreamOutHalAidl implements StreamOutHalInterfaceCallback,
+        // we always go via the CallbackBroker for consistency.
+        setStreamOutCallback(cbCookie, stream);
+    }
     eventCb->setCookie(cbCookie);
     cleanups.disarmAll();
     return OK;