blob: b5b9f866d94f1b60bcb4ecc360df40a811fa5f75 [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 Zhang181e6952019-10-09 13:23:39 -070021#include <android/media/BnResourceManagerService.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070022#include <arpa/inet.h>
23#include <binder/BinderService.h>
Chong Zhang181e6952019-10-09 13:23:39 -070024#include <media/MediaResource.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070025#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 Wu231c3d12015-03-11 15:10:32 -070031namespace android {
32
Ronghua Wua8ec8fc2015-05-07 13:58:22 -070033class ServiceLog;
Ronghua Wu231c3d12015-03-11 15:10:32 -070034struct ProcessInfoInterface;
35
Chong Zhang181e6952019-10-09 13:23:39 -070036namespace media {
37
38using android::binder::Status;
39
40typedef std::map<std::tuple<
41 MediaResource::Type, MediaResource::SubType, std::vector<uint8_t>>,
42 MediaResourceParcel> ResourceList;
43
Ronghua Wu231c3d12015-03-11 15:10:32 -070044struct ResourceInfo {
45 int64_t clientId;
Chong Zhangee33d642019-08-08 14:26:43 -070046 uid_t uid;
Ronghua Wu231c3d12015-03-11 15:10:32 -070047 sp<IResourceManagerClient> client;
Wonsik Kim3e378962017-01-05 17:00:02 +090048 sp<IBinder::DeathRecipient> deathNotifier;
Chong Zhangfb092d32019-08-12 09:45:44 -070049 ResourceList resources;
Ronghua Wu231c3d12015-03-11 15:10:32 -070050};
51
Chong Zhangfb092d32019-08-12 09:45:44 -070052// TODO: convert these to std::map
53typedef KeyedVector<int64_t, ResourceInfo> ResourceInfos;
Ronghua Wu231c3d12015-03-11 15:10:32 -070054typedef KeyedVector<int, ResourceInfos> PidResourceInfosMap;
55
56class ResourceManagerService
57 : public BinderService<ResourceManagerService>,
58 public BnResourceManagerService
59{
60public:
Chong Zhangdd726802019-08-21 17:24:13 -070061 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 Wu231c3d12015-03-11 15:10:32 -070069 static char const *getServiceName() { return "media.resource_manager"; }
70
Ronghua Wu8f9dd872015-04-23 15:24:25 -070071 virtual status_t dump(int fd, const Vector<String16>& args);
72
Ronghua Wu231c3d12015-03-11 15:10:32 -070073 ResourceManagerService();
Chong Zhangdd726802019-08-21 17:24:13 -070074 explicit ResourceManagerService(
75 const sp<ProcessInfoInterface> &processInfo,
76 const sp<SystemCallbackInterface> &systemResource);
Ronghua Wu231c3d12015-03-11 15:10:32 -070077
78 // IResourceManagerService interface
Chong Zhang181e6952019-10-09 13:23:39 -070079 Status config(const std::vector<MediaResourcePolicyParcel>& policies) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -070080
Chong Zhang181e6952019-10-09 13:23:39 -070081 Status addResource(
82 int32_t pid,
83 int32_t uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -070084 int64_t clientId,
Chong Zhang181e6952019-10-09 13:23:39 -070085 const sp<IResourceManagerClient>& client,
86 const std::vector<MediaResourceParcel>& resources) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -070087
Chong Zhang181e6952019-10-09 13:23:39 -070088 Status removeResource(
89 int32_t pid,
90 int64_t clientId,
91 const std::vector<MediaResourceParcel>& resources) override;
Chong Zhangfb092d32019-08-12 09:45:44 -070092
Chong Zhang181e6952019-10-09 13:23:39 -070093 Status removeClient(int32_t pid, int64_t clientId) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -070094
Ronghua Wu05d89f12015-07-07 16:47:42 -070095 // 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 Zhang181e6952019-10-09 13:23:39 -070098 Status reclaimResource(
99 int32_t callingPid,
100 const std::vector<MediaResourceParcel>& resources,
101 bool* _aidl_return) override;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700102
Chong Zhang181e6952019-10-09 13:23:39 -0700103 Status removeResource(int pid, int64_t clientId, bool checkValid);
Wonsik Kim3e378962017-01-05 17:00:02 +0900104
Ronghua Wu231c3d12015-03-11 15:10:32 -0700105protected:
106 virtual ~ResourceManagerService();
107
108private:
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 Wuea15fd22016-03-03 13:35:05 -0800114 bool getAllClients_l(int callingPid, MediaResource::Type type,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700115 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 Wuea15fd22016-03-03 13:35:05 -0800120 bool getLowestPriorityBiggestClient_l(int callingPid, MediaResource::Type type,
Ronghua Wu231c3d12015-03-11 15:10:32 -0700121 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 Wuea15fd22016-03-03 13:35:05 -0800125 bool getLowestPriorityPid_l(MediaResource::Type type, int *pid, int *priority);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700126
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 Wuea15fd22016-03-03 13:35:05 -0800129 bool getBiggestClient_l(int pid, MediaResource::Type type, sp<IResourceManagerClient> *client);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700130
131 bool isCallingPriorityHigher_l(int callingPid, int pid);
132
Ronghua Wu05d89f12015-07-07 16:47:42 -0700133 // A helper function basically calls getLowestPriorityBiggestClient_l and add the result client
134 // to the given Vector.
Chong Zhang181e6952019-10-09 13:23:39 -0700135 void getClientForResource_l(int callingPid,
136 const MediaResourceParcel *res, Vector<sp<IResourceManagerClient>> *clients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700137
Chong Zhang181e6952019-10-09 13:23:39 -0700138 void onFirstAdded(const MediaResourceParcel& res, const ResourceInfo& clientInfo);
139 void onLastRemoved(const MediaResourceParcel& res, const ResourceInfo& clientInfo);
Chong Zhangfb092d32019-08-12 09:45:44 -0700140
Robert Shihc3af31b2019-09-20 21:45:01 -0700141 // Merge r2 into r1
Chong Zhang181e6952019-10-09 13:23:39 -0700142 void mergeResources(MediaResourceParcel& r1, const MediaResourceParcel& r2);
Robert Shihc3af31b2019-09-20 21:45:01 -0700143
Ronghua Wu231c3d12015-03-11 15:10:32 -0700144 mutable Mutex mLock;
145 sp<ProcessInfoInterface> mProcessInfo;
Chong Zhangdd726802019-08-21 17:24:13 -0700146 sp<SystemCallbackInterface> mSystemCB;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700147 sp<ServiceLog> mServiceLog;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700148 PidResourceInfosMap mMap;
149 bool mSupportsMultipleSecureCodecs;
150 bool mSupportsSecureWithNonSecureCodec;
Chong Zhang79d2b282018-04-17 14:14:31 -0700151 int32_t mCpuBoostCount;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700152};
153
154// ----------------------------------------------------------------------------
Chong Zhang181e6952019-10-09 13:23:39 -0700155} // namespace media
156} // namespace android
Ronghua Wu231c3d12015-03-11 15:10:32 -0700157
Chong Zhang181e6952019-10-09 13:23:39 -0700158#endif // ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H