/*
 * 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 <android/media/IAudioTrackCallback.h>
#include <android/media/IEffectClient.h>
#include <audiomanager/IAudioManager.h>
#include <audio_utils/DeferredExecutor.h>
#include <audio_utils/MelProcessor.h>
#include <audio_utils/mutex.h>
#include <binder/MemoryDealer.h>
#include <datapath/AudioStreamIn.h>
#include <datapath/AudioStreamOut.h>
#include <datapath/VolumeInterface.h>
#include <datapath/VolumePortInterface.h>
#include <fastpath/FastMixerDumpState.h>
#include <media/DeviceDescriptorBase.h>
#include <media/MmapStreamInterface.h>
#include <media/audiohal/StreamHalInterface.h>
#include <media/nblog/NBLog.h>
#include <timing/SyncEvent.h>
#include <utils/RefBase.h>
#include <vibrator/ExternalVibration.h>

#include <optional>

namespace com::android::media::permission {
    class IPermissionProvider;
}

namespace android {

class IAfDirectOutputThread;
class IAfDuplicatingThread;
class IAfMmapCaptureThread;
class IAfMmapPlaybackThread;
class IAfPlaybackThread;
class IAfRecordThread;

class IAfEffectChain;
class IAfEffectHandle;
class IAfEffectModule;
class IAfPatchPanel;
class IAfPatchRecord;
class IAfPatchTrack;
class IAfRecordTrack;
class IAfTrack;
class IAfTrackBase;
class Client;
class MelReporter;

// Used internally for Threads.cpp and AudioFlinger.cpp
struct stream_type_t {
    float volume = 1.f;
    bool mute = false;
};

// Note this is exposed through IAfThreadBase::afThreadCallback()
// and hence may be used by the Effect / Track framework.
class IAfThreadCallback : public virtual RefBase {
public:
    virtual audio_utils::mutex& mutex() const
            RETURN_CAPABILITY(audio_utils::AudioFlinger_Mutex) = 0;
    virtual bool isNonOffloadableGlobalEffectEnabled_l() const
            REQUIRES(mutex()) EXCLUDES_ThreadBase_Mutex = 0;  // Tracks
    virtual audio_unique_id_t nextUniqueId(audio_unique_id_use_t use) = 0;
    virtual bool btNrecIsOff() const = 0;
    virtual float masterVolume_l() const
            REQUIRES(mutex()) = 0;
    virtual bool masterMute_l() const
            REQUIRES(mutex()) = 0;
    virtual float getMasterBalance_l() const
            REQUIRES(mutex()) = 0;
    virtual bool streamMute_l(audio_stream_type_t stream) const
            REQUIRES(mutex()) = 0;
    virtual audio_mode_t getMode() const = 0;
    virtual bool isLowRamDevice() const = 0;
    virtual bool isAudioPolicyReady() const = 0;  // Effects
    virtual uint32_t getScreenState() const = 0;
    virtual std::optional<media::AudioVibratorInfo> getDefaultVibratorInfo_l() const
            REQUIRES(mutex()) = 0;
    virtual const sp<IAfPatchPanel>& getPatchPanel() const = 0;
    virtual const sp<MelReporter>& getMelReporter() const = 0;
    virtual const sp<EffectsFactoryHalInterface>& getEffectsFactoryHal() const = 0;
    virtual sp<IAudioManager> getOrCreateAudioManager() = 0;  // Tracks

    virtual bool updateOrphanEffectChains(const sp<IAfEffectModule>& effect)
            EXCLUDES_AudioFlinger_Mutex = 0;
    virtual status_t moveEffectChain_ll(audio_session_t sessionId,
            IAfPlaybackThread* srcThread, IAfPlaybackThread* dstThread,
            IAfEffectChain* srcChain = nullptr)
            REQUIRES(mutex(), audio_utils::ThreadBase_Mutex) = 0;

    virtual void requestLogMerge() = 0;
    virtual sp<NBLog::Writer> newWriter_l(size_t size, const char *name)
            REQUIRES(mutex()) = 0;
    virtual void unregisterWriter(const sp<NBLog::Writer>& writer) = 0;

    virtual sp<audioflinger::SyncEvent> createSyncEvent(AudioSystem::sync_event_t type,
            audio_session_t triggerSession,
            audio_session_t listenerSession,
            const audioflinger::SyncEventCallback& callBack,
            const wp<IAfTrackBase>& cookie)
            EXCLUDES_AudioFlinger_Mutex = 0;

    // Hold either AudioFlinger::mutex or ThreadBase::mutex
    virtual void ioConfigChanged_l(audio_io_config_event_t event,
            const sp<AudioIoDescriptor>& ioDesc,
            pid_t pid = 0) EXCLUDES_AudioFlinger_ClientMutex = 0;
    virtual void onNonOffloadableGlobalEffectEnable() EXCLUDES_AudioFlinger_Mutex = 0;
    virtual void onSupportedLatencyModesChanged(audio_io_handle_t output,
                                                const std::vector<audio_latency_mode_t>& modes)
            EXCLUDES_AudioFlinger_ClientMutex = 0;

    virtual void onHardError(std::set<audio_port_handle_t>& trackPortIds) = 0;

    virtual const ::com::android::media::permission::IPermissionProvider&
            getPermissionProvider() = 0;
};

class IAfThreadBase : public virtual RefBase {
public:
    enum type_t {
        MIXER,          // Thread class is MixerThread
        DIRECT,         // Thread class is DirectOutputThread
        DUPLICATING,    // Thread class is DuplicatingThread
        RECORD,         // Thread class is RecordThread
        OFFLOAD,        // Thread class is OffloadThread
        MMAP_PLAYBACK,  // Thread class for MMAP playback stream
        MMAP_CAPTURE,   // Thread class for MMAP capture stream
        SPATIALIZER,    //
        BIT_PERFECT,    // Thread class for BitPerfectThread
        // When adding a value, also update IAfThreadBase::threadTypeToString()
    };

    static const char* threadTypeToString(type_t type);
    static std::string formatToString(audio_format_t format);  // compliant for MediaMetrics
    static bool isValidPcmSinkChannelMask(audio_channel_mask_t channelMask);
    static bool isValidPcmSinkFormat(audio_format_t format);

    virtual status_t readyToRun() = 0;
    virtual void clearPowerManager() EXCLUDES_ThreadBase_Mutex = 0;
    virtual status_t initCheck() const = 0;
    virtual type_t type() const = 0;
    virtual bool isDuplicating() const = 0;
    virtual audio_io_handle_t id() const = 0;
    virtual uint32_t sampleRate() const = 0;
    virtual audio_channel_mask_t channelMask() const = 0;
    virtual audio_channel_mask_t mixerChannelMask() const = 0;
    virtual audio_format_t format() const = 0;
    virtual uint32_t channelCount() const = 0;

    // Called by AudioFlinger::frameCount(audio_io_handle_t output) and effects,
    // and returns the [normal mix] buffer's frame count.
    virtual size_t frameCount() const = 0;
    virtual audio_channel_mask_t hapticChannelMask() const = 0;
    virtual uint32_t hapticChannelCount() const = 0;
    virtual uint32_t latency_l() const = 0;  // NO_THREAD_SAFETY_ANALYSIS
    virtual void setVolumeForOutput_l(float left, float right) const REQUIRES(mutex()) = 0;

    // Return's the HAL's frame count i.e. fast mixer buffer size.
    virtual size_t frameCountHAL() const = 0;
    virtual size_t frameSize() const = 0;
    // Should be "virtual status_t requestExitAndWait()" and override same
    // method in Thread, but Thread::requestExitAndWait() is not yet virtual.
    virtual void exit() EXCLUDES_ThreadBase_Mutex = 0;
    virtual bool checkForNewParameter_l(const String8& keyValuePair, status_t& status)
             REQUIRES(mutex()) = 0;
    virtual status_t setParameters(const String8& keyValuePairs) EXCLUDES_ThreadBase_Mutex = 0;
    virtual String8 getParameters(const String8& keys) EXCLUDES_ThreadBase_Mutex = 0;
    virtual void ioConfigChanged_l(
            audio_io_config_event_t event, pid_t pid = 0,
            audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE)
            /* holds either AF::mutex or TB::mutex */ = 0;

    // sendConfigEvent_l() must be called with ThreadBase::mLock held
    // Can temporarily release the lock if waiting for a reply from
    // processConfigEvents_l().
    // status_t sendConfigEvent_l(sp<ConfigEvent>& event);
    virtual void sendIoConfigEvent(
            audio_io_config_event_t event, pid_t pid = 0,
            audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) EXCLUDES_ThreadBase_Mutex = 0;
    virtual void sendIoConfigEvent_l(
            audio_io_config_event_t event, pid_t pid = 0,
            audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) REQUIRES(mutex()) = 0;
    virtual void sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp)
            EXCLUDES_ThreadBase_Mutex = 0;
    virtual void sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio, bool forApp)
            REQUIRES(mutex()) = 0;
    virtual status_t sendSetParameterConfigEvent_l(const String8& keyValuePair)
            REQUIRES(mutex()) = 0;
    virtual status_t sendCreateAudioPatchConfigEvent(
            const struct audio_patch* patch, audio_patch_handle_t* handle)
            EXCLUDES_ThreadBase_Mutex = 0;
    virtual status_t sendReleaseAudioPatchConfigEvent(audio_patch_handle_t handle)
            EXCLUDES_ThreadBase_Mutex = 0;
    virtual status_t sendUpdateOutDeviceConfigEvent(
            const DeviceDescriptorBaseVector& outDevices) EXCLUDES_ThreadBase_Mutex = 0;
    virtual void sendResizeBufferConfigEvent_l(int32_t maxSharedAudioHistoryMs)
            REQUIRES(mutex()) = 0;
    virtual void sendCheckOutputStageEffectsEvent() EXCLUDES_ThreadBase_Mutex = 0;
    virtual void sendCheckOutputStageEffectsEvent_l()
            REQUIRES(mutex()) = 0;
    virtual void sendHalLatencyModesChangedEvent_l()
            REQUIRES(mutex()) = 0;

    virtual void processConfigEvents_l()
            REQUIRES(mutex()) = 0;
    virtual void setCheckOutputStageEffects() = 0;  // no mutex needed
    virtual void cacheParameters_l()
            REQUIRES(mutex()) = 0;
    virtual status_t createAudioPatch_l(
            const struct audio_patch* patch, audio_patch_handle_t* handle)
            REQUIRES(mutex()) = 0;
    virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle)
            REQUIRES(mutex()) = 0;
    virtual void updateOutDevices(const DeviceDescriptorBaseVector& outDevices)
            EXCLUDES_ThreadBase_Mutex = 0;
    virtual void toAudioPortConfig(struct audio_port_config* config)
            EXCLUDES_ThreadBase_Mutex = 0;
    virtual void resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs)
            REQUIRES(mutex()) = 0;

    // see note at declaration of mStandby, mOutDevice and mInDevice
    virtual bool inStandby() const = 0;
    virtual const DeviceTypeSet outDeviceTypes_l() const REQUIRES(mutex()) = 0;
    virtual audio_devices_t inDeviceType_l() const REQUIRES(mutex()) = 0;
    virtual DeviceTypeSet getDeviceTypes_l() const REQUIRES(mutex()) = 0;
    virtual const AudioDeviceTypeAddrVector& outDeviceTypeAddrs() const = 0;
    virtual const AudioDeviceTypeAddr& inDeviceTypeAddr() const = 0;
    virtual bool isOutput() const = 0;
    virtual bool isOffloadOrMmap() const = 0;
    virtual sp<StreamHalInterface> stream() const = 0;
    virtual sp<IAfEffectHandle> createEffect_l(
            const sp<Client>& client,
            const sp<media::IEffectClient>& effectClient,
            int32_t priority,
            audio_session_t sessionId,
            effect_descriptor_t* desc,
            int* enabled,
            status_t* status /*non-NULL*/,
            bool pinned,
            bool probe,
            bool notifyFramesProcessed)
            REQUIRES(audio_utils::AudioFlinger_Mutex) EXCLUDES_ThreadBase_Mutex = 0;

    // return values for hasAudioSession (bit field)
    enum effect_state {
        EFFECT_SESSION = 0x1,       // the audio session corresponds to at least one
                                    // effect
        TRACK_SESSION = 0x2,        // the audio session corresponds to at least one
                                    // track
        FAST_SESSION = 0x4,         // the audio session corresponds to at least one
                                    // fast track
        SPATIALIZED_SESSION = 0x8,  // the audio session corresponds to at least one
                                    // spatialized track
        BIT_PERFECT_SESSION = 0x10  // the audio session corresponds to at least one
                                    // bit-perfect track
    };

    // get effect chain corresponding to session Id.
    virtual sp<IAfEffectChain> getEffectChain(audio_session_t sessionId) const
            EXCLUDES_ThreadBase_Mutex = 0;
    // same as getEffectChain() but must be called with ThreadBase mutex locked
    virtual sp<IAfEffectChain> getEffectChain_l(audio_session_t sessionId) const
            REQUIRES(mutex()) = 0;
    virtual std::vector<int> getEffectIds_l(audio_session_t sessionId) const
            REQUIRES(mutex()) = 0;
    // add an effect chain to the chain list (mEffectChains)
    virtual status_t addEffectChain_l(const sp<IAfEffectChain>& chain)
            REQUIRES(mutex()) = 0;
    // remove an effect chain from the chain list (mEffectChains)
    virtual size_t removeEffectChain_l(const sp<IAfEffectChain>& chain)
            REQUIRES(mutex()) = 0;
    // lock all effect chains Mutexes. Must be called before releasing the
    // ThreadBase mutex before processing the mixer and effects. This guarantees the
    // integrity of the chains during the process.
    // Also sets the parameter 'effectChains' to current value of mEffectChains.
    virtual void lockEffectChains_l(Vector<sp<IAfEffectChain>>& effectChains)
            REQUIRES(mutex()) EXCLUDES_EffectChain_Mutex = 0;
    // unlock effect chains after process
    virtual void unlockEffectChains(const Vector<sp<IAfEffectChain>>& effectChains)
            EXCLUDES_ThreadBase_Mutex = 0;
    // get a copy of mEffectChains vector
    virtual Vector<sp<IAfEffectChain>> getEffectChains_l() const
            REQUIRES(mutex()) = 0;
    // set audio mode to all effect chains
    virtual void setMode(audio_mode_t mode)
            EXCLUDES_ThreadBase_Mutex = 0;
    // get effect module with corresponding ID on specified audio session
    virtual sp<IAfEffectModule> getEffect(audio_session_t sessionId, int effectId) const
            EXCLUDES_ThreadBase_Mutex = 0;
    virtual sp<IAfEffectModule> getEffect_l(audio_session_t sessionId, int effectId) const
            REQUIRES(mutex()) = 0;
    // add and effect module. Also creates the effect chain is none exists for
    // the effects audio session. Only called in a context of moving an effect
    // from one thread to another
    virtual status_t addEffect_ll(const sp<IAfEffectModule>& effect)
            REQUIRES(audio_utils::AudioFlinger_Mutex, mutex()) = 0;
    // remove and effect module. Also removes the effect chain is this was the last
    // effect
    virtual void removeEffect_l(const sp<IAfEffectModule>& effect, bool release = false)
            REQUIRES(mutex()) = 0;
    // disconnect an effect handle from module and destroy module if last handle
    virtual void disconnectEffectHandle(IAfEffectHandle* handle, bool unpinIfLast)
            EXCLUDES_ThreadBase_Mutex = 0;
    // detach all tracks connected to an auxiliary effect
    virtual void detachAuxEffect_l(int effectId) REQUIRES(mutex()) = 0;
    // returns a combination of:
    // - EFFECT_SESSION if effects on this audio session exist in one chain
    // - TRACK_SESSION if tracks on this audio session exist
    // - FAST_SESSION if fast tracks on this audio session exist
    // - SPATIALIZED_SESSION if spatialized tracks on this audio session exist
    virtual uint32_t hasAudioSession_l(audio_session_t sessionId) const REQUIRES(mutex()) = 0;
    virtual uint32_t hasAudioSession(audio_session_t sessionId) const
            EXCLUDES_ThreadBase_Mutex = 0;

    // the value returned by default implementation is not important as the
    // strategy is only meaningful for PlaybackThread which implements this method
    virtual product_strategy_t getStrategyForSession_l(audio_session_t sessionId) const
            REQUIRES(mutex()) = 0;

    // check if some effects must be suspended/restored when an effect is enabled
    // or disabled
    virtual void checkSuspendOnEffectEnabled(
            bool enabled, audio_session_t sessionId, bool threadLocked)
            EXCLUDES_ThreadBase_Mutex = 0;

    virtual status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event)
            EXCLUDES_ThreadBase_Mutex = 0;
    // internally static, perhaps make static member.
    virtual bool isValidSyncEvent(const sp<audioflinger::SyncEvent>& event) const = 0;

    // Return a reference to a per-thread heap which can be used to allocate IMemory
    // objects that will be read-only to client processes, read/write to mediaserver,
    // and shared by all client processes of the thread.
    // The heap is per-thread rather than common across all threads, because
    // clients can't be trusted not to modify the offset of the IMemory they receive.
    // If a thread does not have such a heap, this method returns 0.
    virtual sp<MemoryDealer> readOnlyHeap() const = 0;

    virtual sp<IMemory> pipeMemory() const = 0;

    virtual void systemReady() EXCLUDES_ThreadBase_Mutex = 0;

    // checkEffectCompatibility_l() must be called with ThreadBase::mLock held
    virtual status_t checkEffectCompatibility_l(
            const effect_descriptor_t* desc, audio_session_t sessionId) REQUIRES(mutex()) = 0;

    virtual void broadcast_l() REQUIRES(mutex()) = 0;

    virtual bool isTimestampCorrectionEnabled_l() const REQUIRES(mutex()) = 0;

    virtual bool isMsdDevice() const = 0;

    virtual void dump(int fd, const Vector<String16>& args) EXCLUDES_ThreadBase_Mutex = 0;

    // deliver stats to mediametrics.
    virtual void sendStatistics(bool force) EXCLUDES_ThreadBase_Mutex = 0;

    virtual audio_utils::mutex& mutex() const
            RETURN_CAPABILITY(audio_utils::ThreadBase_Mutex) = 0;

    virtual void onEffectEnable(const sp<IAfEffectModule>& effect) EXCLUDES_ThreadBase_Mutex = 0;
    virtual void onEffectDisable() EXCLUDES_ThreadBase_Mutex = 0;

    // invalidateTracksForAudioSession_l must be called with holding mLock.
    virtual void invalidateTracksForAudioSession_l(audio_session_t sessionId) const
            REQUIRES(mutex()) = 0;
    // Invalidate all the tracks with the given audio session.
    virtual void invalidateTracksForAudioSession(audio_session_t sessionId) const
            EXCLUDES_ThreadBase_Mutex = 0;

    virtual bool isStreamInitialized() const = 0;
    virtual void startMelComputation_l(const sp<audio_utils::MelProcessor>& processor)
            REQUIRES(audio_utils::AudioFlinger_Mutex) = 0;
    virtual void stopMelComputation_l()
            REQUIRES(audio_utils::AudioFlinger_Mutex) = 0;

    virtual product_strategy_t getStrategyForStream(audio_stream_type_t stream) const
            EXCLUDES_AUDIO_ALL = 0;

    virtual void setEffectSuspended_l(
            const effect_uuid_t* type, bool suspend, audio_session_t sessionId)
            REQUIRES(mutex()) = 0;

    // Wait while the Thread is busy.  This is done to ensure that
    // the Thread is not busy releasing the Tracks, during which the Thread mutex
    // may be temporarily unlocked.  Some Track methods will use this method to
    // avoid races.
    virtual void waitWhileThreadBusy_l(audio_utils::unique_lock<audio_utils::mutex>& ul)
            REQUIRES(mutex()) = 0;

    // The ThreadloopExecutor is used to defer functors or dtors
    // to when the Threadloop does not hold any mutexes (at the end of the
    // processing period cycle).
    virtual audio_utils::DeferredExecutor& getThreadloopExecutor() = 0;

    // Dynamic cast to derived interface
    virtual sp<IAfDirectOutputThread> asIAfDirectOutputThread() { return nullptr; }
    virtual sp<IAfDuplicatingThread> asIAfDuplicatingThread() { return nullptr; }
    virtual sp<IAfPlaybackThread> asIAfPlaybackThread() { return nullptr; }
    virtual sp<IAfRecordThread> asIAfRecordThread() { return nullptr; }
    virtual IAfThreadCallback* afThreadCallback() const = 0;
};

