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

Commit cc7c8d64 authored by Kensuke Miyagi's avatar Kensuke Miyagi
Browse files

Make Tuner resource reclaiming synchronous

Also in onReclaimResource sequence, handle the reclaim the sharee before
reclaiming the originally requested client

Bug: 197332718
Test: android.media.tv.tuner.cts.TunerTest#testResourceReclaimed and
testResourceReclaimedDifferentThread

Change-Id: Ia4325bf7360a761cf8c6218987e1d62f99096c30
parent 38746443
Loading
Loading
Loading
Loading
+1 −2
Original line number Original line Diff line number Diff line
@@ -53,7 +53,6 @@ import android.media.tv.tunerresourcemanager.TunerFrontendRequest;
import android.media.tv.tunerresourcemanager.TunerLnbRequest;
import android.media.tv.tunerresourcemanager.TunerLnbRequest;
import android.media.tv.tunerresourcemanager.TunerResourceManager;
import android.media.tv.tunerresourcemanager.TunerResourceManager;
import android.os.Handler;
import android.os.Handler;
import android.os.HandlerExecutor;
import android.os.Looper;
import android.os.Looper;
import android.os.Message;
import android.os.Message;
import android.os.Process;
import android.os.Process;
@@ -354,7 +353,7 @@ public class Tuner implements AutoCloseable {
        profile.tvInputSessionId = tvInputSessionId;
        profile.tvInputSessionId = tvInputSessionId;
        profile.useCase = useCase;
        profile.useCase = useCase;
        mTunerResourceManager.registerClientProfile(
        mTunerResourceManager.registerClientProfile(
                profile, new HandlerExecutor(mHandler), mResourceListener, clientId);
                profile, Runnable::run, mResourceListener, clientId);
        mClientId = clientId[0];
        mClientId = clientId[0];


        mUserId = Process.myUid();
        mUserId = Process.myUid();
+2 −2
Original line number Original line Diff line number Diff line
@@ -21,7 +21,7 @@ package android.media.tv.tunerresourcemanager;
 *
 *
 * @hide
 * @hide
 */
 */
oneway interface IResourcesReclaimListener {
interface IResourcesReclaimListener {
    /*
    /*
     * TRM invokes this method when the client's resources need to be reclaimed.
     * TRM invokes this method when the client's resources need to be reclaimed.
     *
     *
+28 −14
Original line number Original line Diff line number Diff line
@@ -922,10 +922,12 @@ public class TunerResourceManagerService extends SystemService implements IBinde
        }
        }
        if (clientId == fe.getOwnerClientId()) {
        if (clientId == fe.getOwnerClientId()) {
            ClientProfile ownerClient = getClientProfile(fe.getOwnerClientId());
            ClientProfile ownerClient = getClientProfile(fe.getOwnerClientId());
            if (ownerClient != null) {
                for (int shareOwnerId : ownerClient.getShareFeClientIds()) {
                for (int shareOwnerId : ownerClient.getShareFeClientIds()) {
                    clearFrontendAndClientMapping(getClientProfile(shareOwnerId));
                    clearFrontendAndClientMapping(getClientProfile(shareOwnerId));
                }
                }
            }
            }
        }
        clearFrontendAndClientMapping(getClientProfile(clientId));
        clearFrontendAndClientMapping(getClientProfile(clientId));
    }
    }


@@ -1039,20 +1041,13 @@ public class TunerResourceManagerService extends SystemService implements IBinde
    @VisibleForTesting
    @VisibleForTesting
    protected boolean reclaimResource(int reclaimingClientId,
    protected boolean reclaimResource(int reclaimingClientId,
            @TunerResourceManager.TunerResourceType int resourceType) {
            @TunerResourceManager.TunerResourceType int resourceType) {
        if (DEBUG) {
            Slog.d(TAG, "Reclaiming resources because higher priority client request resource type "
                    + resourceType);
        }
        try {
            mListeners.get(reclaimingClientId).getListener().onReclaimResources();
        } catch (RemoteException e) {
            Slog.e(TAG, "Failed to reclaim resources on client " + reclaimingClientId, e);
            return false;
        }


        // Reclaim all the resources of the share owners of the frontend that is used by the current
        // Reclaim all the resources of the share owners of the frontend that is used by the current
        // resource reclaimed client.
        // resource reclaimed client.
        ClientProfile profile = getClientProfile(reclaimingClientId);
        ClientProfile profile = getClientProfile(reclaimingClientId);
        if (profile == null) {
            return true;
        }
        Set<Integer> shareFeClientIds = profile.getShareFeClientIds();
        Set<Integer> shareFeClientIds = profile.getShareFeClientIds();
        for (int clientId : shareFeClientIds) {
        for (int clientId : shareFeClientIds) {
            try {
            try {
@@ -1063,6 +1058,17 @@ public class TunerResourceManagerService extends SystemService implements IBinde
            }
            }
            clearAllResourcesAndClientMapping(getClientProfile(clientId));
            clearAllResourcesAndClientMapping(getClientProfile(clientId));
        }
        }

        if (DEBUG) {
            Slog.d(TAG, "Reclaiming resources because higher priority client request resource type "
                    + resourceType + ", clientId:" + reclaimingClientId);
        }
        try {
            mListeners.get(reclaimingClientId).getListener().onReclaimResources();
        } catch (RemoteException e) {
            Slog.e(TAG, "Failed to reclaim resources on client " + reclaimingClientId, e);
            return false;
        }
        clearAllResourcesAndClientMapping(profile);
        clearAllResourcesAndClientMapping(profile);
        return true;
        return true;
    }
    }
@@ -1319,13 +1325,21 @@ public class TunerResourceManagerService extends SystemService implements IBinde
    }
    }


    private void clearFrontendAndClientMapping(ClientProfile profile) {
    private void clearFrontendAndClientMapping(ClientProfile profile) {
        if (profile == null) {
            return;
        }
        for (Integer feId : profile.getInUseFrontendHandles()) {
        for (Integer feId : profile.getInUseFrontendHandles()) {
            FrontendResource fe = getFrontendResource(feId);
            FrontendResource fe = getFrontendResource(feId);
            if (fe.getOwnerClientId() == profile.getId()) {
            int ownerClientId = fe.getOwnerClientId();
            if (ownerClientId == profile.getId()) {
                fe.removeOwner();
                fe.removeOwner();
                continue;
                continue;
            }
            }
            getClientProfile(fe.getOwnerClientId()).stopSharingFrontend(profile.getId());
            ClientProfile ownerClientProfile = getClientProfile(ownerClientId);
            if (ownerClientProfile != null) {
                ownerClientProfile.stopSharingFrontend(profile.getId());
            }

        }
        }
        profile.releaseFrontend();
        profile.releaseFrontend();
    }
    }