Andreas Huber | 1b19c9d | 2012-08-29 11:34:22 -0700 | [diff] [blame^] | 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_ENCRYPT, |
| 34 | }; |
| 35 | |
| 36 | struct BpHDCPObserver : public BpInterface<IHDCPObserver> { |
| 37 | BpHDCPObserver(const sp<IBinder> &impl) |
| 38 | : BpInterface<IHDCPObserver>(impl) { |
| 39 | } |
| 40 | |
| 41 | virtual void notify( |
| 42 | int msg, int ext1, int ext2, const Parcel *obj) { |
| 43 | Parcel data, reply; |
| 44 | data.writeInterfaceToken(IHDCPObserver::getInterfaceDescriptor()); |
| 45 | data.writeInt32(msg); |
| 46 | data.writeInt32(ext1); |
| 47 | data.writeInt32(ext2); |
| 48 | if (obj && obj->dataSize() > 0) { |
| 49 | data.appendFrom(const_cast<Parcel *>(obj), 0, obj->dataSize()); |
| 50 | } |
| 51 | remote()->transact(OBSERVER_NOTIFY, data, &reply, IBinder::FLAG_ONEWAY); |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | IMPLEMENT_META_INTERFACE(HDCPObserver, "android.hardware.IHDCPObserver"); |
| 56 | |
| 57 | struct BpHDCP : public BpInterface<IHDCP> { |
| 58 | BpHDCP(const sp<IBinder> &impl) |
| 59 | : BpInterface<IHDCP>(impl) { |
| 60 | } |
| 61 | |
| 62 | virtual status_t setObserver(const sp<IHDCPObserver> &observer) { |
| 63 | Parcel data, reply; |
| 64 | data.writeInterfaceToken(IHDCP::getInterfaceDescriptor()); |
| 65 | data.writeStrongBinder(observer->asBinder()); |
| 66 | remote()->transact(HDCP_SET_OBSERVER, data, &reply); |
| 67 | return reply.readInt32(); |
| 68 | } |
| 69 | |
| 70 | virtual status_t initAsync(const char *host, unsigned port) { |
| 71 | Parcel data, reply; |
| 72 | data.writeInterfaceToken(IHDCP::getInterfaceDescriptor()); |
| 73 | data.writeCString(host); |
| 74 | data.writeInt32(port); |
| 75 | remote()->transact(HDCP_INIT_ASYNC, data, &reply); |
| 76 | return reply.readInt32(); |
| 77 | } |
| 78 | |
| 79 | virtual status_t shutdownAsync() { |
| 80 | Parcel data, reply; |
| 81 | data.writeInterfaceToken(IHDCP::getInterfaceDescriptor()); |
| 82 | remote()->transact(HDCP_SHUTDOWN_ASYNC, data, &reply); |
| 83 | return reply.readInt32(); |
| 84 | } |
| 85 | |
| 86 | virtual status_t encrypt( |
| 87 | const void *inData, size_t size, uint32_t streamCTR, |
| 88 | uint64_t *outInputCTR, void *outData) { |
| 89 | Parcel data, reply; |
| 90 | data.writeInterfaceToken(IHDCP::getInterfaceDescriptor()); |
| 91 | data.writeInt32(size); |
| 92 | data.write(inData, size); |
| 93 | data.writeInt32(streamCTR); |
| 94 | remote()->transact(HDCP_ENCRYPT, data, &reply); |
| 95 | |
| 96 | status_t err = reply.readInt32(); |
| 97 | |
| 98 | if (err != OK) { |
| 99 | *outInputCTR = 0; |
| 100 | |
| 101 | return err; |
| 102 | } |
| 103 | |
| 104 | *outInputCTR = reply.readInt64(); |
| 105 | reply.read(outData, size); |
| 106 | |
| 107 | return err; |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | IMPLEMENT_META_INTERFACE(HDCP, "android.hardware.IHDCP"); |
| 112 | |
| 113 | status_t BnHDCPObserver::onTransact( |
| 114 | uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags) { |
| 115 | switch (code) { |
| 116 | case OBSERVER_NOTIFY: |
| 117 | { |
| 118 | CHECK_INTERFACE(IHDCPObserver, data, reply); |
| 119 | |
| 120 | int msg = data.readInt32(); |
| 121 | int ext1 = data.readInt32(); |
| 122 | int ext2 = data.readInt32(); |
| 123 | |
| 124 | Parcel obj; |
| 125 | if (data.dataAvail() > 0) { |
| 126 | obj.appendFrom( |
| 127 | const_cast<Parcel *>(&data), |
| 128 | data.dataPosition(), |
| 129 | data.dataAvail()); |
| 130 | } |
| 131 | |
| 132 | notify(msg, ext1, ext2, &obj); |
| 133 | |
| 134 | return OK; |
| 135 | } |
| 136 | |
| 137 | default: |
| 138 | return BBinder::onTransact(code, data, reply, flags); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | status_t BnHDCP::onTransact( |
| 143 | uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags) { |
| 144 | switch (code) { |
| 145 | case HDCP_SET_OBSERVER: |
| 146 | { |
| 147 | CHECK_INTERFACE(IHDCP, data, reply); |
| 148 | |
| 149 | sp<IHDCPObserver> observer = |
| 150 | interface_cast<IHDCPObserver>(data.readStrongBinder()); |
| 151 | |
| 152 | reply->writeInt32(setObserver(observer)); |
| 153 | return OK; |
| 154 | } |
| 155 | |
| 156 | case HDCP_INIT_ASYNC: |
| 157 | { |
| 158 | CHECK_INTERFACE(IHDCP, data, reply); |
| 159 | |
| 160 | const char *host = data.readCString(); |
| 161 | unsigned port = data.readInt32(); |
| 162 | |
| 163 | reply->writeInt32(initAsync(host, port)); |
| 164 | return OK; |
| 165 | } |
| 166 | |
| 167 | case HDCP_SHUTDOWN_ASYNC: |
| 168 | { |
| 169 | CHECK_INTERFACE(IHDCP, data, reply); |
| 170 | |
| 171 | reply->writeInt32(shutdownAsync()); |
| 172 | return OK; |
| 173 | } |
| 174 | |
| 175 | case HDCP_ENCRYPT: |
| 176 | { |
| 177 | size_t size = data.readInt32(); |
| 178 | |
| 179 | void *inData = malloc(2 * size); |
| 180 | void *outData = (uint8_t *)inData + size; |
| 181 | |
| 182 | data.read(inData, size); |
| 183 | |
| 184 | uint32_t streamCTR = data.readInt32(); |
| 185 | uint64_t inputCTR; |
| 186 | status_t err = encrypt(inData, size, streamCTR, &inputCTR, outData); |
| 187 | |
| 188 | reply->writeInt32(err); |
| 189 | |
| 190 | if (err == OK) { |
| 191 | reply->writeInt64(inputCTR); |
| 192 | reply->write(outData, size); |
| 193 | } |
| 194 | |
| 195 | free(inData); |
| 196 | inData = outData = NULL; |
| 197 | |
| 198 | return OK; |
| 199 | } |
| 200 | |
| 201 | default: |
| 202 | return BBinder::onTransact(code, data, reply, flags); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | } // namespace android |