class IAfPlaybackThread : public virtual IAfThreadBase, public virtual VolumeInterface {
public:
    static sp<IAfPlaybackThread> createBitPerfectThread(
            const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
            audio_io_handle_t id, bool systemReady);

    static sp<IAfPlaybackThread> createDirectOutputThread(
            const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
            audio_io_handle_t id, bool systemReady, const audio_offload_info_t& offloadInfo);

    static sp<IAfPlaybackThread> createMixerThread(
            const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
            audio_io_handle_t id, bool systemReady, type_t type = MIXER,
            audio_config_base_t* mixerConfig = nullptr);

    static sp<IAfPlaybackThread> createOffloadThread(
            const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
            audio_io_handle_t id, bool systemReady, const audio_offload_info_t& offloadInfo);

    static sp<IAfPlaybackThread> createSpatializerThread(
            const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output,
            audio_io_handle_t id, bool systemReady, audio_config_base_t* mixerConfig);

    static constexpr int8_t kMaxTrackStopRetriesOffload = 2;

    enum mixer_state {
        MIXER_IDLE,            // no active tracks
        MIXER_TRACKS_ENABLED,  // at least one active track, but no track has any data ready
        MIXER_TRACKS_READY,    // at least one active track, and at least one track has data
        MIXER_DRAIN_TRACK,     // drain currently playing track
        MIXER_DRAIN_ALL,       // fully drain the hardware
        // standby mode does not have an enum value
        // suspend by audio policy manager is orthogonal to mixer state
    };

