Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Marco Nelissen | c7a11b2 | 2014-05-30 10:13:25 -0700 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 18 | #define LOG_TAG "NdkMediaDrm" |
| 19 | |
| 20 | #include "NdkMediaDrm.h" |
| 21 | |
Jeff Tinker | a69729d | 2016-02-12 08:47:00 -0800 | [diff] [blame] | 22 | #include <cutils/properties.h> |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 23 | #include <utils/Log.h> |
| 24 | #include <utils/StrongPointer.h> |
| 25 | #include <gui/Surface.h> |
| 26 | |
| 27 | #include <media/IDrm.h> |
| 28 | #include <media/IDrmClient.h> |
| 29 | #include <media/stagefright/MediaErrors.h> |
| 30 | #include <binder/IServiceManager.h> |
Jeff Tinker | a69729d | 2016-02-12 08:47:00 -0800 | [diff] [blame] | 31 | #include <media/IMediaDrmService.h> |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 32 | #include <ndk/NdkMediaCrypto.h> |
| 33 | |
| 34 | |
| 35 | using namespace android; |
| 36 | |
| 37 | typedef Vector<uint8_t> idvec_t; |
| 38 | |
Jeff Tinker | 3305b99 | 2014-05-14 18:39:25 -0700 | [diff] [blame] | 39 | struct DrmListener: virtual public BnDrmClient |
| 40 | { |
| 41 | private: |
| 42 | AMediaDrm *mObj; |
| 43 | AMediaDrmEventListener mListener; |
| 44 | |
| 45 | public: |
| 46 | DrmListener(AMediaDrm *obj, AMediaDrmEventListener listener) : mObj(obj), mListener(listener) {} |
| 47 | void notify(DrmPlugin::EventType eventType, int extra, const Parcel *obj); |
| 48 | }; |
| 49 | |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 50 | struct AMediaDrm { |
| 51 | sp<IDrm> mDrm; |
| 52 | sp<IDrmClient> mDrmClient; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 53 | List<idvec_t> mIds; |
| 54 | KeyedVector<String8, String8> mQueryResults; |
| 55 | Vector<uint8_t> mKeyRequest; |
| 56 | Vector<uint8_t> mProvisionRequest; |
| 57 | String8 mProvisionUrl; |
| 58 | String8 mPropertyString; |
| 59 | Vector<uint8_t> mPropertyByteArray; |
| 60 | List<Vector<uint8_t> > mSecureStops; |
Jeff Tinker | 3305b99 | 2014-05-14 18:39:25 -0700 | [diff] [blame] | 61 | sp<DrmListener> mListener; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
Jeff Tinker | 3305b99 | 2014-05-14 18:39:25 -0700 | [diff] [blame] | 64 | void DrmListener::notify(DrmPlugin::EventType eventType, int extra, const Parcel *obj) { |
| 65 | if (!mListener) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | AMediaDrmSessionId sessionId = {NULL, 0}; |
| 70 | int32_t sessionIdSize = obj->readInt32(); |
| 71 | if (sessionIdSize) { |
| 72 | uint8_t *sessionIdData = new uint8_t[sessionIdSize]; |
| 73 | sessionId.ptr = sessionIdData; |
| 74 | sessionId.length = sessionIdSize; |
| 75 | obj->read(sessionIdData, sessionId.length); |
| 76 | } |
| 77 | |
| 78 | int32_t dataSize = obj->readInt32(); |
| 79 | uint8_t *data = NULL; |
| 80 | if (dataSize) { |
| 81 | data = new uint8_t[dataSize]; |
| 82 | obj->read(data, dataSize); |
| 83 | } |
| 84 | |
| 85 | // translate DrmPlugin event types into their NDK equivalents |
| 86 | AMediaDrmEventType ndkEventType; |
| 87 | switch(eventType) { |
| 88 | case DrmPlugin::kDrmPluginEventProvisionRequired: |
| 89 | ndkEventType = EVENT_PROVISION_REQUIRED; |
| 90 | break; |
| 91 | case DrmPlugin::kDrmPluginEventKeyNeeded: |
| 92 | ndkEventType = EVENT_KEY_REQUIRED; |
| 93 | break; |
| 94 | case DrmPlugin::kDrmPluginEventKeyExpired: |
| 95 | ndkEventType = EVENT_KEY_EXPIRED; |
| 96 | break; |
| 97 | case DrmPlugin::kDrmPluginEventVendorDefined: |
| 98 | ndkEventType = EVENT_VENDOR_DEFINED; |
| 99 | break; |
| 100 | default: |
| 101 | ALOGE("Invalid event DrmPlugin::EventType %d, ignored", (int)eventType); |
Greg Kaiser | b636524 | 2016-08-11 11:01:37 -0700 | [diff] [blame] | 102 | goto cleanup; |
Jeff Tinker | 3305b99 | 2014-05-14 18:39:25 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 105 | (*mListener)(mObj, &sessionId, ndkEventType, extra, data, dataSize); |
Jeff Tinker | 3305b99 | 2014-05-14 18:39:25 -0700 | [diff] [blame] | 106 | |
Greg Kaiser | b636524 | 2016-08-11 11:01:37 -0700 | [diff] [blame] | 107 | cleanup: |
Jeff Tinker | 3305b99 | 2014-05-14 18:39:25 -0700 | [diff] [blame] | 108 | delete [] sessionId.ptr; |
| 109 | delete [] data; |
| 110 | } |
| 111 | |
| 112 | |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 113 | extern "C" { |
| 114 | |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 115 | static media_status_t translateStatus(status_t status) { |
| 116 | media_status_t result = AMEDIA_ERROR_UNKNOWN; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 117 | switch (status) { |
| 118 | case OK: |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 119 | result = AMEDIA_OK; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 120 | break; |
| 121 | case android::ERROR_DRM_NOT_PROVISIONED: |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 122 | result = AMEDIA_DRM_NOT_PROVISIONED; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 123 | break; |
| 124 | case android::ERROR_DRM_RESOURCE_BUSY: |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 125 | result = AMEDIA_DRM_RESOURCE_BUSY; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 126 | break; |
| 127 | case android::ERROR_DRM_DEVICE_REVOKED: |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 128 | result = AMEDIA_DRM_DEVICE_REVOKED; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 129 | break; |
| 130 | case android::ERROR_DRM_CANNOT_HANDLE: |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 131 | result = AMEDIA_ERROR_INVALID_PARAMETER; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 132 | break; |
| 133 | case android::ERROR_DRM_TAMPER_DETECTED: |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 134 | result = AMEDIA_DRM_TAMPER_DETECTED; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 135 | break; |
| 136 | case android::ERROR_DRM_SESSION_NOT_OPENED: |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 137 | result = AMEDIA_DRM_SESSION_NOT_OPENED; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 138 | break; |
| 139 | case android::ERROR_DRM_NO_LICENSE: |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 140 | result = AMEDIA_DRM_NEED_KEY; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 141 | break; |
| 142 | case android::ERROR_DRM_LICENSE_EXPIRED: |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 143 | result = AMEDIA_DRM_LICENSE_EXPIRED; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 144 | break; |
| 145 | default: |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 146 | break; |
| 147 | } |
| 148 | return result; |
| 149 | } |
| 150 | |
| 151 | static sp<IDrm> CreateDrm() { |
| 152 | sp<IServiceManager> sm = defaultServiceManager(); |
Jeff Tinker | 3003807 | 2016-04-25 13:41:35 -0700 | [diff] [blame] | 153 | sp<IBinder> binder = sm->getService(String16("media.drm")); |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 154 | |
Jeff Tinker | 3003807 | 2016-04-25 13:41:35 -0700 | [diff] [blame] | 155 | sp<IMediaDrmService> service = interface_cast<IMediaDrmService>(binder); |
| 156 | if (service == NULL) { |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 157 | return NULL; |
| 158 | } |
| 159 | |
Jeff Tinker | 3003807 | 2016-04-25 13:41:35 -0700 | [diff] [blame] | 160 | sp<IDrm> drm = service->makeDrm(); |
| 161 | if (drm == NULL || (drm->initCheck() != OK && drm->initCheck() != NO_INIT)) { |
| 162 | return NULL; |
| 163 | } |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 164 | return drm; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | static sp<IDrm> CreateDrmFromUUID(const AMediaUUID uuid) { |
| 169 | sp<IDrm> drm = CreateDrm(); |
| 170 | |
| 171 | if (drm == NULL) { |
| 172 | return NULL; |
| 173 | } |
| 174 | |
| 175 | status_t err = drm->createPlugin(uuid); |
| 176 | |
| 177 | if (err != OK) { |
| 178 | return NULL; |
| 179 | } |
| 180 | |
| 181 | return drm; |
| 182 | } |
| 183 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 184 | EXPORT |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 185 | bool AMediaDrm_isCryptoSchemeSupported(const AMediaUUID uuid, const char *mimeType) { |
| 186 | sp<IDrm> drm = CreateDrm(); |
| 187 | |
| 188 | if (drm == NULL) { |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | String8 mimeStr = mimeType ? String8(mimeType) : String8(""); |
| 193 | return drm->isCryptoSchemeSupported(uuid, mimeStr); |
| 194 | } |
| 195 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 196 | EXPORT |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 197 | AMediaDrm* AMediaDrm_createByUUID(const AMediaUUID uuid) { |
| 198 | AMediaDrm *mObj = new AMediaDrm(); |
| 199 | mObj->mDrm = CreateDrmFromUUID(uuid); |
| 200 | return mObj; |
| 201 | } |
| 202 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 203 | EXPORT |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 204 | void AMediaDrm_release(AMediaDrm *mObj) { |
| 205 | if (mObj->mDrm != NULL) { |
| 206 | mObj->mDrm->setListener(NULL); |
| 207 | mObj->mDrm->destroyPlugin(); |
| 208 | mObj->mDrm.clear(); |
| 209 | } |
| 210 | delete mObj; |
| 211 | } |
| 212 | |
Jeff Tinker | 3305b99 | 2014-05-14 18:39:25 -0700 | [diff] [blame] | 213 | EXPORT |
Marco Nelissen | 7c96d53 | 2014-05-15 15:26:14 -0700 | [diff] [blame] | 214 | media_status_t AMediaDrm_setOnEventListener(AMediaDrm *mObj, AMediaDrmEventListener listener) { |
Jeff Tinker | 3305b99 | 2014-05-14 18:39:25 -0700 | [diff] [blame] | 215 | if (!mObj || mObj->mDrm == NULL) { |
Marco Nelissen | 7c96d53 | 2014-05-15 15:26:14 -0700 | [diff] [blame] | 216 | return AMEDIA_ERROR_INVALID_OBJECT; |
Jeff Tinker | 3305b99 | 2014-05-14 18:39:25 -0700 | [diff] [blame] | 217 | } |
| 218 | mObj->mListener = new DrmListener(mObj, listener); |
| 219 | mObj->mDrm->setListener(mObj->mListener); |
Marco Nelissen | 7c96d53 | 2014-05-15 15:26:14 -0700 | [diff] [blame] | 220 | return AMEDIA_OK; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 221 | } |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 222 | |
| 223 | |
| 224 | static bool findId(AMediaDrm *mObj, const AMediaDrmByteArray &id, List<idvec_t>::iterator &iter) { |
Edwin Wong | 2103044 | 2016-10-18 12:34:05 -0700 | [diff] [blame^] | 225 | for (iter = mObj->mIds.begin(); iter != mObj->mIds.end(); ++iter) { |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 226 | if (iter->array() == id.ptr && iter->size() == id.length) { |
| 227 | return true; |
| 228 | } |
| 229 | } |
| 230 | return false; |
| 231 | } |
| 232 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 233 | EXPORT |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 234 | media_status_t AMediaDrm_openSession(AMediaDrm *mObj, AMediaDrmSessionId *sessionId) { |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 235 | if (!mObj || mObj->mDrm == NULL) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 236 | return AMEDIA_ERROR_INVALID_OBJECT; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 237 | } |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 238 | if (!sessionId) { |
| 239 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 240 | } |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 241 | Vector<uint8_t> session; |
| 242 | status_t status = mObj->mDrm->openSession(session); |
| 243 | if (status == OK) { |
| 244 | mObj->mIds.push_front(session); |
| 245 | List<idvec_t>::iterator iter = mObj->mIds.begin(); |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 246 | sessionId->ptr = iter->array(); |
| 247 | sessionId->length = iter->size(); |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 248 | } |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 249 | return AMEDIA_OK; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 252 | EXPORT |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 253 | media_status_t AMediaDrm_closeSession(AMediaDrm *mObj, const AMediaDrmSessionId *sessionId) { |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 254 | if (!mObj || mObj->mDrm == NULL) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 255 | return AMEDIA_ERROR_INVALID_OBJECT; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 256 | } |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 257 | if (!sessionId) { |
| 258 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 259 | } |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 260 | |
| 261 | List<idvec_t>::iterator iter; |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 262 | if (!findId(mObj, *sessionId, iter)) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 263 | return AMEDIA_DRM_SESSION_NOT_OPENED; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 264 | } |
| 265 | mObj->mDrm->closeSession(*iter); |
| 266 | mObj->mIds.erase(iter); |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 267 | return AMEDIA_OK; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 270 | EXPORT |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 271 | media_status_t AMediaDrm_getKeyRequest(AMediaDrm *mObj, const AMediaDrmScope *scope, |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 272 | const uint8_t *init, size_t initSize, const char *mimeType, AMediaDrmKeyType keyType, |
| 273 | const AMediaDrmKeyValue *optionalParameters, size_t numOptionalParameters, |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 274 | const uint8_t **keyRequest, size_t *keyRequestSize) { |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 275 | |
| 276 | if (!mObj || mObj->mDrm == NULL) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 277 | return AMEDIA_ERROR_INVALID_OBJECT; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 278 | } |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 279 | if (!mimeType || !scope || !keyRequest || !keyRequestSize) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 280 | return AMEDIA_ERROR_INVALID_PARAMETER; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | List<idvec_t>::iterator iter; |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 284 | if (!findId(mObj, *scope, iter)) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 285 | return AMEDIA_DRM_SESSION_NOT_OPENED; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | Vector<uint8_t> mdInit; |
| 289 | mdInit.appendArray(init, initSize); |
| 290 | DrmPlugin::KeyType mdKeyType; |
| 291 | switch (keyType) { |
| 292 | case KEY_TYPE_STREAMING: |
| 293 | mdKeyType = DrmPlugin::kKeyType_Streaming; |
| 294 | break; |
| 295 | case KEY_TYPE_OFFLINE: |
| 296 | mdKeyType = DrmPlugin::kKeyType_Offline; |
| 297 | break; |
| 298 | case KEY_TYPE_RELEASE: |
| 299 | mdKeyType = DrmPlugin::kKeyType_Release; |
| 300 | break; |
| 301 | default: |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 302 | return AMEDIA_ERROR_INVALID_PARAMETER; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 303 | } |
| 304 | KeyedVector<String8, String8> mdOptionalParameters; |
| 305 | for (size_t i = 0; i < numOptionalParameters; i++) { |
| 306 | mdOptionalParameters.add(String8(optionalParameters[i].mKey), |
| 307 | String8(optionalParameters[i].mValue)); |
| 308 | } |
| 309 | String8 defaultUrl; |
Jeff Tinker | d072c90 | 2015-03-16 13:39:29 -0700 | [diff] [blame] | 310 | DrmPlugin::KeyRequestType keyRequestType; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 311 | status_t status = mObj->mDrm->getKeyRequest(*iter, mdInit, String8(mimeType), |
Jeff Tinker | d072c90 | 2015-03-16 13:39:29 -0700 | [diff] [blame] | 312 | mdKeyType, mdOptionalParameters, mObj->mKeyRequest, defaultUrl, |
| 313 | &keyRequestType); |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 314 | if (status != OK) { |
| 315 | return translateStatus(status); |
| 316 | } else { |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 317 | *keyRequest = mObj->mKeyRequest.array(); |
| 318 | *keyRequestSize = mObj->mKeyRequest.size(); |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 319 | } |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 320 | return AMEDIA_OK; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 323 | EXPORT |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 324 | media_status_t AMediaDrm_provideKeyResponse(AMediaDrm *mObj, const AMediaDrmScope *scope, |
| 325 | const uint8_t *response, size_t responseSize, AMediaDrmKeySetId *keySetId) { |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 326 | |
| 327 | if (!mObj || mObj->mDrm == NULL) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 328 | return AMEDIA_ERROR_INVALID_OBJECT; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 329 | } |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 330 | if (!scope || !response || !responseSize || !keySetId) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 331 | return AMEDIA_ERROR_INVALID_PARAMETER; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | List<idvec_t>::iterator iter; |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 335 | if (!findId(mObj, *scope, iter)) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 336 | return AMEDIA_DRM_SESSION_NOT_OPENED; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 337 | } |
| 338 | Vector<uint8_t> mdResponse; |
| 339 | mdResponse.appendArray(response, responseSize); |
| 340 | |
| 341 | Vector<uint8_t> mdKeySetId; |
| 342 | status_t status = mObj->mDrm->provideKeyResponse(*iter, mdResponse, mdKeySetId); |
| 343 | if (status == OK) { |
| 344 | mObj->mIds.push_front(mdKeySetId); |
| 345 | List<idvec_t>::iterator iter = mObj->mIds.begin(); |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 346 | keySetId->ptr = iter->array(); |
| 347 | keySetId->length = iter->size(); |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 348 | } else { |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 349 | keySetId->ptr = NULL; |
| 350 | keySetId->length = 0; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 351 | } |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 352 | return AMEDIA_OK; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 355 | EXPORT |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 356 | media_status_t AMediaDrm_restoreKeys(AMediaDrm *mObj, const AMediaDrmSessionId *sessionId, |
| 357 | const AMediaDrmKeySetId *keySetId) { |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 358 | |
| 359 | if (!mObj || mObj->mDrm == NULL) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 360 | return AMEDIA_ERROR_INVALID_OBJECT; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 361 | } |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 362 | if (!sessionId || !keySetId) { |
| 363 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 364 | } |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 365 | List<idvec_t>::iterator iter; |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 366 | if (!findId(mObj, *sessionId, iter)) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 367 | return AMEDIA_DRM_SESSION_NOT_OPENED; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 368 | } |
| 369 | Vector<uint8_t> keySet; |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 370 | keySet.appendArray(keySetId->ptr, keySetId->length); |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 371 | return translateStatus(mObj->mDrm->restoreKeys(*iter, keySet)); |
| 372 | } |
| 373 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 374 | EXPORT |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 375 | media_status_t AMediaDrm_removeKeys(AMediaDrm *mObj, const AMediaDrmSessionId *keySetId) { |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 376 | if (!mObj || mObj->mDrm == NULL) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 377 | return AMEDIA_ERROR_INVALID_OBJECT; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 378 | } |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 379 | if (!keySetId) { |
| 380 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 381 | } |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 382 | List<idvec_t>::iterator iter; |
| 383 | status_t status; |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 384 | if (!findId(mObj, *keySetId, iter)) { |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 385 | Vector<uint8_t> keySet; |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 386 | keySet.appendArray(keySetId->ptr, keySetId->length); |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 387 | status = mObj->mDrm->removeKeys(keySet); |
| 388 | } else { |
| 389 | status = mObj->mDrm->removeKeys(*iter); |
| 390 | mObj->mIds.erase(iter); |
| 391 | } |
| 392 | return translateStatus(status); |
| 393 | } |
| 394 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 395 | EXPORT |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 396 | media_status_t AMediaDrm_queryKeyStatus(AMediaDrm *mObj, const AMediaDrmSessionId *sessionId, |
| 397 | AMediaDrmKeyValue *keyValuePairs, size_t *numPairs) { |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 398 | |
| 399 | if (!mObj || mObj->mDrm == NULL) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 400 | return AMEDIA_ERROR_INVALID_OBJECT; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 401 | } |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 402 | if (!sessionId || !numPairs) { |
| 403 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 404 | } |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 405 | List<idvec_t>::iterator iter; |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 406 | if (!findId(mObj, *sessionId, iter)) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 407 | return AMEDIA_DRM_SESSION_NOT_OPENED; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | status_t status = mObj->mDrm->queryKeyStatus(*iter, mObj->mQueryResults); |
| 411 | if (status != OK) { |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 412 | *numPairs = 0; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 413 | return translateStatus(status); |
| 414 | } |
| 415 | |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 416 | if (mObj->mQueryResults.size() > *numPairs) { |
| 417 | *numPairs = mObj->mQueryResults.size(); |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 418 | return AMEDIA_DRM_SHORT_BUFFER; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | for (size_t i = 0; i < mObj->mQueryResults.size(); i++) { |
| 422 | keyValuePairs[i].mKey = mObj->mQueryResults.keyAt(i).string(); |
| 423 | keyValuePairs[i].mValue = mObj->mQueryResults.keyAt(i).string(); |
| 424 | } |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 425 | *numPairs = mObj->mQueryResults.size(); |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 426 | return AMEDIA_OK; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 429 | EXPORT |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 430 | media_status_t AMediaDrm_getProvisionRequest(AMediaDrm *mObj, const uint8_t **provisionRequest, |
| 431 | size_t *provisionRequestSize, const char **serverUrl) { |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 432 | if (!mObj || mObj->mDrm == NULL) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 433 | return AMEDIA_ERROR_INVALID_OBJECT; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 434 | } |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 435 | if (!provisionRequest || !provisionRequestSize || !*provisionRequestSize || !serverUrl) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 436 | return AMEDIA_ERROR_INVALID_PARAMETER; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | status_t status = mObj->mDrm->getProvisionRequest(String8(""), String8(""), |
| 440 | mObj->mProvisionRequest, mObj->mProvisionUrl); |
| 441 | if (status != OK) { |
| 442 | return translateStatus(status); |
| 443 | } else { |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 444 | *provisionRequest = mObj->mProvisionRequest.array(); |
| 445 | *provisionRequestSize = mObj->mProvisionRequest.size(); |
| 446 | *serverUrl = mObj->mProvisionUrl.string(); |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 447 | } |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 448 | return AMEDIA_OK; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 451 | EXPORT |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 452 | media_status_t AMediaDrm_provideProvisionResponse(AMediaDrm *mObj, |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 453 | const uint8_t *response, size_t responseSize) { |
| 454 | if (!mObj || mObj->mDrm == NULL) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 455 | return AMEDIA_ERROR_INVALID_OBJECT; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 456 | } |
| 457 | if (!response || !responseSize) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 458 | return AMEDIA_ERROR_INVALID_PARAMETER; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | Vector<uint8_t> mdResponse; |
| 462 | mdResponse.appendArray(response, responseSize); |
| 463 | |
| 464 | Vector<uint8_t> unused; |
| 465 | return translateStatus(mObj->mDrm->provideProvisionResponse(mdResponse, unused, unused)); |
| 466 | } |
| 467 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 468 | EXPORT |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 469 | media_status_t AMediaDrm_getSecureStops(AMediaDrm *mObj, |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 470 | AMediaDrmSecureStop *secureStops, size_t *numSecureStops) { |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 471 | |
| 472 | if (!mObj || mObj->mDrm == NULL) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 473 | return AMEDIA_ERROR_INVALID_OBJECT; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 474 | } |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 475 | if (!numSecureStops) { |
| 476 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 477 | } |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 478 | status_t status = mObj->mDrm->getSecureStops(mObj->mSecureStops); |
| 479 | if (status != OK) { |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 480 | *numSecureStops = 0; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 481 | return translateStatus(status); |
| 482 | } |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 483 | if (*numSecureStops < mObj->mSecureStops.size()) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 484 | return AMEDIA_DRM_SHORT_BUFFER; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 485 | } |
| 486 | List<Vector<uint8_t> >::iterator iter = mObj->mSecureStops.begin(); |
| 487 | size_t i = 0; |
| 488 | while (iter != mObj->mSecureStops.end()) { |
| 489 | secureStops[i].ptr = iter->array(); |
| 490 | secureStops[i].length = iter->size(); |
| 491 | ++iter; |
| 492 | ++i; |
| 493 | } |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 494 | *numSecureStops = mObj->mSecureStops.size(); |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 495 | return AMEDIA_OK; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 498 | EXPORT |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 499 | media_status_t AMediaDrm_releaseSecureStops(AMediaDrm *mObj, |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 500 | const AMediaDrmSecureStop *ssRelease) { |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 501 | |
| 502 | if (!mObj || mObj->mDrm == NULL) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 503 | return AMEDIA_ERROR_INVALID_OBJECT; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 504 | } |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 505 | if (!ssRelease) { |
| 506 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 507 | } |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 508 | |
| 509 | Vector<uint8_t> release; |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 510 | release.appendArray(ssRelease->ptr, ssRelease->length); |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 511 | return translateStatus(mObj->mDrm->releaseSecureStops(release)); |
| 512 | } |
| 513 | |
| 514 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 515 | EXPORT |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 516 | media_status_t AMediaDrm_getPropertyString(AMediaDrm *mObj, const char *propertyName, |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 517 | const char **propertyValue) { |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 518 | |
| 519 | if (!mObj || mObj->mDrm == NULL) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 520 | return AMEDIA_ERROR_INVALID_OBJECT; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 521 | } |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 522 | if (!propertyName || !propertyValue) { |
| 523 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 524 | } |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 525 | |
| 526 | status_t status = mObj->mDrm->getPropertyString(String8(propertyName), |
| 527 | mObj->mPropertyString); |
| 528 | |
| 529 | if (status == OK) { |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 530 | *propertyValue = mObj->mPropertyString.string(); |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 531 | } else { |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 532 | *propertyValue = NULL; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 533 | } |
| 534 | return translateStatus(status); |
| 535 | } |
| 536 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 537 | EXPORT |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 538 | media_status_t AMediaDrm_getPropertyByteArray(AMediaDrm *mObj, |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 539 | const char *propertyName, AMediaDrmByteArray *propertyValue) { |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 540 | if (!mObj || mObj->mDrm == NULL) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 541 | return AMEDIA_ERROR_INVALID_OBJECT; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 542 | } |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 543 | if (!propertyName || !propertyValue) { |
| 544 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 545 | } |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 546 | |
| 547 | status_t status = mObj->mDrm->getPropertyByteArray(String8(propertyName), |
| 548 | mObj->mPropertyByteArray); |
| 549 | |
| 550 | if (status == OK) { |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 551 | propertyValue->ptr = mObj->mPropertyByteArray.array(); |
| 552 | propertyValue->length = mObj->mPropertyByteArray.size(); |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 553 | } else { |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 554 | propertyValue->ptr = NULL; |
| 555 | propertyValue->length = 0; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 556 | } |
| 557 | return translateStatus(status); |
| 558 | } |
| 559 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 560 | EXPORT |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 561 | media_status_t AMediaDrm_setPropertyString(AMediaDrm *mObj, |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 562 | const char *propertyName, const char *value) { |
| 563 | if (!mObj || mObj->mDrm == NULL) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 564 | return AMEDIA_ERROR_INVALID_OBJECT; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | return translateStatus(mObj->mDrm->setPropertyString(String8(propertyName), |
| 568 | String8(value))); |
| 569 | } |
| 570 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 571 | EXPORT |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 572 | media_status_t AMediaDrm_setPropertyByteArray(AMediaDrm *mObj, |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 573 | const char *propertyName, const uint8_t *value, size_t valueSize) { |
| 574 | |
| 575 | Vector<uint8_t> byteArray; |
| 576 | byteArray.appendArray(value, valueSize); |
| 577 | |
| 578 | return translateStatus(mObj->mDrm->getPropertyByteArray(String8(propertyName), |
| 579 | byteArray)); |
| 580 | } |
| 581 | |
| 582 | |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 583 | static media_status_t encrypt_decrypt_common(AMediaDrm *mObj, |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 584 | const AMediaDrmSessionId &sessionId, |
| 585 | const char *cipherAlgorithm, uint8_t *keyId, uint8_t *iv, |
| 586 | const uint8_t *input, uint8_t *output, size_t dataSize, bool encrypt) { |
| 587 | |
| 588 | if (!mObj || mObj->mDrm == NULL) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 589 | return AMEDIA_ERROR_INVALID_OBJECT; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 590 | } |
| 591 | List<idvec_t>::iterator iter; |
| 592 | if (!findId(mObj, sessionId, iter)) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 593 | return AMEDIA_DRM_SESSION_NOT_OPENED; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | status_t status = mObj->mDrm->setCipherAlgorithm(*iter, String8(cipherAlgorithm)); |
| 597 | if (status != OK) { |
| 598 | return translateStatus(status); |
| 599 | } |
| 600 | |
| 601 | Vector<uint8_t> keyIdVec; |
| 602 | const size_t kKeyIdSize = 16; |
| 603 | keyIdVec.appendArray(keyId, kKeyIdSize); |
| 604 | |
| 605 | Vector<uint8_t> inputVec; |
| 606 | inputVec.appendArray(input, dataSize); |
| 607 | |
| 608 | Vector<uint8_t> ivVec; |
| 609 | const size_t kIvSize = 16; |
| 610 | ivVec.appendArray(iv, kIvSize); |
| 611 | |
| 612 | Vector<uint8_t> outputVec; |
| 613 | if (encrypt) { |
Aurimas Liutikas | 214c833 | 2016-02-19 14:48:23 -0800 | [diff] [blame] | 614 | status = mObj->mDrm->encrypt(*iter, keyIdVec, inputVec, ivVec, outputVec); |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 615 | } else { |
Aurimas Liutikas | 214c833 | 2016-02-19 14:48:23 -0800 | [diff] [blame] | 616 | status = mObj->mDrm->decrypt(*iter, keyIdVec, inputVec, ivVec, outputVec); |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 617 | } |
| 618 | if (status == OK) { |
| 619 | memcpy(output, outputVec.array(), outputVec.size()); |
| 620 | } |
| 621 | return translateStatus(status); |
| 622 | } |
| 623 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 624 | EXPORT |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 625 | media_status_t AMediaDrm_encrypt(AMediaDrm *mObj, const AMediaDrmSessionId *sessionId, |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 626 | const char *cipherAlgorithm, uint8_t *keyId, uint8_t *iv, |
| 627 | const uint8_t *input, uint8_t *output, size_t dataSize) { |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 628 | if (!sessionId) { |
| 629 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 630 | } |
| 631 | return encrypt_decrypt_common(mObj, *sessionId, cipherAlgorithm, keyId, iv, |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 632 | input, output, dataSize, true); |
| 633 | } |
| 634 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 635 | EXPORT |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 636 | media_status_t AMediaDrm_decrypt(AMediaDrm *mObj, const AMediaDrmSessionId *sessionId, |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 637 | const char *cipherAlgorithm, uint8_t *keyId, uint8_t *iv, |
| 638 | const uint8_t *input, uint8_t *output, size_t dataSize) { |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 639 | if (!sessionId) { |
| 640 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 641 | } |
| 642 | return encrypt_decrypt_common(mObj, *sessionId, cipherAlgorithm, keyId, iv, |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 643 | input, output, dataSize, false); |
| 644 | } |
| 645 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 646 | EXPORT |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 647 | media_status_t AMediaDrm_sign(AMediaDrm *mObj, const AMediaDrmSessionId *sessionId, |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 648 | const char *macAlgorithm, uint8_t *keyId, uint8_t *message, size_t messageSize, |
| 649 | uint8_t *signature, size_t *signatureSize) { |
| 650 | |
| 651 | if (!mObj || mObj->mDrm == NULL) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 652 | return AMEDIA_ERROR_INVALID_OBJECT; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 653 | } |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 654 | if (!sessionId) { |
| 655 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 656 | } |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 657 | List<idvec_t>::iterator iter; |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 658 | if (!findId(mObj, *sessionId, iter)) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 659 | return AMEDIA_DRM_SESSION_NOT_OPENED; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | status_t status = mObj->mDrm->setMacAlgorithm(*iter, String8(macAlgorithm)); |
| 663 | if (status != OK) { |
| 664 | return translateStatus(status); |
| 665 | } |
| 666 | |
| 667 | Vector<uint8_t> keyIdVec; |
| 668 | const size_t kKeyIdSize = 16; |
| 669 | keyIdVec.appendArray(keyId, kKeyIdSize); |
| 670 | |
| 671 | Vector<uint8_t> messageVec; |
| 672 | messageVec.appendArray(message, messageSize); |
| 673 | |
| 674 | Vector<uint8_t> signatureVec; |
| 675 | status = mObj->mDrm->sign(*iter, keyIdVec, messageVec, signatureVec); |
| 676 | if (signatureVec.size() > *signatureSize) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 677 | return AMEDIA_DRM_SHORT_BUFFER; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 678 | } |
| 679 | if (status == OK) { |
| 680 | memcpy(signature, signatureVec.array(), signatureVec.size()); |
| 681 | } |
| 682 | return translateStatus(status); |
| 683 | } |
| 684 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 685 | EXPORT |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 686 | media_status_t AMediaDrm_verify(AMediaDrm *mObj, const AMediaDrmSessionId *sessionId, |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 687 | const char *macAlgorithm, uint8_t *keyId, const uint8_t *message, size_t messageSize, |
| 688 | const uint8_t *signature, size_t signatureSize) { |
| 689 | |
| 690 | if (!mObj || mObj->mDrm == NULL) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 691 | return AMEDIA_ERROR_INVALID_OBJECT; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 692 | } |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 693 | if (!sessionId) { |
| 694 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 695 | } |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 696 | List<idvec_t>::iterator iter; |
Marco Nelissen | 18a1b59 | 2014-05-20 08:45:18 -0700 | [diff] [blame] | 697 | if (!findId(mObj, *sessionId, iter)) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 698 | return AMEDIA_DRM_SESSION_NOT_OPENED; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | status_t status = mObj->mDrm->setMacAlgorithm(*iter, String8(macAlgorithm)); |
| 702 | if (status != OK) { |
| 703 | return translateStatus(status); |
| 704 | } |
| 705 | |
| 706 | Vector<uint8_t> keyIdVec; |
| 707 | const size_t kKeyIdSize = 16; |
| 708 | keyIdVec.appendArray(keyId, kKeyIdSize); |
| 709 | |
| 710 | Vector<uint8_t> messageVec; |
| 711 | messageVec.appendArray(message, messageSize); |
| 712 | |
| 713 | Vector<uint8_t> signatureVec; |
| 714 | signatureVec.appendArray(signature, signatureSize); |
| 715 | |
| 716 | bool match; |
| 717 | status = mObj->mDrm->verify(*iter, keyIdVec, messageVec, signatureVec, match); |
| 718 | if (status == OK) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 719 | return match ? AMEDIA_OK : AMEDIA_DRM_VERIFY_FAILED; |
Jeff Tinker | 497ca09 | 2014-05-13 09:31:15 -0700 | [diff] [blame] | 720 | } |
| 721 | return translateStatus(status); |
| 722 | } |
| 723 | |
| 724 | } // extern "C" |