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