    // return estimated latency in milliseconds, as reported by HAL
    virtual uint32_t latency() const = 0;  // should be in IAfThreadBase?

    virtual uint32_t& fastTrackAvailMask_l() REQUIRES(mutex()) = 0;

    virtual sp<IAfTrack> createTrack_l(
            const sp<Client>& client,
            audio_stream_type_t streamType,
            const audio_attributes_t& attr,
            uint32_t* sampleRate,
            audio_format_t format,
            audio_channel_mask_t channelMask,
            size_t* pFrameCount,
            size_t* pNotificationFrameCount,
            uint32_t notificationsPerBuffer,
            float speed,
            const sp<IMemory>& sharedBuffer,
            audio_session_t sessionId,
            audio_output_flags_t* flags,
            pid_t creatorPid,
            const AttributionSourceState& attributionSource,
            pid_t tid,
            status_t* status /*non-NULL*/,
            audio_port_handle_t portId,
            const sp<media::IAudioTrackCallback>& callback,
            bool isSpatialized,
            bool isBitPerfect,
            audio_output_flags_t* afTrackFlags,
            float volume,
            bool muted)
            REQUIRES(audio_utils::AudioFlinger_Mutex) = 0;

    virtual status_t addTrack_l(const sp<IAfTrack>& track) REQUIRES(mutex()) = 0;
    virtual bool destroyTrack_l(const sp<IAfTrack>& track) REQUIRES(mutex()) = 0;
    virtual bool isTrackActive(const sp<IAfTrack>& track) const REQUIRES(mutex()) = 0;
    virtual void addOutputTrack_l(const sp<IAfTrack>& track) REQUIRES(mutex()) = 0;

