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