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

Commit b30d2ffc authored by Shunkai Yao's avatar Shunkai Yao
Browse files

PlaybackActivityMon: sync access portId to piid mapping

 - avoid inconsistent between synchronized get and async put
 - some minor logging changes
 - and remove duplicated mPlayerLock

Bug: 294623562
Test: atest AudioPlaybackConfigurationTest --iterations 50
Test: atest CtsMediaAudioTestCases
Change-Id: I4f1b446896ce21dc515ac0ed192ee4b5aca6164b
parent e455c815
Loading
Loading
Loading
Loading
+23 −53
Original line number Original line Diff line number Diff line
@@ -235,6 +235,10 @@ public final class PlaybackActivityMonitor
    public int trackPlayer(PlayerBase.PlayerIdCard pic) {
    public int trackPlayer(PlayerBase.PlayerIdCard pic) {
        final int newPiid = AudioSystem.newAudioPlayerId();
        final int newPiid = AudioSystem.newAudioPlayerId();
        if (DEBUG) { Log.v(TAG, "trackPlayer() new piid=" + newPiid); }
        if (DEBUG) { Log.v(TAG, "trackPlayer() new piid=" + newPiid); }
        if (newPiid == PLAYER_PIID_INVALID) {
            Log.w(TAG, "invalid piid assigned from AudioSystem");
            return newPiid;
        }
        final AudioPlaybackConfiguration apc =
        final AudioPlaybackConfiguration apc =
                new AudioPlaybackConfiguration(pic, newPiid,
                new AudioPlaybackConfiguration(pic, newPiid,
                        Binder.getCallingUid(), Binder.getCallingPid());
                        Binder.getCallingUid(), Binder.getCallingPid());
@@ -365,8 +369,7 @@ public final class PlaybackActivityMonitor
            sEventLogger.enqueue(new PlayerEvent(piid, event, eventValue));
            sEventLogger.enqueue(new PlayerEvent(piid, event, eventValue));


            if (event == AudioPlaybackConfiguration.PLAYER_UPDATE_PORT_ID) {
            if (event == AudioPlaybackConfiguration.PLAYER_UPDATE_PORT_ID) {
                mEventHandler.sendMessage(
                mPortIdToPiid.put(eventValue, piid);
                        mEventHandler.obtainMessage(MSG_II_UPDATE_PORT_EVENT, eventValue, piid));
                return;
                return;
            } else if (event == AudioPlaybackConfiguration.PLAYER_STATE_STARTED) {
            } else if (event == AudioPlaybackConfiguration.PLAYER_STATE_STARTED) {
                for (Integer uidInteger: mBannedUids) {
                for (Integer uidInteger: mBannedUids) {
@@ -429,16 +432,12 @@ public final class PlaybackActivityMonitor
        synchronized (mPlayerLock) {
        synchronized (mPlayerLock) {
            int piid = mPortIdToPiid.get(portId, PLAYER_PIID_INVALID);
            int piid = mPortIdToPiid.get(portId, PLAYER_PIID_INVALID);
            if (piid == PLAYER_PIID_INVALID) {
            if (piid == PLAYER_PIID_INVALID) {
                if (DEBUG) {
                Log.w(TAG, "No piid assigned for invalid/internal port id " + portId);
                    Log.v(TAG, "No piid assigned for invalid/internal port id " + portId);
                }
                return;
                return;
            }
            }
            final AudioPlaybackConfiguration apc = mPlayers.get(piid);
            final AudioPlaybackConfiguration apc = mPlayers.get(piid);
            if (apc == null) {
            if (apc == null) {
                if (DEBUG) {
                Log.w(TAG, "No AudioPlaybackConfiguration assigned for piid " + piid);
                    Log.v(TAG, "No AudioPlaybackConfiguration assigned for piid " + piid);
                }
                return;
                return;
            }
            }


@@ -470,11 +469,18 @@ public final class PlaybackActivityMonitor
    public void releasePlayer(int piid, int binderUid) {
    public void releasePlayer(int piid, int binderUid) {
        if (DEBUG) { Log.v(TAG, "releasePlayer() for piid=" + piid); }
        if (DEBUG) { Log.v(TAG, "releasePlayer() for piid=" + piid); }
        boolean change = false;
        boolean change = false;
        if (piid == PLAYER_PIID_INVALID) {
            Log.w(TAG, "Received releasePlayer with invalid piid: " + piid);
            sEventLogger.enqueue(new EventLogger.StringEvent("releasePlayer with invalid piid:"
                    + piid + ", uid:" + binderUid));
            return;
        }

        synchronized(mPlayerLock) {
        synchronized(mPlayerLock) {
            final AudioPlaybackConfiguration apc = mPlayers.get(new Integer(piid));
            final AudioPlaybackConfiguration apc = mPlayers.get(new Integer(piid));
            if (checkConfigurationCaller(piid, apc, binderUid)) {
            if (checkConfigurationCaller(piid, apc, binderUid)) {
                sEventLogger.enqueue(new EventLogger.StringEvent(
                sEventLogger.enqueue(new EventLogger.StringEvent(
                        "releasing player piid:" + piid));
                        "releasing player piid:" + piid + ", uid:" + binderUid));
                mPlayers.remove(new Integer(piid));
                mPlayers.remove(new Integer(piid));
                mDuckingManager.removeReleased(apc);
                mDuckingManager.removeReleased(apc);
                mFadeOutManager.removeReleased(apc);
                mFadeOutManager.removeReleased(apc);
@@ -484,8 +490,10 @@ public final class PlaybackActivityMonitor
                        AudioPlaybackConfiguration.PLAYER_DEVICEID_INVALID);
                        AudioPlaybackConfiguration.PLAYER_DEVICEID_INVALID);


                // remove all port ids mapped to the released player
                // remove all port ids mapped to the released player
                mEventHandler.sendMessage(
                int portIdx;
                        mEventHandler.obtainMessage(MSG_I_CLEAR_PORTS_FOR_PIID, piid, /*arg2=*/0));
                while ((portIdx = mPortIdToPiid.indexOfValue(piid)) >= 0) {
                    mPortIdToPiid.removeAt(portIdx);
                }


                if (change && mDoNotLogPiidList.contains(piid)) {
                if (change && mDoNotLogPiidList.contains(piid)) {
                    // do not dispatch a change for a "do not log" player
                    // do not dispatch a change for a "do not log" player
@@ -1608,14 +1616,6 @@ public final class PlaybackActivityMonitor
     */
     */
    private static final int MSG_L_TIMEOUT_MUTE_AWAIT_CONNECTION = 1;
    private static final int MSG_L_TIMEOUT_MUTE_AWAIT_CONNECTION = 1;


    /**
     * assign new port id to piid
     * args:
     *     msg.arg1: port id
     *     msg.arg2: piid
     */
    private static final int MSG_II_UPDATE_PORT_EVENT = 2;

    /**
    /**
     * event for player getting muted
     * event for player getting muted
     * args:
     * args:
@@ -1624,14 +1624,7 @@ public final class PlaybackActivityMonitor
     *     msg.obj: extras describing the mute reason
     *     msg.obj: extras describing the mute reason
     *         type: PersistableBundle
     *         type: PersistableBundle
     */
     */
    private static final int MSG_IIL_UPDATE_PLAYER_MUTED_EVENT = 3;
    private static final int MSG_IIL_UPDATE_PLAYER_MUTED_EVENT = 2;

    /**
     * clear all ports assigned to a given piid
     * args:
     *     msg.arg1: the piid
     */
    private static final int MSG_I_CLEAR_PORTS_FOR_PIID = 4;


    /**
    /**
     * event for player reporting playback format and spatialization status
     * event for player reporting playback format and spatialization status
@@ -1641,7 +1634,7 @@ public final class PlaybackActivityMonitor
     *     msg.obj: extras describing the sample rate, channel mask, spatialized
     *     msg.obj: extras describing the sample rate, channel mask, spatialized
     *         type: PersistableBundle
     *         type: PersistableBundle
     */
     */
    private static final int MSG_IIL_UPDATE_PLAYER_FORMAT = 5;
    private static final int MSG_IIL_UPDATE_PLAYER_FORMAT = 3;


    private void initEventHandler() {
    private void initEventHandler() {
        mEventThread = new HandlerThread(TAG);
        mEventThread = new HandlerThread(TAG);
@@ -1660,11 +1653,6 @@ public final class PlaybackActivityMonitor
                        mMuteAwaitConnectionTimeoutCb.accept((AudioDeviceAttributes) msg.obj);
                        mMuteAwaitConnectionTimeoutCb.accept((AudioDeviceAttributes) msg.obj);
                        break;
                        break;


                    case MSG_II_UPDATE_PORT_EVENT:
                        synchronized (mPlayerLock) {
                            mPortIdToPiid.put(/*portId*/msg.arg1, /*piid*/msg.arg2);
                        }
                        break;
                    case MSG_IIL_UPDATE_PLAYER_MUTED_EVENT:
                    case MSG_IIL_UPDATE_PLAYER_MUTED_EVENT:
                        // TODO: replace PersistableBundle with own struct
                        // TODO: replace PersistableBundle with own struct
                        PersistableBundle extras = (PersistableBundle) msg.obj;
                        PersistableBundle extras = (PersistableBundle) msg.obj;
@@ -1680,10 +1668,7 @@ public final class PlaybackActivityMonitor
                            sEventLogger.enqueue(
                            sEventLogger.enqueue(
                                    new PlayerEvent(piid, PLAYER_UPDATE_MUTED, eventValue));
                                    new PlayerEvent(piid, PLAYER_UPDATE_MUTED, eventValue));


                            final AudioPlaybackConfiguration apc;
                            final AudioPlaybackConfiguration apc = mPlayers.get(piid);
                            synchronized (mPlayerLock) {
                                apc = mPlayers.get(piid);
                            }
                            if (apc == null || !apc.handleMutedEvent(eventValue)) {
                            if (apc == null || !apc.handleMutedEvent(eventValue)) {
                                break;  // do not dispatch
                                break;  // do not dispatch
                            }
                            }
@@ -1691,21 +1676,6 @@ public final class PlaybackActivityMonitor
                        }
                        }
                        break;
                        break;


                    case MSG_I_CLEAR_PORTS_FOR_PIID:
                        int piid = msg.arg1;
                        if (piid == AudioPlaybackConfiguration.PLAYER_PIID_INVALID) {
                            Log.w(TAG, "Received clear ports with invalid piid");
                            break;
                        }

                        synchronized (mPlayerLock) {
                            int portIdx;
                            while ((portIdx = mPortIdToPiid.indexOfValue(piid)) >= 0) {
                                mPortIdToPiid.removeAt(portIdx);
                            }
                        }
                        break;

                    case MSG_IIL_UPDATE_PLAYER_FORMAT:
                    case MSG_IIL_UPDATE_PLAYER_FORMAT:
                        final PersistableBundle formatExtras = (PersistableBundle) msg.obj;
                        final PersistableBundle formatExtras = (PersistableBundle) msg.obj;
                        if (formatExtras == null) {
                        if (formatExtras == null) {