    virtual AudioStreamOut* getOutput_l() const REQUIRES(mutex()) = 0;
    virtual AudioStreamOut* getOutput() const EXCLUDES_ThreadBase_Mutex = 0;
    virtual AudioStreamOut* clearOutput() EXCLUDES_ThreadBase_Mutex = 0;

    // a very large number of suspend() will eventually wraparound, but unlikely
    virtual void suspend() = 0;
    virtual void restore() = 0;
    virtual bool isSuspended() const = 0;
    virtual status_t getRenderPosition(uint32_t* halFrames, uint32_t* dspFrames) const
            EXCLUDES_ThreadBase_Mutex = 0;
    // Consider also removing and passing an explicit mMainBuffer initialization
    // parameter to AF::IAfTrack::Track().
    virtual float* sinkBuffer() const = 0;

    virtual status_t attachAuxEffect(const sp<IAfTrack>& track, int EffectId)
            EXCLUDES_ThreadBase_Mutex = 0;
    virtual status_t attachAuxEffect_l(const sp<IAfTrack>& track, int EffectId)
            REQUIRES(mutex()) = 0;

    // called with AudioFlinger lock held
    virtual bool invalidateTracks_l(audio_stream_type_t streamType) REQUIRES(mutex()) = 0;
    virtual bool invalidateTracks_l(std::set<audio_port_handle_t>& portIds) REQUIRES(mutex()) = 0;
    virtual void invalidateTracks(audio_stream_type_t streamType)
            EXCLUDES_ThreadBase_Mutex = 0;
    // Invalidate tracks by a set of port ids. The port id will be removed from
    // the given set if the corresponding track is found and invalidated.
    virtual void invalidateTracks(std::set<audio_port_handle_t>& portIds)
            EXCLUDES_ThreadBase_Mutex = 0;

