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

Commit d387cf7c authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge changes Ie755c7c2,I2259105e

* changes:
  audioserver: Bypass AIDL translation for in-proc AudioFlinger calls
  audioserver: Initialize services before making public.
parents d8371de6 938ef434
Loading
Loading
Loading
Loading
+38 −9
Original line number Diff line number Diff line
@@ -50,6 +50,8 @@ using android::media::audio::common::AudioMMapPolicyType;

int main(int argc __unused, char **argv)
{
    ALOGD("%s: starting", __func__);
    const auto startTime = std::chrono::steady_clock::now();
    // TODO: update with refined parameters
    limitProcessMemory(
        "audio.maxmem", /* "ro.audio.maxmem", property that defines limit */
@@ -144,11 +146,36 @@ int main(int argc __unused, char **argv)
            setpgid(0, 0);                      // but if I die first, don't kill my parent
        }
        android::hardware::configureRpcThreadpool(4, false /*callerWillJoin*/);
        sp<ProcessState> proc(ProcessState::self());

        // Ensure threads for possible callbacks.  Note that get_audio_flinger() does
        // this automatically when called from AudioPolicy, but we do this anyways here.
        ProcessState::self()->startThreadPool();

        // Instantiating AudioFlinger (making it public, e.g. through ::initialize())
        // and then instantiating AudioPolicy (and making it public)
        // leads to situations where AudioFlinger is accessed remotely before
        // AudioPolicy is initialized.  Not only might this
        // cause inaccurate results, but if AudioPolicy has slow audio HAL
        // initialization, it can cause a TimeCheck abort to occur on an AudioFlinger
        // call which tries to access AudioPolicy.
        //
        // We create AudioFlinger and AudioPolicy locally then make it public to ServiceManager.
        // This requires both AudioFlinger and AudioPolicy to be in-proc.
        //
        const auto af = sp<AudioFlinger>::make();
        const auto afAdapter = sp<AudioFlingerServerAdapter>::make(af);
        ALOGD("%s: AudioFlinger created", __func__);
        ALOGW_IF(AudioSystem::setLocalAudioFlinger(af) != OK,
                "%s: AudioSystem already has an AudioFlinger instance!", __func__);
        const auto aps = sp<AudioPolicyService>::make();
        ALOGD("%s: AudioPolicy created", __func__);

        // Add AudioFlinger and AudioPolicy to ServiceManager.
        sp<IServiceManager> sm = defaultServiceManager();
        ALOGI("ServiceManager: %p", sm.get());
        AudioFlinger::instantiate();
        AudioPolicyService::instantiate();
        sm->addService(String16(IAudioFlinger::DEFAULT_SERVICE_NAME), afAdapter,
                false /* allowIsolated */, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT);
        sm->addService(String16(AudioPolicyService::getServiceName()), aps,
                false /* allowIsolated */, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT);

        // AAudioService should only be used in OC-MR1 and later.
        // And only enable the AAudioService if the system MMAP policy explicitly allows it.
@@ -156,7 +183,6 @@ int main(int argc __unused, char **argv)
        // If we cannot get audio flinger here, there must be some serious problems. In that case,
        // attempting to call audio flinger on a null pointer could make the process crash
        // and attract attentions.
        sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
        std::vector<AudioMMapPolicyInfo> policyInfos;
        status_t status = af->getMmapPolicyInfos(
                AudioMMapPolicyType::DEFAULT, &policyInfos);
@@ -169,11 +195,14 @@ int main(int argc __unused, char **argv)
            })) {
            AAudioService::instantiate();
        } else {
            ALOGD("Do not init aaudio service, status %d, policy info size %zu",
                  status, policyInfos.size());
            ALOGD("%s: Do not init aaudio service, status %d, policy info size %zu",
                  __func__, status, policyInfos.size());
        }

        ProcessState::self()->startThreadPool();
        const auto endTime = std::chrono::steady_clock::now();
        using FloatMillis = std::chrono::duration<float, std::milli>;
        const float timeTaken = std::chrono::duration_cast<FloatMillis>(
                endTime - startTime).count();
        ALOGI("%s: initialization done in %.3f ms, joining thread pool", __func__, timeTaken);
        IPCThreadState::self()->joinThreadPool();
    }
}
+33 −20
Original line number Diff line number Diff line
@@ -81,7 +81,7 @@ sp<CaptureStateListenerImpl> gSoundTriggerCaptureStateListener = nullptr;
// Binder for the AudioFlinger service that's passed to this client process from the system server.
// This allows specific isolated processes to access the audio system. Currently used only for the
// HotwordDetectionService.
sp<IBinder> gAudioFlingerBinder = nullptr;
static sp<IBinder> gAudioFlingerBinder = nullptr;

