1 /* 2 * Copyright 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.server.tv.tunerresourcemanager; 17 18 import java.util.HashSet; 19 import java.util.Set; 20 21 /** 22 * A client profile object used by the Tuner Resource Manager to record the registered clients' 23 * information. 24 * 25 * @hide 26 */ 27 public final class ClientProfile { 28 29 public static final int INVALID_GROUP_ID = -1; 30 public static final int INVALID_RESOURCE_ID = -1; 31 32 /** 33 * Client id sent to the client when registering with 34 * {@link #registerClientProfile(ResourceClientProfile, TunerResourceManagerCallback, int[])} 35 */ 36 private final int mId; 37 38 /** 39 * see {@link ResourceClientProfile} 40 */ 41 private final String mTvInputSessionId; 42 43 /** 44 * see {@link ResourceClientProfile} 45 */ 46 private final int mUseCase; 47 48 /** 49 * Process id queried from {@link TvInputManager#getPid(String)}. 50 */ 51 private final int mProcessId; 52 53 private boolean mIsForeground; 54 55 /** 56 * All the clients that share the same resource would be under the same group id. 57 * 58 * <p>If a client's resource is to be reclaimed, all other clients under the same group id 59 * also lose their resources. 60 */ 61 private int mGroupId = INVALID_GROUP_ID; 62 /** 63 * Optional nice value for TRM to reduce client’s priority. 64 */ 65 private int mNiceValue; 66 67 /** 68 * List of the frontend handles that are used by the current client. 69 */ 70 private Set<Integer> mUsingFrontendHandles = new HashSet<>(); 71 72 /** 73 * List of the client ids that share frontend with the current client. 74 */ 75 private Set<Integer> mShareFeClientIds = new HashSet<>(); 76 77 /** 78 * List of the Lnb handles that are used by the current client. 79 */ 80 private Set<Integer> mUsingLnbHandles = new HashSet<>(); 81 82 /** 83 * List of the Cas system ids that are used by the current client. 84 */ 85 private int mUsingCasSystemId = INVALID_RESOURCE_ID; 86 87 /** 88 * CiCam id that is used by the client. 89 */ 90 private int mUsingCiCamId = INVALID_RESOURCE_ID; 91 92 /** 93 * Optional arbitrary priority value given by the client. 94 * 95 * <p>This value can override the default priorotiy calculated from 96 * the client profile. 97 */ 98 private int mPriority; 99 ClientProfile(Builder builder)100 private ClientProfile(Builder builder) { 101 this.mId = builder.mId; 102 this.mTvInputSessionId = builder.mTvInputSessionId; 103 this.mUseCase = builder.mUseCase; 104 this.mProcessId = builder.mProcessId; 105 } 106 getId()107 public int getId() { 108 return mId; 109 } 110 getTvInputSessionId()111 public String getTvInputSessionId() { 112 return mTvInputSessionId; 113 } 114 getUseCase()115 public int getUseCase() { 116 return mUseCase; 117 } 118 getProcessId()119 public int getProcessId() { 120 return mProcessId; 121 } 122 123 /** 124 * Set the current isForeground status. 125 */ setForeground(boolean isForeground)126 public void setForeground(boolean isForeground) { 127 mIsForeground = isForeground; 128 } 129 130 /** 131 * Get the previous recorded isForeground status. 132 */ isForeground()133 public boolean isForeground() { 134 return mIsForeground; 135 } 136 getGroupId()137 public int getGroupId() { 138 return mGroupId; 139 } 140 getPriority()141 public int getPriority() { 142 return mPriority - mNiceValue; 143 } 144 setGroupId(int groupId)145 public void setGroupId(int groupId) { 146 mGroupId = groupId; 147 } 148 setPriority(int priority)149 public void setPriority(int priority) { 150 if (priority < 0) { 151 return; 152 } 153 mPriority = priority; 154 } 155 setNiceValue(int niceValue)156 public void setNiceValue(int niceValue) { 157 mNiceValue = niceValue; 158 } 159 160 /** 161 * Set when the client starts to use a frontend. 162 * 163 * @param frontendHandle being used. 164 */ useFrontend(int frontendHandle)165 public void useFrontend(int frontendHandle) { 166 mUsingFrontendHandles.add(frontendHandle); 167 } 168 169 /** 170 * Update the set of client that share frontend with the current client. 171 * 172 * @param clientId the client to share the fe with the current client. 173 */ shareFrontend(int clientId)174 public void shareFrontend(int clientId) { 175 mShareFeClientIds.add(clientId); 176 } 177 178 /** 179 * Remove the given client id from the share frontend client id set. 180 * 181 * @param clientId the client to stop sharing the fe with the current client. 182 */ stopSharingFrontend(int clientId)183 public void stopSharingFrontend(int clientId) { 184 mShareFeClientIds.remove(clientId); 185 } 186 getInUseFrontendHandles()187 public Set<Integer> getInUseFrontendHandles() { 188 return mUsingFrontendHandles; 189 } 190 getShareFeClientIds()191 public Set<Integer> getShareFeClientIds() { 192 return mShareFeClientIds; 193 } 194 195 /** 196 * Called when the client released a frontend. 197 */ releaseFrontend()198 public void releaseFrontend() { 199 mUsingFrontendHandles.clear(); 200 mShareFeClientIds.clear(); 201 } 202 203 /** 204 * Set when the client starts to use an Lnb. 205 * 206 * @param lnbHandle being used. 207 */ useLnb(int lnbHandle)208 public void useLnb(int lnbHandle) { 209 mUsingLnbHandles.add(lnbHandle); 210 } 211 getInUseLnbHandles()212 public Set<Integer> getInUseLnbHandles() { 213 return mUsingLnbHandles; 214 } 215 216 /** 217 * Called when the client released an lnb. 218 * 219 * @param lnbHandle being released. 220 */ releaseLnb(int lnbHandle)221 public void releaseLnb(int lnbHandle) { 222 mUsingLnbHandles.remove(lnbHandle); 223 } 224 225 /** 226 * Set when the client starts to use a Cas system. 227 * 228 * @param casSystemId cas being used. 229 */ useCas(int casSystemId)230 public void useCas(int casSystemId) { 231 mUsingCasSystemId = casSystemId; 232 } 233 getInUseCasSystemId()234 public int getInUseCasSystemId() { 235 return mUsingCasSystemId; 236 } 237 238 /** 239 * Called when the client released a Cas System. 240 */ releaseCas()241 public void releaseCas() { 242 mUsingCasSystemId = INVALID_RESOURCE_ID; 243 } 244 245 /** 246 * Set when the client starts to connect to a CiCam. 247 * 248 * @param ciCamId ciCam being used. 249 */ useCiCam(int ciCamId)250 public void useCiCam(int ciCamId) { 251 mUsingCiCamId = ciCamId; 252 } 253 getInUseCiCamId()254 public int getInUseCiCamId() { 255 return mUsingCiCamId; 256 } 257 258 /** 259 * Called when the client disconnect to a CiCam. 260 */ releaseCiCam()261 public void releaseCiCam() { 262 mUsingCiCamId = INVALID_RESOURCE_ID; 263 } 264 265 /** 266 * Called to reclaim all the resources being used by the current client. 267 */ reclaimAllResources()268 public void reclaimAllResources() { 269 mUsingFrontendHandles.clear(); 270 mShareFeClientIds.clear(); 271 mUsingLnbHandles.clear(); 272 mUsingCasSystemId = INVALID_RESOURCE_ID; 273 mUsingCiCamId = INVALID_RESOURCE_ID; 274 } 275 276 @Override toString()277 public String toString() { 278 return "ClientProfile[id=" + this.mId + ", tvInputSessionId=" + this.mTvInputSessionId 279 + ", useCase=" + this.mUseCase + ", processId=" + this.mProcessId + "]"; 280 } 281 282 /** 283 * Builder class for {@link ClientProfile}. 284 */ 285 public static class Builder { 286 private final int mId; 287 private String mTvInputSessionId; 288 private int mUseCase; 289 private int mProcessId; 290 Builder(int id)291 Builder(int id) { 292 this.mId = id; 293 } 294 295 /** 296 * Builder for {@link ClientProfile}. 297 * 298 * @param useCase the useCase of the client. 299 */ useCase(int useCase)300 public Builder useCase(int useCase) { 301 this.mUseCase = useCase; 302 return this; 303 } 304 305 /** 306 * Builder for {@link ClientProfile}. 307 * 308 * @param tvInputSessionId the id of the tv input session. 309 */ tvInputSessionId(String tvInputSessionId)310 public Builder tvInputSessionId(String tvInputSessionId) { 311 this.mTvInputSessionId = tvInputSessionId; 312 return this; 313 } 314 315 /** 316 * Builder for {@link ClientProfile}. 317 * 318 * @param processId the id of process. 319 */ processId(int processId)320 public Builder processId(int processId) { 321 this.mProcessId = processId; 322 return this; 323 } 324 325 /** 326 * Build a {@link ClientProfile}. 327 * 328 * @return {@link ClientProfile}. 329 */ build()330 public ClientProfile build() { 331 ClientProfile clientProfile = new ClientProfile(this); 332 return clientProfile; 333 } 334 } 335 } 336