    virtual status_t getTimestamp_l(AudioTimestamp& timestamp) REQUIRES(mutex()) = 0;
    virtual void addPatchTrack(const sp<IAfPatchTrack>& track) EXCLUDES_ThreadBase_Mutex = 0;
    virtual void deletePatchTrack(const sp<IAfPatchTrack>& track) EXCLUDES_ThreadBase_Mutex = 0;

    // Return the asynchronous signal wait time.
    virtual int64_t computeWaitTimeNs_l() const REQUIRES(mutex()) = 0;
    // returns true if the track is allowed to be added to the thread.
    virtual bool isTrackAllowed_l(
            audio_channel_mask_t channelMask, audio_format_t format, audio_session_t sessionId,
            uid_t uid) const REQUIRES(mutex()) = 0;

    virtual bool supportsHapticPlayback() const = 0;

    virtual void setDownStreamPatch(const struct audio_patch* patch)
            EXCLUDES_ThreadBase_Mutex = 0;

    virtual IAfTrack* getTrackById_l(audio_port_handle_t trackId) REQUIRES(mutex()) = 0;

    virtual bool hasMixer() const = 0;

    virtual status_t setRequestedLatencyMode(audio_latency_mode_t mode) = 0;

    virtual status_t getSupportedLatencyModes(std::vector<audio_latency_mode_t>* modes)
           EXCLUDES_ThreadBase_Mutex = 0;

