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

Commit ce3d1106 authored by Andy Hung's avatar Andy Hung
Browse files

AudioSystem: new service cache reduces ANRs and improves performance.

Reduced ANRs:
1) Do not block on AudioFlinger or AudioPolicy for more than 10 seconds
to prevent ANRs.  Audioserver failure is returned to the AudioService which
can be ignored as the ground truth is stored in AudioService and
audioserver is currently dead.

2) Always check for audio services upon each thread request to avoid
ServiceManager notification failure when ThreadPool is locked up.

Improved performance:
1) Trigger actions based on binder driver death notifications and
ServiceManager service notifications, saves about 300ms on Track restore.

2) Single notification handling is shared among all clients.

Flag: EXEMPT bugfix
Test: instrumented audioserver delay, audio setting responsiveness
Test: instrumented audioserver delay, avoid ANR (see bug).
Test: hotword service works
Test: atest powerstats_collector_tests
Bug: 372348587
Change-Id: Ifd5b5457f4ca181ed30b6aea7ed4effad7aebef5
parent d9567a3f
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -37,12 +37,16 @@ cc_fuzz {
    ],
    shared_libs: [
        "com.android.media.aaudio-aconfig-cc",
        "libaconfig_storage_read_api_cc",
        "libaudio_aidl_conversion_common_cpp",
        "libaudioclient_aidl_conversion",
        "libaudiomanager",
        "libaudiopolicy",
        "libbinder",
        "libbinder_ndk",
        "libmediautils",
        "libutils",
        "server_configurable_flags",
    ],
    static_libs: [
        "aaudio-aidl-cpp",
+0 −5
Original line number Diff line number Diff line
@@ -1577,11 +1577,6 @@ status_t AudioRecord::restoreRecord_l(const char *from)
    const int INITIAL_RETRIES = 3;
    int retries = INITIAL_RETRIES;
retry:
    if (retries < INITIAL_RETRIES) {
        // refresh the audio configuration cache in this process to make sure we get new
        // input parameters and new IAudioRecord in createRecord_l()
        AudioSystem::clearAudioConfigCache();
    }
    mFlags = mOrigFlags;

    // if the new IAudioRecord is created, createRecord_l() will modify the
+444 −383

File changed.

Preview size limit exceeded, changes collapsed.

+0 −8
Original line number Diff line number Diff line
@@ -2875,10 +2875,6 @@ status_t AudioTrack::restoreTrack_l(const char *from, bool forceRestore)
            __func__, mPortId, isOffloadedOrDirect_l() ? "Offloaded or Direct" : "PCM", from);
    ++mSequence;

    // refresh the audio configuration cache in this process to make sure we get new
    // output parameters and new IAudioFlinger in createTrack_l()
    AudioSystem::clearAudioConfigCache();

    if (!forceRestore &&
        (isOffloadedOrDirect_l() || mDoNotReconnect)) {
        // FIXME re-creation of offloaded and direct tracks is not yet implemented;
@@ -2911,10 +2907,6 @@ status_t AudioTrack::restoreTrack_l(const char *from, bool forceRestore)
    const int INITIAL_RETRIES = 3;
    int retries = INITIAL_RETRIES;
retry:
    if (retries < INITIAL_RETRIES) {
        // See the comment for clearAudioConfigCache at the start of the function.
        AudioSystem::clearAudioConfigCache();
    }
    mFlags = mOrigFlags;

    // If a new IAudioTrack is successfully created, createTrack_l() will modify the
+5 −13
Original line number Diff line number Diff line
@@ -103,6 +103,7 @@ class AudioSystem
    template <typename ServiceInterface, typename Client, typename AidlInterface,
            typename ServiceTraits>
    friend class ServiceHandler;
    friend class AudioFlingerServiceTraits;

public:

@@ -426,17 +427,12 @@ public:
    static status_t setEffectEnabled(int id, bool enabled);
    static status_t moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io);

    // clear stream to output mapping cache (gStreamOutputMap)
    // and output configuration cache (gOutputs)
    static void clearAudioConfigCache();

    // Sets a local AudioPolicyService interface to be used by AudioSystem.
    // This is used by audioserver main() to allow client object initialization
    // before exposing any interfaces to ServiceManager.
    static status_t setLocalAudioPolicyService(const sp<media::IAudioPolicyService>& aps);

    static sp<media::IAudioPolicyService> get_audio_policy_service();
    static void clearAudioPolicyService();

    // helpers for android.media.AudioManager.getProperty(), see description there for meaning
    static uint32_t getPrimaryOutputSamplingRate();
@@ -813,7 +809,7 @@ public:
            media::audio::common::AudioMMapPolicyType policyType, audio_devices_t device,
            media::audio::common::AudioMMapPolicyInfo *policyInfo);

    class AudioFlingerClient: public IBinder::DeathRecipient, public media::BnAudioFlingerClient
    class AudioFlingerClient: public media::BnAudioFlingerClient
    {
    public:
        AudioFlingerClient() = default;
@@ -823,9 +819,6 @@ public:
                audio_channel_mask_t channelMask, size_t* buffSize) EXCLUDES(mMutex);
        sp<AudioIoDescriptor> getIoDescriptor(audio_io_handle_t ioHandle) EXCLUDES(mMutex);

        // DeathRecipient
        void binderDied(const wp<IBinder>& who) final;

        // IAudioFlingerClient

        // indicate a change in the configuration of an output or input: keeps the cached
@@ -870,8 +863,7 @@ public:
        sp<AudioIoDescriptor> getIoDescriptor_l(audio_io_handle_t ioHandle) REQUIRES(mMutex);
    };

    class AudioPolicyServiceClient: public IBinder::DeathRecipient,
                                    public media::BnAudioPolicyServiceClient {
    class AudioPolicyServiceClient: public media::BnAudioPolicyServiceClient {
    public:
        AudioPolicyServiceClient() = default;

@@ -895,8 +887,7 @@ public:
            return !mAudioVolumeGroupCallbacks.empty();
        }

        // DeathRecipient
        void binderDied(const wp<IBinder>& who) final;
        void onServiceDied();

        // IAudioPolicyServiceClient
        binder::Status onAudioVolumeGroupChanged(int32_t group, int32_t flags) override;
@@ -926,6 +917,7 @@ public:

    static audio_io_handle_t getOutput(audio_stream_type_t stream);
    static sp<AudioFlingerClient> getAudioFlingerClient();
    static sp<AudioPolicyServiceClient> getAudioPolicyClient();
    static sp<AudioIoDescriptor> getIoDescriptor(audio_io_handle_t ioHandle);

    // Invokes all registered error callbacks with the given error code.