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

Commit 06426e48 authored by Vlad Popa's avatar Vlad Popa Committed by Android (Google) Code Review
Browse files

Merge "CTA2075: Add loudness dumpsys logging" into main

parents 4277284b 28bb3e25
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ package android.media;
 *
 * {@hide}
 */
@JavaDerive(equals = true)
@JavaDerive(equals = true, toString = true)
parcelable LoudnessCodecInfo {
    /** Supported codec metadata types for loudness updates. */
    @Backing(type="int")
+6 −0
Original line number Diff line number Diff line
@@ -11372,6 +11372,8 @@ public class AudioService extends IAudioService.Stub
    static final int LOG_NB_EVENTS_SPATIAL = 30;
    static final int LOG_NB_EVENTS_SOUND_DOSE = 30;
    static final int LOG_NB_EVENTS_LOUDNESS_CODEC = 30;
    static final EventLogger
            sLifecycleLogger = new EventLogger(LOG_NB_EVENTS_LIFECYCLE,
            "audio services lifecycle");
@@ -11571,6 +11573,10 @@ public class AudioService extends IAudioService.Stub
        mSpatializerHelper.dump(pw);
        sSpatialLogger.dump(pw);
        pw.println("\n");
        pw.println("\nLoudness alignment:");
        mLoudnessCodecHelper.dump(pw);
        mAudioSystem.dump(pw);
    }
+49 −0
Original line number Diff line number Diff line
@@ -622,6 +622,55 @@ public class AudioServiceEvents {
        }
    }

    static final class LoudnessEvent extends EventLogger.Event {
        static final int START_PIID = 0;

        static final int STOP_PIID = 1;

        static final int CLIENT_DIED = 2;

        final int mEventType;
        final int mIntValue1;
        final int mIntValue2;

        private LoudnessEvent(int event, int i1, int i2) {
            mEventType = event;
            mIntValue1 = i1;
            mIntValue2 = i2;
        }

        static LoudnessEvent getStartPiid(int piid, int pid) {
            return new LoudnessEvent(START_PIID, piid, pid);
        }

        static LoudnessEvent getStopPiid(int piid, int pid) {
            return new LoudnessEvent(STOP_PIID, piid, pid);
        }

        static LoudnessEvent getClientDied(int pid) {
            return new LoudnessEvent(CLIENT_DIED, 0 /* ignored */, pid);
        }


        @Override
        public String eventToString() {
            switch (mEventType) {
                case START_PIID:
                    return String.format(
                            "Start loudness updates for piid %d for client pid %d",
                            mIntValue1, mIntValue2);
                case STOP_PIID:
                    return String.format(
                            "Stop loudness updates for piid %d for client pid %d",
                            mIntValue1, mIntValue2);
                case CLIENT_DIED:
                    return String.format("Loudness client with pid %d died", mIntValue2);

            }
            return new StringBuilder("FIXME invalid event type:").append(mEventType).toString();
        }
    }

    /**
     * Class to log stream type mute/unmute events
     */
+56 −21
Original line number Diff line number Diff line
@@ -47,7 +47,10 @@ import android.util.SparseIntArray;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.audio.AudioServiceEvents.LoudnessEvent;
import com.android.server.utils.EventLogger;

import java.io.PrintWriter;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
@@ -56,6 +59,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Class to handle the updates in loudness parameters and responsible to generate parameters that
@@ -109,12 +113,19 @@ public class LoudnessCodecHelper {
                pid = (Integer) cookie;
            }
            if (pid != null) {
                mLoudnessCodecHelper.removePid(pid);
                if (DEBUG) {
                    Log.d(TAG, "Client with pid " + pid + " died, removing from receiving updates");
                }
                sLogger.enqueue(LoudnessEvent.getClientDied(pid));
                mLoudnessCodecHelper.onClientPidDied(pid);
            }
            super.onCallbackDied(callback, cookie);
        }
    }

    private static final EventLogger sLogger = new EventLogger(
            AudioService.LOG_NB_EVENTS_LOUDNESS_CODEC, "Loudness updates");

    private final LoudnessRemoteCallbackList mLoudnessUpdateDispatchers =
            new LoudnessRemoteCallbackList(this);

