blob: 7f18ed3d9e7671d6fe8cfc720aba9da3a95922fc [file] [log] [blame]
Ronghua Wu231c3d12015-03-11 15:10:32 -07001/*
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 Zhang181e6952019-10-09 13:23:39 -070018#ifndef ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H
19#define ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H
Ronghua Wu231c3d12015-03-11 15:10:32 -070020
Wonsik Kimd20e9362020-04-28 10:42:57 -070021#include <map>
22
Chong Zhangfdd512a2019-11-22 11:03:14 -080023#include <aidl/android/media/BnResourceManagerService.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070024#include <arpa/inet.h>
Chong Zhang181e6952019-10-09 13:23:39 -070025#include <media/MediaResource.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070026#include <utils/Errors.h>
27#include <utils/KeyedVector.h>
28#include <utils/String8.h>
29#include <utils/threads.h>
30#include <utils/Vector.h>
31
Ronghua Wu231c3d12015-03-11 15:10:32 -070032namespace android {
33
Chong Zhangfdd512a2019-11-22 11:03:14 -080034class DeathNotifier;
35class ResourceManagerService;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070036class ServiceLog;
Ronghua Wu231c3d12015-03-11 15:10:32 -070037struct ProcessInfoInterface;
38
Chong Zhangfdd512a2019-11-22 11:03:14 -080039using Status = ::ndk::ScopedAStatus;
40using ::aidl::android::media::IResourceManagerClient;
41using ::aidl::android::media::BnResourceManagerService;
42using ::aidl::android::media::MediaResourceParcel;
43using ::aidl::android::media::MediaResourcePolicyParcel;
Chong Zhang181e6952019-10-09 13:23:39 -070044
45typedef std::map<std::tuple<
Jooyung Han3d564ff2020-02-22 00:46:06 +090046 MediaResource::Type, MediaResource::SubType, std::vector<uint8_t>>,
Chong Zhang181e6952019-10-09 13:23:39 -070047 MediaResourceParcel> ResourceList;
48
Ronghua Wu231c3d12015-03-11 15:10:32 -070049struct ResourceInfo {
50 int64_t clientId;
Chong Zhangee33d642019-08-08 14:26:43 -070051 uid_t uid;
Chong Zhangfdd512a2019-11-22 11:03:14 -080052 std::shared_ptr<IResourceManagerClient> client;
53 sp<DeathNotifier> deathNotifier;
Chong Zhangfb092d32019-08-12 09:45:44 -070054 ResourceList resources;
Wonsik Kimd20e9362020-04-28 10:42:57 -070055 bool pendingRemoval{false};
Ronghua Wu231c3d12015-03-11 15:10:32 -070056};
57
Chong Zhangfb092d32019-08-12 09:45:44 -070058// TODO: convert these to std::map
59typedef KeyedVector<int64_t, ResourceInfo> ResourceInfos;
Ronghua Wu231c3d12015-03-11 15:10:32 -070060typedef KeyedVector<int, ResourceInfos> PidResourceInfosMap;
61
Chong Zhangfdd512a2019-11-22 11:03:14 -080062class DeathNotifier : public RefBase {
63public:
64 DeathNotifier(const std::shared_ptr<ResourceManagerService> &service,
65 int pid, int64_t clientId);
66
67 ~DeathNotifier() {}
68
69 // Implement death recipient
70 static void BinderDiedCallback(void* cookie);
71 void binderDied();
72
73private:
74 std::weak_ptr<ResourceManagerService> mService;
75 int mPid;
76 int64_t mClientId;
77};
78class ResourceManagerService : public BnResourceManagerService {
Ronghua Wu231c3d12015-03-11 15:10:32 -070079public:
Chong Zhangdd726802019-08-21 17:24:13 -070080 struct SystemCallbackInterface : public RefBase {
81 virtual void noteStartVideo(int uid) = 0;
82 virtual void noteStopVideo(int uid) = 0;
83 virtual void noteResetVideo() = 0;
Chong Zhangfdd512a2019-11-22 11:03:14 -080084 virtual bool requestCpusetBoost(bool enable) = 0;
Chong Zhangdd726802019-08-21 17:24:13 -070085 };
86
Ronghua Wu231c3d12015-03-11 15:10:32 -070087 static char const *getServiceName() { return "media.resource_manager"; }
Chong Zhangfdd512a2019-11-22 11:03:14 -080088 static void instantiate();
Ronghua Wu231c3d12015-03-11 15:10:32 -070089
Chong Zhangfdd512a2019-11-22 11:03:14 -080090 virtual inline binder_status_t dump(
91 int /*fd*/, const char** /*args*/, uint32_t /*numArgs*/);
Ronghua Wu8f9dd872015-04-23 15:24:25 -070092
Ronghua Wu231c3d12015-03-11 15:10:32 -070093 ResourceManagerService();
Chong Zhangdd726802019-08-21 17:24:13 -070094 explicit ResourceManagerService(
95 const sp<ProcessInfoInterface> &processInfo,
96 const sp<SystemCallbackInterface> &systemResource);
Chong Zhangfdd512a2019-11-22 11:03:14 -080097 virtual ~ResourceManagerService();
Ronghua Wu231c3d12015-03-11 15:10:32 -070098
99 // IResourceManagerService interface
Chong Zhang181e6952019-10-09 13:23:39 -0700100 Status config(const std::vector<MediaResourcePolicyParcel>& policies) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700101
Chong Zhang181e6952019-10-09 13:23:39 -0700102 Status addResource(
103 int32_t pid,
104 int32_t uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700105 int64_t clientId,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800106 const std::shared_ptr<IResourceManagerClient>& client,
Chong Zhang181e6952019-10-09 13:23:39 -0700107 const std::vector<MediaResourceParcel>& resources) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700108
Chong Zhang181e6952019-10-09 13:23:39 -0700109 Status removeResource(
110 int32_t pid,
111 int64_t clientId,
112 const std::vector<MediaResourceParcel>& resources) override;
Chong Zhangfb092d32019-08-12 09:45:44 -0700113
Chong Zhang181e6952019-10-09 13:23:39 -0700114 Status removeClient(int32_t pid, int64_t clientId) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700115
Ronghua Wu05d89f12015-07-07 16:47:42 -0700116 // Tries to reclaim resource from processes with lower priority than the calling process
117 // according to the requested resources.
118 // Returns true if any resource has been reclaimed, otherwise returns false.
Chong Zhang181e6952019-10-09 13:23:39 -0700119 Status reclaimResource(
120 int32_t callingPid,
121 const std::vector<MediaResourceParcel>& resources,
122 bool* _aidl_return) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700123
Henry Fang32762922020-01-28 18:40:39 -0800124 Status overridePid(
125 int originalPid,
126 int newPid) override;
127
Wonsik Kimd20e9362020-04-28 10:42:57 -0700128 Status markClientForPendingRemoval(int32_t pid, int64_t clientId) override;
129
Wonsik Kim271429d2020-10-01 10:12:56 -0700130 Status reclaimResourcesFromClientsPendingRemoval(int32_t pid) override;
131
Chong Zhang181e6952019-10-09 13:23:39 -0700132 Status removeResource(int pid, int64_t clientId, bool checkValid);
Wonsik Kim3e378962017-01-05 17:00:02 +0900133
Ronghua Wu231c3d12015-03-11 15:10:32 -0700134private:
135 friend class ResourceManagerServiceTest;
136
Wonsik Kim271429d2020-10-01 10:12:56 -0700137 // Reclaims resources from |clients|. Returns true if reclaim succeeded
138 // for all clients.
139 bool reclaimInternal(
140 const Vector<std::shared_ptr<IResourceManagerClient>> &clients);
141
Ronghua Wu231c3d12015-03-11 15:10:32 -0700142 // Gets the list of all the clients who own the specified resource type.
143 // Returns false if any client belongs to a process with higher priority than the
144 // calling process. The clients will remain unchanged if returns false.
Ronghua Wuea15fd22016-03-03 13:35:05 -0800145 bool getAllClients_l(int callingPid, MediaResource::Type type,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800146 Vector<std::shared_ptr<IResourceManagerClient>> *clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700147
148 // Gets the client who owns specified resource type from lowest possible priority process.
149 // Returns false if the calling process priority is not higher than the lowest process
150 // priority. The client will remain unchanged if returns false.
Ronghua Wuea15fd22016-03-03 13:35:05 -0800151 bool getLowestPriorityBiggestClient_l(int callingPid, MediaResource::Type type,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800152 std::shared_ptr<IResourceManagerClient> *client);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700153
154 // Gets lowest priority process that has the specified resource type.
155 // Returns false if failed. The output parameters will remain unchanged if failed.
Ronghua Wuea15fd22016-03-03 13:35:05 -0800156 bool getLowestPriorityPid_l(MediaResource::Type type, int *pid, int *priority);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700157
158 // Gets the client who owns biggest piece of specified resource type from pid.
159 // Returns false if failed. The client will remain unchanged if failed.
Chong Zhangfdd512a2019-11-22 11:03:14 -0800160 bool getBiggestClient_l(int pid, MediaResource::Type type,
Wonsik Kimd20e9362020-04-28 10:42:57 -0700161 std::shared_ptr<IResourceManagerClient> *client,
162 bool pendingRemovalOnly = false);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700163
164 bool isCallingPriorityHigher_l(int callingPid, int pid);
165
Chong Zhangfdd512a2019-11-22 11:03:14 -0800166 // A helper function basically calls getLowestPriorityBiggestClient_l and add
167 // the result client to the given Vector.
168 void getClientForResource_l(int callingPid, const MediaResourceParcel *res,
169 Vector<std::shared_ptr<IResourceManagerClient>> *clients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700170
Chong Zhang181e6952019-10-09 13:23:39 -0700171 void onFirstAdded(const MediaResourceParcel& res, const ResourceInfo& clientInfo);
172 void onLastRemoved(const MediaResourceParcel& res, const ResourceInfo& clientInfo);
Chong Zhangfb092d32019-08-12 09:45:44 -0700173
Robert Shihc3af31b2019-09-20 21:45:01 -0700174 // Merge r2 into r1
Chong Zhang181e6952019-10-09 13:23:39 -0700175 void mergeResources(MediaResourceParcel& r1, const MediaResourceParcel& r2);
Robert Shihc3af31b2019-09-20 21:45:01 -0700176
Henry Fang32762922020-01-28 18:40:39 -0800177 // Get priority from process's pid
178 bool getPriority_l(int pid, int* priority);
179
Ronghua Wu231c3d12015-03-11 15:10:32 -0700180 mutable Mutex mLock;
181 sp<ProcessInfoInterface> mProcessInfo;
Chong Zhangdd726802019-08-21 17:24:13 -0700182 sp<SystemCallbackInterface> mSystemCB;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700183 sp<ServiceLog> mServiceLog;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700184 PidResourceInfosMap mMap;
185 bool mSupportsMultipleSecureCodecs;
186 bool mSupportsSecureWithNonSecureCodec;
Chong Zhang79d2b282018-04-17 14:14:31 -0700187 int32_t mCpuBoostCount;
Chong Zhangfdd512a2019-11-22 11:03:14 -0800188 ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
Henry Fang32762922020-01-28 18:40:39 -0800189 std::map<int, int> mOverridePidMap;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700190};
191
192// ----------------------------------------------------------------------------
Chong Zhang181e6952019-10-09 13:23:39 -0700193} // namespace android
Ronghua Wu231c3d12015-03-11 15:10:32 -0700194
Chong Zhang181e6952019-10-09 13:23:39 -0700195#endif // ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H