| 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 |  | 
| Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 18 | #ifndef ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H | 
 | 19 | #define ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 20 |  | 
| Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 21 | #include <aidl/android/media/BnResourceManagerService.h> | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 22 | #include <arpa/inet.h> | 
| Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 23 | #include <media/MediaResource.h> | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 24 | #include <utils/Errors.h> | 
 | 25 | #include <utils/KeyedVector.h> | 
 | 26 | #include <utils/String8.h> | 
 | 27 | #include <utils/threads.h> | 
 | 28 | #include <utils/Vector.h> | 
 | 29 |  | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 30 | namespace android { | 
 | 31 |  | 
| Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 32 | class DeathNotifier; | 
 | 33 | class ResourceManagerService; | 
| Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 34 | class ServiceLog; | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 35 | struct ProcessInfoInterface; | 
 | 36 |  | 
| Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 37 | using Status = ::ndk::ScopedAStatus; | 
 | 38 | using ::aidl::android::media::IResourceManagerClient; | 
 | 39 | using ::aidl::android::media::BnResourceManagerService; | 
 | 40 | using ::aidl::android::media::MediaResourceParcel; | 
 | 41 | using ::aidl::android::media::MediaResourcePolicyParcel; | 
| Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 42 |  | 
 | 43 | typedef std::map<std::tuple< | 
| Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 44 |         MediaResource::Type, MediaResource::SubType, std::vector<int8_t>>, | 
| Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 45 |         MediaResourceParcel> ResourceList; | 
 | 46 |  | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 47 | struct ResourceInfo { | 
 | 48 |     int64_t clientId; | 
| Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 49 |     uid_t uid; | 
| Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 50 |     std::shared_ptr<IResourceManagerClient> client; | 
 | 51 |     sp<DeathNotifier> deathNotifier; | 
| Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 52 |     ResourceList resources; | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 53 | }; | 
 | 54 |  | 
| Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 55 | // TODO: convert these to std::map | 
 | 56 | typedef KeyedVector<int64_t, ResourceInfo> ResourceInfos; | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 57 | typedef KeyedVector<int, ResourceInfos> PidResourceInfosMap; | 
 | 58 |  | 
| Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 59 | class DeathNotifier : public RefBase { | 
 | 60 | public: | 
 | 61 |     DeathNotifier(const std::shared_ptr<ResourceManagerService> &service, | 
 | 62 |             int pid, int64_t clientId); | 
 | 63 |  | 
 | 64 |     ~DeathNotifier() {} | 
 | 65 |  | 
 | 66 |     // Implement death recipient | 
 | 67 |     static void BinderDiedCallback(void* cookie); | 
 | 68 |     void binderDied(); | 
 | 69 |  | 
 | 70 | private: | 
 | 71 |     std::weak_ptr<ResourceManagerService> mService; | 
 | 72 |     int mPid; | 
 | 73 |     int64_t mClientId; | 
 | 74 | }; | 
 | 75 | class ResourceManagerService : public BnResourceManagerService { | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 76 | public: | 
| Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 77 |     struct SystemCallbackInterface : public RefBase { | 
 | 78 |         virtual void noteStartVideo(int uid) = 0; | 
 | 79 |         virtual void noteStopVideo(int uid) = 0; | 
 | 80 |         virtual void noteResetVideo() = 0; | 
| Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 81 |         virtual bool requestCpusetBoost(bool enable) = 0; | 
| Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 82 |     }; | 
 | 83 |  | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 84 |     static char const *getServiceName() { return "media.resource_manager"; } | 
| Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 85 |     static void instantiate(); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 86 |  | 
| Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 87 |     virtual inline binder_status_t dump( | 
 | 88 |             int /*fd*/, const char** /*args*/, uint32_t /*numArgs*/); | 
| Ronghua Wu | 8f9dd87 | 2015-04-23 15:24:25 -0700 | [diff] [blame] | 89 |  | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 90 |     ResourceManagerService(); | 
| Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 91 |     explicit ResourceManagerService( | 
 | 92 |             const sp<ProcessInfoInterface> &processInfo, | 
 | 93 |             const sp<SystemCallbackInterface> &systemResource); | 
| Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 94 |     virtual ~ResourceManagerService(); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 95 |  | 
 | 96 |     // IResourceManagerService interface | 
| Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 97 |     Status config(const std::vector<MediaResourcePolicyParcel>& policies) override; | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 98 |  | 
| Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 99 |     Status addResource( | 
 | 100 |             int32_t pid, | 
 | 101 |             int32_t uid, | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 102 |             int64_t clientId, | 
| Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 103 |             const std::shared_ptr<IResourceManagerClient>& client, | 
| Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 104 |             const std::vector<MediaResourceParcel>& resources) override; | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 105 |  | 
| Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 106 |     Status removeResource( | 
 | 107 |             int32_t pid, | 
 | 108 |             int64_t clientId, | 
 | 109 |             const std::vector<MediaResourceParcel>& resources) override; | 
| Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 110 |  | 
| Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 111 |     Status removeClient(int32_t pid, int64_t clientId) override; | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 112 |  | 
| Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 113 |     // Tries to reclaim resource from processes with lower priority than the calling process | 
 | 114 |     // according to the requested resources. | 
 | 115 |     // Returns true if any resource has been reclaimed, otherwise returns false. | 
| Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 116 |     Status reclaimResource( | 
 | 117 |             int32_t callingPid, | 
 | 118 |             const std::vector<MediaResourceParcel>& resources, | 
 | 119 |             bool* _aidl_return) override; | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 120 |  | 
| Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 121 |     Status overridePid( | 
 | 122 |             int originalPid, | 
 | 123 |             int newPid) override; | 
 | 124 |  | 
| Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 125 |     Status removeResource(int pid, int64_t clientId, bool checkValid); | 
| Wonsik Kim | 3e37896 | 2017-01-05 17:00:02 +0900 | [diff] [blame] | 126 |  | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 127 | private: | 
 | 128 |     friend class ResourceManagerServiceTest; | 
 | 129 |  | 
 | 130 |     // Gets the list of all the clients who own the specified resource type. | 
 | 131 |     // Returns false if any client belongs to a process with higher priority than the | 
 | 132 |     // calling process. The clients will remain unchanged if returns false. | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 133 |     bool getAllClients_l(int callingPid, MediaResource::Type type, | 
| Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 134 |             Vector<std::shared_ptr<IResourceManagerClient>> *clients); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 135 |  | 
 | 136 |     // Gets the client who owns specified resource type from lowest possible priority process. | 
 | 137 |     // Returns false if the calling process priority is not higher than the lowest process | 
 | 138 |     // priority. The client will remain unchanged if returns false. | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 139 |     bool getLowestPriorityBiggestClient_l(int callingPid, MediaResource::Type type, | 
| Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 140 |             std::shared_ptr<IResourceManagerClient> *client); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 141 |  | 
 | 142 |     // Gets lowest priority process that has the specified resource type. | 
 | 143 |     // Returns false if failed. The output parameters will remain unchanged if failed. | 
