blob: 95a2d1c51cab27d16c380d00c45cee6e5b38d85e [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
18//#define LOG_NDEBUG 0
19#define LOG_TAG "IResourceManagerService"
20#include <utils/Log.h>
21
22#include "media/IResourceManagerService.h"
23
24#include <binder/Parcel.h>
25
26#include <stdint.h>
27#include <sys/types.h>
28
29namespace android {
30
31enum {
32 CONFIG = IBinder::FIRST_CALL_TRANSACTION,
33 ADD_RESOURCE,
34 REMOVE_RESOURCE,
35 RECLAIM_RESOURCE,
36};
37
38template <typename T>
39static void writeToParcel(Parcel *data, const Vector<T> &items) {
40 size_t size = items.size();
41 size_t sizePosition = data->dataPosition();
42 // truncates size, but should be okay for this usecase
43 data->writeUint32(static_cast<uint32_t>(size));
44 for (size_t i = 0; i < size; i++) {
45 size_t position = data->dataPosition();
46 items[i].writeToParcel(data);
47 }
48}
49
50template <typename T>
51static void readFromParcel(const Parcel &data, Vector<T> *items) {
52 size_t size = (size_t)data.readUint32();
53 for (size_t i = 0; i < size; i++) {
54 T item;
55 item.readFromParcel(data);
56 items->add(item);
57 }
58}
59
60class BpResourceManagerService : public BpInterface<IResourceManagerService>
61{
62public:
63 BpResourceManagerService(const sp<IBinder> &impl)
64 : BpInterface<IResourceManagerService>(impl)
65 {
66 }
67
68 virtual void config(const Vector<MediaResourcePolicy> &policies) {
69 Parcel data, reply;
70 data.writeInterfaceToken(IResourceManagerService::getInterfaceDescriptor());
71 writeToParcel(&data, policies);
72 remote()->transact(CONFIG, data, &reply);
73 }
74
75 virtual void addResource(
76 int pid,
77 int64_t clientId,
78 const sp<IResourceManagerClient> client,
79 const Vector<MediaResource> &resources) {
80 Parcel data, reply;
81 data.writeInterfaceToken(IResourceManagerService::getInterfaceDescriptor());
82 data.writeInt32(pid);
83 data.writeInt64(clientId);
84 data.writeStrongBinder(IInterface::asBinder(client));
85 writeToParcel(&data, resources);
86
87 remote()->transact(ADD_RESOURCE, data, &reply);
88 }
89
90 virtual void removeResource(int64_t clientId) {
91 Parcel data, reply;
92 data.writeInterfaceToken(IResourceManagerService::getInterfaceDescriptor());
93 data.writeInt64(clientId);
94
95 remote()->transact(REMOVE_RESOURCE, data, &reply);
96 }
97
98 virtual bool reclaimResource(int callingPid, const Vector<MediaResource> &resources) {
99 Parcel data, reply;
100 data.writeInterfaceToken(IResourceManagerService::getInterfaceDescriptor());
101 data.writeInt32(callingPid);
102 writeToParcel(&data, resources);
103
104 bool ret = false;
105 status_t status = remote()->transact(RECLAIM_RESOURCE, data, &reply);
106 if (status == NO_ERROR) {
107 ret = (bool)reply.readInt32();
108 }
109 return ret;
110 }
111};
112
113IMPLEMENT_META_INTERFACE(ResourceManagerService, "android.media.IResourceManagerService");
114
115// ----------------------------------------------------------------------
116
117
118status_t BnResourceManagerService::onTransact(
119 uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags)
120{
121 switch (code) {
122 case CONFIG: {
123 CHECK_INTERFACE(IResourceManagerService, data, reply);
124 int pid = data.readInt32();
125 sp<IResourceManagerClient> client(
126 interface_cast<IResourceManagerClient>(data.readStrongBinder()));
127 Vector<MediaResourcePolicy> policies;
128 readFromParcel(data, &policies);
129 config(policies);
130 return NO_ERROR;
131 } break;
132
133 case ADD_RESOURCE: {
134 CHECK_INTERFACE(IResourceManagerService, data, reply);
135 int pid = data.readInt32();
136 int64_t clientId = data.readInt64();
137 sp<IResourceManagerClient> client(
138 interface_cast<IResourceManagerClient>(data.readStrongBinder()));
139 Vector<MediaResource> resources;
140 readFromParcel(data, &resources);
141 addResource(pid, clientId, client, resources);
142 return NO_ERROR;
143 } break;
144
145 case REMOVE_RESOURCE: {
146 CHECK_INTERFACE(IResourceManagerService, data, reply);
147 int64_t clientId = data.readInt64();
148 removeResource(clientId);
149 return NO_ERROR;
150 } break;
151
152 case RECLAIM_RESOURCE: {
153 CHECK_INTERFACE(IResourceManagerService, data, reply);
154 int callingPid = data.readInt32();
155 Vector<MediaResource> resources;
156 readFromParcel(data, &resources);
157 bool ret = reclaimResource(callingPid, resources);
158 reply->writeInt32(ret);
159 return NO_ERROR;
160 } break;
161
162 default:
163 return BBinder::onTransact(code, data, reply, flags);
164 }
165}
166
167// ----------------------------------------------------------------------------
168
169}; // namespace android