void AudioSystem::setAudioFlingerBinder(const sp<IBinder>& audioFlinger) {
    if (audioFlinger->getInterfaceDescriptor() != media::IAudioFlingerService::descriptor) {
@@ -97,6 +97,15 @@ void AudioSystem::setAudioFlingerBinder(const sp<IBinder>& audioFlinger) {
    gAudioFlingerBinder = audioFlinger;
}

static sp<IAudioFlinger> gLocalAudioFlinger; // set if we are local.

status_t AudioSystem::setLocalAudioFlinger(const sp<IAudioFlinger>& af) {
    Mutex::Autolock _l(gLock);
    if (gAudioFlinger != nullptr) return INVALID_OPERATION;
    gLocalAudioFlinger = af;
    return OK;
}

// establish binder interface to AudioFlinger service
const sp<IAudioFlinger> AudioSystem::get_audio_flinger() {
    sp<IAudioFlinger> af;
@@ -104,7 +113,19 @@ const sp<IAudioFlinger> AudioSystem::get_audio_flinger() {
    bool reportNoError = false;
    {
        Mutex::Autolock _l(gLock);
        if (gAudioFlinger == 0) {
        if (gAudioFlinger != nullptr) {
            return gAudioFlinger;
        }

        if (gAudioFlingerClient == nullptr) {
            gAudioFlingerClient = sp<AudioFlingerClient>::make();
        } else {
            reportNoError = true;
        }

        if (gLocalAudioFlinger != nullptr) {
            gAudioFlinger = gLocalAudioFlinger;
        } else {
            sp<IBinder> binder;
            if (gAudioFlingerBinder != nullptr) {
                binder = gAudioFlingerBinder;
@@ -112,32 +133,24 @@ const sp<IAudioFlinger> AudioSystem::get_audio_flinger() {
                sp<IServiceManager> sm = defaultServiceManager();
                do {
                    binder = sm->getService(String16(IAudioFlinger::DEFAULT_SERVICE_NAME));
                    if (binder != 0)
                        break;
                    if (binder != nullptr) break;
                    ALOGW("AudioFlinger not published, waiting...");
                    usleep(500000); // 0.5 s
                } while (true);
            }
            if (gAudioFlingerClient == NULL) {
                gAudioFlingerClient = new AudioFlingerClient();
            } else {
                reportNoError = true;
            }
            binder->linkToDeath(gAudioFlingerClient);
            gAudioFlinger = new AudioFlingerClientAdapter(
                    interface_cast<media::IAudioFlingerService>(binder));
            LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
            const auto afs = interface_cast<media::IAudioFlingerService>(binder);
            LOG_ALWAYS_FATAL_IF(afs == nullptr);
            gAudioFlinger = sp<AudioFlingerClientAdapter>::make(afs);
        }
        afc = gAudioFlingerClient;
        af = gAudioFlinger;
        // Make sure callbacks can be received by gAudioFlingerClient
        ProcessState::self()->startThreadPool();
    }
        af = gAudioFlinger;
    }
    if (afc != 0) {
        int64_t token = IPCThreadState::self()->clearCallingIdentity();
    const int64_t token = IPCThreadState::self()->clearCallingIdentity();
    af->registerClient(afc);
    IPCThreadState::self()->restoreCallingIdentity(token);
    }
    if (reportNoError) reportError(NO_ERROR);
    return af;
}
+4 −0
Original line number Diff line number Diff line
@@ -166,6 +166,10 @@ public:
    // HotwordDetectionService.
    static void setAudioFlingerBinder(const sp<IBinder>& audioFlinger);

    // Sets a local AudioFlinger interface to be used by AudioSystem.
    // This is used by audioserver main() to avoid binder AIDL translation.
    static status_t setLocalAudioFlinger(const sp<IAudioFlinger>& af);

    // helper function to obtain AudioFlinger service handle
    static const sp<IAudioFlinger> get_audio_flinger();

+1 −0
Original line number Diff line number Diff line
@@ -131,6 +131,7 @@ using android::content::AttributionSourceState;

class AudioFlinger : public AudioFlingerServerAdapter::Delegate
{
    friend class sp<AudioFlinger>;
public:
    static void instantiate() ANDROID_API;

+1 −1
Original line number Diff line number Diff line
@@ -69,7 +69,7 @@ class AudioPolicyService :
    public IBinder::DeathRecipient,
    public SpatializerPolicyCallback
{
    friend class BinderService<AudioPolicyService>;
    friend class sp<AudioPolicyService>;

public:
    // for BinderService