    virtual status_t setBluetoothVariableLatencyEnabled(bool enabled) = 0;

    virtual void setStandby() EXCLUDES_ThreadBase_Mutex = 0;
    virtual void setStandby_l() REQUIRES(mutex()) = 0;
    virtual bool waitForHalStart() EXCLUDES_ThreadBase_Mutex = 0;

    virtual bool hasFastMixer() const = 0;
    virtual FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const = 0;
    virtual const std::atomic<int64_t>& framesWritten() const = 0;

    virtual bool usesHwAvSync() const = 0;

    virtual void setTracksInternalMute(std::map<audio_port_handle_t, bool>* tracksInternalMute)
            EXCLUDES_ThreadBase_Mutex = 0;

    virtual status_t setPortsVolume(const std::vector<audio_port_handle_t> &portIds, float volume,
                                    bool muted) EXCLUDES_ThreadBase_Mutex = 0;
};

class IAfDirectOutputThread : public virtual IAfPlaybackThread {
public:
    virtual status_t selectPresentation(int presentationId, int programId) = 0;
};

class IAfDuplicatingThread : public virtual IAfPlaybackThread {
public:
    static sp<IAfDuplicatingThread> create(
            const sp<IAfThreadCallback>& afThreadCallback, IAfPlaybackThread* mainThread,
            audio_io_handle_t id, bool systemReady);

