blob: f500c62c7a298c86c4f89a9b4c52240da8ff8dcf [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
Chong Zhangfdd512a2019-11-22 11:03:14 -080021#include <aidl/android/media/BnResourceManagerService.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070022#include <arpa/inet.h>
Chong Zhang181e6952019-10-09 13:23:39 -070023#include <media/MediaResource.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070024#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 Wu231c3d12015-03-11 15:10:32 -070030namespace android {
31
Chong Zhangfdd512a2019-11-22 11:03:14 -080032class DeathNotifier;
33class ResourceManagerService;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070034class ServiceLog;
Ronghua Wu231c3d12015-03-11 15:10:32 -070035struct ProcessInfoInterface;
36
Chong Zhangfdd512a2019-11-22 11:03:14 -080037using Status = ::ndk::ScopedAStatus;
38using ::aidl::android::media::IResourceManagerClient;
39using ::aidl::android::media::BnResourceManagerService;
40using ::aidl::android::media::MediaResourceParcel;
41using ::aidl::android::media::MediaResourcePolicyParcel;
Chong Zhang181e6952019-10-09 13:23:39 -070042
43typedef std::map<std::tuple<
Chong Zhangfdd512a2019-11-22 11:03:14 -080044 MediaResource::Type, MediaResource::SubType, std::vector<int8_t>>,
Chong Zhang181e6952019-10-09 13:23:39 -070045 MediaResourceParcel> ResourceList;
46
Ronghua Wu231c3d12015-03-11 15:10:32 -070047struct ResourceInfo {
48 int64_t clientId;
Chong Zhangee33d642019-08-08 14:26:43 -070049 uid_t uid;
Chong Zhangfdd512a2019-11-22 11:03:14 -080050 std::shared_ptr<IResourceManagerClient> client;
51 sp<DeathNotifier> deathNotifier;
Chong Zhangfb092d32019-08-12 09:45:44 -070052 ResourceList resources;
Ronghua Wu231c3d12015-03-11 15:10:32 -070053};
54
Chong Zhangfb092d32019-08-12 09:45:44 -070055// TODO: convert these to std::map
56typedef KeyedVector<int64_t, ResourceInfo> ResourceInfos;
Ronghua Wu231c3d12015-03-11 15:10:32 -070057typedef KeyedVector<int, ResourceInfos> PidResourceInfosMap;
58
Chong Zhangfdd512a2019-11-22 11:03:14 -080059class DeathNotifier : public RefBase {
60public:
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
70private:
71 std::weak_ptr<ResourceManagerService> mService;
72 int mPid;
73 int64_t mClientId;
74};
75class ResourceManagerService : public BnResourceManagerService {
Ronghua Wu231c3d12015-03-11 15:10:32 -070076public:
Chong Zhangdd726802019-08-21 17:24:13 -070077 struct SystemCallbackInterface : public RefBase {
78 virtual void noteStartVideo(int uid) = 0;
79 virtual void noteStopVideo(int uid) = 0;
80 virtual void noteResetVideo() = 0;
Chong Zhangfdd512a2019-11-22 11:03:14 -080081 virtual bool requestCpusetBoost(bool enable) = 0;
Chong Zhangdd726802019-08-21 17:24:13 -070082 };
83
Ronghua Wu231c3d12015-03-11 15:10:32 -070084 static char const *getServiceName() { return "media.resource_manager"; }
Chong Zhangfdd512a2019-11-22 11:03:14 -080085 static void instantiate();
Ronghua Wu231c3d12015-03-11 15:10:32 -070086
Chong Zhangfdd512a2019-11-22 11:03:14 -080087 virtual inline binder_status_t dump(
88 int /*fd*/, const char** /*args*/, uint32_t /*numArgs*/);
Ronghua Wu8f9dd872015-04-23 15:24:25 -070089
Ronghua Wu231c3d12015-03-11 15:10:32 -070090 ResourceManagerService();
Chong Zhangdd726802019-08-21 17:24:13 -070091 explicit ResourceManagerService(
92 const sp<ProcessInfoInterface> &processInfo,
93 const sp<SystemCallbackInterface> &systemResource);
Chong Zhangfdd512a2019-11-22 11:03:14 -080094 virtual ~ResourceManagerService();
Ronghua Wu231c3d12015-03-11 15:10:32 -070095
96 // IResourceManagerService interface
Chong Zhang181e6952019-10-09 13:23:39 -070097 Status config(const std::vector<MediaResourcePolicyParcel>& policies) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -070098
Chong Zhang181e6952019-10-09 13:23:39 -070099 Status addResource(
100 int32_t pid,
101 int32_t uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700102 int64_t clientId,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800103 const std::shared_ptr<IResourceManagerClient>& client,
Chong Zhang181e6952019-10-09 13:23:39 -0700104 const std::vector<MediaResourceParcel>& resources) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700105
Chong Zhang181e6952019-10-09 13:23:39 -0700106 Status removeResource(
107 int32_t pid,
108 int64_t clientId,
109 const std::vector<MediaResourceParcel>& resources) override;
Chong Zhangfb092d32019-08-12 09:45:44 -0700110
Chong Zhang181e6952019-10-09 13:23:39 -0700111 Status removeClient(int32_t pid, int64_t clientId) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700112
Ronghua Wu05d89f12015-07-07 16:47:42 -0700113 // 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 Zhang181e6952019-10-09 13:23:39 -0700116 Status reclaimResource(
117 int32_t callingPid,
118 const std::vector<MediaResourceParcel>& resources,
119 bool* _aidl_return) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700120
Henry Fang32762922020-01-28 18:40:39 -0800121 Status overridePid(
122 int originalPid,
123 int newPid) override;
124
Chong Zhang181e6952019-10-09 13:23:39 -0700125 Status removeResource(int pid, int64_t clientId, bool checkValid);
Wonsik Kim3e378962017-01-05 17:00:02 +0900126
Ronghua Wu231c3d12015-03-11 15:10:32 -0700127private:
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 Wuea15fd22016-03-03 13:35:05 -0800133 bool getAllClients_l(int callingPid, MediaResource::Type type,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800134 Vector<std::shared_ptr<IResourceManagerClient>> *clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700135
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 Wuea15fd22016-03-03 13:35:05 -0800139 bool getLowestPriorityBiggestClient_l(int callingPid, MediaResource::Type type,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800140 std::shared_ptr<IResourceManagerClient> *client);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700141
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 Wuea15fd22016-03-03 13:35:05 -0800144 bool getLowestPriorityPid_l(MediaResource::Type type, int *pid, int *priority);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700145
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 Zhangfdd512a2019-11-22 11:03:14 -0800148 bool getBiggestClient_l(int pid, MediaResource::Type type,
Lajos Molnar0dfdf932020-03-09 21:22:44 +0000149 std::shared_ptr<IResourceManagerClient> *client);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700150
151 bool isCallingPriorityHigher_l(int callingPid, int pid);
152
Chong Zhangfdd512a2019-11-22 11:03:14 -0800153 // 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 Wu05d89f12015-07-07 16:47:42 -0700157
Chong Zhang181e6952019-10-09 13:23:39 -0700158 void onFirstAdded(const MediaResourceParcel& res, const ResourceInfo& clientInfo);
159 void onLastRemoved(const MediaResourceParcel& res, const ResourceInfo& clientInfo);
Chong Zhangfb092d32019-08-12 09:45:44 -0700160
Robert Shihc3af31b2019-09-20 21:45:01 -0700161 // Merge r2 into r1
Chong Zhang181e6952019-10-09 13:23:39 -0700162 void mergeResources(MediaResourceParcel& r1, const MediaResourceParcel& r2);
Robert Shihc3af31b2019-09-20 21:45:01 -0700163
Henry Fang32762922020-01-28 18:40:39 -0800164 // Get priority from process's pid
165 bool getPriority_l(int pid, int* priority);
166
Ronghua Wu231c3d12015-03-11 15:10:32 -0700167 mutable Mutex mLock;
168 sp<ProcessInfoInterface> mProcessInfo;
Chong Zhangdd726802019-08-21 17:24:13 -0700169 sp<SystemCallbackInterface> mSystemCB;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700170 sp<ServiceLog> mServiceLog;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700171 PidResourceInfosMap mMap;
172 bool mSupportsMultipleSecureCodecs;
173 bool mSupportsSecureWithNonSecureCodec;
Chong Zhang79d2b282018-04-17 14:14:31 -0700174 int32_t mCpuBoostCount;
Chong Zhangfdd512a2019-11-22 11:03:14 -0800175 ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
Henry Fang32762922020-01-28 18:40:39 -0800176 std::map<int, int> mOverridePidMap;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700177};
178
179// ----------------------------------------------------------------------------
Chong Zhang181e6952019-10-09 13:23:39 -0700180} // namespace android
Ronghua Wu231c3d12015-03-11 15:10:32 -0700181
Chong Zhang181e6952019-10-09 13:23:39 -0700182#endif // ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H