AudioFlinger: Extract inner Client class

Test: atest audiorecord_tests audiotrack_tests audiorouting_tests trackplayerbase_tests audiosystem_tests
Test: atest AudioTrackTest AudioRecordTest
Test: YouTube and Camera
Bug: 288339104
Bug: 289135349
Merged-In: I5b2973c12b3d36663fadd2e5ed362602785e1447
Change-Id: I5b2973c12b3d36663fadd2e5ed362602785e1447
(cherry picked from commit 59867e43c84de5563c73043594e13941cd016246)
diff --git a/services/audioflinger/AudioFlinger.cpp b/services/audioflinger/AudioFlinger.cpp
index 5e90a34..4ae070b 100644
--- a/services/audioflinger/AudioFlinger.cpp
+++ b/services/audioflinger/AudioFlinger.cpp
@@ -1029,7 +1029,7 @@
     return NO_ERROR;
 }
 
-sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
+sp<Client> AudioFlinger::registerPid(pid_t pid)
 {
     Mutex::Autolock _cl(mClientLock);
     // If pid is already in the mClients wp<> map, then use that entry
@@ -2311,19 +2311,19 @@
 
 // ----------------------------------------------------------------------------
 
-AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
+Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
     :   RefBase(),
         mAudioFlinger(audioFlinger),
         mPid(pid),
         mClientAllocator(AllocatorFactory::getClientAllocator()) {}
 
 // Client destructor must be called with AudioFlinger::mClientLock held
-AudioFlinger::Client::~Client()
+Client::~Client()
 {
     mAudioFlinger->removeClient_l(mPid);
 }
 
-AllocatorFactory::ClientAllocator& AudioFlinger::Client::allocator()
+AllocatorFactory::ClientAllocator& Client::allocator()
 {
     return mClientAllocator;
 }
diff --git a/services/audioflinger/AudioFlinger.h b/services/audioflinger/AudioFlinger.h
index e2d340b..8fbf692 100644
--- a/services/audioflinger/AudioFlinger.h
+++ b/services/audioflinger/AudioFlinger.h
@@ -117,6 +117,8 @@
 #include "android/media/BnAudioRecord.h"
 #include "android/media/BnEffect.h"
 
+#include "Client.h"
+
 // include AudioFlinger component interfaces
 #include "IAfEffect.h"
 
@@ -146,6 +148,7 @@
 class AudioFlinger : public AudioFlingerServerAdapter::Delegate
 {
     friend class sp<AudioFlinger>;
+    friend class Client; // removeClient_l();
 public:
     static void instantiate() ANDROID_API;
 
@@ -497,26 +500,6 @@
 private:
     void dumpToThreadLog_l(const sp<ThreadBase> &thread);
 
-public:
-    // TODO(b/288339104) Move to separate file
-    // --- Client ---
-    class Client : public RefBase {
-      public:
-        Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
-        virtual             ~Client();
-        AllocatorFactory::ClientAllocator& allocator();
-        pid_t               pid() const { return mPid; }
-        sp<AudioFlinger>    audioFlinger() const { return mAudioFlinger; }
-
-    private:
-        DISALLOW_COPY_AND_ASSIGN(Client);
-
-        const sp<AudioFlinger>    mAudioFlinger;
-        const pid_t         mPid;
-        AllocatorFactory::ClientAllocator mClientAllocator;
-    };
-private:
-
     // --- Notification Client ---
     class NotificationClient : public IBinder::DeathRecipient {
     public:
diff --git a/services/audioflinger/Client.h b/services/audioflinger/Client.h
new file mode 100644
index 0000000..cb507fe
--- /dev/null
+++ b/services/audioflinger/Client.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2023 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.
+ */
+
+#pragma once
+
+// TODO(b/288339104) Move to nested namespace
+namespace android {
+
+class AudioFlinger;
+
+class Client : public RefBase {
+public:
+    Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
+
+    // TODO(b/289139675) make Client container.
+    // Client destructor must be called with AudioFlinger::mClientLock held
+    ~Client() override;
+    AllocatorFactory::ClientAllocator& allocator();
+    pid_t pid() const { return mPid; }
+    sp<AudioFlinger> audioFlinger() const { return mAudioFlinger; }
+
+private:
+    DISALLOW_COPY_AND_ASSIGN(Client);
+
+    const sp<AudioFlinger> mAudioFlinger;
+    const pid_t mPid;
+    AllocatorFactory::ClientAllocator mClientAllocator;
+};
+
+} // namespace android
diff --git a/services/audioflinger/DeviceEffectManager.cpp b/services/audioflinger/DeviceEffectManager.cpp
index ac0a842..325f17b 100644
--- a/services/audioflinger/DeviceEffectManager.cpp
+++ b/services/audioflinger/DeviceEffectManager.cpp
@@ -59,7 +59,7 @@
 sp<IAfEffectHandle> AudioFlinger::DeviceEffectManager::createEffect_l(
         effect_descriptor_t *descriptor,
         const AudioDeviceTypeAddr& device,
-        const sp<AudioFlinger::Client>& client,
+        const sp<Client>& client,
         const sp<IEffectClient>& effectClient,
         const std::map<audio_patch_handle_t, PatchPanel::Patch>& patches,
         int *enabled,
diff --git a/services/audioflinger/DeviceEffectManager.h b/services/audioflinger/DeviceEffectManager.h
index 3a33a71..c589714 100644
--- a/services/audioflinger/DeviceEffectManager.h
+++ b/services/audioflinger/DeviceEffectManager.h
@@ -32,7 +32,7 @@
 
     sp<IAfEffectHandle> createEffect_l(effect_descriptor_t *descriptor,
                 const AudioDeviceTypeAddr& device,
-                const sp<AudioFlinger::Client>& client,
+                const sp<Client>& client,
                 const sp<media::IEffectClient>& effectClient,
                 const std::map<audio_patch_handle_t, PatchPanel::Patch>& patches,
                 int *enabled,
diff --git a/services/audioflinger/Effects.cpp b/services/audioflinger/Effects.cpp
index d891a32..75530c0 100644
--- a/services/audioflinger/Effects.cpp
+++ b/services/audioflinger/Effects.cpp
@@ -1674,17 +1674,16 @@
 /* static */
 sp<IAfEffectHandle> IAfEffectHandle::create(
         const sp<IAfEffectBase>& effect,
-        const sp<RefBase /*AudioFlinger::Client */>& client, // TODO(b/288339104) update type
+        const sp<Client>& client,
         const sp<media::IEffectClient>& effectClient,
         int32_t priority, bool notifyFramesProcessed)
 {
     return sp<EffectHandle>::make(
-            effect, sp<AudioFlinger::Client>::cast(client),
-            effectClient, priority, notifyFramesProcessed);
+            effect, client, effectClient, priority, notifyFramesProcessed);
 }
 
 EffectHandle::EffectHandle(const sp<IAfEffectBase>& effect,
-                                         const sp<AudioFlinger::Client>& client,
+                                         const sp<Client>& client,
                                          const sp<media::IEffectClient>& effectClient,
                                          int32_t priority, bool notifyFramesProcessed)
     : BnEffect(),
diff --git a/services/audioflinger/Effects.h b/services/audioflinger/Effects.h
index 796b6c5..b8d495d 100644
--- a/services/audioflinger/Effects.h
+++ b/services/audioflinger/Effects.h
@@ -290,7 +290,7 @@
 public:
 
     EffectHandle(const sp<IAfEffectBase>& effect,
-            const sp<AudioFlinger::Client>& client,
+            const sp<Client>& client,
             const sp<media::IEffectClient>& effectClient,
             int32_t priority, bool notifyFramesProcessed);
     ~EffectHandle() override;
@@ -311,8 +311,7 @@
     android::binder::Status getConfig(media::EffectConfig* _config,
                                       int32_t* _aidl_return) final;
 
-    // TODO(b/288339104) type
-    sp<RefBase /* AudioFlinger::Client */> client() const final { return mClient; }
+    const sp<Client>& client() const final { return mClient; }
 
     sp<android::media::IEffect> asIEffect() final {
         return sp<android::media::IEffect>::fromExisting(this);
@@ -357,7 +356,7 @@
     Mutex mLock;                             // protects IEffect method calls
     const wp<IAfEffectBase> mEffect;               // pointer to controlled EffectModule
     const sp<media::IEffectClient> mEffectClient;  // callback interface for client notifications
-    /*const*/ sp<AudioFlinger::Client> mClient;    // client for shared memory allocation, see
+    /*const*/ sp<Client> mClient;            // client for shared memory allocation, see
                                              //   disconnect()
     sp<IMemory> mCblkMemory;                 // shared memory for control block
     effect_param_cblk_t* mCblk;              // control block for deferred parameter setting via
diff --git a/services/audioflinger/IAfEffect.h b/services/audioflinger/IAfEffect.h
index 7c3be0f..75112ca 100644
--- a/services/audioflinger/IAfEffect.h
+++ b/services/audioflinger/IAfEffect.h
@@ -307,7 +307,7 @@
 public:
     static sp<IAfEffectHandle> create(
             const sp<IAfEffectBase>& effect,
-            const sp<RefBase /*AudioFlinger::Client */>& client,  // TODO(b/288339104) type
+            const sp<Client>& client,
             const sp<media::IEffectClient>& effectClient,
             int32_t priority, bool notifyFramesProcessed);
 
@@ -316,8 +316,7 @@
     virtual int id() const = 0;
     virtual wp<IAfEffectBase> effect() const = 0;
     virtual sp<android::media::IEffect> asIEffect() = 0;
-    // TODO(b/288339104) type
-    virtual sp<RefBase /* AudioFlinger::Client */> client() const = 0;
+    virtual const sp<Client>& client() const = 0;
 
 private:
     virtual void setControl(bool hasControl, bool signal, bool enabled) = 0;
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index 2b25599..16f5a98 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -1534,7 +1534,7 @@
 
 // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held
 sp<IAfEffectHandle> AudioFlinger::ThreadBase::createEffect_l(
-        const sp<AudioFlinger::Client>& client,
+        const sp<Client>& client,
         const sp<IEffectClient>& effectClient,
         int32_t priority,
         audio_session_t sessionId,
@@ -2298,7 +2298,7 @@
 
 // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held
 sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l(
-        const sp<AudioFlinger::Client>& client,
+        const sp<Client>& client,
         audio_stream_type_t streamType,
         const audio_attributes_t& attr,
         uint32_t *pSampleRate,
@@ -8579,7 +8579,7 @@
 
 // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held
 sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l(
-        const sp<AudioFlinger::Client>& client,
+        const sp<Client>& client,
         const audio_attributes_t& attr,
         uint32_t *pSampleRate,
         audio_format_t format,
diff --git a/services/audioflinger/Threads.h b/services/audioflinger/Threads.h
index b4b0e75..ed75fc0 100644
--- a/services/audioflinger/Threads.h
+++ b/services/audioflinger/Threads.h
@@ -411,7 +411,7 @@
     virtual     sp<StreamHalInterface> stream() const = 0;
 
                 sp<IAfEffectHandle> createEffect_l(
-                                    const sp<AudioFlinger::Client>& client,
+                                    const sp<Client>& client,
                                     const sp<media::IEffectClient>& effectClient,
                                     int32_t priority,
                                     audio_session_t sessionId,
@@ -981,7 +981,7 @@
                 void        setVolumeForOutput_l(float left, float right) const override;
 
                 sp<Track>   createTrack_l(
-                                const sp<AudioFlinger::Client>& client,
+                                const sp<Client>& client,
                                 audio_stream_type_t streamType,
                                 const audio_attributes_t& attr,
                                 uint32_t *sampleRate,
@@ -1943,7 +1943,7 @@
     virtual sp<IMemory> pipeMemory() const { return mPipeMemory; }
 
             sp<AudioFlinger::RecordThread::RecordTrack>  createRecordTrack_l(
-                    const sp<AudioFlinger::Client>& client,
+                    const sp<Client>& client,
                     const audio_attributes_t& attr,
                     uint32_t *pSampleRate,
                     audio_format_t format,