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