@@ -290,7 +301,10 @@ public class LoudnessCodecHelper {
            Set<LoudnessCodecInfo> infoSet = new HashSet<>(codecInfoList);
            mStartedPiids.put(piid, infoSet);

            mPiidToPidCache.put(piid, Binder.getCallingPid());
            int pid = Binder.getCallingPid();
            mPiidToPidCache.put(piid, pid);

            sLogger.enqueue(LoudnessEvent.getStartPiid(piid, pid));
        }

        try (SafeCloseable ignored = ClearCallingIdentityContext.create()) {
@@ -304,12 +318,15 @@ public class LoudnessCodecHelper {
        if (DEBUG) {
            Log.d(TAG, "stopLoudnessCodecUpdates: piid " + piid);
        }

        synchronized (mLock) {
            if (!mStartedPiids.contains(piid)) {
                Log.w(TAG, "Loudness updates are already stopped for piid " + piid);
                return;
            }
            mStartedPiids.remove(piid);

            sLogger.enqueue(LoudnessEvent.getStopPiid(piid, mPiidToPidCache.get(piid, -1)));
            mPiidToDeviceIdCache.delete(piid);
            mPiidToPidCache.delete(piid);
        }
@@ -366,24 +383,6 @@ public class LoudnessCodecHelper {
        }
    }

    void removePid(int pid) {
        if (DEBUG) {
            Log.d(TAG, "Removing pid " + pid + " from receiving updates");
        }
        synchronized (mLock) {
            for (int i = 0; i < mPiidToPidCache.size(); ++i) {
                int piid = mPiidToPidCache.keyAt(i);
                if (mPiidToPidCache.get(piid) == pid) {
                    if (DEBUG) {
                        Log.d(TAG, "Removing piid  " + piid);
                    }
                    mStartedPiids.delete(piid);
                    mPiidToDeviceIdCache.delete(piid);
                }
            }
        }
    }

    PersistableBundle getLoudnessParams(int piid, LoudnessCodecInfo codecInfo) {
        if (DEBUG) {
            Log.d(TAG, "getLoudnessParams: piid " + piid + " codecInfo " + codecInfo);
@@ -452,7 +451,43 @@ public class LoudnessCodecHelper {
        updateApcList.forEach(apc -> updateCodecParametersForConfiguration(apc));
    }

    /** Updates and dispatches the new loudness parameters for all its registered codecs.
    /** Updates and dispatches the new loudness parameters for all its registered codecs. */
    void dump(PrintWriter pw) {
        // Registered clients
        pw.println("\nRegistered clients:\n");
        synchronized (mLock) {
            for (int i = 0; i < mStartedPiids.size(); ++i) {
                int piid = mStartedPiids.keyAt(i);
                int pid = mPiidToPidCache.get(piid, -1);
                final Set<LoudnessCodecInfo> codecInfos = mStartedPiids.get(piid);
                pw.println(String.format("Player piid %d pid %d active codec types %s\n", piid,
                        pid, codecInfos.stream().map(Object::toString).collect(
                                Collectors.joining(", "))));
            }
            pw.println();
        }

        sLogger.dump(pw);
        pw.println();
    }

    private void onClientPidDied(int pid) {
        synchronized (mLock) {
            for (int i = 0; i < mPiidToPidCache.size(); ++i) {
                int piid = mPiidToPidCache.keyAt(i);
                if (mPiidToPidCache.get(piid) == pid) {
                    if (DEBUG) {
                        Log.d(TAG, "Removing piid  " + piid);
                    }
                    mStartedPiids.delete(piid);
                    mPiidToDeviceIdCache.delete(piid);
                }
            }
        }
    }

    /**
     * Updates and dispatches the new loudness parameters for the {@code codecInfos} set.
     *
     * @param apc the player configuration for which the loudness parameters are updated.
     */