    virtual void addOutputTrack(IAfPlaybackThread* thread) EXCLUDES_ThreadBase_Mutex = 0;
    virtual uint32_t waitTimeMs() const = 0;
    virtual void removeOutputTrack(IAfPlaybackThread* thread) EXCLUDES_ThreadBase_Mutex = 0;
};

class IAfRecordThread : public virtual IAfThreadBase {
public:
    static sp<IAfRecordThread> create(
            const sp<IAfThreadCallback>& afThreadCallback, AudioStreamIn* input,
            audio_io_handle_t id, bool systemReady);

    virtual sp<IAfRecordTrack> createRecordTrack_l(
            const sp<Client>& client,
            const audio_attributes_t& attr,
            uint32_t* pSampleRate,
            audio_format_t format,
            audio_channel_mask_t channelMask,
            size_t* pFrameCount,
            audio_session_t sessionId,
            size_t* pNotificationFrameCount,
            pid_t creatorPid,
            const AttributionSourceState& attributionSource,
            audio_input_flags_t* flags,
            pid_t tid,
            status_t* status /*non-NULL*/,
            audio_port_handle_t portId,
            int32_t maxSharedAudioHistoryMs)
            REQUIRES(audio_utils::AudioFlinger_Mutex) EXCLUDES_ThreadBase_Mutex = 0;
    virtual void destroyTrack_l(const sp<IAfRecordTrack>& track) REQUIRES(mutex()) = 0;
    virtual void removeTrack_l(const sp<IAfRecordTrack>& track) REQUIRES(mutex()) = 0;

    virtual status_t start(
            IAfRecordTrack* recordTrack, AudioSystem::sync_event_t event,
            audio_session_t triggerSession) EXCLUDES_ThreadBase_Mutex = 0;

    // ask the thread to stop the specified track, and
    // return true if the caller should then do it's part of the stopping process
    virtual bool stop(IAfRecordTrack* recordTrack) EXCLUDES_ThreadBase_Mutex = 0;

    // NO_THREAD_SAFETY_ANALYSIS: consider atomics
    virtual AudioStreamIn* getInput() const = 0;
    virtual AudioStreamIn* clearInput() = 0;

    virtual status_t getActiveMicrophones(
            std::vector<media::MicrophoneInfoFw>* activeMicrophones)
            const EXCLUDES_ThreadBase_Mutex = 0;
    virtual status_t setPreferredMicrophoneDirection(audio_microphone_direction_t direction)
            EXCLUDES_ThreadBase_Mutex = 0;
    virtual status_t setPreferredMicrophoneFieldDimension(float zoom)
            EXCLUDES_ThreadBase_Mutex = 0;

