Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 92653e55 authored by Andy Hung's avatar Andy Hung
Browse files

AudioFlinger: clean up ctor initialization

Test: atest AudioTrackTest AudioRecordTest
Test: atest AAudioTests AudioTrackOffloadTest
Test: atest AudioPlaybackCaptureTest
Test: Camera YouTube
Bug: 298529417
Merged-In: Idd3be2e20eb90dff7f794aad096b286b2cd7d938
Change-Id: Idd3be2e20eb90dff7f794aad096b286b2cd7d938
parent b4cef76c
Loading
Loading
Loading
Loading
+8 −29
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@

#include "Configuration.h"
#include "AudioFlinger.h"
#include "EffectConfiguration.h"

//#define BUFLOG_NDEBUG 0
#include <afutils/BufLog.h>
@@ -232,23 +231,6 @@ void AudioFlinger::instantiate() {
}

AudioFlinger::AudioFlinger()
    : mMediaLogNotifier(new AudioFlinger::MediaLogNotifier()),
      mPrimaryHardwareDev(NULL),
      mAudioHwDevs(NULL),
      mHardwareStatus(AUDIO_HW_IDLE),
      mMasterVolume(1.0f),
      mMasterMute(false),
      // mNextUniqueId(AUDIO_UNIQUE_ID_USE_MAX),
      mMode(AUDIO_MODE_INVALID),
      mBtNrecIsOff(false),
      mIsLowRamDevice(true),
      mIsDeviceTypeKnown(false),
      mTotalMemory(0),
      mClientSharedHeapSize(kMinimumClientSharedHeapSizeBytes),
      mGlobalEffectEnableTime(0),
      mPatchCommandThread(sp<PatchCommandThread>::make()),
      mSystemReady(false),
      mBluetoothLatencyModesEnabled(true)
{
    // Move the audio session unique ID generator start base as time passes to limit risk of
    // generating the same ID again after an audioserver restart.
@@ -285,9 +267,6 @@ AudioFlinger::AudioFlinger()
    // in bad state, reset the state upon service start.
    BatteryNotifier::getInstance().noteResetAudio();

    mDevicesFactoryHal = DevicesFactoryHalInterface::create();
    mEffectsFactoryHal = audioflinger::EffectConfiguration::getEffectsFactoryHal();

    mMediaLogNotifier->run("MediaLogNotifier");
    std::vector<pid_t> halPids;
    mDevicesFactoryHal->getHalPids(&halPids);
@@ -1329,7 +1308,7 @@ status_t AudioFlinger::setMode(audio_mode_t mode)
        if (mPrimaryHardwareDev == nullptr) {
            return INVALID_OPERATION;
        }
        sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
        sp<DeviceHalInterface> dev = mPrimaryHardwareDev.load()->hwDevice();
        mHardwareStatus = AUDIO_HW_SET_MODE;
        ret = dev->setMode(mode);
        mHardwareStatus = AUDIO_HW_IDLE;
@@ -1366,7 +1345,7 @@ status_t AudioFlinger::setMicMute(bool state)
    if (mPrimaryHardwareDev == nullptr) {
        return INVALID_OPERATION;
    }
    sp<DeviceHalInterface> primaryDev = mPrimaryHardwareDev->hwDevice();
    sp<DeviceHalInterface> primaryDev = mPrimaryHardwareDev.load()->hwDevice();
    if (primaryDev == nullptr) {
        ALOGW("%s: no primary HAL device", __func__);
        return INVALID_OPERATION;
@@ -1394,7 +1373,7 @@ bool AudioFlinger::getMicMute() const
    if (mPrimaryHardwareDev == nullptr) {
        return false;
    }
    sp<DeviceHalInterface> primaryDev = mPrimaryHardwareDev->hwDevice();
    sp<DeviceHalInterface> primaryDev = mPrimaryHardwareDev.load()->hwDevice();
    if (primaryDev == nullptr) {
        ALOGW("%s: no primary HAL device", __func__);
        return false;
@@ -1912,7 +1891,7 @@ size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t form
    }
    mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;

    sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
    sp<DeviceHalInterface> dev = mPrimaryHardwareDev.load()->hwDevice();

    std::vector<audio_channel_mask_t> channelMasks = {channelMask};
    if (channelMask != AUDIO_CHANNEL_IN_MONO) {
@@ -2003,7 +1982,7 @@ status_t AudioFlinger::setVoiceVolume(float value)
    if (mPrimaryHardwareDev == nullptr) {
        return INVALID_OPERATION;
    }
    sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
    sp<DeviceHalInterface> dev = mPrimaryHardwareDev.load()->hwDevice();
    mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
    ret = dev->setVoiceVolume(value);
    mHardwareStatus = AUDIO_HW_IDLE;
@@ -2558,7 +2537,7 @@ AudioHwDevice* AudioFlinger::loadHwModule_l(const char *name)
    if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) {
        mPrimaryHardwareDev = audioDevice;
        mHardwareStatus = AUDIO_HW_SET_MODE;
        mPrimaryHardwareDev->hwDevice()->setMode(mMode);
        mPrimaryHardwareDev.load()->hwDevice()->setMode(mMode);
        mHardwareStatus = AUDIO_HW_IDLE;
    }

@@ -2692,7 +2671,7 @@ audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId
        if (mPrimaryHardwareDev == nullptr) {
            return AUDIO_HW_SYNC_INVALID;
        }
        dev = mPrimaryHardwareDev->hwDevice();
        dev = mPrimaryHardwareDev.load()->hwDevice();
    }
    if (dev == nullptr) {
        return AUDIO_HW_SYNC_INVALID;
@@ -2964,7 +2943,7 @@ status_t AudioFlinger::openOutput(const media::OpenOutputRequest& request,
                mPrimaryHardwareDev = playbackThread->getOutput()->audioHwDev;

                mHardwareStatus = AUDIO_HW_SET_MODE;
                mPrimaryHardwareDev->hwDevice()->setMode(mMode);
                mPrimaryHardwareDev.load()->hwDevice()->setMode(mMode);
                mHardwareStatus = AUDIO_HW_IDLE;
            }
        } else {
+25 −25
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
// Classes and interfaces directly used.
#include "Client.h"
#include "DeviceEffectManager.h"
#include "EffectConfiguration.h"
#include "IAfEffect.h"
#include "IAfPatchPanel.h"
#include "IAfThread.h"
@@ -459,7 +460,7 @@ private:
        static const int kPostTriggerSleepPeriod = 1000000;
    };

    const sp<MediaLogNotifier> mMediaLogNotifier;
    const sp<MediaLogNotifier> mMediaLogNotifier = sp<MediaLogNotifier>::make();

    // Find io handle by session id.
    // Preference is given to an io handle with a matching effect chain to session id.
@@ -565,13 +566,12 @@ private:
                // NOTE: If both mLock and mHardwareLock mutexes must be held,
                // always take mLock before mHardwareLock

                // guarded by mHardwareLock
                AudioHwDevice* mPrimaryHardwareDev;
                DefaultKeyedVector<audio_module_handle_t, AudioHwDevice*>  mAudioHwDevs;
    std::atomic<AudioHwDevice*> mPrimaryHardwareDev = nullptr;
    DefaultKeyedVector<audio_module_handle_t, AudioHwDevice*> mAudioHwDevs{nullptr /* defValue */};

                // These two fields are immutable after onFirstRef(), so no lock needed to access
                sp<DevicesFactoryHalInterface> mDevicesFactoryHal;
                sp<DevicesFactoryHalCallback> mDevicesFactoryHalCallback;
     const sp<DevicesFactoryHalInterface> mDevicesFactoryHal =
             DevicesFactoryHalInterface::create();
     /* const */ sp<DevicesFactoryHalCallback> mDevicesFactoryHalCallback;  // set onFirstRef().

    // for dump, indicates which hardware operation is currently in progress (but not stream ops)
    enum hardware_call_state {
@@ -601,15 +601,14 @@ private:
        AUDIO_HW_SET_SIMULATE_CONNECTIONS, // setSimulateDeviceConnections
    };

    mutable     hardware_call_state                 mHardwareStatus;    // for dump only

    mutable hardware_call_state mHardwareStatus = AUDIO_HW_IDLE;  // for dump only

    DefaultKeyedVector<audio_io_handle_t, sp<IAfPlaybackThread>> mPlaybackThreads;
                stream_type_t                       mStreamTypes[AUDIO_STREAM_CNT];

                // member variables below are protected by mLock
                float                               mMasterVolume;
                bool                                mMasterMute;
    float mMasterVolume = 1.f;
    bool mMasterMute = false;
    float mMasterBalance = 0.f;
                // end of variables protected by mLock

@@ -619,10 +618,10 @@ private:
                DefaultKeyedVector< pid_t, sp<NotificationClient> >    mNotificationClients;

                // updated by atomic_fetch_add_explicit
                volatile atomic_uint_fast32_t       mNextUniqueIds[AUDIO_UNIQUE_ID_USE_MAX];
    volatile atomic_uint_fast32_t mNextUniqueIds[AUDIO_UNIQUE_ID_USE_MAX];  // ctor init

                audio_mode_t                        mMode;
                std::atomic_bool                    mBtNrecIsOff;
    std::atomic<audio_mode_t> mMode = AUDIO_MODE_INVALID;
    std::atomic<bool> mBtNrecIsOff = false;

                // protected by mLock
                Vector<AudioSessionRef*> mAudioSessionRefs;
@@ -661,24 +660,25 @@ private:
    // though the variables are updated with mLock.
    size_t getClientSharedHeapSize() const;

    std::atomic<bool> mIsLowRamDevice;
    bool    mIsDeviceTypeKnown;
    int64_t mTotalMemory;
    std::atomic<size_t> mClientSharedHeapSize;
    std::atomic<bool> mIsLowRamDevice = true;
    bool mIsDeviceTypeKnown = false;
    int64_t mTotalMemory = 0;
    std::atomic<size_t> mClientSharedHeapSize = kMinimumClientSharedHeapSizeBytes;
    static constexpr size_t kMinimumClientSharedHeapSizeBytes = 1024 * 1024; // 1MB

    nsecs_t mGlobalEffectEnableTime;  // when a global effect was last enabled
    nsecs_t mGlobalEffectEnableTime = 0;  // when a global effect was last enabled

    /* const */ sp<IAfPatchPanel> mPatchPanel;

    sp<EffectsFactoryHalInterface> mEffectsFactoryHal;
    const sp<EffectsFactoryHalInterface> mEffectsFactoryHal =
            audioflinger::EffectConfiguration::getEffectsFactoryHal();

    const sp<PatchCommandThread> mPatchCommandThread;
    const sp<PatchCommandThread> mPatchCommandThread = sp<PatchCommandThread>::make();
    /* const */ sp<DeviceEffectManager> mDeviceEffectManager;  // set onFirstRef
    /* const */ sp<MelReporter> mMelReporter;  // set onFirstRef

    bool       mSystemReady;
    std::atomic_bool mAudioPolicyReady{};
    bool mSystemReady = false;
    std::atomic<bool> mAudioPolicyReady = false;

    mediautils::UidInfo mUidInfo;

@@ -699,7 +699,7 @@ private:
    mediautils::atomic_sp<IAudioManager>       mAudioManager;

    // Bluetooth Variable latency control logic is enabled or disabled
    std::atomic_bool mBluetoothLatencyModesEnabled;
    std::atomic<bool> mBluetoothLatencyModesEnabled = true;
};

// ----------------------------------------------------------------------------