1 /*
2 * Copyright 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 "MediaCrypto-JNI"
19 #include <utils/Log.h>
20
21 #include "android_media_MediaCrypto.h"
22
23 #include "android_runtime/AndroidRuntime.h"
24 #include "jni.h"
25 #include <nativehelper/JNIHelp.h>
26
27 #include <cutils/properties.h>
28 #include <media/stagefright/foundation/ADebug.h>
29 #include <mediadrm/DrmUtils.h>
30 #include <mediadrm/ICrypto.h>
31
32 namespace android {
33
34 struct fields_t {
35 jfieldID context;
36 };
37
38 static fields_t gFields;
39
getCrypto(JNIEnv * env,jobject thiz)40 static sp<JCrypto> getCrypto(JNIEnv *env, jobject thiz) {
41 return (JCrypto *)env->GetLongField(thiz, gFields.context);
42 }
43
JCrypto(JNIEnv * env,jobject thiz,const uint8_t uuid[16],const void * initData,size_t initSize)44 JCrypto::JCrypto(
45 JNIEnv *env, jobject thiz,
46 const uint8_t uuid[16], const void *initData, size_t initSize) {
47 mObject = env->NewWeakGlobalRef(thiz);
48
49 mCrypto = MakeCrypto(uuid, initData, initSize);
50 }
51
~JCrypto()52 JCrypto::~JCrypto() {
53 if (mCrypto != NULL) {
54 mCrypto->destroyPlugin();
55 }
56 mCrypto.clear();
57
58 JNIEnv *env = AndroidRuntime::getJNIEnv();
59
60 env->DeleteWeakGlobalRef(mObject);
61 mObject = NULL;
62 }
63
64 // static
MakeCrypto()65 sp<ICrypto> JCrypto::MakeCrypto() {
66 return DrmUtils::MakeCrypto();
67 }
68
69 // static
MakeCrypto(const uint8_t uuid[16],const void * initData,size_t initSize)70 sp<ICrypto> JCrypto::MakeCrypto(
71 const uint8_t uuid[16], const void *initData, size_t initSize) {
72 sp<ICrypto> crypto = MakeCrypto();
73
74 if (crypto == NULL) {
75 return NULL;
76 }
77
78 status_t err = crypto->createPlugin(uuid, initData, initSize);
79
80 if (err != OK) {
81 return NULL;
82 }
83
84 return crypto;
85 }
86
requiresSecureDecoderComponent(const char * mime) const87 bool JCrypto::requiresSecureDecoderComponent(const char *mime) const {
88 if (mCrypto == NULL) {
89 return false;
90 }
91
92 return mCrypto->requiresSecureDecoderComponent(mime);
93 }
94
95 // static
IsCryptoSchemeSupported(const uint8_t uuid[16])96 bool JCrypto::IsCryptoSchemeSupported(const uint8_t uuid[16]) {
97 sp<ICrypto> crypto = MakeCrypto();
98
99 if (crypto == NULL) {
100 return false;
101 }
102
103 return crypto->isCryptoSchemeSupported(uuid);
104 }
105
initCheck() const106 status_t JCrypto::initCheck() const {
107 return mCrypto == NULL ? NO_INIT : OK;
108 }
109
110 // static
GetCrypto(JNIEnv * env,jobject obj)111 sp<ICrypto> JCrypto::GetCrypto(JNIEnv *env, jobject obj) {
112 jclass clazz = env->FindClass("android/media/MediaCrypto");
113 CHECK(clazz != NULL);
114
115 if (!env->IsInstanceOf(obj, clazz)) {
116 return NULL;
117 }
118
119 sp<JCrypto> jcrypto = getCrypto(env, obj);
120
121 if (jcrypto == NULL) {
122 return NULL;
123 }
124
125 return jcrypto->mCrypto;
126 }
127
128 // JNI conversion utilities
JByteArrayToVector(JNIEnv * env,jbyteArray const & byteArray)129 static Vector<uint8_t> JByteArrayToVector(JNIEnv *env, jbyteArray const &byteArray) {
130 Vector<uint8_t> vector;
131 size_t length = env->GetArrayLength(byteArray);
132 vector.insertAt((size_t)0, length);
133 env->GetByteArrayRegion(byteArray, 0, length, (jbyte *)vector.editArray());
134 return vector;
135 }
136
137 } // namespace android
138
139 using namespace android;
140
setCrypto(JNIEnv * env,jobject thiz,const sp<JCrypto> & crypto)141 static sp<JCrypto> setCrypto(
142 JNIEnv *env, jobject thiz, const sp<JCrypto> &crypto) {
143 sp<JCrypto> old = (JCrypto *)env->GetLongField(thiz, gFields.context);
144 if (crypto != NULL) {
145 crypto->incStrong(thiz);
146 }
147 if (old != NULL) {
148 old->decStrong(thiz);
149 }
150 env->SetLongField(thiz, gFields.context, (jlong)crypto.get());
151
152 return old;
153 }
154
android_media_MediaCrypto_release(JNIEnv * env,jobject thiz)155 static void android_media_MediaCrypto_release(JNIEnv *env, jobject thiz) {
156 setCrypto(env, thiz, NULL);
157 }
158
android_media_MediaCrypto_native_init(JNIEnv * env)159 static void android_media_MediaCrypto_native_init(JNIEnv *env) {
160 jclass clazz = env->FindClass("android/media/MediaCrypto");
161 CHECK(clazz != NULL);
162
163 gFields.context = env->GetFieldID(clazz, "mNativeContext", "J");
164 CHECK(gFields.context != NULL);
165 }
166
android_media_MediaCrypto_native_setup(JNIEnv * env,jobject thiz,jbyteArray uuidObj,jbyteArray initDataObj)167 static void android_media_MediaCrypto_native_setup(
168 JNIEnv *env, jobject thiz,
169 jbyteArray uuidObj, jbyteArray initDataObj) {
170 jsize uuidLength = env->GetArrayLength(uuidObj);
171
172 if (uuidLength != 16) {
173 jniThrowException(
174 env,
175 "java/lang/IllegalArgumentException",
176 NULL);
177 return;
178 }
179
180 jboolean isCopy;
181 jbyte *uuid = env->GetByteArrayElements(uuidObj, &isCopy);
182
183 jsize initDataLength = 0;
184 jbyte *initData = NULL;
185
186 if (initDataObj != NULL) {
187 initDataLength = env->GetArrayLength(initDataObj);
188 initData = env->GetByteArrayElements(initDataObj, &isCopy);
189 }
190
191 sp<JCrypto> crypto = new JCrypto(
192 env, thiz, (const uint8_t *)uuid, initData, initDataLength);
193
194 status_t err = crypto->initCheck();
195
196 if (initDataObj != NULL) {
197 env->ReleaseByteArrayElements(initDataObj, initData, 0);
198 initData = NULL;
199 }
200
201 env->ReleaseByteArrayElements(uuidObj, uuid, 0);
202 uuid = NULL;
203
204 if (err != OK) {
205 std::string strerr(StrCryptoError(err));
206 jniThrowExceptionFmt(
207 env,
208 "android/media/MediaCryptoException",
209 "Failed to instantiate crypto object: %s", strerr.c_str());
210 return;
211 }
212
213 setCrypto(env,thiz, crypto);
214 }
215
android_media_MediaCrypto_native_finalize(JNIEnv * env,jobject thiz)216 static void android_media_MediaCrypto_native_finalize(
217 JNIEnv *env, jobject thiz) {
218 android_media_MediaCrypto_release(env, thiz);
219 }
220
android_media_MediaCrypto_isCryptoSchemeSupportedNative(JNIEnv * env,jobject,jbyteArray uuidObj)221 static jboolean android_media_MediaCrypto_isCryptoSchemeSupportedNative(
222 JNIEnv *env, jobject /* thiz */, jbyteArray uuidObj) {
223 jsize uuidLength = env->GetArrayLength(uuidObj);
224
225 if (uuidLength != 16) {
226 jniThrowException(
227 env,
228 "java/lang/IllegalArgumentException",
229 NULL);
230 return JNI_FALSE;
231 }
232
233 jboolean isCopy;
234 jbyte *uuid = env->GetByteArrayElements(uuidObj, &isCopy);
235
236 bool result = JCrypto::IsCryptoSchemeSupported((const uint8_t *)uuid);
237
238 env->ReleaseByteArrayElements(uuidObj, uuid, 0);
239 uuid = NULL;
240
241 return result ? JNI_TRUE : JNI_FALSE;
242 }
243
android_media_MediaCrypto_requiresSecureDecoderComponent(JNIEnv * env,jobject thiz,jstring mimeObj)244 static jboolean android_media_MediaCrypto_requiresSecureDecoderComponent(
245 JNIEnv *env, jobject thiz, jstring mimeObj) {
246 if (mimeObj == NULL) {
247 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
248 return JNI_FALSE;
249 }
250
251 sp<JCrypto> crypto = getCrypto(env, thiz);
252
253 if (crypto == NULL) {
254 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
255 return JNI_FALSE;
256 }
257
258 const char *mime = env->GetStringUTFChars(mimeObj, NULL);
259
260 if (mime == NULL) {
261 return JNI_FALSE;
262 }
263
264 bool result = crypto->requiresSecureDecoderComponent(mime);
265
266 env->ReleaseStringUTFChars(mimeObj, mime);
267 mime = NULL;
268
269 return result ? JNI_TRUE : JNI_FALSE;
270 }
271
android_media_MediaCrypto_setMediaDrmSession(JNIEnv * env,jobject thiz,jbyteArray jsessionId)272 static void android_media_MediaCrypto_setMediaDrmSession(
273 JNIEnv *env, jobject thiz, jbyteArray jsessionId) {
274 if (jsessionId == NULL) {
275 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
276 return;
277 }
278
279 sp<ICrypto> crypto = JCrypto::GetCrypto(env, thiz);
280
281 if (crypto == NULL) {
282 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
283 return;
284 }
285
286 Vector<uint8_t> sessionId(JByteArrayToVector(env, jsessionId));
287
288 status_t err = crypto->setMediaDrmSession(sessionId);
289
290 if (err != OK) {
291 String8 msg("setMediaDrmSession failed");
292 if (err == ERROR_DRM_SESSION_NOT_OPENED) {
293 msg += ": session not opened";
294 } else if (err == ERROR_UNSUPPORTED) {
295 msg += ": not supported by this crypto scheme";
296 } else if (err == NO_INIT) {
297 msg += ": crypto plugin not initialized";
298 } else {
299 std::string strerr(StrCryptoError(err));
300 msg.appendFormat(": general failure (%s)", strerr.c_str());
301 }
302 jniThrowException(env, "android/media/MediaCryptoException", msg.string());
303 }
304 }
305
306 static const JNINativeMethod gMethods[] = {
307 { "release", "()V", (void *)android_media_MediaCrypto_release },
308 { "native_init", "()V", (void *)android_media_MediaCrypto_native_init },
309
310 { "native_setup", "([B[B)V",
311 (void *)android_media_MediaCrypto_native_setup },
312
313 { "native_finalize", "()V",
314 (void *)android_media_MediaCrypto_native_finalize },
315
316 { "isCryptoSchemeSupportedNative", "([B)Z",
317 (void *)android_media_MediaCrypto_isCryptoSchemeSupportedNative },
318
319 { "requiresSecureDecoderComponent", "(Ljava/lang/String;)Z",
320 (void *)android_media_MediaCrypto_requiresSecureDecoderComponent },
321
322 { "setMediaDrmSession", "([B)V",
323 (void *)android_media_MediaCrypto_setMediaDrmSession },
324 };
325
register_android_media_Crypto(JNIEnv * env)326 int register_android_media_Crypto(JNIEnv *env) {
327 return AndroidRuntime::registerNativeMethods(env,
328 "android/media/MediaCrypto", gMethods, NELEM(gMethods));
329 }
330
331