blob: 1cf987adfd3d0aceafa4b094aacf3a48ab4edfee [file] [log] [blame]
Andreas Huber1b19c9d2012-08-29 11:34:22 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "IHDCP"
19#include <utils/Log.h>
20
21#include <binder/Parcel.h>
22#include <media/IHDCP.h>
23#include <media/stagefright/MediaErrors.h>
24#include <media/stagefright/foundation/ADebug.h>
25
26namespace android {
27
28enum {
29 OBSERVER_NOTIFY = IBinder::FIRST_CALL_TRANSACTION,
30 HDCP_SET_OBSERVER,
31 HDCP_INIT_ASYNC,
32 HDCP_SHUTDOWN_ASYNC,
Chong Zhangec3acca2013-09-03 14:35:37 -070033 HDCP_GET_CAPS,
Andreas Huber1b19c9d2012-08-29 11:34:22 -070034 HDCP_ENCRYPT,
Chong Zhang308bcaa2013-05-03 21:54:17 -070035 HDCP_ENCRYPT_NATIVE,
Andreas Hubera6a88d92013-01-30 10:41:25 -080036 HDCP_DECRYPT,
Andreas Huber1b19c9d2012-08-29 11:34:22 -070037};
38
39struct BpHDCPObserver : public BpInterface<IHDCPObserver> {
40 BpHDCPObserver(const sp<IBinder> &impl)
41 : BpInterface<IHDCPObserver>(impl) {
42 }
43
44 virtual void notify(
45 int msg, int ext1, int ext2, const Parcel *obj) {
46 Parcel data, reply;
47 data.writeInterfaceToken(IHDCPObserver::getInterfaceDescriptor());
48 data.writeInt32(msg);
49 data.writeInt32(ext1);
50 data.writeInt32(ext2);
51 if (obj && obj->dataSize() > 0) {
52 data.appendFrom(const_cast<Parcel *>(obj), 0, obj->dataSize());
53 }
54 remote()->transact(OBSERVER_NOTIFY, data, &reply, IBinder::FLAG_ONEWAY);
55 }
56};
57
58IMPLEMENT_META_INTERFACE(HDCPObserver, "android.hardware.IHDCPObserver");
59
60struct BpHDCP : public BpInterface<IHDCP> {
61 BpHDCP(const sp<IBinder> &impl)
62 : BpInterface<IHDCP>(impl) {
63 }
64
65 virtual status_t setObserver(const sp<IHDCPObserver> &observer) {
66 Parcel data, reply;
67 data.writeInterfaceToken(IHDCP::getInterfaceDescriptor());
68 data.writeStrongBinder(observer->asBinder());
69 remote()->transact(HDCP_SET_OBSERVER, data, &reply);
70 return reply.readInt32();
71 }
72
73 virtual status_t initAsync(const char *host, unsigned port) {
74 Parcel data, reply;
75 data.writeInterfaceToken(IHDCP::getInterfaceDescriptor());
76 data.writeCString(host);
77 data.writeInt32(port);
78 remote()->transact(HDCP_INIT_ASYNC, data, &reply);
79 return reply.readInt32();
80 }
81
82 virtual status_t shutdownAsync() {
83 Parcel data, reply;
84 data.writeInterfaceToken(IHDCP::getInterfaceDescriptor());
85 remote()->transact(HDCP_SHUTDOWN_ASYNC, data, &reply);
86 return reply.readInt32();
87 }
88
Chong Zhangec3acca2013-09-03 14:35:37 -070089 virtual uint32_t getCaps() {
90 Parcel data, reply;
91 data.writeInterfaceToken(IHDCP::getInterfaceDescriptor());
92 remote()->transact(HDCP_GET_CAPS, data, &reply);
93 return reply.readInt32();
94 }
95
Andreas Huber1b19c9d2012-08-29 11:34:22 -070096 virtual status_t encrypt(
97 const void *inData, size_t size, uint32_t streamCTR,
98 uint64_t *outInputCTR, void *outData) {
99 Parcel data, reply;
100 data.writeInterfaceToken(IHDCP::getInterfaceDescriptor());
101 data.writeInt32(size);
102 data.write(inData, size);
103 data.writeInt32(streamCTR);
104 remote()->transact(HDCP_ENCRYPT, data, &reply);
105
106 status_t err = reply.readInt32();
107
108 if (err != OK) {
109 *outInputCTR = 0;
110
111 return err;
112 }
113
114 *outInputCTR = reply.readInt64();
115 reply.read(outData, size);
116
117 return err;
118 }
Andreas Hubera6a88d92013-01-30 10:41:25 -0800119
Chong Zhang308bcaa2013-05-03 21:54:17 -0700120 virtual status_t encryptNative(
121 const sp<GraphicBuffer> &graphicBuffer,
122 size_t offset, size_t size, uint32_t streamCTR,
123 uint64_t *outInputCTR, void *outData) {
124 Parcel data, reply;
125 data.writeInterfaceToken(IHDCP::getInterfaceDescriptor());
126 data.write(*graphicBuffer);
127 data.writeInt32(offset);
128 data.writeInt32(size);
129 data.writeInt32(streamCTR);
130 remote()->transact(HDCP_ENCRYPT_NATIVE, data, &reply);
131
132 status_t err = reply.readInt32();
133
134 if (err != OK) {
135 *outInputCTR = 0;
136 return err;
137 }
138
139 *outInputCTR = reply.readInt64();
140 reply.read(outData, size);
141
142 return err;
143 }
144
Andreas Hubera6a88d92013-01-30 10:41:25 -0800145 virtual status_t decrypt(
146 const void *inData, size_t size,
147 uint32_t streamCTR, uint64_t inputCTR,
148 void *outData) {
149 Parcel data, reply;
150 data.writeInterfaceToken(IHDCP::getInterfaceDescriptor());
151 data.writeInt32(size);
152 data.write(inData, size);
153 data.writeInt32(streamCTR);
154 data.writeInt64(inputCTR);
155 remote()->transact(HDCP_DECRYPT, data, &reply);
156
157 status_t err = reply.readInt32();
158
159 if (err != OK) {
160 return err;
161 }
162
163 reply.read(outData, size);
164
165 return err;
166 }
Andreas Huber1b19c9d2012-08-29 11:34:22 -0700167};
168
169IMPLEMENT_META_INTERFACE(HDCP, "android.hardware.IHDCP");
170
171status_t BnHDCPObserver::onTransact(
172 uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags) {
173 switch (code) {
174 case OBSERVER_NOTIFY:
175 {
176 CHECK_INTERFACE(IHDCPObserver, data, reply);
177
178 int msg = data.readInt32();
179 int ext1 = data.readInt32();
180 int ext2 = data.readInt32();
181
182 Parcel obj;
183 if (data.dataAvail() > 0) {
184 obj.appendFrom(
185 const_cast<Parcel *>(&data),
186 data.dataPosition(),
187 data.dataAvail());
188 }
189
190 notify(msg, ext1, ext2, &obj);
191
192 return OK;
193 }
194
195 default:
196 return BBinder::onTransact(code, data, reply, flags);
197 }
198}
199
200status_t BnHDCP::onTransact(
201 uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags) {
202 switch (code) {
203 case HDCP_SET_OBSERVER:
204 {
205 CHECK_INTERFACE(IHDCP, data, reply);
206
207 sp<IHDCPObserver> observer =
208 interface_cast<IHDCPObserver>(data.readStrongBinder());
209
210 reply->writeInt32(setObserver(observer));
211 return OK;
212 }
213
214 case HDCP_INIT_ASYNC:
215 {
216 CHECK_INTERFACE(IHDCP, data, reply);
217
218 const char *host = data.readCString();
219 unsigned port = data.readInt32();
220
221 reply->writeInt32(initAsync(host, port));
222 return OK;
223 }
224
225 case HDCP_SHUTDOWN_ASYNC:
226 {
227 CHECK_INTERFACE(IHDCP, data, reply);
228
229 reply->writeInt32(shutdownAsync());
230 return OK;
231 }
232
Chong Zhangec3acca2013-09-03 14:35:37 -0700233 case HDCP_GET_CAPS:
234 {
235 CHECK_INTERFACE(IHDCP, data, reply);
236
237 reply->writeInt32(getCaps());
238 return OK;
239 }
240
Andreas Huber1b19c9d2012-08-29 11:34:22 -0700241 case HDCP_ENCRYPT:
242 {
243 size_t size = data.readInt32();
244
245 void *inData = malloc(2 * size);
246 void *outData = (uint8_t *)inData + size;
247
248 data.read(inData, size);
249
250 uint32_t streamCTR = data.readInt32();
251 uint64_t inputCTR;
252 status_t err = encrypt(inData, size, streamCTR, &inputCTR, outData);
253
254 reply->writeInt32(err);
255
256 if (err == OK) {
257 reply->writeInt64(inputCTR);
258 reply->write(outData, size);
259 }
260
261 free(inData);
262 inData = outData = NULL;
263
264 return OK;
265 }
266
Chong Zhang308bcaa2013-05-03 21:54:17 -0700267 case HDCP_ENCRYPT_NATIVE:
268 {
269 CHECK_INTERFACE(IHDCP, data, reply);
270
271 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer();
272 data.read(*graphicBuffer);
273 size_t offset = data.readInt32();
274 size_t size = data.readInt32();
275 uint32_t streamCTR = data.readInt32();
276 void *outData = malloc(size);
277 uint64_t inputCTR;
278
279 status_t err = encryptNative(graphicBuffer, offset, size,
280 streamCTR, &inputCTR, outData);
281
282 reply->writeInt32(err);
283
284 if (err == OK) {
285 reply->writeInt64(inputCTR);
286 reply->write(outData, size);
287 }
288
289 free(outData);
290 outData = NULL;
291
292 return OK;
293 }
294
Andreas Hubera6a88d92013-01-30 10:41:25 -0800295 case HDCP_DECRYPT:
296 {
297 size_t size = data.readInt32();
298
299 void *inData = malloc(2 * size);
300 void *outData = (uint8_t *)inData + size;
301
302 data.read(inData, size);
303
304 uint32_t streamCTR = data.readInt32();
305 uint64_t inputCTR = data.readInt64();
306 status_t err = decrypt(inData, size, streamCTR, inputCTR, outData);
307
308 reply->writeInt32(err);
309
310 if (err == OK) {
311 reply->write(outData, size);
312 }
313
314 free(inData);
315 inData = outData = NULL;
316
317 return OK;
318 }
319
Andreas Huber1b19c9d2012-08-29 11:34:22 -0700320 default:
321 return BBinder::onTransact(code, data, reply, flags);
322 }
323}
324
325} // namespace android