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

Commit a4434157 authored by Andy Hung's avatar Andy Hung Committed by Android (Google) Code Review
Browse files

Merge "AudioSystem: Move AudioPolicyService fetch to ServiceHandler" into main

parents cf1c4256 88e7173f
Loading
Loading
Loading
Loading
+37 −53
Original line number Diff line number Diff line
@@ -71,16 +71,13 @@ record_config_callback AudioSystem::gRecordConfigCallback = NULL;
routing_callback AudioSystem::gRoutingCallback = NULL;
vol_range_init_req_callback AudioSystem::gVolRangeInitReqCallback = NULL;

std::mutex AudioSystem::gApsCallbackMutex;
std::mutex AudioSystem::gErrorCallbacksMutex;
std::set<audio_error_callback> AudioSystem::gAudioErrorCallbacks;

std::mutex AudioSystem::gSoundTriggerMutex;
sp<CaptureStateListenerImpl> AudioSystem::gSoundTriggerCaptureStateListener;

std::mutex AudioSystem::gAPSMutex;
sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;

// Sets the Binder for the AudioFlinger service, passed to this client process
// from the system server.
// This allows specific isolated processes to access the audio system. Currently used only for the
@@ -929,44 +926,35 @@ status_t AudioSystem::AudioFlingerClient::removeSupportedLatencyModesCallback(
    gVolRangeInitReqCallback = cb;
}

