Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2015, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "ResourceManagerService" |
| 20 | #include <utils/Log.h> |
| 21 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 22 | #include <android/binder_manager.h> |
| 23 | #include <android/binder_process.h> |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 24 | #include <binder/IMediaResourceMonitor.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 25 | #include <binder/IServiceManager.h> |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 26 | #include <cutils/sched_policy.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 27 | #include <dirent.h> |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 28 | #include <media/MediaResourcePolicy.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 29 | #include <media/stagefright/ProcessInfo.h> |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 30 | #include <mediautils/BatteryNotifier.h> |
| 31 | #include <mediautils/SchedulingPolicyService.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 32 | #include <string.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <sys/time.h> |
| 36 | #include <unistd.h> |
| 37 | |
| 38 | #include "ResourceManagerService.h" |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 39 | #include "ServiceLog.h" |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 40 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 41 | namespace android { |
| 42 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 43 | DeathNotifier::DeathNotifier(const std::shared_ptr<ResourceManagerService> &service, |
| 44 | int pid, int64_t clientId) |
| 45 | : mService(service), mPid(pid), mClientId(clientId) {} |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 46 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 47 | //static |
| 48 | void DeathNotifier::BinderDiedCallback(void* cookie) { |
| 49 | auto thiz = static_cast<DeathNotifier*>(cookie); |
| 50 | thiz->binderDied(); |
| 51 | } |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 52 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 53 | void DeathNotifier::binderDied() { |
| 54 | // Don't check for pid validity since we know it's already dead. |
| 55 | std::shared_ptr<ResourceManagerService> service = mService.lock(); |
| 56 | if (service == nullptr) { |
| 57 | ALOGW("ResourceManagerService is dead as well."); |
| 58 | return; |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 59 | } |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 60 | service->removeResource(mPid, mClientId, false); |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 61 | |
| 62 | service->overridePid(mPid, -1); |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 63 | } |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 64 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 65 | template <typename T> |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 66 | static String8 getString(const std::vector<T> &items) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 67 | String8 itemsStr; |
| 68 | for (size_t i = 0; i < items.size(); ++i) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 69 | itemsStr.appendFormat("%s ", toString(items[i]).string()); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 70 | } |
| 71 | return itemsStr; |
| 72 | } |
| 73 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 74 | static bool hasResourceType(MediaResource::Type type, const ResourceList& resources) { |
| 75 | for (auto it = resources.begin(); it != resources.end(); it++) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 76 | if (it->second.type == type) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 77 | return true; |
| 78 | } |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
Chih-Hung Hsieh | 51873d8 | 2016-08-09 14:18:51 -0700 | [diff] [blame] | 83 | static bool hasResourceType(MediaResource::Type type, const ResourceInfos& infos) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 84 | for (size_t i = 0; i < infos.size(); ++i) { |
| 85 | if (hasResourceType(type, infos[i].resources)) { |
| 86 | return true; |
| 87 | } |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | static ResourceInfos& getResourceInfosForEdit( |
| 93 | int pid, |
| 94 | PidResourceInfosMap& map) { |
| 95 | ssize_t index = map.indexOfKey(pid); |
| 96 | if (index < 0) { |
| 97 | // new pid |
| 98 | ResourceInfos infosForPid; |
| 99 | map.add(pid, infosForPid); |
| 100 | } |
| 101 | |
| 102 | return map.editValueFor(pid); |
| 103 | } |
| 104 | |
| 105 | static ResourceInfo& getResourceInfoForEdit( |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 106 | uid_t uid, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 107 | int64_t clientId, |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 108 | const std::shared_ptr<IResourceManagerClient>& client, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 109 | ResourceInfos& infos) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 110 | ssize_t index = infos.indexOfKey(clientId); |
| 111 | |
| 112 | if (index < 0) { |
| 113 | ResourceInfo info; |
| 114 | info.uid = uid; |
| 115 | info.clientId = clientId; |
| 116 | info.client = client; |
| 117 | |
| 118 | index = infos.add(clientId, info); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 119 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 120 | |
| 121 | return infos.editValueAt(index); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 124 | static void notifyResourceGranted(int pid, const std::vector<MediaResourceParcel> &resources) { |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 125 | static const char* const kServiceName = "media_resource_monitor"; |
Dongwon Kang | 2642c84 | 2016-03-23 18:07:29 -0700 | [diff] [blame] | 126 | sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName)); |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 127 | if (binder != NULL) { |
| 128 | sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder); |
| 129 | for (size_t i = 0; i < resources.size(); ++i) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 130 | if (resources[i].subType == MediaResource::SubType::kAudioCodec) { |
Dongwon Kang | 69c23dd | 2016-03-22 15:22:45 -0700 | [diff] [blame] | 131 | service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_AUDIO_CODEC); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 132 | } else if (resources[i].subType == MediaResource::SubType::kVideoCodec) { |
Dongwon Kang | 69c23dd | 2016-03-22 15:22:45 -0700 | [diff] [blame] | 133 | service->notifyResourceGranted(pid, IMediaResourceMonitor::TYPE_VIDEO_CODEC); |
| 134 | } |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 139 | binder_status_t ResourceManagerService::dump( |
| 140 | int fd, const char** /*args*/, uint32_t /*numArgs*/) { |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 141 | String8 result; |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 142 | |
dcashman | 014e91e | 2015-09-11 09:33:01 -0700 | [diff] [blame] | 143 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 144 | result.format("Permission Denial: " |
| 145 | "can't dump ResourceManagerService from pid=%d, uid=%d\n", |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 146 | AIBinder_getCallingPid(), |
| 147 | AIBinder_getCallingUid()); |
dcashman | 014e91e | 2015-09-11 09:33:01 -0700 | [diff] [blame] | 148 | write(fd, result.string(), result.size()); |
| 149 | return PERMISSION_DENIED; |
| 150 | } |
| 151 | |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 152 | PidResourceInfosMap mapCopy; |
| 153 | bool supportsMultipleSecureCodecs; |
| 154 | bool supportsSecureWithNonSecureCodec; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 155 | std::map<int, int> overridePidMapCopy; |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 156 | String8 serviceLog; |
| 157 | { |
| 158 | Mutex::Autolock lock(mLock); |
| 159 | mapCopy = mMap; // Shadow copy, real copy will happen on write. |
| 160 | supportsMultipleSecureCodecs = mSupportsMultipleSecureCodecs; |
| 161 | supportsSecureWithNonSecureCodec = mSupportsSecureWithNonSecureCodec; |
| 162 | serviceLog = mServiceLog->toString(" " /* linePrefix */); |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 163 | overridePidMapCopy = mOverridePidMap; |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | const size_t SIZE = 256; |
| 167 | char buffer[SIZE]; |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 168 | snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this); |
| 169 | result.append(buffer); |
| 170 | result.append(" Policies:\n"); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 171 | snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", supportsMultipleSecureCodecs); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 172 | result.append(buffer); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 173 | snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n", |
| 174 | supportsSecureWithNonSecureCodec); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 175 | result.append(buffer); |
| 176 | |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 177 | result.append(" Processes:\n"); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 178 | for (size_t i = 0; i < mapCopy.size(); ++i) { |
| 179 | snprintf(buffer, SIZE, " Pid: %d\n", mapCopy.keyAt(i)); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 180 | result.append(buffer); |
| 181 | |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 182 | const ResourceInfos &infos = mapCopy.valueAt(i); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 183 | for (size_t j = 0; j < infos.size(); ++j) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 184 | result.append(" Client:\n"); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 185 | snprintf(buffer, SIZE, " Id: %lld\n", (long long)infos[j].clientId); |
| 186 | result.append(buffer); |
| 187 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 188 | std::string clientName; |
| 189 | Status status = infos[j].client->getName(&clientName); |
| 190 | if (!status.isOk()) { |
| 191 | clientName = "<unknown client>"; |
| 192 | } |
| 193 | snprintf(buffer, SIZE, " Name: %s\n", clientName.c_str()); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 194 | result.append(buffer); |
| 195 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 196 | const ResourceList &resources = infos[j].resources; |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 197 | result.append(" Resources:\n"); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 198 | for (auto it = resources.begin(); it != resources.end(); it++) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 199 | snprintf(buffer, SIZE, " %s\n", toString(it->second).string()); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 200 | result.append(buffer); |
| 201 | } |
| 202 | } |
| 203 | } |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 204 | result.append(" Process Pid override:\n"); |
| 205 | for (auto it = overridePidMapCopy.begin(); it != overridePidMapCopy.end(); ++it) { |
| 206 | snprintf(buffer, SIZE, " Original Pid: %d, Override Pid: %d\n", |
| 207 | it->first, it->second); |
| 208 | result.append(buffer); |
| 209 | } |
Ronghua Wu | 022ed72 | 2015-05-11 15:15:09 -0700 | [diff] [blame] | 210 | result.append(" Events logs (most recent at top):\n"); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 211 | result.append(serviceLog); |
Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 212 | |
| 213 | write(fd, result.string(), result.size()); |
| 214 | return OK; |
| 215 | } |
| 216 | |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 217 | struct SystemCallbackImpl : |
| 218 | public ResourceManagerService::SystemCallbackInterface { |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 219 | SystemCallbackImpl() : mClientToken(new BBinder()) {} |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 220 | |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 221 | virtual void noteStartVideo(int uid) override { |
| 222 | BatteryNotifier::getInstance().noteStartVideo(uid); |
| 223 | } |
| 224 | virtual void noteStopVideo(int uid) override { |
| 225 | BatteryNotifier::getInstance().noteStopVideo(uid); |
| 226 | } |
| 227 | virtual void noteResetVideo() override { |
| 228 | BatteryNotifier::getInstance().noteResetVideo(); |
| 229 | } |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 230 | virtual bool requestCpusetBoost(bool enable) override { |
| 231 | return android::requestCpusetBoost(enable, mClientToken); |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | protected: |
| 235 | virtual ~SystemCallbackImpl() {} |
| 236 | |
| 237 | private: |
| 238 | DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl); |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 239 | sp<IBinder> mClientToken; |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | ResourceManagerService::ResourceManagerService() |
| 243 | : ResourceManagerService(new ProcessInfo(), new SystemCallbackImpl()) {} |
| 244 | |
| 245 | ResourceManagerService::ResourceManagerService( |
| 246 | const sp<ProcessInfoInterface> &processInfo, |
| 247 | const sp<SystemCallbackInterface> &systemResource) |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 248 | : mProcessInfo(processInfo), |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 249 | mSystemCB(systemResource), |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 250 | mServiceLog(new ServiceLog()), |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 251 | mSupportsMultipleSecureCodecs(true), |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 252 | mSupportsSecureWithNonSecureCodec(true), |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 253 | mCpuBoostCount(0), |
| 254 | mDeathRecipient(AIBinder_DeathRecipient_new(DeathNotifier::BinderDiedCallback)) { |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 255 | mSystemCB->noteResetVideo(); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 256 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 257 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 258 | //static |
| 259 | void ResourceManagerService::instantiate() { |
| 260 | std::shared_ptr<ResourceManagerService> service = |
| 261 | ::ndk::SharedRefBase::make<ResourceManagerService>(); |
| 262 | binder_status_t status = |
| 263 | AServiceManager_addService(service->asBinder().get(), getServiceName()); |
| 264 | if (status != STATUS_OK) { |
| 265 | return; |
| 266 | } |
| 267 | // TODO: mediaserver main() is already starting the thread pool, |
| 268 | // move this to mediaserver main() when other services in mediaserver |
| 269 | // are converted to ndk-platform aidl. |
| 270 | //ABinderProcess_startThreadPool(); |
| 271 | } |
| 272 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 273 | ResourceManagerService::~ResourceManagerService() {} |
| 274 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 275 | Status ResourceManagerService::config(const std::vector<MediaResourcePolicyParcel>& policies) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 276 | String8 log = String8::format("config(%s)", getString(policies).string()); |
| 277 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 278 | |
| 279 | Mutex::Autolock lock(mLock); |
| 280 | for (size_t i = 0; i < policies.size(); ++i) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 281 | const std::string &type = policies[i].type; |
| 282 | const std::string &value = policies[i].value; |
| 283 | if (type == MediaResourcePolicy::kPolicySupportsMultipleSecureCodecs()) { |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 284 | mSupportsMultipleSecureCodecs = (value == "true"); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 285 | } else if (type == MediaResourcePolicy::kPolicySupportsSecureWithNonSecureCodec()) { |
Ronghua Wu | 9ba21b9 | 2015-04-21 14:23:06 -0700 | [diff] [blame] | 286 | mSupportsSecureWithNonSecureCodec = (value == "true"); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 289 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 292 | void ResourceManagerService::onFirstAdded( |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 293 | const MediaResourceParcel& resource, const ResourceInfo& clientInfo) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 294 | // first time added |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 295 | if (resource.type == MediaResource::Type::kCpuBoost |
| 296 | && resource.subType == MediaResource::SubType::kUnspecifiedSubType) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 297 | // Request it on every new instance of kCpuBoost, as the media.codec |
| 298 | // could have died, if we only do it the first time subsequent instances |
| 299 | // never gets the boost. |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 300 | if (mSystemCB->requestCpusetBoost(true) != OK) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 301 | ALOGW("couldn't request cpuset boost"); |
| 302 | } |
| 303 | mCpuBoostCount++; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 304 | } else if (resource.type == MediaResource::Type::kBattery |
| 305 | && resource.subType == MediaResource::SubType::kVideoCodec) { |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 306 | mSystemCB->noteStartVideo(clientInfo.uid); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | void ResourceManagerService::onLastRemoved( |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 311 | const MediaResourceParcel& resource, const ResourceInfo& clientInfo) { |
| 312 | if (resource.type == MediaResource::Type::kCpuBoost |
| 313 | && resource.subType == MediaResource::SubType::kUnspecifiedSubType |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 314 | && mCpuBoostCount > 0) { |
| 315 | if (--mCpuBoostCount == 0) { |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 316 | mSystemCB->requestCpusetBoost(false); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 317 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 318 | } else if (resource.type == MediaResource::Type::kBattery |
| 319 | && resource.subType == MediaResource::SubType::kVideoCodec) { |
Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 320 | mSystemCB->noteStopVideo(clientInfo.uid); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 324 | void ResourceManagerService::mergeResources( |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 325 | MediaResourceParcel& r1, const MediaResourceParcel& r2) { |
| 326 | // The resource entry on record is maintained to be in [0,INT64_MAX]. |
| 327 | // Clamp if merging in the new resource value causes it to go out of bound. |
| 328 | // Note that the new resource value could be negative, eg.DrmSession, the |
| 329 | // value goes lower when the session is used more often. During reclaim |
| 330 | // the session with the highest value (lowest usage) would be closed. |
| 331 | if (r2.value < INT64_MAX - r1.value) { |
| 332 | r1.value += r2.value; |
| 333 | if (r1.value < 0) { |
| 334 | r1.value = 0; |
| 335 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 336 | } else { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 337 | r1.value = INT64_MAX; |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 341 | Status ResourceManagerService::addResource( |
| 342 | int32_t pid, |
| 343 | int32_t uid, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 344 | int64_t clientId, |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 345 | const std::shared_ptr<IResourceManagerClient>& client, |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 346 | const std::vector<MediaResourceParcel>& resources) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 347 | String8 log = String8::format("addResource(pid %d, clientId %lld, resources %s)", |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 348 | pid, (long long) clientId, getString(resources).string()); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 349 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 350 | |
| 351 | Mutex::Autolock lock(mLock); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 352 | if (!mProcessInfo->isValidPid(pid)) { |
| 353 | ALOGE("Rejected addResource call with invalid pid."); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 354 | return Status::fromServiceSpecificError(BAD_VALUE); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 355 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 356 | ResourceInfos& infos = getResourceInfosForEdit(pid, mMap); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 357 | ResourceInfo& info = getResourceInfoForEdit(uid, clientId, client, infos); |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 358 | |
| 359 | for (size_t i = 0; i < resources.size(); ++i) { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 360 | const auto &res = resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 361 | const auto resType = std::tuple(res.type, res.subType, res.id); |
| 362 | |
| 363 | if (res.value < 0 && res.type != MediaResource::Type::kDrmSession) { |
| 364 | ALOGW("Ignoring request to remove negative value of non-drm resource"); |
| 365 | continue; |
| 366 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 367 | if (info.resources.find(resType) == info.resources.end()) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 368 | if (res.value <= 0) { |
| 369 | // We can't init a new entry with negative value, although it's allowed |
| 370 | // to merge in negative values after the initial add. |
| 371 | ALOGW("Ignoring request to add new resource entry with value <= 0"); |
| 372 | continue; |
| 373 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 374 | onFirstAdded(res, info); |
| 375 | info.resources[resType] = res; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 376 | } else { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 377 | mergeResources(info.resources[resType], res); |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 380 | if (info.deathNotifier == nullptr && client != nullptr) { |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 381 | info.deathNotifier = new DeathNotifier(ref<ResourceManagerService>(), pid, clientId); |
| 382 | AIBinder_linkToDeath(client->asBinder().get(), |
| 383 | mDeathRecipient.get(), info.deathNotifier.get()); |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 384 | } |
Dongwon Kang | fe508d3 | 2015-12-15 14:22:05 +0900 | [diff] [blame] | 385 | notifyResourceGranted(pid, resources); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 386 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 389 | Status ResourceManagerService::removeResource( |
| 390 | int32_t pid, int64_t clientId, |
| 391 | const std::vector<MediaResourceParcel>& resources) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 392 | String8 log = String8::format("removeResource(pid %d, clientId %lld, resources %s)", |
| 393 | pid, (long long) clientId, getString(resources).string()); |
| 394 | mServiceLog->add(log); |
| 395 | |
| 396 | Mutex::Autolock lock(mLock); |
| 397 | if (!mProcessInfo->isValidPid(pid)) { |
| 398 | ALOGE("Rejected removeResource call with invalid pid."); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 399 | return Status::fromServiceSpecificError(BAD_VALUE); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 400 | } |
| 401 | ssize_t index = mMap.indexOfKey(pid); |
| 402 | if (index < 0) { |
| 403 | ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 404 | return Status::ok(); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 405 | } |
| 406 | ResourceInfos &infos = mMap.editValueAt(index); |
| 407 | |
| 408 | index = infos.indexOfKey(clientId); |
| 409 | if (index < 0) { |
| 410 | ALOGV("removeResource: didn't find clientId %lld", (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 411 | return Status::ok(); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | ResourceInfo &info = infos.editValueAt(index); |
| 415 | |
| 416 | for (size_t i = 0; i < resources.size(); ++i) { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 417 | const auto &res = resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 418 | const auto resType = std::tuple(res.type, res.subType, res.id); |
| 419 | |
| 420 | if (res.value < 0) { |
| 421 | ALOGW("Ignoring request to remove negative value of resource"); |
| 422 | continue; |
| 423 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 424 | // ignore if we don't have it |
| 425 | if (info.resources.find(resType) != info.resources.end()) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 426 | MediaResourceParcel &resource = info.resources[resType]; |
| 427 | if (resource.value > res.value) { |
| 428 | resource.value -= res.value; |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 429 | } else { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 430 | onLastRemoved(res, info); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 431 | info.resources.erase(resType); |
| 432 | } |
| 433 | } |
| 434 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 435 | return Status::ok(); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 438 | Status ResourceManagerService::removeClient(int32_t pid, int64_t clientId) { |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 439 | removeResource(pid, clientId, true); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 440 | return Status::ok(); |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 441 | } |
| 442 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 443 | Status ResourceManagerService::removeResource(int pid, int64_t clientId, bool checkValid) { |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 444 | String8 log = String8::format( |
| 445 | "removeResource(pid %d, clientId %lld)", |
| 446 | pid, (long long) clientId); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 447 | mServiceLog->add(log); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 448 | |
| 449 | Mutex::Autolock lock(mLock); |
Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 450 | if (checkValid && !mProcessInfo->isValidPid(pid)) { |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 451 | ALOGE("Rejected removeResource call with invalid pid."); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 452 | return Status::fromServiceSpecificError(BAD_VALUE); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 453 | } |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 454 | ssize_t index = mMap.indexOfKey(pid); |
| 455 | if (index < 0) { |
| 456 | ALOGV("removeResource: didn't find pid %d for clientId %lld", pid, (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 457 | return Status::ok(); |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 458 | } |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 459 | ResourceInfos &infos = mMap.editValueAt(index); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 460 | |
| 461 | index = infos.indexOfKey(clientId); |
| 462 | if (index < 0) { |
| 463 | ALOGV("removeResource: didn't find clientId %lld", (long long) clientId); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 464 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 465 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 466 | |
| 467 | const ResourceInfo &info = infos[index]; |
| 468 | for (auto it = info.resources.begin(); it != info.resources.end(); it++) { |
| 469 | onLastRemoved(it->second, info); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 470 | } |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 471 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 472 | AIBinder_unlinkToDeath(info.client->asBinder().get(), |
| 473 | mDeathRecipient.get(), info.deathNotifier.get()); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 474 | |
| 475 | infos.removeItemsAt(index); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 476 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 479 | void ResourceManagerService::getClientForResource_l( |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 480 | int callingPid, const MediaResourceParcel *res, |
| 481 | Vector<std::shared_ptr<IResourceManagerClient>> *clients) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 482 | if (res == NULL) { |
| 483 | return; |
| 484 | } |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 485 | std::shared_ptr<IResourceManagerClient> client; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 486 | if (getLowestPriorityBiggestClient_l(callingPid, res->type, &client)) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 487 | clients->push_back(client); |
| 488 | } |
| 489 | } |
| 490 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 491 | Status ResourceManagerService::reclaimResource( |
| 492 | int32_t callingPid, |
| 493 | const std::vector<MediaResourceParcel>& resources, |
| 494 | bool* _aidl_return) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 495 | String8 log = String8::format("reclaimResource(callingPid %d, resources %s)", |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 496 | callingPid, getString(resources).string()); |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 497 | mServiceLog->add(log); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 498 | *_aidl_return = false; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 499 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 500 | Vector<std::shared_ptr<IResourceManagerClient>> clients; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 501 | { |
| 502 | Mutex::Autolock lock(mLock); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 503 | if (!mProcessInfo->isValidPid(callingPid)) { |
| 504 | ALOGE("Rejected reclaimResource call with invalid callingPid."); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 505 | return Status::fromServiceSpecificError(BAD_VALUE); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 506 | } |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 507 | const MediaResourceParcel *secureCodec = NULL; |
| 508 | const MediaResourceParcel *nonSecureCodec = NULL; |
| 509 | const MediaResourceParcel *graphicMemory = NULL; |
| 510 | const MediaResourceParcel *drmSession = NULL; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 511 | for (size_t i = 0; i < resources.size(); ++i) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 512 | MediaResource::Type type = resources[i].type; |
| 513 | if (resources[i].type == MediaResource::Type::kSecureCodec) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 514 | secureCodec = &resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 515 | } else if (type == MediaResource::Type::kNonSecureCodec) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 516 | nonSecureCodec = &resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 517 | } else if (type == MediaResource::Type::kGraphicMemory) { |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 518 | graphicMemory = &resources[i]; |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 519 | } else if (type == MediaResource::Type::kDrmSession) { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 520 | drmSession = &resources[i]; |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
| 524 | // first pass to handle secure/non-secure codec conflict |
| 525 | if (secureCodec != NULL) { |
| 526 | if (!mSupportsMultipleSecureCodecs) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 527 | if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) { |
| 528 | return Status::ok(); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | if (!mSupportsSecureWithNonSecureCodec) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 532 | if (!getAllClients_l(callingPid, MediaResource::Type::kNonSecureCodec, &clients)) { |
| 533 | return Status::ok(); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | } |
| 537 | if (nonSecureCodec != NULL) { |
| 538 | if (!mSupportsSecureWithNonSecureCodec) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 539 | if (!getAllClients_l(callingPid, MediaResource::Type::kSecureCodec, &clients)) { |
| 540 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | } |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 544 | if (drmSession != NULL) { |
| 545 | getClientForResource_l(callingPid, drmSession, &clients); |
| 546 | if (clients.size() == 0) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 547 | return Status::ok(); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 548 | } |
| 549 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 550 | |
| 551 | if (clients.size() == 0) { |
| 552 | // if no secure/non-secure codec conflict, run second pass to handle other resources. |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 553 | getClientForResource_l(callingPid, graphicMemory, &clients); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 554 | } |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 555 | |
| 556 | if (clients.size() == 0) { |
| 557 | // if we are here, run the third pass to free one codec with the same type. |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 558 | getClientForResource_l(callingPid, secureCodec, &clients); |
| 559 | getClientForResource_l(callingPid, nonSecureCodec, &clients); |
| 560 | } |
| 561 | |
| 562 | if (clients.size() == 0) { |
| 563 | // if we are here, run the fourth pass to free one codec with the different type. |
| 564 | if (secureCodec != NULL) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 565 | MediaResource temp(MediaResource::Type::kNonSecureCodec, 1); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 566 | getClientForResource_l(callingPid, &temp, &clients); |
| 567 | } |
| 568 | if (nonSecureCodec != NULL) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 569 | MediaResource temp(MediaResource::Type::kSecureCodec, 1); |
Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 570 | getClientForResource_l(callingPid, &temp, &clients); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 571 | } |
| 572 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | if (clients.size() == 0) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 576 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 579 | std::shared_ptr<IResourceManagerClient> failedClient; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 580 | for (size_t i = 0; i < clients.size(); ++i) { |
Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 581 | log = String8::format("reclaimResource from client %p", clients[i].get()); |
| 582 | mServiceLog->add(log); |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 583 | bool success; |
| 584 | Status status = clients[i]->reclaimResource(&success); |
| 585 | if (!status.isOk() || !success) { |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 586 | failedClient = clients[i]; |
| 587 | break; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 588 | } |
| 589 | } |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 590 | |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 591 | if (failedClient == NULL) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 592 | *_aidl_return = true; |
| 593 | return Status::ok(); |
Ronghua Wu | 76d4c7f | 2015-10-23 15:01:53 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 596 | { |
| 597 | Mutex::Autolock lock(mLock); |
| 598 | bool found = false; |
| 599 | for (size_t i = 0; i < mMap.size(); ++i) { |
| 600 | ResourceInfos &infos = mMap.editValueAt(i); |
| 601 | for (size_t j = 0; j < infos.size();) { |
| 602 | if (infos[j].client == failedClient) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 603 | j = infos.removeItemsAt(j); |
Ronghua Wu | 67e7f54 | 2015-03-13 10:47:08 -0700 | [diff] [blame] | 604 | found = true; |
| 605 | } else { |
| 606 | ++j; |
| 607 | } |
| 608 | } |
| 609 | if (found) { |
| 610 | break; |
| 611 | } |
| 612 | } |
| 613 | if (!found) { |
| 614 | ALOGV("didn't find failed client"); |
| 615 | } |
| 616 | } |
| 617 | |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 618 | return Status::ok(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 621 | Status ResourceManagerService::overridePid( |
| 622 | int originalPid, |
| 623 | int newPid) { |
| 624 | String8 log = String8::format("overridePid(originalPid %d, newPid %d)", |
| 625 | originalPid, newPid); |
| 626 | mServiceLog->add(log); |
| 627 | |
| 628 | // allow if this is called from the same process or the process has |
| 629 | // permission. |
| 630 | if ((AIBinder_getCallingPid() != getpid()) && |
| 631 | (checkCallingPermission(String16( |
| 632 | "android.permission.MEDIA_RESOURCE_OVERRIDE_PID")) == false)) { |
| 633 | ALOGE( |
| 634 | "Permission Denial: can't access overridePid method from pid=%d, " |
| 635 | "self pid=%d\n", |
| 636 | AIBinder_getCallingPid(), getpid()); |
| 637 | return Status::fromServiceSpecificError(PERMISSION_DENIED); |
| 638 | } |
| 639 | |
| 640 | { |
| 641 | Mutex::Autolock lock(mLock); |
| 642 | mOverridePidMap.erase(originalPid); |
| 643 | if (newPid != -1) { |
| 644 | mOverridePidMap.emplace(originalPid, newPid); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | return Status::ok(); |
| 649 | } |
| 650 | |
| 651 | bool ResourceManagerService::getPriority_l(int pid, int* priority) { |
| 652 | int newPid = pid; |
| 653 | |
| 654 | if (mOverridePidMap.find(pid) != mOverridePidMap.end()) { |
| 655 | newPid = mOverridePidMap[pid]; |
| 656 | ALOGD("getPriority_l: use override pid %d instead original pid %d", |
| 657 | newPid, pid); |
| 658 | } |
| 659 | |
| 660 | return mProcessInfo->getPriority(newPid, priority); |
| 661 | } |
| 662 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 663 | bool ResourceManagerService::getAllClients_l( |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 664 | int callingPid, MediaResource::Type type, |
| 665 | Vector<std::shared_ptr<IResourceManagerClient>> *clients) { |
| 666 | Vector<std::shared_ptr<IResourceManagerClient>> temp; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 667 | for (size_t i = 0; i < mMap.size(); ++i) { |
| 668 | ResourceInfos &infos = mMap.editValueAt(i); |
| 669 | for (size_t j = 0; j < infos.size(); ++j) { |
| 670 | if (hasResourceType(type, infos[j].resources)) { |
| 671 | if (!isCallingPriorityHigher_l(callingPid, mMap.keyAt(i))) { |
| 672 | // some higher/equal priority process owns the resource, |
| 673 | // this request can't be fulfilled. |
| 674 | ALOGE("getAllClients_l: can't reclaim resource %s from pid %d", |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 675 | asString(type), mMap.keyAt(i)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 676 | return false; |
| 677 | } |
| 678 | temp.push_back(infos[j].client); |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | if (temp.size() == 0) { |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 683 | ALOGV("getAllClients_l: didn't find any resource %s", asString(type)); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 684 | return true; |
| 685 | } |
| 686 | clients->appendVector(temp); |
| 687 | return true; |
| 688 | } |
| 689 | |
| 690 | bool ResourceManagerService::getLowestPriorityBiggestClient_l( |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 691 | int callingPid, MediaResource::Type type, |
| 692 | std::shared_ptr<IResourceManagerClient> *client) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 693 | int lowestPriorityPid; |
| 694 | int lowestPriority; |
| 695 | int callingPriority; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 696 | if (!getPriority_l(callingPid, &callingPriority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 697 | ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d", |
| 698 | callingPid); |
| 699 | return false; |
| 700 | } |
| 701 | if (!getLowestPriorityPid_l(type, &lowestPriorityPid, &lowestPriority)) { |
| 702 | return false; |
| 703 | } |
| 704 | if (lowestPriority <= callingPriority) { |
| 705 | ALOGE("getLowestPriorityBiggestClient_l: lowest priority %d vs caller priority %d", |
| 706 | lowestPriority, callingPriority); |
| 707 | return false; |
| 708 | } |
| 709 | |
| 710 | if (!getBiggestClient_l(lowestPriorityPid, type, client)) { |
| 711 | return false; |
| 712 | } |
| 713 | return true; |
| 714 | } |
| 715 | |
| 716 | bool ResourceManagerService::getLowestPriorityPid_l( |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 717 | MediaResource::Type type, int *lowestPriorityPid, int *lowestPriority) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 718 | int pid = -1; |
| 719 | int priority = -1; |
| 720 | for (size_t i = 0; i < mMap.size(); ++i) { |
| 721 | if (mMap.valueAt(i).size() == 0) { |
| 722 | // no client on this process. |
| 723 | continue; |
| 724 | } |
| 725 | if (!hasResourceType(type, mMap.valueAt(i))) { |
| 726 | // doesn't have the requested resource type |
| 727 | continue; |
| 728 | } |
| 729 | int tempPid = mMap.keyAt(i); |
| 730 | int tempPriority; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 731 | if (!getPriority_l(tempPid, &tempPriority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 732 | ALOGV("getLowestPriorityPid_l: can't get priority of pid %d, skipped", tempPid); |
| 733 | // TODO: remove this pid from mMap? |
| 734 | continue; |
| 735 | } |
| 736 | if (pid == -1 || tempPriority > priority) { |
| 737 | // initial the value |
| 738 | pid = tempPid; |
| 739 | priority = tempPriority; |
| 740 | } |
| 741 | } |
| 742 | if (pid != -1) { |
| 743 | *lowestPriorityPid = pid; |
| 744 | *lowestPriority = priority; |
| 745 | } |
| 746 | return (pid != -1); |
| 747 | } |
| 748 | |
| 749 | bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid) { |
| 750 | int callingPidPriority; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 751 | if (!getPriority_l(callingPid, &callingPidPriority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 752 | return false; |
| 753 | } |
| 754 | |
| 755 | int priority; |
Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 756 | if (!getPriority_l(pid, &priority)) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 757 | return false; |
| 758 | } |
| 759 | |
| 760 | return (callingPidPriority < priority); |
| 761 | } |
| 762 | |
| 763 | bool ResourceManagerService::getBiggestClient_l( |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 764 | int pid, MediaResource::Type type, std::shared_ptr<IResourceManagerClient> *client) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 765 | ssize_t index = mMap.indexOfKey(pid); |
| 766 | if (index < 0) { |
| 767 | ALOGE("getBiggestClient_l: can't find resource info for pid %d", pid); |
| 768 | return false; |
| 769 | } |
| 770 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 771 | std::shared_ptr<IResourceManagerClient> clientTemp; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 772 | uint64_t largestValue = 0; |
| 773 | const ResourceInfos &infos = mMap.valueAt(index); |
| 774 | for (size_t i = 0; i < infos.size(); ++i) { |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 775 | const ResourceList &resources = infos[i].resources; |
| 776 | for (auto it = resources.begin(); it != resources.end(); it++) { |
Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 777 | const MediaResourceParcel &resource = it->second; |
| 778 | if (resource.type == type) { |
| 779 | if (resource.value > largestValue) { |
| 780 | largestValue = resource.value; |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 781 | clientTemp = infos[i].client; |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | if (clientTemp == NULL) { |
Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 788 | ALOGE("getBiggestClient_l: can't find resource type %s for pid %d", asString(type), pid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 789 | return false; |
| 790 | } |
| 791 | |
| 792 | *client = clientTemp; |
| 793 | return true; |
| 794 | } |
| 795 | |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 796 | } // namespace android |