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

Commit c2480caa authored by Amy Zhang's avatar Amy Zhang Committed by Automerger Merge Worker
Browse files

Merge "Complete the Cas System request/release/update implementation in TRM"...

Merge "Complete the Cas System request/release/update implementation in TRM" into rvc-dev am: aaa8e866 am: 208c1d32 am: 7abeb41f

Change-Id: I73a504633625883e5b9af648b2dc9d33219243b3
parents 237fe566 7abeb41f
Loading
Loading
Loading
Loading
+10 −10
Original line number Original line Diff line number Diff line
@@ -388,7 +388,7 @@ public final class MediaCas implements AutoCloseable {
            @Override
            @Override
            public void onReclaimResources() {
            public void onReclaimResources() {
                synchronized (mSessionMap) {
                synchronized (mSessionMap) {
                    mSessionMap.forEach((casSession, sessionResourceId) -> casSession.close());
                    mSessionMap.forEach((casSession, sessionResourceHandle) -> casSession.close());
                }
                }
                mEventHandler.sendMessage(mEventHandler.obtainMessage(
                mEventHandler.sendMessage(mEventHandler.obtainMessage(
                        EventHandler.MSG_CAS_RESOURCE_LOST));
                        EventHandler.MSG_CAS_RESOURCE_LOST));
@@ -868,7 +868,7 @@ public final class MediaCas implements AutoCloseable {
        }
        }
    }
    }


    private int getSessionResourceId() throws MediaCasException {
    private int getSessionResourceHandle() throws MediaCasException {
        validateInternalStates();
        validateInternalStates();


        int[] sessionResourceHandle = new int[1];
        int[] sessionResourceHandle = new int[1];
@@ -884,11 +884,11 @@ public final class MediaCas implements AutoCloseable {
        return sessionResourceHandle[0];
        return sessionResourceHandle[0];
    }
    }


    private void addSessionToResourceMap(Session session, int sessionResourceId) {
    private void addSessionToResourceMap(Session session, int sessionResourceHandle) {


        if (sessionResourceId != -1) {
        if (sessionResourceHandle != TunerResourceManager.INVALID_RESOURCE_HANDLE) {
            synchronized (mSessionMap) {
            synchronized (mSessionMap) {
                mSessionMap.put(session, sessionResourceId);
                mSessionMap.put(session, sessionResourceHandle);
            }
            }
        }
        }
    }
    }
@@ -918,13 +918,13 @@ public final class MediaCas implements AutoCloseable {
     * @throws MediaCasStateException for CAS-specific state exceptions.
     * @throws MediaCasStateException for CAS-specific state exceptions.
     */
     */
    public Session openSession() throws MediaCasException {
    public Session openSession() throws MediaCasException {
        int sessionResourceId = getSessionResourceId();
        int sessionResourceHandle = getSessionResourceHandle();


        try {
        try {
            OpenSessionCallback cb = new OpenSessionCallback();
            OpenSessionCallback cb = new OpenSessionCallback();
            mICas.openSession(cb);
            mICas.openSession(cb);
            MediaCasException.throwExceptionIfNeeded(cb.mStatus);
            MediaCasException.throwExceptionIfNeeded(cb.mStatus);
            addSessionToResourceMap(cb.mSession, sessionResourceId);
            addSessionToResourceMap(cb.mSession, sessionResourceHandle);
            return cb.mSession;
            return cb.mSession;
        } catch (RemoteException e) {
        } catch (RemoteException e) {
            cleanupAndRethrowIllegalState();
            cleanupAndRethrowIllegalState();
@@ -952,7 +952,7 @@ public final class MediaCas implements AutoCloseable {
    @Nullable
    @Nullable
    public Session openSession(@SessionUsage int sessionUsage, @ScramblingMode int scramblingMode)
    public Session openSession(@SessionUsage int sessionUsage, @ScramblingMode int scramblingMode)
            throws MediaCasException {
            throws MediaCasException {
        int sessionResourceId = getSessionResourceId();
        int sessionResourceHandle = getSessionResourceHandle();


        if (mICasV12 == null) {
        if (mICasV12 == null) {
            Log.d(TAG, "Open Session with scrambling mode is only supported by cas@1.2+ interface");
            Log.d(TAG, "Open Session with scrambling mode is only supported by cas@1.2+ interface");
@@ -963,7 +963,7 @@ public final class MediaCas implements AutoCloseable {
            OpenSession_1_2_Callback cb = new OpenSession_1_2_Callback();
            OpenSession_1_2_Callback cb = new OpenSession_1_2_Callback();
            mICasV12.openSession_1_2(sessionUsage, scramblingMode, cb);
            mICasV12.openSession_1_2(sessionUsage, scramblingMode, cb);
            MediaCasException.throwExceptionIfNeeded(cb.mStatus);
            MediaCasException.throwExceptionIfNeeded(cb.mStatus);
            addSessionToResourceMap(cb.mSession, sessionResourceId);
            addSessionToResourceMap(cb.mSession, sessionResourceHandle);
            return cb.mSession;
            return cb.mSession;
        } catch (RemoteException e) {
        } catch (RemoteException e) {
            cleanupAndRethrowIllegalState();
            cleanupAndRethrowIllegalState();
+5 −5
Original line number Original line Diff line number Diff line
@@ -218,11 +218,11 @@ interface ITunerResourceManager {
     * <p><strong>Note:</strong> {@link #updateCasInfo(int, int)} must be called before this request.
     * <p><strong>Note:</strong> {@link #updateCasInfo(int, int)} must be called before this request.
     *
     *
     * @param request {@link CasSessionRequest} information of the current request.
     * @param request {@link CasSessionRequest} information of the current request.
     * @param sessionResourceId a one-element array to return the granted cas session id.
     * @param casSessionHandle a one-element array to return the granted cas session handle.
     *
     *
     * @return true if there is CAS session granted.
     * @return true if there is CAS session granted.
     */
     */
    boolean requestCasSession(in CasSessionRequest request, out int[] sessionResourceId);
    boolean requestCasSession(in CasSessionRequest request, out int[] casSessionHandle);


    /*
    /*
     * This API is used by the Tuner framework to request an available Lnb from the TunerHAL.
     * This API is used by the Tuner framework to request an available Lnb from the TunerHAL.
@@ -276,7 +276,7 @@ interface ITunerResourceManager {
     *
     *
     * <p>Client must call this whenever it releases a descrambler.
     * <p>Client must call this whenever it releases a descrambler.
     *
     *
     * @param demuxHandle the handle of the released Tuner Descrambler.
     * @param descramblerHandle the handle of the released Tuner Descrambler.
     * @param clientId the id of the client that is releasing the descrambler.
     * @param clientId the id of the client that is releasing the descrambler.
     */
     */
    void releaseDescrambler(in int descramblerHandle, int clientId);
    void releaseDescrambler(in int descramblerHandle, int clientId);
@@ -288,10 +288,10 @@ interface ITunerResourceManager {
     *
     *
     * <p><strong>Note:</strong> {@link #updateCasInfo(int, int)} must be called before this release.
     * <p><strong>Note:</strong> {@link #updateCasInfo(int, int)} must be called before this release.
     *
     *
     * @param sessionResourceId the id of the released CAS session.
     * @param casSessionHandle the handle of the released CAS session.
     * @param clientId the id of the client that is releasing the cas session.
     * @param clientId the id of the client that is releasing the cas session.
     */
     */
    void releaseCasSession(in int sessionResourceId, int clientId);
    void releaseCasSession(in int casSessionHandle, int clientId);


    /*
    /*
     * Notifies the TRM that the Lnb with the given handle was released.
     * Notifies the TRM that the Lnb with the given handle was released.
+7 −8
Original line number Original line Diff line number Diff line
@@ -362,17 +362,16 @@ public class TunerResourceManager {
     * request.
     * request.
     *
     *
     * @param request {@link CasSessionRequest} information of the current request.
     * @param request {@link CasSessionRequest} information of the current request.
     * @param sessionResourceId a one-element array to return the granted cas session id.
     * @param casSessionHandle a one-element array to return the granted cas session handel.
     *                          If no CAS granted, this will return
     *                         If no CAS granted, this will return {@link #INVALID_RESOURCE_HANDLE}.
     *                          {@link #INVALID_CAS_SESSION_RESOURCE_ID}.
     *
     *
     * @return true if there is CAS session granted.
     * @return true if there is CAS session granted.
     */
     */
    public boolean requestCasSession(@NonNull CasSessionRequest request,
    public boolean requestCasSession(@NonNull CasSessionRequest request,
                @NonNull int[] sessionResourceId) {
                @NonNull int[] casSessionHandle) {
        boolean result = false;
        boolean result = false;
        try {
        try {
            result = mService.requestCasSession(request, sessionResourceId);
            result = mService.requestCasSession(request, casSessionHandle);
        } catch (RemoteException e) {
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
            throw e.rethrowFromSystemServer();
        }
        }
@@ -471,12 +470,12 @@ public class TunerResourceManager {
     * <p><strong>Note:</strong> {@link #updateCasInfo(int, int)} must be called before this
     * <p><strong>Note:</strong> {@link #updateCasInfo(int, int)} must be called before this
     * release.
     * release.
     *
     *
     * @param sessionResourceId the id of the released CAS session.
     * @param casSessionHandle the handle of the released CAS session.
     * @param clientId the id of the client that is releasing the cas session.
     * @param clientId the id of the client that is releasing the cas session.
     */
     */
    public void releaseCasSession(int sessionResourceId, int clientId) {
    public void releaseCasSession(int casSessionHandle, int clientId) {
        try {
        try {
            mService.releaseCasSession(sessionResourceId, clientId);
            mService.releaseCasSession(casSessionHandle, clientId);
        } catch (RemoteException e) {
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
            throw e.rethrowFromSystemServer();
        }
        }
+152 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.server.tv.tunerresourcemanager;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * A Cas resource object used by the Tuner Resource Manager to record the cas
 * information.
 *
 * @hide
 */
public final class CasResource {

    private final int mSystemId;

    private int mMaxSessionNum;

    private int mAvailableSessionNum;

    /**
     * The owner clients' ids when part of the Cas is occupied.
     */
    private Map<Integer, Integer> mOwnerClientIdsToSessionNum = new HashMap<>();

    private CasResource(Builder builder) {
        this.mSystemId = builder.mSystemId;
        this.mMaxSessionNum = builder.mMaxSessionNum;
        this.mAvailableSessionNum = builder.mMaxSessionNum;
    }

    public int getSystemId() {
        return mSystemId;
    }

    public int getMaxSessionNum() {
        return mMaxSessionNum;
    }

    public int getUsedSessionNum() {
        return (mMaxSessionNum - mAvailableSessionNum);
    }

    public boolean isFullyUsed() {
        return mAvailableSessionNum == 0;
    }

    /**
     * Update max session number.
     *
     * @param maxSessionNum the new max session num.
     */
    public void updateMaxSessionNum(int maxSessionNum) {
        mAvailableSessionNum = Math.max(
                0, mAvailableSessionNum + (maxSessionNum - mMaxSessionNum));
        mMaxSessionNum = maxSessionNum;
    }

    /**
     * Set an owner for the cas
     *
     * @param ownerId the client id of the owner.
     */
    public void setOwner(int ownerId) {
        int sessionNum = mOwnerClientIdsToSessionNum.get(ownerId) == null
                ? 1 : (mOwnerClientIdsToSessionNum.get(ownerId) + 1);
        mOwnerClientIdsToSessionNum.put(ownerId, sessionNum);
        mAvailableSessionNum--;
    }

    /**
     * Remove an owner of the Cas.
     *
     * @param ownerId the removing client id of the owner.
     */
    public void removeOwner(int ownerId) {
        mAvailableSessionNum += mOwnerClientIdsToSessionNum.get(ownerId);
        mOwnerClientIdsToSessionNum.remove(ownerId);
    }

    public Set<Integer> getOwnerClientIds() {
        return mOwnerClientIdsToSessionNum.keySet();
    }

    @Override
    public String toString() {
        return "CasResource[systemId=" + this.mSystemId
                + ", isFullyUsed=" + (this.mAvailableSessionNum == 0)
                + ", maxSessionNum=" + this.mMaxSessionNum
                + ", ownerClients=" + ownersMapToString() + "]";
    }

    /**
     * Builder class for {@link CasResource}.
     */
    public static class Builder {

        private int mSystemId;
        private int mMaxSessionNum;

        Builder(int systemId) {
            this.mSystemId = systemId;
        }

        /**
         * Builder for {@link CasResource}.
         *
         * @param maxSessionNum the max session num the current Cas has.
         */
        public Builder maxSessionNum(int maxSessionNum) {
            this.mMaxSessionNum = maxSessionNum;
            return this;
        }

        /**
         * Build a {@link CasResource}.
         *
         * @return {@link CasResource}.
         */
        public CasResource build() {
            CasResource cas = new CasResource(this);
            return cas;
        }
    }

    private String ownersMapToString() {
        StringBuilder string = new StringBuilder("{");
        for (int clienId : mOwnerClientIdsToSessionNum.keySet()) {
            string.append(" clientId=")
                  .append(clienId)
                  .append(", owns session num=")
                  .append(mOwnerClientIdsToSessionNum.get(clienId))
                  .append(",");
        }
        return string.append("}").toString();
    }
}
+27 −1
Original line number Original line Diff line number Diff line
@@ -27,6 +27,7 @@ import java.util.Set;
public final class ClientProfile {
public final class ClientProfile {


    public static final int INVALID_GROUP_ID = -1;
    public static final int INVALID_GROUP_ID = -1;
    public static final int INVALID_RESOURCE_ID = -1;


    /**
    /**
     * Client id sent to the client when registering with
     * Client id sent to the client when registering with
@@ -56,7 +57,6 @@ public final class ClientProfile {
     * also lose their resources.
     * also lose their resources.
     */
     */
    private int mGroupId = INVALID_GROUP_ID;
    private int mGroupId = INVALID_GROUP_ID;

    /**
    /**
     * Optional nice value for TRM to reduce client’s priority.
     * Optional nice value for TRM to reduce client’s priority.
     */
     */
@@ -72,6 +72,11 @@ public final class ClientProfile {
     */
     */
    private Set<Integer> mUsingLnbIds = new HashSet<>();
    private Set<Integer> mUsingLnbIds = new HashSet<>();


    /**
     * List of the Cas system ids that are used by the current client.
     */
    private int mUsingCasSystemId = INVALID_RESOURCE_ID;

    /**
    /**
     * Optional arbitrary priority value given by the client.
     * Optional arbitrary priority value given by the client.
     *
     *
@@ -171,12 +176,33 @@ public final class ClientProfile {
        mUsingLnbIds.remove(lnbId);
        mUsingLnbIds.remove(lnbId);
    }
    }


    /**
     * Set when the client starts to use a Cas system.
     *
     * @param casSystemId cas being used.
     */
    public void useCas(int casSystemId) {
        mUsingCasSystemId = casSystemId;
    }

    public int getInUseCasSystemId() {
        return mUsingCasSystemId;
    }

    /**
     * Called when the client released a Cas System.
     */
    public void releaseCas() {
        mUsingCasSystemId = INVALID_RESOURCE_ID;
    }

    /**
    /**
     * Called to reclaim all the resources being used by the current client.
     * Called to reclaim all the resources being used by the current client.
     */
     */
    public void reclaimAllResources() {
    public void reclaimAllResources() {
        mUsingFrontendIds.clear();
        mUsingFrontendIds.clear();
        mUsingLnbIds.clear();
        mUsingLnbIds.clear();
        mUsingCasSystemId = INVALID_RESOURCE_ID;
    }
    }


    @Override
    @Override
Loading