1 /* 2 * Copyright 2013, 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 #ifndef _ANDROID_MEDIA_DRM_H_ 18 #define _ANDROID_MEDIA_DRM_H_ 19 20 #include "jni.h" 21 22 #include <media/stagefright/foundation/ABase.h> 23 #include <mediadrm/IDrm.h> 24 #include <mediadrm/IDrmClient.h> 25 #include <hidl/HidlSupport.h> 26 #include <utils/Errors.h> 27 #include <utils/RefBase.h> 28 29 namespace { 30 31 enum { 32 // TODO(b/180483929): use reverse jni e.g. android_media_MediaDrm_native_init 33 // KEEP IN SYNC with MediaDrm$ErrorCodes in MediaDrm.java! 34 JERROR_DRM_UNKNOWN = 0, 35 JERROR_DRM_NO_LICENSE = 1, 36 JERROR_DRM_LICENSE_EXPIRED = 2, 37 JERROR_DRM_RESOURCE_BUSY = 3, 38 JERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION = 4, 39 JERROR_DRM_SESSION_NOT_OPENED = 5, 40 JERROR_DRM_CANNOT_HANDLE = 6, 41 JERROR_DRM_INSUFFICIENT_SECURITY = 7, 42 JERROR_DRM_FRAME_TOO_LARGE = 8, 43 JERROR_DRM_SESSION_LOST_STATE = 9, 44 JERROR_DRM_CERTIFICATE_MALFORMED = 10, 45 JERROR_DRM_CERTIFICATE_MISSING = 11, 46 JERROR_DRM_CRYPTO_LIBRARY = 12, 47 JERROR_DRM_GENERIC_OEM = 13, 48 JERROR_DRM_GENERIC_PLUGIN = 14, 49 JERROR_DRM_INIT_DATA = 15, 50 JERROR_DRM_KEY_NOT_LOADED = 16, 51 JERROR_DRM_LICENSE_PARSE = 17, 52 JERROR_DRM_LICENSE_POLICY = 18, 53 JERROR_DRM_LICENSE_RELEASE = 19, 54 JERROR_DRM_LICENSE_REQUEST_REJECTED = 20, 55 JERROR_DRM_LICENSE_RESTORE = 21, 56 JERROR_DRM_LICENSE_STATE = 22, 57 JERROR_DRM_MEDIA_FRAMEWORK = 23, 58 JERROR_DRM_PROVISIONING_CERTIFICATE = 24, 59 JERROR_DRM_PROVISIONING_CONFIG = 25, 60 JERROR_DRM_PROVISIONING_PARSE = 26, 61 JERROR_DRM_PROVISIONING_REQUEST_REJECTED = 27, 62 JERROR_DRM_PROVISIONING_RETRY = 28, 63 JERROR_DRM_RESOURCE_CONTENTION = 29, 64 JERROR_DRM_SECURE_STOP_RELEASE = 30, 65 JERROR_DRM_STORAGE_READ = 31, 66 JERROR_DRM_STORAGE_WRITE = 32, 67 JERROR_DRM_ZERO_SUBSAMPLES = 33, 68 }; 69 70 struct ListenerArgs { 71 jbyteArray jSessionId; 72 jbyteArray jData; 73 jlong jExpirationTime; 74 jobject jKeyStatusList; 75 jboolean jHasNewUsableKey; 76 }; 77 78 } 79 80 namespace android { 81 82 class DrmListener: virtual public RefBase 83 { 84 public: 85 virtual void notify(DrmPlugin::EventType eventType, int extra, 86 const ListenerArgs *args) = 0; 87 }; 88 89 struct JDrm : public IDrmClient { 90 static status_t IsCryptoSchemeSupported(const uint8_t uuid[16], 91 const String8 &mimeType, 92 DrmPlugin::SecurityLevel level, 93 bool *isSupported); 94 95 JDrm(JNIEnv *env, jobject thiz, const uint8_t uuid[16], const String8 &appPackageName); 96 97 status_t initCheck() const; getDrmJDrm98 sp<IDrm> getDrm() { return mDrm; } 99 100 void sendEvent( 101 DrmPlugin::EventType eventType, 102 const hardware::hidl_vec<uint8_t> &sessionId, 103 const hardware::hidl_vec<uint8_t> &data) override; 104 105 void sendExpirationUpdate( 106 const hardware::hidl_vec<uint8_t> &sessionId, 107 int64_t expiryTimeInMS) override; 108 109 void sendKeysChange( 110 const hardware::hidl_vec<uint8_t> &sessionId, 111 const std::vector<DrmKeyStatus> &keyStatusList, 112 bool hasNewUsableKey) override; 113 114 void sendSessionLostState( 115 const hardware::hidl_vec<uint8_t> &sessionId) override; 116 117 status_t setListener(const sp<DrmListener>& listener); 118 119 void disconnect(); 120 121 protected: 122 virtual ~JDrm(); 123 124 private: 125 jweak mObject; 126 sp<IDrm> mDrm; 127 128 sp<DrmListener> mListener; 129 Mutex mNotifyLock; 130 Mutex mLock; 131 132 static sp<IDrm> MakeDrm(); 133 static sp<IDrm> MakeDrm(const uint8_t uuid[16], const String8 &appPackageName); 134 135 void notify(DrmPlugin::EventType, int extra, const ListenerArgs *args); 136 137 DISALLOW_EVIL_CONSTRUCTORS(JDrm); 138 }; 139 140 jint MediaErrorToJavaError(status_t err); 141 142 } // namespace android 143 144 #endif // _ANDROID_MEDIA_DRM_H_ 145