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

Commit d6907003 authored by Felix Oghina's avatar Felix Oghina Committed by Automerger Merge Worker
Browse files

Merge "[hotword] init audioflinger outside of lock" into udc-dev am: 1246a1f6

parents 47ec949c 1246a1f6
Loading
Loading
Loading
Loading
+22 −11
Original line number Diff line number Diff line
@@ -154,7 +154,8 @@ final class HotwordDetectionConnection {
    private int mRestartCount = 0;
    @NonNull private ServiceConnection mRemoteHotwordDetectionService;
    @NonNull private ServiceConnection mRemoteVisualQueryDetectionService;
    private IBinder mAudioFlinger;
    @GuardedBy("mLock")
    @Nullable private IBinder mAudioFlinger;
    @GuardedBy("mLock")
    private boolean mDebugHotwordLogging = false;

@@ -194,7 +195,7 @@ final class HotwordDetectionConnection {
                new Intent(VisualQueryDetectionService.SERVICE_INTERFACE);
        visualQueryDetectionServiceIntent.setComponent(mVisualQueryDetectionComponentName);

        initAudioFlingerLocked();
        initAudioFlinger();

        mHotwordDetectionServiceConnectionFactory =
                new ServiceConnectionFactory(hotwordDetectionServiceIntent,
@@ -227,31 +228,41 @@ final class HotwordDetectionConnection {
        }
    }

    private void initAudioFlingerLocked() {
    private void initAudioFlinger() {
        if (DEBUG) {
            Slog.d(TAG, "initAudioFlingerLocked");
            Slog.d(TAG, "initAudioFlinger");
        }
        mAudioFlinger = ServiceManager.waitForService("media.audio_flinger");
        if (mAudioFlinger == null) {
        final IBinder audioFlinger = ServiceManager.waitForService("media.audio_flinger");
        if (audioFlinger == null) {
            setAudioFlinger(null);
            throw new IllegalStateException("Service media.audio_flinger wasn't found.");
        }
        if (DEBUG) {
            Slog.d(TAG, "Obtained audio_flinger binder.");
        }
        try {
            mAudioFlinger.linkToDeath(mAudioServerDeathRecipient, /* flags= */ 0);
            audioFlinger.linkToDeath(mAudioServerDeathRecipient, /* flags= */ 0);
        } catch (RemoteException e) {
            Slog.w(TAG, "Audio server died before we registered a DeathRecipient; "
                    + "retrying init.", e);
            initAudioFlingerLocked();
            initAudioFlinger();
            return;
        }

        setAudioFlinger(audioFlinger);
    }

    private void setAudioFlinger(@Nullable IBinder audioFlinger) {
        synchronized (mLock) {
            mAudioFlinger = audioFlinger;
        }
    }

    private void audioServerDied() {
        Slog.w(TAG, "Audio server died; restarting the HotwordDetectionService.");
        synchronized (mLock) {
        // TODO: Check if this needs to be scheduled on a different thread.
            initAudioFlingerLocked();
        initAudioFlinger();
        synchronized (mLock) {
            // We restart the process instead of simply sending over the new binder, to avoid race
            // conditions with audio reading in the service.
            restartProcessLocked();