AudioFlinger: Extract inner AudioStreamIn and Source classes

Ensure member variables are const.

Test: atest audiorecord_tests audiotrack_tests audiorouting_tests trackplayerbase_tests audiosystem_tests
Test: atest AudioTrackTest AudioRecordTest
Test: YouTube Camera
Bug: 288339104
Bug: 290269791
Merged-In: Idb7586437ce43ae1c8455bcef645a943ad2e79de
Change-Id: Idb7586437ce43ae1c8455bcef645a943ad2e79de
diff --git a/services/audioflinger/AudioFlinger.h b/services/audioflinger/AudioFlinger.h
index ebff932..8629429 100644
--- a/services/audioflinger/AudioFlinger.h
+++ b/services/audioflinger/AudioFlinger.h
@@ -98,6 +98,7 @@
 #include <timing/SynchronizedRecordState.h>
 
 #include <datapath/AudioHwDevice.h>
+#include <datapath/AudioStreamIn.h>
 #include <datapath/AudioStreamOut.h>
 #include <datapath/SpdifStreamOut.h>
 #include <datapath/ThreadMetrics.h>
@@ -577,7 +578,6 @@
     class PatchPanel;
     class DeviceEffectManagerCallback;
 private:
-    struct AudioStreamIn;
     struct TeePatch;
 public:
     using TeePatches = std::vector<TeePatch>;
@@ -764,29 +764,6 @@
                         audio_io_handle_t upStream, const String8& keyValuePairs,
                         const std::function<bool(const sp<PlaybackThread>&)>& useThread = nullptr);
 
-    // AudioStreamIn is immutable, so their fields are const.
-    // For emphasis, we could also make all pointers to them be "const *",
-    // but that would clutter the code unnecessarily.
-
-    struct AudioStreamIn : public Source {
-        AudioHwDevice* const audioHwDev;
-        sp<StreamInHalInterface> stream;
-        audio_input_flags_t flags;
-
-        sp<DeviceHalInterface> hwDev() const { return audioHwDev->hwDevice(); }
-
-        AudioStreamIn(AudioHwDevice *dev, const sp<StreamInHalInterface>& in,
-                audio_input_flags_t flags) :
-            audioHwDev(dev), stream(in), flags(flags) {}
-        status_t read(void *buffer, size_t bytes, size_t *read) override {
-            return stream->read(buffer, bytes, read);
-        }
-        status_t getCapturePosition(int64_t *frames, int64_t *time) override {
-            return stream->getCapturePosition(frames, time);
-        }
-        status_t standby() override { return stream->standby(); }
-    };
-
     struct TeePatch {
         sp<IAfPatchRecord> patchRecord;
         sp<IAfPatchTrack> patchTrack;
diff --git a/services/audioflinger/IAfTrack.h b/services/audioflinger/IAfTrack.h
index 541be0a..4718474 100644
--- a/services/audioflinger/IAfTrack.h
+++ b/services/audioflinger/IAfTrack.h
@@ -549,15 +549,6 @@
                                              *  even if it might glitch. */);
 };
 
-// Abstraction for the Audio Source for the RecordThread (HAL or PassthruPatchRecord).
-struct Source {
-    virtual ~Source() = default;
-    // The following methods have the same signatures as in StreamHalInterface.
-    virtual status_t read(void* buffer, size_t bytes, size_t* read) = 0;
-    virtual status_t getCapturePosition(int64_t* frames, int64_t* time) = 0;
-    virtual status_t standby() = 0;
-};
-
 class IAfPatchRecord : public virtual IAfRecordTrack, public virtual IAfPatchTrackBase {
 public:
     static sp<IAfPatchRecord> create(
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index 8c68751..ba68741 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -9467,7 +9467,7 @@
     return ids;
 }
 
-AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput()
+AudioStreamIn* AudioFlinger::RecordThread::clearInput()
 {
     Mutex::Autolock _l(mLock);
     AudioStreamIn *input = mInput;
@@ -10880,7 +10880,7 @@
     return MmapThread::exitStandby_l();
 }
 
-AudioFlinger::AudioStreamIn* AudioFlinger::MmapCaptureThread::clearInput()
+AudioStreamIn* AudioFlinger::MmapCaptureThread::clearInput()
 {
     Mutex::Autolock _l(mLock);
     AudioStreamIn *input = mInput;
diff --git a/services/audioflinger/datapath/AudioStreamIn.h b/services/audioflinger/datapath/AudioStreamIn.h
new file mode 100644
index 0000000..7b3a090
--- /dev/null
+++ b/services/audioflinger/datapath/AudioStreamIn.h
@@ -0,0 +1,59 @@
+/*
+ * 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
+
+#include <media/audiohal/DeviceHalInterface.h>
+#include <media/audiohal/StreamHalInterface.h>
+
+namespace android {
+
+// Abstraction for the Audio Source for the RecordThread (HAL or PassthruPatchRecord).
+struct Source {
+    virtual ~Source() = default;
+    // The following methods have the same signatures as in StreamHalInterface.
+    virtual status_t read(void* buffer, size_t bytes, size_t* read) = 0;
+    virtual status_t getCapturePosition(int64_t* frames, int64_t* time) = 0;
+    virtual status_t standby() = 0;
+};
+
+// AudioStreamIn is immutable, so its fields are const.
+// The methods must not be const to match StreamHalInterface signature.
+
+struct AudioStreamIn : public Source {
+    const AudioHwDevice* const audioHwDev;
+    const sp<StreamInHalInterface> stream;
+    const audio_input_flags_t flags;
+
+    AudioStreamIn(
+            const AudioHwDevice* dev, const sp<StreamInHalInterface>& in,
+            audio_input_flags_t flags)
+        : audioHwDev(dev), stream(in), flags(flags) {}
+
+    status_t read(void* buffer, size_t bytes, size_t* read) final {
+        return stream->read(buffer, bytes, read);
+    }
+
+    status_t getCapturePosition(int64_t* frames, int64_t* time) final {
+        return stream->getCapturePosition(frames, time);
+    }
+
+    status_t standby() final { return stream->standby(); }
+
+    sp<DeviceHalInterface> hwDev() const { return audioHwDev->hwDevice(); }
+};
+
+}  // namespace android