// establish binder interface to AudioPolicy service
sp<IAudioPolicyService> AudioSystem::get_audio_policy_service() {
    sp<IAudioPolicyService> ap;
    sp<AudioPolicyServiceClient> apc;
    {
        std::lock_guard _l(gAPSMutex);
        if (gAudioPolicyService == 0) {
            sp<IServiceManager> sm = defaultServiceManager();
            sp<IBinder> binder = sm->waitForService(String16("media.audio_policy"));
            if (binder == nullptr) {
                return nullptr;
            }
            if (gAudioPolicyServiceClient == NULL) {
                gAudioPolicyServiceClient = new AudioPolicyServiceClient();
            }
            binder->linkToDeath(gAudioPolicyServiceClient);
            gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
            LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
            apc = gAudioPolicyServiceClient;
            // Make sure callbacks can be received by gAudioPolicyServiceClient
            ProcessState::self()->startThreadPool();
        }
        ap = gAudioPolicyService;
    }
    if (apc != 0) {
        int64_t token = IPCThreadState::self()->clearCallingIdentity();
struct AudioPolicyTraits {
    static void onServiceCreate(const sp<IAudioPolicyService>& ap,
            const sp<AudioSystem::AudioPolicyServiceClient>& apc) {
        const int64_t token = IPCThreadState::self()->clearCallingIdentity();
        ap->registerClient(apc);
        ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
        ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
        IPCThreadState::self()->restoreCallingIdentity(token);
    }

    return ap;
    static void onClearService(const sp<AudioSystem::AudioPolicyServiceClient>&) {}

    static constexpr const char *SERVICE_NAME = "media.audio_policy";
};

[[clang::no_destroy]] static constinit ServiceHandler<IAudioPolicyService,
        AudioSystem::AudioPolicyServiceClient, IAudioPolicyService,
        AudioPolicyTraits> gAudioPolicyServiceHandler;

status_t AudioSystem::setLocalAudioPolicyService(const sp<IAudioPolicyService>& aps) {
    return gAudioPolicyServiceHandler.setLocalService(aps);
}

sp<IAudioPolicyService> AudioSystem::get_audio_policy_service() {
    return gAudioPolicyServiceHandler.getService();
}

void AudioSystem::clearAudioPolicyService() {
    std::lock_guard _l(gAPSMutex);
    gAudioPolicyService.clear();
    gAudioPolicyServiceHandler.clearService();
}

// ---------------------------------------------------------------------------
@@ -1730,12 +1718,11 @@ status_t AudioSystem::setAudioPortConfig(const struct audio_port_config* config)
status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback) {
    const sp<IAudioPolicyService> aps = get_audio_policy_service();
    if (aps == 0) return PERMISSION_DENIED;
    const auto apc = gAudioPolicyServiceHandler.getClient();
    if (apc == nullptr) return NO_INIT;

    std::lock_guard _l(gAPSMutex);
    if (gAudioPolicyServiceClient == 0) {
        return NO_INIT;
    }
    int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
    std::lock_guard _l(gApsCallbackMutex);
    const int ret = apc->addAudioPortCallback(callback);
    if (ret == 1) {
        aps->setAudioPortCallbacksEnabled(true);
    }
@@ -1746,12 +1733,11 @@ status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback
status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback) {
    const sp<IAudioPolicyService> aps = get_audio_policy_service();
    if (aps == 0) return PERMISSION_DENIED;
    const auto apc = gAudioPolicyServiceHandler.getClient();
    if (apc == nullptr) return NO_INIT;

    std::lock_guard _l(gAPSMutex);
    if (gAudioPolicyServiceClient == 0) {
        return NO_INIT;
    }
    int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
    std::lock_guard _l(gApsCallbackMutex);
    const int ret = apc->removeAudioPortCallback(callback);
    if (ret == 0) {
        aps->setAudioPortCallbacksEnabled(false);
    }
@@ -1761,12 +1747,11 @@ status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callb
status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
    const sp<IAudioPolicyService> aps = get_audio_policy_service();
    if (aps == 0) return PERMISSION_DENIED;
    const auto apc = gAudioPolicyServiceHandler.getClient();
    if (apc == nullptr) return NO_INIT;

    std::lock_guard _l(gAPSMutex);
    if (gAudioPolicyServiceClient == 0) {
        return NO_INIT;
    }
    int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback);
    std::lock_guard _l(gApsCallbackMutex);
    const int ret = apc->addAudioVolumeGroupCallback(callback);
    if (ret == 1) {
        aps->setAudioVolumeGroupCallbacksEnabled(true);
    }
@@ -1776,12 +1761,11 @@ status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallb
status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) {
    const sp<IAudioPolicyService> aps = get_audio_policy_service();
    if (aps == 0) return PERMISSION_DENIED;
    const auto apc = gAudioPolicyServiceHandler.getClient();
    if (apc == nullptr) return NO_INIT;

    std::lock_guard _l(gAPSMutex);
    if (gAudioPolicyServiceClient == 0) {
        return NO_INIT;
    }
    int ret = gAudioPolicyServiceClient->removeAudioVolumeGroupCallback(callback);
    std::lock_guard _l(gApsCallbackMutex);
    const int ret = apc->removeAudioVolumeGroupCallback(callback);
    if (ret == 0) {
        aps->setAudioVolumeGroupCallbacksEnabled(false);
    }
+6 −6
Original line number Diff line number Diff line
@@ -411,6 +411,11 @@ public:
    // 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();

@@ -909,6 +914,7 @@ public:
    static routing_callback gRoutingCallback GUARDED_BY(gMutex);
    static vol_range_init_req_callback gVolRangeInitReqCallback GUARDED_BY(gMutex);

    [[clang::no_destroy]] static std::mutex gApsCallbackMutex;
    [[clang::no_destroy]] static std::mutex gErrorCallbacksMutex;
    [[clang::no_destroy]] static std::set<audio_error_callback> gAudioErrorCallbacks
            GUARDED_BY(gErrorCallbacksMutex);
@@ -916,12 +922,6 @@ public:
    [[clang::no_destroy]] static std::mutex gSoundTriggerMutex;
    [[clang::no_destroy]] static sp<CaptureStateListenerImpl> gSoundTriggerCaptureStateListener
            GUARDED_BY(gSoundTriggerMutex);

    [[clang::no_destroy]] static std::mutex gAPSMutex;
    [[clang::no_destroy]] static sp<media::IAudioPolicyService> gAudioPolicyService
            GUARDED_BY(gAPSMutex);
    [[clang::no_destroy]] static sp<AudioPolicyServiceClient> gAudioPolicyServiceClient
            GUARDED_BY(gAPSMutex);
};

}  // namespace android