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