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