IDrmManagerService.cpp revision 09ed70fab1f1424971ccc105dcdf5be5ce2e2643
1/*
2 * Copyright (C) 2010 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 "IDrmManagerService(Native)"
19#include <utils/Log.h>
20
21#include <stdint.h>
22#include <sys/types.h>
23#include <binder/IPCThreadState.h>
24
25#include <drm/DrmInfo.h>
26#include <drm/DrmConstraints.h>
27#include <drm/DrmMetadata.h>
28#include <drm/DrmRights.h>
29#include <drm/DrmInfoStatus.h>
30#include <drm/DrmConvertedStatus.h>
31#include <drm/DrmInfoRequest.h>
32#include <drm/DrmSupportInfo.h>
33
34#include "IDrmManagerService.h"
35
36#define INVALID_BUFFER_LENGTH -1
37
38using namespace android;
39
40static void writeDecryptHandleToParcelData(
41        const DecryptHandle* handle, Parcel* data) {
42    data->writeInt32(handle->decryptId);
43    data->writeString8(handle->mimeType);
44    data->writeInt32(handle->decryptApiType);
45    data->writeInt32(handle->status);
46
47    int size = handle->copyControlVector.size();
48    data->writeInt32(size);
49    for (int i = 0; i < size; i++) {
50        data->writeInt32(handle->copyControlVector.keyAt(i));
51        data->writeInt32(handle->copyControlVector.valueAt(i));
52    }
53
54    size = handle->extendedData.size();
55    data->writeInt32(size);
56    for (int i = 0; i < size; i++) {
57        data->writeString8(handle->extendedData.keyAt(i));
58        data->writeString8(handle->extendedData.valueAt(i));
59    }
60
61    if (NULL != handle->decryptInfo) {
62        data->writeInt32(handle->decryptInfo->decryptBufferLength);
63    } else {
64        data->writeInt32(INVALID_BUFFER_LENGTH);
65    }
66}
67
68static void readDecryptHandleFromParcelData(
69        DecryptHandle* handle, const Parcel& data) {
70    if (0 == data.dataAvail()) {
71        return;
72    }
73
74    handle->decryptId = data.readInt32();
75    handle->mimeType = data.readString8();
76    handle->decryptApiType = data.readInt32();
77    handle->status = data.readInt32();
78
79    int size = data.readInt32();
80    for (int i = 0; i < size; i++) {
81        DrmCopyControl key = (DrmCopyControl)data.readInt32();
82        int value = data.readInt32();
83        handle->copyControlVector.add(key, value);
84    }
85
86    size = data.readInt32();
87    for (int i = 0; i < size; i++) {
88        String8 key = data.readString8();
89        String8 value = data.readString8();
90        handle->extendedData.add(key, value);
91    }
92
93    handle->decryptInfo = NULL;
94    const int bufferLen = data.readInt32();
95    if (INVALID_BUFFER_LENGTH != bufferLen) {
96        handle->decryptInfo = new DecryptInfo();
97        handle->decryptInfo->decryptBufferLength = bufferLen;
98    }
99}
100
101static void clearDecryptHandle(DecryptHandle* handle) {
102    if (handle == NULL) {
103        return;
104    }
105    if (handle->decryptInfo) {
106        delete handle->decryptInfo;
107        handle->decryptInfo = NULL;
108    }
109    handle->copyControlVector.clear();
110    handle->extendedData.clear();
111}
112
113int BpDrmManagerService::addUniqueId(bool isNative) {
114    ALOGV("add uniqueid");
115    Parcel data, reply;
116    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
117    data.writeInt32(isNative);
118    remote()->transact(ADD_UNIQUEID, data, &reply);
119    return reply.readInt32();
120}
121
122void BpDrmManagerService::removeUniqueId(int uniqueId) {
123    ALOGV("remove uniqueid");
124    Parcel data, reply;
125    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
126    data.writeInt32(uniqueId);
127    remote()->transact(REMOVE_UNIQUEID, data, &reply);
128}
129
130void BpDrmManagerService::addClient(int uniqueId) {
131    Parcel data, reply;
132    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
133    data.writeInt32(uniqueId);
134    remote()->transact(ADD_CLIENT, data, &reply);
135}
136
137void BpDrmManagerService::removeClient(int uniqueId) {
138    Parcel data, reply;
139    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
140    data.writeInt32(uniqueId);
141    remote()->transact(REMOVE_CLIENT, data, &reply);
142}
143
144status_t BpDrmManagerService::setDrmServiceListener(
145            int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
146    ALOGV("setDrmServiceListener");
147    Parcel data, reply;
148
149    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
150    data.writeInt32(uniqueId);
151    data.writeStrongBinder(drmServiceListener->asBinder());
152    remote()->transact(SET_DRM_SERVICE_LISTENER, data, &reply);
153    return reply.readInt32();
154}
155
156DrmConstraints* BpDrmManagerService::getConstraints(
157            int uniqueId, const String8* path, const int action) {
158    ALOGV("Get Constraints");
159    Parcel data, reply;
160
161    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
162    data.writeInt32(uniqueId);
163    data.writeString8(*path);
164    data.writeInt32(action);
165
166    remote()->transact(GET_CONSTRAINTS_FROM_CONTENT, data, &reply);
167
168    DrmConstraints* drmConstraints = NULL;
169    if (0 != reply.dataAvail()) {
170        //Filling Drm Constraints
171        drmConstraints = new DrmConstraints();
172
173        const int size = reply.readInt32();
174        for (int index = 0; index < size; ++index) {
175            const String8 key(reply.readString8());
176            const int bufferSize = reply.readInt32();
177            char* data = NULL;
178            if (0 < bufferSize) {
179                data = new char[bufferSize];
180                reply.read(data, bufferSize);
181                drmConstraints->put(&key, data);
182                delete[] data;
183            }
184        }
185    }
186    return drmConstraints;
187}
188
189DrmMetadata* BpDrmManagerService::getMetadata(int uniqueId, const String8* path) {
190    ALOGV("Get Metadata");
191    Parcel data, reply;
192    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
193    data.writeInt32(uniqueId);
194
195    DrmMetadata* drmMetadata = NULL;
196    data.writeString8(*path);
197    remote()->transact(GET_METADATA_FROM_CONTENT, data, &reply);
198
199    if (0 != reply.dataAvail()) {
200        //Filling Drm Metadata
201        drmMetadata = new DrmMetadata();
202
203        const int size = reply.readInt32();
204        for (int index = 0; index < size; ++index) {
205            const String8 key(reply.readString8());
206            const int bufferSize = reply.readInt32();
207            char* data = NULL;
208            if (0 < bufferSize) {
209                data = new char[bufferSize];
210                reply.read(data, bufferSize);
211                drmMetadata->put(&key, data);
212                delete[] data;
213            }
214        }
215    }
216    return drmMetadata;
217}
218
219bool BpDrmManagerService::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
220    ALOGV("Can Handle");
221    Parcel data, reply;
222
223    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
224    data.writeInt32(uniqueId);
225
226    data.writeString8(path);
227    data.writeString8(mimeType);
228
229    remote()->transact(CAN_HANDLE, data, &reply);
230
231    return static_cast<bool>(reply.readInt32());
232}
233
234DrmInfoStatus* BpDrmManagerService::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
235    ALOGV("Process DRM Info");
236    Parcel data, reply;
237
238    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
239    data.writeInt32(uniqueId);
240
241    //Filling DRM info
242    data.writeInt32(drmInfo->getInfoType());
243    const DrmBuffer dataBuffer = drmInfo->getData();
244    const int dataBufferSize = dataBuffer.length;
245    data.writeInt32(dataBufferSize);
246    if (0 < dataBufferSize) {
247        data.write(dataBuffer.data, dataBufferSize);
248    }
249    data.writeString8(drmInfo->getMimeType());
250
251    data.writeInt32(drmInfo->getCount());
252    DrmInfo::KeyIterator keyIt = drmInfo->keyIterator();
253
254    while (keyIt.hasNext()) {
255        const String8 key = keyIt.next();
256        data.writeString8(key);
257        const String8 value = drmInfo->get(key);
258        data.writeString8((value == String8("")) ? String8("NULL") : value);
259    }
260
261    remote()->transact(PROCESS_DRM_INFO, data, &reply);
262
263    DrmInfoStatus* drmInfoStatus = NULL;
264    if (0 != reply.dataAvail()) {
265        //Filling DRM Info Status
266        const int statusCode = reply.readInt32();
267        const int infoType = reply.readInt32();
268        const String8 mimeType = reply.readString8();
269
270        DrmBuffer* drmBuffer = NULL;
271        if (0 != reply.dataAvail()) {
272            const int bufferSize = reply.readInt32();
273            char* data = NULL;
274            if (0 < bufferSize) {
275                data = new char[bufferSize];
276                reply.read(data, bufferSize);
277            }
278            drmBuffer = new DrmBuffer(data, bufferSize);
279        }
280        drmInfoStatus = new DrmInfoStatus(statusCode, infoType, drmBuffer, mimeType);
281    }
282    return drmInfoStatus;
283}
284
285DrmInfo* BpDrmManagerService::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInforequest) {
286    ALOGV("Acquire DRM Info");
287    Parcel data, reply;
288
289    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
290    data.writeInt32(uniqueId);
291
292    //Filling DRM Info Request
293    data.writeInt32(drmInforequest->getInfoType());
294    data.writeString8(drmInforequest->getMimeType());
295
296    data.writeInt32(drmInforequest->getCount());
297    DrmInfoRequest::KeyIterator keyIt = drmInforequest->keyIterator();
298
299    while (keyIt.hasNext()) {
300        const String8 key = keyIt.next();
301        data.writeString8(key);
302        const String8 value = drmInforequest->get(key);
303        if (key == String8("FileDescriptorKey")) {
304            int fd = -1;
305            sscanf(value.string(), "FileDescriptor[%d]", &fd);
306            data.writeFileDescriptor(fd);
307        } else {
308            data.writeString8((value == String8("")) ? String8("NULL") : value);
309        }
310    }
311
312    remote()->transact(ACQUIRE_DRM_INFO, data, &reply);
313
314    DrmInfo* drmInfo = NULL;
315    if (0 != reply.dataAvail()) {
316        //Filling DRM Info
317        const int infoType = reply.readInt32();
318        const int bufferSize = reply.readInt32();
319        char* data = NULL;
320
321        if (0 < bufferSize) {
322            data = new char[bufferSize];
323            reply.read(data, bufferSize);
324        }
325        drmInfo = new DrmInfo(infoType, DrmBuffer(data, bufferSize), reply.readString8());
326
327        const int size = reply.readInt32();
328        for (int index = 0; index < size; ++index) {
329            const String8 key(reply.readString8());
330            const String8 value(reply.readString8());
331            drmInfo->put(key, (value == String8("NULL")) ? String8("") : value);
332        }
333    }
334    return drmInfo;
335}
336
337status_t BpDrmManagerService::saveRights(
338            int uniqueId, const DrmRights& drmRights,
339            const String8& rightsPath, const String8& contentPath) {
340    ALOGV("Save Rights");
341    Parcel data, reply;
342
343    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
344    data.writeInt32(uniqueId);
345
346    //Filling Drm Rights
347    const DrmBuffer dataBuffer = drmRights.getData();
348    data.writeInt32(dataBuffer.length);
349    data.write(dataBuffer.data, dataBuffer.length);
350
351    const String8 mimeType = drmRights.getMimeType();
352    data.writeString8((mimeType == String8("")) ? String8("NULL") : mimeType);
353
354    const String8 accountId = drmRights.getAccountId();
355    data.writeString8((accountId == String8("")) ? String8("NULL") : accountId);
356
357    const String8 subscriptionId = drmRights.getSubscriptionId();
358    data.writeString8((subscriptionId == String8("")) ? String8("NULL") : subscriptionId);
359
360    data.writeString8((rightsPath == String8("")) ? String8("NULL") : rightsPath);
361    data.writeString8((contentPath == String8("")) ? String8("NULL") : contentPath);
362
363    remote()->transact(SAVE_RIGHTS, data, &reply);
364    return reply.readInt32();
365}
366
367String8 BpDrmManagerService::getOriginalMimeType(int uniqueId, const String8& path, int fd) {
368    ALOGV("Get Original MimeType");
369    Parcel data, reply;
370
371    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
372    data.writeInt32(uniqueId);
373    data.writeString8(path);
374    int32_t isFdValid = (fd >= 0);
375    data.writeInt32(isFdValid);
376    if (isFdValid) {
377        data.writeFileDescriptor(fd);
378    }
379
380    remote()->transact(GET_ORIGINAL_MIMETYPE, data, &reply);
381    return reply.readString8();
382}
383
384int BpDrmManagerService::getDrmObjectType(
385            int uniqueId, const String8& path, const String8& mimeType) {
386    ALOGV("Get Drm object type");
387    Parcel data, reply;
388
389    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
390    data.writeInt32(uniqueId);
391    data.writeString8(path);
392    data.writeString8(mimeType);
393
394    remote()->transact(GET_DRM_OBJECT_TYPE, data, &reply);
395
396    return reply.readInt32();
397}
398
399int BpDrmManagerService::checkRightsStatus(int uniqueId, const String8& path, int action) {
400    ALOGV("checkRightsStatus");
401    Parcel data, reply;
402
403    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
404    data.writeInt32(uniqueId);
405    data.writeString8(path);
406    data.writeInt32(action);
407
408    remote()->transact(CHECK_RIGHTS_STATUS, data, &reply);
409
410    return reply.readInt32();
411}
412
413status_t BpDrmManagerService::consumeRights(
414            int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
415    ALOGV("consumeRights");
416    Parcel data, reply;
417
418    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
419    data.writeInt32(uniqueId);
420
421    writeDecryptHandleToParcelData(decryptHandle, &data);
422
423    data.writeInt32(action);
424    data.writeInt32(static_cast< int>(reserve));
425
426    remote()->transact(CONSUME_RIGHTS, data, &reply);
427    return reply.readInt32();
428}
429
430status_t BpDrmManagerService::setPlaybackStatus(
431            int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int64_t position) {
432    ALOGV("setPlaybackStatus");
433    Parcel data, reply;
434
435    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
436    data.writeInt32(uniqueId);
437
438    writeDecryptHandleToParcelData(decryptHandle, &data);
439
440    data.writeInt32(playbackStatus);
441    data.writeInt64(position);
442
443    remote()->transact(SET_PLAYBACK_STATUS, data, &reply);
444    return reply.readInt32();
445}
446
447bool BpDrmManagerService::validateAction(
448            int uniqueId, const String8& path,
449            int action, const ActionDescription& description) {
450    ALOGV("validateAction");
451    Parcel data, reply;
452
453    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
454    data.writeInt32(uniqueId);
455    data.writeString8(path);
456    data.writeInt32(action);
457    data.writeInt32(description.outputType);
458    data.writeInt32(description.configuration);
459
460    remote()->transact(VALIDATE_ACTION, data, &reply);
461
462    return static_cast<bool>(reply.readInt32());
463}
464
465status_t BpDrmManagerService::removeRights(int uniqueId, const String8& path) {
466    ALOGV("removeRights");
467    Parcel data, reply;
468
469    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
470    data.writeInt32(uniqueId);
471    data.writeString8(path);
472
473    remote()->transact(REMOVE_RIGHTS, data, &reply);
474    return reply.readInt32();
475}
476
477status_t BpDrmManagerService::removeAllRights(int uniqueId) {
478    ALOGV("removeAllRights");
479    Parcel data, reply;
480
481    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
482    data.writeInt32(uniqueId);
483
484    remote()->transact(REMOVE_ALL_RIGHTS, data, &reply);
485    return reply.readInt32();
486}
487
488int BpDrmManagerService::openConvertSession(int uniqueId, const String8& mimeType) {
489    ALOGV("openConvertSession");
490    Parcel data, reply;
491
492    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
493    data.writeInt32(uniqueId);
494    data.writeString8(mimeType);
495
496    remote()->transact(OPEN_CONVERT_SESSION, data, &reply);
497    return reply.readInt32();
498}
499
500DrmConvertedStatus* BpDrmManagerService::convertData(
501            int uniqueId, int convertId, const DrmBuffer* inputData) {
502    ALOGV("convertData");
503    Parcel data, reply;
504
505    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
506    data.writeInt32(uniqueId);
507    data.writeInt32(convertId);
508    data.writeInt32(inputData->length);
509    data.write(inputData->data, inputData->length);
510
511    remote()->transact(CONVERT_DATA, data, &reply);
512
513    DrmConvertedStatus* drmConvertedStatus = NULL;
514
515    if (0 != reply.dataAvail()) {
516        //Filling DRM Converted Status
517        const int statusCode = reply.readInt32();
518        const off64_t offset = reply.readInt64();
519
520        DrmBuffer* convertedData = NULL;
521        if (0 != reply.dataAvail()) {
522            const int bufferSize = reply.readInt32();
523            char* data = NULL;
524            if (0 < bufferSize) {
525                data = new char[bufferSize];
526                reply.read(data, bufferSize);
527            }
528            convertedData = new DrmBuffer(data, bufferSize);
529        }
530        drmConvertedStatus = new DrmConvertedStatus(statusCode, convertedData, offset);
531    }
532    return drmConvertedStatus;
533}
534
535DrmConvertedStatus* BpDrmManagerService::closeConvertSession(int uniqueId, int convertId) {
536    ALOGV("closeConvertSession");
537    Parcel data, reply;
538
539    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
540    data.writeInt32(uniqueId);
541    data.writeInt32(convertId);
542
543    remote()->transact(CLOSE_CONVERT_SESSION, data, &reply);
544
545    DrmConvertedStatus* drmConvertedStatus = NULL;
546
547    if (0 != reply.dataAvail()) {
548        //Filling DRM Converted Status
549        const int statusCode = reply.readInt32();
550        const off64_t offset = reply.readInt64();
551
552        DrmBuffer* convertedData = NULL;
553        if (0 != reply.dataAvail()) {
554            const int bufferSize = reply.readInt32();
555            char* data = NULL;
556            if (0 < bufferSize) {
557                data = new char[bufferSize];
558                reply.read(data, bufferSize);
559            }
560            convertedData = new DrmBuffer(data, bufferSize);
561        }
562        drmConvertedStatus = new DrmConvertedStatus(statusCode, convertedData, offset);
563    }
564    return drmConvertedStatus;
565}
566
567status_t BpDrmManagerService::getAllSupportInfo(
568            int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
569    ALOGV("Get All Support Info");
570    Parcel data, reply;
571
572    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
573    data.writeInt32(uniqueId);
574
575    remote()->transact(GET_ALL_SUPPORT_INFO, data, &reply);
576
577    //Filling DRM Support Info
578    const int arraySize = reply.readInt32();
579    if (0 < arraySize) {
580        *drmSupportInfoArray = new DrmSupportInfo[arraySize];
581
582        for (int index = 0; index < arraySize; ++index) {
583            DrmSupportInfo drmSupportInfo;
584
585            const int fileSuffixVectorSize = reply.readInt32();
586            for (int i = 0; i < fileSuffixVectorSize; ++i) {
587                drmSupportInfo.addFileSuffix(reply.readString8());
588            }
589
590            const int mimeTypeVectorSize = reply.readInt32();
591            for (int i = 0; i < mimeTypeVectorSize; ++i) {
592                drmSupportInfo.addMimeType(reply.readString8());
593            }
594
595            drmSupportInfo.setDescription(reply.readString8());
596            (*drmSupportInfoArray)[index] = drmSupportInfo;
597        }
598    }
599    *length = arraySize;
600    return reply.readInt32();
601}
602
603DecryptHandle* BpDrmManagerService::openDecryptSession(
604            int uniqueId, int fd, off64_t offset, off64_t length, const char* mime) {
605    ALOGV("Entering BpDrmManagerService::openDecryptSession");
606    Parcel data, reply;
607
608    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
609    data.writeInt32(uniqueId);
610    data.writeFileDescriptor(fd);
611    data.writeInt64(offset);
612    data.writeInt64(length);
613    String8 mimeType;
614    if (mime) {
615        mimeType = mime;
616    }
617    data.writeString8(mimeType);
618
619    remote()->transact(OPEN_DECRYPT_SESSION, data, &reply);
620
621    DecryptHandle* handle = NULL;
622    if (0 != reply.dataAvail()) {
623        handle = new DecryptHandle();
624        readDecryptHandleFromParcelData(handle, reply);
625    }
626    return handle;
627}
628
629DecryptHandle* BpDrmManagerService::openDecryptSession(
630        int uniqueId, const char* uri, const char* mime) {
631
632    ALOGV("Entering BpDrmManagerService::openDecryptSession: mime=%s", mime? mime: "NULL");
633    Parcel data, reply;
634
635    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
636    data.writeInt32(uniqueId);
637    data.writeString8(String8(uri));
638    String8 mimeType;
639    if (mime) {
640        mimeType = mime;
641    }
642    data.writeString8(mimeType);
643
644    remote()->transact(OPEN_DECRYPT_SESSION_FROM_URI, data, &reply);
645
646    DecryptHandle* handle = NULL;
647    if (0 != reply.dataAvail()) {
648        handle = new DecryptHandle();
649        readDecryptHandleFromParcelData(handle, reply);
650    } else {
651        ALOGV("no decryptHandle is generated in service side");
652    }
653    return handle;
654}
655
656DecryptHandle* BpDrmManagerService::openDecryptSession(
657            int uniqueId, const DrmBuffer& buf, const String8& mimeType) {
658    ALOGV("Entering BpDrmManagerService::openDecryptSession");
659    Parcel data, reply;
660
661    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
662    data.writeInt32(uniqueId);
663    if (buf.data != NULL && buf.length > 0) {
664        data.writeInt32(buf.length);
665        data.write(buf.data, buf.length);
666    } else {
667        data.writeInt32(0);
668    }
669    data.writeString8(mimeType);
670
671    remote()->transact(OPEN_DECRYPT_SESSION_FOR_STREAMING, data, &reply);
672
673    DecryptHandle* handle = NULL;
674    if (0 != reply.dataAvail()) {
675        handle = new DecryptHandle();
676        readDecryptHandleFromParcelData(handle, reply);
677    } else {
678        ALOGV("no decryptHandle is generated in service side");
679    }
680    return handle;
681}
682
683status_t BpDrmManagerService::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
684    ALOGV("closeDecryptSession");
685    Parcel data, reply;
686
687    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
688    data.writeInt32(uniqueId);
689
690    writeDecryptHandleToParcelData(decryptHandle, &data);
691
692    remote()->transact(CLOSE_DECRYPT_SESSION, data, &reply);
693
694    return reply.readInt32();
695}
696
697status_t BpDrmManagerService::initializeDecryptUnit(
698            int uniqueId, DecryptHandle* decryptHandle,
699            int decryptUnitId, const DrmBuffer* headerInfo) {
700    ALOGV("initializeDecryptUnit");
701    Parcel data, reply;
702
703    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
704    data.writeInt32(uniqueId);
705
706    writeDecryptHandleToParcelData(decryptHandle, &data);
707
708    data.writeInt32(decryptUnitId);
709
710    data.writeInt32(headerInfo->length);
711    data.write(headerInfo->data, headerInfo->length);
712
713    remote()->transact(INITIALIZE_DECRYPT_UNIT, data, &reply);
714    return reply.readInt32();
715}
716
717status_t BpDrmManagerService::decrypt(
718            int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId,
719            const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
720    ALOGV("decrypt");
721    Parcel data, reply;
722
723    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
724    data.writeInt32(uniqueId);
725
726    writeDecryptHandleToParcelData(decryptHandle, &data);
727
728    data.writeInt32(decryptUnitId);
729    data.writeInt32((*decBuffer)->length);
730
731    data.writeInt32(encBuffer->length);
732    data.write(encBuffer->data, encBuffer->length);
733
734    if (NULL != IV) {
735        data.writeInt32(IV->length);
736        data.write(IV->data, IV->length);
737    }
738
739    remote()->transact(DECRYPT, data, &reply);
740
741    const status_t status = reply.readInt32();
742    ALOGV("Return value of decrypt() is %d", status);
743
744    if (status == NO_ERROR) {
745        const int size = reply.readInt32();
746        (*decBuffer)->length = size;
747        reply.read((void *)(*decBuffer)->data, size);
748    }
749
750    return status;
751}
752
753status_t BpDrmManagerService::finalizeDecryptUnit(
754            int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
755    ALOGV("finalizeDecryptUnit");
756    Parcel data, reply;
757
758    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
759    data.writeInt32(uniqueId);
760
761    writeDecryptHandleToParcelData(decryptHandle, &data);
762
763    data.writeInt32(decryptUnitId);
764
765    remote()->transact(FINALIZE_DECRYPT_UNIT, data, &reply);
766    return reply.readInt32();
767}
768
769ssize_t BpDrmManagerService::pread(
770            int uniqueId, DecryptHandle* decryptHandle, void* buffer,
771            ssize_t numBytes, off64_t offset) {
772    ALOGV("read");
773    Parcel data, reply;
774    int result;
775
776    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
777    data.writeInt32(uniqueId);
778
779    writeDecryptHandleToParcelData(decryptHandle, &data);
780
781    data.writeInt32(numBytes);
782    data.writeInt64(offset);
783
784    remote()->transact(PREAD, data, &reply);
785    result = reply.readInt32();
786    if (0 < result) {
787        reply.read(buffer, result);
788    }
789    return result;
790}
791
792IMPLEMENT_META_INTERFACE(DrmManagerService, "drm.IDrmManagerService");
793
794status_t BnDrmManagerService::onTransact(
795            uint32_t code, const Parcel& data,
796            Parcel* reply, uint32_t flags) {
797    ALOGV("Entering BnDrmManagerService::onTransact with code %d", code);
798
799    switch (code) {
800    case ADD_UNIQUEID:
801    {
802        ALOGV("BnDrmManagerService::onTransact :ADD_UNIQUEID");
803        CHECK_INTERFACE(IDrmManagerService, data, reply);
804        int uniqueId = addUniqueId(data.readInt32());
805        reply->writeInt32(uniqueId);
806        return DRM_NO_ERROR;
807    }
808
809    case REMOVE_UNIQUEID:
810    {
811        ALOGV("BnDrmManagerService::onTransact :REMOVE_UNIQUEID");
812        CHECK_INTERFACE(IDrmManagerService, data, reply);
813        removeUniqueId(data.readInt32());
814        return DRM_NO_ERROR;
815    }
816
817    case ADD_CLIENT:
818    {
819        ALOGV("BnDrmManagerService::onTransact :ADD_CLIENT");
820        CHECK_INTERFACE(IDrmManagerService, data, reply);
821        addClient(data.readInt32());
822        return DRM_NO_ERROR;
823    }
824
825    case REMOVE_CLIENT:
826    {
827        ALOGV("BnDrmManagerService::onTransact :REMOVE_CLIENT");
828        CHECK_INTERFACE(IDrmManagerService, data, reply);
829        removeClient(data.readInt32());
830        return DRM_NO_ERROR;
831    }
832
833    case SET_DRM_SERVICE_LISTENER:
834    {
835        ALOGV("BnDrmManagerService::onTransact :SET_DRM_SERVICE_LISTENER");
836        CHECK_INTERFACE(IDrmManagerService, data, reply);
837
838        const int uniqueId = data.readInt32();
839        const sp<IDrmServiceListener> drmServiceListener
840            = interface_cast<IDrmServiceListener> (data.readStrongBinder());
841
842        status_t status = setDrmServiceListener(uniqueId, drmServiceListener);
843
844        reply->writeInt32(status);
845        return DRM_NO_ERROR;
846    }
847
848    case GET_CONSTRAINTS_FROM_CONTENT:
849    {
850        ALOGV("BnDrmManagerService::onTransact :GET_CONSTRAINTS_FROM_CONTENT");
851        CHECK_INTERFACE(IDrmManagerService, data, reply);
852
853        const int uniqueId = data.readInt32();
854        const String8 path = data.readString8();
855
856        DrmConstraints* drmConstraints
857            = getConstraints(uniqueId, &path, data.readInt32());
858
859        if (NULL != drmConstraints) {
860            //Filling DRM Constraints contents
861            reply->writeInt32(drmConstraints->getCount());
862
863            DrmConstraints::KeyIterator keyIt = drmConstraints->keyIterator();
864            while (keyIt.hasNext()) {
865                const String8 key = keyIt.next();
866                reply->writeString8(key);
867                const char* value = drmConstraints->getAsByteArray(&key);
868                int bufferSize = 0;
869                if (NULL != value) {
870                    bufferSize = strlen(value);
871                    reply->writeInt32(bufferSize + 1);
872                    reply->write(value, bufferSize + 1);
873                } else {
874                    reply->writeInt32(0);
875                }
876            }
877        }
878        delete drmConstraints; drmConstraints = NULL;
879        return DRM_NO_ERROR;
880    }
881
882    case GET_METADATA_FROM_CONTENT:
883    {
884        ALOGV("BnDrmManagerService::onTransact :GET_METADATA_FROM_CONTENT");
885        CHECK_INTERFACE(IDrmManagerService, data, reply);
886
887        const int uniqueId = data.readInt32();
888        const String8 path = data.readString8();
889
890        DrmMetadata* drmMetadata = getMetadata(uniqueId, &path);
891        if (NULL != drmMetadata) {
892            //Filling DRM Metadata contents
893            reply->writeInt32(drmMetadata->getCount());
894
895            DrmMetadata::KeyIterator keyIt = drmMetadata->keyIterator();
896            while (keyIt.hasNext()) {
897                const String8 key = keyIt.next();
898                reply->writeString8(key);
899                const char* value = drmMetadata->getAsByteArray(&key);
900                int bufferSize = 0;
901                if (NULL != value) {
902                    bufferSize = strlen(value);
903                    reply->writeInt32(bufferSize + 1);
904                    reply->write(value, bufferSize + 1);
905                } else {
906                    reply->writeInt32(0);
907                }
908            }
909        }
910        delete drmMetadata; drmMetadata = NULL;
911        return NO_ERROR;
912    }
913
914    case CAN_HANDLE:
915    {
916        ALOGV("BnDrmManagerService::onTransact :CAN_HANDLE");
917        CHECK_INTERFACE(IDrmManagerService, data, reply);
918
919        const int uniqueId = data.readInt32();
920        const String8 path = data.readString8();
921        const String8 mimeType = data.readString8();
922
923        bool result = canHandle(uniqueId, path, mimeType);
924
925        reply->writeInt32(result);
926        return DRM_NO_ERROR;
927    }
928
929    case PROCESS_DRM_INFO:
930    {
931        ALOGV("BnDrmManagerService::onTransact :PROCESS_DRM_INFO");
932        CHECK_INTERFACE(IDrmManagerService, data, reply);
933
934        const int uniqueId = data.readInt32();
935
936        //Filling DRM info
937        const int infoType = data.readInt32();
938        const int bufferSize = data.readInt32();
939        char* buffer = NULL;
940        if (0 < bufferSize) {
941            buffer = (char *)data.readInplace(bufferSize);
942        }
943        const DrmBuffer drmBuffer(buffer, bufferSize);
944        DrmInfo* drmInfo = new DrmInfo(infoType, drmBuffer, data.readString8());
945
946        const int size = data.readInt32();
947        for (int index = 0; index < size; ++index) {
948            const String8 key(data.readString8());
949            const String8 value(data.readString8());
950            drmInfo->put(key, (value == String8("NULL")) ? String8("") : value);
951        }
952
953        DrmInfoStatus* drmInfoStatus = processDrmInfo(uniqueId, drmInfo);
954
955        if (NULL != drmInfoStatus) {
956            //Filling DRM Info Status contents
957            reply->writeInt32(drmInfoStatus->statusCode);
958            reply->writeInt32(drmInfoStatus->infoType);
959            reply->writeString8(drmInfoStatus->mimeType);
960
961            if (NULL != drmInfoStatus->drmBuffer) {
962                const DrmBuffer* drmBuffer = drmInfoStatus->drmBuffer;
963                const int bufferSize = drmBuffer->length;
964                reply->writeInt32(bufferSize);
965                if (0 < bufferSize) {
966                    reply->write(drmBuffer->data, bufferSize);
967                }
968                delete [] drmBuffer->data;
969                delete drmBuffer; drmBuffer = NULL;
970            }
971        }
972        delete drmInfo; drmInfo = NULL;
973        delete drmInfoStatus; drmInfoStatus = NULL;
974        return DRM_NO_ERROR;
975    }
976
977    case ACQUIRE_DRM_INFO:
978    {
979        ALOGV("BnDrmManagerService::onTransact :ACQUIRE_DRM_INFO");
980        CHECK_INTERFACE(IDrmManagerService, data, reply);
981
982        const int uniqueId = data.readInt32();
983
984        //Filling DRM info Request
985        const int infoType = data.readInt32();
986        const String8 mimeType = data.readString8();
987        DrmInfoRequest* drmInfoRequest = new DrmInfoRequest(infoType, mimeType);
988
989        const int size = data.readInt32();
990        for (int index = 0; index < size; ++index) {
991            const String8 key(data.readString8());
992            if (key == String8("FileDescriptorKey")) {
993                char buffer[16];
994                int fd = data.readFileDescriptor();
995                sprintf(buffer, "%lu", (unsigned long)fd);
996                drmInfoRequest->put(key, String8(buffer));
997            } else {
998                const String8 value(data.readString8());
999                drmInfoRequest->put(key, (value == String8("NULL")) ? String8("") : value);
1000            }
1001        }
1002
1003        DrmInfo* drmInfo = acquireDrmInfo(uniqueId, drmInfoRequest);
1004
1005        if (NULL != drmInfo) {
1006            //Filling DRM Info
1007            const DrmBuffer drmBuffer = drmInfo->getData();
1008            reply->writeInt32(drmInfo->getInfoType());
1009
1010            const int bufferSize = drmBuffer.length;
1011            reply->writeInt32(bufferSize);
1012            if (0 < bufferSize) {
1013                reply->write(drmBuffer.data, bufferSize);
1014            }
1015            reply->writeString8(drmInfo->getMimeType());
1016            reply->writeInt32(drmInfo->getCount());
1017
1018            DrmInfo::KeyIterator keyIt = drmInfo->keyIterator();
1019            while (keyIt.hasNext()) {
1020                const String8 key = keyIt.next();
1021                reply->writeString8(key);
1022                const String8 value = drmInfo->get(key);
1023                reply->writeString8((value == String8("")) ? String8("NULL") : value);
1024            }
1025            delete [] drmBuffer.data;
1026        }
1027        delete drmInfoRequest; drmInfoRequest = NULL;
1028        delete drmInfo; drmInfo = NULL;
1029        return DRM_NO_ERROR;
1030    }
1031
1032    case SAVE_RIGHTS:
1033    {
1034        ALOGV("BnDrmManagerService::onTransact :SAVE_RIGHTS");
1035        CHECK_INTERFACE(IDrmManagerService, data, reply);
1036
1037        const int uniqueId = data.readInt32();
1038
1039        //Filling DRM Rights
1040        const int bufferSize = data.readInt32();
1041        const DrmBuffer drmBuffer((char *)data.readInplace(bufferSize), bufferSize);
1042
1043        const String8 mimeType(data.readString8());
1044        const String8 accountId(data.readString8());
1045        const String8 subscriptionId(data.readString8());
1046        const String8 rightsPath(data.readString8());
1047        const String8 contentPath(data.readString8());
1048
1049        DrmRights drmRights(drmBuffer,
1050                            ((mimeType == String8("NULL")) ? String8("") : mimeType),
1051                            ((accountId == String8("NULL")) ? String8("") : accountId),
1052                            ((subscriptionId == String8("NULL")) ? String8("") : subscriptionId));
1053
1054        const status_t status = saveRights(uniqueId, drmRights,
1055                            ((rightsPath == String8("NULL")) ? String8("") : rightsPath),
1056                            ((contentPath == String8("NULL")) ? String8("") : contentPath));
1057
1058        reply->writeInt32(status);
1059        return DRM_NO_ERROR;
1060    }
1061
1062    case GET_ORIGINAL_MIMETYPE:
1063    {
1064        ALOGV("BnDrmManagerService::onTransact :GET_ORIGINAL_MIMETYPE");
1065        CHECK_INTERFACE(IDrmManagerService, data, reply);
1066
1067        const int uniqueId = data.readInt32();
1068        const String8 path = data.readString8();
1069        const int32_t isFdValid = data.readInt32();
1070        int fd = -1;
1071        if (isFdValid) {
1072            fd = data.readFileDescriptor();
1073        }
1074        const String8 originalMimeType = getOriginalMimeType(uniqueId, path, fd);
1075
1076        reply->writeString8(originalMimeType);
1077        return DRM_NO_ERROR;
1078    }
1079
1080    case GET_DRM_OBJECT_TYPE:
1081    {
1082        ALOGV("BnDrmManagerService::onTransact :GET_DRM_OBJECT_TYPE");
1083        CHECK_INTERFACE(IDrmManagerService, data, reply);
1084
1085        const int uniqueId = data.readInt32();
1086        const String8 path = data.readString8();
1087        const String8 mimeType = data.readString8();
1088        const int drmObjectType = getDrmObjectType(uniqueId, path, mimeType);
1089
1090        reply->writeInt32(drmObjectType);
1091        return DRM_NO_ERROR;
1092    }
1093
1094    case CHECK_RIGHTS_STATUS:
1095    {
1096        ALOGV("BnDrmManagerService::onTransact :CHECK_RIGHTS_STATUS");
1097        CHECK_INTERFACE(IDrmManagerService, data, reply);
1098
1099        const int uniqueId = data.readInt32();
1100        const String8 path = data.readString8();
1101        const int action = data.readInt32();
1102        const int result = checkRightsStatus(uniqueId, path, action);
1103
1104        reply->writeInt32(result);
1105        return DRM_NO_ERROR;
1106    }
1107
1108    case CONSUME_RIGHTS:
1109    {
1110        ALOGV("BnDrmManagerService::onTransact :CONSUME_RIGHTS");
1111        CHECK_INTERFACE(IDrmManagerService, data, reply);
1112
1113        const int uniqueId = data.readInt32();
1114
1115        DecryptHandle handle;
1116        readDecryptHandleFromParcelData(&handle, data);
1117
1118        const int action = data.readInt32();
1119        const bool reserve = static_cast<bool>(data.readInt32());
1120        const status_t status
1121            = consumeRights(uniqueId, &handle, action, reserve);
1122        reply->writeInt32(status);
1123
1124        clearDecryptHandle(&handle);
1125        return DRM_NO_ERROR;
1126    }
1127
1128    case SET_PLAYBACK_STATUS:
1129    {
1130        ALOGV("BnDrmManagerService::onTransact :SET_PLAYBACK_STATUS");
1131        CHECK_INTERFACE(IDrmManagerService, data, reply);
1132
1133        const int uniqueId = data.readInt32();
1134
1135        DecryptHandle handle;
1136        readDecryptHandleFromParcelData(&handle, data);
1137
1138        const int playbackStatus = data.readInt32();
1139        const int64_t position = data.readInt64();
1140        const status_t status
1141            = setPlaybackStatus(uniqueId, &handle, playbackStatus, position);
1142        reply->writeInt32(status);
1143
1144        clearDecryptHandle(&handle);
1145        return DRM_NO_ERROR;
1146    }
1147
1148    case VALIDATE_ACTION:
1149    {
1150        ALOGV("BnDrmManagerService::onTransact :VALIDATE_ACTION");
1151        CHECK_INTERFACE(IDrmManagerService, data, reply);
1152
1153        const int uniqueId = data.readInt32();
1154        const String8 path = data.readString8();
1155        const int action = data.readInt32();
1156        const int outputType = data.readInt32();
1157        const int configuration = data.readInt32();
1158        bool result = validateAction(uniqueId, path, action,
1159                ActionDescription(outputType, configuration));
1160
1161        reply->writeInt32(result);
1162        return DRM_NO_ERROR;
1163    }
1164
1165    case REMOVE_RIGHTS:
1166    {
1167        ALOGV("BnDrmManagerService::onTransact :REMOVE_RIGHTS");
1168        CHECK_INTERFACE(IDrmManagerService, data, reply);
1169
1170        int uniqueId = data.readInt32();
1171        String8 path = data.readString8();
1172        const status_t status = removeRights(uniqueId, path);
1173        reply->writeInt32(status);
1174
1175        return DRM_NO_ERROR;
1176    }
1177
1178    case REMOVE_ALL_RIGHTS:
1179    {
1180        ALOGV("BnDrmManagerService::onTransact :REMOVE_ALL_RIGHTS");
1181        CHECK_INTERFACE(IDrmManagerService, data, reply);
1182
1183        const status_t status = removeAllRights(data.readInt32());
1184        reply->writeInt32(status);
1185
1186        return DRM_NO_ERROR;
1187    }
1188
1189    case OPEN_CONVERT_SESSION:
1190    {
1191        ALOGV("BnDrmManagerService::onTransact :OPEN_CONVERT_SESSION");
1192        CHECK_INTERFACE(IDrmManagerService, data, reply);
1193
1194        const int uniqueId = data.readInt32();
1195        const String8 mimeType = data.readString8();
1196        const int convertId = openConvertSession(uniqueId, mimeType);
1197
1198        reply->writeInt32(convertId);
1199        return DRM_NO_ERROR;
1200    }
1201
1202    case CONVERT_DATA:
1203    {
1204        ALOGV("BnDrmManagerService::onTransact :CONVERT_DATA");
1205        CHECK_INTERFACE(IDrmManagerService, data, reply);
1206
1207        const int uniqueId = data.readInt32();
1208        const int convertId = data.readInt32();
1209
1210        //Filling input data
1211        const int bufferSize = data.readInt32();
1212        DrmBuffer* inputData = new DrmBuffer((char *)data.readInplace(bufferSize), bufferSize);
1213
1214        DrmConvertedStatus*    drmConvertedStatus = convertData(uniqueId, convertId, inputData);
1215
1216        if (NULL != drmConvertedStatus) {
1217            //Filling Drm Converted Ststus
1218            reply->writeInt32(drmConvertedStatus->statusCode);
1219            reply->writeInt64(drmConvertedStatus->offset);
1220
1221            if (NULL != drmConvertedStatus->convertedData) {
1222                const DrmBuffer* convertedData = drmConvertedStatus->convertedData;
1223                const int bufferSize = convertedData->length;
1224                reply->writeInt32(bufferSize);
1225                if (0 < bufferSize) {
1226                    reply->write(convertedData->data, bufferSize);
1227                }
1228                delete [] convertedData->data;
1229                delete convertedData; convertedData = NULL;
1230            }
1231        }
1232        delete inputData; inputData = NULL;
1233        delete drmConvertedStatus; drmConvertedStatus = NULL;
1234        return DRM_NO_ERROR;
1235    }
1236
1237    case CLOSE_CONVERT_SESSION:
1238    {
1239        ALOGV("BnDrmManagerService::onTransact :CLOSE_CONVERT_SESSION");
1240        CHECK_INTERFACE(IDrmManagerService, data, reply);
1241
1242        const int uniqueId = data.readInt32();
1243        const int convertId = data.readInt32();
1244        DrmConvertedStatus* drmConvertedStatus
1245            = closeConvertSession(uniqueId, convertId);
1246
1247        if (NULL != drmConvertedStatus) {
1248            //Filling Drm Converted Ststus
1249            reply->writeInt32(drmConvertedStatus->statusCode);
1250            reply->writeInt64(drmConvertedStatus->offset);
1251
1252            if (NULL != drmConvertedStatus->convertedData) {
1253                const DrmBuffer* convertedData = drmConvertedStatus->convertedData;
1254                const int bufferSize = convertedData->length;
1255                reply->writeInt32(bufferSize);
1256                if (0 < bufferSize) {
1257                    reply->write(convertedData->data, bufferSize);
1258                }
1259                delete [] convertedData->data;
1260                delete convertedData; convertedData = NULL;
1261            }
1262        }
1263        delete drmConvertedStatus; drmConvertedStatus = NULL;
1264        return DRM_NO_ERROR;
1265    }
1266
1267    case GET_ALL_SUPPORT_INFO:
1268    {
1269        ALOGV("BnDrmManagerService::onTransact :GET_ALL_SUPPORT_INFO");
1270        CHECK_INTERFACE(IDrmManagerService, data, reply);
1271
1272        const int uniqueId = data.readInt32();
1273        int length = 0;
1274        DrmSupportInfo* drmSupportInfoArray = NULL;
1275
1276        status_t status = getAllSupportInfo(uniqueId, &length, &drmSupportInfoArray);
1277
1278        reply->writeInt32(length);
1279        for (int i = 0; i < length; ++i) {
1280            DrmSupportInfo drmSupportInfo = drmSupportInfoArray[i];
1281
1282            reply->writeInt32(drmSupportInfo.getFileSuffixCount());
1283            DrmSupportInfo::FileSuffixIterator fileSuffixIt
1284                = drmSupportInfo.getFileSuffixIterator();
1285            while (fileSuffixIt.hasNext()) {
1286                reply->writeString8(fileSuffixIt.next());
1287            }
1288
1289            reply->writeInt32(drmSupportInfo.getMimeTypeCount());
1290            DrmSupportInfo::MimeTypeIterator mimeTypeIt = drmSupportInfo.getMimeTypeIterator();
1291            while (mimeTypeIt.hasNext()) {
1292                reply->writeString8(mimeTypeIt.next());
1293            }
1294            reply->writeString8(drmSupportInfo.getDescription());
1295        }
1296        delete [] drmSupportInfoArray; drmSupportInfoArray = NULL;
1297        reply->writeInt32(status);
1298        return DRM_NO_ERROR;
1299    }
1300
1301    case OPEN_DECRYPT_SESSION:
1302    {
1303        ALOGV("BnDrmManagerService::onTransact :OPEN_DECRYPT_SESSION");
1304        CHECK_INTERFACE(IDrmManagerService, data, reply);
1305
1306        const int uniqueId = data.readInt32();
1307        const int fd = data.readFileDescriptor();
1308
1309        const off64_t offset = data.readInt64();
1310        const off64_t length = data.readInt64();
1311        const String8 mime = data.readString8();
1312
1313        DecryptHandle* handle
1314            = openDecryptSession(uniqueId, fd, offset, length, mime.string());
1315
1316        if (NULL != handle) {
1317            writeDecryptHandleToParcelData(handle, reply);
1318            clearDecryptHandle(handle);
1319            delete handle; handle = NULL;
1320        }
1321        return DRM_NO_ERROR;
1322    }
1323
1324    case OPEN_DECRYPT_SESSION_FROM_URI:
1325    {
1326        ALOGV("BnDrmManagerService::onTransact :OPEN_DECRYPT_SESSION_FROM_URI");
1327        CHECK_INTERFACE(IDrmManagerService, data, reply);
1328
1329        const int uniqueId = data.readInt32();
1330        const String8 uri = data.readString8();
1331        const String8 mime = data.readString8();
1332
1333        DecryptHandle* handle = openDecryptSession(uniqueId, uri.string(), mime.string());
1334
1335        if (NULL != handle) {
1336            writeDecryptHandleToParcelData(handle, reply);
1337
1338            clearDecryptHandle(handle);
1339            delete handle; handle = NULL;
1340        } else {
1341            ALOGV("NULL decryptHandle is returned");
1342        }
1343        return DRM_NO_ERROR;
1344    }
1345
1346    case OPEN_DECRYPT_SESSION_FOR_STREAMING:
1347    {
1348        ALOGV("BnDrmManagerService::onTransact :OPEN_DECRYPT_SESSION_FOR_STREAMING");
1349        CHECK_INTERFACE(IDrmManagerService, data, reply);
1350
1351        const int uniqueId = data.readInt32();
1352        const int bufferSize = data.readInt32();
1353        DrmBuffer buf((bufferSize > 0) ? (char *)data.readInplace(bufferSize) : NULL,
1354                bufferSize);
1355        const String8 mimeType(data.readString8());
1356
1357        DecryptHandle* handle = openDecryptSession(uniqueId, buf, mimeType);
1358
1359        if (handle != NULL) {
1360            writeDecryptHandleToParcelData(handle, reply);
1361            clearDecryptHandle(handle);
1362            delete handle;
1363            handle = NULL;
1364        } else {
1365            ALOGV("NULL decryptHandle is returned");
1366        }
1367        return DRM_NO_ERROR;
1368    }
1369
1370    case CLOSE_DECRYPT_SESSION:
1371    {
1372        ALOGV("BnDrmManagerService::onTransact :CLOSE_DECRYPT_SESSION");
1373        CHECK_INTERFACE(IDrmManagerService, data, reply);
1374
1375        const int uniqueId = data.readInt32();
1376
1377        DecryptHandle* handle = new DecryptHandle();
1378        readDecryptHandleFromParcelData(handle, data);
1379
1380        const status_t status = closeDecryptSession(uniqueId, handle);
1381        reply->writeInt32(status);
1382        return DRM_NO_ERROR;
1383    }
1384
1385    case INITIALIZE_DECRYPT_UNIT:
1386    {
1387        ALOGV("BnDrmManagerService::onTransact :INITIALIZE_DECRYPT_UNIT");
1388        CHECK_INTERFACE(IDrmManagerService, data, reply);
1389
1390        const int uniqueId = data.readInt32();
1391
1392        DecryptHandle handle;
1393        readDecryptHandleFromParcelData(&handle, data);
1394
1395        const int decryptUnitId = data.readInt32();
1396
1397        //Filling Header info
1398        const int bufferSize = data.readInt32();
1399        DrmBuffer* headerInfo = NULL;
1400        headerInfo = new DrmBuffer((char *)data.readInplace(bufferSize), bufferSize);
1401
1402        const status_t status
1403            = initializeDecryptUnit(uniqueId, &handle, decryptUnitId, headerInfo);
1404        reply->writeInt32(status);
1405
1406        clearDecryptHandle(&handle);
1407        delete headerInfo; headerInfo = NULL;
1408        return DRM_NO_ERROR;
1409    }
1410
1411    case DECRYPT:
1412    {
1413        ALOGV("BnDrmManagerService::onTransact :DECRYPT");
1414        CHECK_INTERFACE(IDrmManagerService, data, reply);
1415
1416        const int uniqueId = data.readInt32();
1417
1418        DecryptHandle handle;
1419        readDecryptHandleFromParcelData(&handle, data);
1420
1421        const int decryptUnitId = data.readInt32();
1422        const int decBufferSize = data.readInt32();
1423
1424        const int encBufferSize = data.readInt32();
1425        DrmBuffer* encBuffer
1426            = new DrmBuffer((char *)data.readInplace(encBufferSize), encBufferSize);
1427
1428        char* buffer = NULL;
1429        buffer = new char[decBufferSize];
1430        DrmBuffer* decBuffer = new DrmBuffer(buffer, decBufferSize);
1431
1432        DrmBuffer* IV = NULL;
1433        if (0 != data.dataAvail()) {
1434            const int ivBufferlength = data.readInt32();
1435            IV = new DrmBuffer((char *)data.readInplace(ivBufferlength), ivBufferlength);
1436        }
1437
1438        const status_t status
1439            = decrypt(uniqueId, &handle, decryptUnitId, encBuffer, &decBuffer, IV);
1440
1441        reply->writeInt32(status);
1442
1443        if (status == NO_ERROR) {
1444            const int size = decBuffer->length;
1445            reply->writeInt32(size);
1446            reply->write(decBuffer->data, size);
1447        }
1448
1449        clearDecryptHandle(&handle);
1450        delete encBuffer; encBuffer = NULL;
1451        delete decBuffer; decBuffer = NULL;
1452        delete [] buffer; buffer = NULL;
1453        delete IV; IV = NULL;
1454        return DRM_NO_ERROR;
1455    }
1456
1457    case FINALIZE_DECRYPT_UNIT:
1458    {
1459        ALOGV("BnDrmManagerService::onTransact :FINALIZE_DECRYPT_UNIT");
1460        CHECK_INTERFACE(IDrmManagerService, data, reply);
1461
1462        const int uniqueId = data.readInt32();
1463
1464        DecryptHandle handle;
1465        readDecryptHandleFromParcelData(&handle, data);
1466
1467        const status_t status = finalizeDecryptUnit(uniqueId, &handle, data.readInt32());
1468        reply->writeInt32(status);
1469
1470        clearDecryptHandle(&handle);
1471        return DRM_NO_ERROR;
1472    }
1473
1474    case PREAD:
1475    {
1476        ALOGV("BnDrmManagerService::onTransact :READ");
1477        CHECK_INTERFACE(IDrmManagerService, data, reply);
1478
1479        const int uniqueId = data.readInt32();
1480
1481        DecryptHandle handle;
1482        readDecryptHandleFromParcelData(&handle, data);
1483
1484        const int numBytes = data.readInt32();
1485        char* buffer = new char[numBytes];
1486
1487        const off64_t offset = data.readInt64();
1488
1489        ssize_t result = pread(uniqueId, &handle, buffer, numBytes, offset);
1490        reply->writeInt32(result);
1491        if (0 < result) {
1492            reply->write(buffer, result);
1493        }
1494
1495        clearDecryptHandle(&handle);
1496        delete [] buffer, buffer = NULL;
1497        return DRM_NO_ERROR;
1498    }
1499
1500    default:
1501        return BBinder::onTransact(code, data, reply, flags);
1502    }
1503}
1504