    virtual void addPatchTrack(const sp<IAfPatchRecord>& record)
            EXCLUDES_ThreadBase_Mutex = 0;
    virtual void deletePatchTrack(const sp<IAfPatchRecord>& record)
            EXCLUDES_ThreadBase_Mutex = 0;
    virtual bool fastTrackAvailable() const = 0;
    virtual void setFastTrackAvailable(bool available) = 0;

    virtual void setRecordSilenced(audio_port_handle_t portId, bool silenced)
            EXCLUDES_ThreadBase_Mutex = 0;
    virtual bool hasFastCapture() const = 0;

    virtual void checkBtNrec() EXCLUDES_ThreadBase_Mutex = 0;
    virtual uint32_t getInputFramesLost() const EXCLUDES_ThreadBase_Mutex = 0;

    virtual status_t shareAudioHistory(
            const std::string& sharedAudioPackageName,
            audio_session_t sharedSessionId = AUDIO_SESSION_NONE,
            int64_t sharedAudioStartMs = -1) EXCLUDES_ThreadBase_Mutex = 0;
    virtual void resetAudioHistory_l() REQUIRES(mutex()) = 0;
};

class IAfMmapThread : public virtual IAfThreadBase {
public:
    // createIAudioTrackAdapter() is a static constructor which creates an
    // MmapStreamInterface AIDL interface adapter from the MmapThread object that
    // may be passed back to the client.
    //
    // Only one AIDL MmapStreamInterface interface adapter should be created per MmapThread.
    static sp<MmapStreamInterface> createMmapStreamInterfaceAdapter(
            const sp<IAfMmapThread>& mmapThread);

    virtual void configure(
            const audio_attributes_t* attr,
            audio_stream_type_t streamType,
            audio_session_t sessionId,
            const sp<MmapStreamCallback>& callback,
            const DeviceIdVector& deviceIds,
            audio_port_handle_t portId) EXCLUDES_ThreadBase_Mutex = 0;
    virtual void disconnect() EXCLUDES_ThreadBase_Mutex = 0;

    // MmapStreamInterface handling (see adapter)
    virtual status_t createMmapBuffer(
            int32_t minSizeFrames, struct audio_mmap_buffer_info* info)
            EXCLUDES_ThreadBase_Mutex = 0;
    virtual status_t getMmapPosition(struct audio_mmap_position* position) const
            EXCLUDES_ThreadBase_Mutex = 0;
    virtual status_t start(
            const AudioClient& client, const audio_attributes_t* attr,
            audio_port_handle_t* handle) EXCLUDES_ThreadBase_Mutex = 0;
    virtual status_t stop(audio_port_handle_t handle) EXCLUDES_ThreadBase_Mutex = 0;
    virtual status_t standby() EXCLUDES_ThreadBase_Mutex = 0;
    virtual status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) const
            EXCLUDES_ThreadBase_Mutex = 0;
    virtual status_t reportData(const void* buffer, size_t frameCount)
            EXCLUDES_ThreadBase_Mutex = 0;

    // TODO(b/291317898)  move to IAfThreadBase?
    virtual void invalidateTracks(std::set<audio_port_handle_t>& portIds)
            EXCLUDES_ThreadBase_Mutex = 0;

    // Sets the UID records silence - TODO(b/291317898)  move to IAfMmapCaptureThread
    virtual void setRecordSilenced(audio_port_handle_t portId, bool silenced)
            EXCLUDES_ThreadBase_Mutex = 0;

    virtual sp<IAfMmapPlaybackThread> asIAfMmapPlaybackThread() { return nullptr; }
    virtual sp<IAfMmapCaptureThread> asIAfMmapCaptureThread() { return nullptr; }
};

class IAfMmapPlaybackThread : public virtual IAfMmapThread, public virtual VolumeInterface {
public:
    static sp<IAfMmapPlaybackThread> create(
            const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id,
            AudioHwDevice* hwDev, AudioStreamOut* output, bool systemReady);

    virtual AudioStreamOut* clearOutput() EXCLUDES_ThreadBase_Mutex = 0;

    virtual status_t setPortsVolume(const std::vector<audio_port_handle_t>& portIds, float volume,
                                    bool muted) EXCLUDES_ThreadBase_Mutex = 0;
};

class IAfMmapCaptureThread : public virtual IAfMmapThread {
public:
    static sp<IAfMmapCaptureThread> create(
            const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id,
            AudioHwDevice* hwDev, AudioStreamIn* input, bool systemReady);

    virtual AudioStreamIn* clearInput() EXCLUDES_ThreadBase_Mutex = 0;
};

}  // namespace android
