blob: ae12d7bf2d237ec23a5999f7691c60480659d42e [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
Chong Zhang181e6952019-10-09 13:23:39 -0700121 Status removeResource(int pid, int64_t clientId, bool checkValid);
Wonsik Kim3e378962017-01-05 17:00:02 +0900122
Ronghua Wu231c3d12015-03-11 15:10:32 -0700123private:
124 friend class ResourceManagerServiceTest;
125
126 // Gets the list of all the clients who own the specified resource type.
127 // Returns false if any client belongs to a process with higher priority than the
128 // calling process. The clients will remain unchanged if returns false.
Ronghua Wuea15fd22016-03-03 13:35:05 -0800129 bool getAllClients_l(int callingPid, MediaResource::Type type,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800130 Vector<std::shared_ptr<IResourceManagerClient>> *clients);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700131
132 // Gets the client who owns specified resource type from lowest possible priority process.
133 // Returns false if the calling process priority is not higher than the lowest process
134 // priority. The client will remain unchanged if returns false.
Ronghua Wuea15fd22016-03-03 13:35:05 -0800135 bool getLowestPriorityBiggestClient_l(int callingPid, MediaResource::Type type,
Chong Zhangfdd512a2019-11-22 11:03:14 -0800136 std::shared_ptr<IResourceManagerClient> *client);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700137
138 // Gets lowest priority process that has the specified resource type.
139 // Returns false if failed. The output parameters will remain unchanged if failed.
Ronghua Wuea15fd22016-03-03 13:35:05 -0800140 bool getLowestPriorityPid_l(MediaResource::Type type, int *pid, int *priority);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700141
142 // Gets the client who owns biggest piece of specified resource type from pid.
143 // Returns false if failed. The client will remain unchanged if failed.
Chong Zhangfdd512a2019-11-22 11:03:14 -0800144 bool getBiggestClient_l(int pid, MediaResource::Type type,
145 std::shared_ptr<IResourceManagerClient> *client);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700146
147 bool isCallingPriorityHigher_l(int callingPid, int pid);
148
Chong Zhangfdd512a2019-11-22 11:03:14 -0800149 // A helper function basically calls getLowestPriorityBiggestClient_l and add
150 // the result client to the given Vector.
151 void getClientForResource_l(int callingPid, const MediaResourceParcel *res,
152 Vector<std::shared_ptr<IResourceManagerClient>> *clients);
Ronghua Wu05d89f12015-07-07 16:47:42 -0700153
Chong Zhang181e6952019-10-09 13:23:39 -0700154 void onFirstAdded(const MediaResourceParcel& res, const ResourceInfo& clientInfo);
155 void onLastRemoved(const MediaResourceParcel& res, const ResourceInfo& clientInfo);
Chong Zhangfb092d32019-08-12 09:45:44 -0700156
Robert Shihc3af31b2019-09-20 21:45:01 -0700157 // Merge r2 into r1
Chong Zhang181e6952019-10-09 13:23:39 -0700158 void mergeResources(MediaResourceParcel& r1, const MediaResourceParcel& r2);
Robert Shihc3af31b2019-09-20 21:45:01 -0700159
Ronghua Wu231c3d12015-03-11 15:10:32 -0700160 mutable Mutex mLock;
161 sp<ProcessInfoInterface> mProcessInfo;
Chong Zhangdd726802019-08-21 17:24:13 -0700162 sp<SystemCallbackInterface> mSystemCB;
Ronghua Wua8ec8fc2015-05-07 13:58:22 -0700163 sp<ServiceLog> mServiceLog;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700164 PidResourceInfosMap mMap;
165 bool mSupportsMultipleSecureCodecs;
166 bool mSupportsSecureWithNonSecureCodec;
Chong Zhang79d2b282018-04-17 14:14:31 -0700167 int32_t mCpuBoostCount;
Chong Zhangfdd512a2019-11-22 11:03:14 -0800168 ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
Ronghua Wu231c3d12015-03-11 15:10:32 -0700169};
170
171// ----------------------------------------------------------------------------
Chong Zhang181e6952019-10-09 13:23:39 -0700172} // namespace android
Ronghua Wu231c3d12015-03-11 15:10:32 -0700173
Chong Zhang181e6952019-10-09 13:23:39 -0700174#endif // ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H