| Ronghua Wu | ea15fd2 | 2016-03-03 13:35:05 -0800 | [diff] [blame] | 144 |     bool getLowestPriorityPid_l(MediaResource::Type type, int *pid, int *priority); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 145 |  | 
 | 146 |     // Gets the client who owns biggest piece of specified resource type from pid. | 
 | 147 |     // Returns false if failed. The client will remain unchanged if failed. | 
| Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 148 |     bool getBiggestClient_l(int pid, MediaResource::Type type, | 
| Lajos Molnar | 0dfdf93 | 2020-03-09 21:22:44 +0000 | [diff] [blame^] | 149 |             std::shared_ptr<IResourceManagerClient> *client); | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 150 |  | 
 | 151 |     bool isCallingPriorityHigher_l(int callingPid, int pid); | 
 | 152 |  | 
| Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 153 |     // A helper function basically calls getLowestPriorityBiggestClient_l and add | 
 | 154 |     // the result client to the given Vector. | 
 | 155 |     void getClientForResource_l(int callingPid, const MediaResourceParcel *res, | 
 | 156 |             Vector<std::shared_ptr<IResourceManagerClient>> *clients); | 
| Ronghua Wu | 05d89f1 | 2015-07-07 16:47:42 -0700 | [diff] [blame] | 157 |  | 
| Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 158 |     void onFirstAdded(const MediaResourceParcel& res, const ResourceInfo& clientInfo); | 
 | 159 |     void onLastRemoved(const MediaResourceParcel& res, const ResourceInfo& clientInfo); | 
| Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 160 |  | 
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 161 |     // Merge r2 into r1 | 
| Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 162 |     void mergeResources(MediaResourceParcel& r1, const MediaResourceParcel& r2); | 
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 163 |  | 
| Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 164 |     // Get priority from process's pid | 
 | 165 |     bool getPriority_l(int pid, int* priority); | 
 | 166 |  | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 167 |     mutable Mutex mLock; | 
 | 168 |     sp<ProcessInfoInterface> mProcessInfo; | 
| Chong Zhang | dd72680 | 2019-08-21 17:24:13 -0700 | [diff] [blame] | 169 |     sp<SystemCallbackInterface> mSystemCB; | 
| Ronghua Wu | a8ec8fc | 2015-05-07 13:58:22 -0700 | [diff] [blame] | 170 |     sp<ServiceLog> mServiceLog; | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 171 |     PidResourceInfosMap mMap; | 
 | 172 |     bool mSupportsMultipleSecureCodecs; | 
 | 173 |     bool mSupportsSecureWithNonSecureCodec; | 
| Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 174 |     int32_t mCpuBoostCount; | 
| Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 175 |     ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient; | 
| Henry Fang | 3276292 | 2020-01-28 18:40:39 -0800 | [diff] [blame] | 176 |     std::map<int, int> mOverridePidMap; | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 177 | }; | 
 | 178 |  | 
 | 179 | // ---------------------------------------------------------------------------- | 
| Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 180 | } // namespace android | 
| Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 181 |  | 
| Chong Zhang | 181e695 | 2019-10-09 13:23:39 -0700 | [diff] [blame] | 182 | #endif // ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H |